View
223
Download
1
Embed Size (px)
Citation preview
Dynamic Semantics
COS 441
Princeton University
Fall 2004
Outline for Today
• Evaluation/Big-Step/Natural Semantics– Semantics for -calculus– SML interpreter for -calculus
• Transition/Small-Step/SOS Semantics– Semantics for -calculus– SML interpreter for -calculus
• Harper covers these two topics in the other order
• Relationship between the two approaches
Evaluation Semantics
• Evaluation semantics requires the definition of two sets– evaluatable expressions (E)– values (V)
• Useful but not required that V ½ E
• Defines a binary relation ½ E £ V
Static Semantics for -calculus
Names x 2 …Expressions e ::= lam(x.e) | apply(e1,e2)| x
` X okX 2
ok-V
` apply(E1,E2)ok
` E1 ok ` E2 okok-A
` lam(X.E)ok
[ {X} ` E okok-L
Evaluation Semantics for -calculus
Lambda Expr’s E = { E | {} ` E ok }
Lambda Values V = { lam(X.E) | lam(X.E) 2 E }
apply(E1,E2) V
E1 lam(X’.E’) E2 V’ [X’ Ã V’] E’ V eval-A
lam(X.E)lam(X.E)
lam(X.E) 2 V eval-L
Evaluation Semantics for -calculus
Substitution of a closed term V’ into E’ is closed
Lambda Expr’s E = { E | {} ` E ok }
Lambda Values V = { lam(X.E) | lam(X.E) 2 E }
apply(E1,E2) V
E1 lam(X’.E’) E2 V’ [X’ Ã V’] E’ V eval-A
lam(X.E)lam(X.E)
lam(X.E) 2 V eval-L
Example: Evaluations
apply(lam(X.X),lam(Y.Y)) ??
apply(lam(X.Z),lam(Y.Y)) ??
lam(X.X) ??
apply(X,lam(Y.Y)) ??
Example: Evaluations
apply(lam(X.X),lam(Y.Y)) ??
apply(lam(X.Z),lam(Y.Y)) ??
lam(X.X) lam(X.X)
apply(X,lam(Y.Y)) ??
Example: Evaluations
apply(lam(X.X),lam(Y.Y)) lam(Y.Y)
apply(lam(X.Z),lam(Y.Y)) ??
lam(X.X) lam(X.X)
apply(X,lam(Y.Y)) ??
Example: Evaluations
apply(lam(X.X),lam(Y.Y)) lam(Y.Y)
apply(lam(X.Z),lam(Y.Y)) ??
lam(X.X) lam(X.X)
apply(X,lam(Y.Y)) ??
Example: Evaluations
apply(lam(X.X),lam(Y.Y)) lam(Y.Y)
apply(lam(X.Z),lam(Y.Y)) ??
lam(X.X) lam(X.X)
apply(X,lam(Y.Y)) ??
Example: Evaluations
apply(lam(X.apply(X,X)), lam(X.apply(X,X)) ??
Example: Evaluations
apply(lam(X.apply(X,X)), lam(X.apply(X,X)) ??
[XÃlam(X.apply(X,X))] lam(X.apply(X,X)) apply(lam(X.apply(X,X)),
lam(X.apply(X,X))
Example: Evaluations
There is no derivation because the expression “infinite loops”
apply(lam(X.apply(X,X)), lam(X.apply(X,X)) ??
[XÃlam(X.apply(X,X))] lam(X.apply(X,X)) apply(lam(X.apply(X,X)),
lam(X.apply(X,X))
Evaluation “Order”
• Which subterm do we evaluate first?
• Rules don’t specify, you can build your proof tree up in any order!
• Order of evaluation doesn’t really matter in this case
apply(E1,E2)
Evaluation Semantics in SML
apply(E1,E2) V
E1 lam(X’.E’) E2 V’ [X’ Ã V’] E’ V eval-A
lam(X.E)lam(X.E)
lam(X.E) 2 V eval-L
Evaluation Semantics in SML eval(lam(X.E)) lam(X.E)
if lam(X.E) 2 V
apply(E1,E2) V
E1 lam(X’.E’) E2 V’ [X’ Ã V’] E’ V eval-A
Evaluation Semantics in SML eval(lam(X.E)) lam(X.E)
if lam(X.E) 2 V
eval(apply(E1,E2)) eval([X’ÃV’]E’))
if lam(X’.E’)eval(E1))
and V’ eval(E2)
Evaluation Semantics in SMLtype name = Names.namedatatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name)
val subst : (name * exp * exp) -> exp val ok : (Names.set * exp) -> bool
Evaluation Semantics in SML
fun eval(Lam(x,e)) = if ok(Set.empty,Lam(x,e)) then Lam(x,e) else raise Wrong | eval(Apply(e1,e2)) = let val Lam(x’,e’) = eval(e1) val v’ = eval(e2) in eval(subst(x,v’,e’)) end
Evaluation Semantics in SML
fun eval(Lam(x,e)) = if ok(Set.empty,Lam(x,e)) then Lam(x,e) else raise Wrong | eval(Apply(e1,e2)) = (case(eval(e1),eval(e2)) of (Lam(x’,e’),v’) =>
eval(subst(x,v’,e’)) | _ => raise Impossible) | eval _ = raise Wrong
Evaluation Semantics in SML
fun eval(e as Lam _) = if ok(Set.empty,e)then e else raise Wrong | eval(Apply(e1,e2)) = (case(eval(e1),eval(e2)) of (Lam(x’,e’),v’) =>
eval(subst(x’,v’,e’)) | _ => raise Impossible) | eval _ = raise Wrong
Another Approach
Introduce an type that represents valuestype name = Names.name
datatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name) type value = (name * exp)
Another Approach
fun eval(Lam(x,e)) = if ok(Set.empty,Lam(x,e)) then (x,e) else raise Wrong | eval(Apply(e1,e2)) = let val (x’,e’) = eval(e1) val v’ = eval(e2) in eval(subst(x,Lam v’,e’)) end | eval _ = raise Wrong
Evaluation Semantics
• Non-termination expressions do not evaluate to a value
• Evaluation “order” unspecified
• Concise and “intuitive”
• Easy to translate into efficient interpreter– The main reason we study it!
Transition Semantics
• Transition semantics requires the definition of universe of states S– initial states (I)– final states (F)
• Required that I ½ S and F ½ S
• A binary step relation ½ S £ S
• Iterated multi-step versions *,!, and n
Multistep Relations
S * SZ*
S * S’’
S S’ S’ * S’’S*
S ! S
S 2 FZ!
S ! S’’
S S’ S’ ! S’’S!
S 0 SZn
S n+1 S’’
S S’ S’ n S’’Sn
Transition Semantics for -calculus
((x.e1) e2) ((x.e1) e’2)
e2 e’2A2
((x.e1) (y.e2)) [xÃ(y.e2)] e1 A1
(e1 e2) (e’1 e2)
e1 e’1 A3
I = { E | {} ` E ok }
S = { E | 9. ` E ok }
F = { x.e | {} ` x.e ok }
Example:
((x.x) (y.y)) ??
x.x) ??
((x.(x x)) (x.(x x))) ??
(((x.x) (x.x)) ((x.x) (x.x))) ??
Example:
((x.x) (y.y)) ??
x.x) ??
((x.(x x)) (x.(x x))) ??
(((x.x) (x.x)) ((x.x) (x.x))) ??
Example:
((x.x) (y.y)) (x.x)
x.x) ??
((x.(x x)) (x.(x x))) ??
(((x.x) (x.x)) ((x.x) (x.x))) ??
Example:
((x.x) (y.y)) (x.x)
x.x) ??
((x.(x x)) (x.(x x))) ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) ??
Example:
((x.x) (y.y)) (x.x)
x.x) ??
((x.(x x)) (x.(x x))) ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) ((x.x) ((x.x) (x.x)))
Example:
((x.x) (y.y)) (x.x)
x.x) ??
((x.(x x)) (x.(x x))) ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) ((x.x) ((x.x) (x.x)))
((x.x) ((x.x) (x.x))) ??
Example:
((x.x) (y.y)) (x.x)
x.x) ??
((x.(x x)) (x.(x x))) ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) ((x.x) ((x.x) (x.x)))
((x.x) ((x.x) (x.x))) ((x.x) (x.x))
Example:
((x.x) (y.y)) (x.x)
x.x) ??
((x.(x x)) (x.(x x))) ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) ((x.x) ((x.x) (x.x)))
((x.x) ((x.x) (x.x))) ((x.x) (x.x))
((x.x) (x.x)) ??
Example:
((x.x) (y.y)) (x.x)
x.x) ??
((x.(x x)) (x.(x x))) ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) ((x.x) ((x.x) (x.x)))
((x.x) ((x.x) (x.x))) ((x.x) (x.x))
((x.x) (x.x)) (x.x)
Example: *
((x.x) (y.y)) * ??
x.x) * ??
((x.(x x)) (x.(x x))) * ??
(((x.x) (x.x)) ((x.x) (x.x))) * ??
Example: *
((x.x) (y.y)) * ((x.x) (y.y))
x.x) * x.x)
((x.(x x)) (x.(x x))) * ??
(((x.x) (x.x)) ((x.x) (x.x))) * ??
Example: *
((x.x) (y.y)) * (y.y)
x.x) * x.x)
((x.(x x)) (x.(x x))) * ??
(((x.x) (x.x)) ((x.x) (x.x))) * ??
Example: *
((x.x) (y.y)) * (y.y)
x.x) * x.x)
((x.(x x)) (x.(x x))) * ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) * ??
Example: *
((x.x) (y.y)) * (y.y)
x.x) * x.x)
((x.(x x)) (x.(x x))) * ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) * (((x.x) (x.x)) ((x.x) (x.x)))
Example: *
((x.x) (y.y)) * (y.y)
x.x) * x.x)
((x.(x x)) (x.(x x))) * ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) * ((x.x) ((x.x) (x.x)))
Example: *
((x.x) (y.y)) * (y.y)
x.x) * x.x)
((x.(x x)) (x.(x x))) * ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) * ((x.x) (x.x))
Example: *
((x.x) (y.y)) * (y.y)
x.x) * x.x)
((x.(x x)) (x.(x x))) * ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) * (x.x)
Example: !
((x.x) (y.y)) ! ??
x.x) ! ??
((x.(x x)) (x.(x x))) ! ??
(((x.x) (x.x)) ((x.x) (x.x))) ! ??
Example: !
((x.x) (y.y)) ! ??
x.x) ! x.x)
((x.(x x)) (x.(x x))) ! ??
(((x.x) (x.x)) ((x.x) (x.x))) ! ??
Example: !
((x.x) (y.y)) ! (y.y)
x.x) ! x.x)
((x.(x x)) (x.(x x))) ! ??
(((x.x) (x.x)) ((x.x) (x.x))) ! ??
Example: !
((x.x) (y.y)) ! (y.y)
x.x) ! x.x)
((x.(x x)) (x.(x x))) ! ??
(((x.x) (x.x)) ((x.x) (x.x))) ! ??
Example: !
((x.x) (y.y)) ! (y.y)
x.x) ! x.x)
((x.(x x)) (x.(x x))) ! ??
(((x.x) (x.x)) ((x.x) (x.x))) ! (x.x)
Example: 0
((x.x) (y.y)) 0 ??
x.x) 0 ??
((x.(x x)) (x.(x x))) 0 ??
(((x.x) (x.x)) ((x.x) (x.x))) 0 ??
Example: 0
((x.x) (y.y)) 0 ((x.x) (y.y))
x.x) 0 x.x)
((x.(x x)) (x.(x x))) 0 ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) 0 (((x.x) (x.x)) ((x.x) (x.x)))
Example: 1
((x.x) (y.y)) 1 (y.y)
x.x) 1 ??
((x.(x x)) (x.(x x))) 1 ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) 1 ((x.x) ((x.x) (x.x)))
Example: 2
((x.x) (y.y)) 2 ??
x.x) 2 ??
((x.(x x)) (x.(x x))) 2 ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) 2 ((x.x) (x.x))
Example: 3
((x.x) (y.y)) 3 ??
x.x) 3 ??
((x.(x x)) (x.(x x))) 3 ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) 3 (x.x)
Example: 4
((x.x) (y.y)) 4 ??
x.x) 4 ??
((x.(x x)) (x.(x x))) 4 ((x.(x x)) (x.(x x)))
(((x.x) (x.x)) ((x.x) (x.x))) 4 (x.x)
!
((x.x) (y.y)) ! (y.y)
x.x) ! x.x)
((x.(x x)) (x.(x x))) ! ??
(((x.x) (x.x)) ((x.x) (x.x))) ! (x.x)
((x.x) (y.y)) (y.y)
x.x) x.x)
((x.(x x)) (x.(x x))) ??
(((x.x) (x.x)) ((x.x) (x.x))) (x.x)
Relation Between ! and
Theorem: e ! (x.e’) iff e (x.e’)Proof: By lemma 1 and lemma 2
Lemma 1: If e (x.e’) then e ! (x.e’)
Lemma 2: If e ! (x.e’) then e (x.e’)Proofs: See Harper 7.3 for sketch
Transition Semantics
• Has direct correspondence with evaluation semantics
• Let us reason about– Evaluation order– Computational complexity of program– Partial computations
• Easier to relate with static semantics to prove type safety– Next lecture!