Formal Semantics of Programming Languages 虞慧群 Topic 6: Advanced Issues
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
Plan Handling memory allocation (Ex 2) A simple parallel construct Guarded commands Concurrency with communication
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
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
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
Expression rules (1) 1 1 1, sel {car, cdr} s=(cells, car, cdr) and c cells, sel {car, cdr} and sel(c)=c’ 1
Expression rules (2) 1 1 s=(cells, car, cdr), c0 Val 1 s=(cells, car, cdr), c cells, c0, c1 Val 1
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
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)
Commands (2) 1 1 1 ’, 1 ’’ 1 ’’ 1, 1 ’’ 1 ’’ 1
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
Parallelism Introduces (Demonic) Nondeterminism (X := 0 || X := 1); if X = 0 then c0 else c1
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
Rules for commands 1 n 1 [n/X] 1 ’ 1 1 1 1 1 fail 1 1 1
Rules for guarded commands true 1 1 1 1 false 1 fail 1 fail 1 fail 1 fail
Example do X >Y X := X - Y Y >X Y := Y - X od
Communicating processes Languages for modeling distributed systems CSP, Occam, Ada? Hoare, Milner Support Parallelism Non-determinism Synchronization via communication ? X ! a
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
Examples do (true ? X ! X) od do (true ? X ! X) od || do (true ? Y ! Y) od ||
Examples if (true ? X c0) (true ? Y c1) fi if (true ? X; c0) (true ? Y ;c1) fi
Formal semantics Need a way to model communication events Label transitions { ? n | Chan & n N} { ! n | Chan & n N}
Example transitions if n ?n !n if n ?n !n
Conventions in formal semantics Empty command * *; c c; * c || * * || c c * ; * (* ) * 1 = ? n = ! n =
Rules for commands n [n/X] ?n n !n
Rules for commands(2) fail ?n !n ?n !n
Rules for commands(3) provided that ?n and !n
Rules for guarded commands(1) true false fail false false fail fail fail
Rules for guarded commands(2) true true, n !n
Uncovered Calculus for Communicating Systems (CCS) A specification language The modal -calculus Local model checking
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
Exercise 6 (1)