Formalizing the Lambda Calculus
This is a continuation of last lecture on how to formalize programming languages mathematically.
import Lait
#lait
Small-Step Semantics for the Lambda Calculus
Now we can write down the small-step semantics for the lambda calculus.
To do this smoothly, we will take one more leap of faith: instead of writing inference rules with abstract syntax (such as with EAdd, or ELam for the lambda calculus), we will use concrete syntax.
For this, we need just a bit more notation.
If \(e\) is a lambda calculus program, then we write \(e[x \mapsto v]\) for
the result of substituting \(v\) for the variable \(x\) in \(e\).
We will use the metavariable \(v\) to stand for values; in the case of the
lambda calculus, all values are abstractions.
Then, we can write the small-step semantics for the call-by-value
lambda calculus:
\[ \dfrac{e_1 \rightarrow e_1^{\prime}}{e_1\ e_2 \rightarrow e_1^{\prime}\ e_2}\ \text{(E-App1)} \qquad \dfrac{e_2 \rightarrow e_2^{\prime}}{v_1\ e_2 \rightarrow v_1\ e_2^{\prime}}\ \text{(E-App2)} \qquad \dfrac{}{(\lambda x. e)\ v_2 \rightarrow e[x \mapsto v_2]}\ \text{(E-Beta)} \]
The type of the small-step relation \(\rightarrow\) is that it relates lambda-calculus expressions with lambda-calculus expressions.
These three rules are exactly the informal stepping rules from Lecture 4: step the function until
it is a value, then step the argument until it is a value, then \(\beta\)-reduce.
Just as with CalcLang, there is no rule for stepping an abstraction: values do not step.
Let’s see some examples of stepping specific lambda terms:
\[ \dfrac{}{(\lambda x. x)\ (\lambda y. y) \rightarrow \lambda y. y}\ \text{(E-Beta)} \]
Another one:
\[ \dfrac{\dfrac{}{(\lambda x. x)\ (\lambda y. y) \rightarrow \lambda y. y}\ \text{(E-Beta)}} {((\lambda x. x)\ (\lambda y. y))\ (\lambda z. z) \rightarrow (\lambda y. y)\ (\lambda z. z)}\ \text{(E-App1)} \]
Another one:
\[ \dfrac{\dfrac{}{(\lambda x. x)\ (\lambda y. y) \rightarrow \lambda y. y}\ \text{(E-Beta)}} {(\lambda z. z)\ ((\lambda x. x)\ (\lambda y. y)) \rightarrow (\lambda z. z)\ (\lambda y. y)}\ \text{(E-App2)} \]
As we did for CalcLang, we can translate these rules into a Lait function. Let’s recall our
definitions from before, changing them slightly to make our small-steps more ergonomic:
type Exp := | EVar (var : String)
| ELam (var : String) (body : Exp)
| EApp (e1 : Exp) (e2 : Exp)
def Exp.isValue (e : Exp) : Bool :=
match e with
| ELam _ _ => true
| _ => false
end
def subst (var : String) (assignment : Exp) (body : Exp) : Exp :=
match body with
| EVar var' => if var == var' then assignment else EVar var'
| EApp e1 e2 => EApp (subst var assignment e1) (subst var assignment e2)
| ELam var' body' =>
if var == var' then
ELam var' body' -- Shadowing: no substitution
else
ELam var' (subst var assignment body')
end
Now, each rule becomes one arm of a match (again, not stepping values now).
Let’s also have our stepper print out the rule that it is using:
def stepLam (e : Exp) : Exp :=
match e with
| EVar _ => error "stepLam: got a variable"
| ELam _ _ => error "stepLam: values do not step"
| EApp e1 e2 =>
match e1 with
| ELam x body =>
if Exp.isValue e2 then
-- ------------------------------- (E-Beta)
-- (\x. body) v --> body[x |-> v]
let _ := print "E-Beta" in
subst x e2 body
else
-- e2 --> e2'
-- --------------------- (E-App2)
-- v1 e2 --> v1 e2'
let _ := print "E-App2" in
EApp e1 (stepLam e2)
| _ =>
-- e1 --> e1'
-- --------------------- (E-App1)
-- e1 e2 --> e1' e2
let _ := print "E-App1" in
EApp (stepLam e1) e2
end
end
Let’s check it now against the three derivations above.
(Remember that you can hover/click on #eval results if they have more than one line to show.)
def Lx_x := ELam "x" (EVar "x")
def Ly_y := ELam "y" (EVar "y")
def Lz_z := ELam "z" (EVar "z")
#eval stepLam (EApp Lx_x Ly_y)>> E-Beta
-- Another example: repeatedly stepping a term..
#eval stepLam (EApp (EApp Lx_x Ly_y) Lz_z)>> E-App1
#eval stepLam (stepLam (EApp (EApp Lx_x Ly_y) Lz_z))>> E-App1
#eval stepLam (stepLam (stepLam (EApp (EApp Lx_x Ly_y) Lz_z)))
-- Another example:
#eval stepLam (EApp Lz_z (EApp Lx_x Ly_y))>> E-App2
Big-Step Semantics for the Lambda Calculus
Now that we have seen the small-step semantics, let’s turn to the big-step semantics. Here is is below:
\[ \dfrac{}{(\lambda x. e) \Downarrow (\lambda x. e)}\ \text{B-Lam} \qquad \dfrac{e_1 \Downarrow (\lambda x. e) \quad e_2 \Downarrow v_2 \quad e[x \mapsto v_2] \Downarrow v}{e_1\ e_2 \Downarrow v}\ \text{B-App} \]
The type of the big-step relation \(\Downarrow\) is that it relates lambda-calculus expressions with values of the form \(\lambda x. e\). As we saw in lecture 6, values big-step to values; this gives us the first rule. For the second rule, we think of how we would write the interpreter for applications. To interpret \(e_1\ e_2\), we:
- Interpret \(e_1\) down to a value, \(\lambda x. e\);
- Interpret \(e_2\) down to a value, \(v_2\);
- And continue interpreting \(e[x \mapsto v_2]\).
In essence, the big-step rule “chains together” multiple evaluations to give a single rule for application, rather than a collection of rules (E-App1, E-App2, E-Beta).
Relating the Big and Small-Step Semantics
So far, we’ve seen the big-step and the small-step semantics as two separate ways of defining the semantics of programs. But, intuitively, these two semantics ought to agree. The formal relationship we want is that if one repeatedly performs small steps until a term is fully simplified, then this fully simplified value is equal to the value given by the big-step semantics.
Let’s make this relationship precise by defining an additional relation \(\rightarrow^{\ast}\) (called the multi-step relation, sometimes also called “star step”) that repeatedly performs small steps. We can define this using the small-step semantics as:
\[ \dfrac{e \rightarrow e^{\prime} \quad e^{\prime} \rightarrow^{\ast} e^{\prime\prime}}{e \rightarrow^{\ast} e^{\prime\prime}}\ \text{(E-Trans)} \qquad \dfrac{}{v \rightarrow^{\ast} v}\ \text{(E-Refl)} \]
The (E-Trans) rule, short for transitivity, says that \(e\) multi-steps to \(e^{\prime\prime}\) if there is some intermediate \(e^{\prime}\) such that \(e\) steps to \(e^{\prime}\), and \(e^{\prime}\) multi-steps to \(e^{\prime\prime}\). The (E-Refl) rule, short for reflexivity, says that a value multi-steps to itself. (Since (E-Refl) only applies to values, \(e \rightarrow^{\ast} e^{\prime\prime}\) means that \(e\) runs all the way to the value \(e^{\prime\prime}\).) Let’s see an example:
\[ \dfrac{ \dfrac{\dfrac{}{\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 2) \rightarrow \verb|NumE | 3}\ \text{(E-Add1)}} {\verb|AddE | (\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 2))\ (\verb|NumE | 3) \rightarrow \verb|AddE | (\verb|NumE | 3)\ (\verb|NumE | 3)}\ \text{(E-Add2)} \quad \dfrac{ \dfrac{}{\verb|AddE | (\verb|NumE | 3)\ (\verb|NumE | 3) \rightarrow \verb|NumE | 6}\ \text{(E-Add1)} \quad \dfrac{}{\verb|NumE | 6 \rightarrow^{\ast} \verb|NumE | 6}\ \text{(E-Refl)} } {\verb|AddE | (\verb|NumE | 3)\ (\verb|NumE | 3) \rightarrow^{\ast} \verb|NumE | 6}\ \text{(E-Trans)} } {\verb|AddE | (\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 2))\ (\verb|NumE | 3) \rightarrow^{\ast} \verb|NumE | 6}\ \text{(E-Trans)} \]
We implemented this relation as the run function in lecture 2.
Using our new step function, which only applies to non-values, it looks like this:
def multistep (e : CalcLang) : CalcLang :=
match e with
| NumE n => NumE n -- (E-Refl)
| AddE _ _ => multistep (step e) -- (E-Trans)
end
Then, we can state the following theorem that formally relates the two semantics:
Theorem. For any CalcLang program \(e\), \(e \rightarrow^{\ast} n\) if and only if
\(e \Downarrow n\).
We won’t prove this theorem right now, but the proof is a structural inductive argument
not unlike the one we did for showing that all CalcLang programs terminate.
It’s not too difficult, but it is quite tedious, since it requires handling many
cases.
An analogous theorem holds for the lambda calculus: its small-step rules agree with the
big-step interp function from lecture 4. This justifies our giving only a small-step semantics for the
lambda calculus above: if we want a big-step semantics, we can define \(e \Downarrow v\) to mean \(e \rightarrow^{\ast} v\).
Differences between Big-Step and Small-Step
One might wonder why we consider both big-step and small-step semantics, since they can seem redundant. There are a lot of different reasons, but we will focus on two:
- Simplicity. Small-step semantics typically have more rules, but each rule is simpler. On the other hand, big-step semantics has less rules, but each rule is more complicated. This is a tradeoff.
- Nontermination. Big-step semantics has a different view on programs that run forever than small step. Consider our nonterminating lambda calculus term \(\Omega := (\lambda x. x\ x)\ (\lambda x. x\ x)\). Under a small-step semantics, we have the infinite chain of steps \(\Omega \rightarrow \Omega \rightarrow \Omega \rightarrow \dots \). Under a big-step semantics, we can’t talk about \(\Omega\): indeed, there is no value \(v\) such that \(\Omega \Downarrow v\) (under an appropriate big-step semantics for the lambda calculus).
Errors and Being Stuck
Finally, we will conclude this discussion by revisiting IfLang from lecture 3, which
adds Booleans and conditionals to our calculator language. (We leave out and for brevity.)
<expr> ::= <number>
| true
| false
| <expr> + <expr>
| if <expr> then <expr> else <expr>
| ( <expr> )
Let’s define our semantics so that the guards of if-expressions are expected to be Booleans.
Here is what the rules look like for this. Below, we are making one more step towards brevity, by writing IfLang as well in concrete syntax. Note that this makes the rule (E-Add1) a little subtle: the addition + in the conclusion of the rule is addition at the level of the AST, while the side condition is using + in the metalanguage.
\[ \dfrac{}{n_1 + n_2 \rightarrow n}\ \text{(E-Add1)} \quad \text{where } n = n_1 + n_2 \]
\[ \dfrac{e_1 \rightarrow e_1^{\prime}}{e_1 + e_2 \rightarrow e_1^{\prime} + e_2}\ \text{(E-Add2)} \qquad \dfrac{e_2 \rightarrow e_2^{\prime}}{n + e_2 \rightarrow n + e_2^{\prime}}\ \text{(E-Add3)} \]
\[ \dfrac{}{\mathsf{if}\ \mathsf{true}\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2 \rightarrow e_1}\ \text{(E-IfT)} \qquad \dfrac{}{\mathsf{if}\ \mathsf{false}\ \mathsf{then}\ e_1\ \mathsf{else}\ e_2 \rightarrow e_2}\ \text{(E-IfF)} \]
\[ \dfrac{e_1 \rightarrow e_1^{\prime}}{\mathsf{if}\ e_1\ \mathsf{then}\ e_2\ \mathsf{else}\ e_3 \rightarrow \mathsf{if}\ e_1^{\prime}\ \mathsf{then}\ e_2\ \mathsf{else}\ e_3}\ \text{(E-IfStep)} \]
Note what happens if we try to step the program if 10 then true else false: there is no
rule that applies to this program! The guard 10 is not true or false, so neither (E-IfT) nor (E-IfF) applies,
and 10 cannot take a step, so (E-IfStep) does not apply either. Similarly, no rule applies to true + 1.
Formally, a term is stuck if (1)
there is no small-step rule that applies to it, and (2) it is not a value.
Stuck terms are exactly the ones that cause runtime errors in interpreters. In lecture 3, our interpreter for IfLang
raised an error when this happened (via Value.toInt and Value.toBool).
Open terms in the lambda calculus are another example: a variable \(x\) on its own is not a value,
and no rule applies to it, so it is stuck. This is why both interp and stepLam raise an error when they reach a variable:
#eval stepLam (EVar "x")
Since we don’t want our interpreters to crash, we also don’t want our semantics to get stuck. Thus, we will now turn to talking about types, which allow us to ensure that we never get stuck terms.