Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formal Semantics of Programming Languages 虞慧群 Topic 6: Advanced Issues.

Similar presentations


Presentation on theme: "Formal Semantics of Programming Languages 虞慧群 Topic 6: Advanced Issues."— Presentation transcript:

1 Formal Semantics of Programming Languages 虞慧群 yhq@ecust.edu.cn Topic 6: Advanced Issues

2 Motivation Specifying the semantics of real programming languages is more difficult than IMP… Language Features o Higher order types o Dynamic memory allocation o Pointers o Procedures and recursion o Parameter passing o Concurrency

3 Plan Handling memory allocation (Ex 2) A simple parallel construct Guarded commands Concurrency with communication

4 Abstract Syntax for IMP++ L X | L.car | L.cdr Aexp a ::= n | L | cons(a, a) | nil | a 0 + a 1 | a 0 – a 1 | a 0  a 1 Bexp b ::= true | false | a 0 = a 1 | a 0  a 1 |  b | b 0  b 1 | b 0  b 1 Com c ::= skip | L := a | c 0 ; c 1 | if b then c 0 else c 1 | while b do c

5 Extending the semantic domain States cannot be mapping from variables to values Need a way to represent “sharing” Two level stores  = (env, store) env : Loc  Val store=(Cells, car, cdr) car: Cells  Val, cdr: Cells  Val Val = Cells  {nil}  N

6 Extending the semantic relation expressions  1 What is the intermediate result of computing L- value? (((X).cdr).car) Allow location expressions a  cells cons expressions modify the store

7 Expression rules (1)  1  1  1, sel  {car, cdr} s=(cells, car, cdr) and c  cells, sel  {car, cdr} and sel(c)=c’  1

8 Expression rules (2)  1  1 s=(cells, car, cdr), c0  Val  1 s=(cells, car, cdr), c  cells, c0, c1  Val  1

9 Boolean expressions(1)  1, t  {true, false}  1  1  1, s=(cells, car, cdr), c0  Val  1 s=(cells, car, cdr),c0, c1  Val, c0=c1  1 s=(cells, car, cdr),c0, c1  Val, c0≠c1  1

10 Commands(1)  1   1 X  Loc  1  1, X  Loc, c  Val  1  1  1 s=(cells, car, cdr), c  cells  1 s=(cells, car, cdr), c0  cells, c1  Val  1 <(e, (cells, car[c1/c0], cdr)

11 Commands (2)  1  1  1  ’,  1  ’’  1  ’’  1,  1  ’’  1  ’’  1

12 A Simple Parallel Construct c0 || c1 Execute co and c1 in parallel (X := 1 || (X:=2 ; X := X + 1)) Natural Operational Semantics Small step rules  1

13 Parallelism Introduces (Demonic) Nondeterminism (X := 0 || X := 1); if X = 0 then c0 else c1

14 Guarded Commands Com c ::= skip | abort | X := a | c 0 ; c 1 | if gc fi | do gc od GC gc ::= b  c | gc0  gc1 if X  Y  MAX := X  Y  X  MAX := Y fi do X >Y  X := X - Y  Y >X  Y := Y - X od

15 Rules for commands  1   n  1  [n/X]  1  ’  1  1  1  1  1 fail  1  1   1

16 Rules for guarded commands  true  1  1  1  1 false  1 fail  1 fail  1 fail  1 fail

17 Example do X >Y  X := X - Y  Y >X  Y := Y - X od

18 Communicating processes Languages for modeling distributed systems CSP, Occam, Ada? Hoare, Milner Support Parallelism Non-determinism Synchronization via communication  ? X   ! a

19 Communication Processes Channel names , ,   Chan Input expression  ? X where X  Loc Output expressions  ! A where a  Aexp Commands c::= skip | abort | X := a |  ? X |  ! A | c0 ; c1 | if gc fi | do gc od | c0 || c1 | c   Guarded commands gc ::= b  c | b   ? X  c | b   ! a  c| gc 0  gc 1

20 Examples do (true   ? X   ! X) od do (true   ? X   ! X) od || do (true   ? Y   ! Y) od || 

21 Examples if (true   ? X  c0)  (true   ? Y  c1) fi if (true   ? X; c0)  (true   ? Y ;c1) fi

22 Formal semantics Need a way to model communication events Label transitions {  ? n |  Chan & n  N}  {  ! n |  Chan & n  N}

23 Example transitions  if  n   ?n   !n if  n   ?n   !n

24 Conventions in formal semantics Empty command * *; c  c; *  c || *  * || c  c * ; *  (*   )  *    1  =  ? n =  ! n = 

25 Rules for commands    n   [n/X]   ?n  n   !n 

26 Rules for commands(2)  fail         ?n   !n    ?n   !n 

27 Rules for commands(3)  provided that  ?n and  !n

28 Rules for guarded commands(1)  true  false   fail  false  false  fail  fail  fail

29 Rules for guarded commands(2)  true  true,  n   !n  

30 Uncovered Calculus for Communicating Systems (CCS) A specification language The modal  -calculus Local model checking

31 Summary Writing a small step semantics for a real programming language is non-trivial Small step semantics can model Nondeterminism Concurrency Failures Guarded command is a powerful language construct

32 Exercise 6 (1)


Download ppt "Formal Semantics of Programming Languages 虞慧群 Topic 6: Advanced Issues."

Similar presentations


Ads by Google