01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston...

Post on 20-Dec-2015

219 views 0 download

Transcript of 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston...

01/17/2003 1

Guarded Recursive Datatype Constructors

Hongwei Xi and Chiyan Chen and Gang Chen

Boston University

01/17/2003 2

Talk Overview

Examples of guarded recursive (g.r.) datatype constructors

Issues on type-checking in the presence of g.r. datatype constructors

Applications of g.r. datatype constructors

01/17/2003 3

Type Representation (I)

The following syntax declares a guarded recursive datatype constructor TY of the kind type type.

typecon (type) TY = (* TY: type type *)| (int) TYint| {’a,’b}. (’a ’b) TYtup of ’a TY * ’b TY| {’a,’b}. (’a ’b) TYfun of ’a TY * ’b TY| {’a}. (’a TY) TYtyp of ’a TY

The value constructors associated with TY are assigned the following types:

TYint: (int) TY (int TY)TYtup: {’a,’b}. ’a TY ’b TY (’a ’b) TY ( TY TY TY) TYfun: {’a,’b}. ’a TY ’b TY (’a ’b) TY ( TY TY TY) TYtyp: {’a}. ’a TY (’a TY) TY ( TY TYTY)

Given a type , the representation of has the type () TY

01/17/2003 4

Type Representation (II)

For instance, The type int is represented as TYint, which is of

the type (int)TY. The type int int is represented as

TYfun (TYint, TYint),which is of the type (int int) TY.

The type int int int is represented as TYfun (TYtup (TYint, TYint), TYint),which is of the type (int int int)TY.

01/17/2003 5

Type Representation (III)

fun val2string (TYint) = fn x => int2string x| val2string (TYtup (pf1, pf2)) = fn x => “(“ ^ val2string pf1 (fst x) ^ “,” ^ val2string pf2 (snd x) ^ “)”| val2string (TYfun _) = fn _ => “[a function value]”| val2string (TYtyp _) = fn _ => “[a type value]”

withtype {‘a}. ‘a TY ‘a string

Given a term pf representing type and a value v of

type , (val2sting pf v) returns a string representation

of v.

01/17/2003 6

A Special Case: Datatypes in MLThe datatype constructors in ML are a special case of

g.r. datatype constructors.

For instance,

datatype ‘a list = nil | cons of ‘a ‘a list

corresponds to:

typecon (type) list = {‘a}. (‘a) nil| {‘a}. (‘a) cons of ‘a ‘a list

01/17/2003 7

Another Special Case: Nested DatatypesThe nested datatype constructors are also a special case of g.r.

datatype constructors.

For instance,

datatype ‘a raList =Nil | Even of (‘a ‘a) raList | Odd of ‘a * (‘a ‘a) raList

correponds to

typecon (type) raList = {‘a}. (‘a) Nil| {‘a}. (‘a) Even of (‘a ‘a) raList

| {‘a}. (‘a) Odd of ‘a (‘a ‘a) raList

01/17/2003 8

Define G.R. Datatype Constructorstypes

::= | | | | n| type variable contexts

::= | , | ,

As an example, TY is formally defined as follows:t: type type. . int.1 ,, . t t , , . t t , t. 1 t

So we call TY a guarded recursive datatype constructor.

01/17/2003 9

Type Constraints

A type constraint is of the form: We say is a solution to if

for each guard in , where is the usual syntactic equality modulo -conversion.

holds if for all solutions to

The relation is decidable.

01/17/2003 10

Typing Patterns:

pwhere is a term variable

context defined as follows:

::= | x

For instance, the following rule is for handling constructors:

cmnm n npcpnm n n

01/17/2003 11

Typing Pattern Match Clauses (I) The following rule is for typing pattern match

clauses:

pe: p e:

01/17/2003 12

Typing Pattern Match Clauses (II)Let us see an example:

TYint => (fn x => int2string x) ‘a TY (‘a string)

TYint ‘a TY => (int ‘a; )

int ‘a; fn x => int2string x : ‘a string

01/17/2003 13

Higher-Order Abstract Syntax Trees (I)datatype HOAS =

HOASlam of HOAS HOAS| HOASapp of HOAS HOAS

For instance, the lambda-term xy.x(y) is represented as

HOASlam(fn x => HOASlam (fn y => HOASapp (x, y)))

typecon (type) HOAS = {’a,’b}. (’a ’b) HOASlam of ’a HOAS ’b HOAS| {’a,’b}. (’b) HOASapp of (’a ‘b) HOAS ’b HOAS

HOASlam: {’a,’b}. (’a HOAS ’b HOAS) (’a ’b) HOASHOASapp: {’a,’b}. (’a ‘b) HOAS ’a HOAS ‘b HOAS

()HOAS is the type for h.o.a.s. trees representing expressions whosevalues have the type .

Note g.r. datatype constructors cannot in general be defined inductively over types.

01/17/2003 14

Higher-Order Abstract Syntax Trees (II)fun hnf (t as HOASlam _) = t

| hnf (HOASapp (t1, t2)) = case hnf (t1) of HOASlam f => hnf (f t2) | t1’ => HOASapp (t1’, t2)

withtype {‘a}. ‘a HOAS ‘a HOAS

The function hnf computes the head normal form of a given lambda-expression.

01/17/2003 15

Implementing Programming Objects Let MSG be an extensible g.r. datatype

constructor of the kind type type. Intuitively, () MSG is the type for a message

that requires its receiver to return a value of type .

We use the following type Obj for objects: {‘a}. (‘a) MSG ‘aThat is, an object is a function that returns a value of type when applied to a message of the type () MSG.

01/17/2003 16

Integer Pair Objects

We first assume that the following value constructors have been defined through some syntax:

MSGgetfst: (int) MSG MSGgetsnd: (int) MSGMSGsetfst: int (unit) MSG MSGsetsnd: int (unit) MSG

fun newIntPair (x: int, y: int): Obj = let val xref = ref x and yref = ref y fun dispatch (MSGgetfst) = !x | dispatch (MSGgetsnd) = !y | dispatch (MSGsetfst x’) = xref := x’ | dispatch (MSGsetsnd y’) = yref := y’ | dispatch _ = raise UnknownMessage in dispatch end

01/17/2003 17

The Type Obj Is Unsatisfactory There is an obvious problem with the type

Obj = {‘a} (‘a) MSG ‘a for objects: it is impossible to use types to differentiate objects.

Assume anIntPair is an integer pair object and MSGfoo is some message; then anIntPair (MSGfoo)is well-typed and its execution results in a run-time UnknownMessage exception to be raised.

01/17/2003 18

But We Can Do Much Better!

Please find in the paper an approachto implementing programming objects based on the above idea.

01/17/2003 19

More Applications

Implementing type classes Implementing meta-programming Implementing typed code transformation Implementing …

01/17/2003 20

Some Related Works

Intensional polymorphism (Harper, Morrisett, Crary, Weirich; Shao, Trifinov, Saha, et al)

Generic polymorphism (Dubois, Rouaix, Weis) Qualified Types (Mark Jones) Dependent ML (Xi and Pfenning)

01/17/2003 21

End of the Talk

Thank you!

Questions?