1 Semantics Q S EMANTICS (Q1,’07) Week 2 Jacob Andersen PhD student
2 Semantics Q 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) »…»…
3 Semantics Q H IGHLIGHTS FROM LAST WEEK
4 Semantics Q Correlation: Investment ~ Benefit Ex: Studying ~ Exam result Do Active Learning: Study = read + make exercises + reflect (for your own sake)! Active vs. Passive Learning Active LearningPassive Learning vs. Source: J. Biggs, “Teaching for quality learning at university”
5 Semantics Q 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
6 Semantics Q Relations A k-ary relation, L, over the sets S 1, S 2, …, S k is a subset L S 1 S 2 … S k
7 Semantics Q Inference Systems Inference System: Inductive (recursive) specification of relations Consists of axioms and rules | _ even n | _ even m m = n+2 premise(s) conclusion side-condition(s) Note: The “ “ is actually just another way of writing a “ ” !
8 Semantics Q Transition Systems A Transition System is a structure: is a set of configurations is a binary relation (called the transition relation) Alternative flavours: –Labelled TS –Terminal TS –Etc… , , , T where T and t T : : t , A, and A
9 Semantics Q S YNTAX VS S EMANTICS
10 Semantics Q Syntax and Semantics Syntax: = Structure – vs. – Semantics: = Meaning "Eats shoots and leaves" "Colorless Green Ideas Sleep Furiously" -- N. Chomsky (1957) ; ; Has legal syntactic structure, but no meaning ( ),,
11 Semantics Q 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
12 Semantics Q 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 appropriate level of abstraction)! is_busy is_busy is_done higher level of abstraction ..., transistor = 5V, transistor = 0V,... ..., transistor = 5V, transistor = 5V,... z z z
13 Semantics Q T HE L ANGUAGE “L”
14 Semantics Q The Language ”L” Basic Syntactic Sets: Truthvalues: ranged over by: t, t’, t 0, … Numbers: ranged over by: m, n, … Variables: ranged over by: v, v’, … T = { tt, ff } N = { 0, 1, 2, …} VAR = { a, b, c, …, z } Meta-variables
15 Semantics Q 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 i.e. Exp = “all ASTs generated by this grammar”
16 Semantics Q V IRTUAL M ACHINE S EMANTICS 'The world before 1981'
17 Semantics Q 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 BExp Com )* VAR N ( Exp { +, –, } ... )* Initially commands/expressions, and later bits of commands/expr's For accumulating partial results and other pieces needed
18 Semantics Q Configurations: Notation:, M VAR N –i.e., “Memory Update” 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
19 Semantics Q 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 Semantics Q Virtual Machine: Example Given program: and memory: (((x + 1) – y) 7) M = [x=9,y=4]
21 Semantics Q 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
22 Semantics Q A RITHMETIC E XPRESSIONS
23 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 ‘+’
28 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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’ := N Store 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q B OOLEAN E XPRESSIONS
41 Semantics Q 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 := T Env 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 Semantics Q 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 ] for alternative semantics (with a smaller step size)
43 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q –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 Semantics Q 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 whiteboard] ‘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 Semantics Q Negation: “ ~b ” Boolean Expressions ( b BExp): – Live exercise: [Think 3 mins; then interactively on the whiteboard] b ::= t | e = e’ | b or b’ | ~ b
49 Semantics Q 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 Semantics Q C OMMANDS
51 Semantics Q 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) TC CTC 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 Semantics Q 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 C 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 Semantics Q Nil and Assignment (SOS) SOS for nil : SOS for assignment: v := e C ’ A * [ ASS ] C ’ = [m/v] C [ NIL ] C
54 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q 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 Semantics Q "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 Right away
59 Semantics Q Remember to sign up or check that you have been signed up for the exam !!! Sept
60 Semantics Q Next week: big-step vs. small-step, errors, type checking Any Questions?