01/17/20031 guarded recursive datatype constructors hongwei xi and chiyan chen and gang chen boston...
Post on 20-Dec-2015
219 views
TRANSCRIPT
![Page 1: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/1.jpg)
01/17/2003 1
Guarded Recursive Datatype Constructors
Hongwei Xi and Chiyan Chen and Gang Chen
Boston University
![Page 2: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/2.jpg)
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
![Page 3: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/3.jpg)
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
![Page 4: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/4.jpg)
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.
![Page 5: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/5.jpg)
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.
![Page 6: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/6.jpg)
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
![Page 7: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/7.jpg)
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
![Page 8: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/8.jpg)
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.
![Page 9: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/9.jpg)
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.
![Page 10: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/10.jpg)
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
![Page 11: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/11.jpg)
01/17/2003 11
Typing Pattern Match Clauses (I) The following rule is for typing pattern match
clauses:
pe: p e:
![Page 12: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/12.jpg)
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
![Page 13: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/13.jpg)
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.
![Page 14: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/14.jpg)
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.
![Page 15: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/15.jpg)
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.
![Page 16: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/16.jpg)
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
![Page 17: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/17.jpg)
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.
![Page 18: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/18.jpg)
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.
![Page 19: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/19.jpg)
01/17/2003 19
More Applications
Implementing type classes Implementing meta-programming Implementing typed code transformation Implementing …
![Page 20: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/20.jpg)
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)
![Page 21: 01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University](https://reader030.vdocuments.us/reader030/viewer/2022032704/56649d475503460f94a229d4/html5/thumbnails/21.jpg)
01/17/2003 21
End of the Talk
Thank you!
Questions?