Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Semantics COS 441 Princeton University Fall 2004.

Similar presentations


Presentation on theme: "Dynamic Semantics COS 441 Princeton University Fall 2004."— Presentation transcript:

1 Dynamic Semantics COS 441 Princeton University Fall 2004

2 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

3 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

4 Static Semantics for -calculus Names x 2 … Expressions e::= lam( x.e ) | apply( e 1, e 2 ) | x  ` X ok X 2 X 2  ok-V  ` apply( E 1, E 2 ) ok  ` E 1 ok  ` E 2 ok ok-A  ` lam( X.E ) ok  [ {X} ` E ok ok-L

5 Evaluation Semantics for -calculus Lambda Expr’s E={ E | {} ` E ok } Lambda Values V= { lam( X.E ) | lam( X.E ) 2 E } apply( E 1, E 2 )  V E 1  lam( X’.E’ ) E 2  V’ [X’ Ã V’] E’  V eval-A lam( X.E )  lam( X.E )  lam( X.E ) 2 V eval-L

6 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( E 1, E 2 )  V E 1  lam( X’.E’ ) E 2  V’ [X’ Ã V’] E’  V eval-A lam( X.E )  lam( X.E )  lam( X.E ) 2 V eval-L

7 Example: Evaluations apply(lam( X.X ),lam( Y.Y ))  ?? apply(lam( X.Z ),lam( Y.Y ))  ?? lam( X.X )  ?? apply( X,lam( Y.Y ))  ??

8 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 ))  ??

9 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 ))  ??

10 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 ))  ??

11 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 ))  ??

12 Example: Evaluations apply(lam( X. apply( X,X )), lam( X. apply( X,X ))  ??

13 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 ))

14 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 ))

15 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( E 1, E 2 )

16 Evaluation Semantics in SML apply( E 1, E 2 )  V E 1  lam( X’.E’ ) E 2  V’ [X’ Ã V’] E’  V eval-A lam( X.E )  lam( X.E )  lam( X.E ) 2 V eval-L

17 Evaluation Semantics in SML eval( lam( X.E ) )  lam( X.E ) if lam( X.E ) 2 V apply( E 1, E 2 )  V E 1  lam( X’.E’ ) E 2  V’ [X’ Ã V’] E’  V eval-A

18 Evaluation Semantics in SML eval( lam( X.E ) )  lam( X.E ) if lam( X.E ) 2 V eval( apply( E 1, E 2 ) )  eval([X’ Ã V’]E’)) if lam( X’.E’ )  eval(E 1 )) and V’  eval(E 2 )

19 Evaluation Semantics in SML type name = Names.name datatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name) val subst : (name * exp * exp) -> exp val ok : (Names.set * exp) -> bool

20 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

21 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

22 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

23 Another Approach Introduce an type that represents values type name = Names.name datatype exp = Lam of (name * exp) | Apply of (exp * exp) | Var of (name) type value = (name * exp)

24 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

25 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!

26 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

27 Multistep Relations S  * S Z* S  * S’’ S  S’ S’  * S’’ S* S  ! S S 2 F Z! S  ! S’’ S  S’ S’  ! S’’ S! S  0 S Zn S  n+1 S’’ S  S’ S’  n S’’ Sn

28 Transition Semantics for -calculus (( x.e 1 ) e 2 )  (( x.e 1 ) e’ 2 ) e 2  e’ 2 A2 (( x.e 1 ) ( y.e 2 ))  [x à ( y.e 2 )] e 1 A1 (e 1 e 2 )  (e’ 1 e 2 ) e 1  e’ 1 A3 I={ E | {} ` E ok } S= { E | 9 .  ` E ok } F= { x.e | {} ` x.e ok }

29 Example:  (( x.x) ( y.y))  ??  x.x)  ?? (( x.(x x)) ( x.(x x)))  ?? ((( x.x) ( x.x)) (( x.x) ( x.x)))  ??

30 Example:  (( x.x) ( y.y))  ??  x.x)  ?? (( x.(x x)) ( x.(x x)))  ?? ((( x.x) ( x.x)) (( x.x) ( x.x)))  ??

31 Example:  (( x.x) ( y.y))  ( x.x)  x.x)  ?? (( x.(x x)) ( x.(x x)))  ?? ((( x.x) ( x.x)) (( x.x) ( x.x)))  ??

32 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)))  ??

33 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)))

34 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)))  ??

35 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))

36 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))  ??

37 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)

38 Example:  * (( x.x) ( y.y))  * ??  x.x)  * ?? (( x.(x x)) ( x.(x x)))  * ?? ((( x.x) ( x.x)) (( x.x) ( x.x)))  * ??

39 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)))  * ??

40 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)))  * ??

41 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)))  * ??

42 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)))

43 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)))

44 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))

45 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)

46 Example:  ! (( x.x) ( y.y))  ! ??  x.x)  ! ?? (( x.(x x)) ( x.(x x)))  ! ?? ((( x.x) ( x.x)) (( x.x) ( x.x)))  ! ??

47 Example:  ! (( x.x) ( y.y))  ! ??  x.x)  !  x.x) (( x.(x x)) ( x.(x x)))  ! ?? ((( x.x) ( x.x)) (( x.x) ( x.x)))  ! ??

48 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)))  ! ??

49 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)))  ! ??

50 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)

51 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 ??

52 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)))

53 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)))

54 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))

55 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)

56 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)

57 ! ! (( 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)

58  (( 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)

59 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

60 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!


Download ppt "Dynamic Semantics COS 441 Princeton University Fall 2004."

Similar presentations


Ads by Google