Input/output Tiny +++ S ∈ Stmt ::= . . . | read x | write e Andrzej Tarlecki: Semantics & Verification - 106 -
Semantic domains Stream = Int × Stream + { eof } Input = Stream Output = Stream State = Store × Input × Output ✬ ✩ Actually: Stream = ( Int ⊗ L Stream ) ⊕ { eof } ⊥ where: D ⊗ L D ′ = D ⊗ D ′ ✫ ✪ ⊥ Andrzej Tarlecki: Semantics & Verification - 107 -
Semantic functions E : Exp → VEnv → State → ( Int + { ? ? } ) � �� � EXP B : BExp → VEnv → State → ( Bool + { ? ? } ) � �� � BEXP Only one clause to modify here: E [ [ x ] ] ρ V � s, i, o � = s l where l = ρ V x Andrzej Tarlecki: Semantics & Verification - 108 -
Semantics of statements S : Stmt → VEnv → PEnv → State → ( State + { ? ? } ) � �� � STMT Again, only one clause to change: S [ [ x := e ] ] ρ V ρ P � s, i, o � = � s [ l �→ n ] , i, o � where l = ρ V x, n = E [ [ e ] ] ρ V � s, i, o � (plus a similar change in D V [ [ var x ; D V ] ] . . . = . . . ) and two clauses to add: ] ρ V ρ P � s, i, o � = � s [ l �→ n ] , i ′ , o � where l = ρ V x, � n, i ′ � = i S [ [ read x ] S [ [ write e ] ] ρ V ρ P � s, i, o � = � s, i, � n, o �� where n = E [ [ e ] ] ρ V � s, i, o � ✎ ☞ ✍ ✌ � n, i ′ � = i is ? ? when i = eof Cheating a bit: writing out in the reverse order Andrzej Tarlecki: Semantics & Verification - 109 -
Programs New syntactic domain: Prog ::= prog S with obvious semantic function: P : Prog → Input → ( Output + { ? ? } ) � �� � PROG given by: ] i = o ′ where S [ ] ρ ∅ V ρ ∅ P � s ∅ , i, eof � = � s ′ , i ′ , o ′ � , P [ [ prog S ] [ S ] ? , s ∅ next = 0 , s ∅ l = ? ρ ∅ ? , ρ ∅ V x = ? P p = ? ? Andrzej Tarlecki: Semantics & Verification - 110 -
Okay, but. . . Do we want to allow statements to erase output? Changing philosophy From: What happens now? To: What the overall answer will be? Andrzej Tarlecki: Semantics & Verification - 111 -
Direct semantics ; ; begin . . . . . . . . . end S [ [ ... ] ] S [ [ ... ] ] S [ [ ... ] ] s ∅ s ′ ñ − → s i − → s j − → “overall result” ñ ò Continuation semantics ; . . . ; . . . end begin . . . κ ′ : ñ ò “overall result” ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ S [ [ ... ] ] ← − κ i : ñ ò “overall result” ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ S [ [ ... ] ] ← − κ j : ñ ò “overall result” ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ ñ S [ [ ... ] ] − κ ∅ : ñ ← ò “overall result” ñ Andrzej Tarlecki: Semantics & Verification - 112 -
Continuations Cont = State → Ans Now: State = Store × Input • states do not include outputs Ans = Output • answers are outputs (or errors) • these are continuations for statements; semantics for statements is given by: S : Stmt → VEnv → PEnv → Cont → Cont � �� � STMT That is: STMT = VEnv → PEnv → Cont → State → Ans Andrzej Tarlecki: Semantics & Verification - 113 -
Expression and declaration continuations • continuations for other syntactic categories may be additionally parameterised by whatever these pass on: − expressions pass on values, so Cont E = Int → State → Ans Cont B = Bool → State → Ans − declarations pass on environments, so Cont D V = VEnv → State → Ans Cont D P = PEnv → State → Ans Andrzej Tarlecki: Semantics & Verification - 114 -
Tiny +++ N ∈ Num ::= 0 | 1 | 2 | · · · x ∈ Var ::= · · · p ∈ IDE ::= · · · e ∈ Exp ::= N | x | e 1 + e 2 | e 1 ∗ e 2 | e 1 − e 2 b ∈ BExp ::= true | false | e 1 ≤ e 2 | ¬ b ′ | b 1 ∧ b 2 S ∈ Stmt ::= x := e | skip | S 1 ; S 2 | if b then S 1 else S 2 | while b do S ′ | begin D V D P S end | call p | call p ( vr x ) | read x | write e D V ∈ VDecl ::= var x ; D V | ε D P ∈ PDecl ::= proc p is ( S ); D P | proc p ( vr x ) is ( S ); D P | ε Prog ::= prog S Andrzej Tarlecki: Semantics & Verification - 115 -
Semantic domains Input = Int × Input + { eof } Int = . . . State = Store × Input Bool = . . . Output = Int × Output + { eof , ? ? } Loc = . . . Store = . . . Cont = State → Output VEnv = . . . Cont E = Int → Cont Cont B = Bool → Cont PROC 0 = Cont → Cont Cont D V = VEnv → Cont PROC vr 1 = Loc → PROC 0 Cont D P = PEnv → Cont PEnv = IDE → ( PROC 0 + PROC vr 1 + { ? ? } ) Andrzej Tarlecki: Semantics & Verification - 116 -
Semantic functions E : Exp → VEnv → Cont E → Cont � �� � EXP B : BExp → VEnv → Cont B → Cont � �� � BEXP S : Stmt → VEnv → PEnv → Cont → Cont � �� � STMT D V : VDecl → VEnv → Cont D V → Cont � �� � VDECL D P : PDecl → VEnv → PEnv → Cont D P → Cont � �� � PDECL P : Prog → Input → Output � �� � PROG Andrzej Tarlecki: Semantics & Verification - 117 -
Sample semantic clauses P κ ∅ � s ∅ , i � ] ρ ∅ V ρ ∅ P [ [ prog S ] ] i = S [ [ S ] Programs: ? , κ ∅ s = eof , s ∅ next = 0 , s ∅ l = ? where ρ ∅ ? , ρ ∅ V x = ? P p = ? ? D P [ [ ε ] ] ρ V ρ P κ P = κ P ρ P D P [ [ proc p is ( S ); D P ] ] ρ V ρ P = D P [ [ D P ] ] ρ V ρ P [ p �→ P ] where P = S [ [ S ] ] ρ V ρ P [ p �→ P ] Declarations: D V [ [ var x ; D V ] ] ρ V κ V � s, i � = ] ρ ′ V κ V � s ′ , i � where l = s next , ρ ′ D V [ [ D V ] V = ρ V [ x �→ l ] , s ′ = s [ l �→ ? ✤ ✜ ? , next �→ l + 1] ✛ ✘ may be rewritten to a more No continuations really used here, but: ✚ ✙ ✣ ✢ standard continuation style Andrzej Tarlecki: Semantics & Verification - 118 -
Sample semantic clauses ☛ ✟ E [ [ x ] ] ρ V κ E = λ � s, i � : State .κ E n � s, i � where l = ρ V x, n = s l ✡ ✠ this means: ? ? if ρ V x = ? ? or s l = ? ? Expressions: E [ [ e 1 + e 2 ] ] ρ V κ E = ☛ ✟ E [ [ e 1 ] ] ρ V λn 1 : Int . E [ [ e 2 ] ] ρ V λn 2 : Int .κ E ( n 1 + n 2 ) ✡ ✠ check the types! B [ [ true ] ] ρ V κ B = κ B tt B [ [ e 1 ≤ e 2 ] ] ρ V κ B = Boolean expressions: ✎ ☞ E [ [ e 1 ] ] ρ V λn 1 : Int . E [ [ e 2 ] ] ρ V λn 2 : Int . ✞ ☎ κ B ifte ( n 1 ≤ n 2 , tt , ff ) ✝ ✆ ✍ ✌ No comments? Andrzej Tarlecki: Semantics & Verification - 119 -
Statements S [ [ x := e ] ] ρ V ρ P κ = E [ [ e ] ] ρ V ( λn : Int .λ � s, i � : State .κ � s [ l �→ n ] , i � ) where l = ρ V x S [ [ skip ] ] ρ V ρ P = id Cont S [ [ S 1 ; S 2 ] ] ρ V ρ P κ = S [ [ S 1 ] ] ρ V ρ P ( S [ [ S 2 ] ] ρ V ρ P κ ) S [ [ if b then S 1 else S 2 ] ] ρ V ρ P κ = B [ [ b ] ] ρ V λv : Bool . ifte ( v, S [ [ S 1 ] ] ρ V ρ P κ, S [ [ S 2 ] ] ρ V ρ P κ ) S [ [ while b do S ] ] ρ V ρ P κ = B [ [ b ] ] ρ V λv : Bool . ifte ( v, S [ [ S ] ] ρ V ρ P ( S [ [ while b do S ] ] ρ V ρ P κ ) , κ ) S [ [ call p ] ] ρ V ρ P = P where P = ρ P p ] ρ V ρ P = P l where P = ρ P p ∈ PROC vr S [ [ call p ( vr x )] 1 , l = ρ V x ] ρ V ρ P κ � s, i � = κ � s [ l �→ n ] , i ′ � where l = ρ V x, � n, i ′ � = i S [ [ read x ] S [ [ write e ] ] ρ V ρ P κ = E [ [ e ] ] ρ V λn : Int .λ � s, i � : State . � n, κ � s, i �� Andrzej Tarlecki: Semantics & Verification - 120 -
Blocks S [ [ begin D V D P S end ] ] ρ V ρ P κ = ] ρ V λρ ′ ] ρ ′ V ρ P λρ ′ ] ρ ′ V ρ ′ D V [ [ D V ] V : VEnv . D P [ [ D P ] P : PEnv . S [ [ S ] P κ This got separated, because we want to add jumps. . . Andrzej Tarlecki: Semantics & Verification - 121 -
Goto’s S ∈ Stmt ::= . . . | L : S | goto L L ∈ LAB ::= . . . • Labels are visible inside the block in which they are declared • No jumps into a block are allowed; jumps into other statements are okay Andrzej Tarlecki: Semantics & Verification - 122 -
Semantics — sketch • Yet another environment: LEnv = LAB → ( Cont + { ? ? } ) • The appropriate semantic functions get another environment parameter: S : Stmt → VEnv → PEnv → LEnv → Cont → Cont � �� � STMT D P : PDecl → VEnv → PEnv → LEnv → Cont D P → Cont � �� � PDECL • Semantic clauses for declarations and statements of the “old” forms take the ✎ ☞ ✞ ☎ extra parameter and disregard it (passing it “down”); semantics for programs ✝ ✆ introduces label environment with no label declared. ✍ ✌ . . . to be continued Andrzej Tarlecki: Semantics & Verification - 123 -
Goto’s — sketch of the semantics continues • We add a declaration-like semantics for statements: D S : Stmt → VEnv → PEnv → LEnv → Cont → LEnv • With a few trivial clauses, like: D S [ [ x := e ] ] ρ V ρ P ρ L κ = ρ L and similarly for skip , call p , call p ( vr x ) , read x , write e and goto L , where no visible labels can be introduced. Perhaps surprisingly, also: D S [ [ begin D V D P S end ] ] ρ V ρ P ρ L κ = ρ L ✎ ☞ ✞ ☎ ✝ ✆ ✍ ✌ . . . to be continued Andrzej Tarlecki: Semantics & Verification - 124 -
Recommend
More recommend