Download presentation
Presentation is loading. Please wait.
1
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ http://www.daimi.au.dk/~brabrand/ ] S EMANTICS (Q1,’06) W EEK 2: ”STRUCTURAL OPERATIONAL SEMANTICS”
2
C LAUS B RABRAND [ 2 ] S EMANTICS (Q1,’06) S EP 7, 2006 Week 2 - Outline 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) …
3
C LAUS B RABRAND [ 3 ] S EMANTICS (Q1,’06) S EP 7, 2006 Active vs. Passive Learning: Correlation: Investment ~ Benefit Example: Studying ~ Exam result Active vs. Passive Learning 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 LearningPassive Learning aka. "Learning-by-Osmosis" vs.
4
C LAUS B RABRAND [ 4 ] S EMANTICS (Q1,’06) S EP 7, 2006 "Learning 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
5
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 W HY S EMANTICS ?!? Keywords: Why bother with semantics?!?
6
C LAUS B RABRAND [ 6 ] S EMANTICS (Q1,’06) S EP 7, 2006 Why bother learning Semantics?!? Why Semantics?!? Fully understand a prog. lang. Semantics- preserving prog. changes design new prog. lang.s rapidly learn new prog. lang. debugging programs cross- compilation automated optimization automated error detection write faster programs write "better" programs $$ $$$$ Pass this course
7
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 S YNTAX VS S EMANTICS Keywords: Structure vs. Meaning
8
C LAUS B RABRAND [ 8 ] S EMANTICS (Q1,’06) S EP 7, 2006 Syntax and Semantics Syntax: = Structure vs. Semantics: = Meaning "Eats shoots and leaves" "Colorless Green Ideas Sleep Furiously" -- N. Chomsky (1957) The syntactic structure affects our interpretation ; ; Has legal syntactic structure, but no meaning ( ),,
9
C LAUS B RABRAND [ 9 ] S EMANTICS (Q1,’06) S EP 7, 2006 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...): "Time flies like an arrow " "Fruit flies like a banana " ---------N --------V-----------N-------P -------------------N -------V---------------N But, if I replace two nouns (syntactic) replace noun
10
C LAUS B RABRAND [ 10 ] S EMANTICS (Q1,’06) S EP 7, 2006 Level of Abstraction Semantic specification(s): Appropriate level of abstraction:...so that we can use the semantics for something !!! is_busy is_busy is_done higher level of abstraction ..., transistor 57824 = 5V, transistor 57825 = 0V,... ..., transistor 57824 = 5V, transistor 57825 = 5V,... z z z
11
C LAUS B RABRAND [ 11 ] S EMANTICS (Q1,’06) S EP 7, 2006 Glasses of Abstraction… It’s Amazing… See behind concrete details; Perceive only what is relevant and at the appropriate level of abstraction)! Only $12,95 “See only the relevant!” “ The Glasses of Abstraction ”
12
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 T HE L ANGUAGE “L” Keywords: Expressions, Boolean Expressions, Commands, Abstract Syntax, Concrete Syntax
13
C LAUS B RABRAND [ 13 ] S EMANTICS (Q1,’06) S EP 7, 2006 The Language ”L” Basic Syntactic Sets: Truthvalues: Set ranged over by: t, t’, t 0, … Numbers: Set ranged over by: m, n, … Variables: Set ranged over by: v, v’, … T = { tt, ff } N = { 0, 1, 2, …} VAR = { a, b, c, …, z } Meta-variables
14
C LAUS B RABRAND [ 14 ] S EMANTICS (Q1,’06) S EP 7, 2006 Alice in Wonderland Different levels of abstraction: Things ‘A Sitting on a Gate’ Names of things ‘The Aged Aged Man.’ Things are called something ‘Ways and Means’ Names are called something ‘Haddocks’ Eyes.’ "[...] The name of the song is called ‘Haddocks’ Eyes.’ ” “Oh, that’s the name of the song, is it?” Alice said, trying to feel interested. “No, you don’t understand,” the Kinght said, looking a little vexed. “That’s what the name is called. The name really is ‘The Aged Aged Man.’ ” “Then I ought to have said, ‘That’s what the song is called’?” Alice corrected herself. “No, you oughtn’t: that’s another thing. The song is called ‘Ways and Means’ : but that’s only what it’s called, you know!” “Well, what is the song, then?” said Alice, who was by this time completely bewildered. “I was coming to that,” the Knight said. “The song really is ‘A Sitting on a Gate’ : and the tune’s my own invention.”
15
C LAUS B RABRAND [ 15 ] S EMANTICS (Q1,’06) S EP 7, 2006 The Language ”L” Derived Syntactic Sets: Arithmetic Expressions ( e Exp): Boolean Expressions ( b BExp): Commands ( c Com): 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
16
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 V IRTUAL M ACHINE S EMANTICS Keywords: The “SMC” Machine, Virtual Machine Semantics, Machine Code Semantics 'The world before 1981'
17
C LAUS B RABRAND [ 17 ] S EMANTICS (Q1,’06) S EP 7, 2006 Value Stack: Set ranged over by: S Memories: Functions ranged over by: M Control Stack: Set ranged over by: C SMC Machine (Trans. Sys. Semantics): Configurations: Virtual Machines ( SMC Expressions ) = Value Stack Memories Control Stack ( T N ... )* VAR N ( Exp { +, –, } ... )* Initially commands/expressions, and later bits of commands/expr's For accumulating partial results and other pieces needed
18
C LAUS B RABRAND [ 18 ] S EMANTICS (Q1,’06) S EP 7, 2006 Configurations: Notation:, M VAR N i.e., “Memory Update” More common syntax for this: Notation: Memory Update = ( T N ... )* VAR N ( Exp { +, –, } ...)* m, if v = v’ M’(v’) = M(v’), otherwise M[m/v] M[m/v] = M’ where M[m v]
19
C LAUS B RABRAND [ 19 ] S EMANTICS (Q1,’06) S EP 7, 2006 Virtual Machine Transitions Configurations: Transitions: Defined "by case" according to top of control stack: [case n ]: [case v ]: [case e e’ ]: [case ]: …where n = m m’ Syntactic ‘+’ Semantic ‘+’ = ( T N ... )* VAR N ( Exp { +, –, } ...)*
20
C LAUS B RABRAND [ 20 ] S EMANTICS (Q1,’06) S EP 7, 2006 Virtual Machine: Example Given program: and memory: (((x + 1) – y) 7) M = [x=9,y=4]
21
C LAUS B RABRAND [ 21 ] S EMANTICS (Q1,’06) S EP 7, 2006 VM Semantics: Major Drawbacks! Advantage: Easy to implement (and efficient) Drawbacks: Non-intuitive Too concrete (e.g., stack for computation fragments) 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
22
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 A RITHMETIC E XPRESSIONS Keywords: structural operational semantics (sos) big-step & small-step semantics, side-effects
23
C LAUS B RABRAND [ 23 ] S EMANTICS (Q1,’06) S EP 7, 2006 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”
24
C LAUS B RABRAND [ 24 ] S EMANTICS (Q1,’06) S EP 7, 2006 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)
25
C LAUS B RABRAND [ 25 ] S EMANTICS (Q1,’06) S EP 7, 2006 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
26
C LAUS B RABRAND [ 26 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 “ e v ” automatically! A: Actually, for Exp termination is decidable…
27
C LAUS B RABRAND [ 27 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 ‘+’ Live exercise: explain difference between syntactic and semantic entities above...
28
C LAUS B RABRAND [ 28 ] S EMANTICS (Q1,’06) S EP 7, 2006 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.
29
C LAUS B RABRAND [ 29 ] S EMANTICS (Q1,’06) S EP 7, 2006 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
30
C LAUS B RABRAND [ 30 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 ’
31
C LAUS B RABRAND [ 31 ] S EMANTICS (Q1,’06) S EP 7, 2006 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
32
C LAUS B RABRAND [ 32 ] S EMANTICS (Q1,’06) S EP 7, 2006 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
33
C LAUS B RABRAND [ 33 ] S EMANTICS (Q1,’06) S EP 7, 2006 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)
34
C LAUS B RABRAND [ 34 ] S EMANTICS (Q1,’06) S EP 7, 2006 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’
35
C LAUS B RABRAND [ 35 ] S EMANTICS (Q1,’06) S EP 7, 2006 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)
36
C LAUS B RABRAND [ 36 ] S EMANTICS (Q1,’06) S EP 7, 2006 In fact: we have 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…]
37
C LAUS B RABRAND [ 37 ] S EMANTICS (Q1,’06) S EP 7, 2006 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
38
C LAUS B RABRAND [ 38 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 <---
39
C LAUS B RABRAND [ 39 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 e’ def : eval(e, ) = eval(e’, ) 1+1 2x+y y+xz+z 2*z Live Exercise: Does " " depend on the semantics (if so, how/where)?
40
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 B OOLEAN E XPRESSIONS Keywords: eager evaluation, lazy evaluation, parallel evaluation
41
C LAUS B RABRAND [ 41 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 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
42
C LAUS B RABRAND [ 42 ] S EMANTICS (Q1,’06) S EP 7, 2006 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. 42-44] for alternative semantics (with a smaller step size)
43
C LAUS B RABRAND [ 43 ] S EMANTICS (Q1,’06) S EP 7, 2006 Disjunction: “or” SOS for Disjunction (“or”): What about the boolean expression:...?!? | _ 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 "tt or b" Live Exercise: syntax vs semantic operators?
44
C LAUS B RABRAND [ 44 ] S EMANTICS (Q1,’06) S EP 7, 2006 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...exploiting: b: ff or b b...exploiting:
45
C LAUS B RABRAND [ 45 ] S EMANTICS (Q1,’06) S EP 7, 2006 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()
46
C LAUS B RABRAND [ 46 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 [later…] 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)
47
C LAUS B RABRAND [ 47 ] S EMANTICS (Q1,’06) S EP 7, 2006 Parallel Evaluation of Disjunction Live exercise: Evaluate BExps non-deterministically (via. interleaving or in parallel) left / right operands to or: [Think 3 mins; then interactively on the blackboard] ‘P’‘P’ b := tt or (3 = 1-2) b := tt or (1 = 2/0) b := tt or (x := tt) “side-effects” b := tt or loop() Relationship : ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination? b, : | _ b B * t | _ b P * t
48
C LAUS B RABRAND [ 48 ] S EMANTICS (Q1,’06) S EP 7, 2006 Negation: “ ~b ” Boolean Expressions ( b BExp): Live exercise: [Think 3 mins; then interactively on the blackboard] b ::= t | e = e’ | b or b’ | ~ b
49
C LAUS B RABRAND [ 49 ] S EMANTICS (Q1,’06) S EP 7, 2006 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 * t b b’ def : eval(b, ) = eval(b’, ) b ~~b1 = 2 ffx = x tt
50
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C OMMANDS Keywords: Assignments, Conditionals, Loops, Control flow
51
C LAUS B RABRAND [ 51 ] S EMANTICS (Q1,’06) S EP 7, 2006 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?!?
52
C LAUS B RABRAND [ 52 ] S EMANTICS (Q1,’06) S EP 7, 2006 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)
53
C LAUS B RABRAND [ 53 ] S EMANTICS (Q1,’06) S EP 7, 2006 Nil and Assignment (SOS) SOS for nil : SOS for assignment: v := e C ’ A * [ ASS ] C ’ = [m/v] C [ NIL ] C
54
C LAUS B RABRAND [ 54 ] S EMANTICS (Q1,’06) S EP 7, 2006 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’
55
C LAUS B RABRAND [ 55 ] S EMANTICS (Q1,’06) S EP 7, 2006 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
56
C LAUS B RABRAND [ 56 ] S EMANTICS (Q1,’06) S EP 7, 2006 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
57
C LAUS B RABRAND [ 57 ] S EMANTICS (Q1,’06) S EP 7, 2006 Com: Behavior and Equivalence Definitions: DEF: "Command Behavior": Note: exec is only a partial function (e.g. non-termination, div-by-zero, …) DEF: "Command Equivalence": Examples: exec(c, ) = ’ def C * ’ c c’ def : exec(c, ) = exec(c’, ) if b then c else c’ if ~b then c’ else c while ff do c nil
58
C LAUS B RABRAND [ 58 ] S EMANTICS (Q1,’06) S EP 7, 2006 "Three minutes paper" Please spend three minutes writing down the most important things that you have learned today (now). After 1 day After 1 week After 3 weeks After 2 weeks Immediately
59
C LAUS B RABRAND [ 59 ] S EMANTICS (Q1,’06) S EP 7, 2006
60
C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 Next week: big-step vs. small-step, errors, type checking Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.