## Meta-programming through typeful code representation (2003)

### Cached

### Download Links

- [www.cs.utah.edu]
- [www.cs.bu.edu]
- [www.cs.bu.edu]
- [www.cs.bu.edu]
- [www.church-project.org]
- [www.church-project.org]
- [www.cs.bu.edu]
- [www.cs.bu.edu]
- [www.cs.bu.edu]
- DBLP

### Other Repositories/Bibliography

Venue: | In Proceedings of the Eighth ACM SIGPLAN International Conference on Functional Programming |

Citations: | 22 - 4 self |

### BibTeX

@INPROCEEDINGS{Chen03meta-programmingthrough,

author = {Chiyan Chen and Hongwei Xi},

title = {Meta-programming through typeful code representation},

booktitle = {In Proceedings of the Eighth ACM SIGPLAN International Conference on Functional Programming},

year = {2003},

pages = {169--180},

publisher = {ACM Press}

}

### Years of Citing Articles

### OpenURL

### Abstract

By allowing the programmer to write code that can generate code at run-time, meta-programming offers a powerful approach to program construction. For instance, meta-programming can often be employed to enhance program efficiency and facilitate the construction of generic programs. However, meta-programming, especially in an untyped setting, is notoriously error-prone. In this paper, we aim at making meta-programming less error-prone by providing a type system to facilitate the construction of correct meta-programs. We first introduce some code constructors for constructing typeful code representation in which program variables are replaced with deBruijn indices, and then formally demonstrate how such typeful code representation can be used to support meta-programming. The main contribution of the paper lies in recognition and then formalization of a novel approach to typed meta-programming that is practical, general and flexible.

### Citations

1562 | The Definition of Standard ML
- Miller, Tofte, et al.
- 1990
(Show Context)
Citation Context ...d code of type τ. A common approach to capturing the notion of closed code is through higher-order abstract syntax (h.o.a.s.) [3, 24, 23]. For instance, the following declaration in Standard ML (SML) =-=[19]-=- declares a datatype for representing pure untyped closed λexpressions: datatype exp = Lam of (exp -> exp) | App of exp * exp As an example, the representation of the untyped λ-expression λx.λy.y(x) i... |

605 |
The Definition of Standard ML (Revised
- Milner, Tofte, et al.
- 1997
(Show Context)
Citation Context ...e notion of closed code is through higherorder abstract syntax (h.o.a.s.) (Church, 1940; Pfenning & Elliott, 1988; Pfenning & Lee, 1989). For instance, the following declaration in Standard ML (SML) (=-=Milner et al., 1997-=-) declares a datatype for representing pure untyped closed λ-expressions: datatype exp = Lam of (exp -> exp) | App of exp * exp As an example, the representation of the untyped λ-expression λx.λy.y(x)... |

358 |
Principal type-schemes for functional programs
- Damas, Milner
- 1982
(Show Context)
Citation Context ...anguage MLcode by extending ML with code constructors (Lift, One, Shi, App, Lam, Fix) and the special function run, and then employ a type inference algorithm (e.g., one based on the one described in =-=[4]-=-) to elaborate programs in MLcode into programs, or more precisely typing derivations of programs, in λcode. As an example, we show that the function genEvalPoly in Section 1 can be implemented as fol... |

302 |
Higher-order abstract syntax
- Pfenning, Elliot
- 1988
(Show Context)
Citation Context ...type τ, the type (τ)code should only be for expressions representing closed code of type τ. A common approach to capturing the notion of closed code is through higher-order abstract syntax (h.o.a.s.) =-=[3, 24, 23]-=-. For instance, the following declaration in Standard ML (SML) [19] declares a datatype for representing pure untyped closed λexpressions: datatype exp = Lam of (exp -> exp) | App of exp * exp As an e... |

302 | Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the Church-Rosser theorem - Bruijn - 1972 |

237 | Multi-Stage Programming with Explicit Annotations
- Taha, Sheard
- 1997
(Show Context)
Citation Context ... stage needs to be used at a later stage. For instance, in the expression ‘(lam x.x + x), the function +, which is defined at stage 0, is used at stage 1. This is called cross-stage persistence (CSP) =-=[32]-=-. As is indicated in the typing rules (ty-var-0) and (ty-cst), CSP for variables at stage 0 and constants is implicit in λ + code . However, for variables introduced at stage k > 0, CSP needs to be ex... |

224 |
E cient implementation of the Smalltalk-80 system
- Deutsch, man
- 1984
(Show Context)
Citation Context ...programs that can generate programs at run-time. For instance, there are numerous cases (kernel implementation [18], graphics [26], interactive media [10], method dispatch in objectoriented languages =-=[9, 2]-=-, etc.) where run-time code generation can be employed to reap significant gain in run-time performance [17]. To illustrate this point, we define a function evalPoly as follows in Scheme for evaluatin... |

191 | Customization: Optimizing compiler technology for SELF, a dynamically-typed object-oriented programming language
- CHAMBERS, UNGAR
- 1989
(Show Context)
Citation Context ...programs that can generate programs at run-time. For instance, there are numerous cases (kernel implementation [18], graphics [26], interactive media [10], method dispatch in objectoriented languages =-=[9, 2]-=-, etc.) where run-time code generation can be employed to reap significant gain in run-time performance [17]. To illustrate this point, we define a function evalPoly as follows in Scheme for evaluatin... |

185 | A modal analysis of staged computation
- Davies, Pfenning
(Show Context)
Citation Context ...to multi-level staged computation [15, 14]. Most of the work along this line attempts to stage programs automatically (e.g., by performing binding-time analysis) and is done in an untyped setting. In =-=[7]-=-, a lambda-calculus λ based on the intuitionistic modal logic S4 is presented for studying staged computation in a typed setting. Given a type A, a type constructor , which corresponds to a modality o... |

162 | Nominal Logic, A first order theory of names and binding
- Pitts
(Show Context)
Citation Context ...e notion of (potentially open) code in λ ○ , the calculus ν in [21] relaxes the notion of closed code in λ by extending λ with a notion of names that is inspired by some developments in Nominal Logic =-=[27]-=- and FreshML [28]. Given an expression representing some code, the free variables in the code are represented as certain distinct names; the set of these names, which is called the support of the expr... |

156 | Optimizing ml with run-time code generation
- Leone, Lee
- 1995
(Show Context)
Citation Context ... [18], graphics [26], interactive media [10], method dispatch in objectoriented languages [9, 2], etc.) where run-time code generation can be employed to reap significant gain in run-time performance =-=[17]-=-. To illustrate this point, we define a function evalPoly as follows in Scheme for evaluating a polynomial p at a given point x. (define (evalPoly p x) (if (null? p) 0 (+ (car p) (* x (evalPoly (cdr p... |

154 |
de Bruijn. Lambda calculus notation with nameless dummies, a tool for automatic formula manipulation, with application to the church-rosser theorem
- G
- 1972
(Show Context)
Citation Context ...s to represent code that not only support direct open code manipulation but also avoid the problem of free variable evaluation. As for the free program variables in open code, we use deBruijn indices =-=[8]-=- to represent them. For instance, we can declare the following datatype in Standard ML to represent pure untyped λ-expressions. datatype exp = One | Shi of exp | Lam of exp | App of exp * exp We use O... |

129 | Guarded recursive datatype constructors
- Xi, Chen, et al.
- 2003
(Show Context)
Citation Context ... difficult, if not impossible, to declare a datatype in ML for precisely representing typed λ-expressions, this can be readily done if we extend ML with guarded recursive (g.r.) datatype constructors =-=[35]-=-. For instance, we can declare a g.r. datatype constructor (·)HOAS and associate with it two value constructors HOASlam and HOASapp that are assigned the following types, respectively. 2 ∀α∀β.((α)HOAS... |

122 |
An experiment in partial evaluation: The generation of a compiler generator
- Jones, Sestoft, et al.
- 1985
(Show Context)
Citation Context ...f generating extensions, which Figure 8. A meta-programming example: inner product is now often called staged computation, is introduced in [12] and later expanded into multi-level staged computation =-=[15, 14]-=-. Most of the work along this line attempts to stage programs automatically (e.g., by performing binding-time analysis) and is done in an untyped setting. In [7], a lambda-calculus λ based on the intu... |

102 | 98: A non-strict, purely functional language. http://www.haskell.org - Jones, Hughes - 1999 |

92 | Simple imperative polymorphism
- Wright
- 1995
(Show Context)
Citation Context ...d Λ e allows us to uniquely determine the rule that is applied last in the typing derivation of a given expression. Preparing for accommodating effects in λcode, we impose the usual value restriction =-=[34]-=- by requiring that Λ i be only applied to values. It is straightforward to extend λcode with some base types (e.g., bool and int for booleans and integers, respectively) and constants and functions re... |

88 | A metalanguage for programming with bound names modulo renaming
- Pitts, Gabbay
(Show Context)
Citation Context ...ntially open) code in λ ○ , the calculus ν in [21] relaxes the notion of closed code in λ by extending λ with a notion of names that is inspired by some developments in Nominal Logic [27] and FreshML =-=[28]-=-. Given an expression representing some code, the free variables in the code are represented as certain distinct names; the set of these names, which is called the support of the expression, is reflec... |

84 | Synthesis: An Efficient Implementation of Fundamental Operating System Services
- Massalin
- 1992
(Show Context)
Citation Context ....edu 1 Introduction Situations often arise in practice where there is a need for writing programs that can generate programs at run-time. For instance, there are numerous cases (kernel implementation =-=[18]-=-, graphics [26], interactive media [10], method dispatch in objectoriented languages [9, 2], etc.) where run-time code generation can be employed to reap significant gain in run-time performance [17].... |

43 |
M.F.: Environment classifiers
- Taha, Nielsen
- 2003
(Show Context)
Citation Context ...s there cannot assign f (‘x) a “closed code” type. Though this is a highly contrived example, it nonetheless indicates some inadequacy in the notion of closed types captured by these type systems. In =-=[31]-=-, there is another type system that aims at assigning more accurate types to meta-programs in MetaML. In the type system, a notion of environment classifiers is introduced. Generally speaking, environ... |

39 | Hardware/software tradeoffs for bitmap graphics on the blit
- Pike, Locanthi, et al.
- 1985
(Show Context)
Citation Context ...tion Situations often arise in practice where there is a need for writing programs that can generate programs at run-time. For instance, there are numerous cases (kernel implementation [18], graphics =-=[26]-=-, interactive media [10], method dispatch in objectoriented languages [9, 2], etc.) where run-time code generation can be employed to reap significant gain in run-time performance [17]. To illustrate ... |

32 | T.: Closed types for a safe imperative MetaML
- Calcagno, Moggi, et al.
- 2003
(Show Context)
Citation Context ...nstance, if n = 2, then f1 is basically equivalent to the function defined below; fn v1 => ‘(fn v2 => 0 + ˆ(Lift (sub (v1, 0))) * sub (v2, 0) + ˆ(Lift (sub (v1, 1))) * sub (v2, 1)) if v1[0] = 6 and v1=-=[1]-=- = 23, then f2 is basically equivalent to the function defined below. fn v2 => 0 + 6 * sub (v2, 0) + 23 * sub (v2, 1) Notice that this example involves expressions at level 2 and the use of a CSP oper... |

30 | Computation and Deduction
- Pfenning
- 2000
(Show Context)
Citation Context ...type τ, the type (τ)code should only be for expressions representing closed code of type τ. A common approach to capturing the notion of closed code is through higher-order abstract syntax (h.o.a.s.) =-=[3, 24, 23]-=-. For instance, the following declaration in Standard ML (SML) [19] declares a datatype for representing pure untyped closed λexpressions: datatype exp = Lam of (exp -> exp) | App of exp * exp As an e... |

28 | An automatic program generator for multilevel specialization. Lisp and symbolic computation
- Glück, Jørgensen
- 1997
(Show Context)
Citation Context ...f generating extensions, which Figure 8. A meta-programming example: inner product is now often called staged computation, is introduced in [12] and later expanded into multi-level staged computation =-=[15, 14]-=-. Most of the work along this line attempts to stage programs automatically (e.g., by performing binding-time analysis) and is done in an untyped setting. In [7], a lambda-calculus λ based on the intu... |

27 |
et al. Haskell 98: A non-strict, purely functional language
- Jones, Hughes
- 1999
(Show Context)
Citation Context ...xpression is expected to be assigned the type scheme σ = ∀α1 ...∀αn.τ. We have implemented a type inference algorithm based on the one in [4] that supports the usual let-polymorphism. Like in Haskell =-=[22]-=-, if the type of a recursive function is given, then polymorphic recursion is allowed in the definition of the function. We are now ready to present some examples of meta-programs in ML + code . Examp... |

27 |
A Language with Eval and Polymorphism
- Pfenning, Lee
- 1989
(Show Context)
Citation Context ...t takes an s-expression and an environment as its arguments and returns the value of the expression represented by the s-expression. 2 Note that, unlike a similar inductively defined type constructor =-=[25]-=-, HOAS cannot be inductively defined. By associating with HOAS some extra value constructors, we can represent closed code of type τ as expressions of type (τ)HOAS. In other words, we can define (·)co... |

24 | Manufacturing datatypes
- Hinze
- 2001
(Show Context)
Citation Context ... used to encode values (e.g., natural numbers) are particularly abundant in Haskell and are often referred to as pearls in functional programming. For instance, some of such examples can be found in (=-=Hinze, 2001-=-; Chen et al., 2004b). However, we feel that the use of env allows for a cleaner presentation of λcode. We use Θ for finite mappings defined below and dom(Θ) for the domain of Θ. Θ ::= [] | Θ[a ↦→ s] ... |

22 |
A temporal logic approach to binding-time analysis
- Davies
- 1996
(Show Context)
Citation Context ...ersion (power2) does not have a counterpart in λ as it involves the use of open code: there is a free variable in the code produced by (aux n ‘x). An approach to addressing the limitation is given in =-=[6]-=-, where a type constructor ○ is introduced, which corresponds to the modality in discrete temporal logic for propositions that are true at the subsequent time moment. Given a type A, the type ○A is fo... |

19 |
Schiffman: Efficient Implementation of the
- Deutsch, Allan
- 1983
(Show Context)
Citation Context ... run-time. For instance, there are many examples (kernel implementation (Massalin, 1992), graphics (Pike et al., 1985), interactive media (Draves, 1998), method dispatch in object-oriented languages (=-=Deutsch & Schiffman, 1984-=-; Chambers & Ungar, 1989), etc.) where run-time code generation can be employed to reap significant gain in run-time performance (Leone & Lee, 1996). To illustrate this point, we define a function eva... |

13 |
On the partial computation principle
- Ershov
- 1977
(Show Context)
Citation Context ...ted for generating compilers from interpreters. The notion of generating extensions, which Figure 8. A meta-programming example: inner product is now often called staged computation, is introduced in =-=[12]-=- and later expanded into multi-level staged computation [15, 14]. Most of the work along this line attempts to stage programs automatically (e.g., by performing binding-time analysis) and is done in a... |

12 |
Dybvig. Writing Hygienic Macros in Scheme with Syntax-Case
- Kent
- 1992
(Show Context)
Citation Context ...alPoly p) ’())) (define (aux xs) (if (null? xs) 0 (+ (proc (car xs)) (aux xs)))) (aux xs)) Meta-programming, though useful, is notoriously error-prone in general and approaches such as hygenic macros =-=[11]-=- have been proposed to address the issue. Programs generated at run-time often contain type errors or fail to be closed, and errors in meta-programs are generally more difficult to locate and fix than... |

12 | Implementing cut elimination: A case study of simulating dependent types in Haskell
- Chen, Zhu, et al.
- 2004
(Show Context)
Citation Context ...de values (e.g., natural numbers) are particularly abundant in Haskell and are often referred to as pearls in functional programming. For instance, some of such examples can be found in (Hinze, 2001; =-=Chen et al., 2004-=-b). However, we feel that the use of env allows for a cleaner presentation of λcode. We use Θ for finite mappings defined below and dom(Θ) for the domain of Θ. Θ ::= [] | Θ[a ↦→ s] Note that [] stands... |

10 |
An idealized MetaML: Simpler, and more expressive (includes proofs
- Moggi, Taha, et al.
- 1998
(Show Context)
Citation Context ... system of MetaML contained a defect caused by free variable evaluation (as the function run is available in MetaML) and there have since been a number of attempts to fix the defect. For instance, in =-=[20]-=-, types for (potentially) open code are refined and it then becomes possible to form types for closed code only. In general, a value can be assigned a type for closed code only if the value does not d... |

9 |
A formulation of the simple type theory of types
- Church
- 1940
(Show Context)
Citation Context ...type τ, the type (τ)code should only be for expressions representing closed code of type τ. A common approach to capturing the notion of closed code is through higher-order abstract syntax (h.o.a.s.) =-=[3, 24, 23]-=-. For instance, the following declaration in Standard ML (SML) [19] declares a datatype for representing pure untyped closed λexpressions: datatype exp = Lam of (exp -> exp) | App of exp * exp As an e... |

8 | A simple take on typed abstract syntax in Haskell-like languages
- Danvy, Rhiger
- 2001
(Show Context)
Citation Context ...xpression to the representation of the expression). However, it handles reification for complex values such as functions in a manner that seems too limited to support (practical) meta-programming. In =-=[5]-=-, an approach is presented that implements (a form of) typeful h.o.a.s. in Haskell-like languages to represent simply typed λterms. With this approach, it is shown that an implementation of the normal... |

8 | Writing Hygienic Macros in Scheme with Syntax-Case
- Dybvig
- 1992
(Show Context)
Citation Context ...Poly p) ’())) (define (aux xs) (if (null? xs) 0 (+ (proc (car xs)) (aux xs)))) (aux xs)) Meta-programming, though useful, is notoriously error-prone in general and approaches such as hygienic macros (=-=Dybvig, 1992-=-) have been proposed to address the issue. Programs generated at run-time often contain type errors or fail to be closed, and errors in meta-programs are generally more difficult to locate and fix tha... |

6 |
Type-safe, self inspecting code
- Baars, Swierstra
- 2004
(Show Context)
Citation Context ...the latter uses typeful code representation. Recently, we have seen that the technique developed in (Baars & Swierstra, 2002) is subsequently employed in an application involving parsing combinators (=-=Baars & Swierstra, 2004-=-). 3 The Language λ + code We extend λcode to λ + code with some meta-programming syntax adopted from Scheme and MetaML: expressions e ::= . . . | ‘(e) | ˆ(e) Loosely speaking, the notation ‘(·) corre... |

6 |
An idealized MetaML
- Moggi, Taha, et al.
- 1999
(Show Context)
Citation Context ...system of MetaML contained a defect caused by free variable evaluation (as the function run is available in MetaML) and there have since been a number of attempts to fix the defect. For instance, in (=-=Moggi et al., 1999-=-), types for (potentially) open code are refined and it then becomes possible to form types for closed code only. In general, a value can be assigned a type for closed code only if the value does not ... |

4 |
Partial evaluation for media processing
- Draves
- 1998
(Show Context)
Citation Context ...ise in practice where there is a need for writing programs that can generate programs at run-time. For instance, there are numerous cases (kernel implementation [18], graphics [26], interactive media =-=[10]-=-, method dispatch in objectoriented languages [9, 2], etc.) where run-time code generation can be employed to reap significant gain in run-time performance [17]. To illustrate this point, we define a ... |

4 |
Higher-order abstract syntax. Pages 199–208 of
- Pfenning, Elliott
- 1988
(Show Context)
Citation Context ...pe code(τ) should only be for expressions representing closed code of type τ. A common approach to capturing the notion of closed code is through higherorder abstract syntax (h.o.a.s.) (Church, 1940; =-=Pfenning & Elliott, 1988-=-; Pfenning & Lee, 1989). For instance, the following declaration in Standard ML (SML) (Milner et al., 1997) declares a datatype for representing pure untyped closed λ-expressions: datatype exp = Lam o... |

4 | Template meta-programming for Haskell. Pages 1–16 of: Haskell: Workshop - Sheard, Jones, et al. - 2002 |

3 |
On the partial computation principle. Information processing letters
- Ershov
- 1977
(Show Context)
Citation Context ...t level 1 the three Futamura projections are presented for generating compilers from interpreters. The notion of generating extensions, which is now often called staged computation, is introduced in (=-=Ershov, 1977-=-) and later expanded into multi-level staged computation (Jones et al., 1985; Glück & Jørgensen, 1997). Most of the work along this line attempts to stage programs automatically (e.g., by performing b... |

2 |
Implementing Typeful Program Transformations. Pages 20–28 of: Proceedings of acm sigplan workshop on partial evaluation and semantics based program manipulation
- Chen, Xi
- 2003
(Show Context)
Citation Context ...tion. For instance, the function comp defined in Figure 5 is just such an example. Also, a typeful implementation of a continuation-passing style translation on simply typed programs can be found in (=-=Chen & Xi, 2003-=-). In a study on typing dynamic typing (Baars & Swierstra, 2002), an example is given to demonstrate how a compilation function can be implemented in Haskell in a typeful manner that translates typele... |

2 | F.: Meta-Programming with Names and Necessity, To appear in JFP. A previous version appeared - Nanevski, Pfenning |

1 |
Partial evaluation of computation process
- Futamara
- 1971
(Show Context)
Citation Context ...iew of the techniques for program generation, partial evaluation and run-time code generation, has been studied extensively in the literature. An early reference to partial evaluation can be found in =-=[13]-=-, where the three Futamura projections are presented for generating compilers from interpreters. The notion of generating extensions, which Figure 8. A meta-programming example: inner product is now o... |

1 |
Meta-Programming with Names and Necessity. A previous version appeared
- Nanevski, Pfenning
(Show Context)
Citation Context ...e % is a shorthand for ˆLift, that is, %(e) represents ˆ(Lift(e)) for any expression e. Note that Lift can also be defined as ‘%, that is, Lift(e) can be treated as ‘(%e) for any expression e. 4 In ν =-=[21]-=-, e0 cannot be typed, either. However, e0 can be typed in the current implementation of MetaML [29] and MetaOCaml [30]; in the former e0 evaluates to 1 (which we suspect may be caused by an implementa... |

1 |
Available at http://www.cse.ogi.edu/PacSoft/projects/metaml
- MetaML
(Show Context)
Citation Context ...ift can also be defined as ‘%, that is, Lift(e) can be treated as ‘(%e) for any expression e. 4 In ν [21], e0 cannot be typed, either. However, e0 can be typed in the current implementation of MetaML =-=[29]-=- and MetaOCaml [30]; in the former e0 evaluates to 1 (which we suspect may be caused by an implementation error) but in the latter the evaluation of e0 raises a run-time exception caused by free varia... |

1 |
Available at http://www.cs.rice.edu/˜taha/MetaOCaml
- MetaOCaml
(Show Context)
Citation Context ...ined as ‘%, that is, Lift(e) can be treated as ‘(%e) for any expression e. 4 In ν [21], e0 cannot be typed, either. However, e0 can be typed in the current implementation of MetaML [29] and MetaOCaml =-=[30]-=-; in the former e0 evaluates to 1 (which we suspect may be caused by an implementation error) but in the latter the evaluation of e0 raises a run-time exception caused by free variable evaluation.stra... |

1 |
Typing Dynamic Typing. Pages 157–166 of
- Baars, Swierstra
- 2002
(Show Context)
Citation Context ... 5 is just such an example. Also, a typeful implementation of a continuation-passing style translation on simply typed programs can be found in (Chen & Xi, 2003). In a study on typing dynamic typing (=-=Baars & Swierstra, 2002-=-), an example is given to demonstrate how a compilation function can be implemented in Haskell in a typeful manner that translates typeless first-order abstract syntax trees (representing simply typed... |

1 |
Distributed meta-programming. Available at http://www.cs.bu.edu/~hwxi/academic/drafts/DMP.ps
- Chen, Shi, et al.
- 2004
(Show Context)
Citation Context ...de values (e.g., natural numbers) are particularly abundant in Haskell and are often referred to as pearls in functional programming. For instance, some of such examples can be found in (Hinze, 2001; =-=Chen et al., 2004-=-b). However, we feel that the use of env allows for a cleaner presentation of λcode. We use Θ for finite mappings defined below and dom(Θ) for the domain of Θ. Θ ::= [] | Θ[a ↦→ s] Note that [] stands... |

1 |
Environment Classifiers. Pages 26–37 of
- Taha, Nielsen
- 2003
(Show Context)
Citation Context ...s there cannot assign f(‘x) a “closed code” type. Though this is a highly contrived example, it nonetheless indicates some inadequacy in the notion of closed types captured by these type systems. In (=-=Taha & Nielsen, 2003-=-), there is another type system that aims at assigning more accurate types to meta-programs in MetaML. In the type system, a notion of environment classifiers is introduced. Generally speaking, enviro... |