## From Interpreter to Compiler using Staging and Monads (1998)

Citations: | 3 - 0 self |

### BibTeX

@MISC{Sheard98frominterpreter,

author = {Tim Sheard and Zine-el-abidine Benaissa},

title = {From Interpreter to Compiler using Staging and Monads},

year = {1998}

}

### OpenURL

### Abstract

In writing this paper we had two goals. First, to promote MetaML, a programming language for writing staged programs, and second, to demonstrate that staging a program can have significant benefits. We do this by example: the derivation of an executable compiler for a small language. We derive the compiler in a rigorous fashion from a semantic description of the language. This is done by staging a denotational semantics, expressed as a monadic interpreter. The compiler is a program generator, taking a program in the source language "a while-program" as input and producing an ML program as target. The ML program produced is in a restricted subset of ML over which the programmer has complete control. It is encapsulated in a special data-structure called code. The meta-programming capabilities of MetaML allow this data-structure to be directly executed "run-time code generation", or to be analysed. We illustrate this analysis of generated code to build a source to source transf...

### Citations

1307 | Lectures on constructive functional programming
- Bird
- 1989
(Show Context)
Citation Context ... new piece of code <1 %+ 5>. Running a piece of code, strips away the enclosing brackets, and evaluates the expression inside. 3 Monads in MetaML We assume the reader has a working knowledge of monads=-=[30, 33]-=-. We use the unit and bind formulation of monads[32]. In MetaML a monad is a data structure encapsulating atype constructor M and the unit and bind functions. datatype ('M : * -> * ) Monad = Mon of ([... |

731 | Notions of Computation and Monads
- Moggi
- 1991
(Show Context)
Citation Context ...ork Our work was inspired by work in many di erent areas. Derivation of compilers from speci cations and the use of action-semantics [19,23,11, 22]; the use of monads to structure programs in general =-=[18, 31, 26]-=- and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; partial evaluation [34, 17, 1, 3,2, 9]; higher order abstract synt... |

638 |
Partial evaluation and automatic program generation
- Jones, Gomard, et al.
- 1993
(Show Context)
Citation Context ...ocations appear as constants in the next stage. Splitting the environment into two components is a standard technique (often called a binding time improvement) used by the partial evaluation community=-=[9]-=-. We capture this precisely by the following purely functional implementation. type location = int; type index = string list; type stack = int list; (* position : string -> index -> location *) fun po... |

458 | Comprehending Monads
- Wadler
- 1990
(Show Context)
Citation Context ... new piece of code <1 %+ 5>. Running a piece of code, strips away the enclosing brackets, and evaluates the expression inside. 3 Monads in MetaML We assume the reader has a working knowledge of monads=-=[30, 33]-=-. We use the unit and bind formulation of monads[32]. In MetaML a monad is a data structure encapsulating atype constructor M and the unit and bind functions. datatype ('M : * -> * ) Monad = Mon of ([... |

287 | A logic programming language with lambda-abstraction, function variables, and simple unification
- Miller
- 1991
(Show Context)
Citation Context ... only in the function position of escaped applications, and the arguments of these escaped applications may only be bracketed bound variables (like <x>), pattern-matching and uni cation are decidable =-=[16, 25]-=-. We now possess the tools to present the monad-law and code-optimizing MetaMLfunction opt: fun opt < Do ~st {x <- ~v ; return ~st' x } > = opt v | opt w as < Do ~st {x <- Return ~st' ~e ; ~(z <x>) > ... |

233 | Monad Transformers and Modular Interpreters
- Liang, Hudak, et al.
- 1995
(Show Context)
Citation Context ...f expressed in a monadic style, an interpreter can be mapped closer to the underlying system, and the structuring properties of the monad even allow the interpreter to be reused as the system evolves =-=[32, 14, 27]-=-. Nevertheless, the e ort used to build the interpreter is often considered wasteful since the programmer still needs to re-implement the compiler from scratch after building the interpreter. Our solu... |

185 | A modal analysis of staged computation
- Davies, Pfenning
(Show Context)
Citation Context ...eci cations and the use of action-semantics [19,23,11, 22]; the use of monads to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming =-=[5, 6]-=- and its use in structuring compilers [29, 20, 4]; partial evaluation [34, 17, 1, 3,2, 9]; higher order abstract syntax and pattern matching [16, 7] For space considerations we limit detailed discussi... |

178 | A system of constructor classes: overloading and implicit higher-order polymorphism
- Jones
- 1993
(Show Context)
Citation Context ...SML's post x notation for type application, and two non-standard extensions to ML. First, it declares that the argument ('M : * -> * ) of the type constructor Monad is itself a unary type constructor =-=[8]-=-. We say that 'M has kind: * -> *. Second, it declares that the arguments to the constructor Mon must be polymorphic functions [21]. The type variables in brackets, e.g. ['a,'b], are universally quant... |

95 | Putting type annotations to work
- Odersky, Laufer
- 1996
(Show Context)
Citation Context ... * ) of the type constructor Monad is itself a unary type constructor [8]. We say that 'M has kind: * -> *. Second, it declares that the arguments to the constructor Mon must be polymorphic functions =-=[21]-=-. The type variables in brackets, e.g. ['a,'b], are universally quanti ed. Because of the explicit type annotations in the datatype de nitions the e ect of these extensions on the Hindley-Milner type ... |

91 | Topics in online partial evaluation
- Ruf
- 1993
(Show Context)
Citation Context ...s to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; partial evaluation =-=[34, 17, 1, 3,2, 9]-=-; higher order abstract syntax and pattern matching [16, 7] For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compilation Perhaps, the most related work is t... |

80 | A temporal-logic approach to binding-time analysis
- Davies
- 1996
(Show Context)
Citation Context ...eci cations and the use of action-semantics [19,23,11, 22]; the use of monads to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming =-=[5, 6]-=- and its use in structuring compilers [29, 20, 4]; partial evaluation [34, 17, 1, 3,2, 9]; higher order abstract syntax and pattern matching [16, 7] For space considerations we limit detailed discussi... |

70 | A tour of Schism: a partial evaluation system for higher-order applicative languages
- Consel
- 1993
(Show Context)
Citation Context ...s to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; partial evaluation =-=[34, 17, 1, 3,2, 9]-=-; higher order abstract syntax and pattern matching [16, 7] For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compilation Perhaps, the most related work is t... |

56 | Modular Denotational Semantics for Compiler Construction
- Liang, Hudak
- 1996
(Show Context)
Citation Context ...7] For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compilation Perhaps, the most related work is the work of Sheng Liang and his thesis advisor Paul Hudak =-=[12, 13]-=-. They investigate the derivation of a compiler from a modular monadic interpreter. Our work is a continuation of their e ort of using monads as a standard compilation mechanism. However, some di eren... |

53 |
The essence of functional programming (invited talk
- Wadler
- 1992
(Show Context)
Citation Context ...f expressed in a monadic style, an interpreter can be mapped closer to the underlying system, and the structuring properties of the monad even allow the interpreter to be reused as the system evolves =-=[32, 14, 27]-=-. Nevertheless, the e ort used to build the interpreter is often considered wasteful since the programmer still needs to re-implement the compiler from scratch after building the interpreter. Our solu... |

42 |
Calculating compilers
- Meijer
- 1992
(Show Context)
Citation Context ...phisms through the transformer. This is hard and not completely understood. We may try to duplicate Liang's approach in future work. 14s5.2 Staging and compilation In his thesis Calculating Compilers =-=[15]-=- Erik Meijer advocates staging a compiler by using self discipline. Construct a compiler by building it as the composition of compile-time and run-time components. A critical step in this process is n... |

41 |
A functional theory of exceptions
- Spivey
- 1990
(Show Context)
Citation Context ...ork Our work was inspired by work in many di erent areas. Derivation of compilers from speci cations and the use of action-semantics [19,23,11, 22]; the use of monads to structure programs in general =-=[18, 31, 26]-=- and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; partial evaluation [34, 17, 1, 3,2, 9]; higher order abstract synt... |

40 |
Two-Level Functional Languages
- Nielson, Nielson
- 1992
(Show Context)
Citation Context ...[19,23,11, 22]; the use of monads to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers =-=[29, 20, 4]-=-; partial evaluation [34, 17, 1, 3,2, 9]; higher order abstract syntax and pattern matching [16, 7] For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compila... |

40 | Report on the programming language haskell: A non-strict, purely functional language
- Fasel, Hammond, et al.
- 1992
(Show Context)
Citation Context ...tactic forms which are aware of the Monad datatype: Do and Return. Do and Return are MetaML's syntactic interface to the unit and bind of a monad. We have modeled them after the do-notation of Haskell=-=[10, 24]-=-. An important di erence is that MetaML's Do and Return are both parameterized by an expression of type 'M Monad. Users may freely construct their own monads, though they should be very careful that t... |

34 | Multi-stage programming: Axiomatization and type safety (extended abstract
- Taha, Benaissa, et al.
- 1998
(Show Context)
Citation Context ...curs because both are free variables and are lexically captured. Now we can state the equivalence relationship between the monadic eval1 and the staged eval2. We use the axiomatic semantics of MetaML =-=[28]-=-, in particular the axioms for the annotations, such as the splice axiom above. Proposition 1. For all expressions exp, and list of names index: eval1 exp index = run (eval2 exp index) Proof. We might... |

31 | Building Interpreters by Composing Monads
- Steele
- 1994
(Show Context)
Citation Context ...f expressed in a monadic style, an interpreter can be mapped closer to the underlying system, and the structuring properties of the monad even allow the interpreter to be reused as the system evolves =-=[32, 14, 27]-=-. Nevertheless, the e ort used to build the interpreter is often considered wasteful since the programmer still needs to re-implement the compiler from scratch after building the interpreter. Our solu... |

26 | Provably Correct Compiler Generation
- Palsberg
- 1992
(Show Context)
Citation Context ...ssembly code, could be implemented in the same fashion. 5 Related work Our work was inspired by work in many di erent areas. Derivation of compilers from speci cations and the use of action-semantics =-=[19,23,11, 22]-=-; the use of monads to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; p... |

18 |
Compiling actions by partial evaluation
- Bondorf, Palsberg
- 1993
(Show Context)
Citation Context ...bly language). Because the programmer has complete control over the structure of the residual program this can be a trivial task. Staging of interpreters using partial evaluation has been done before =-=[2, 4]-=-. The contribution of this paper is to show that this can all be done in a single program. A system incorporating staging as a rst class feature of a language is a powerful tool. While using such a to... |

17 |
Linear unification of higher-order patterns
- Qian
- 1993
(Show Context)
Citation Context ...only in the function position of escaped applications, and the arguments of these escaped applications may only be bracketed bound variables (like !x?), pattern-matching and unification are decidable =-=[16, 25]-=-. We now possess the tools to present the monad-law and code-optimizing MetaMLfunction opt: fun opt ! Do ~st --x !- ~v ; return ~st' xs? = opt v --- opt w as ! Do ~st --x !- Return ~st' ~e ; ~(z !x?) ... |

16 |
Realistic Compiler Generation. Foundations of Computer Science
- Lee
- 1989
(Show Context)
Citation Context ...ssembly code, could be implemented in the same fashion. 5 Related work Our work was inspired by work in many di erent areas. Derivation of compilers from speci cations and the use of action-semantics =-=[19,23,11, 22]-=-; the use of monads to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; p... |

11 |
Report on the programming language Haskell
- Jones, Hughes
(Show Context)
Citation Context ...tactic forms which are aware of the Monad datatype: Do and Return. Do and Return are MetaML's syntactic interface to the unit and bind of a monad. We have modeled them after the do-notation of Haskell=-=[10, 24]-=-. An important di erence is that MetaML's Do and Return are both parameterized by an expression of type 'M Monad. Users may freely construct their own monads, though they should be very careful that t... |

10 | Compiling monads
- Danvy, Koslowski, et al.
(Show Context)
Citation Context ...bly language). Because the programmer has complete control over the structure of the residual program this can be a trivial task. Staging of interpreters using partial evaluation has been done before =-=[2, 4]-=-. The contribution of this paper is to show that this can all be done in a single program. A system incorporating staging as a rst class feature of a language is a powerful tool. While using such a to... |

6 |
SIS-semantics implementation system, reference manual and user guide
- Mosses
- 1979
(Show Context)
Citation Context ...ssembly code, could be implemented in the same fashion. 5 Related work Our work was inspired by work in many di erent areas. Derivation of compilers from speci cations and the use of action-semantics =-=[19,23,11, 22]-=-; the use of monads to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; p... |

2 |
Methods and Tools for Compiler Construction
- Paulson
- 1984
(Show Context)
Citation Context |

2 |
Linear uni cation of higher-order patterns
- Qian
- 1993
(Show Context)
Citation Context ... only in the function position of escaped applications, and the arguments of these escaped applications may only be bracketed bound variables (like <x>), pattern-matching and uni cation are decidable =-=[16, 25]-=-. We now possess the tools to present the monad-law and code-optimizing MetaMLfunction opt: fun opt < Do ~st {x <- ~v ; return ~st' x } > = opt v | opt w as < Do ~st {x <- Return ~st' ~e ; ~(z <x>) > ... |

1 |
Automatic autoprojectin of recursive equations with global variables and data types
- Bondorf, Danvy
- 1991
(Show Context)
Citation Context ...s to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; partial evaluation =-=[34, 17, 1, 3,2, 9]-=-; higher order abstract syntax and pattern matching [16, 7] For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compilation Perhaps, the most related work is t... |

1 |
Sch urmann Frank Pfenning, Jo elle Despeyroux. Primitive recursion for higher-order abstract syntax
- Carsten
- 1997
(Show Context)
Citation Context ...tions in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers [29, 20, 4]; partial evaluation [34, 17, 1, 3,2, 9]; higher order abstract syntax and pattern matching =-=[16, 7]-=- For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compilation Perhaps, the most related work is the work of Sheng Liang and his thesis advisor Paul Hudak [1... |

1 |
Modular Monadi Semantics and Compilation
- Liang
- 1998
(Show Context)
Citation Context ...7] For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compilation Perhaps, the most related work is the work of Sheng Liang and his thesis advisor Paul Hudak =-=[12, 13]-=-. They investigate the derivation of a compiler from a modular monadic interpreter. Our work is a continuation of their e ort of using monads as a standard compilation mechanism. However, some di eren... |

1 |
Self-applicable partial evaluation for the pure lambda calculus
- Mogenson
- 1992
(Show Context)
Citation Context |

1 |
Scherlis Urlik Jorring. Compilers and staging transformations
- William
- 1986
(Show Context)
Citation Context ...[19,23,11, 22]; the use of monads to structure programs in general [18, 31, 26] and language implementations in particular [32, 27, 14]; staged programming [5, 6] and its use in structuring compilers =-=[29, 20, 4]-=-; partial evaluation [34, 17, 1, 3,2, 9]; higher order abstract syntax and pattern matching [16, 7] For space considerations we limit detailed discussion to the following areas. 5.1 Monads and compila... |