Advanced Functional Programming

In his paper Embedding an Interpreted Language Using Higher-Order Functions and Types, Ramsey presents an API to interface applications, written in the host language, from within embedded interpreters. While the general principles and advantages of embedding an interpreted language are not new, his contribution is the design of an API suitable for interpreters embedded in an ML-like host language that takes advantage of higher-order functions and static typing. The main novelty of Ramsey's approach is the use of higher-order functions to (semi-) automatically generate much of the " glue code " that mediates between embedded and host language. The first section reviews the motivation for using embedded, interpreted languages, and contains a discussion of existing work and its shortcomings: Script-ing languages are a useful tool to customize complex applications; to obtain a reusable scripting language its interpreter should be embedded into the host language. This enables applications to take advantage of scripts, for instance by interpreting configuration files. The benefit of the embedding approach is that lexing, parsing and interpretation of scripts need not concern the application developer. The drawback is that glue code must be provided to facilitate interaction between application and embedded interpreter, taking into account the different representations of values and different calling conventions. Previous embedded implementations of various scripting languages were available only for C. The paper addresses this problem by demonstrating how to design an API to embed into a statically typed host language with higher-order functions. The concrete presentation is in terms of Objective Caml and Lua (" Lua-ML "), but Section 2 gives an indication of both the general mechanisms as well as aspects that are peculiar to Lua-ML. Section 3 contains the key contribution of the paper: A side-effect of replacing C as host language is that Objective Caml's types and higher-order functions can simplify the generation of most glue code. Embedding-projection pairs (e-p pairs) are used to translate between the representation of values in host and embedded language. An immediate benefit is that specific programming conventions of either language are explicated in a single, well-defined, point of the program. Moreover, the proposed API provides support to lift e-p pairs from base types to data types and higher types, in a type-directed way. The treatment of functions is where the languages differ most: Caml has curried higher order functions; Lua has uncurried functions, but with an arbitrary number of arguments and results. …