semantics for minml cos 441 princeton university fall 2004
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/1.jpg)
Semantics for MinML
COS 441
Princeton University
Fall 2004
![Page 2: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/2.jpg)
MinML Concrete Syntax
![Page 3: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/3.jpg)
Fib in MiniML
fun fib(n:int):int is
if <(n,2) then 1
else +(apply(fib,-(n,1)),
apply(fib,-(n,2)))
![Page 4: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/4.jpg)
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
![Page 5: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/5.jpg)
ABT for Fib in MiniML
fun(int,int,
(fib,n.if(<(n,2),1,
+(apply(fib,-(n,1)),
apply(fib,-(n,2))))))
![Page 6: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/6.jpg)
Type Environment
• Type environment () maps names to type• [x:] extends environment defined as
• Typing rule for names is
![Page 7: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/7.jpg)
Integers
![Page 8: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/8.jpg)
Booleans
![Page 9: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/9.jpg)
Booleans
must match
![Page 10: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/10.jpg)
Functions
![Page 11: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/11.jpg)
Inversion Theorems
![Page 12: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/12.jpg)
Substitution and Weakening
![Page 13: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/13.jpg)
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))
![Page 14: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/14.jpg)
Primitive “Instructions”
![Page 15: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/15.jpg)
Search Rules
![Page 16: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/16.jpg)
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
![Page 17: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/17.jpg)
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
![Page 18: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/18.jpg)
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
![Page 19: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/19.jpg)
A Simple Design for Reals
Just add new operators and constantsApproach 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)
![Page 20: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/20.jpg)
Typing for Simple Reals
` +.(e1,e2): real` e1 : real ` e2 : real
` =.(e1,e2): bool` e1 : real ` e2 : real
` i2r(e) : real` e : int
` r2i(e): int` e : real
` rn : real
![Page 21: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/21.jpg)
Step Function for Simple Reals
+.(rn1,rn2) rn1 +real rn2
i2r(n) n.0
r2i(n.m) n
![Page 22: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/22.jpg)
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(n2) coercions with more numeric
types
![Page 23: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/23.jpg)
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)
![Page 24: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/24.jpg)
Typing for Overloaded Reals
` +(e1,e2): real` e1 : real ` e2 : real
` =(e1,e2): bool` e1 : real ` e2 : real
` e : real` e : int
` trunc(e): int` e : real
` rn : real
![Page 25: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/25.jpg)
Step Fun. for Overloaded Reals
+(rn1,rn2) rn1 +real rn2
n n.0
trunc(n.m) n
![Page 26: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/26.jpg)
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
![Page 27: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/27.jpg)
Reals with Overloading (Fixed)
+(n1,rn2) n1.0 +real rn2
trunc(n.m) n
+(rn1,n2) rn1 +real n2.0
![Page 28: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/28.jpg)
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!
![Page 29: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/29.jpg)
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
![Page 30: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/30.jpg)
Type Directed Translation
` (e ) i2r(e’)) : real` (e )e’) : int
` (rn ) rn) : real ` (n ) n) : int
` (+(e1,e2) ) r+(e’1,e’2)): real` (e1)e’1) : real ` (e2)e’2) : real
` (+(e1,e2)) i+(e’1,e’2)): int` (e1)e’1) : int ` (e2)e’2) : int
![Page 31: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/31.jpg)
MinML with ()list
Simple ML generic list with hd/tl
Types ::= … | ()listExpr’s e ::= …
| nil | cons(e1,e2)
| hd(e) | tl(e)| isNil(e)
![Page 32: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/32.jpg)
Typing MinML with ()list
` cons(e1,e2): ()list` e1 : ` e2 : ()list
` nil : ()list
` hd(e): ` e : ()list
` tl(e): ()list` e : ()list
` isNil(e): bool` e : ()list
![Page 33: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/33.jpg)
Step Fun. for MinML with ()list
hd(cons(vhd,vtl)) vhd
tl(cons(vhd,vtl)) vtl
What should the behavior of hd(nil) or tl(nil) be?
![Page 34: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/34.jpg)
MinML with ()list and casel
Simple ML generic list with casel
Types ::= … | ()listExpr’s e ::= …
| nil | cons(e1,e2)
| casel(e) of
nil => e1
or cons(x:,y)=> e2
end
![Page 35: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/35.jpg)
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
![Page 36: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/36.jpg)
MinML with real and()listLists hd/tl Lists casel
Reals
Separate
Ops
Reals
Overloaed
Ops
![Page 37: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/37.jpg)
MinML with real and()listLists hd/tl Lists casel
Reals
Separate
Ops
No unpleasant interactions
No unpleasant interactions
Reals
Overloaed
Ops
![Page 38: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/38.jpg)
MinML with real and()listLists hd/tl Lists casel
Reals
Separate
Ops
No unpleasant interactions
No unpleasant interactions
Reals
Overloaed
Ops
How do we type check?
+(hd(nil),1)
No unpleasant
interactions
with a little thinking
![Page 39: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/39.jpg)
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
![Page 40: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/40.jpg)
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!
![Page 41: Semantics for MinML COS 441 Princeton University Fall 2004](https://reader035.vdocuments.us/reader035/viewer/2022062715/56649d6d5503460f94a4d463/html5/thumbnails/41.jpg)
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