1 Semantics Q1 2007 S EMANTICS (Q1,’07) Week 3 Jacob Andersen PhD student [email protected].
S EMANTICS (Q1,’07) Week 2
description
Transcript of S EMANTICS (Q1,’07) Week 2
2
Semantics Q1 2007
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 Q1 2007
HIGHLIGHTS FROM LAST WEEK
4
Semantics Q1 2007
• Correlation:• Investment ~ Benefit
Ex: Studying ~ Exam result
• Do Active Learning:• Study = read + make exercises
+ reflect (for your own sake)!
Active vs. Passive Learning
Active Learning Passive Learning
vs.
Source: J. Biggs, “Teaching for quality learning at university”
5
Semantics Q1 2007
Why bother learning Semantics?!?
Why Semantics?!?
Fully understand
a prog. lang.
Semantics-preserving
prog. changes
designnewprog. lang.s
rapidly
learn new
prog. lang.debuggingprograms
cross-
compilationautomatedoptimization
automatederror detection
writefaster
programs
write"better"
programs
$$
$$$$
Pass thiscourse
6
Semantics Q1 2007
Relations• A k-ary relation, L, over the sets
S1, S2, … , Sk
is a subsetL S1 S2 … Sk
7
Semantics Q1 2007
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 Q1 2007
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 Q1 2007
SYNTAX VS SEMANTICS
10
Semantics Q1 2007
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
( )
, ,
11
Semantics Q1 2007
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 replace noun
12
Semantics Q1 2007
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
high
er le
vel o
f ab
stra
ctio
n
..., transistor57824 = 5V , transistor57825 = 0V , ...
..., transistor57824 = 5V , transistor57825 = 5V , ...
zzz
13
Semantics Q1 2007
THE LANGUAGE “L”
14
Semantics Q1 2007
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’, …
T = {tt, ff}
N = {0, 1, 2, …}
VAR = {a, b, c, …, z}
Meta-variables
15
Semantics Q1 2007
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 Q1 2007
VIRTUAL MACHINE SEMANTICS
'The world before 1981'
17
Semantics Q1 2007
• 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 resultsand other pieces needed
18
Semantics Q1 2007
• 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 Q1 2007
Virtual Machine Transitions• Configurations:
• Transitions:• Defined "by case" according to top of control stack:
[case n]: < S , M , n C > < n S , M , C >
[case v]: < S , M , v C > < M(v) S , M , C >
[case ee’]: < S , M , e e’ C > < S , M , e e’ C >
[case ]: < m’ m S , M , C > < n S , M , C > …where n = m m’
Syntactic ‘+’
Semantic ‘+’
:=
( T N ... )* VAR N ( Exp { +, –, } ...)*
20
Semantics Q1 2007
Virtual Machine: Example• Given program: and memory:(((x + 1) – y) 7)
< , M, (((x + 1) – y) 7) >
< , M, (x + 1) ; y ; – ; 7 ; >
< , M, ((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 ; >
< 9, M, 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, >
21
Semantics Q1 2007
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 Q1 2007
ARITHMETIC EXPRESSIONS
23
Semantics Q1 2007
• Given program: and memory:
VM: Processing of Additions
< , M, ((1 + (2 + 3)) + 4) > < , M, (1 + (2 + 3)) 4 + >
< , M, (1 + (2 + 3)) 4 + > < , M, 1 (2 + 3) + 4 + >
< , M, 1 (2 + 3) + 4 + > < 1, M, (2 + 3) + 4 + >
< 1, M, (2 + 3) + 4 + > < 1, M, 2 3 + + 4 + >
< 1, M, 2 3 + + 4 + > < 2 1, M, 3 + + 4 + >
< 2 1, M, 3 + + 4 + > < 3 2 1, M, + + 4 + >
< 3 2 1, M, + + 4 + > < 5 1, M, + 4 + >
< 5 1, M, + 4 + > < 6, M, 4 + >
< 6, M, 4 + > < 4 6, M, + >
< 4 6, M, + > < 10, M, >
((1 + (2 + 3)) + 4) M
rearrange
rearrange
addition1!
addition2!
rearrange
addition3!
rearrange
rearrange
rearrange
rearrange
12 3
Note: Only three transitions are of real interest as “system events”
24
Semantics Q1 2007
• 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)
(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 Q1 2007
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
e0 n0
e1 n1
n0 n1 m
26
Semantics Q1 2007
Inference System Semantics
• Inference System Semantics:• Abbreviate as
» Meaning: “e terminates and evaluates to v”
e 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 Q1 2007
Inference System Sem. (cont’d)
• Inference System Semantics:
e0 + e1 mm = n0 + n1
e0 n0 e1 n1[SUM][CON] n n
‘L’ Exp N
Syntactic ‘+’Semantic ‘+’
28
Semantics Q1 2007
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
e0 + e1 mm = n0 + n1
e0 n0 e1 n1[SUM][CON] n n
[SUM]
[CON]
[CON]
[SUM]
[SUM]
[CON] [CON]“big-step semantics”
“small-step semantics”vs.
29
Semantics Q1 2007
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
30
Semantics Q1 2007
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’(e,e’) ‘L’
31
Semantics Q1 2007
Small-step Semantics
• Transition System Semantics:
L := Exp
TL := N Exp
L Exp Exp
[SUM1]
e0 + e1 e0’ + e1
e0 e0’
[SUM2]
n0 + e1 n0 + e1’
e1 e1’
[SUM3]
n0 + n1 mm = n0 + n1
We call this a STRUCTURAL OPERATIONAL SEMANTICS
32
Semantics Q1 2007
Example Revisited (small-step)
• Transition sequence (with explanation):
((1 + (2 + 3)) + 4) ((1 + 5) + 4)
(1 + (2 + 3)) (1 + 5)
(2 + 3) 5
[SUM1]
[SUM2]
[SUM3]
“order of discovery”
find predicates make conclusions
33
Semantics Q1 2007
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 Q1 2007
Adding Variables• Language L’:
– Arithmetic Expressions (e Exp)•
• Structural Operational Semantics:– Configurations:
–
– Final Configurations:–
– Transition Relation…–
e ::= n | v | e0 + e1
L’ := Exp Store
TL’ := N Store L’
Note the change in terminology: Store <--- Memory <--- MThe 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: <e,><e,> L <e’,'>
35
Semantics Q1 2007
Small-step Semantics w/ Stores• Structural Operational Semantics (w/ Stores):
•
•
•
•
[SUM1]< e0 + e1 , > < e0’ + e1 , ’ >
< e0 , > < e0’ , ’ >
[SUM2]
[SUM3]< n0 + n1 , > < m , >
m = n0 + n1
< n0 + e1 , > < n0 + e1’ , ’ >< e1 , > < e1’ , ’ >
< v , > < m , >[VAR]
Store = Var Nm = (v)
36
Semantics Q1 2007
In fact: we have no Side-Effects!
• No side-effects! •
•
•
•
[SUM1]< e0 + e1 , > < e0’ + e1 , ’ >
< e0 , > < e0’ , ’ >
[SUM2]
[SUM3]< n0 + n1 , > < m , >
m = n0 + n1
< n0 + e1 , > < n0 + e1’ , ’ >< e1 , > < e1’ , ’ >
< v , > < m , >[VAR]
Store = Var Nm = (v)
e,: <e,> <e’,’> => = ’Easily proved by structural induction [later…]
37
Semantics Q1 2007
Explicit Absence of Side-effects• SOS Semantics:
•
•
•
•
[SUM1]
|_ e0 + e1 e0’ + e1
|_ e0 e0’
[SUM2]
[SUM3] |_ n0 + n1 m
m = n0 + n1
|_ n0 + e1 n0 + e1’
|_ e1 e1’
|_ v m
[VAR]Store = Var Nm = (v)
|_ e e’ <e,> <e’,>
Note: Absence of side-effects is now explicit
for
38
Semantics Q1 2007
Explicit Absence of Side-effects• Terminology (more common):
•
•
•
•
[SUM1]
|_ e0 + e1 e0’ + e1
|_ e0 e0’
[SUM2]
[SUM3] |_ n0 + n1 m
m = n0 + n1
|_ n0 + e1 n0 + e1’
|_ e1 e1’
|_ v m
[VAR]
Env = Var N (= Store)m = (v)
Note the change in terminology: Env <--- Store <---
39
Semantics Q1 2007
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)?
40
Semantics Q1 2007
BOOLEAN EXPRESSIONS
41
Semantics Q1 2007
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
TB := 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 Q1 2007
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”!
|_ e0 = e1 B t
[EQ]B |_ e0 E
* n0 |_ e1 E* n1
B , TB , B
E , TE , E
t = tt, n0 = n1
ff, n0 n1
See [Plotkin, p. 42-44] for alternative semantics (with a smaller step size)
43
Semantics Q1 2007
Disjunction: “or”• SOS for Disjunction (“or”):
•
•
•
• What about the boolean expression: ...?!?
|_ b0 or b1 B b0’ or b1
|_ b0 B b0’[OR1]B
|_ t0 or b1 B t0 or b1’
|_ b1 B b1’[OR2]B
|_ t0 or t1 B t
[OR3]B t = t0 t1
"tt or b"
Live Exercise:syntax vs semanticoperators?
44
Semantics Q1 2007
Disjunction: Lazy Evaluation• Lazy Semantics for Disjunction (“or”):
•
•
•
|_ b0 or b1 L b0’ or b1
|_ b0 L b0’[OR1]L
[OR2]L
|_ ff or b1 L b1
[OR3]L
b: tt or b tt |_ tt or b1 L tt...exploiting:
b: ff or b b
...exploiting:
45
Semantics Q1 2007
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 Q1 2007
– For the language, BExp, we have that:
– Whereas:
Eager vs. Lazy Semantics (cont’d)
b,: <b,> B* <t,’> ==> <b,> L
* <t,”>
b,: <b,> B* <t,’> <== <b,> L
* <t,”>
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 Q1 2007
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’
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 Q1 2007
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 Q1 2007
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
50
Semantics Q1 2007
COMMANDS
51
Semantics Q1 2007
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 C
Store = Var N
<c,> C <c’,’>
Here we need a Store (since we have to model side-effects)
((c,),(c’,’)) C
Problem: how do we end computation?!?
52
Semantics Q1 2007
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
TC := Store C
Store = Var N
<c,> C <c’,’> ((c,),(c’,’)) C
<c,> C ’ ((c,),’) C(!)forand
Here we need a Store (since we have to model side-effects)
53
Semantics Q1 2007
Nil and Assignment (SOS)• SOS for nil:
•
• SOS for assignment:•
v := e
< v := e , > C ’
< e , > A* < m , >
[ASS]C
’ = [m/v]
< nil , > C [NIL]C
54
Semantics Q1 2007
Sequential Composition (SOS)• SOS for Sequential Composition:
•
•
• Diagramatically:• “(Control) flow-charts”
c0 ; c1
< c0 ; c1 , > C < c0’ ; c1 , ’ >
< c0 , > C < c0’ , ’ >[SEQ1]C
< c0 ; c1 , > C < c1 , ’ >
< c0 , > C ’[SEQ2]C
c c’
55
Semantics Q1 2007
If-then-else (SOS)• SOS for “if-then-else”:
•
•
• Diagramatically:• “(Control) flow-charts”
if b then c else c’
< if b then c else c’ , > C < c , >
< b , > B* < tt , >[IF1]C
< if b then c else c’ , > C < c’ , >
< b , > B* < ff , >[IF2]C
c
c’
b
tt
ff
56
Semantics Q1 2007
While-do (SOS)• SOS for “while-do”:
•
•
• Diagramatically:• “(Control) flow-charts”
while b do c
< while b do c, > C < c ; while b do c , >
< b , > B* < tt , >[WH1]C
< while b do c , > C
< b , > B* < ff , >[WH2]C
tt
ff
cb
57
Semantics Q1 2007
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 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 Q1 2007
"Three minutes paper"• Please spend three minutes writing down the
most important things that you have learned today (now).
After 1 dayAfter 1 week
After 3 weeksAfter 2 weeks
Right away
59
Semantics Q1 2007
Remember to sign up or check that you have been signed up for the exam !!!
Sept. 1 - 15
60
Semantics Q1 2007
Next week: big-step vs. small-step,errors, type checking
Any Questions?