41
Semantics for MinML COS 441 Princeton University Fall 2004

Semantics for MinML COS 441 Princeton University Fall 2004

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Semantics for MinML COS 441 Princeton University Fall 2004

Semantics for MinML

COS 441

Princeton University

Fall 2004

Page 2: Semantics for MinML COS 441 Princeton University Fall 2004

MinML Concrete Syntax

Page 3: Semantics for MinML COS 441 Princeton University Fall 2004

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

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

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

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

Integers

Page 8: Semantics for MinML COS 441 Princeton University Fall 2004

Booleans

Page 9: Semantics for MinML COS 441 Princeton University Fall 2004

Booleans

must match

Page 10: Semantics for MinML COS 441 Princeton University Fall 2004

Functions

Page 11: Semantics for MinML COS 441 Princeton University Fall 2004

Inversion Theorems

Page 12: Semantics for MinML COS 441 Princeton University Fall 2004

Substitution and Weakening

Page 13: Semantics for MinML COS 441 Princeton University Fall 2004

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

Primitive “Instructions”

Page 15: Semantics for MinML COS 441 Princeton University Fall 2004

Search Rules

Page 16: Semantics for MinML COS 441 Princeton University Fall 2004

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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