Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semantics for MinML COS 441 Princeton University Fall 2004.

Similar presentations


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

1 Semantics for MinML COS 441 Princeton University Fall 2004

2 MinML Concrete Syntax

3 Fib in MiniML fun fib(n:int):int is if <(n,2) then 1 else +(apply(fib,-(n,1)), apply(fib,-(n,2)))

4 MinML Abstract Syntax Syntax fun f ( x :  1 ):   is e represented as ABT fun(  1,  2, (f,x.e) ) f and x both bound in e

5 ABT for Fib in MiniML fun(int,int, (fib,n. if(<( n,2),1, +(apply( fib,-( n,1)), apply( fib,-( n,2)))) ) )

6 Type Environment Type environment (  ) maps names to type [x:  ] extends environment defined as Typing rule for names is

7 Integers

8 Booleans

9 must match

10 Functions

11 Inversion Theorems

12 Substitution and Weakening

13 Transition Semantics Set of states are closed expressions Set of final states (F) are values (v) v ::= true | false | n | fun(  1,  2,(f,x.e) )

14 Primitive “Instructions”

15 Search Rules

16 MinML Summary Hopefully what you saw was completely unsurprising and boring –It will get less boring when you have to implement the semantics for homework in SML Now that we have a very simple base language we can explore extensions to the language

17 Extending MinML We will consider extensions to the MinML –Adding real numbers –Adding lists –Adding lists and real numbers We will examine the impact on the static and dynamic semantics as well as discuss design alternatives and issues for each extension

18 MinML with real This is easy right? Just add new operators or overload existing operators Automatic or explicit conversion between real and int ? Examine simplest design first than consider more complicated ones

19 A Simple Design for Reals Just add new operators and constants Approach OCaml (ML Variant) uses Real Num’s rn::= 0.0 | 0.5 | 3.145 | … Real Op’s ro::= +. | -. | *. | =. | <. Types  ::= … | real Expr’s e::= … | i2r( e ) | r2i( e )

20 Typing for Simple Reals  ` +.( e 1,e 2 ) : real  ` e 1 : real  ` e 2 : real  ` =.( e 1,e 2 ) : bool  ` e 1 : real  ` e 2 : real  ` i2r( e ) : real  ` e : int  ` r2i( e ) : int  ` e : real   ` rn : real

21 Step Function for Simple Reals +.( rn 1,rn 2 )  rn 1 + real rn 2 i2r( n )  n.0 r2i( n. m )  n 

22 A Simple Design for Reals Straightforward no complications –Type checking syntax directed –Extensions to dynamic semantics Arguably clumsy from a user perspective –New operators for every type –Possibly O(n 2 ) coercions with more numeric types

23 Reals with Overloading Overload the existing operators Automatically promote integers to real but not the reverse Basically the Java design Real Num’s rn::= 0.0 | 0.5 | 3.145 | … Types  ::= … | real Expr’s e::= … | trunc( e )

24 Typing for Overloaded Reals  ` +( e 1,e 2 ) : real  ` e 1 : real  ` e 2 : real  ` =( e 1,e 2 ) : bool  ` e 1 : real  ` e 2 : real  ` e : real  ` e : int  ` trunc( e ) : int  ` e : real   ` rn : real

25 Step Fun. for Overloaded Reals +( rn 1,rn 2 )  rn 1 + real rn 2 n  n.0 trunc( n. m )  n 

26 Reals with Overloading Many issues with our extensions Type checking not syntax directed –But can choose unambiguous order or rule applications to get “sensible” system –Bottom up type checking algorithm Dynamic semantics non-deterministic –Bigger problem here semantics is not clear when ints are promoted to reals – +(1,2)  * 3.0 and +(1,2)  * 3

27 Reals with Overloading (Fixed) +( n 1,rn 2 )  n 1.0 + real rn 2 trunc( n. m )  n  +( rn 1,n 2 )  rn 1 + real n 2.0

28 Fixing Dynamic Semantics Rewrite semantics so that ints are promoted to reals only when used in context expecting a real Mirrors intuition of type-checking rules Suggests we must distinguish between reals and integers at runtime –Not a problem for an interpret but bigger problem for compiler –Messy number for rules!

29 Translation Semantics Avoid complicated dynamic semantics by using original simpler semantics of and type-directed translation of program Best of both worlds –User gets overloading –Compiler and proof hacker’s get simpler systems

30 Type Directed Translation  ` (e ) i2r( e’ ) ) : real  ` (e ) e’) : int   ` (rn ) rn) : real  ` (n ) n) : int  ` ( +( e 1,e 2 ) ) r+( e’ 1,e’ 2 ) ): real  ` (e 1 ) e’ 1 ) : real  ` (e 2 ) e’ 2 ) : real  ` ( +( e 1,e 2 ) ) i+( e’ 1,e’ 2 ) ): int  ` (e 1 ) e’ 1 ) : int  ` (e 2 ) e’ 2 ) : int

31 MinML with (  )list Simple ML generic list with hd/tl Types  ::= … | (  )list Expr’s e::=… | nil | cons( e 1, e 2 ) | hd( e ) | tl( e ) | isNil( e )

32 Typing MinML with (  )list  ` cons( e 1,e 2 ) : (  )list  ` e 1 :   ` e 2 : (  )list   ` nil : (  )list  ` hd( e ) :   ` e : (  )list  ` tl( e ) : (  )list  ` e : (  )list  ` isNil( e ) : bool  ` e : (  )list

33 Step Fun. for MinML with (  )list hd(cons( v hd, v tl ))  v hd tl(cons( v hd, v tl ))  v tl  What should the behavior of hd(nil) or tl(nil) be?

34 MinML with (  )list and casel Simple ML generic list with casel Types  ::= … | (  )list Expr’s e::=… | nil | cons( e 1, e 2 ) | casel( e ) of nil => e 1 or cons(x: ,y)=> e 2 end

35 MinML with (  )list Type checking not syntax directed –nil has many types –Can use weaker form of type inference to assign type to nil or raise error if it is not determinable casel version is less error prone and avoids messing up the dynamic semantics with corner cases

36 MinML with real and (  )list Lists hd/tl Lists casel Reals Separate Ops Reals Overloaed Ops

37 MinML with real and (  )list Lists hd/tl Lists casel Reals Separate Ops No unpleasant interactions Reals Overloaed Ops

38 MinML with real and (  )list Lists hd/tl Lists casel Reals Separate Ops No unpleasant interactions Reals Overloaed Ops How do we type check? +(hd(nil),1) No unpleasant interactions with a little thinking

39 What’s Going On? In the real type system all the primitive leaf rules/axioms for expressions provided us one unique type Bottom up reading of the expression could resolve overloading ambiguities Type inference for “ nil ” violates that assumption breaking overloading

40 What can we do about it? Give up on either overloading or type-inference! Complain when our algorithm gets too confused –Choose sane defaults (e.g. SML + is by default (int * int) -> int) –Allow optional annotations to help checker and override defaults –Pragmatic engineering and design issues Adopt Haskell type classes which allow for polymorphic runtime overloading!

41 Lessons Learned We can easily specify languages at a high- level with formal techniques We can reason about the design and interaction of systems incrementally The formalizes are just an aid to force you to think about all the details that come up when trying to do good language design


Download ppt "Semantics for MinML COS 441 Princeton University Fall 2004."

Similar presentations


Ads by Google