Title: Implementing Typeful Program Transformations
1Implementing Typeful Program Transformations
- Chiyan Chen and Hongwei Xi
- Computer Science Department Boston University
2Program Transformation
- Essential and ubiquitous in programming language
study - Implementing interpreters reduction,
normalization. - Implementing compilers closure conversion, CPS
transformation. - Implementing meta-programming (partial
evaluation, run-time code generation).
3Representing Programs
- For program transformation, we first need a way
to represent the object language in the
meta-language. - Object language the language to which we apply
program transformations. - Meta language the language in which we implement
program transformations.
4Problems
- Types can effectively capture programming
invariants. - Usually object programs written in a typed
language are represented in a typeless way. - Typing information of an object program cannot be
reflected in the type of its representation in
the meta-language.
5A Typeless Representation for Simply-Typed
l-calculus
string ! exp ! exp ! exp
datatype exp Var of string Lam of string
exp App of exp exp
fun subst0 x sub e case e of
Var y ) if x y then sub else e
Lam (y, e) ) if x y then e
else Lam (y, subst0 x sub e)
App (e1, e2) ) App (subst0
x sub e1, subst0 x sub e2)
closed term!
Typing issue
Do not deal with name capturance
6Our Solution Typeful Program Representation and
Transformation
- In contrast to typeless representation, typing
information of an object program can be reflected
in the type of its representation. - Can be used to implement program transformations
in a typeful manner. - Meta properties of program transformations can be
reflect by the types of their implementations in
the meta language.
7Main Techniques
- Nameless representation (De Bruijn notations)
- Free variables in object programs are represented
using De Bruijn indices. - Reason avoid the involvement of variable names
in the types of the program representations. - Dependent types
- Capture typing information of the object programs
in the meta language. - An implementation in Dependent ML (DML)
- Meta language DML
- Object language simply-typed l-calculus
8De Bruijn Notation
Named l-calculus e x l x. e e1 (e2)
l x. l y. y (x)
Nameless l-calculus e n l e e1 (e2) n
1 2
l l 1 (2)
9Representing Types in the Object Language
Define new sorts for index expressions. Sorts
are types of index expressions.
Infix operators
infix ! sort ty int ! of (ty, ty) sort env
nil of (ty, env)
Representing types t
Define new sort
Representing typing environments G
Examples int ty, int ! int ty (int (int !
int) nil) env
10A Dependent Datatype for Typeful Program
Representation
datatype VAR (env, ty) t ty, G env VARone
(t G, t) t ty, t ty, G env VARshi (t
G, t) of VAR (G, t)
VARone P G env P t ty. VAR (tG, t) VARshi P
Genv P t ty P t ty. VAR(G, t) ! VAR(tG, t)
Given G and t representing G and t respectively,
we can use the datatype VAR(G, t) for variables
that are assigned type t under G.
11A Dependent Datatype for Typeful Program
Representation
- datatype EXP (env, ty)
- t ty, G env EXPvar (G, t) of VAR (G, t)
- t ty, t ty, G env EXPlam (G, t ! t) of
EXP (t G, t) - t ty, t ty, G env EXPapp (G, t) of EXP
(G, t ! t) EXP (G, t)
EXPvar P Genv P t ty. VAR(G, t) ! EXP(G,
t) EXPlam P G env P t ty P tty. EXP (tG,
t) ! EXP (G, t ! t) EXPapp P G env P t ty P
t ty. EXP (G, t ! t) EXP (G, t) ! EXP (G, t)
Given G and t representing G and t respectively,
we can use the datatype EXP(G, t) for expressions
that are assigned type t under G.
12Representing Programs Typefully
l x int. l y int ! int. y (x)
EXPlam (EXPlam (EXPapp (EXPvar VARone, EXPvar
(VARshi VARone)))) EXP (nil, int ! (int ! int)
! int)
13Implementing Substitution
s stands for mappings from variables (De Bruijn
indices) to terms. n s s(n) (l. e) s l. e
1 (s ") (e1 (e2)) s (e1 s) (e2 s)
binary operator, (e s) maps index 1 to e,
and maps index i1 with s(i) binary operator,
composition of two mappings. " a mapping that
maps a index i to i1.
From ls
typedef SUB (G1 env, G2 env) P t ty. VAR
(G1, t) ! EXP (G2, t) fun subst sub e
withtype P G1 env. P G2 env. P t ty. SUB
(G1, G2) ! EXP (G1, t) ! EXP (G2, t)
14Compute Head Normal Form
fun hnf e case e of EXPvar v
) e EXPlam e ) EXPlam (hnf e)
EXPapp (e1, e2) ) case (hnf e1)
of EXPlam e1 ) subst (subpre (hnf
e2) EXPvar) e1 e1 ) EXPapp (e1,
(hnf e2)) withtype P G env. P t ty. EXP(G,
t) ! EXP(G, t)
Potential open program manipulation
This is the operation as introduced in the
previous slide.
Type preserving!
15CPS Transformation (with named representation)
Question Now that we do not have names, how do
we transform the variables?
16Variable Mappings
Variable mappings (n) are very similar to
substitution mappings (s). A substitution
mapping maps variables to terms, while a variable
mapping maps variables in the source language to
variables in the target language. typedef VM
(G1 env, G2 env) P t ty. VAR (G1, t) ! VAR
(G2, T2 (t)) Given G1 and G2 representing G1
and G2, a value of the type VM(G1, G2) represents
a mapping that maps a variable typed to t in
context G1 into a variable typed to T2(t) in
context G2.
17CPS Transformation (with De Bruijn Notation)
Variable mappings
18A Key Property of CPS Transformation
- Assume that G e t is derivable, and G is a
type checking environment such that, - dom(G) ยต dom(G)
- For any x 2 dom(G), G(x) T2(G(x))
- then we have the following,
- G cps(e) T1(t)
- G cpsw(k, e) ?, if G k T2(t) ! ?
19A Typeful Implementation of CPS Transformation
fun cps withtype P t ty. P G1 env. P
G2 env. VM (G1, G2) ! EXP (G1, t) ! EXP(G2,
T1(t)) and cpsw withtype P t ty. P G1
env. P G2 env. VM (G1, G2) ! EXP(G2, T2(t) ! ?)
!
EXP (G1, t) ! EXP(G2, T1(t))
Reflect the key property!
20Conclusion
- An approach to represent (potentially open)
programs in a typeful manner. - De Bruijn notations.
- dependent types.
- Some general techniques for manipulating typeful
program representations, and doing typeful
program transformations. - Advantages
- Meta-properties of object program transformations
can be captured by the types of their
implementations in the meta-language. - A step towards certifying compiler.
21Related Work
- Twelf (Schurmann and Pfenning).
- Theorem prover/logical programming language.
- Use dependent type to represent object programs.
- Tagless interpreter (Pasalic, Sheard and Taha).
- Employs nameless representations (De Bruijn
notations). - Typeful representation with phantom types (Danvy
and Rhiger) - Program representations can not be used as
function arguments. - Guarded recursive datatype constructors (Xi, Chen
and Chen).
22Future Work
- Implementing other program transformations
- Typeful closure conversion.
- Representing calculus with richer features
- Polymorphism
- Pattern matching