CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 22, 2005 CLAUS BRABRAND © 2005, University of Aarhus [ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ] SEMANTICS (Q1,’05) WEEK 4: ”DEFINITIONS: STATIC AND DYNAMIC SEMANTICS”
Dec 22, 2015
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 22, 2005
CLAUS BRABRAND
© 2005, University of Aarhus
[ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ]
SEMANTICS (Q1,’05)
WEEK 4: ”DEFINITIONS: STATIC AND DYNAMIC SEMANTICS”
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 2 ]
SEP 22, 2005
Week 4 - Outline
Issues from week 3; mini-project; competition
Return of the Structural Induction Detailed Example: Determinism of SOS for L-exp’s
Infinite vs. Finite Stores Simple Definitions (functional languages) Compound Definitions (fun) Type Checking Definitions (fun) Declarations (imperative langs) [week 7]
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 3 ]
SEP 22, 2005
“3x3 main issues” from week 3
Class X: 1x. course speed: easy; too slow (3rd year, has had dOvs) ! 2x. step size: what step size should be used ? 3x. examples: some of the examples are too unrealistic/exotic !?
Class Y: 1y. course speed: hard; too fast (2nd year, hasn’t had dOvs) ! 2y. flip exercise: what was the point (if there was one) ?! 3y. which language?: 1) what’s the syntax; 2) what’s the sem. !!!
Class Z: 1z. course speed: appropriate ! 2z. implementation: how much ML are we expected to know ? 3z. implementation: can you show ML examples at the lecture ?
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 4 ]
SEP 22, 2005
Minor Project
Minor project (cf.: compulsory programme): When?: 10 days: (Oct 3 -- Oct 13, 2005). What?: covering all topics (so far) Why?: chance to learn, measure, and get feedback Who?: everybody (individually) Amount?: roughly corresponds to an exercise class Form?: written, take-home Grading?: [0-4]-scale ~ [Aims & Goals]; (2+ to pass) Correcting?: your teaching assistants Consequences?: project approved attend exam Help?: TAs will answer un-applied understanding Q’s
”Good news”: less exercises for classes those 2 weeks
CLAUS BRABRAND SEMANTICS (Q1,’05) SEP 22, 2005
STRUCTURAL INDUCTION
A Detailed Example:
Determinism of SOS for L-Expressions
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 6 ]
SEP 22, 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)[ 7 ]
SEP 22, 2005
Intuition: Induction vs. Str’ Induction
Induction: Holds for ?
Structural Induction: Holds for ?
P(0) P(0) => P(1) P(1) => P(2) P(2) => P(3)
P(3)
P(7+(x+y))
P(7)
P(x) P(y)
P(x+y)
P(7+(x+y))
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 8 ]
SEP 22, 2005
Structural Induction Examples
Given: Arithmetic Expressions (e Exp)
Determinism of SOS* for L-expressions : “One-step evaluation of expressions is deterministic”
e ::= n | v | e0+e1
P(e) ,’: [ ( |_ e |_ e ’ ) => = ’ ]
e: P(e)
*/ using the (side-effectless) small-step operational semantics
Let be given: Prove: (i.e., “prove property, P, holds for all expressions”)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 9 ]
SEP 22, 2005
Proof Structure (according to SI)
Base cases: Case [n]; show :
Case [v]; show :
Inductive case: Case [e0+e1]; show :
assume induction hypothesis [lhs]: P(e0) P(e1)
show inductive step [rhs]: P(e0+e1)
P(n) ,’: ( |_ n |_ n ’ ) => = ’
P(v) ,’: ( |_ v |_ v ’ ) => = ’
P(e0) ,’: ( |_ e0 |_ e0 ’ ) => = ’
P(e1) ,’: ( |_ e1 |_ e1 ’ ) => = ’
P(e0+e1) ,’: ( |_ e0+e1 |_ e0+e1 ’ ) => = ’
P(n)
P(v)
P(e0) P(e1) => P(e0+e1)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 10 ]
SEP 22, 2005
Proof: Base Case [n] Case [n]; show :
Show implication “=>” (assume [lhs], show [rhs]): However, since [lhs] is trivially false (no rules for constants), the whole
thing is trivially true (aka. “vacuously true”) Recall that: [explanation]
P(n) ,’: ( |_ n |_ n ’ ) => = ’
P(n)
b : (false => b) true
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 11 ]
SEP 22, 2005
Proof: Base Case [v] Case [v]; show :
Show implication “=>” (assume [lhs], show [rhs]): assume [lhs]: and for some ,’ show [rhs]:
could only be because ; thus, = (v) …and similarly:
could only be because ; thus, ’ = (v)
Thus, we have that: = (v) = ’ (as required)
P(v) ,’: ( |_ v |_ v ’ ) => = ’
P(v)
|_ v
= ’
|_ v ’
|_ v |_ v (v)
|_ v ’ |_ v (v)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 12 ]
SEP 22, 2005
Proof: Inductive Case [e0+e1]
Case [e0+e1]; show : assume induction hypothesis: P(e0) P(e1)
show inductive step: P(e0+e1)
Show implication “=>” (assume [lhs], show [rhs]): Assume [lhs]: and for some ,’ Show [rhs]:
When could we have that: (?) Case Analysis (on e0+e1)
Three possibilities ( [SUM1], [SUM2], and [SUM3] )…
P(e0) ,’: ( |_ e0 |_ e0 ’ ) => = ’
P(e1) ,’: ( |_ e1 |_ e1 ’ ) => = ’
P(e0+e1) ,’: ( |_ e0+e1 |_ e0+e1 ’ ) => = ’
P(e0) P(e1) => P(e0+e1)
|_ e0+e1 |_ e0+e1 ’
= ’
|_ e0+e1
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 13 ]
SEP 22, 2005
Proof: Case Analysis (on e0+e1)
Case Analysis (on e0+e1):
Case [SUM1 (e0 Z)]: could only be because could only be because
But from our induction hypothesis, P(e0); i.e. … :
… we get that: and implies that: e0’ = e0”
Thus we have that: = e0’+e1 = e0”+e1 = ’ (as required)
|_ e0+e1
|_ e0 e0’
|_ e0+e1 e0’+e1 =
|_ e0 e0”
|_ e0+e1 e0”+e1 = ’
|_ e0+e1 ’
|_ e0 e0’ |_ e0 e0”
P(e0) ,’: ( |_ e0 |_ e0 ’ ) => = ’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 14 ]
SEP 22, 2005
Proof: Case Analysis (on e0+e1)
Case [SUM2 (e0 Z , e1 Z)]: (analoguous)
Case [SUM3 (e0 Z , e1 Z)]: could only be because could only be because
Thus, we have that: = m = n0+n1 = m’ = ’ (as required)
|_ n0+n1
|_ n0+n1 ’
|_ e0+e1 m =
|_ e0+e1 m’ = ’
m = n0+n1
m’ = n0+n1
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 15 ]
SEP 22, 2005
What did we do: Proof Structure
Base cases: Case [n]; showed :
Case [v]; showed :
Inductive case: Case [e0+e1]; showed :
assumed induction hypothesis [lhs]: P(e0) P(e1)
showed inductive step [rhs] via case analysis: P(e0+e1)
P(n) ,’: ( |_ n |_ n ’ ) => = ’
P(v) ,’: ( |_ v |_ v ’ ) => = ’
P(e0) ,’: ( |_ e0 |_ e0 ’ ) => = ’
P(e1) ,’: ( |_ e1 |_ e1 ’ ) => = ’
P(e0+e1) ,’: ( |_ e0+e1 |_ e0+e1 ’ ) => = ’
P(n)
P(v)
P(e0) P(e1) => P(e0+e1)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 17 ]
SEP 22, 2005
Problem: Infinite Stores
Problem: (Var infinite)
Solution: (for V FIN Var)
Now define finite configurations:
Store = Var Z
v StoreV = V Z
V := { <e, > | Var(e) V StoreV }
appropriate storeall vars in “V”
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 18 ]
SEP 22, 2005
Finite Typeable Configurations
Configurations (Exp):
Transitions (using “V,E”):
Configurations (Com):
Transitions (using “V,C”):
V.C := { <c, > | |_wfc c VarC(c) V StoreV }
V,E := { <e, > | : |_ e: VarE(e) V StoreV }
< e0 + e1 , > V,E < e0’ + e1 , ’ >
< e0 , > V,E < e0’ , ’ >[SUM1]
< c0 ; c1 , > V,C < c0’ ; c1 , ’ >
< c0 , > V,C < c0’ , ’ >[SEQ1]
{ | StoreV }
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 19 ]
SEP 22, 2005
Variable Extraction Functions
VarE: Exp P(Var) VarE(n) = Ø
VarE(t) = Ø
VarE(v) = { v }
VarE(~ e) = VarE(e)
VarE(e e’) = VarE(e) VarE(e’)
VarC: Com P(Var) VarC(nil) = Ø
VarC(x := e) = { x } VarE(e)
VarC(c ; c’) = VarC(c) VarC(c’)
VarC(if e then c else c’) = VarE(e) VarC(c) VarC(c’)
VarC(while e do c) = VarE(e) VarC(c)
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 21 ]
SEP 22, 2005
Introducing Binding Constructs
Consider the language ”Let”:
Expressions (e Exp):
Example Let program:
Note: the actual identifier names are irrellevant (only the use-def links are significant)
e ::= n | x | e e’ | let x = e in e’
{ +, -, * }…where:
x + let x = 5 in x + let x = 1 + x in x * x
Definition (def) Usage (use): Free use Bound use
use-def linkTerminology
definition scope
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 22 ]
SEP 22, 2005
Free Variables
Determine the free variables?
FV: Exp P(Var) FV(n) = Ø FV(x) = { x } FV(e e’) = FV(e) FV(e’) FV(let x=e in e’) = FV(e) ( FV(e’) \ { x } )
Note: error in [Plotkin, p. 56]; all intersections in figure should be unions.
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 23 ]
SEP 22, 2005
Dynamic Semantics (Trans. Sys.)
Define (family of) environments: recall terminology (no side-effects)
Configurations: i.e., numbers
Transition relation: ; writing for
V = { <e, > | EnvV FV(e) V }
TV = { <z, > | EnvV z Z }
v EnvV = V Z
Note: this is done slightly differently in [Plotkin, p. 57-…]:through “relative transition systems” that are (implicitly) relative to an environment.
V V V |- e V e (<e,>,<e,>) ‘V’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 24 ]
SEP 22, 2005
Dynamic Semantics [n] | [x] | [ee]
Structural Operational Semantics:
[BOP1]
[BOP2]
[BOP3]
[VAR]
|- e0 e1 V e0’ + e1
|- e0 V e0’
|- z0 z1 V zz = z0 z1
|- z0 e1 V z0 + e1’
|- e1 V e1’
|- x V zz = (x)
EnvV = V Z
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 25 ]
SEP 22, 2005
Dynamic Semantics [let-in]
Structural Operational Semantics:
Note: the premisis works in the extended environment
Live: derive in environment:
[LET2]
[LET3]
[LET1]
EnvV = V Z
|- let x = e0 in e1 V let x = e0’ in e1
|- e0 V e0’
|- let x = z in n V n
|- let x = z in e1 V let x = z in e1’
[xz] |- e1 V{x} e1’
let x=y in x [y7]
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 27 ]
SEP 22, 2005
Compound Definitions
Consider language Let++: Expressions (e Exp):
Definitions (d Def):
e ::= n | x | e e’ | let d in e
d ::= nil | x = e | d ; d’ | d and d’ | d in d’
x + 3 y = x+7Graphically:
value env{x} env{y}
[x1]
env{x}
[x1] [y8]4
{ +, -, * }…where:
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 28 ]
SEP 22, 2005
Graphically: [x = e] | [d ; d’]
Definition block:
E.g.:
Sequential definition:
E.g.:
let d in e
d ; d’
let x=a ; y=bin b * c + y
<< exercise >>
a
b
c
x
ybc
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 29 ]
SEP 22, 2005
Graphically: [d and d’] | [d in d’]
Simultaneous definition:
E.g.:
Private definition:
E.g.:
d and d’
d in d’
<< exercise >>
<< exercise >>
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 30 ]
SEP 22, 2005
Static Semantics: Let++
Problem: I.e. non-disjoint simultaneous definitions!
Solution: static semantics exp / def “well-formedness relations”: ‘|-wfe’ / ‘|-wfd’:
let x = 1 and x = 2 in x
|_wfe e |_
wfe e’
|_wfe e e’
Exp well-formedness: Definition well-formedness:
|_wfe n
|_wfe x
|_wfd d |_
wfe e
|_wfe let d in e
|_wfd nil
|_wfe e
|_wfd x = e
|_wfd d |_
wfd d’
|_wfd d ; d’
|_wfd d |_
wfd d’
|_wfd d and d’
|_wfd d |_
wfd d’
|_wfd d in d’
DV(d) DV(d’) = Ø
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 31 ]
SEP 22, 2005
Recall: Free Variables for Let
Let: FV(n) = Ø FV(x) = { x } FV(e e’) = FV(e) FV(e’)
FV(let x=e in e’) = FV(e) ( FV(e’) \ { x } )
Let++: FV(let d in e’) = FV(d) ( FV(e’) \ DV(d) )
So we need FV(d) and DV(d) …
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 32 ]
SEP 22, 2005
Defining Variables and Free Variables
DV: Def P(Var) defining variables DV(nil) = Ø DV(x = e) = { x } DV(d ; d’) = DV(d) DV(d’) DV(d and d’) = DV(d) DV(d’) DV(d in d’) = DV(d’)
FV: Def P(Var) free variables FV(nil) = Ø FV(x = e) = FV(e) FV(d ; d’) = FV(d) ( FV(d’) \ DV(d) ) FV(d and d’) = FV(d) FV(d’) FV(d in d’) = FV(d) ( FV(d’) \ DV(d) )
+ should be disjoint union
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 33 ]
SEP 22, 2005
Dynamic Semantics (Trans. Sys.)
Expression Configurations:
Problem: what should definitions evaluate to? Environments (like expressions evaluate to numbers)
Solution: add “environments results” as intermediate syntax:
Definition Configurations:
V,E = { <e, > | EnvV FV(e) V |-wfe e }
TV,E = { <z, > | EnvV z Z } only well-formed exps
d ::= nil | x = e | d ; d’ | d and d’ | d in d’ | 0
V,D = { <d, > | EnvV FV(d) V |-wfd d }
TV,D = { <0, ’> | EnvV } only well-formed defs
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 34 ]
SEP 22, 2005
Dynamic Semantics: Exp
Structural Operational Semantics:
Note: the premisis works in the 0-extended environment
[LET2]
[LET3]
[LET1]
EnvV = V Z
|- let d in e V let d’ in e
|- d V d’
|- let 0 in n V n
|- let 0 in e V let 0 in e’
[0] |- e VDV(0) e’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 35 ]
SEP 22, 2005
Dynamic Sem.: Def [nil] | [x=e]
Structural Operational Semantics:
EnvV = V Z
[NIL] |- nil V [] the empty environment, []
[DEF1] |- x = e V x = e’
|- e V e’
[DEF2] |- x = n V [xn] the environment, [xn]
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 36 ]
SEP 22, 2005
Dynamic Sem.: Def [d;d’]
Structural Operational Semantics:
Note: the premisis works in the 0-extended environment
Note: the result is the combined environment, 0[1]
[SEQ2]
[SEQ3]
[SEQ1]
EnvV = V Z
|- d0 ; d1 V d0’ ; d1
|- d0 V d0’
|- 0 ; 1 V 0[1]
|- 0 ; d1 V 0 ; d1’
[0] |- d1 VDV(0) d1’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 37 ]
SEP 22, 2005
Dynamic Sem.: Def [d and d’]
Structural Operational Semantics:
Notice the independence of the two operands (d and d’)
Note: the result is the disj. combined environment, 01
[AND2]
[AND3]
[AND1]
EnvV = V Z
|- d0 and d1 V d0’ and d1
|- d0 V d0’
|- 0 and 1 V 01
|- 0 and d1 V 0 and d1’
|- d1 V d1’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 38 ]
SEP 22, 2005
Dynamic Sem.: Def [d in d’]
Structural Operational Semantics:
Note: the premisis works in the 0-extended environment
Note: the result is only the last environment, 1
[IN2]
[IN3]
[IN1]
EnvV = V Z
|- d0 in d1 V d0’ in d1
|- d0 V d0’
|- 0 in 1 V 1
|- 0 in d1 V 0 in d1’
[0] |- d1 VDV(0) d1’
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 40 ]
SEP 22, 2005
Adding Boolean Variables: LetB++
Consider language LetB++: Mixed Expressions (e Exp):
Definitions (d Def):
Note: The type of a var is now context dependent I.e. a context-free (grammar) approach will not suffice
Examples: ? ? …
e ::= n | t | x | ~ e | e e’ | e ? e’ : e” | let d in e
d ::= nil | x = e | d ; d’ | d and d’ | d in d’
{ +, -, * , =, or }…where:
x or tt x * x
type definition (annotation) { bool, int }…where:
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 41 ]
SEP 22, 2005
Static Semantics: LetB++
DV and FV as before …adding:
We now need type environments: Type = { bool, int }
Define static semantics (type checking rel’s): “e has type (given type environment )”
provided
“d yields type env. (given type env. )” provided
FV(e ? e’ : e”) = FV(e) FV(e’) FV(e”)
TEnvV = V Type
|-V e :
|-V d :
FV(e) V
FV(d) V
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 42 ]
SEP 22, 2005
Static Semantics (expressions)
Expressions:[NUM] |-V
n : int |-V t : bool[TVL]
[VAR] |-V x : (x)
[VAR] |-V ~ e : bool
|-V e : bool[LET]
|-V let d in e :
|-V d : [] |-VDV{} e :
[IFE] |-V e0 ? e1 : e2 :
|-V e0 : bool |-V e1 : 1 |-V e2 : 2 = 1 =2
[BOP] |-V e0 e1 : type(0,1)
|-V e0 : 0 |-V e1 : 1
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 43 ]
SEP 22, 2005
Static Semantics (definitions)
Definitions:[NIL] |-V
nil : [] [DEF] |-V x = e : [x]
|-V e : ’ = ’
[SEQ] |-V d0 ; d1 : 0[1]
|-V d0 : 0 [0] |-VDV{0} d1 : 1
[AND] |-V d0 and d1 : 01
|-V d0 : 0 |-V d1 : 1
[IN] |-V d0 in d1 : 1
|-V d0 : 0 [0] |-VDV{0} d1 : 1
DV(d0) DV(d1) = Ø
Note:combined environment
Note:only last environment
Note:disjointenvironment
Note: type check
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 44 ]
SEP 22, 2005
Dynamic Semantics: LetB++
Type Environment (Compile-time): Type = { bool, int }
Environment (Runtime): B = { tt, ff }
Define type correspondence relation ‘~’:
Note:
v TEnvV = V Type
v EnvV = V B Z
v ~ v x V: v(x) = bool v(x) B v(x) = int v(x) Z
( 0 ~ 0 1 ~ 1 ) 0[1] ~ 0[1]
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 45 ]
SEP 22, 2005
Exp. Transition System for LetB++
Let++ Expression Configurations:
LetB++ Expression Configurations: V Type:
Env := i.e., “only type corresponding runtime environments”
V = { <e, > | EnvV FV(e) V |-wfe e }
TV = { <z, > | EnvV z Z } only well-formed exps
= { <e, > | Env FV(e) V : |-V e : }
T = { <r, > | Env r B Z }
{ ( V B Z ) | ~ }
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 46 ]
SEP 22, 2005
Def. Transition System for LetB++
Let++ Definition Configurations:
LetB++ Definition Configurations: V Type:
Env := i.e., “only type corresponding runtime environments”
V = { <d, > | EnvV FV(d) V |-wfd d }
only well-formed defsTV = { <0, ’> | EnvV }
= { <d, > | Env FV(d) V : |-V d : }
T = { <0, > | Env }
{ ( V B Z ) | ~ }
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 47 ]
SEP 22, 2005
Exp. Dynamic Semantics: LetB++
SOS of Expressions for LetB++: E.g. [LET]:
- Basically substituting for V (from dyn. sem. of Let++)
[LET2]
[LET3]
[LET1]
Env
|- let d in e let d’ in e
|- d d’
|- let 0 in r r
|- let 0 in e let 0 in e’
[0] |- e [0] e’ 0 ~ 0
CLAUS BRABRAND © SEMANTICS (Q1,’05)[ 48 ]
SEP 22, 2005
Def. Dynamic Semantics: LetB++
SOS of Definitions for LetB++: E.g [SEQ]:
- Again, basically substituting for V (from dyn. sem. of Let++)
[SEQ2]
[SEQ3]
[SEQ1]
|- d0 ; d1 d0’ ; d1
|- d0 d0’
|- 0 ; 1 0[1]
|- 0 ; d1 0 ; d1’
[0] |- d1 [0] d1’
Env
0 ~ 0