Written by
Li Zhuohua
on
on
Notes on Type Theory
Here are some notes about type theory, mainly from the book Type Theory and Formal Proof: An Introduction.
\(\lambda\to\) (terms depending on terms) #
Definitions #
- Variables: \(V={x,y,z,\ldots}\)
- Type variables: \(\mathbb{V}={\alpha, \beta, \gamma, \ldots}\)
- Types: \(\mathbb{T}=\mathbb{V}\mid \mathbb{T}\to \mathbb{T}\)
- Pre-typed \(\lambda\)-terms: \(\Lambda_{\mathbb{T}}=V\mid (\Lambda_{\mathbb{T}}\Lambda_{\mathbb{T}})\mid (\lambda V:\mathbb{T}.\Lambda_{\mathbb{T}})\)
- Statement, declaration, context, judgement
- Statement: \(M:\sigma\), where \(M\in\Lambda_\mathbb{T}, \sigma\in\mathbb{T}\)
- Declaration: a statement with \(M\in V\)
- Context: \(\Gamma\), a list of declarations with different \(M\)
- Judgement: \(\Gamma \vdash M:\sigma\)
- \(\beta\)-Reduction (\(\to_{\beta}\)): \((\lambda x:\sigma.\; M)N \to_{\beta} M[x:=N]\)
- Derivation rules:
- \(\textit{(var)} \quad \Gamma \vdash x:\sigma \text{ if } x:\sigma \in \Gamma\)
- \(\textit(appl) \quad \dfrac{\Gamma \vdash M:\sigma\to\tau\qquad \Gamma \vdash N:\sigma}{\Gamma \vdash MN:\tau}\)
- \(\textit(abst) \quad \dfrac{\Gamma,x:\sigma \vdash M:\tau}{\Gamma \vdash \lambda x:\sigma.\; M:\sigma\to\tau}\)
Notes #
- Basically the same with untyped lambda calculus, except that we add types.
- The type for each variable is explicitly given in advance, aka Church typing.
- The type, if existing, is unique.
- Church-Rosser Theorem holds.
- \(\lambda\to\) is strongly normalizing, meaning that a well-typed term always reduced to a value. This also means \(\lambda\to\) is not Turing complete.
\(\lambda2\) (\(\lambda\to\) + terms depending on types) (polymorphisim, generic programming) #
Definitions #
- Variables: \(V={x,y,z,\ldots}\)
- Type variables: \(\mathbb{V}={\alpha, \beta, \gamma, \ldots}\)
- The type of all types: \(*\), e.g. \(\alpha:*\)
- Type-binder (\(\Pi\)-binder): The type of functions that convert an arbitrary type to a term of type, e.g. \(\lambda\alpha:*.\; \lambda x:\alpha.\; x:\Pi\alpha:*.\; \alpha\to\alpha\)
- Types: \(\mathbb{T}2=\mathbb{V}\mid(\mathbb{T}2\to\mathbb{T}2)\mid(\Pi\mathbb{V}:*.\; \mathbb{T}2)\)
- \(\lambda2\)-terms: \(\Lambda_{\mathbb{T}2}=V\mid (\Lambda_{\mathbb{T}2}\Lambda_{\mathbb{T}2})\mid (\Lambda_{\mathbb{T}2}\mathbb{T}2)\mid(\lambda V:\mathbb{T}2.\Lambda_{\mathbb{T}2})\mid(\lambda \mathbb{V}:*.\; \Lambda_{\mathbb{T}2})\)
- Statement, declaration, context, judgement
- Statement: either \(M:\sigma\) where \(M\in\Lambda_{\mathbb{T}2}, \sigma\in\mathbb{T}2\), or \(\sigma:*\), where \(\sigma\in\mathbb{T}2\)
- Declaration: a statement with \(M\in V\) or \(M\in\mathbb{V}\)
- Context: \(\Gamma\), a list of declarations with the restriction that all variables must be declared before they can be used
- Judgement: \(\Gamma \vdash M:\sigma\), or \(\Gamma \vdash \sigma:*\)
- \(\beta\)-Reduction (\(\to_{\beta}\)):
- (First order): \((\lambda x:\sigma.\; M)N\to_{\beta}M[x:=N]\)
- (Second order): \((\lambda \alpha:*.\; M)T\to_{\beta}M[\alpha:=T]\)
- Derivation rules:
- \(\textit{(var)} \quad \Gamma \vdash x:\sigma \text{ if } \Gamma \text{ is a } \lambda 2\text{-context and } x:\sigma \in \Gamma\)
- \(\textit(appl) \quad \dfrac{\Gamma \vdash M:\sigma\to\tau\qquad \Gamma \vdash N:\sigma}{\Gamma \vdash MN:\tau}\)
- \(\textit(abst) \quad \dfrac{\Gamma,x:\sigma \vdash M:\tau}{\Gamma \vdash \lambda x:\sigma.\; M:\sigma\to\tau}\)
- \(\textit(form) \quad \Gamma \vdash B:* \text{ if } \Gamma \text{ is a } \lambda2\text{-context, } B\in\mathbb{T}2 \text{ and all free type variables in } B \text{ are declared in } \Gamma\)
- \(\textit(appl_2) \quad \dfrac{\Gamma\vdash M:(\Pi\alpha:*.\; A) \qquad \Gamma \vdash B:*}{\Gamma\vdash MB:A[\alpha:=B]}\)
- \(\textit(abst_2) \quad \dfrac{\Gamma,\alpha:* \vdash M: A}{\Gamma\vdash \lambda\alpha:*.\; M:\Pi\alpha:*.\; A}\)
Notes #
- Comparing with \(\lambda\to\), \(\lambda2\) adds the type of all types (\(*\)), and \(\Pi\)-types
- \(*\) is used to introduce second-order \(\lambda\)-abstraction, e.g. \(\lambda \alpha:*.\; \lambda x:\alpha.\; x\)
- \(\Pi\)-type is used to represent the types of the second-order terms, e.g. \(\lambda \alpha:*.\; \lambda x:\alpha.\; x\; :\; \Pi\alpha: *.\; \alpha\to\alpha\)
- Two new derivation rules ((\(appl_2\)), (\(abst_2\))) capture second order abstraction and application rules
- Uniqueness of Types, Church-Rosser Theorem and Strong Normalization Theorem still hold
\(\lambda\underline{\omega}\) (\(\lambda\to\) + types depending on types) (type constructor) #
Definitions #
- Variables and type variables are unified: \(V={x,y,z,\ldots,\alpha, \beta, \gamma, \ldots}\)
- The type of all types: \(*\), e.g. \(\alpha:*\)
- The type of type constructors: \(*\to*\), e.g. \(\lambda\alpha:*.\; \alpha\to\alpha: :: *\to*\)
- Kinds: \(\mathbb{K}=*\mid(\mathbb{K}\to\mathbb{K})\)
- The type of all kinds: \(\Box\), e.g. \(*:\Box\), and \(*\to*:\Box\)
- Sort: either \(*\) or \(\Box\), denoted by \(s\)
- Terms and types are unified as expressions: \(\mathcal{E}=V\mid\Box\mid * \mid(\mathcal{E}\mathcal{E})\mid(\lambda V:\mathcal{E}.\; \mathcal{E})\mid(\mathcal{E}\to \mathcal{E})\)
- Statement, declaration, context, judgement
- Statement: \(A:B\), where \(A, B\in \mathcal{E}\)
- Declaration: no longer has such terminology, because context is defined in a different way
- Context: \(\Gamma\), it is well-formed if it can be constructed by derivation rules, i.e., there are \(A\) and \(B\) such that \(\Gamma \vdash A:B\)
- Judgement: \(\Gamma \vdash A:B\)
- \(\beta\)-Reduction: Natural thus omitted
- Derivation rules:
- \(\textit(sort) \quad \emptyset \vdash *:\Box\)
- \(\textit(var) \quad \dfrac{\Gamma \vdash A:s}{\Gamma, x:A\vdash x:A} \quad \text{if } x\notin\Gamma\)
- \(\textit(weak) \quad \dfrac{\Gamma \vdash A:B \qquad \Gamma \vdash C:s}{\Gamma, x:C\vdash A:B} \quad \text{if } x\notin\Gamma\)
- \(\textit(form) \quad \dfrac{\Gamma \vdash A:s \qquad \Gamma \vdash B:s}{\Gamma\vdash A\to B:s}\)
- \(\textit(appl) \quad \dfrac{\Gamma \vdash M:A\to B \qquad \Gamma \vdash N:A}{\Gamma\vdash MN:B}\)
- \(\textit(abst) \quad \dfrac{\Gamma, x:A \vdash M:B \qquad \Gamma \vdash A\to B:s}{\Gamma\vdash \lambda x:A.\; M:A\to B}\)
- \(\textit(conv) \quad \dfrac{\Gamma \vdash A:B \qquad \Gamma \vdash B’:s}{\Gamma\vdash A:B’} \quad \text{if } B=_{\beta}B’\)
Notes #
- Comparing with \(\lambda\to\), \(\lambda\underline{\omega}\) adds type constructors, a function that takes a type and outputs a new type. E.g. \(\lambda \alpha:*.\; \alpha\to\alpha\), which maps the type \(\alpha\) to the new type \(\alpha\to\alpha\)
- The type of type constructors is \(*\to*\). A type can be also regarded as a type constructor because it is “a function that takes nothing and outputs a type”.
- Since now we have a “super-type” \(*\to*\), we define kinds as above
- The type of all kinds is defined as \(\Box\)
- Unlike the previous two systems, types are not given beforehand as a fixed set. The permissibility of a type occurring in a judgement depends on whether we can formally derive it
- Uniqueness of Types, Church-Rosser Theorem and Strong Normalization Theorem still hold
\(\lambda P\) (\(\lambda\to\) + types depending on terms) (dependent types) #
Definitions #
- \(V\), \(*\), kinds, sorts are the same as those in \(\lambda\underline{\omega}\)
- Expressions: \(\mathcal{E}=V\mid\Box\mid * \mid(\mathcal{E}\mathcal{E})\mid(\lambda V:\mathcal{E}.\; \mathcal{E})\mid(\Pi V:\mathcal{E}.\; \mathcal{E})\)
- Statement, declaration, context, judgement are the same those in \(\lambda\underline{\omega}\)
- Derivation rules (Note that (sort), (var), (weak), (conv) are the same as those in \(\lambda\underline{\omega}\))
- \(\textit(sort) \quad \emptyset \vdash *:\Box\)
- \(\textit(var) \quad \dfrac{\Gamma \vdash A:s}{\Gamma, x:A\vdash x:A} \quad \text{if } x\notin\Gamma\)
- \(\textit(weak) \quad \dfrac{\Gamma \vdash A:B \qquad \Gamma \vdash C:s}{\Gamma, x:C\vdash A:B} \quad \text{if } x\notin\Gamma\)
- \(\textit(form) \quad \dfrac{\Gamma \vdash A:* \qquad \Gamma,x:A \vdash B:s}{\Gamma\vdash \Pi x:A.\; B:s}\)
- \(\textit(appl) \quad \dfrac{\Gamma \vdash M:\Pi x:A.\; B \qquad \Gamma \vdash N:A}{\Gamma\vdash MN:B[x:=N]}\)
- \(\textit(abst) \quad \dfrac{\Gamma, x:A \vdash M:B \qquad \Gamma \vdash \Pi x:A.\; B:s}{\Gamma\vdash \lambda x:A.\; M:\Pi x:A.\; B}\)
- \(\textit(conv) \quad \dfrac{\Gamma \vdash A:B \qquad \Gamma \vdash B’:s}{\Gamma\vdash A:B’} \quad \text{if } B=_{\beta}B’\)
Notes #
- Comparing with \(\lambda\underline{\omega}\), \(\lambda P\) introduces \(\Pi\)-types (in rule (form)), because \(x\) can appear in \(B\) as free variables.
- Since \(\lambda P\) does not have types depending on types, in \(\Pi x:A.\; B\), \(x\) must be a term so \(A\) can only have type \(*\)
- \(\lambda P\) is able to code minimal predicate logic, which has implication and universal quantification operations:
- \(\textit(\Rightarrow-elim) \quad \dfrac{A\Rightarrow B \qquad A}{B}\)
- \(\textit(\Rightarrow-intro) \quad \dfrac{\text{Assume }A\text{ then we can show } B\text{ holds}}{A\Rightarrow B}\)
- \(\textit(\forall-elim) \quad \dfrac{\forall_{x\in S} (P(x)) \qquad N\in S}{P(N)}\)
- \(\textit(\forall-intro) \quad \dfrac{\text{Let }x\in S\text{ then we can show } P(x)\text{ holds}}{\forall_{x\in S} (P(x))}\)
-
The coding of minimal predicate logic by means of the derivation rules of \(\lambda P\):
Minimal predicate logic The type theory of \(\lambda P\) (S\) is a set| (S:*\)| (A\) is a proposition| (A:*\)| (a\in S\)| (a:S\)| (p\) proves \(A\)| (p:A\)| (P\) is a predicate on \(S\)| (P:S\to *\)| (A\Rightarrow B\)| (A\to B\) (\(=\Pi x:A.\; B\))| (\forall_{x\in S}(P(x))\)| (\Pi x:S.\; Px\)| \(\Rightarrow-elim\))| \(appl\))| \(\Rightarrow-intro\))| \(abst\))| \(\forall-elim\))| \(appl\))| \(\forall-intro\))| \(abst\))| -
Term finding is decidable in \(\lambda\to\) and \(\lambda\underline{\omega}\), but undecidable in \(\lambda P\). This is understandable because \(\lambda P\) is equivalent to minimal predicate logic, thus according to the Church-Turing Undecidability Theorem (Entscheidungsproblem), there is no general method to prove or disprove an arbitrary theorem.
\(\lambda C\) (\(\lambda2 + \lambda\underline{\omega} + \lambda P\)) (types/terms depending on types/terms) #
Definitions #
- The only difference between \(\lambda P\) and \(\lambda C\) is the rule (form):
- \(\textit(form_{\lambda C}) \quad \dfrac{\Gamma \vdash A:s_1 \qquad \Gamma,x:A \vdash B:s_2}{\Gamma\vdash \Pi x:A.\; B:s_2}\)
Notes #
- By choosing the combinations of \(s_1\) and \(s_2\), we get 8 systems, and they form the \(\lambda\)-cube:
- Uniqueness of Types holds up to conversion, i.e., if \(\Gamma \vdash A:B_1\) and \(\Gamma \vdash A:B_2\), then \(B_1=_\beta B_2\)
- Church-Rosser Therorem and Strong Normalization Theorem hold
- Term finding is decidable in \(\lambda\to\) and \(\lambda\underline{\omega}\), but undecidable in all other systems