C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [...
-
date post
19-Dec-2015 -
Category
Documents
-
view
214 -
download
1
Transcript of C LAUS B RABRAND S EMANTICS (Q1,’05) S EP 8, 2005 C LAUS B RABRAND © 2005, University of Aarhus [...
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 8, 2005
CLAUS BRABRAND
© 2005, University of Aarhus
[ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ]
SEMANTICS (Q1,’05)
WEEK 2: ”STRUCTURAL OPERATIONAL SEMANTICS”
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 2 ]
SEP 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
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 8, 2005
REPETITION
Keywords:
inference systems (relations), transition systems, virtual machine semantics
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 4 ]
SEP 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… !
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 5 ]
SEP 8, 2005
Program Relationship (Example c)
Program worldModel world
ConcreteAbstract
~
P
P’
M
M’
1. P ~ P’ ?2. abstract
3. M ~ M’ ?
4. relate
5. M ~ M’ !6. concretize7. P ~ P’ !
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 6 ]
SEP 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: nounEtymology: New Latin, short for endosmosis1 : movement of a solvent through a semipermeable membrane (as of a living cell) […]2 : a process of absorption […] usually effortless often unconscious assimilation <learned a number of languages by osmosis >
Active Learning vs. Passive Learning (aka. Learning-by-Osmosis)
“I am sorry for saying this, but…”
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 7 ]
SEP 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
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 8, 2005
ARITHMETIC EXPRESSIONS
Keywords:
structural operational semantics (sos) big-step & small-step semantics, side-effects
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 9 ]
SEP 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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 10 ]
SEP 8, 2005
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”
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 11 ]
SEP 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)
(6 + 4)
explanation
10
((1 + (2 + 3)) + 4)
explanation
((1 + 5) + 4)
explanation
(6 + 4)
explanation
10
(aka. derivation sequence)(aka. reduction sequence)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 12 ]
SEP 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
e0 + e1
e0 n0
e1 n1
n0 n1 m
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 13 ]
SEP 8, 2005
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 “c v” automatically!
A: Actually, for Exp termination is decidable…
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 14 ]
SEP 8, 2005
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 ‘+’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 15 ]
SEP 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
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.
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 16 ]
SEP 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
e0 + e1
e0
e0’
e0 e1
e1’
e0 e1
m
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 17 ]
SEP 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
TL := N Exp
L Exp Exp
e e’(e,e’) ‘L’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 18 ]
SEP 8, 2005
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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 19 ]
SEP 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
[SUM1]
[SUM2]
[SUM3]
“order of discovery”
find predicates make conclusions
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 20 ]
SEP 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)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 21 ]
SEP 8, 2005
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’ := 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’,>
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 22 ]
SEP 8, 2005
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)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 23 ]
SEP 8, 2005
Note: 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…]
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 24 ]
SEP 8, 2005
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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 25 ]
SEP 8, 2005
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 <=
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 26 ]
SEP 8, 2005
Exp: Behavior and Equivalence
Definitions: Behavior:
Note: eval is only a partial functionif we have division (div-by-zero)
Equivalence:
Examples:
eval(e,) = m <=> <e,> E* <m,>
e e’ <=> : eval(e,) = eval(e’,)
1+1 2 x+y y+x
Can be proved by structural induction [soon…]
z+z 2*z
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 8, 2005
BOOLEAN EXPRESSIONS
Keywords:
eager evaluation, lazy evaluation,
parallel evaluation
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 28 ]
SEP 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
TB := 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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 29 ]
SEP 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”!
|_ 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. 31] for alternative semantics
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 30 ]
SEP 8, 2005
Disjunction: “or”
SOS for Disjunction (“or”):
|_ 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
Note: but what about ?!?tt or <bexp>
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 31 ]
SEP 8, 2005
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 ttExploit:
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 32 ]
SEP 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()
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 33 ]
SEP 8, 2005
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 [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)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 34 ]
SEP 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’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 35 ]
SEP 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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 36 ]
SEP 8, 2005
BExp: Behavior and Equivalence
Definitions: Behavior:
Note: eval is only a partial functionif we have division (div-by-zero)
Equivalence:
Examples:
eval(b,) = t <=> <b,> B* t
b b’ <=> : eval(b,) = eval(b’,)
b ~~b 1 = 2 ff
Can be proved by structural induction [soon…]
x = x tt
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 8, 2005
COMMANDS
Keywords:
Assignments, Conditionals, Loops,
Control flow
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 38 ]
SEP 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)
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?!?
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 39 ]
SEP 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
TC := Store
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)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 40 ]
SEP 8, 2005
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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 41 ]
SEP 8, 2005
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’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 42 ]
SEP 8, 2005
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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 43 ]
SEP 8, 2005
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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 44 ]
SEP 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 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…]
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 8, 2005
STRUCTURAL INDUCTION
Keywords:
Induction, base case, induction hypothesis, induction step structural induction
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 46 ]
SEP 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 }
nN : 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)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 47 ]
SEP 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) [ 20 + 21 + … + 2n = 2n+1 – 1 ]
P(0) [ 20 = 20+1 – 1 ]
[ 20 + 21 + … + 2n = 2n+1 – 1 ]
[ 20 + 21 + … + 2n+1 = 2(n+1)+1 – 1 ]
20 + 21 + … + 2n + 2n+1 (20 + 21 + … + 2n) + 2n+1=
(2n+1 – 1) + 2n+1 == 2*2n+1 – 1 = 2(n+1)+1 – 1 I.H.
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 48 ]
SEP 8, 2005
Structural Induction (for Exp)
Given: Arithmetic Expressions (e Exp)
e ::= n | v | e0+e1
e Exp : P(e)
P(n)
composite (inductive) casebase cases
Principle of structural induction:
P(e0) P(e1) P(e0+e1) P(v) and
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 49 ]
SEP 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
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 50 ]
SEP 8, 2005
More on this next week…
More on Structural Induction next week…
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 8, 2005
Next week: type checking, defn’s and decl’s
Any Questions?