Imperative Languages - McMaster University

Download Report

Transcript Imperative Languages - McMaster University

Sequence Control
Syntax and Semantics
Jian Xu
March 3, 2004
CS706, CAS McMaster
Imperative programming: describes
computation in terms of program state
and statements that change the state.
• Almost all hardware implementation is
imperative.
• The program state is the contents of memory,
and the statements are instructions.
• Higher-level imperative languages use
variables and more complex statements.
• Support assignment, looping, conditional
branching, and unconditional branching.
• FORTRAN, COBOL, Pascal, Ada
2
Unconditional branch
• FORTRAN
– GOTO 21
– ASSIGN 21 to K; GOTO K
– GOTO (n1, n2, …, np), I
• COBOL
– GOTO L1, L2, …, Ln DEPENDING ON , I.
• Pascal
– GOTO statement labels
• Ada
– GOTO statement
– exit & exit LOOP-LABEL
3
Syntax - Conditional branch
• FORTRAN
– IF expr nneg, nzero, npos.
– IF expr statement.
– IF expr THEN statements
ELSE IF expr THEN statements
… ELSE statements END IF
• COBOL
– IF expr statements ELSE statements
• Pascal
– if expr then statement.
• Ada
– if expr then statements
elsif expr then statements
… else statements end if;
4
Semantics – conditional branch
• Operational
 (b)  True  ( S 1)   1
 (if b then S 1 else S 2)   1
 (b)  False  ( S 2)   2
 (if b then S 1 else S 2)   2
• Denotational
if b then S 1 else S 2
• Axiomatic
 S1 
  
 S2 
if b   True
if b   False
{P  b} S 1 {Q} {P  b} S 2 {Q}
{P} if b then S 1 else S 2 {Q}
5
Semantics – conditional branch
• Operational
 (b1)  True  ( S 1)   1
 (if b1 then S 1 elseif b2 then S 2
else Sn)   1
 (b1)  False  (b2)  True  ( S 2)   2
 (if b1 then S 1 elseif b2 then S 2
else Sn)   2
 (b1)  False
 (bn  1)  False  ( Sn)   n
 (if b1 then S 1 elseif b2 then S 2
else Sn)  
• Denotational
if b1 then S 1 elseif b 2 then S 2
• Axiomatic
{P  b1} S 1 {Q}
 S1 
 S2 

else Sn endif  = 

 Sn 
if b1   True
if b1   False  b 2   True
if b1   False 
{P  b1 
 bn  2  bn  1} Sn  1 {Q} {P  b1 
{P} if b1 then S 1 elseif b2 then S 2
else Sn endif {Q}
 bn  1   False
 bn  1} Sn {Q}
6
Syntax - case
• Pascal
case expr of
const: statement;
…
const: statement
end
• Ada
case expr is
when choice => statement;
…
when others => statement
end
7
Semantics - case
• Operational
 (exp r  choice1)  True  ( S 1)   1
 (S )   1
 (exp r  choice1)  False
 (exp r  choicen  1)  False
 (S )   n
 ( Sn)   n
• Denotational
 S1 
 S2 

S  = 


 Sn 
if exp r  choice1   True
if exp r  choice1   False  exp r  choice 2   True
if exp r  choice1   False 
 exp r  choicen  1   False
• Axiomatic
{P  exp r  choice1} S 1 {Q}
 {P  (exp r  choice1) 
{P} S {Q}
 (exp r  choicen  1)} Sn {Q}
8
Semantics - case
• Operational
 (exp r  choice1)  True  ( S 1)   1
 (S )   1
 (exp r  choicen)  True
 (S )   n
 ( Sn)   n
• Denotational
 S1 
 S2 

S  = 

 Sn 
if exp r  choice1   True
if exp r  choice 2   True
if exp r  choicen   True
• Axiomatic
{P  exp r  choice1} S 1 {Q}
 {P  exp r  choicen} Sn {Q}
{P} S {Q}
9
Syntax - loops
• FORTRAN
Do label index = init-val, final-val, incr-val
• COBOL
PERFORM L1 THRU Ln k TIMES
PERFORM L1 THRU Ln UNTIL <condition>.
• Parcal
for index := init-val to final-val do statement
while expr do statement
repeat statement until expr
• Ada
loop statements end loop;
for index in range loop statement end loop;
while expr loop statement end loop;
10
Semantics – while loop
• Operational
 (b)  True  ( S )   1  1(while b do S )   2
 (while b do S )   2
 (b)  False
 (while b do S )  
• Denotational
while b do S = LFP F
f
F   f . . 

S 
• Axiomatic
where
if b   True
if b   False
{ INV  b} S {INV}
{INV} while b do S {INV  b}
11
Syntax - subprogram
•
FORTRAN
FUNCTION fun-nam (formal-par-lst)
•
COBOL
PERFORM L1 THRU Ln.
•
Parcal
function fun-name (var-lst:type): result-type;
procedure proc-name (var-lst:type; var var-lst:type);
•
Ada
function fun-name (var-lst: in type): result-type is
procedure proc-name (var-lst: in type; var-lst: out
type; var-lst: in out type is
12
Semantics - subprogram
• Only 4 kinds of statement, namely assignment,
sequential, conditional, and procedure call.
• All procedures are declared globally before use,
formally,
Pr og ::   D | S 
D :: D1  S 1,
,
Dn  Sn
S :: var : t | S 1; S 2 | if b then S 1 else S 2 | P
• Procedure call do not allow argument passing
mechanism.
• Recursive calls as well as mutual recursive calls are
allowed.
13
Semantics -subprogram
• Operational
 ( Si )   i
 ( Pi )   i
• Denotational
 P1 , P2 ,
, Pn   LFP  F 1, F 2,
Fi   f 1, f 2,
, fn
, Fn 
where
Si
• Axiomatic
{P} Si {Q}
{P} Pi {Q}
14
Concepts recall
• cpo is a partially ordered set satisfying (1) each chain
has a least upper bound, (2) it has a bottom element.
• A function F: C1 → C2 is monotonic if,
x, y in C1, if x  y, then F(x)  F(y).
• A function F: C1 → C2 is continuous if, for each chain x
= x1,x2, … in C1, F(x) = F(xi).
• <StateTrans, > is a cpo, where StateTrans is the set
all state transformations f: State → State.
• f1  f2 iff σ f1 (σ)  f2 (σ)  and f1 (σ)=f2(σ)
• If we understand function as set of mappings, then f1
 f2 iff f1 is a subset of f2.
• The bottom is the totally undefined function or
empty set.
• Thus, for a any chain f = f1,f2, … , f is simply the
union f.
15
Semantics - subprogram
• First we need to show F1, …, Fn are continuous with
respect to f1, …, fn. This can be done by structure
induction on statement S.
• Then we can define the least fix points as follows by
generalized Tarski’s Theorem.
 P1 , P2 ,
, Pn   LFP  F 1, F 2,
, Fn 
where
Fi   f 1, f 2,
LFP  F1, F 2,
 f10 ,
, fn
, Fn   
, fn0   ,
Si

k
k 0 1
f ,

k 0
f nk 
where
, 
 f1k 1, , fnk 1   F1( f1k , , fnk ), , Fn( f1k , , f nk )
• We can prove the equivalence of these semantics by
induction on the approximation steps k.
16
Remark - continuity
• If function f is continuous, we can
approximate the function value of a least
upper bound of a chain by the function values
of all components of the chain. Say, π=
(3,3.1,3.14, …), f(π)= (f(3),f(3.1),f(3.14), …).
• In λ calculus, we each expression denotes a
function which can apply to any other. This
imply that we need a so call reflexive domain,
in the sense that the domain is isomorphic to
its function space. The domain of continuous
functions turns out to be a choice.
17