1 / 31

Formal Semantics of Programming Language s

Formal Semantics of Programming Language s. Topic 6: Advanced Issues. 虞慧群 yhq@ecust.edu.cn. Motivation. Specifying the semantics of real programming languages is more difficult than IMP…. Language Features. Higher order types Dynamic memory allocation Pointers

Download Presentation

Formal Semantics of Programming Language s

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Motivation • Specifying the semantics of real programming languages is more difficult than IMP… Language Features • Higher order types • Dynamic memory allocation • Pointers • Procedures and recursion • Parameter passing • 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 | a0 + a1 | a0 – a1 | a0 a1 • Bexp • b ::= true | false | a0 = a1 | a0 a1 | b | b0 b1 | b0  b1 • Com • c ::= skip |L:= a | c0 ; c1 | if b then c0else c1| 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 • <a, > 1 <a’, > • 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) <n,  > 1 <n,  > <nil,  > 1 <nil,  > <X, (e , s)>1 <e(X), (e, s)> <L, (e, s) > 1 <L’, (e, s)> <L.sel, (e, s) > 1 <L’.sel, (e, s)> , sel {car, cdr} s=(cells, car, cdr) and c  cells, sel {car, cdr} and sel(c)=c’ <c.sel, (e, s) > 1 <c’, (e, s)>

  8. Expression rules (2) <a0,  > 1 <a0’, ’> <cons(a0, a1),  > 1 <cons(a0’, a1), ’> <a1, (e, s) > 1 <a1’, (e, s’)> s=(cells, car, cdr), c0Val <cons(c0+a1), (e, s)> 1 <cons(c0, a1’), (e, s’)> s=(cells, car, cdr), ccells, c0, c1 Val <cons(c0, c1), (e, s) > 1 <c, (e, cells{c}, car[c0/c], cdr[c1/c])>

  9. Boolean expressions(1) <t,  > 1 <t,  >, t {true, false} <a0, > 1 <a0’, ’> <a0=a1,  > 1 <a0’, ’> <a1, (e, s)> 1 <a1’, (e, s’)>, s=(cells, car, cdr), c0Val <c0=a1, (e, s) > 1 <c0=a1’, (e, s’)> s=(cells, car, cdr),c0, c1 Val, c0=c1<c0=c1, (e, s) > 1 <true, (e, s)> s=(cells, car, cdr),c0, c1 Val, c0≠c1<c0=c1, (e, s) > 1 <false, (e, s)>

  10. Commands(1) <skip, > 1 <a, (e, s) > 1 <a’, (e, s’)> X  Loc <X:=a, (e, s)> 1 <X:=a’, (e, s’)> <X := c, (e, s) > 1 <(e[c/X], s)>, X  Loc, cVal <a0, (e, s) > 1 <a0’,(e, s)> <a0.car := a1, (e, s) >1 <a0’.car:=a1, (e, s)> <a1, (e, s)> 1 <a1’, (e, s’)> s=(cells, car, cdr), c  cells <c.car := a1, (e, s) >1 <(c.car :=a1’, (e, s’)> s=(cells, car, cdr), c0  cells, c1  Val <c0.car := c1, (e, s) >1 <(e, (cells, car[c1/c0], cdr)

  11. Commands (2) <c0, > 1<c’0, ’> <c’0; c1, > 1<c’0;c1, ’> <c0, > 1’, <c1, ’> 1’’ <c0; c1, > 1’’ <b, >1<true, ’>, <c0, ’> 1’’ <if b then c0 else c1, >1’’ <b, >1<false, ’>, <c1, ’> 1’’ <if b then c0 else c1, >1’’ <while b do c1, >1 <if b then (c1; while b do c) else skip, >

  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 • <c, > 1 <c’, >

  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 | c0 ; c1 | 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 <skip, > 1 <a,  >  n <X:=a,  > 1 [n/X] <c0,  > 1’ <c0,  > 1 <c’0, ’> <c0;c1,  >1 <c1, ’> <c0;c1,  >1 <c’0; c1, ’> <gc, > 1 <c, ’> <if gc fi,  >1 <c, ’> <gc,  > 1fail<gc,  > 1 <c, ’> <do gc od,  >1 <do gc od,  >1 <c’; do gc od, ’>

  16. Rules for guarded commands <b,  > true <bc,  > 1 <c, > <gc0,  > 1 <c, ’> <gc1,  > 1 <c, ’> <gc0gc1,  >1 <c, ’> <gc0gc1,  >1 <c, ’> <b,  > 1 false<gc0,  > 1 fail <gc1,  > 1 fail <bc,  >1fail <gc0 gc1,  >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|gc0 gc1

  20. do (true   ? X   ! X) od || do (true   ? Y   ! Y) od ||  Examples do (true   ? X   ! X) 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 • <?X; c , > • Label transitions • {? n |  Chan & n  N} {! n |  Chan & n  N}

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

  24. Rules for commands <skip, >  <a,  >  n <X:=a,  >  [n/X] < ? X ; c, >  ?n <c, [n/X]> <a, >  n < ! e ; c, >  !n <c, > <c0,  >  <c’0, ’> <c0;c1,  > <c’0; c1, ’>

  25. Rules for commands(2) <gc,  > fail<gc,  >  <c, ’> <do gc od,  > <do gc od,  ><c’; do gc od, ’> <c0,  > <c’0, ’><c1 , >  <c’1, ’> <c0 || c1,  ><c’0 || c1, ’> < c0 || c1,  ><c0 || c’1, ’> <c0,  > ?n<c’0, ’><c1 , > !n <c’1, > <c0 || c1,  ><c’0 || c’1, ’> <c0,  > ?n<c’0, ’><c1 , > !n <c’1, > <c0 || c1,  ><c’0 || c’1, ’>

  26. Rules for commands(3) provided that ?n and !n <c,  >  <c’, ’> <c ,  > <c , ’>

  27. Rules for guarded commands(1) <b,  > true <b,  >  false <bc,  >  <c, > <bc,  >fail <b,  >  false <b,  >  false <b ?X c,  >  fail <b !e c, >  fail <gc0,  >  fail <gc1,  >  fail <gc0 gc1,  >fail

  28. Rules for guarded commands(2) <b,  > true < b ?Xc, ?n <c, [n/X]> <b,  >  true, <a, >n <b !a  c, >  !n <c, > <gc0,  >  <c, ’> <gc1,  >  <c, ’> <gc0gc1,  ><c, ’> <gc0gc1,  > <c, ’>

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

  30. 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

  31. Exercise 6 (1)

More Related