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

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Modern Programming Languages, 2nd ed.
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Type Analysis and Typed Compilation Stephanie Weirich Cornell University.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Data  Consists of constructions that can be inspected, taken apart, or joined to form.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
Distributed Meta- Programming Rui Shi, Chiyan Chen and Hongwei Xi Boston University.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Typed Compilation of Recursive Datatypes Joseph C. Vanderwaart, Derek Dreyer, Leaf Petersen, Karl Crary, Robert Harper, and Perry Cheng Carnegie Mellon.
What is a recursive module? Crary, Harper, Puri Module Systems, Fall 2002 Aleksey Kliger.
Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.
25 September 2002BU CAS CS520 1 Parsing Combinators Hongwei Xi Boston University.
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
1 CPS Transform for Dependent ML Hongwei Xi University of Cincinnati and Carsten Schürmann Yale University.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Dependently Typed Pattern Matching Hongwei Xi Boston University.
Survey of Typed Assembly Language (TAL) Introduction and Motivation –Conventional untyped compiler < Typed intermediate languages –Typed intermediate language.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
1 A Dependently Typed Assembly Language Hongwei Xi University of Cincinnati and Robert Harper Carnegie Mellon University.
Good Advice for Type-directed Programming Aspect-oriented Programming and Extensible Generic Functions Geoffrey Washburn [ ] Joint.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
10 September Implementing Staged Computation Chiyan Chen and Hongwei Xi Boston University.
Generative type abstraction and type-level computation (Wrestling with System FC) Stephanie Weirich, Steve Zdancewic University of Pennsylvania Dimitrios.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Distributed Meta- Programming (To appear GPCE’06) Rui Shi, Chiyan Chen and Hongwei Xi Boston University.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Last Time Introduction Course Logistics Introduction to ML Base Types Tuples and Records Functions & Polymorphism See Harper ’ s Intro to ML.
1 Unifying Object-Oriented Programming with Typed Functional Programming Hongwei Xi Boston University.
PADL041 A Typeful Approach to Object- Oriented Programming with Multiple Inheritance Chiyan Chen, Rui Shi, Hongwei Xi Computer Science Dept. Boston.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Closure and Environment Compiler Baojian Hua
Compiling with Dependent Types Hongwei Xi University of Cincinnati.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
A Theory of Hygienic Macros David Herman, Mitchell Wand Northeastern University.
CSE S. Tanimoto Introduction to ML 1 Introduction to ML History Special features Interacting with ML ML’s basic types ML’s composite types Math.
Chapter 9: Functional Programming in a Typed Language.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Singleton Kinds and Singleton Types Christopher A. Stone August 2, 1999 Thesis Committee Bob Harper, chair Peter Lee John Reynolds Jon Riecke (Bell Laboratories)
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CSE-321 Programming Languages (So Many Topics) POSTECH May 29, 2006 박성우.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics.
Programming Languages and Compilers (CS 421)
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
Course Overview PART I: overview material PART II: inside a compiler
Review Previously User-defined data types: records, variants
Presentation transcript:

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

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

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

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.

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.

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

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

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.

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.

01/17/ 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 

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

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

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

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

01/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/ But We Can Do Much Better! Please find in the paper an approach to implementing programming objects based on the above idea.

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

01/17/ 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/ End of the Talk Thank you! Questions?