Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Semantics Q1 2007 S EMANTICS (Q1,’07) Week 7 Jacob Andersen PhD student

Similar presentations


Presentation on theme: "1 Semantics Q1 2007 S EMANTICS (Q1,’07) Week 7 Jacob Andersen PhD student"— Presentation transcript:

1 1 Semantics Q1 2007 S EMANTICS (Q1,’07) Week 7 Jacob Andersen PhD student andersen@daimi.au.dk

2 2 Semantics Q1 2007 News… Exam: –Time and Place (final): Oct. 31 st 9.00-13.00 in Benjamin bld. –Curriculum: On the webpage (schedule) –Materials: SOS chapter 1-3, CCS chapter 1-3, Note on SI –Lecture slides »A service to you to ease note-taking (alternative: whiteboard-only lectures) »Price (for you to “pay”): I may require that you use things at the exam, that are only introduced on the slides. –Exercises and hand-ins (including TA feedback) »Competences developed and trained through exercises. »Many points illustrated best by practical experience. Sample Solution to 2005 Miniproject

3 3 Semantics Q1 2007 Week 7 - Outline Exam Bisimulation Course Evaluation SOS Implementation (Interpretation) Program Equivalence Imperative Blocks The Environment-Store Model Other Semantic Formalisms

4 4 Semantics Q1 2007 Exam 1 st page Evaluating your competences, i.e. –new problems which you have not seen before. –Hidden “traps” or insights –… to avoid pattern-matching. –Don’t Panic! A solution without explanations / motivations is useless!! –Matches (at most) the “describe” competence. –If the solution is wrong it cannot be “rescued” by a good explanation.

5 5 Semantics Q1 2007 B ISIMULATION

6 6 Semantics Q1 2007 Def: A Strong Bisimulation Let (Proc, Act,  ) be a LTS Def: a bin. rel. R  Proc  Proc is a strong bisimulation iff whenever ( s, t )  R :  a  Act : if s  s’ then t  t’ for some t’ such that ( s’, t’ )  R if t  t’ then s  s’ for some s’ such that ( s’, t’ )  R Note: 1. Definition on LTS (not necessarily wrt. processes) 2. Definition relative to a (SOS) semantics (via LTS) a a a a a Intuition: “Only equate as consistently allowed by the semantics”

7 7 Semantics Q1 2007 Def: Strongly Bisimilar ( ~ ) A Strong Bisimulation: Def: a bin. rel. R  Proc  Proc is a strong bisimulation iff whenever ( s, t )  R :  a  Act : if s  s’ then t  t’ for some t’ such that ( s’, t’ )  R if t  t’ then s  s’ for some s’ such that ( s’, t’ )  R The Strong Bisimilarity relation ( ~ ): Def: two (processes) s and t are strongly bisimilar ( s ~ t ) iff  strong bisimulation R : ( s, t )  R. i.e. a a a a ‘ ~ ’ :=  { R | R is a strong bisimulation }

8 8 Semantics Q1 2007 How to Prove Strong Bisimilarity ? How to prove strong bisimilarity for two processes ? i.e. ?: Exhibit a (any) bisimulation R, for which: –By definition we get that: » since ‘ ~ ’ was the largest bisimulation How to disprove strong bisimilarity? Strong bisimulation game ( s, t )  R  ‘ ~ ’ ( s, t )  R s ~ ts ~ t

9 9 Semantics Q1 2007 Example Proof of Bisimilarity Example: Buffer (capacity 1): Buffer (capacity 2): Show that: A 0 = def in. A 1 A 1 = def out. A 0 B 0 = def in. B 1 B 1 = def in. B 2 + out. B 0 B 2 = def out. B 1 B 0 ~ A 0 |A 0 B0B0 B1B1 B2B2 A 0 |A 0 A 1 |A 0 A 0 |A 1 A 1 |A 1 R = { ( B 0, A 0 |A 0 ), ( B 1, A 1 |A 0 ), ( B 1, A 0 |A 1 ), ( B 2, A 1 |A 1 ) }

10 10 Semantics Q1 2007 Other Properties of ( ~ ) The following properties hold  P, Q, R : P+Q ~ Q+P // ‘ + ’ commutative (P+Q)+R ~ P+(Q+R) // ‘ + ’ associative P|Q ~ Q|P // ‘ | ’ commutative (P|Q)|R ~ P|(Q|R) // ‘ | ’ associative P+0 ~ P // ‘ 0 ’ neutral wrt. ‘ + ’ P|0 ~ P // ‘ 0 ’ neutral wrt. ‘ | ’... Live exercise: Prove one of these properties

11 11 Semantics Q1 2007 Summary: Strong Bisimilarity ( ~ ) Properties of ( ~ ): an equivalence relation: »reflexive, symmetric, and transitive the largest strong bisimulation: »for proving bisimilarity (exhibit a bisimulation) strong bisimulation game: »for proving non-bisimilarity (winning attack strategy) a congruence: »P ~ Q => C[P] ~ C[Q] obeys the following algebraic laws: »‘ + ’ and ‘ | ’ commutative, associative, and ‘ 0 ’ neutrality, …

12 12 Semantics Q1 2007 Summary: Weak Bisimilarity (  ) Properties of (  ): an equivalence relation: »reflexive, symmetric, and transitive the largest weak bisimulation: »for proving bisimilarity (exhibit a bisimulation) weak bisimulation game: »for proving non-bisimilarity (winning attack strategy) not a congruence: »P  Q => C[P]  C[Q] obeys the following algebraic laws: »‘ + ’ and ‘ | ’ commutative, associative, and ‘ 0 ’ neutrality, … abstracts away from internal tau-actions

13 13 Semantics Q1 2007 (  ):“Fair Abstraction from Divergence” Consider: A = def a.0 + .B B = def b.0 + .A –Note that: »A  B  a.0 + b.0 !!!..and even: Div = def .Div »0  Div !!! Intuition: “Fair Abstraction from Divergence”: »  “assumes processes (eventually) escape from loops”

14 14 Semantics Q1 2007 C OURSE E VALUATION

15 15 Semantics Q1 2007 Course Evaluation Your e aluation matters!: »Gives you a chance to voice your opinion »Helps improve next year’s course »Helps improve my teaching (in general) »May influence larger didactic strategies for whole dept. / uni Why two evaluations? »Compulsory in order to get valid results.

16 16 Semantics Q1 2007 SOS Implementation Example: L implementation in SML

17 17 Semantics Q1 2007 Representation of Exp/BExp/Com type number = int type variable = string type truthvalue = bool datatype exp = Number of number | Variable of variable | Add of exp * exp | Sub of exp * exp | Mul of exp * exp datatype bexp= Truthvalue of truthvalue | Eq of exp * exp | Or of bexp * bexp | Not of bexp datatype com = Skip | Assign of variable * exp | Seq of com * com | If of bexp * com * com | While of bexp * com

18 18 Semantics Q1 2007 Representation of Store type store = (variable * number) list fun update s v n = let val s' = List.filter (fn (v', _) => v <> v') s in (v, n) :: s' end fun lookup s v = let val pair = List.find (fn (v', _) => v = v') s fun match (SOME (_, n)) = n | match NONE = raise (Fail "Stuck!") in match pair end

19 19 Semantics Q1 2007 Small-step semantics for Exp fun smallStepExp (Variable var, store) (* Var *) = let val n = lookup store var in (Number n, store) end | smallStepExp (Add (Number m, Number m'), store) (* Sum3 *) = let val n = m + m' in (Number n, store) end | smallStepExp (Add (Number m, e1), store) (* Sum2 *) = let val (e1', _) = smallStepExp (e1, store) in (Add (Number m, e1'), store) end | smallStepExp (Add (e0, e1), store) (* Sum1 *) = let val (e0', _) = smallStepExp (e0, store) in (Add (e0', e1), store) end

20 20 Semantics Q1 2007 Pretty Printing Exp fun prettyExp (Number n) = print (Int.toString n) | prettyExp (Variable var) = print var | prettyExp (Add (e1, e2)) = ( prettyExp e1; print " + "; prettyExp e2 ) | prettyExp (Sub (e1, e2)) = ( prettyExp e1; print " - "; prettyExp e2 ) | prettyExp (Mul (e1, e2)) = ( prettyExp e1; print " * "; prettyExp e2 ) fun prettyBExp... =... fun prettyCom... =...

21 21 Semantics Q1 2007 P ROGRAM E QUIVALENCE

22 22 Semantics Q1 2007 Program Equivalence (  )? Program equivalence (  ) ?:        x  FV(E 2 )  y  FV(E 1 )... How do we know they are “equivalent” ? …and what does that mean ? C ; nilnil ; CC if B then C else C’if ~B then C’ else C (C 1 ; C 2 ) ; C 3 C 1 ; (C 2 ; C 3 ) repeat C until BC ; while ~B do C x := E 1 ; y := E 2 y := E 2 ; x := E 1 nilnil ; nil

23 23 Semantics Q1 2007 Behavior and Behavioral Equivalence Assume deterministic language L: Def: Behavior: Partial function: exec(C,  ) = Def: Behavioral equivalence ( C  C’ ):  ’ if  *  ’ undef otherwise e.g. nontermination, abnormal termination exec : Com  Store  Store  Store: exec(C,  ) = exec(C’,  ) i.e. the two commands produce the same resulting store,  ’, (but not necessarily in the same number of steps) if both defined

24 24 Semantics Q1 2007 Congruence (  ) Theorem: “  ” is a congruence [proof omitted] »i.e., we can substitute equivalent fragments in programs! Example (Java): C  C’ => P[C]  P[C’], for all contexts P[] class C { D void m() { S’ for (E 1 ; E 2 ; E 3 ) S 0 S” } safe transformation who: compiler, homo-sapiens, combination (refactoring tools), … why: readability, optimization, simplification, … class C { D void m() { S’ { E 1 ; while (E 2 ) { S 0 E 3 ; }} S” } class C { D void m() { S’ [ ] S’’ }

25 25 Semantics Q1 2007 How to Prove Behavioral Equivalence? How do we prove: (for given C, C’ )? i.e.: »For derivation sequences of any length, n C  C’ ,  ’: (  *  ’)  (  *  ’)  Store: exec(C,  ) = exec(C’,  ) if both defined  ,  ’: (  *  ’)  (  *  ’)  ,  ’: (  *  ’)  (  *  ’)  ,  ’: (  n  ’)  (  *  ’) ,  ’: (  *  ’)  (  n  ’)  

26 26 Semantics Q1 2007 Induction on the Length of Derivation Seq’s Base case: P(k=1) Prove that the property, P, holds »for all derivation sequences of length 1 (one) Inductive step: P(k)  P(k+1) Assume P(k): »that the property holds for derivation sequences of length k Prove P(k+1): »that it holds for derivation sequences of length k+1 Then:  n  1: P(n) »Property P holds for all derivation sequences (any length)

27 27 Semantics Q1 2007 …Or How do we prove: (for given C, C’ )? i.e.: »For some intermediate configuration,  C  C’ ,  ’: (  *  ’)  (  *  ’)  Store: exec(C,  ) = exec(C’,  ) if both defined  ,  ’: (  *  ’)  (  *  ’)  ,  ’: (  *  ’)  (  *  ’)   : (  *  )  (  *  )   : (  *  )  (  *  ) 

28 28 Semantics Q1 2007 Example (Proof Structure) Example:  Prove “  ” (let  be given w/o assumptions): Assume [LHS]: show [RHS]: »Case analysis on possible derivations for [LHS]… if B then C else C’if ~B then C’ else C  *    *  for some   * 

29 29 Semantics Q1 2007 Example (cont’d) Case [B  * tt]: Then construct: »Analogous for [B  * ff] »Symmetric for the other direction “  ” C1C1 B*B* [IF 1 ] C1C1 B1B1 [IF 2 ] B*B* [NEG 1 ] proof C*C* C*C* ’’ ’’ proof ’

30 30 Semantics Q1 2007 I MPERATIVE B LOCKS

31 31 Semantics Q1 2007 Blocks Consider the language ABCD: Example: A ::= z | v | A 0 + A 1 | A 0 - A 1 | A 0  A 1 B ::= b | ~ B | B 0 or B 1 | A 0 = A 1 C ::= nil | x := A | if B then C else C’ | while B do C | begin D ; C end // local block D ::= nil | var x := A | D 0 ; D 1 // local defs. if (~ (x = y)) then begin var t := x ; x := y ; y := t end else nil

32 32 Semantics Q1 2007 Semantics of Definitions Semantics of Definitions: [ NIL ] D  D   D  ’[x=n] [ VAR ] D  A *  D [ SEQ 1 ] D  D [ SEQ 2 ] D  D  ’ extend store Note: [Plotkin] does this differently (through env-store model); read it yourselves…

33 33 Semantics Q1 2007 Semantics of Blocks SOS for Blocks: – – – [ BLK 1 ] C  C  D *  ’ [ BLK 2 ] C  C [ BLK 3 ] C  C (  ’ \ V) [  0 ]  C  ’ remember values of shadowed variables :  0 =  | V remember set of locally defined variables : V=DV(D) purge locally defined variables and restore old shadowed values

34 34 Semantics Q1 2007 Dynamic vs. Static Scope Rules – Example: x := 2 ; begin var x := 7 ; nil end // here: x has the value... [ BLK 3 ] C  C (  ’ \ V) [  0 ]  C  ’ purge locally defined variables and restore old shadowed values “Static Scope Rules” x = 2 “Dynamic Scope Rules” x = 7 restoring old shadowed values not restoring …

35 35 Semantics Q1 2007 Inaccessible Val’s (Garbage Collection) – Example: [ BLK 3 ] C  C (  ’ \ V) [  0 ]  C  ’ purge locally defined variables and restore old shadowed values // x undefined begin var x := 7 ; nil end // here x is... “No Inaccessible Values” x isn’t in the store (garbage collection)! “Inaccessible Values” x is in the store (but inaccessible)! purging locally defined vars not purging …

36 36 Semantics Q1 2007 T HE E NVIRONMENT- S TORE M ODEL

37 37 Semantics Q1 2007 “The Environment-Store Model” “The Environment-Store Model”: Introducing abstract locations: Transitions: »  | -  x ℓ v VARLOC VAL      environmentstore  (x)  (  (x)) x  : VAR  LOC,  : LOC  VAL env : doesn’t change w/ exec store: mutates with execution

38 38 Semantics Q1 2007 Examples (Pointers) Pointers Static Semantics: Dynamic Semantics: ptr p = 0xCAFEBABE;//  ( p)  Loc Z a location const int x = *p; // *p  Z (since  ( p)  Loc Z ) [ DER ]  |- * E :   |- E : LOC  [ DER 2 ]  |-  n =  (ℓ) [ DER 1 ]  |-  #define ptr (int*) (for the C-hackers: :) "DER" for (pointer) dereference

39 39 Semantics Q1 2007 Examples (cont’d) Aliasing (similarly with call-by-reference): – Explicit allocation: – Explicit deallocation: – { ptr p = allocate(1); //  (p) = ℓ fresh ℓ fresh  Loc Z *p = 42; // side-effecting:  ’ =  [ ℓ fresh =42] } // ℓ fresh  Dm(  ) ptr p =...; free(p); //  (p)=ℓ, but ℓ  Dm(  ); “dangling reference”! ptr q = p; // location aliasing:  (q) = ℓ =  (p) *p = 42; // side-effecting:  ’ =  [ ℓ =42] // now *q also has the value 42:  (  (q)) is 42

40 40 Semantics Q1 2007 O THER S EMANTIC F ORMALISMS

41 41 Semantics Q1 2007 Operational Semantics Operational Semantics: Labelled Transition System: »  0 =  »  1 =  »  2 =  »  3 =  result = [x=2,y=1,z=1] Variations in step-sizes (small-step, big-step, …) The meaning of a construct is specified by the computation it induces when it is executed on a machine. In particular, it is of interest how the effect of a computation is produced. -- [Nielson & Nielson, “Semantics with Applications”, ’93]

42 42 Semantics Q1 2007 Operational Semantics (cont’d) Example: Modular SOS –Using “Generalized LTS” –Essentially: Neighbouring labels must be “composable”. –Configurations does not contain stores or anything else but the program state. Stores, environments, I/O etc. are embedded in the labels, e.g.: –In this case labels are composable iff the second store component in a label is equal to the first store component in the subsequent label. x := e -X-> x := e’ e –X-> e’ x := n –(σ,σ’)-> nil σ’=σ[n/x] [ASS 1 ] [ASS 2 ]

43 43 Semantics Q1 2007 Denotational Semantics Denotational Semantics: Describe everything as mathematical functions: »[[ z=x;(x=y;y=z) ]] = [[ x=y;y=z ]] o [[ z=x ]] = [[ y=z ]] o [[ x=y ]] o [[ z=x ]] = s.s[y=s(z)] o s.s[x=s(y)] o s.s[z=s(x)] = s.s[x=s(y),y=s(x),z=s(x)] –Ex. R5RS (Revised 5 Report on the Alg. Lang. Scheme) –Loops expressed as fixed-points of rec’sive functors »i.e., functions that takes functions as arguments Meanings are modelled by mathematical objects that represent the effect of executing the constructs. Thus, only the effect is of interest, not how it is obtained. -- [Nielson & Nielson, “Semantics with Applications”, ’93]

44 44 Semantics Q1 2007 Axiomatic Semantics Axiomatic Semantics: Partial correctness; –Command C is partially correct wrt. a pre and a post- condition if whenever the initial state fulfils the pre- condition and the program terminates, then the final state fulfils the post-condition. –{ x=1,y=2 } z=x;x=y;y=z { x=2,y=1 } Specific properties of the effect of executing the constructs are expressed as assertions. Thus, there may be aspects of the executions that are ignored. -- [Nielson & Nielson, “Semantics with Applications”, ’93] {  pre } C {  post } {P} C;C’ {R} {P} C {Q} {Q} C’ {R} {P} while B do C {¬B ∧ P} {B ∧ P} C {P}

45 45 Semantics Q1 2007

46 46 Semantics Q1 2007 Next week: Revision Period; then Exam Good Luck! Any Questions?


Download ppt "1 Semantics Q1 2007 S EMANTICS (Q1,’07) Week 7 Jacob Andersen PhD student"

Similar presentations


Ads by Google