Types and Type Systems

March 1, 2024
All notes are expanded based on

Why Data Types?


Terminology


Types as Specifications


Sound Type System


Strongly Typed Language


Static vs Dynamic Types


Type Checking


Dynamic Type Checking


Static Type Checking


Type Declarations


Type Inference


Format of Type Judgments


Axioms - Constants (Monomorphic)


\(\textcolor{blue}{\Large\frac{}{\Gamma \: \vdash \: n \: : \: int}}\) (assuming \(\textcolor{blue}{n}\) is an integer constant)

\[ \textcolor{blue}{\frac{}{\Gamma \: \vdash \: \text{ true } \: : \: \text{ bool }}\qquad\qquad \frac{}{\Gamma \: \vdash \: \text{ false } \: : \: \text{ bool }}} \]

Axioms - Variables (Monomorphic Rule)


Notation: Let \(\textcolor{blue}{\Gamma(x) = \sigma}\) if \(\textcolor{blue}{x : \sigma \in \Gamma}\) Note: if such \(\textcolor{blue}{\sigma}\) exits, its unique

Variable axiom:

\[ \textcolor{blue}{\frac{}{\Gamma \: \vdash \: x \: : \: \sigma}} \qquad \qquad \text{ if } \textcolor{blue}{\Gamma(x) = \sigma}\]

Simple Rules - Arithmetic (Mono)


Primitive Binary operators \((\textcolor{blue}{\oplus \in \{+, -, *, ...\}}):\)

\[\textcolor{blue}{\large\frac{ \Gamma \: \vdash \: e_1 \: : \: \tau_1 \quad \Gamma \: \vdash \: e_2 \: : \: \tau_2 \quad (\oplus): \: \tau_1 \: \rightarrow \: \tau_2 \: \rightarrow \: \tau_3 }{\Gamma \: \vdash \: e_1 \: \oplus \: e_2 \: : \: \tau_3}}\]

Special case: Relations \((\textcolor{blue}{\sim \in \{<, >, =, <=, >=\}})\):

\[\textcolor{blue}{\large\frac{ \Gamma \: \vdash \: e_1 \: : \: \tau \quad \Gamma \: \vdash e_2 \: : \: \tau \quad (\sim) \: : \: \tau \: \rightarrow \: \tau \: \rightarrow \: \text{ bool } }{\Gamma \:\: \vdash \: e_1 \sim e_2 : \text{ bool }}}\]

\[ \text{For the moment, think } \textcolor{blue}{\tau} \text{ is int}\]

Example: \(\scriptsize{\{x : \text{int}\} \vdash x + 2 = 3 : \text{ bool}}\)


What do we need to show first?

What we need fot the left and right side?

How to finish?

And

Complete Proof (type derivation)

\[ \large{\text{Bin}\frac{ \text{Bin}\frac{ \text{Var}\frac{}{\{x \: : \: \text{int} \} \: \vdash \: x \: : \: \text{int}} \quad \text{Const}\frac{}{\{x \: : \: \text{int} \} \: \vdash \: 2 \: : \: \text{int}} }{\{x \: : \: \text{int} \} \: \vdash \: x + 2 \: : \: \text{int}} \quad \text{Const}\frac{}{\{x : \text{int} \} \vdash 3 : \text{int}} }{\{x : \text{int} \} \vdash x + 2 = 3 : \text{int}}} \]

Simple Rules - Booleans


Connectives

\[\textcolor{blue}{\large{\frac{\Gamma \: \vdash \: e_1 \: : \: \text{bool} \quad \Gamma \: \vdash \: e_2 \: : \: \text{bool}}{\Gamma \: \vdash \: e_1 \: \&\& \: e_2 \: : \: \text{ bool}}}}\]

\[\textcolor{blue}{\large{\frac{\Gamma \: \vdash \: e_1 \: : \: \text{bool} \quad \Gamma \: \vdash \: e_2 \: : \: \text{bool}}{\Gamma \: \vdash \: e_1 \: || \: e_2 \: : \: \text{ bool}}}}\]

Type Variables in Rules


\[\textcolor{blue}{\frac{\Gamma \: \vdash \: e_1 \: :\text{ bool } \quad \Gamma \: \vdash \: e_2 \: : \: \tau \quad \Gamma \: \vdash \: e_3 \: : \: \tau}{\Gamma \: \vdash \: (\text{if } e_1 \text{ then } e_2 \text{ else } e_3) \: \tau}}\]

Example derivation: if-then-else:


\(\large{\textcolor{blue}{\Gamma = \{x : \text{int, int\_of\_float : float } \rightarrow \text{ int, y : float }\}}}\)