StlcThe Simply Typed Lambda-Calculus

Our job for this chapter: Formalize a small functional language and its type system.
Language: The simply typed lambda-calculus (STLC).
  • A small subset of Coq's built-in functional language...
  • ...but we'll use different informal syntax (to avoid confusion, and for consistency with standard treatments)
Main new technical challenges:
  • variable binding
  • substitution

Overview

Begin with some set of base types (here, just Bool)
Add:
  • variables
  • function abstractions
  • application
Informal concrete syntax:

       t ::= x variable
           | \x:T.t abstraction
           | t t application
           | tru constant true
           | fls constant false
           | test t then t else t conditional
The \ symbol in a function abstraction \x:T.t is generally written as a Greek letter "lambda" (hence the name of the calculus). The variable x is called the parameter to the function; the term t is its body. The annotation :T specifies the type of arguments that the function can be applied to.

Some examples:
  • \x:Bool. x
    The identity function for booleans.
  • (\x:Bool. x) tru
    The identity function for booleans, applied to the boolean tru.
  • \x:Bool. test x then fls else tru
    The boolean "not" function.
  • \x:Bool. tru
    The constant function that takes every (boolean) argument to tru.

  • \x:Bool. \y:Bool. x
    A two-argument function that takes two booleans and returns the first one. (As in Coq, a two-argument function is really a one-argument function whose body is also a one-argument function.)
  • (\x:Bool. \y:Bool. x) fls tru
    A two-argument function that takes two booleans and returns the first one, applied to the booleans fls and tru.
    As in Coq, application associates to the left -- i.e., this expression is parsed as ((\x:Bool. \y:Bool. x) fls) tru.
  • \f:BoolBool. f (f tru)
    A higher-order function that takes a function f (from booleans to booleans) as an argument, applies f to tru, and applies f again to the result.
  • (\f:BoolBool. f (f tru)) (\x:Bool. fls)
    The same higher-order function, applied to the constantly fls function.

Note that all functions are anonymous.
We'll see how to add named function declarations as "syntactic sugar" in the next chapter.

The types of the STLC include Bool for boolean values and arrow types for functions.

      T ::= Bool
          | TT
For example:
  • \x:Bool. fls has type BoolBool
  • \x:Bool. x has type BoolBool
  • (\x:Bool. x) tru has type Bool
  • \x:Bool. \y:Bool. x has type BoolBoolBool (i.e., Bool (BoolBool))
  • (\x:Bool. \y:Bool. x) fls has type BoolBool
  • (\x:Bool. \y:Bool. x) fls tru has type Bool

What is the type of the following term?

 \f:BoolBool. f (f tru)
(1) Bool (Bool Bool)
(2) (BoolBool) Bool
(3) BoolBool
(4) Bool
(5) none of the above
How about this?

 (\f:BoolBool. f (f tru)) (\x:Bool. fls)
(1) Bool (Bool Bool)
(2) (BoolBool) Bool
(3) BoolBool
(4) Bool
(5) none of the above

Syntax

We next formalize the syntax of the STLC.

Types


Inductive ty : Type :=
  | Bool : ty
  | Arrow : tytyty.

Terms


Inductive tm : Type :=
  | var : stringtm
  | app : tmtmtm
  | abs : stringtytmtm
  | tru : tm
  | fls : tm
  | test : tmtmtmtm.
Note that an abstraction \x:T.t (formally, abs x T t) is always annotated with the type T of its parameter, in contrast to Coq (and other functional languages like ML, Haskell, etc.), which use type inference to fill in missing annotations. We're not considering type inference here.

Some examples...

Open Scope string_scope.

Definition x := "x".
Definition y := "y".
Definition z := "z".
idB = \x:Bool. x

Notation idB :=
  (abs x Bool (var x)).
idBB = \x:BoolBool. x

Notation idBB :=
  (abs x (Arrow Bool Bool) (var x)).
idBBBB = \x:(BoolBool) (BoolBool). x

Notation idBBBB :=
  (abs x (Arrow (Arrow Bool Bool)
                      (Arrow Bool Bool))
    (var x)).

k = \x:Bool. \y:Bool. x

Notation k := (abs x Bool (abs y Bool (var x))).
notB = \x:Bool. test x then fls else tru

Notation notB := (abs x Bool (test (var x) fls tru)).

Operational Semantics

To define the small-step semantics of STLC terms...
  • We begin by defining the set of values.
  • Next, we define free variables and substitution. These are used in the reduction rule for application expressions.
  • Finally, we give the small-step relation itself.

Values

To define the values of the STLC, we have a few cases to consider.
First, for the boolean part of the language, the situation is clear: tru and fls are the only values. A test expression is never a value.

Second, an application is not a value: it represents a function being invoked on some argument, which clearly still has work left to do.

Third, for abstractions, we have a choice:
  • We can say that \x:T. t is a value only when t is a value -- i.e., only if the function's body has been reduced (as much as it can be without knowing what argument it is going to be applied to).
  • Or we can say that \x:T. t is always a value, no matter whether t is one or not -- in other words, we can say that reduction stops at abstractions.
Our usual way of evaluating expressions in Coq makes the first choice -- for example,
         Compute (fun x:bool ⇒ 3 + 4)
yields:
          fun x:bool ⇒ 7
Most real-world functional programming languages make the second choice -- reduction of a function's body only begins when the function is actually applied to an argument. We also make the second choice here.

Inductive value : tmProp :=
  | v_abs : x T t,
      value (abs x T t)
  | v_tru :
      value tru
  | v_fls :
      value fls.

STLC Programs

Finally, we must consider what constitutes a complete program.
Intuitively, a "complete program" must not refer to any undefined variables. We'll see shortly how to define the free variables in a STLC term. A complete program is closed -- that is, it contains no free variables.
(Conversely, a term with free variables is often called an open term.)

Having made the choice not to reduce under abstractions, we don't need to worry about whether variables are values, since we'll always be reducing programs "from the outside in," and that means the step relation will always be working with closed terms.

Substitution

Now we come to the heart of the STLC: the operation of substituting one term for a variable in another term. This operation is used below to define the operational semantics of function application, where we will need to substitute the argument term for the function parameter in the function's body. For example, we reduce
       (\x:Bool. test x then tru else x) fls
to
       test fls then tru else fls
by substituting fls for the parameter x in the body of the function.
In general, we need to be able to substitute some given term s for occurrences of some variable x in another term t. In informal discussions, this is usually written [x:=s]t and pronounced "substitute s for x in t."

Here are some examples:
  • [x:=tru] (test x then x else fls) yields test tru then tru else fls
  • [x:=tru] x yields tru
  • [x:=tru] (test x then x else y) yields test tru then tru else y
  • [x:=tru] y yields y
  • [x:=tru] fls yields fls (vacuous substitution)
  • [x:=tru] (\y:Bool. test y then x else fls) yields \y:Bool. test y then tru else fls
  • [x:=tru] (\y:Bool. x) yields \y:Bool. tru
  • [x:=tru] (\y:Bool. y) yields \y:Bool. y
  • [x:=tru] (\x:Bool. x) yields \x:Bool. x
The last example is very important: substituting x with tru in \x:Bool. x does not yield \x:Bool. tru! The reason for this is that the x in the body of \x:Bool. x is bound by the abstraction: it is a new, local name that just happens to be spelled the same as some global name x.

Here is the definition, informally...
       [x:=s]x = s
       [x:=s]y = y if xy
       [x:=s](\x:T11. t12) = \x:T11. t12
       [x:=s](\y:T11. t12) = \y:T11. [x:=s]t12 if xy
       [x:=s](t1 t2) = ([x:=s]t1) ([x:=s]t2)
       [x:=s]tru = tru
       [x:=s]fls = fls
       [x:=s](test t1 then t2 else t3) =
              test [x:=s]t1 then [x:=s]t2 else [x:=s]t3

... and formally:

Reserved Notation "'[' x ':=' s ']' t" (at level 20).

Fixpoint subst (x : string) (s : tm) (t : tm) : tm :=
  match t with
  | var x'
      if eqb_string x x' then s else t
  | abs x' T t1
      abs x' T (if eqb_string x x' then t1 else ([x:=s] t1))
  | app t1 t2
      app ([x:=s] t1) ([x:=s] t2)
  | tru
      tru
  | fls
      fls
  | test t1 t2 t3
      test ([x:=s] t1) ([x:=s] t2) ([x:=s] t3)
  end

where "'[' x ':=' s ']' t" := (subst x s t).

Technical note: Substitution becomes trickier to define if we consider the case where s, the term being substituted for a variable in some other term, may itself contain free variables. Since we are only interested here in defining the step relation on closed terms (i.e., terms like \x:Bool. x that include binders for all of the variables they mention), we can sidestep this extra complexity, but it must be dealt with when formalizing richer languages.

For example, using the definition of substitution above to substitute the open term s = \x:Bool. r, where r is a free reference to some global resource, for the variable z in the term t = \r:Bool. z, where r is a bound variable, we would get \r:Bool. \x:Bool. r, where the free reference to r in s has been "captured" by the binder at the beginning of t.
Why would this be bad? Because it violates the principle that the names of bound variables do not matter. For example, if we rename the bound variable in t, e.g., let t' = \w:Bool. z, then [x:=s]t' is \w:Bool. \x:Bool. r, which does not behave the same as [x:=s]t = \r:Bool. \x:Bool. r. That is, renaming a bound variable changes how t behaves under substitution.

What is the result of the following substitution?

       [x:=s](\y:T11.x (\x:T12. x))
(1) (\y:T11.x (\x:T12. x))
(2) (\y:T11.s (\x:T12. s))
(3) (\y:T11.s (\x:T12. x))
(4) none of the above

Reduction

value v2 (ST_AppAbs)  

(\x:T.t12) v2 --> [x:=v2]t12
t1 --> t1' (ST_App1)  

t1 t2 --> t1' t2
value v1
t2 --> t2' (ST_App2)  

v1 t2 --> v1 t2'
(plus the usual rules for conditionals).
This is call by value reduction: to reduce an application (t1 t2),
  • first reduce t1 to a value (a function)
  • then reduce the argument t2 to a value
  • then reduce the application itself by substituting t2 for the bound variable x in the body t1.
The ST_AppAbs rule is often called beta-reduction.

Reserved Notation "t1 '-->' t2" (at level 40).

Inductive step : tmtmProp :=
  | ST_AppAbs : x T t12 v2,
         value v2
         (app (abs x T t12) v2) --> [x:=v2]t12
  | ST_App1 : t1 t1' t2,
         t1 --> t1'
         app t1 t2 --> app t1' t2
  | ST_App2 : v1 t2 t2',
         value v1
         t2 --> t2'
         app v1 t2 --> app v1 t2'
  | ST_TestTru : t1 t2,
      (test tru t1 t2) --> t1
  | ST_TestFls : t1 t2,
      (test fls t1 t2) --> t2
  | ST_Test : t1 t1' t2 t3,
      t1 --> t1'
      (test t1 t2 t3) --> (test t1' t2 t3)

where "t1 '-->' t2" := (step t1 t2).
What does the following term step to?
    (\x:BoolBool. x) (\x:Bool. x) --> ???
(1) \x:Bool. x
(2) \x:BoolBool. x
(3) (\x:BoolBool. x) (\x:Bool. x)
(4) none of the above
What does the following term step to?
   (\x:BoolBool. x)
       ((\x:BoolBool. x) (\x:Bool. x))
   --> ???
(1) \x:Bool. x
(2) \x:BoolBool. x
(3) (\x:BoolBool. x) (\x:Bool. x)
(4) (\x:BoolBool. x) ((\x:BoolBool. x) (\x:Bool. x))
(5) none of the above
What does the following term normalize to?
   (\x:BoolBool. x) notB tru -->* ???
where notB abbreviates \x:Bool. test x then fls else tru
(1) \x:Bool. x
(2) tru
(3) fls
(4) notB
(5) none of the above
What does the following term normalize to?
  (\x:Bool. x) (notB tru) -->* ???
(1) \x:Bool. x
(2) tru
(3) fls
(4) notB tru
(5) none of the above

Do values and normal forms coincide in the language presented so far?
(1) yes
(2) no

Typing

Next we consider the typing relation of the STLC.

Contexts

Question: What is the type of the term "x y"?
Answer: It depends on the types of x and y!
I.e., in order to assign a type to a term, we need to know what assumptions we should make about the types of its free variables.
This leads us to a three-place typing judgment, informally written Gamma t \in T, where Gamma is a "typing context" -- a mapping from variables to their types.
Following the usual notation for partial maps, we write (X > T11, Gamma) for "update the partial function Gamma so that it maps x to T."

Definition context := partial_map ty.

Typing Relation

Gamma x = T (T_Var)  

Gamma ⊢ x ∈ T
(x > T11 ; Gamma) ⊢ t12 ∈ T12 (T_Abs)  

Gamma ⊢ \x:T11.t12 ∈ T11->T12
Gamma ⊢ t1 ∈ T11->T12
Gamma ⊢ t2 ∈ T11 (T_App)  

Gamma ⊢ t1 t2 ∈ T12
   (T_Tru)  

Gamma ⊢ tru ∈ Bool
   (T_Fls)  

Gamma ⊢ fls ∈ Bool
Gamma ⊢ t1 ∈ Bool    Gamma ⊢ t2 ∈ T    Gamma ⊢ t3 ∈ T (T_Test)  

Gamma ⊢ test t1 then t2 else t3 ∈ T
We can read the three-place relation Gamma t \in T as: "under the assumptions in Gamma, the term t has the type T."

Examples


Example typing_example_1 :
  emptyabs x Bool (var x) \in Arrow Bool Bool.
Proof.
  apply T_Abs. apply T_Var. reflexivity. Qed.
Note that, since we added the has_type constructors to the hints database, auto can actually solve this one immediately.

Example typing_example_1' :
  emptyabs x Bool (var x) \in Arrow Bool Bool.
Proof. auto. Qed.

More examples:
       empty ⊢ \x:A. \y:AA. y (y x)
             \in A → (AA) → A.

Example typing_example_2 :
  empty
    (abs x Bool
       (abs y (Arrow Bool Bool)
          (app (var y) (app (var y) (var x))))) \in
    (Arrow Bool (Arrow (Arrow Bool Bool) Bool)).
Proof with auto using update_eq.
  apply T_Abs.
  apply T_Abs.
  eapply T_App. apply T_Var...
  eapply T_App. apply T_Var...
  apply T_Var...
Qed.

       empty ⊢ \x:BoolB. \y:BoolBool. \z:Bool.
                   y (x z)
             \in T.

We can also show that some terms are not typable. For example, let's check that there is no typing derivation assigning a type to the term \x:Bool. \y:Bool, x y -- i.e.,
    ¬ T,
        empty ⊢ \x:Bool. \y:Bool, x y \in T.

Example typing_nonexample_1 :
  ¬ T,
      empty
        (abs x Bool
            (abs y Bool
               (app (var x) (var y)))) \in
        T.
Proof.
  intros Hc. destruct Hc as [T Hc].
  (* The clear tactic is useful here for tidying away bits of
     the context that we're not going to need again. *)

  inversion Hc; subst; clear Hc.
  inversion H4; subst; clear H4.
  inversion H5; subst; clear H5 H4.
  inversion H2; subst; clear H2.
  discriminate H1.
Qed.
Another nonexample:
    ¬( S T,
          empty ⊢ \x:S. x x \in T).

Which of the following propositions is not provable?
(1) y:Bool \x:Bool.x \in BoolBool
(2) T, empty \y:BoolBool. \x:Bool. y x \in T
(3) T, empty \y:BoolBool. \x:Bool. x y \in T
(4) S, x:S \y:BoolBool. y x \in S
Which of these is not provable?
(1) T, empty \y:BoolBoolBool. \x:Bool. y x \in T
(2) S T, x:S x x x \in T
(3) S U T, x:S, y:U \z:Bool. x (y z) \in T
(4) S T, x:S \y:Bool. x (x y) \in T