Model Solution: Semantics of Conditionals

Comp 317: Semantics of Programming Languages
Model Solution: Semantics of Conditionals
1. (a) Extend the syntax of SIMPLE with ternary conditionals.
Solution:
hExpi ::= (hBExpi)hExpi?:hBExpi
(b) Extend the semantics of SIMPLE by describing formally how ternary
conditionals are evaluated.
Solution: For all Boolean expressions B, Expressions E1 and E2 ,
and states S,
[[E1 ]]Exp (S) if [[B]]BExp (S)
[[(B) ? E1 : E2 ]]Exp (S) =
[[E2 ]]Exp (S) if not [[B]]BExp (S)
(c) Use your semantics to show that the assignment
’x
:=
(’y < 0) ? 0 : 1 ;
has the same semantics as the program
if
(’y < 0) { ’x := 0; } else { ’x := 1; } .
Solution: For any state S, we have [[’x := (’y < 0) ?
equal to
S[’x ← [[(’y < 0) ? 0 : 1]]Exp (S)] .
0 :
1 ;]]Pgm (S)
(1)
If [[’y < 0]]BExp (S), then this is equal to S[’x ← 0], and we also have
[[if (’y < 0) { ’x := 0; } else { ’x := 1; }]]Pgm (S)
=
[[’x := 0;]]Pgm (S)
= S[’x ← 0] .
If not [[’y < 0]]BExp (S), then (1) is equal to S[’x ← 1], and we also
have
[[if (’y < 0) { ’x := 0; } else { ’x := 1; }]]Pgm (S)
=
[[’x := 1;]]Pgm (S)
= S[’x ← 1] .
This shows that the two programs are semantically equivalent.
1
2. (a) Give a BNF specification of a syntactic category, hClauseListi, of
lists of clauses separated by ‘;;’, where each clause is of the form T
: P , where T is a boolean expression and P a program.
Solution:
hClauseListi ::= hBExpi:hPgmi
|
hBExpi:hPgmi ;; hClauseListi
(b) Extend the BNF specification of SIMPLE programs with multi-if
statements.
Solution:
hPgmi ::= multi-if hClauseListi end-if
(c) Use your answer to part (a) to define a function
[[CL]]ClauseList : State×State → State
by induction on the hClauseListi CL so that for all states S and S 0 ,
[[CL]]ClauseList (S, S 0 )
is the state that results from executing, in S 0 , all the programs in all
the clauses in CL whose corresponding tests evaluate to true in state
S.
Solution: For all Boolean expressions B, programs P , ClauseLists
CL, and states S and S 0 ,
[[P ]]Pgm (S 0 ) if [[B]]BExp (S)
[[B:P ]]ClauseList (S, S 0 ) =
S0
if not [[B]]BExp (S)
[[CL]]ClauseList (S, [[P ]]Pgm (S 0 )) if [[B]]BExp (S)
[[B:P ;; CL]]ClauseList (S, S 0 ) =
[[CL]]ClauseList (S, S 0 )
if not [[B]]BExp (S)
(d) Use the function defined in part (iii) to extend the semantics of SIMPLE to multiple-if statements by completing the following: for any
ClauseList CL,
[[multi-if CL end-if]]Pgm (S) = . . . .
Solution: . . . = [[CL]]ClauseList (S, S)
(e) Use your answers to parts (iii) and (iv) to show that for any boolean
expression T and programs P1 and P2 , the program
multi-if
T : P1
;;
!T : P2
has the same semantics as
if
(T ) { P1 } else { P2 } .
2
endif
Solution: For any state S, if [[T ]]BExp (S), then
[[multi-if T : P1 ;; !T : P2 end-if]]Pgm (S)
=
[[T : P1 ;; !T : P2 ]]ClauseList (S, S)
=
[[!T : P2 ]]ClauseList (S, [[P1 ]]Pgm (S))
=
[[P1 ]]Pgm (S)
=
[[if (T ) {P1 } else {P2 }]]Pgm (S)
where the second-last step uses the consequence that [[!T ]]BExp (S) is
false. Similarly, if [[T ]]BExp (S) is false, then
[[multi-if T : P1 ;; !T : P2 end-if]]Pgm (S)
=
[[T : P1 ;; !T : P2 ]]ClauseList (S, S)
=
[[!T : P2 ]]ClauseList (S, S)
=
[[P2 ]]Pgm (S)
=
[[if (T ) {P1 } else {P2 }]]Pgm (S)
Which shows the two programs are semantically equivalent.
3