Download presentation
Presentation is loading. Please wait.
1
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [ brabrand@daimi.au.dk ] [ http://www.daimi.au.dk/~brabrand/ ] S EMANTICS (Q1,’05) W EEK 2: ”STRUCTURAL OPERATIONAL SEMANTICS”
2
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 2 ] S EP 8, 2005 Week 2 - Outline Repetition (week 1) Structural Operational Semantics Expressions (Exp) Big-step vs. Small-step semantics Side-effects Behavior and Equivalence, … Boolean Expressions (BExp) Lazy evaluation, … Commands (Com) … Induction and Structural Induction
3
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 R EPETITION Keywords: inference systems (relations), transition systems, virtual machine semantics
4
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 4 ] S EP 8, 2005 Repetition (“3x3 things…”) Class X: 1x. Transition Systems: determinism vs. non-determinism ? 2x. Inference Systems: premise(s) vs. side-condition(s) ? 3x. SMC Machine: physical memory vs. abstract memory ? Class Y: 1y. Homepage: program relationship figure (example c) ? 2y. Inference Systems: name all applications ! 3y. Prolog: how much Prolog are we expected to know ? Class Z: 1z. Functions: total functions vs. partial functions ? 2z. Prolog: did Prolog really answer “no” for nat(-3) ? 3z. Exercise classes: active learning vs. passive learning… !
5
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 5 ] S EP 8, 2005 Program Relationship (Example c) Program world Model world Concrete Abstract ~ P P’ M M’ 1. P ~ P’ ? 2. abstract 3. M ~ M’ ? 4. relate 5. M ~ M’ ! 6. concretize 7. P ~ P’ !
6
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 6 ] S EP 8, 2005 Should be blatantly obvious, but here goes: Correlation: Investment ~ Benefit Example: Studying ~ Exam result Main Entry: os·mo·sis Pronunciation: äz-'mO-s&s, äs- Function: noun Etymology: New Latin, short for endosmosis 1 : movement of a solvent through a semipermeable membrane (as of a living cell) […] 2 : a process of absorption […] usually effortless often unconscious assimilation Active Learning vs. Passive Learning (aka. Learning-by-Osmosis) “I am sorry for saying this, but…”
7
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 7 ] S EP 8, 2005 Learn by Osmosis It’s Amazing… Do Active Learning: Study = read + make exercises + reflect (for your own sake)! “Learn while you sleep!” “The Semantics Pillow” Only $19,95
8
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 A RITHMETIC E XPRESSIONS Keywords: structural operational semantics (sos) big-step & small-step semantics, side-effects
9
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 9 ] S EP 8, 2005 VM Semantics: Major Drawbacks! Advantage: Easy to implement (and efficient) Drawbacks: Non-intuitive Too concrete (e.g., stack) Indirect semantics (not syntax directed) Computational step? “High-level language understood in terms of low-level machine code” “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
10
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 10 ] S EP 8, 2005 Given program: and memory: VM: Processing of Additions ((1 + (2 + 3)) + 4)M rearrange addition 1 ! addition 2 ! rearrange addition 3 ! rearrange 12 3 Note: Only three transitions are of real interest as “system events”
11
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 11 ] S EP 8, 2005 Ideally we would like: …i.e., the following transition sequence: Ideal Processing of Additions ((1 + (2 + 3)) + 4)((1 + 5) + 4) explanation ((1 + 5) + 4) explanation (6 + 4) explanation 10 ((1 + (2 + 3)) + 4) explanation ((1 + 5) + 4) explanation (6 + 4) explanation 10 (aka. derivation sequence) (aka. reduction sequence)
12
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 12 ] S EP 8, 2005 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 e 0 + e 1 e0e0 n0n0 e1e1 n1n1 n0n0 n1n1 m
13
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 13 ] S EP 8, 2005 Inference System Semantics Inference System Semantics: Abbreviate as Meaning: “ e terminates and evaluates to v” e ve v ‘ L ’ Exp N ( e,v) ‘ L ’ Q: Did I just solve the halting problem here?!? A: No; nobody can decide “ c v ” automatically! A: Actually, for Exp termination is decidable…
14
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 14 ] S EP 8, 2005 Inference System Sem. (cont’d) Inference System Semantics: e 0 + e 1 m m = n 0 + n 1 e0 n0e0 n0 e1 n1e1 n1 [ SUM ] [ CON ] n n ‘ L ’ Exp N Syntactic ‘ + ’ Semantic ‘+’
15
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 15 ] S EP 8, 2005 Example Revisited (big-step) Inference Tree: ((1 + (2 + 3)) + 4) 10 (1 + (2 + 3)) 6 4 4 1 1 (2 + 3) 5 2 2 3 3 e 0 + e 1 m m = n 0 + n 1 e0 n0e0 n0 e1 n1e1 n1 [ SUM ] [ CON ] n n [ SUM ] [ CON ] [ SUM ] [ CON ] “big-step semantics” “small-step semantics” vs.
16
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 16 ] S EP 8, 2005 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 e 0 + e 1 e0e0 e0’e0’ e0e0 e1e1 e1’e1’ e0e0 e1e1 m
17
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 17 ] S EP 8, 2005 Small-step Formalization Transition System Semantics: Configurations: Final Configurations: Transition Relation: Abbreviate as Meaning: “ e evaluates to e’ in one step” L := Exp T L := N Exp L Exp Exp e e’ ( e, e’ ) ‘ L ’
18
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 18 ] S EP 8, 2005 Small-step Semantics Transition System Semantics: L := Exp T L := N Exp L Exp Exp [ SUM 1 ] e 0 + e 1 e 0 ’ + e 1 e 0 e 0 ’ [ SUM 2 ] n 0 + e 1 n 0 + e 1 ’ e 1 e 1 ’ [ SUM 3 ] n 0 + n 1 m m = n 0 + n 1 We call this a STRUCTURAL OPERATIONAL SEMANTICS
19
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 19 ] S EP 8, 2005 Example Revisited (small-step) Transition sequence (with explanation): ((1 + (2 + 3)) + 4) ((1 + 5) + 4) (1 + (2 + 3)) (1 + 5) (2 + 3) 5 [ SUM 1 ] [ SUM 2 ] [ SUM 3 ] “order of discovery” find predicatesmake conclusions
20
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 20 ] S EP 8, 2005 Transition Sequence Information A transition sequence …specifies two things: 1. A “sequence of steps” themselves (here additions) 2. …and reasons why they should be performed e e’ e’’ … m ((1 + (2 + 3)) + 4) explanation ((1 + 5) + 4)
21
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 21 ] S EP 8, 2005 Adding Variables Language L’: Arithmetic Expressions ( e Exp) Structural Operational Semantics: Configurations: Final Configurations: Transition Relation… e ::= n | v | e 0 + e 1 L’ := Exp Store T L’ := L’ Note the change in terminology: Store <= Memory <= M The term Store is more generally accepted (as an abstraction of a computer’s physical memory) Store = Var N (= Memory) where A configuration now looks like: L’
22
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 22 ] S EP 8, 2005 Small-step Semantics w/ Stores Structural Operational Semantics (w/ Stores): [ SUM 1 ] [ SUM 2 ] [ SUM 3 ] m = n 0 + n 1 [ VAR ] Store = Var N m = (v)
23
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 23 ] S EP 8, 2005 Note: No Side-Effects! No side-effects! [ SUM 1 ] [ SUM 2 ] [ SUM 3 ] m = n 0 + n 1 [ VAR ] Store = Var N m = (v) e, : => = ’ Easily proved by structural induction [later…]
24
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 24 ] S EP 8, 2005 Explicit Absence of Side-effects SOS Semantics: [ SUM 1 ] | _ e 0 + e 1 e 0 ’ + e 1 | _ e 0 e 0 ’ [ SUM 2 ] [ SUM 3 ] | _ n 0 + n 1 m m = n 0 + n 1 | _ n 0 + e 1 n 0 + e 1 ’ | _ e 1 e 1 ’ | _ v m [ VAR ] Store = Var N m = (v) | _ e e’ Note: Absence of side- effects is now explicit for
25
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 25 ] S EP 8, 2005 Explicit Absence of Side-effects Terminology (more common): [ SUM 1 ] | _ e 0 + e 1 e 0 ’ + e 1 | _ e 0 e 0 ’ [ SUM 2 ] [ SUM 3 ] | _ n 0 + n 1 m m = n 0 + n 1 | _ n 0 + e 1 n 0 + e 1 ’ | _ e 1 e 1 ’ | _ v m [ VAR ] Env = Var N (= Store) m = (v) Note the change in terminology: Env <= Store <=
26
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 26 ] S EP 8, 2005 Exp: Behavior and Equivalence Definitions: Behavior: Note: eval is only a partial function if we have division (div-by-zero) Equivalence: Examples: eval(e, ) = m E * e e’ : eval(e, ) = eval(e’, ) 1+1 2x+y y+x Can be proved by structural induction [soon…] z+z 2*z
27
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 B OOLEAN E XPRESSIONS Keywords: eager evaluation, lazy evaluation, parallel evaluation
28
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 28 ] S EP 8, 2005 Boolean Expressions BExp Language B: Boolean Expressions ( b BExp): Structural Operational Semantics: Configurations: where Final Configurations: Transition Relation… as short-hand for b ::= t | e = e’ | b or b’ | ~ b B := BExp Env T B := B Env = Var N | _ b B b’ We could also have used a Store (we actually have to if BExp had had side-effects) (b, ,b’, ) B
29
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 29 ] S EP 8, 2005 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”! | _ e 0 = e 1 B t [ EQ ] B | _ e 0 E * n 0 | _ e 1 E * n 1 B, T B, B E, T E, E t = tt, n 0 = n 1 ff, n 0 n 1 See [Plotkin, p. 31] for alternative semantics
30
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 30 ] S EP 8, 2005 Disjunction: “or” SOS for Disjunction (“or”): | _ b 0 or b 1 B b 0 ’ or b 1 | _ b 0 B b 0 ’ [ OR 1 ] B | _ t 0 or b 1 B t 0 or b 1 ’ | _ b 1 B b 1 ’ [ OR 2 ] B | _ t 0 or t 1 B t [ OR 3 ] B t = t 0 t 1 Note: but what about ?!? tt or
31
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 31 ] S EP 8, 2005 Disjunction: Lazy Evaluation Lazy Semantics for Disjunction (“or”): | _ b 0 or b 1 L b 0 ’ or b 1 | _ b 0 L b 0 ’ [ OR 1 ] L [ OR 2 ] L | _ ff or b 1 L b 1 [ OR 3 ] L b: tt or b tt | _ tt or b 1 L tt Exploit:
32
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 32 ] S EP 8, 2005 Eager vs. Lazy Semantics for “or” Eager Semantics: ‘ B ’ Lazy Semantics: ‘ L ’ Relationship : ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination? b := tt or (3 = 1-2) b, : | _ b B * t | _ b L * t b := tt or (1 = 2/0) b := tt or (x := tt) “side-effects” b := tt or loop()
33
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 33 ] S EP 8, 2005 For the language, BExp, we have that: Whereas: Eager vs. Lazy Semantics (cont’d) b, : B * ==> L * b, : B * L * Can be proved by structural induction [soon…] Easily disproved by a counterexample (e.g. ) Relationship : ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination? b := tt or (3 = 1-2) b, : | _ b B * t | _ b L * t b := tt or (1 = 2/0) b := tt or (x := tt) “side-effects” b := tt or loop() b := tt or (3 = 1-2)
34
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 34 ] S EP 8, 2005 Parallel Evaluation of Disjunction Idea: Evaluate non-deterministically (via. interleaving or in parallel) left / right operands to or: Live exercise… :) [Think 3 mins; then interactively on the blackboard] ‘P’‘P’
35
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 35 ] S EP 8, 2005 Negation: “ ~b ” Boolean Expressions ( b BExp): Live exercise… :) [Think 3 mins; then interactively on the blackboard] b ::= t | e = e’ | b or b’ | ~ b
36
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 36 ] S EP 8, 2005 BExp: Behavior and Equivalence Definitions: Behavior: Note: eval is only a partial function if we have division (div-by-zero) Equivalence: Examples: eval(b, ) = t B * t b b’ : eval(b, ) = eval(b’, ) b ~~b1 = 2 ff Can be proved by structural induction [soon…] x = x tt
37
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C OMMANDS Keywords: Assignments, Conditionals, Loops, Control flow
38
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 38 ] S EP 8, 2005 SOS of Commands Commands ( c Com): Structural Operational Semantics: Configurations: where Final Configurations: (?) Transition Relation… for c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c C := (Com Store) T C := C Store = Var N C Here we need a Store (since we have to model side-effects) ((c, ),(c’, ’)) C Problem: how do we end computation?!?
39
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 39 ] S EP 8, 2005 SOS of Commands (cont’d) Commands ( c Com): Structural Operational Semantics: Configurations: where Final Configurations: (!) Transition Relation… for c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c C := (Com Store) Store T C := Store Store = Var N C ((c, ),(c’, ’)) C C ’ ((c, ), ’) C (!)(!) for and Here we need a Store (since we have to model side-effects)
40
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 40 ] S EP 8, 2005 Nil and Assignment (SOS) SOS for nil : SOS for assignment: v := e C ’ A * [ ASS ] C ’ = [m/v] C [ NIL ] C
41
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 41 ] S EP 8, 2005 Sequential Composition (SOS) SOS for Sequential Composition: Diagramatically: “(Control) flow-charts” c 0 ; c 1 C [ SEQ 1 ] C C C ’ [ SEQ 2 ] C c c’
42
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 42 ] S EP 8, 2005 If-then-else (SOS) SOS for “ if-then-else ”: Diagramatically: “(Control) flow-charts” if b then c else c’ C B * [ IF 1 ] C C B * [ IF 2 ] C c c’ b tt ff
43
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 43 ] S EP 8, 2005 While-do (SOS) SOS for “ while-do ”: Diagramatically: “(Control) flow-charts” while b do c C B * [ WH 1 ] C C B * [ WH 2 ] C tt ff c b
44
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 44 ] S EP 8, 2005 Com: Behavior and Equivalence Definitions: Behavior: Note: exec is only a partial function (e.g. non-termination, div-by-zero, …) Equivalence: Examples: exec(c, ) = ’ C * ’ c c’ : exec(c, ) = exec(c’, ) if b then c else c’ if ~b then c’ else c while ff do c nil Can be proved by structural induction [v. soon…]
45
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 S TRUCTURAL I NDUCTION Keywords: Induction, base case, induction hypothesis, induction step structural induction
46
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 46 ] S EP 8, 2005 Principle of Mathematical Induction Let P be a predicate (i.e. a boolean function): then we have that: Intuitive: ? P: N { true, false } n N : P(n) P(0) induction stepbase case Principle of mathematical induction: P(n) P(n+1) P(3) P(0)P(0) => P(1)P(1) => P(2)P(2) => P(3)
47
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 47 ] S EP 8, 2005 Example Induction Proof Example: Base case (i.e. prove P(0) ): Induction step (i.e. prove P(n) => P(n+1) ): Assume the induction hypothesis (I.H.) (i.e. assume P(n) ): Now prove P(n+1) : P(n) [ 2 0 + 2 1 + … + 2 n = 2 n+1 – 1 ] P(0) [ 2 0 = 2 0+1 – 1 ] [ 2 0 + 2 1 + … + 2 n = 2 n+1 – 1 ] [ 2 0 + 2 1 + … + 2 n+1 = 2 (n+1)+1 – 1 ] 2 0 + 2 1 + … + 2 n + 2 n+1 (2 0 + 2 1 + … + 2 n ) + 2 n+1 = (2 n+1 – 1) + 2 n+1 == 2*2 n+1 – 1 = 2 (n+1)+1 – 1 I.H.
48
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 48 ] S EP 8, 2005 Structural Induction (for Exp) Given: Arithmetic Expressions ( e Exp) e ::= n | v | e 0 +e 1 e Exp : P(e) P(n) composite (inductive) case base cases Principle of structural induction: P(e 0 ) P(e 1 ) P(e 0 +e 1 ) P(v) and
49
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 49 ] S EP 8, 2005 Structural Induction (for BExp) Boolean Expressions ( b BExp): Live exercise… :) [Think 3 mins; then interactively on the blackboard] b ::= t | e = e’ | b or b’ | ~ b
50
C LAUS B RABRAND © S EMANTICS (Q1,’05) [ 50 ] S EP 8, 2005 More on this next week… More on Structural Induction next week…
51
C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 Next week: type checking, defn’s and decl’s Any Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.