1 / 60

S EMANTICS (Q1,’07) Week 2

S EMANTICS (Q1,’07) Week 2. Jacob Andersen PhD student andersen@daimi.au.dk. Week 2 - Outline. Highlights from last week Syntax vs. Semantics Virtual Machine Semantics Structural Operational Semantics Expressions ( Exp ) Big-step vs. small-step semantics Side-effects

lynton
Télécharger la présentation

S EMANTICS (Q1,’07) Week 2

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. SEMANTICS (Q1,’07)Week 2 Jacob Andersen PhD student andersen@daimi.au.dk Semantics Q1 2007

  2. Week 2 - Outline • Highlights from last week • Syntax vs. Semantics • Virtual Machine Semantics • Structural Operational Semantics • Expressions (Exp) • Big-step vs. small-step semantics • Side-effects • Behavior and Equivalence, … • Boolean Expressions (BExp) • Lazy evaluation, … • Commands (Com) • … Semantics Q1 2007

  3. HIGHLIGHTS FROM LAST WEEK Semantics Q1 2007

  4. Active vs. Passive Learning • Correlation: • Investment ~ Benefit Ex: Studying ~ Exam result • Do Active Learning: • Study = read + make exercises+ reflect (for your own sake)! Source: J. Biggs, “Teaching for quality learning at university” vs. Active Learning Passive Learning Semantics Q1 2007

  5. Why bother learning Semantics?!? rapidly learn new prog. lang. Semantics- preserving prog. changes debugging programs Fully understand a prog. lang. write faster programs Pass thiscourse $$ Why Semantics?!? write "better" programs design new prog. lang.s cross- compilation automated error detection automated optimization $$$$ Semantics Q1 2007

  6. Relations • A k-ary relation, L, over the setsS1, S2, … , Skis a subsetL S1  S2  …  Sk Semantics Q1 2007

  7. Inference Systems • Inference System: • Inductive (recursive) specification of relations • Consists of axiomsand rules premise(s) |_evenn |_evenm side-condition(s) m = n+2 Note: The “ “ is actually just another way of writing a “” ! conclusion Semantics Q1 2007

  8. Transition Systems • A Transition System is a structure: •  is a setof configurations •      is a binary relation (called the transition relation) • Alternative flavours: • Labelled TS • Terminal TS • Etc… ,   , A ,   and     A   ,  , T  where T   and t  T :     : t   Semantics Q1 2007

  9. SYNTAX VSSEMANTICS Semantics Q1 2007

  10. Syntax and Semantics • Syntax: • = Structure • vs. • Semantics: • = Meaning ) ( "Eats shoots and leaves" , , "Eats shoots and leaves" ; ; "Colorless Green Ideas Sleep Furiously" -- N. Chomsky (1957) Has legal syntactic structure, but no meaning Semantics Q1 2007

  11. Syntax vs. Semantics (cont'd) • In natural languages (English, Danish, ...): • Syntax  Semantics • In programming languages (C, Java, ML, ...): • Syntax  Semantics • By design (for very good reasons...): But, if I replace two nouns (syntactic) "Time flies like an arrow " --------V -------P -----------N ---------N replace noun replace noun "Fruit flies like a banana " -------V ---------------N -------------------N Semantics Q1 2007

  12. Level of Abstraction • Semantic specification(s): • Appropriate level of abstraction: • ...so that we can use the semantics for something !!! • See behind concrete details; • Perceive only what is relevant and at the appropriatelevel of abstraction)! z z  z  is_busy  is_busy  is_done  higher level of abstraction  ..., transistor57824 = 5V , transistor57825 = 0V , ...     ..., transistor57824 = 5V , transistor57825 = 5V , ...  Semantics Q1 2007

  13. THE LANGUAGE “L” Semantics Q1 2007

  14. The Language ”L” • Basic Syntactic Sets: • Truthvalues: ranged over by: t , t’, t0, … • Numbers: ranged over by: m, n, … • Variables: ranged over by: v, v’, … Meta-variables T = {tt, ff} N = {0, 1, 2, …} VAR = {a, b, c, …, z} Semantics Q1 2007

  15. The Language ”L” • Derived Syntactic Sets: • Arithmetic Expressions(e Exp): • Boolean Expressions(b BExp): • Commands (c Com): i.e. Exp = “all ASTs generated by this grammar” e ::= n | v | e + e’ | e – e’ | e  e’ b ::= t | e = e’ | b or b’ | ~ b c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c Semantics Q1 2007

  16. VIRTUAL MACHINE SEMANTICS 'The world before 1981' Semantics Q1 2007

  17. Virtual Machines (SMC Expressions) • SMC Machine (Trans. Sys. Semantics): • Configurations:  =  Value Stack  Memories  Control Stack Initially commands/expressions, and later bits of commands/expr's For accumulating partial results and other pieces needed • Value Stack: • Set ranged over by: S • Memories: • Functions ranged over by: M • Control Stack: • Set ranged over by: C ( T  N  Var  BExp  Com )* VAR  N ( Exp  { +, –,  } ...)* Semantics Q1 2007

  18. Notation: Memory Update • Configurations: • Notation: , M  VAR  N • i.e., “Memory Update”  :=    ( Exp  { +, –,  }  ...)* ( T  N ... )* VAR  N M[m/v] m , if v = v’ M’(v’) = M(v’) , otherwise M[m/v] = M’ where Semantics Q1 2007

  19. Virtual Machine Transitions • Configurations: • Transitions: • Defined "by case" according to top of control stack:  :=    ( Exp  { +, –,  }  ...)* ( T  N ... )* VAR  N < S , M , n C >  < n S , M , C > [case n]: < S , M , v C >  < M(v)S , M , C > [case v]: < S , M , e  e’ C >  < S , M , e e’C > [case ee’]: Semantic ‘+’ < m’m S , M ,  C >  < n S , M , C > [case ]: …where n = m  m’ Syntactic ‘+’ Semantics Q1 2007

  20. Virtual Machine: Example • Given program: and memory: (((x + 1) – y)  7) M = [x=9,y=4]  < , M,((x + 1) – y);7;> < , M,(((x + 1) – y) 7)>  < , M,(x + 1);y;–; 7 ; > < , M,((x + 1)–y) ; 7 ; >  < , M,(x+1) ; y ; – ; 7 ; > < , M,x;1;+; y ; – ; 7 ; >  < 9, M, 1 ; + ; y ; – ; 7 ;  > < , M,x; 1 ; + ; y ; – ; 7 ; >  < 9, M,1; + ; y ; – ; 7 ; > < 1; 9, M, + ; y ; – ; 7 ;  >  < 9 ; 1, M, +; y ; – ; 7 ; > < 10, M, y ; – ; 7 ;  >  < 10, M,y; – ; 7 ; > < 4; 10, M, – ; 7 ;  >  < 4;10, M, –; 7 ;  > < 6, M, 7 ;  >  < 6, M,7;  > < 7 ; 6, M, >  < 7 ; 6, M, > < 42, M, > Semantics Q1 2007

  21. VM Semantics: Major Drawbacks! • Advantage: • Easy to implement (and efficient)  • Drawbacks: • “High-level language understood in terms of low-level machine code” • Non-intuitive  • Too concrete (e.g., stack for computation fragments)  • Indirect semantics (not syntax directed)  • Computational step?  “Many other machine along these lines […]. They all have a tendency to pull the syntax into pieces or at any rate to wander around the syntax creating various complex symbolic structures which do not seem particularly forced by the demands of the language itself” - Gordon Plotkin, ‘81 Semantics Q1 2007

  22. ARITHMETIC EXPRESSIONS Semantics Q1 2007

  23. VM: Processing of Additions • Given program: and memory: 2 1 3 ((1 + (2 + 3)) + 4) M  rearrange < , M,(1 + (2 + 3))4+> < , M,((1 + (2 + 3))+4)>  rearrange < , M,(1+(2 + 3))4+ > < , M,1(2 + 3)+4+>  rearrange < , M,1(2 + 3)+4+ > < 1, M,(2 + 3)+4+ >  rearrange < 1, M,(2+3)+4+ > < 1, M,23++4+ >  rearrange < 1, M,23++4+ > < 2 1, M,3++4+ >  rearrange < 2 1, M,3++4+ > < 3 2 1, M,++4+ >  addition1! < 3 2 1, M,++4+ > < 5 1, M,+4+ >  addition2! < 5 1, M,+4+ > < 6, M,4+ > Note: Only three transitions are of real interest as “system events”  rearrange < 6, M,4+ > < 4 6, M, + >  addition3! < 4 6, M, + > < 10, M, > Semantics Q1 2007

  24. Ideal Processing of Additions • Ideally we would like: • i.e., the following transition sequence: explanation  ((1 + (2 + 3)) + 4) ((1 + 5) + 4) explanation  (6 + 4) ((1 + 5) + 4) explanation  (6 + 4) 10 (aka. derivation sequence) (aka. reduction sequence) explanation explanation explanation    ((1 + (2 + 3)) + 4) ((1 + 5) + 4) (6 + 4) 10 Semantics Q1 2007

  25. Informal vs. Formal Specification • Informally: • “One evaluates from left to right...” • Description (pseudo-formally): • CONSTANTS: • Any constant, n, is already evaluated (with itself as value) • SUMS: • Step 1. Evaluate to obtain result ; • Step 2. Evaluate to obtain result ; • Step 3. Add and to obtain final result . n e0 + e1 n0 e0 n1 e1 n0 n1 m Semantics Q1 2007

  26. Inference System Semantics • Inference System Semantics: • Abbreviate as • Meaning: “e terminates and evaluates to v” ‘L’  Exp  N (e,v)  ‘L’ e  v Q: Did I just solve the halting problem here?!? A: No; nobody can decide “e  v” automatically! A: Actually, for Exp termination is decidable… Semantics Q1 2007

  27. Inference System Sem. (cont’d) • Inference System Semantics: ‘L’  Exp  N e0n0 e1n1 [SUM] [CON] m = n0+n1 n n e0 + e1m Semantic ‘+’ Syntactic ‘+’ Semantics Q1 2007

  28. Example Revisited (big-step) • Inference Tree: e0n0 e1n1 [SUM] [CON] m = n0+n1 n n e0 + e1m “big-step semantics” [CON] [CON] 2 2 3 3 [SUM] [CON] 1 1 (2 + 3) 5 [SUM] [CON] (1 + (2 + 3)) 6 4 4 [SUM] ((1 + (2 + 3)) + 4) 10 vs. “small-step semantics” Semantics Q1 2007

  29. Small-step Description • Description: • CONSTANTS: • Any constant, n, is already evaluated (with itself as value) • SUMS: • Step 1. If is not a constant, evaluate it (one step) to obtain result ; • Step 2. If is a constant, but is not, evaluate it (one step) to obtain result ; • Step 3. If and are both constants, add them to obtain result, say . n e0 + e1 e0 e0’ e0 e1 e1’ e0 e1 m Semantics Q1 2007

  30. Small-step Formalization • Transition System Semantics: • Configurations: • Final Configurations: • Transition Relation: • Abbreviate as • Meaning: “e evaluates to e’ in one step” L := Exp TL := N Exp L  Exp  Exp (e,e’)  ‘L’ e  e’ Semantics Q1 2007

  31. Small-step Semantics • Transition System Semantics: L := Exp TL := N Exp L  Exp  Exp e0 e0’ e1 e1’ e0 + e1 e0’ + e1 n0 + e1 n0 + e1’ m = n0+n1 [SUM1] n0 + n1 m [SUM2] [SUM3] We call this a STRUCTURAL OPERATIONAL SEMANTICS Semantics Q1 2007

  32. Example Revisited (small-step) • Transition sequence (with explanation): find predicates [SUM3] make conclusions (2+3)  5 [SUM2] (1 +(2 + 3)) (1 +5) [SUM1] ((1 + (2 + 3))+ 4) ((1 + 5)+ 4) “order of discovery” Semantics Q1 2007

  33. Transition Sequence Information • A transition sequence …specifies two things: • 1. A “sequence of steps” themselves (here additions) • 2. …and reasonswhy they should be performed e  e’  e’’  …  m explanation  ((1 + (2 + 3)) + 4) ((1 + 5) + 4) Semantics Q1 2007

  34. Adding Variables • Language L’: • Arithmetic Expressions(e Exp) • Structural Operational Semantics: • Configurations: • Final Configurations: • Transition Relation… e ::= n | v | e0 + e1 L’ := ExpStore Store = Var  N(= Memory) where Note the change in terminology: Store<---Memory <---M The term Store is more generally accepted (as an abstraction of a computer’s physical memory) TL’ := NStoreL’ <e,> L <e’,'> <e,> A configuration now looks like: Semantics Q1 2007

  35. Small-step Semantics w/ Stores • Structural Operational Semantics (w/Stores): [VAR] m = (v) Store = Var  N < v ,  > < m ,  > < e0 ,  > < e0’ , ’ > [SUM1] < e0 + e1 ,  >  < e0’ + e1 , ’ > < e1 ,  >  < e1’ , ’ > [SUM2] < n0 + e1 ,  >  < n0 + e1’ , ’ > [SUM3] m = n0 + n1 < n0 + n1 ,  > < m ,  > Semantics Q1 2007

  36. In fact: we have noSide-Effects! Easily proved by structural induction [later…] • No side-effects!  e,: <e,>  <e’,’> =>  = ’ [VAR] m = (v) Store = Var  N < v ,  > < m ,  >  < e0 ,  > < e0’ , ’ > [SUM1]  < e0 + e1 ,  >  < e0’ + e1 , ’ >  < e1 ,  >  < e1’ , ’ > [SUM2]  < n0 + e1 ,  >  < n0 + e1’ , ’ > [SUM3] m = n0 + n1 < n0 + n1 ,  > < m ,  > Semantics Q1 2007

  37. Explicit Absence of Side-effects • SOS Semantics: for |_ ee’ <e,>  <e’,> [VAR] m = (v) Store = Var  N  |_ v m |_ e0 e0’ [SUM1] |_ e0 + e1 e0’ + e1 Note: Absence of side-effects is now explicit |_ e1 e1’ [SUM2] |_ n0 + e1 n0 + e1’ [SUM3] m = n0 + n1 |_ n0 + n1  m Semantics Q1 2007

  38. Explicit Absence of Side-effects • Terminology (more common): Note the change in terminology: Env<---Store  <--- [VAR] m = (v)  |_ v m Env = Var  N (= Store) |_ e0 e0’ [SUM1] |_ e0 + e1 e0’ + e1 |_ e1 e1’ [SUM2] |_ n0 + e1 n0 + e1’ [SUM3] m = n0 + n1 |_ n0 + n1  m Semantics Q1 2007

  39. Exp: Behavior and Equivalence • Definitions: • DEF:"Behavior" (eval): • Note: eval is only a partial function(e.g., if we have division; div-by-zero) • DEF:"Program equivalence" (''): • Examples: eval(e,) = m <=>def <e,> E* <m,> e  e’<=>def  : eval(e,) = eval(e’,) 1+1  2 x+y  y+x z+z  2*z Live Exercise: Does "" depend on the semantics (if so, how/where)? Semantics Q1 2007

  40. BOOLEAN EXPRESSIONS Semantics Q1 2007

  41. Boolean Expressions BExp • Language B: • Boolean Expressions(b BExp): • Structural Operational Semantics: • Configurations: • where • Final Configurations: • Transition Relation… • short-hand for b ::= t | e = e’ | b or b’ | ~ b B := BExpEnv Env = Var  N We could also have used a Store (we actually have to if BExp had had side-effects) TB := T EnvB ((b,),(b’,))  B |_ b B b’ Semantics Q1 2007

  42. Boolean Expressions BExp (cont’d) • Structural Operational Semantics: • Note that: • The (this) boolean expression transition system...: • …uses the arithmetic expression transition system…: • ...and does it transitively(E*) in “one step”! [EQ]B |_ e0E*n0 |_ e1E*n1 tt, n0 = n1 t = |_ e0 = e1Bt ff, n0 n1  B , TB , B   E , TE , E  See [Plotkin, p. 42-44] for alternative semantics (with a smaller step size) Semantics Q1 2007

  43. Disjunction: “or” • SOS for Disjunction (“or”): • What about the boolean expression: ...?!? [OR1]B |_ b0Bb0’ |_ b0or b1Bb0’ or b1 [OR2]B |_ b1Bb1’ |_ t0or b1Bt0or b1’ Live Exercise: syntax vs semantic operators? [OR3]B t= t0t1 |_ t0or t1Bt "ttorb" Semantics Q1 2007

  44. Disjunction: Lazy Evaluation • Lazy Semantics for Disjunction (“or”): [OR1]L |_ b0Lb0’ |_ b0or b1Lb0’ or b1 [OR2]L ...exploiting: |_ttor b1Ltt b: ttor b  tt [OR3]L ...exploiting: |_ffor b1Lb1 b: ffor b  b Semantics Q1 2007

  45. Eager Semantics: ‘B’ Lazy Semantics: ‘L’ Eager vs. Lazy Semantics for “or” • Relationship: ? • Stuck configurations? • …e.g. div-by-zero? • Assignment? • Non-termination? b,: |_bB* t <=>|_bL* t b := ttor (3 = 1-2) b := ttor (1 = 2/0) “side-effects” b := ttor (x := tt) b := ttorloop() Semantics Q1 2007

  46. Eager vs. Lazy Semantics (cont’d) • Relationship: ? • Stuck configurations? • …e.g. div-by-zero? • Assignment? • Non-termination? b,: |_bB* t <=>|_bL* t • For the language, BExp, we have that: • Whereas: b := ttor (3 = 1-2) b := ttor (1 = 2/0) “side-effects” b := ttor (x := tt) b := ttorloop() b,: <b,> B* <t,’> ==><b,> L* <t,”> Can be proved by structural induction[later…] b,: <b,> B* <t,’> <==<b,> L* <t,”> Easily disproved by a counterexample (e.g. ) b := ttor (3 = 1-2) Semantics Q1 2007

  47. Parallel Evaluation of Disjunction • Live exercise: • Evaluate BExps non-deterministically(via.interleavingor in parallel) left / right operands to or: • [Think 3 mins; then interactively on the whiteboard] ‘P’ • Relationship: ? • Stuck configurations? • …e.g. div-by-zero? • Assignment? • Non-termination? b,: |_bB* t <=>|_bP* t b := ttor (3 = 1-2) b := ttor (1 = 2/0) “side-effects” b := ttor (x := tt) b := ttorloop() Semantics Q1 2007

  48. Negation: “~b” • Boolean Expressions(b BExp): • Live exercise: • [Think 3 mins; then interactively on the whiteboard] b ::= t | e = e’ | b or b’ | ~ b Semantics Q1 2007

  49. BExp: Behavior and Equivalence • Definitions: • DEF:"Boolean Expression Behavior": • Note: eval is only a partial function(e.g., if we have division; div-by-zero) • DEF:"Boolean Expression Equivalence": • Examples: eval(b,) = t <=>def <b,> B* t b  b’<=>def  : eval(b,) = eval(b’,) b  ~~b 1 = 2  ff x = x  tt Semantics Q1 2007

  50. COMMANDS Semantics Q1 2007

More Related