A program that interprets untyped lambda terms (and more) ========================================================= Let's start with an OCaml datatype for untyped lambda terms: type identifier = string type term = | Var of identifier | App of term * term | Lambda of identifier * term Some examples of `term`s are: Var "x" App(Var "x", Var "y") App(Lambda("x",Var "x"), Var "y") We're going to want to design an _interpreter_ program that will reduce or evaluate the final term down to: Var "y" It would be nice if it also knew how to display that in the more user-friendly format: y And it would be doubly-specially nice if we didn't have to type our input in the long explicit OCaml notation, but could instead just write: (\x.x) y and behind the scenes that got converted into: App(Lambda("x",Var "x"), Var "y") which is what the functions of our interpreter program have to deal with. You're going to work with a program skeleton where these extra nice features are built in to the infrastructure. It will only be the core reduction or evaluation functions that you need to complete, to get us from: App(Lambda("x",Var "x"), Var "y") to: Var "y" As long as we're at it, why don't we make out language a bit more full-featured. We could throw in `let`-terms and `if-then`-terms, too: type term = | Var of identifier | App of term * term | Lambda of identifier * term | Let of identifier * term * term | LetRec of (identifier * term) list * term | If of term * term * term We're not actually going to implement `LetRec` in this exercise; that's just there as a placeholder for later. Perhaps also some boolean and number constants: type literal = Bool of bool | Num of int type term = ... | Literal of literal But then once we have those, we are going to want to have some primitive operations to deal with them. Otherwise they'll just be black boxes that we can only pass around, without ever inspecting or making differentiated use of. Thus we might want operations like `zero?` and `pred` for numbers, `not` for negation, and so on. Long story short, we are going to want some *primitive functions*, some *constructors* (like `cons` and `[]` were for lists), and some *deconstructors* or operations appropriate to those constructors (these were `head` and `tail` for lists). Thus our fleshed-out datatype might look like this: type term = | Var of identifier | App of term * term | Lambda of identifier * term | Let of identifier * term * term | LetRec of (identifier * term) list * term | If of term * term * term (* Constants, functional and otherwise *) | Primfun of primfun | Constructor of constructor | Destructor of destructor | Literal of literal The official term for operations like `head` and `tail` is "deconstructor", not "destructor", but the latter is shorter and more fun to type. In the exercises that follow, the tricky parts that deal with the `Primfun`s and `Constructor`s and `Destructor`s and `Literal`s will always already be supplied for you. You'll just need to think about the `Var`s and `App`s and `Lambda`s and `Let`s and `If`s. We're going to explore how to implement the interpreter using two different methods. One is a translation of the method Chris demonstrated for combinatorial logic over to the lambda calculus. You check if any part of a term is reducible, and keep repeating single-step reductions until you can't find any more eligible redexes. The second method works a bit differently. You can begin by thinking of it an improvement on the first method, that tries to get by without having to do all the laborious substitutions that reducing again and again would entail. But its virtues will be more than merely ones of efficiency. It will also start to look a lot more like the kinds of semantics, using assignment functions, that we're more familiar with as formal semanticists. In programming circles, assignment functions are called "environments" and we'll use that terminology here. So the two methods we're going to look at will be, first, a reduce-and-repeat interpreter; and second, an environment-based one. Now you could just work this out on paper. That's fine, and will save you some complications. But it will be more fun for you to play with an actual computer implementation. Plus then you will get the benefits of user-friendly input and user-friendly printing of the results. But these benefits come at a price. The actual code you're going to look at has some complexity to it. The skeleton program we'll be providing you with is based on source code accompanying Pierce's excellent book _Types and Programming Languages_. (In particular, the files here .) We aggressively modified that code to suit our somewhat different needs (and taste). Notably, Pierce's code only provides a reduce-and-repeat interpreter; so part of our refactoring was to make it easier to switch back and forth between that and an environment-based interpreter. Now part of the complexity of this can be factored out into extra files that you can just ignore, and we've tried to organize the code to maximize that. But sadly, some of the complexity inevitably ends up polluting our nice simple datatype. We're going to explain where this pollution comes from, in the hope that then you'll be able to see through it to the simple underlying form, that we displayed above. A **first source of complexity** is that we want to get friendlier error messages, for example if we run the interpreter on a file `./input` that contains: let x = 3 in x + y The interpreter will complain: ./input:1.17: Unbound variable `y` What this means is that while processing the file `./input`, after character 17 on line 1, something gave the interpreter indigestion, and its particular complaint is that it encountered an ``Unbound variable `y` ``. When the code you're feeding the interpreter is non-trivial, it's very helpful to get this kind of information about where in the file the error originated from. But to keep track of that, our datatype has to get messier. We'll have some extra information (we'll call it `info`, don't worry about its internal structure) associated with each term (and subterm) that we're handling. And so our datatypes will look instead like this: type bare_term = | Var of identifier ... type term = info * bare_term And when we pattern match on the (full, annotated) terms, our code will look like this: match term with | _,Var ident -> ... ... with the extra `_,` at the start of the pattern to catch and discard the extra information about where the term came from. In some cases we need to retain that information instead of discarding it, so the code will instead look like this: match term with | fi,Var ident -> ... ... using the variable `fi` for the "source file info". A **second complication** has to do with the distinction between `term`s in general and what we want to count as `result`s of our computations. In these exercises, unlike the combinatorial logic ones, we are only going to work with "call-by-value" interpreters. That is, we will only apply functions (whether primitives or those expressed by Lambdas) to what we deem as "value"s or "result"s. At a minimum, these must be terms that it is not possible to reduce further. So `1` and `\x. x` count as values. But we will also legislate that terms like `1 (\x. x)`, though non-reducible (our booleans and numbers won't be Church functions, but rather will be non-functional atoms), count as "stuck" and so aren't results either. (As suggested in class, you can think of "stuck" terms as representing or being crashed computations.) As a result of never applying functions to non-results, non-results will never get bound to any variables, either. Now in the V1/reduce-and-repeat part of the exercise, results will simply be a proper subset of our terms. They will be abstracts, plus literals and all the other primitives. They won't include free/unbound variables. We'll count those as "stuck" or crashed computations, too. (But _bound_ variables are okay, because they'll always be bound to results.) However, when we move to the V2/environment-based interpreter, we will need to introduce some results that aren't (just) terms, called `Closure`s. We'll discuss these later; but the upshot is that we're going to need eventually to work with code where the result datatype and the term datatype may be partly disjoint. So we are going to need two parallel datatypes: type bare_term = | TermVar of identifier | TermApp of term * term | TermLambda of identifier * term | TermLet of identifier * term * term | TermLetRec of (identifier * term) list * term | TermIf of term * term * term (* Constants, functional and otherwise *) | TermPrimfun of primfun | TermConstructor of constructor | TermDestructor of destructor | TermLiteral of literal type term = info * bare_term type bare_result = (*| Symbol of ... *) (*| Closure of ... *) | ResLambda of identifier * term | ResPrimfun of primfun | ResConstructor of constructor | ResDestructor of destructor | ResLiteral of literal type result = info * bare_result We'll explain the `Symbol` and `Closure` variants on the `bare_result` datatype below. Having these two parallel datatypes is rather annoying, and requires us to insert some translation functions `term_of_result` and `result_of_term` at a few places in the program. But the core, non-fancy parts of OCaml don't supply any more elegant way to specify that one datatype is a subtype of another, so this is simply what we'll need to do. A **third complication** has to do with environments. On the one hand, we don't have any really compelling need for environments in the first phase of the exercise, when we're just making a reduce-and-repeat interpreter. They don't play any role in the fundamental task we're focusing on. But on the other hand, weaving environments into the system when we *will* need them, for the second phase of the exercise, is not simple and would require lots of code changes. So that is a reason to include them from the beginning, just off to the side not doing any important work until we want them. And as long as we've got them at the party, we might just as well give them _something_ to do. Pierce's original code takes a source file, which it expects to contain a sequence of terms or other toplevel declarations separated by semicolons, and prints out the result of maximally reducing each of the terms. Note I said "or other toplevel declarations." In addition to terms like this: 1 + 2; let x = 1 in x + 2 that code also accepted toplevel declarations like this: let y = 1 + 2 end which means that we should reduce the rhs `1 + 2`, and *save* the result to be used for any locally-unbound occurrences of `y` in later terms. If the interpreter encounters *other* locally-unbound variables, where no such toplevel binding has been established, it will fail with an error. Thus: let y = 1 + 2 end; let x = 0 in (x,y) will evaluate to `(0,3)`, but: let y = 1 + 2 end; let x = 0 in (x,w) will crash, because `w` has no result bound to it. Note that you can locally rebind variables like `y` that have toplevel bindings. Thus: let y = 1 + 2 end; (y, (let y = 0 in y)) evaluates to `(3,0)`. (You have to enclose the `let y = 0 in y` in parentheses in order to embed it in terms like the pair construction `(_, _)`.) Note that what I'm writing above isn't the syntax parsed by Pierce's actual program. He uses a syntax that would be unfamiliar to you, so we've translated his code into a more local dialect; and the above examples are expressed using that dialect. Pierce's code had a second kind of toplevel declaration, too, which looks like this (in our dialect, not his): symbol w; let x = 0 in (x,w) Now that won't crash, but will instead evaluate to `(0,w)`. The code now treats unbound occurrences of `w` as atomic uninterpreted symbols, like Scheme's `'w`. And these symbols do count as results. (They are also available to be rebound; that is, you are allowed to say `symbol w; let w = 0 in ...`. But perhaps we should prohibit that.) So these two kinds of toplevel declarations are already handled by Pierce's code, in a form, and it seemed neatest just to clean them up to our taste and leave them in place. And environments are needed, even in the V1/reduce-and-repeat interpreter (which is all Pierce's code provides), to implement those toplevel declarations. But as I said before, you can for now just think of the environments as sitting in the sidelines. They aren't used in any way for interpreting terms like: let y = 1 + 2 in let x = y + 10 in (x, y) But now *what are* environments? In order to emphasize the essential commitments rather than any one particular implementation of environments, we're going to show you the general interface environments need to satisfy, and then show you a couple of different ways of implementing that interface. These will be just plug-and-play. You should be able to switch the implementations at will, and have everything work out the same. TODO selecting an environment Okay, that **completes our survey of the complications**. The V1/reducuction-based interpreter ------------------------------------ For phase 1 of this exercise, you're going to write a `reduce1` function that converts OCaml values like: TermApp(TermApp(TermLambda("x", TermApp(Var "x", Var "y")), TermLambda("w", Var "w")), ...) to: TermApp(TermApp(TermLambda("w", Var "w"), Var "y"), ...) The further reduction, to: TermApp(Var "y", ...) has to come from a subsequent re-invocation of the function. Before we get down to business, let's think about how we will detect that the term has been reduced as far as we can take it. In the reduce-and-repeat interpreter Chris demonstrated for combinatorial logic, we had the `reduce1` function perform a single reduction *if it could*, and then it was up to the caller to compare the result to the original term to see whether any reduction took place. That worked for the example we had. But it has some disadvantages. One is that it's inefficient. Another is that it's sensitive to the idiosyncracies of how your programming language handles equality comparisons on complex structures; and these details turn out to be very complex and vary from language to language (and even across different versions of different implementations of a single language). We'd be glad to discuss these subtleties offline, but if you're not prepared to master them, it would be smart to foster an ingrained hesitation to blindly applying a language's `=` operator to complex structures. (Some problem cases: large numbers, set structures, structures that contain functions.) A third difficulty is that it's sensitive to the particular combinators we took as basic. With `S` and `K` and `I`, it can never happen that a term has been reduced, but the output is identical to the input. That can happen in the lambda calculus, though (remember `ω ω`); and it can happen in combinatorial logic if other terms are chosen as primitive (`W W1 W2` reduces to `W1 W2 W2`, so let them all just be plain `W`s). So let's consider different strategies for how to detect that the term cannot be reduced any further. One possibility is to write a function that traverses the term ahead of time, and just reports whether it's already a result, without trying to perform any reductions itself. Another strategy is to "raise an exception" or error when we ask the `reduce1` function to reduce an irreducible term; then we can use OCaml's error-handling facilities to "catch" the error at an earlier point in our code and we'll know then that we're finished. Pierce's code used a mix of these two strategies. What we're going to do instead is to have our `reduce1` function wrap up its output in a special datatype, that describes how that output was reached. That type looks like this: type reduceOutcome = AlreadyResult of result | StuckAt of term | ReducedTo of term That is, one possible outcome is that the term supplied is already a result, and so can't be reduced further for that reason. In that case, we use the first variant. We could omit the `of result` parameter, since the code that called `reduce1` already knows what term it was trying to reduce; but in some cases it's a bit more convenient to have `reduce1` tell us explicitly what the result was, so we make that accompany the `AlreadyResult` tag. A second outcome is that the term is "stuck" and so for that reason also can't be reduced further. In this case, we want to supply the stuck subterm, and propagate that upward through the recursive calls to `reduce1`, so that the callers can see specifically what subterm it was that caused the trouble. Finally, there is also the possibility that we *were* able to reduce the term by one step, in which case we return `ReducedTo newly_reduced_term`. The structure of our `reduce1` function, then, will look like this: let rec reduce1 (term : term) (env : env) : reduceOutcome = match term with | _, AA -> TODO | _, BB -> We've supplied some of the guts of this function, especially the fiddly bits about how to deal with primitives and literals. But you need to fill in the gaps yourself to make it work. As mentioned before, don't worry about the environments. They're needed to see if variables are associated with any toplevel declarations, but we've filled in that part of the code ourselves. You don't need to worry about the primitives or literals, either. Now, some function needs to call the `reduce1` function repeatedly and figure out when it's appropriate to stop. This is done by the `reduce` function, which looks like this: let rec reduce (term : term) (env : env) : result = match reduce1 term with | AlreadyResult res -> res | StuckAt subterm -> die_with_stuck_term subterm | ReducedTo term' -> reduce term' env (* keep trying *) This recursively calls `reduce1` until it gets a result or gets stuck. In the latter case, it calls a function that prints an error message and quits the program. If it did get a result, it returns that to the caller. One of the helper functions used by `reduce1` is a `substitute` function, which begins: let rec substitute (ident : identifier) (replacement : term) (original : term) = match original with ... This makes sure to substitute the replacement for any free occurrences of `Var ident` in the original, renaming bound variables in the original as needed so that no terms free in the replacement become captured by binding `Lambda`s or `Let`s in the original. This function is tricky to write correctly; so we're just supplying it for you. However, one of the helper functions that *it* calls is `free_in (ident : identifier) (term : term) : bool`, and this was a function that you did write for an earlier homework. You should adapt your implementation of that to the datatype we're using here. Here is a skeleton: let rec free_in (ident : identifier) (term : term) : bool = match term with | _,TermVar(var_ident) -> var_ident = ident | _,TermApp(head, arg) -> ... | _,TermLambda(bound_ident, body) -> ... | _,TermLet(bound_ident, arg, body) -> ... | _,TermLetRec _ -> failwith "letrec not implemented" | _,TermIf(test, yes, no) -> ... | _,TermPrimfun _ | _,TermConstructor _ | _,TermDestructor | _,TermLiteral _ -> false That is, `Var ident` occurs free in `Var var_ident` iff the identifiers `ident` and `var_ident` are the same. The last four cases are easy: primitive functions, constructors, destructors, and literals contain no bindable variables so variables never occur free in them. These values may contain *results*, for instance if we partially apply the primitive function `+` to `3`, what we'll get back is another primitive function that remembers it has already been applied to `3`. But our interpreter is set up to make sure that this only happens when the argument (`3`) is already a result, and in our design that means it doesn't contain any bindable variables. But now what about the cases for App, Lambda, Let, and If? You will need to supply the missing code for these. It's just an adaptation/extension of what you did in the previous week's homework. For reference, OCaml's disjunction operator is written with an infix `||`; conjunction with `&&`; equality with `=`; and inequality with `<>`. TODO MORE TIPS You should also fill in the gaps in the `reduce1` function, as described above. You can do this all with pencil and paper and your brain, and **that will constitute phase 1 of (this part of) the homework.** But you can also download the real source code, as we'll explain below, and try to build/compile your work, and see if OCaml accepts it. Then you can play around with the result. If you're not going to do that, then skip down and read about Moving to the V2 interpreter. TODO link Getting, reading, and compiling the source code ----------------------------------------------- You can download the source code for the intepreter here: TODO link. That link will always give you the latest version. We will update it as we find any issues. Let us know about any difficulties you experience. When you unpack the downloaded source code, you will get a folder with the following contents, sorted here by logical order rather than alphabetically. interface.mli types.ml engine.ml main.ml primitives.ml hilevel.ml lolevel.ml lexer.mll parser.mly types.mli engine.mli primitives.mli hilevel.mli lolevel.mli Makefile .depend The first three files in this list are the only ones you need to worry about. An `.mli` file like `interface.mli` specifies types for OCaml, in a way that gets published for other files in the same program to see. This `interface.mli` file contains the basic datatypes the program works with. And even this file (necessarily) has some complexity you can ignore. All you really need to pay attention to at first are the datatypes we described before. For phase 1 of the exercise TODO, you shouldn't need to change anything in this file anyway. The second file, `types.ml`, contains different implementations for the environments. If you like, you can look at how these work, since they will play an important role in phase 2 of the exercise. The common interface these implementations have to supply is: TODO Each implementation is itself pretty simple, though this file also needs to have some trickery in it to work around constraints imposed by OCaml. (The trickery is marked as such.) The third file, `engine.ml`, is where you should be focusing your attention. That contains the skeleton of the `reduce1` and `free_in` functions that you need to complete for phase 1 of the exercise, and also contains the skeleton of the `eval` function that you need to complete for phase 2 of the exercise. At the bottom of the file are also instructions on how to shift the interpreter between using the V1 or the V2 functions. After you've edited the `engine.ml` file to complete the `reduce1` function, you can try building it and running the interpreter like this. First, make sure you're in a Terminal and that you're working directory is the folder that the source code unpacked to. Then just type `make`. That should take care of everything. If you see errors that you don't think are your fault, let us know about them. Possibly some Windows computers that _do_ have OCaml on them might nonetheless fail to have the `make` program. (It isn't OCaml-specific, and will be found by default on Macs and many Linux systems.) In that case, you can try entering the following sequence of commands by hand: ocamllex lexer.mll ocamlyacc -v parser.mly ocamlc -c lolevel.mli ocamlc -c lolevel.ml ocamlc -c interface.mli ocamlc -c types.mli ocamlc -c types.ml ocamlc -c hilevel.mli ocamlc -c hilevel.ml ocamlc -c primitives.mli ocamlc -w -8 -c primitives.ml ocamlc -c engine.mli ocamlc -c engine.ml ocamlc -c parser.mli ocamlc -c parser.ml ocamlc -c lexer.ml ocamlc -c main.ml ocamlc -o interp.exe lolevel.cmo types.cmo hilevel.cmo primitives.cmo engine.cmo parser.cmo lexer.cmo main.cmo If your computer doesn't ... TODO OK, I built the interpeter. How can I use it? --------------------------------------------- The interpreter you built is called `interp` (or on Windows, `interp.exe`). You can see its help message by running `./interp --help`. The interpreter takes input that you feed it, by any of various methods, and passes it through a _parser_. The parser expects the input to contain a sequence of terms (plus possibly the other toplevel declarations we described before), separated by semicolons. It's optional whether to have a semicolon after the last term. If the input doesn't parse correctly, you will have a Syntax error TODO. If it does parse, then the parser hands the result off to your interpretation functions (either `reduce`/`reduce1` in V1 of the interpreter, or `eval`/`evaluate`, in V2 described below). If they are able to reduce/evaluate each term to a result, they will print those results in a user-friendly format, and then move to the next result. If they ever encounter a stuck term or unbound variable, they will instead fail with an error. So how do you supply the interpreter with input. Suppose the file `./input` contains: let x = 3 in (x, 0); 5*2 - 1; \x. x Then here is a demonstration of three methods for supplying input. These will all give equivalent results: * `./interp input` * `echo 'let x = 3 in (x, 0); 5*2 - 1; \x. x' | ./interp -` * `./interp -e 'let x = 3 in (x, 0)' -e '5*2 - 1' -e '\x. x'` In the last method, the different `-e` elements are concatenated with `;`s; and then it works just like the preceding methods. The format the parser accepts should look somewhat familiar to you. Lambda expressions work as normal, except that they must always be "dotted". You can also use some infix operators in the ways you can in Kapulet and Haskell: 2-1 (-) 2 1 (2-) 1 (-1) 2 /* this works as in Kapulet not Haskell; it means 2-1 */ flip (-) 1 2 All giving the same result. As an experiment, the parser accepts two kinds of numeric input. Numbers like `0`, `1`, and so on are handled as native OCaml numbers; whereas numbers like `0.`, `1.` and so on are handled as abbreviations for `Zero`, `Succ Zero` and so on, with the implied OCaml datatype: type dotted_number = Zero | Succ of dotted_number The supplied primitives `pred`, `succ`, and `zero?` work only on the dotted numbers; whereas the supplied infix primitives `+`, `-`, and `*` work only on the undotted numbers. There are also `<` and `<=` and `==` for the undotted numbers. (The parser will also accept `>` and `>=`, but will translate them into the corresponding uses of `<` and `<=`.) Here is some more sample inputs, each of which the parser is happy with: /* these are comments */ if zero? x then 1 else pred x zero? 0. /* zero? and pred and succ work with dotted numbers */ x == 0 /* the infix comparison operators and + - * work with undotted numbers */ /* both of the following get translated into if-terms */ x and y x or y lambda x. x \x. x \x y. y (x y) let x = 1 + 2 in 2 * x let x = 1 + 2; y = x+1 in y let f = \x.x+1 in f 0 let f x = x + 1 in f 0 true (false,1) (,1) true (,) true 1 (,,) x y z /* but only up to triples, there are no bigger tuples */ box 1 /* this is a 1-tuple */ /* it won't be clear yet why it's useful, but wait for later weeks */ fst (1,2) /* evaluates to 1 */ /* you can use fst on boxes, pairs, and triples */ /* snd also on pairs and triples */ /* and trd on triples */ id x const x y /* our friend the K combinator */ flip f x y /* gives f y x */ g comp f /* comp is an infix composition operator */ f pmoc g /* this is flip comp, that is \x.g(f x) */ f x $ g y $ h z /* as in Haskell, this is f x (g y (h z)) */ /* note that it's not f x (g y) (h z) */ "strings" /* you can input these and pass them around, but can't perform any operations on them */ The parser also accepts `letrec ... in ...` terms, but currently there is no implementation for how to reduce/interpret these (that's for a later assignment), so you'll just get an error. As explained before, you can also include toplevel declarations like: let y = ... end; Calling them "toplevel" means they can't be embedded inside terms. The above declaration interprets the rhs as a term, and then binds the variable `y` to the result, making it available in later terms where `y` hasn't been locally rebound. Thus: let y = 1 + 2 end; let x = 0 in (x,y) gets interpreted as `(0,3)`. Another toplevel declaration `symbol w` tells the interpreter that you want to specially designate `w` as okay to appear uninterpreted in terms you use. If the interpreter encounters other locally-unbound variables, where no toplevel declaration has already been given, it will fail with an error. Moving to the V2/environment-based interpreter ---------------------------------------------- ...