What needs to have a type?
- Values
- Reduxes
We will define a type T->T
where the first T
is the domain and the second T
is the range. The ->
acts as a type constructor.
T1->T2
says if given a type T1 a function will return a type T2
T ::= T->T
The problem with this is that there are no base cases. So we need base types. Base types are concrete types.
Here we add Bool
T ::= T->T | Bool
We can now type a lambda as following:
It can also be applied for App:
Type Ascription
In general it is impossible to figure out a
Type Rules
The context of a term is a list of all the bound variables and their types.
A type rule for variables is as follows
Syntax
Type Safety
Proof of Progress
if
Proof.
- Assume combinators (no free variables), thus is not a term to consider as it it not bound and therefor not a combinator. - Lambdas are values and the theorem holds true. - Assume progress for and - Case 1:
is not a value because it is not lambda. Then (inductive hypothesis) and EApp1
applies. - Case 2:
is not a value because it is not lambda. Then (inductive hypothesis) and EApp2
applies. - Case 3:
and are both values, then BetaReduction
applies.
- Case 1:
Proof of Preservation
This involves the use of a few other lemmas.
Permutation Lemma
If
This says that the ordering of
In order to prevent this we prevent shadowing meaning a variable can't be used more than once. Languages like Haskell get around this by mangling variables and renaming them to be unique, this is called alpha reduction.
Proof.
By induction on terms:
- Case 1:
implies by type inversion. By definition of permutation so by TVar
.
- Case 2:
by type inversion. The induction hypothesis says permutation holds for , . Thus holds by TLambda
- Case 3:
implies there exists some and by type inversion. Induction hypothesis says permutation holds . Thus holds by TApp
.
Weakening Lemma
If
This essentially says typing is invariant if unique variables are added to the context.
Preservation of Types Under Substitution
If
This essentially says typing is invariant with respect to replacing a well-typed variable with a well-typed term of the same type.
Proof.
TVar
- t = z=>z:T
by type inversion - Case 1
: Since we know z:T and x:S then T=S and s:T - Case 2
: and s:T
TLambda
- t = λy: T2.t1
- T = T2 -> T1
by S=T2 by induction hypothesis
The Proof
uhhh...
Simple Extensions
There are three ways to enrich a language:
- Extension - Add new syntax, new values, new types and associated inference rules
- Derived Forms - Add new syntax and new types defined in terms of existing constructs
- Libraries - Add new functionality by using the language to define new resuable constructs
Prefer 2 over 3
Base Types
Extend the definition of
Unit
and ;
Unit
is a type with a single value.
Syntax:
Type Rules:
A conservative extension is an extension to a language that does not reference any other bits of the language. It does not affect how the language works.
Unit
in this case is a conservative extension.
Derived Form:
This assumes call by value evaluation semantics.
This is defining sequencing (;
) such that where
condition also requires that
Derived forms are evaluated by expanding the syntax with their definition
Inference Rules:
The above rule requires
This rule takes the unit production of the first rule and removes it to evaluate
Type Rules:
Let Binding
Syntax
Evaluation Rules
Type Rules
Derived Forms
let
Some Proofs
Define
Pairs
Syntax