Aggregate Data Structures COS 441 Princeton University Fall 2004.

25
Aggregate Data Structures COS 441 Princeton University Fall 2004
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    1

Transcript of Aggregate Data Structures COS 441 Princeton University Fall 2004.

Page 1: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Aggregate Data Structures

COS 441

Princeton University

Fall 2004

Page 2: Aggregate Data Structures COS 441 Princeton University Fall 2004.

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.

Page 3: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Product Types

Page 4: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Products: Static Semantics

Page 5: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Products: Dynamic Semantics

Page 6: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Sum Types

Page 7: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Sums: Static Semantics

Page 8: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Sums: Dynamic Semantics

Page 9: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Recursive Types

Page 10: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Rec. Types: Static Semantics

Page 11: Aggregate Data Structures COS 441 Princeton University Fall 2004.

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

Page 12: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Rec. Types: Dynamic Semantics

Page 13: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Derived Types

bool unit + unit

ilist rec t is (unit + (int * t))

Page 14: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Examples: Booleans

true inlunit + unit(())

false inrunit + unit(())

if(e,e1,e2)

case e0 of

inl(x:unit) => e1

| inr(y:unit) => e2

Page 15: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Examples: Lists

ilist rec t is (unit + (int * t))

nil roll(inl unit +(int * ilist)(()))

cons(e1,e2)

roll(inr unit +(int * ilist)((e1,e2)))

Page 16: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Examples: Lists

listcase e of

nil => e1 | cons(x,y) => e2

case unroll(e0) of

inl(_:unit) => e1

| inr(z:int * ilist) =>

split z as (x,y) in e2

Page 17: Aggregate Data Structures COS 441 Princeton University Fall 2004.

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

Page 18: Aggregate Data Structures COS 441 Princeton University Fall 2004.

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

Page 19: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Checking vs Inference

• We can view the relation ` e : in several ways

• A predicate on environments expressions and types

val check : (env * exp * typ) -> bool• A function partial function that computes

the type of an expressionval infer : (env * exp) -> typ

Page 20: Aggregate Data Structures COS 441 Princeton University Fall 2004.

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

Page 21: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Case

Can infer by computing type of e1 and e2

Worth keeping for better error reporting.

redundant

Page 22: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Recursive Types

If we compute the type for e still must “guess” to check rule

Homework annotates roll to avoid need to “guess”

Page 23: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Example

roll(inlunit + (rec t is (unit + t))(unit)) : rec t is

What should be for the above to type check?

Page 24: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Example

roll(inlunit + (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)

Page 25: Aggregate Data Structures COS 441 Princeton University Fall 2004.

Summary

• Can express many interesting type using these primitive type constructors

• Algorithmic issues of type-checking sometime clutter the presentation of the rules