StlcThe Simply Typed Lambda-Calculus
- 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)
- variable binding
- substitution
Overview
- variables
- function abstractions
- application
t ::= x (variable)
| \x:T,t (abstraction)
| t t (application)
| true (constant true)
| false (constant false)
| if t then t else t (conditional)
Some examples:
- \x:Bool, x
- (\x:Bool, x) true
- \x:Bool, if x then false else true
- \x:Bool, true
- \x:Bool, \y:Bool, x
- (\x:Bool, \y:Bool, x) false true
- \f:Bool→Bool, f (f true)
- (\f:Bool→Bool, f (f true)) (\x:Bool, false)
Note that all functions are anonymous.
The types of the STLC include the base type Bool for boolean values and arrow types for functions.
T ::= Bool
| T → T For example:
- \x:Bool, false has type Bool→Bool
- \x:Bool, x has type Bool→Bool
- (\x:Bool, x) true has type Bool
- \x:Bool, \y:Bool, x has type Bool→Bool→Bool
(i.e., Bool → (Bool→Bool))
- (\x:Bool, \y:Bool, x) false has type Bool→Bool
- (\x:Bool, \y:Bool, x) false true has type Bool
What is the type of the following term?
\f:Bool→Bool, f (f true)
(1) Bool → (Bool → Bool)
(2) (Bool→Bool) → Bool
(3) Bool→Bool
(4) Bool
(5) none of the above
\f:Bool→Bool, f (f true)
How about this?
(\f:Bool→Bool, f (f true)) (\x:Bool, false)
(1) Bool→ (Bool → Bool)
(2) (Bool→Bool) → Bool
(3) Bool→Bool
(4) Bool
(5) none of the above
(\f:Bool→Bool, f (f true)) (\x:Bool, false)
Inductive ty : Type :=
| Ty_Bool : ty
| Ty_Arrow : ty → ty → ty.
| Ty_Bool : ty
| Ty_Arrow : ty → ty → ty.
Inductive tm : Type :=
| tm_var : string → tm
| tm_app : tm → tm → tm
| tm_abs : string → ty → tm → tm
| tm_true : tm
| tm_false : tm
| tm_if : tm → tm → tm → tm.
| tm_var : string → tm
| tm_app : tm → tm → tm
| tm_abs : string → ty → tm → tm
| tm_true : tm
| tm_false : tm
| tm_if : tm → tm → tm → tm.
Declare Custom Entry stlc.
Notation "<{ e }>" := e (e custom stlc at level 99).
Notation "( x )" := x (in custom stlc, x at level 99).
Notation "x" := x (in custom stlc at level 0, x constr at level 0).
Notation "S -> T" := (Ty_Arrow S T) (in custom stlc at level 50, right associativity).
Notation "x y" := (tm_app x y) (in custom stlc at level 1, left associativity).
Notation "\ x : t , y" :=
(tm_abs x t y) (in custom stlc at level 90, x at level 99,
t custom stlc at level 99,
y custom stlc at level 99,
left associativity).
Coercion tm_var : string >-> tm.
Notation "'Bool'" := Ty_Bool (in custom stlc at level 0).
Notation "'if' x 'then' y 'else' z" :=
(tm_if x y z) (in custom stlc at level 89,
x custom stlc at level 99,
y custom stlc at level 99,
z custom stlc at level 99,
left associativity).
Notation "'true'" := true (at level 1).
Notation "'true'" := tm_true (in custom stlc at level 0).
Notation "'false'" := false (at level 1).
Notation "'false'" := tm_false (in custom stlc at level 0).
Notation "<{ e }>" := e (e custom stlc at level 99).
Notation "( x )" := x (in custom stlc, x at level 99).
Notation "x" := x (in custom stlc at level 0, x constr at level 0).
Notation "S -> T" := (Ty_Arrow S T) (in custom stlc at level 50, right associativity).
Notation "x y" := (tm_app x y) (in custom stlc at level 1, left associativity).
Notation "\ x : t , y" :=
(tm_abs x t y) (in custom stlc at level 90, x at level 99,
t custom stlc at level 99,
y custom stlc at level 99,
left associativity).
Coercion tm_var : string >-> tm.
Notation "'Bool'" := Ty_Bool (in custom stlc at level 0).
Notation "'if' x 'then' y 'else' z" :=
(tm_if x y z) (in custom stlc at level 89,
x custom stlc at level 99,
y custom stlc at level 99,
z custom stlc at level 99,
left associativity).
Notation "'true'" := true (at level 1).
Notation "'true'" := tm_true (in custom stlc at level 0).
Notation "'false'" := false (at level 1).
Notation "'false'" := tm_false (in custom stlc at level 0).
Now we need some notation magic to set up the concrete syntax, as
we did in the Imp chapter...
Definition x : string := "x".
Definition y : string := "y".
Definition z : string := "z".
Hint Unfold x : core.
Hint Unfold y : core.
Hint Unfold z : core.
Definition y : string := "y".
Definition z : string := "z".
Hint Unfold x : core.
Hint Unfold y : core.
Hint Unfold z : core.
Note that an abstraction \x:T,t (formally, tm_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...
Notation idB :=
<{\x:Bool, x}>.
Notation idBB :=
<{\x:Bool→Bool, x}>.
Notation idBBBB :=
<{\x:((Bool→Bool)->(Bool→Bool)), x}>.
Notation k := <{\x:Bool, \y:Bool, x}>.
Notation notB := <{\x:Bool, if x then false else true}>.
<{\x:Bool, x}>.
Notation idBB :=
<{\x:Bool→Bool, x}>.
Notation idBBBB :=
<{\x:((Bool→Bool)->(Bool→Bool)), x}>.
Notation k := <{\x:Bool, \y:Bool, x}>.
Notation notB := <{\x:Bool, if x then false else true}>.
Operational Semantics
- 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
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.
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 : tm → Prop :=
| v_abs : ∀ x T2 t1,
value <{\x:T2, t1}>
| v_true :
value <{true}>
| v_false :
value <{false}>.
| v_abs : ∀ x T2 t1,
value <{\x:T2, t1}>
| v_true :
value <{true}>
| v_false :
value <{false}>.
STLC Programs
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
(\x:Bool, if x then true else x) false to
if false then true else false by substituting false for the parameter x in the body of the function.
Here are some examples:
- [x:=true] (if x then x else false)
yields if true then true else false
- [x:=true] x yields true
- [x:=true] (if x then x else y) yields if true then true else y
- [x:=true] y yields y
- [x:=true] false yields false (vacuous substitution)
- [x:=true] (\y:Bool, if y then x else false)
yields \y:Bool, if y then true else false
- [x:=true] (\y:Bool, x) yields \y:Bool, true
- [x:=true] (\y:Bool, y) yields \y:Bool, y
- [x:=true] (\x:Bool, x) yields \x:Bool, x
Here is the definition, informally...
[x:=s]x = s
[x:=s]y = y if x ≠ y
[x:=s](\x:T, t) = \x:T, t
[x:=s](\y:T, t) = \y:T, [x:=s]t if x ≠ y
[x:=s](t1 t2) = ([x:=s]t1) ([x:=s]t2)
[x:=s]true = true
[x:=s]false = false
[x:=s](if t1 then t2 else t3) =
if [x:=s]t1 then [x:=s]t2 else [x:=s]t3
... and formally:
Fixpoint subst (x : string) (s : tm) (t : tm) : tm :=
match t with
| tm_var y ⇒
if eqb_string x y then s else t
| <{\y:T, t1}> ⇒
if eqb_string x y then t else <{\y:T, [x:=s] t1}>
| <{t1 t2}> ⇒
<{([x:=s] t1) ([x:=s] t2)}>
| <{true}> ⇒
<{true}>
| <{false}> ⇒
<{false}>
| <{if t1 then t2 else t3}> ⇒
<{if ([x:=s] t1) then ([x:=s] t2) else ([x:=s] t3)}>
end
where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc).
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.
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.
What is the result of the following substitution?
[x:=s](\y:T1, x (\x:T2, x))
(1) (\y:T1, x (\x:T2, x))
(2) (\y:T1, s (\x:T2, s))
(3) (\y:T1, s (\x:T2, x))
(4) none of the above
[x:=s](\y:T1, x (\x:T2, x))
Reduction
value v2 | (ST_AppAbs) |
(\x:T2,t1) v2 --> [x:=v2]t1 |
t1 --> t1' | (ST_App1) |
t1 t2 --> t1' t2 |
value v1 | |
t2 --> t2' | (ST_App2) |
v1 t2 --> v1 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.
Inductive step : tm → tm → Prop :=
| ST_AppAbs : ∀ x T2 t1 v2,
value v2 →
<{(\x:T2, t1) v2}> --> <{ [x:=v2]t1 }>
| ST_App1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 t2}> --> <{t1' t2}>
| ST_App2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 t2}> --> <{v1 t2'}>
| ST_IfTrue : ∀ t1 t2,
<{if true then t1 else t2}> --> t1
| ST_IfFalse : ∀ t1 t2,
<{if false then t1 else t2}> --> t2
| ST_If : ∀ t1 t1' t2 t3,
t1 --> t1' →
<{if t1 then t2 else t3}> --> <{if t1' then t2 else t3}>
where "t '-->' t'" := (step t t').
| ST_AppAbs : ∀ x T2 t1 v2,
value v2 →
<{(\x:T2, t1) v2}> --> <{ [x:=v2]t1 }>
| ST_App1 : ∀ t1 t1' t2,
t1 --> t1' →
<{t1 t2}> --> <{t1' t2}>
| ST_App2 : ∀ v1 t2 t2',
value v1 →
t2 --> t2' →
<{v1 t2}> --> <{v1 t2'}>
| ST_IfTrue : ∀ t1 t2,
<{if true then t1 else t2}> --> t1
| ST_IfFalse : ∀ t1 t2,
<{if false then t1 else t2}> --> t2
| ST_If : ∀ t1 t1' t2 t3,
t1 --> t1' →
<{if t1 then t2 else t3}> --> <{if t1' then t2 else t3}>
where "t '-->' t'" := (step t t').
What does the following term step to?
(\x:Bool→Bool, x) (\x:Bool, x) --> ???
(1) \x:Bool, x
(2) \x:Bool→Bool, x
(3) (\x:Bool→Bool, x) (\x:Bool, x)
(4) none of the above
(\x:Bool→Bool, x) (\x:Bool, x) --> ???
What does the following term step to?
(\x:Bool→Bool, x)
((\x:Bool→Bool, x) (\x:Bool, x))
--> ???
(1) \x:Bool, x
(2) \x:Bool→Bool, x
(3) (\x:Bool→Bool, x) (\x:Bool, x)
(4) (\x:Bool→Bool, x) ((\x:Bool→Bool, x) (\x:Bool, x))
(5) none of the above
(\x:Bool→Bool, x)
((\x:Bool→Bool, x) (\x:Bool, x))
--> ???
What does the following term normalize to?
(\x:Bool→Bool, x) notB true -->* ??? where notB abbreviates \x:Bool, if x then false else true
(1) \x:Bool, x
(2) true
(3) false
(4) notB
(5) none of the above
(\x:Bool→Bool, x) notB true -->* ??? where notB abbreviates \x:Bool, if x then false else true
What does the following term normalize to?
(\x:Bool, x) (notB true) -->* ???
(1) \x:Bool, x
(2) true
(3) false
(4) notB true
(5) none of the above
(\x:Bool, x) (notB true) -->* ???
Do values and normal forms coincide in the language presented so far?
(1) yes
(2) no
Contexts
Definition context := partial_map ty.
Typing Relation
Gamma x = T1 | (T_Var) |
Gamma ⊢ x ∈ T1 |
x ⊢> T2 ; Gamma ⊢ t1 ∈ T1 | (T_Abs) |
Gamma ⊢ \x:T2,t1 ∈ T2->T1 |
Gamma ⊢ t1 ∈ T2->T1 | |
Gamma ⊢ t2 ∈ T2 | (T_App) |
Gamma ⊢ t1 t2 ∈ T1 |
(T_True) | |
Gamma ⊢ true ∈ Bool |
(T_False) | |
Gamma ⊢ false ∈ Bool |
Gamma ⊢ t1 ∈ Bool Gamma ⊢ t2 ∈ T1 Gamma ⊢ t3 ∈ T1 | (T_If) |
Gamma ⊢ if t1 then t2 else t3 ∈ T1 |
Example typing_example_1 :
empty ⊢ \x:Bool, x \in (Bool → Bool).
Proof.
apply T_Abs. apply T_Var. reflexivity. Qed.
empty ⊢ \x:Bool, x \in (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' :
empty ⊢ \x:Bool, x \in (Bool → Bool).
Proof. auto. Qed.
empty ⊢ \x:Bool, x \in (Bool → Bool).
Proof. auto. Qed.
Example typing_example_2 :
empty ⊢
\x:Bool,
\y:Bool→Bool,
(y (y x)) \in
(Bool → (Bool → Bool) → Bool).
Proof.
apply T_Abs.
apply T_Abs.
eapply T_App. apply T_Var. apply update_eq.
eapply T_App. apply T_Var. apply update_eq.
apply T_Var. apply update_neq. intros Contra. discriminate.
Qed.
empty ⊢ \x:Bool→B, \y:Bool→Bool, \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 ⊢
\x:Bool,
\y:Bool,
(x 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).
¬(∃ S T,
empty ⊢ \x:S, x x \in T).
Which of the following propositions is not provable?
(1) y:Bool ⊢ \x:Bool, x \in Bool→Bool
(2) ∃ T, empty ⊢ \y:Bool→Bool, \x:Bool, y x \in T
(3) ∃ T, empty ⊢ \y:Bool→Bool, \x:Bool, x y \in T
(4) ∃ S, x:S ⊢ \y:Bool→Bool, y x \in (Bool→Bool)->S
Which of these is not provable?
(1) ∃ T, empty ⊢ \y:Bool→Bool→Bool, \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