Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aggregate Data Structures COS 441 Princeton University Fall 2004.

Similar presentations


Presentation on theme: "Aggregate Data Structures COS 441 Princeton University Fall 2004."— Presentation transcript:

1 Aggregate Data Structures COS 441 Princeton University Fall 2004

2 Data-Structures for MinML Products Types –n-tuples general case, will study 0-tuple (unit) and 2-tuples (pairs) Sums Types –Tagged types (We can just study binary tags) Recursive Types –Lists, Trees, … etc.

3 Product Types

4 Products: Static Semantics

5 Products: Dynamic Semantics

6 Sum Types

7 Sums: Static Semantics

8 Sums: Dynamic Semantics

9 Recursive Types

10 Rec. Types: Static Semantics

11 datatype nat = Zero | Succ of nat e : rec t is (Zero + Succ of t) unroll(e) : {(rec t is (Zero + Succ of t))/t} (Zero + Succ of t) unroll(e) : (Zero + Succ of (rec t is (Zero + Succ of t))

12 Rec. Types: Dynamic Semantics

13 Derived Types bool  unit + unit ilist  rec t is (unit + (int * t))

14 Examples: Booleans true  inl unit + unit (()) false  inr unit + unit (()) if(e,e 1,e 2 )  case  e 0 of inl(x:unit) => e 1 | inr(y:unit) => e 2

15 Examples: Lists ilist  rec t is (unit + (int * t)) nil  roll(inl unit +(int * ilist) (())) cons(e 1,e 2 )  roll(inr unit +(int * ilist) ((e 1,e 2 )))

16 Examples: Lists listcase e of nil => e 1 | cons(x,y) => e 2  case unroll(e 0 ) of inl(_:unit) => e 1 | inr(z:int * ilist) => split z as (x,y) in e 2

17 Roll/Unroll The roll and unroll primitives are there just to make the proof a bit easier and to guarantee syntax directed checking The are not needed in a realistic implementation only act as a notational fiction Compare to to inl/inr which have a real computational purpose

18 Some Subtle Bugs Harper’s notes present rules/syntax that are “buggy” Rules presented in a fashion that makes implementing type checking in a bottom up way impossible Also contains some redundant info Abstract syntax corrected in homework

19 Checking vs Inference We can view the relation  ` e :  in several ways A predicate on environments expressions and types val check : (env * exp * typ) -> boo l A function partial function that computes the type of an expression val infer : (env * exp) -> typ

20 Type Annotations Abstract syntax is “decorated” with type information to make checking/inference syntax directed More sophisticated implementations of checkers can avoid the need for type information

21 Case Can infer  by computing type of e 1 and e 2 Worth keeping for better error reporting. redundant

22 Recursive Types If we compute the type for e still must “guess”  to check rule Homework annotates roll to avoid need to “guess”

23 Example roll(inl unit + (rec t is (unit + t)) (unit)) : rec t is  What should  be for the above to type check?

24 Example roll(inl unit + (rec t is (unit + t)) (unit)) : rec t is  What should  be for the above to type check?  = (unit + t) {rec t is (unit + t) / t} (unit + t) = unit + rec t is (unit + t)

25 Summary Can express many interesting type using these primitive type constructors Algorithmic issues of type-checking sometime clutter the presentation of the rules


Download ppt "Aggregate Data Structures COS 441 Princeton University Fall 2004."

Similar presentations


Ads by Google