## Finally Tagless, Partially Evaluated -- Tagless Staged Interpreters for Simpler Typed Languages

Citations: | 32 - 7 self |

### BibTeX

@MISC{Carette_finallytagless,,

author = {Jacques Carette and Oleg Kiselyov and Chung-chieh Shan},

title = {Finally Tagless, Partially Evaluated -- Tagless Staged Interpreters for Simpler Typed Languages},

year = {}

}

### Years of Citing Articles

### OpenURL

### Abstract

We have built the first family of tagless interpretations for a higher-order typed object language in a typed metalanguage (Haskell or ML) that require no dependent types, generalized algebraic data types, or postprocessing to eliminate tags. The statically type-preserving interpretations include an evaluator, a compiler (or staged evaluator), a partial evaluator, and call-by-name and call-by-value CPS transformers. Our main idea is to encode HOAS using cogen functions rather than data constructors. In other words, we represent object terms not in an initial algebra but using the coalgebraic structure of the λ-calculus. Our representation also simulates inductive maps from types to types, which are required for typed partial evaluation and CPS transformations. Our encoding of an object term abstracts over the various ways to interpret it, yet statically assures that the interpreters never get stuck. To achieve self-interpretation and show Jones-optimality, we relate this exemplar of higher-rank and higher-kind polymorphism to plugging a term into a context of let-polymorphic bindings.

### Citations

652 |
Partial Evaluation and Automatic Program Generation
- Jones, Gomard, et al.
- 1993
(Show Context)
Citation Context ...In the accompanying source code is a conservative alternative P.fix that unfolds recursion only once, then residualizes. Many sophisticated approaches have been developed to decide how much to unfold =-=[17]-=-, but this issue is orthogonal to our presentation. Given this implementation of P, our running example let module E = EX(P) in E.test1 () evaluates to {P.st = Some true; P.dy = .<true>.} of type (’a,... |

309 |
Higher-order abstract syntax
- Pfenning, Elliott
- 1988
(Show Context)
Citation Context ...thout their polymorphic lift but with more constants so as to more conveniently express Fibonacci, factorial, and power. In contrast to §1, we encode binding using higher-order abstract syntax (HOAS) =-=[20, 25]-=- rather than de Bruijn indices. This makes the encoding convenient and ensures that our object programs are closed. 2.1 How to make encoding flexible: abstract the interpreter We embed our language in... |

305 | De interpreters for higherorder programming languages
- Reynolds
- 1972
(Show Context)
Citation Context ...ntactic type structure. Ideally, such a treatment should be metacircular, in the sense that the type structure used in the defined language should be adequate for the defining language. John Reynolds =-=[28]-=- 1 Introduction A popular way to define and implement a language is to embed it in another [28]. Embedding means to represent terms and values of the object language as terms and values in the metalan... |

267 | Compiling polymorphism using intensional type analysis
- Harper, Morrisett
- 1995
(Show Context)
Citation Context ...ive types compels them to include the polymorphic lift that we avoid in §4.1. Our encoding of the type function repr_pe in §4.3 emulates type-indexed types and is related to intensional type analysis =-=[13, 14]-=-. However, our object language and running examples in HOAS include fix, which intensional type analysis cannot handle [37]. Our final approach seems related to Washburn and Weirich’s approach to HOAS... |

208 | Type-directed partial evaluation
- Danvy
(Show Context)
Citation Context ...id values of ’sv and ’dv), and between static and dynamic terms. It is customary to implement such a bijection using an injection-projection pair, as done for interpreters [4, 27], partial evaluation =-=[7]-=-, and type-level functions [22]. As explained in §4.3, we avoid injection and projection at the type level by adding an argument to repr. Our solution could have been even more straightforward if Meta... |

194 | A modal analysis of staged computation
- Davies, Pfenning
- 2001
(Show Context)
Citation Context ...pes [33]. In fact, this problem motivated much work on GADTs [24, 37] and on dependent types [11, 23]. Yet other type systems have been proposed to distinguish closed terms like test1 from open terms =-=[9, 21, 34]-=-, so that lookup never receives an empty environment. We discuss these proposals further in §5. 1.2 Our final proposal We represent object programs using ordinary functions rather than data constructo... |

161 | Simple unification-based type inference for GADTs
- Jones, Simon, et al.
- 2006
(Show Context)
Citation Context ...s known statically,snever what type it has. In other words, our partial evaluator tags phases (with Some and None) but not object types. 5 Related work Our initial motivation came from several papers =-=[23, 24, 33, 37]-=- that use embedded interpreters to justify advanced type systems, in particular GADTs. We admire all this technical machinery, but these motivating examples do not need it. Although GADTs may indeed b... |

146 | Building domain-specific embedded languages
- Hudak
- 1996
(Show Context)
Citation Context ...nguage as terms and values in the metalanguage. Embedding is especially appropriate for domainspecific object languages because it supports rapid prototyping and integration with the host environment =-=[16]-=-. If the metalanguage supports staging, then the embedding can compile object programs to the metalanguage and avoid the overhead of interpreting them on the fly [23]. A staged definitional interprete... |

133 | Guarded recursive datatype constructors
- Xi, Chen, et al.
- 2003
(Show Context)
Citation Context ...ct language in Figure 1 throughout this paper. We aim not just for evaluation of object programs but also for compilation, partial evaluation, and other processing. Paˇsalić et al. [23] and Xi et al. =-=[37]-=- motivated interpreting a typed object language in a typed metalanguage as an interesting problem. The common solutions to this problem store object terms and values in the metalanguage in a universal... |

58 | Typing dynamic typing
- Baars, Swierstra
- 2002
(Show Context)
Citation Context ... proposal and contributions. We leave aside the solved problem of writing a parser/type-checker, for embedding object language objects into the metalanguage (whether using dependent types [23] or not =-=[2]-=-), and just enter them by hand. 1.1 The tag problem It is straightforward to create an algebraic data type, say in OCaml, Fig. 2(a), to represent object terms such as those in Figure 1. For brevity, w... |

55 | Tagless staged interpreters for typed languages
- Pašalić, Taha, et al.
- 2002
(Show Context)
Citation Context ...gration with the host environment [16]. If the metalanguage supports staging, then the embedding can compile object programs to the metalanguage and avoid the overhead of interpreting them on the fly =-=[23]-=-. A staged definitional interpreter is thus a promising way to build a domain-specific language (DSL). ⋆ We thank Martin Sulzmann and Walid Taha for helpful discussions. Sam Staton, Pieter Hofstra, an... |

49 | A logic programming approach to manipulating formulas and programs
- Miller, Nadathur
- 1987
(Show Context)
Citation Context ...thout their polymorphic lift but with more constants so as to more conveniently express Fibonacci, factorial, and power. In contrast to §1, we encode binding using higher-order abstract syntax (HOAS) =-=[20, 25]-=- rather than de Bruijn indices. This makes the encoding convenient and ensures that our object programs are closed. 2.1 How to make encoding flexible: abstract the interpreter We embed our language in... |

44 |
Environment classifiers
- Taha, Nielsen
- 2003
(Show Context)
Citation Context ...pes [33]. In fact, this problem motivated much work on GADTs [24, 37] and on dependent types [11, 23]. Yet other type systems have been proposed to distinguish closed terms like test1 from open terms =-=[9, 21, 34]-=-, so that lookup never receives an empty environment. We discuss these proposals further in §5. 1.2 Our final proposal We represent object programs using ordinary functions rather than data constructo... |

42 | Encoding types in ML-Like languages
- Yang
(Show Context)
Citation Context ...nguage requires either advanced types or tagging overhead; (3) Thus, an indirect interpreter is necessary, which needs a universal type and hence tagging. While the logic is sound, we (following Yang =-=[38]-=-) showed that the first step’s premise is not valid. Danvy and López [8] discuss Jones optimality at length and apply HOAS to typed self-interpretation. However, their source language is untyped. Ther... |

37 | Boxes go bananas: Encoding higherorder abstract syntax with parametric polymorphism
- Washburn, Weirich
(Show Context)
Citation Context ...ning examples in HOAS include fix, which intensional type analysis cannot handle [37]. Our final approach seems related to Washburn and Weirich’s approach to HOAS using catamorphisms and anamorphisms =-=[36]-=-. We could not find work that establishes that the typed λ-calculus has a final coalgebra structure. (See Honsell and Lenisa [15] for the untyped case.) We observe that higher-rank and higher-kind pol... |

36 | Typed cross-module compilation
- Shao
- 1998
(Show Context)
Citation Context ...gher-rank and higher-kind polymorphism lets us typecheck and compile object terms separately from interpreters. This is consistent with the role of polymorphism in the separate compilation of modules =-=[30]-=-. 6 Conclusions We solve the problem of embedding a typed object language in a typed metalanguage without using GADTs, dependent types, or a universal type. Our family of interpreters include an evalu... |

31 |
Extensional normalisation and type-directed partial evaluation for typed lambda calculus with sums
- Balat, Cosmo, et al.
- 2004
(Show Context)
Citation Context ...erpreter appears to be tagless, but they could not have implemented a compiler or partial evaluator in the same way, since they rely heavily on injection-projection pairs. Fiore [10] and Balat et al. =-=[3]-=- also build a tagless partial evaluator, using delimited control operators. It is type-directed, so the user must represent, as a term, the type of every term to be partially evaluated. We shift this ... |

30 | Concoqtion: Indexed types now
- Fogarty, Pašalić, et al.
- 2007
(Show Context)
Citation Context ...ect language in a typed metalanguage while preserving types is difficult and requires GADTs or dependent types [33]. In fact, this problem motivated much work on GADTs [24, 37] and on dependent types =-=[11, 23]-=-. Yet other type systems have been proposed to distinguish closed terms like test1 from open terms [9, 21, 34], so that lookup never receives an empty environment. We discuss these proposals further i... |

28 | ML-like inference for classifiers
- Calcagno, Moggi, et al.
- 2004
(Show Context)
Citation Context ...ur object language into OCaml code using MetaOCaml’s staging facilities. MetaOCaml represents future-stage expressions of type t as values of type (’c, t) code, where ’c is the environment classifier =-=[6, 34]-=-. Code values are created by a bracket form .<e>., which quotes the expression e for evaluation at a future stage. The escape .~e must occur within a bracket and specifies that the expression e must b... |

28 | Cogen in six lines
- Thiemann
- 1996
(Show Context)
Citation Context ...ual stages [31]: our partial evaluator P reuses our taglesssevaluator R and tagless compiler C, so it is patent that the output of P never gets stuck. It would be interesting to try to derive a cogen =-=[35]-=- in the same manner. It is common to implement an embedded DSL by providing multiple interpretations of host-language pervasives such as addition and application. It is also common to use phantom type... |

26 | TypeCase: A design pattern for type-indexed functions
- Oliveira, Gibbons
- 2005
(Show Context)
Citation Context ...epr) option * (’c,’a->’b) C.reprsunlike P1.repr of int or bool. That is, we need a nonparametric data type, something akin to type-indexed functions and type-indexed types, which Oliveira and Gibbons =-=[22]-=- dub the typecase design pattern. Thus, typed partial evaluation, like typed CPS transformation, inductively defines a map from source types to target types that performs case distinction on the sourc... |

22 | Tag elimination and Jones-optimality
- Taha, Makholm, et al.
- 2001
(Show Context)
Citation Context ...gram is well-typed. This failure necessitates tagging and nonexhaustive pattern-matching operations that incur a performance penalty in interpretation [23] and impair optimality in partial evaluation =-=[33]-=-. In short, the universal-type solution is unsatisfactory because it does not preserve typing. It is commonly thought that to interpret a typed object language in a typed metalanguage while preserving... |

21 | Two for the price of one: composing partial evaluation and compilation
- Sperber, Thiemann
- 1997
(Show Context)
Citation Context ... offline partial evaluation. They predate us in deforesting the object term representation to enable tagless partial evaluation. We strive for modularity by reusing interpreters for individual stages =-=[31]-=-: our partial evaluator P reuses our taglesssevaluator R and tagless compiler C, so it is patent that the output of P never gets stuck. It would be interesting to try to derive a cogen [35] in the sam... |

16 |
Semantic analysis of normalisation by evaluation for typed lambda calculus
- Fiore
(Show Context)
Citation Context ...ct language. Their interpreter appears to be tagless, but they could not have implemented a compiler or partial evaluator in the same way, since they rely heavily on injection-projection pairs. Fiore =-=[10]-=- and Balat et al. [3] also build a tagless partial evaluator, using delimited control operators. It is type-directed, so the user must represent, as a term, the type of every term to be partially eval... |

16 | A hybrid approach to online and offline partial evaluation
- Sumii, Kobayashi
- 2001
(Show Context)
Citation Context ...etation compatible with the above. Self-interpretation turned out to be harder than expected. 6 Our code is surprisingly simple and obvious in hindsight, but it has been cited as a difficult problem (=-=[32]-=- notwithstanding) to interpret a typed object language in a typed metalanguage without tagging or type-system extensions. For example, Taha et al. [33] say that “expressing such an interpreter in a st... |

11 | Staged computation with names and necessity
- Nanevski, Pfenning
(Show Context)
Citation Context ...pes [33]. In fact, this problem motivated much work on GADTs [24, 37] and on dependent types [11, 23]. Yet other type systems have been proposed to distinguish closed terms like test1 from open terms =-=[9, 21, 34]-=-, so that lookup never receives an empty environment. We discuss these proposals further in §5. 1.2 Our final proposal We represent object programs using ordinary functions rather than data constructo... |

7 |
optimality, binding-time improvements, and the strength of program specializers
- Jones
- 2002
(Show Context)
Citation Context ...run-time error, and is in fact total. Because this safety is obvious not just to us but also to the metalanguage implementation, we avoid the serious performance penalty [23] of error checking. Glück =-=[12]-=- explains deeper technical reasons that inevitably lead to these performance penalties. Our solution is not Church-encoding the universal type. The Church encoding of the type u in §1.1 requires two c... |

7 | ML Module Mania: A Type-Safe Separately Compiled, Extensible Interpreter
- Ramsey
- 2005
(Show Context)
Citation Context ...c and dynamic types (the valid values of ’sv and ’dv), and between static and dynamic terms. It is customary to implement such a bijection using an injection-projection pair, as done for interpreters =-=[4, 27]-=-, partial evaluation [7], and type-level functions [22]. As explained in §4.3, we avoid injection and projection at the type level by adding an argument to repr. Our solution could have been even more... |

5 | Self-interpretation and reflection in a statically typed language
- Läufer, Odersky
- 1993
(Show Context)
Citation Context ...hereas our proofs are trivial. Rhiger’s approach does not support typed CPS transformation (his §3.3.4). We are not the first to implement a typed interpreter for a typed language. Läufer and Odersky =-=[18]-=- use type classes to implement a metacircular interpreter (rather than a self-interpreter) of a typed version of the SK language, which is quite different from our object language. Their interpreter a... |

4 |
Metacircularity in the polymorphic λ-calculus
- Pfenning, Lee
- 1991
(Show Context)
Citation Context ...ding term-level type representations, our approach makes it easier to perform algebraic simplifications (as in §4.3). We encode terms in elimination form, as a coalgebraic structure. Pfenning and Lee =-=[26]-=- first described this basic idea and applied it to metacircular interpretation. Our approach, however, can be implemented in mainstream ML and supports type inference, typed CPS transformation and par... |

3 | Coinductive Characterizations on Applicative Structures
- Honsell
(Show Context)
Citation Context ...rn and Weirich’s approach to HOAS using catamorphisms and anamorphisms [36]. We could not find work that establishes that the typed λ-calculus has a final coalgebra structure. (See Honsell and Lenisa =-=[15]-=- for the untyped case.) We observe that higher-rank and higher-kind polymorphism lets us typecheck and compile object terms separately from interpreters. This is consistent with the role of polymorphi... |

3 | Higher-Order Program Generation
- Rhiger
- 2001
(Show Context)
Citation Context ...ing multiple interpretations of host-language pervasives such as addition and application. It is also common to use phantom types to rule out ill-typed object terms, as done in Lava [5] and by Rhiger =-=[29]-=-. However, these approaches are not tagless because they still use universal types, such as Lava’s Bit and NumSig, and Rhiger’s Raw (his Fig. 2.2) and Term (his Chap. 3), which incur the attendant ove... |

1 |
Type-indexed data types. Sci. Comput. Program
- Hinze, Jeuring, et al.
- 2004
(Show Context)
Citation Context ...ive types compels them to include the polymorphic lift that we avoid in §4.1. Our encoding of the type function repr_pe in §4.3 emulates type-indexed types and is related to intensional type analysis =-=[13, 14]-=-. However, our object language and running examples in HOAS include fix, which intensional type analysis cannot handle [37]. Our final approach seems related to Washburn and Weirich’s approach to HOAS... |