Datatypes and complications

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 terms 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:


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.

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.

We're going to explore how to implement the interpreter using two different methods. One is a translation of the method Chris demonstrated for combinatory logic over to the lambda calculus. You check if any part of a term is reducible, if so perform the appropriate substitutions, and then keep repeating single-step reductions until you can't find any more eligible redexes. This involves substituting the argument of a redex into any free occurrences of the variable bound in the head. 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 substitute-and-repeat interpreter; and second, an environment-based one.

The code of this interpreter 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 substitute-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.

We provided you with a homework assignment that is a simplified version of the code of our interpreter. After getting that to work, you may be interested to play around with the fuller version, which adds literal numbers and booleans, and user-friendly input and printing of results.

But these benefits come at a price. The code has some complexity to it.

Now part of the complexity can be factored out into extra files that you might 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 terms in general and what we want to count as results of our computations.

In these exercises, unlike the combinatory 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 VA/substitute-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 VB/environment-based interpreter, we will need to introduce some results that aren't (just) terms, called Closures. 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 overlaps or is a subtype of another, so this is what works best.

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 substitute-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 as input, 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 original code. 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 VA/substitute-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)

Okay, that completes our survey of the complications.

The VA/reducuction-based interpreter

For one part of the homework, we had you complete a reduce_head_once function that converts OCaml values like:

TermApp(TermApp(TermLambda("x", TermApp(Var "x", Var "y")), TermLambda("w", Var "w")), ...)


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.

Let's think about how we should detect that the term has been reduced as far as we can take it. In the substitute-and-repeat interpreter Chris demonstrated for combinatory logic, we had the reduce_if_redex 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 idiosyncrasies 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, cyclic structures.) 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 combinatory logic if other terms are chosen as primitive (W W1 W2 reduces to W1 W2 W2, so let them all just be plain Ws).

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 reduce_head_once 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 reduce_head_once 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 reduce_head_once already knows what term it was trying to reduce; but in some cases it's a bit more convenient to have reduce_head_once 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 reduce_head_once, 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 reduce_head_once function, then, will look like this:

let rec reduce_head_once (term : term) (env : env) : reduceOutcome =
  match term with
  | _,TermLambda _
  | _,TermPrimfun _
  | _,TermConstructor _
  | _,TermDestructor _
  | _,TermLiteral _ -> AlreadyResult (result_of_term term)

  | fi,TermVar var -> ...

  (* notice we never evaluate a yes/no branch until it is chosen *)
  | _,TermIf((_,TermLiteral(Bool true)),yes,no) ->
      ReducedTo yes
  | _,TermIf((_,TermLiteral(Bool false)),yes,no) ->
      ReducedTo no
  | fi,TermIf(test,yes,no) ->
      (match reduce_head_once test env with
      | AlreadyResult _ -> StuckAt term (* if test was not reducible to a bool, the if-term is not reducible at all *)
      | StuckAt _ as outcome -> outcome (* propagate Stuck subterm *)
      | ReducedTo test' -> ReducedTo(fi,TermIf(test',yes,no)))

  | ...

The homework asked you to fill in some of the gaps in this function. As mentioned before, the only role that environments play in this function is to see if variables are associated with any toplevel bindings. (If you're willing to forego those, you can select an implementation Env0 of the environments that is guaranteed to do nothing.)

Now, some function needs to call the reduce_head_once 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 reduce_head_once term with
  | AlreadyResult res -> res
  | StuckAt subterm -> die_with_stuck_term subterm
  | ReducedTo term' -> reduce term' env (* keep trying *)

This recursively calls reduce_head_once 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.

The structure of these two functions, reduce_head_once and reduce, is similar to the functions reduce_if_redex and reduce_try2 in the combinatory evaluator. The only difference is that reduce_if_redex only performed a reduction if its argument was exactly a redex. It had to rely on its caller to detect cases where the term was instead a longer sequence of applications that had a redex at its head. In the present code, on the other hand, we have reduce_head_once take care of this itself, by calling itself recursively where appropriate. Still, it will only perform at most one reduction per invocation.

One of the helper functions used by reduce_head_once 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 Lambdas or Lets in the original. This function is tricky to write correctly; so we supplied 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. Hence we asked you to adapt your implementation of that to the term datatype used in this interpreter. Here is a skeleton of this function:

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? Think about what the code for these should look like. It's just an adaptation/extension of what you did in the previous week's homework. In the assignment, we supplied some of the code for these, and asked you to complete the rest.

We also asked you to fill in some gaps in the reduce_head_once function.

Moving to the VB/environment-based interpreter

The previous interpreter strategy is nice because it corresponds so closely to the reduction rules we give when specifying our lambda calculus. (Including specifying evaluation order, which redexes it's allowed to reduce, and so on.) But keeping track of free and bound variables, computing fresh variables when needed, that's all a pain.

Here's a better strategy. Instead of keeping all of the information about which variables have been bound or are still free implicitly inside of the terms, we'll keep a separate scorecard, which we will call an "environment". This is a familiar strategy for philosophers of language and for linguists, since it amounts to evaluating terms relative to an assignment function. The difference between the substitute-and-repeat approach above, and this approach, is one huge step towards monads.

Read this page explaining about the need for Closures in this interpreter strategy..

Now Closures are not a new kind of lambda term: the syntax for our language doesn't have any constituents that get parsed into Closures. Closures are only created during the course of evaluating terms: specifically, when a variable gets bound to an abstract, which may itself contain variables that are locally free (not bound by the abstract itself). This is why we have separate datatypes for terms and for the results that terms can evaluate to. Closures are results, but they aren't terms. Apps are terms, but not results. Our boolean and number literals, as well as our primitive functions, constructors, and destructors, are both.

In later weeks, we will see more examples of results that aren't terms, but can only be generated during the course of a computation. (I'm thinking of mutable reference cells. Arguably, partially applied constructors are yet another example, that we're already familiar with.)

Getting, reading, and compiling the source code

You can download the source code for the intepreter here. 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.


The first three files in this list are the ones you should focus on.

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.

The second file,, contains different implementations for the environments. If you like, you can look at how these work. The common interface these implementations have to supply is declared in the previous file:

module type ENV = sig
  type env
  val empty : env
  val shift : identifier -> binding -> env -> env
  val lookup : identifier -> env -> binding option
  val version : string
end (* ENV *)

Each implementation of that interface is itself pretty simple, though the file does need to have some trickery in it to work around constraints imposed by OCaml. (The trickery is marked as such.)

OCaml's terminology for the abstract interfaces is module type S = sig ... end, and its terminology for the concrete implementations of these is module M = struct ... end. (By the way, the *.mli files get compiled into the former of these, and the *.ml files get compiled into the latter.) The implementations have to define (at least) all the types and values declared in the abstract interface. Notice that in the ENV interface, we just said type env. That means there has to be some type env; but different implementations can define it differently. Also, when other parts of the code use the interface, the details of how the env type is implemented won't be exposed to them. They have to interact with the envs via the declared shift and lookup functions, and the empty environment that every implementation is obliged to provide.

What the function lookup does is take an identifier like "x" and an existing env, and try to return the result that this env associates with that identifier, if any. Else it returns None. (There are some complications, in that we don't really return a result option, but rather a binding option, where the binding type is a small wrapper around a type bound, which is identified with the type result. The point of the binding wrapper is to help handle the toplevel declarations. But you can ignore that and just think of the bindings as bound/results. The point of having the two identified types bound and result is to prepare for later developments. The result is what a term like Var "x" evaluates to (in a context where it's not free). The bound is what the environment binds the identifier "x" to. In our present system, these are of course the same. But later when we introduce mutable state into our system, they may come apart, depending on design choices we make.)

What the function shift does is take an env and add a new binding for a given identifier. It returns an env with this new binding. The identifier may or may not have already had a binding in the original env; but in any case, the new env will only return the supplied new binding when you lookup the ident.

As we've said, there are different ways to implement these environments. That's what's in the file. The Env0 implementation provides the demanded interface, but doesn't do anything. It won't remember any new bindings. You can select this for the VA interpreter, if you like, to demonstrate that the envs are inessential to that interpretation strategy. (Though in that case the toplevel declarations won't be remembered.) Env1 implements the environments as a list of pairs of identifiers and bindings. Env2 implements the environments instead as functions from identifiers to Some binding or to None, if the identifier has no binding in that environment. At the end of the file is the line:

include Env1

You can change that to whichever of these implementations you'd like to use.

The third file,, is where the action is. Most of the homework assignment was just a simplified version of this file. At the bottom of the file are also instructions on how to shift the interpreter between using the VA or the VB functions:

(* Put comment (* *)s around exactly one of the following two pairs of lines. *)

let version = "A (reduce by substituting; " ^ version ^ ")"
let interpret = VA.reduce

let version = "B (use environment for local bindings; " ^ version ^ ")"
let interpret = VB.evaluate

You can try building and running the interpreter like this. First, make sure you're in a Terminal and that your 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
ocamlc -c interface.mli
ocamlc -c types.mli
ocamlc -c
ocamlc -c hilevel.mli
ocamlc -c
ocamlc -c primitives.mli
ocamlc -w -8 -c
ocamlc -c engine.mli
ocamlc -c
ocamlc -c parser.mli
ocamlc -c
ocamlc -c
ocamlc -c
ocamlc -o interp.exe lolevel.cmo types.cmo hilevel.cmo primitives.cmo \
  engine.cmo parser.cmo lexer.cmo main.cmo 

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 get a "Syntax error." If it does parse, then the parser hands the result off to your interpretation functions (either reduce/reduce_head_once in VA of the interpreter, or eval/evaluate, in VB). 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" (that is, use \x.x not \x x). You can also use some infix operators in the ways you can in Kapulet and Haskell:

(-) 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 <=.)

This version of the interpreter doesn't come pre-equipped with any Church numbers (or booleans or tuples or lists), but you can code them manually if you like. You can also code a fixed point combinator manually if you like. But be sure to use Θ′ or Y′, since the unprimed fixed point combinators we introduced you to won't terminate in a call-by-value interpreter, like this one. (The lambda evaluator on the website, by contrast, uses a normal order evaluation strategy.)

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
(,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 o f /* `o` is an infix composition operator */
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 */

Predefined combinators include: S, K (same as const), I (same as id), B (same as (o), occurring in prefix not infix position), C (same as flip), T (same as flip ($)), V (the Church pairing combinator), W, M (better known as ω), and L.

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.