Formalism
We have reached a point now where we have quite a complicated little garden of languages, each with their own syntax and semantics. It is time to bring order to this situation by introducing a concise and powerful system of notation for describing a program’s behavior. This new notation will empower us to quickly and precisely write down a description of a program’s semantics. As we move through the course, you will see more of this notation as we use it to describe type systems and other facets of how programs operate.
This lecture is heavily based on Chapter 3 of Pierce, B. C. (2002). Types and Programming Languages. MIT Press.import Lait
#lait
Inference Rules and Derivation Trees
Let’s go back to the CalcLang example we’ve been studying since
the first lecture:
type CalcLang := | NumE (val : Int) | AddE (left : CalcLang) (right : CalcLang)
We’ve seen two ways of giving semantics to CalcLang: big-step (via a
definitional interpreter) and small-step (via a stepper). Let’s start with the small-step semantics.
In Lecture 2, our stepper behaved as follows:
NumE vis a value, so there is nothing left to do.- To step
AddE e1 e2:- if
e1ande2are both valuesNumE v1andNumE v2, step to their sum,NumE (v1 + v2); - if
e1is a value (bute2is not), stepe2; - if
e1isn’t a value, stepe1.
- if
This is quite a mouthful, and it is a bit awkward to describe these rules in English. So, we will introduce the following notation to summarize these rules:
\[ \dfrac{}{\verb|AddE | (\verb|NumE | n_1)\ (\verb|NumE |n_2) \rightarrow \verb|NumE |(n_1 + n_2)}\ \text{E-Add1} \]
\[ \dfrac{e_1 \rightarrow e_1^{\prime}}{\verb|AddE | e_1\ e_2 \rightarrow \verb|AddE | e_1^{\prime}\ e_2}\ \text{E-Add2} \qquad \dfrac{e_2 \rightarrow e_2^{\prime}}{\verb|AddE | (\verb|NumE | n)\ e_2 \rightarrow \verb|AddE | (\verb|NumE | n)\ e_2^{\prime}}\ \text{E-Add3} \]
Note
Inference rules can be tricky the first time you see them! It’s good to get some practice using them.
This notation is probably unfamiliar, so let’s define what it means. An inference rule is a collection of premises followed by a conclusion, separated by a horizontal line. For example, the following is a simple inference rule that says that if it is raining and you have no umbrella, then you are wet:
\[ \dfrac{\text{It is raining.} \quad \text{You have no umbrella.}}{\text{You are wet.}} \]
If an inference rule has no premises, then it is called an axiom. Axioms are always true. The following is an example of an axiom:
\[ \dfrac{}{\text{The sun is hot.}} \]
Using inference rules for programming languages. To use inference rules to describe the semantics of programs, we have \(\rightarrow\) be interpreted as a relation between two programs. Thus, for any programs \(e_1\) and \(e_2\), it makes sense to write down the proposition \(e_1 \rightarrow e_2\). What inference rules do is tell us which propositions of the form \(e_1 \rightarrow e_2\) are true.
Note that there are infinitely many programs \(e_1\) and \(e_2\) such that \(e_1 \rightarrow e_2\). For example, we have that \(\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 2) \rightarrow \verb|NumE | 3\), and \(\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 3) \rightarrow \verb|NumE | 4\), and so on. Thus, to make the rules easier to write, we use metavariables, like \(n\) and \(e_1\), which are place holders that stand in for concrete syntax. The field of programming languages often uses certain conventions when using metavariables: for example, \(n\), \(n_1\), \(n’\) and so on stand for numbers, while \(e\) and \(e’\) stand for arbitrary expressions. Going through each rule one at a time:
- (E-Add1) handles the case when both arguments are numbers.
It is an axiom: it says that the program
AddE (NumE n1) (NumE n2)steps toNumE (n1 + n2). Be careful here: the \(+\) in the conclusion is Lait-level addition of actual numbers, whileAddEis our abstract syntax for addition inCalcLang. - (E-Add2) says that if
e1to some other expressione1', thenAddE e1 e2steps toAddE e1' e2. - (E-Add3) says that if
e2can take a step toe2', thenAddE (NumE n) e2can take a step toAddE (NumE n) e2'.
Note that, unlike our stepper in Lecture 2, there is no rule whose conclusion is \(\verb|NumE | n_1 \rightarrow \dots\): here, values do not step at all. Also note that these rules are deterministic: at most one inference rule can apply at a time.
Derivation Trees and Running Programs by Hand
To apply an inference rule to a concrete program, we match the program against the rule’s
conclusion, finding a piece of concrete syntax for each metavariable. This process is called unification.
For example, suppose we want to determine what the following CalcLang
program steps to: AddE (NumE 10) (NumE 20).
To do this, we need to determine which of the above inference rules apply to this program. We do this by process of elimination:
- The (E-Add1) rule applies: the syntax
AddE (NumE 10) (NumE 20)matches the left-hand side of the \(\rightarrow\) in its conclusion. Concretely, the number10unifies with the mtavariablen1, and20unifies with the metavariablen2. - The (E-Add2) rule does not apply: to use it, we would need as a hypothesis
that
NumE 10can take a step, butNumE ncannot take a step for anyn. - The (E-Add3) rule does not apply, for the same reason: we would need
NumE 20to take a step.
So, we can draw the following derivation that shows how to apply the (E-Add1) rule to take a step:
\[ \dfrac{}{\verb|AddE | (\verb|NumE | 10)\ (\verb|NumE | 20) \rightarrow \verb|NumE | 30}\ \text{(E-Add1)} \]
It is typical to write the name of the applied inference rule whenever it is used, but it may sometimes be left out for brevity, especially in the case of axioms.
One of the key properties of inference rules is that they can be chained together, with the conclusions of one rule feeding into the premises of the next rule. For instance, we can chain together the (E-Add1) and (E-Add2) rules to establish a more complicated fact:
\[
\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 | 2) \rightarrow \verb|AddE | (\verb|NumE | 3)\ (\verb|NumE |
2) }\ \text{(E-Add2)}
\]
This structure is called a derivation tree. At the bottom of the
tree is the final conclusion. A derivation tree is valid if
all of the leaves of the tree are axioms,
i.e., statements that hold without premises.
Notice how in the above derivation tree, the metavariable \(e_1\) of the
(E-Add2) rule unifies with the syntax AddE (NumE 1) (NumE 2), and the
metavariable \(e_2\) unifies with the syntax NumE 2.
From Inference Rules to Implementation
Inference rules are a specification language: they are an improvement over English for describing the semantics of programs. So, it is important to know how to translate inference rules into implementations.
Because our rules for CalcLang are deterministic, they describe a partial function: if we have an expression \(e_1\), then
there is at most one \(e_2\) such that \(e_1 \rightarrow e_2\). Hence, in this case, we can implement the small-step rules in Lait as a function
from expressions to expressions, with one case for each inference rule:
def step (e : CalcLang) : CalcLang :=
match e with
| NumE _ => error "step: numbers do not step"
| AddE e1 e2 =>
match e1 with
| NumE n1 =>
match e2 with
| NumE n2 =>
-- ------------------ (E-Add1) where n = n1 + n2
-- n1 + n2 --> n
NumE (n1 + n2)
| _ =>
-- e2 --> e2'
-- ------------------------ (E-Add3)
-- n1 + e2 --> n1 + e2'
let e2' := step e2 in
AddE (NumE n1) e2'
end
| _ =>
-- e1 --> e1'
-- ------------------------ (E-Add2)
-- e1 + e2 --> e1' + e2
let e1' := step e1 in
AddE e1' e2
end
end
We implement step (as usual) by pattern matching on the expression.
Notice, however, that each arm of the match corresponds to a particular inference rule,
and how each premise of the form \(e \rightarrow e^{\prime}\) turns into a recursive call to step.
Since no rule applies to numbers,
we raise an error in that case.
Let’s check that step agrees with the derivations we drew by hand:
#test step (AddE (NumE 10) (NumE 20)) === NumE 30
#test step (AddE (AddE (NumE 1) (NumE 2)) (NumE 2)) === AddE (NumE 3) (NumE 2)
#eval step (NumE 3)
Big-Step Semantics
Recall our definitional interpreter for CalcLang:
def interpCalc (e : CalcLang) : Int :=
match e with
| NumE n => n
| AddE e1 e2 => interpCalc e1 + interpCalc e2
end
A big-step semantics relates programs to the values that they run to. We can use our inference rule notation to describe this relation in a manner similar to the small-step semantics. We write \(e \Downarrow v\) to mean that program \(e\) runs to value \(v\):
\[ \dfrac{}{\verb|NumE | n \Downarrow n}\ \text{(B-Num)} \qquad \dfrac{e_1 \Downarrow v_1 \quad e_2 \Downarrow v_2}{\verb|AddE | e_1\ e_2 \Downarrow (v_1 + v_2)}\ \text{(B-Add)} \]
Note that while the small-step semantics $\to$ is a relation between programs
and programs, big-step $\Downarrow$ is a relation between programs and values.
(For CalcLang, we take values to just be mathematical integers).
Now we can use these inference rules to draw derivation trees that
describe running CalcLang programs to values:
\[ \dfrac{\dfrac{}{\verb|NumE | 1 \Downarrow 1}\ \text{(B-Num)} \qquad \dfrac{}{\verb|NumE | 2 \Downarrow 2}\ \text{(B-Num)}} {\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 2) \Downarrow 3}\ \text{(B-Add)} \]
Another example:
\[ \dfrac{ \dfrac{ \dfrac{}{\verb|NumE | 1 \Downarrow 1}\ \text{(B-Num)} \quad \dfrac{}{\verb|NumE | 2 \Downarrow 2}\ \text{(B-Num)} } {\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 2) \Downarrow 3}\ \text{(B-Add)} \quad \dfrac{}{\verb|NumE | 3 \Downarrow 3}\ \text{(B-Num)}} {\verb|AddE | (\verb|AddE | (\verb|NumE | 1)\ (\verb|NumE | 2))\ (\verb|NumE | 3) \Downarrow 6}\ \text{(B-Add)} \]
Observe: this derivation tree looks exactly like a trace of running
interpCalc. Each use of (B-Add) corresponds to a call to interpCalc on an AddE,
whose two premises correspond to the two recursive calls. In this way, one can
think of the big-step semantics as a concise shorthand for describing
how definitional interpreters behave.
#test interpCalc (AddE (AddE (NumE 1) (NumE 2)) (NumE 3)) === 6
Structural Induction
Inference rules give us the scaffolding to argue carefully about what programs
do. Suppose we want to establish the following seemingly obvious fact: all
CalcLang programs terminate. This seems quite obviously true (as long as
the abstract syntax trees of programs we consider are finite in size, which is
an assumption we always make). How might we go about proving such a thing?
First, we want to translate this statement into something mathematically rigorous.
For a program \(e\) to terminate means that there exists an integer \(n\) such that \(e \Downarrow n\).
Theorem. For every CalcLang program \(e\), \(e \Downarrow n\) for some \(n\).
We will prove this by structural induction on program syntax. To use structural induction on programs, you do the following:
- Prove the theorem for the base cases, which are the programs which do not syntactically contain other sub-programs. For
CalcLang, the only base case isNumE. - Then, prove each inductive case, which are the programs that do have sub-programs. For
CalcLang, the only inductive case isAddE. When we prove an inductive case, we get to assume the theorem already holds for the sub-programs; these assumptions are called inductive hypotheses.
Note
You may notice here a subtle difference between this notion of induction and the one you may have seen in your math classes. In math class, you typically make inductive arguments using the natural numbers, like “first, show the theorem holds for objects of size 1. Then, assuming the theorem holds for objects of size \(n\), show that the theorem holds for objects of size \(n+1\). Then, we can conclude that the theorem holds for all objects of size at least 1.” Here, we are not inducting on the naturals; we are instead inducting on our abstract syntax tree.
Now, let’s prove the theorem.
- Case \( \verb|NumE | n \): this is a base case. Thus, we must show that \( \verb|NumE | n \Downarrow n’ \) for some \(n’\). By applying rule (B-Num), we can set \(n’ := n\) and the result follows.
- Case \(\verb|AddE | e_1\ e_2\): this is an inductive case. Thus, we already know that \(e_1 \Downarrow n_1\) and \(e_2 \Downarrow n_2\) for some integers \(n_1\) and \(n_2\), and we need to show that \(\verb|AddE | e_1\ e_2 \Downarrow n_3\) for some \(n_3\). We can set \(n_3 := n_1 + n_2\), and apply rule (B-Add).
We have proven each base case and inductive case, so the result follows.
Notice that this proof has the same shape as interpCalc itself: interpCalc only ever
calls itself recursively on sub-terms of its input. This kind of recursion is called
structural recursion, and structural induction is how we reason about it.
Note
The following statements are also provable by structural induction:
- Determinism: If \(e \Downarrow n\) and \(e \Downarrow n’\), then \(n = n’\).
- Safety against negative numbers: If \(e\) does not contain any subterms of the form \(\verb|NumE | m\) where \(m < 0\), then if \(e \Downarrow m\), we have that \(m \geq 0\).
Extensions to CalcLang
Let’s now see how our inference rules look like when we extend the CalcLang language.
For simplicity, we will write them using big-step semantics.
If0
First, let’s consider an extension with a construct for if0/then/else:
e ::= NumE n | AddE e1 e2 | If0 e1 e2 e3
Informally, the semantics of If0 e1 e2 e3 is:
- Evaluate
e1to a valuev. - If
v = 0, evaluatee2. - Otherwise, evaluate
e3.
Before today, we would only specify If0 via a definitional interpreter in Lait. However, now we can specify it much easier, by the following pair of rules:
\[ \dfrac{e_1 \Downarrow v_1 \quad v_1 = 0 \quad e_2 \Downarrow v}{\verb|If0 | e_1\ e_2\ e_3 \Downarrow v} \qquad \dfrac{e_1 \Downarrow v_1 \quad v_1 \neq 0 \quad e_3 \Downarrow v}{\verb|If0 | e_1\ e_2\ e_3 \Downarrow v} \]
In English first rule states that If0 e1 e2 e3 evaluates to v whenever e1 evaluates to v1, v1 = 0, and e2 evaluates to v.
Similarly, the second rule states that If0 e1 e2 e3 evaluates to v whenever e1 evaluates to v1, v1 != 0, and e3 evaluates to v.
Arbitrary
Because inference rules are used to define arbitrary relations, they can define the semantics of programs that don’t nicely correspond to an interpreter in Lait.
For example, let’s extend CalcLang again with a construct for “give me an arbitrary number”:
e ::= ... | Arb
The semantics is that Arb could evaluate to anything. While making this precise in Lait is very difficult, let’s see how we can specify such an idea using an inference rule:
\[ \dfrac{}{\verb|Arb| \Downarrow v} \]
That’s it! This gives us a nondeterministic semantics.
Question. What do the derivation trees for \(\verb|AddE | (\verb|NumE |42)\ \verb|Arb| \Downarrow v\) look like? What can they evaluate to?
Question. Even though Arb seems like anything is possible and thus all
understanding about our programs is lost, this is not necessarily the case.
Suppose we also had MulE. Then what is a program that uses Arb but nevertheless has a deterministic semantics?