Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University."— Presentation transcript:

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

2 01/17/20032 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

3 01/17/20033 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  TY  TY) Given a type , the representation of  has the type (  ) TY

4 01/17/20034 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.

5 01/17/20035 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.

6 01/17/20036 A Special Case: Datatypes in ML The 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

7 01/17/20037 Another Special Case: Nested Datatypes The 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

8 01/17/20038 Define G.R. Datatype Constructors types  ::=  |  |       |      |    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.

9 01/17/20039 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.

10 01/17/200310 Typing Patterns:    p  where  is a term variable context defined as follows:  ::=  |  x  For instance, the following rule is for handling constructors: c    m    n       m       n    n  p      c  p    n    m       n    n 

11 01/17/200311 Typing Pattern Match Clauses (I) The following rule is for typing pattern match clauses:    p          e:         p  e:    

12 01/17/200312 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

13 01/17/200313 Higher-Order Abstract Syntax Trees (I) datatype HOAS = HOASlam of HOAS  HOAS | HOASapp of HOAS  HOAS For instance, the lambda-term x y.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) HOAS HOASapp: {’a,’b}. (’a  ‘b) HOAS  ’a HOAS  ‘b HOAS (  )HOAS is the type for h.o.a.s. trees representing expressions whose values have the type . Note g.r. datatype constructors cannot in general be defined inductively over types.

14 01/17/200314 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.

15 01/17/200315 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  ‘a That is, an object is a function that returns a value of type  when applied to a message of the type (  ) MSG.

16 01/17/200316 Integer Pair Objects We first assume that the following value constructors have been defined through some syntax: MSGgetfst: (int) MSG MSGgetsnd: (int) MSG MSGsetfst: 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

17 01/17/200317 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.

18 01/17/200318 But We Can Do Much Better! Please find in the paper an approach to implementing programming objects based on the above idea.

19 01/17/200319 More Applications Implementing type classes Implementing meta-programming Implementing typed code transformation Implementing …

20 01/17/200320 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)

21 01/17/200321 End of the Talk Thank you! Questions?


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

Similar presentations


Ads by Google