PROGRAMMING PARADIGMS · PART 3 · SESSION 10, BLOCK 1

Static type systems

Block 1. Typing of a functional language

Léon Gondelman · Aalborg University Copenhagen · 2026

Mars Climate Orbiter, 1999

two trajectories towards Mars: the estimated one, at 226 km, and the actual one, at 57 km
figure 4 of the investigation board's report (NASA, 1999)

Lockheed Martin's program writes impulses in pound-force seconds

JPL's program reads them as newton-seconds

a factor 4.45: 57 km above Mars instead of 226 km, and the spacecraft is lost

what would have caught it?

one type for each unit: the mix becomes a type error, reported before the run

type checking

an operation applied to a value of the wrong type:

"5" + 37

do we get

  • a compile-time error? (OCaml, Rust, Go)
  • a runtime error? (Python, Julia)
  • the number 42? (PHP; Visual Basic with Option Strict Off)
  • the string "537"? (Java, Scala, Kotlin)
  • an out-of-bounds pointer? (C, C++)
  • something else?
a carrot+two potatoes

and what does this one give?

37 / "5"

types, intuitively

a type names a collection of values, together with the operations defined on them

typevaluesoperations
int\(\dots, -1, 0, 1, 2, \dots\)\(+\), \(\times\), \(<\)
booltrue, falseconditional, negation
string"", "a", "ab", \(\dots\)concatenation, length
\(\mathtt{int} \rightarrow \mathtt{bool}\)the functions from integers to Booleansapplication

a type error is an operation applied to a value on which it is not defined: length(42)1 + length42("abc")

types: a second reading

the intuitive reading is an approximation: a type is assigned to an expression, in the text of the program, before any value exists

  • if true then 1 else "a" evaluates to the integer 1, and OCaml gives it no type: typing judges the text, not one run
  • for a function, the type \(\mathtt{int} \rightarrow \mathtt{int}\) is a claim about every call; no single value shows it
  • a stack and the list that implements it hold the same values, and are two types: a type also hides a representation

“A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.” Pierce, 2002

“Type structure is a syntactic discipline for enforcing levels of abstraction.” Reynolds, 1983

static typing: a sound approximation

all programs 1 + true programs that never commit a type error if true then 1 else "a" accepted by the type checker if true then 1 else 2

can a checker accept exactly the blue set?

no: whether a program ever commits a type error is undecidable

a type checker is

  • decidable: it always answers
  • sound: what it accepts is in the blue set (when the type system is safe)
  • hence incomplete: it rejects some programs of the blue set

types in programming languages: a timeline

theory languages 1910 1940 1960 1970 1980 1990 2000 2010 1908Russella theory of types, to keepparadoxes out of logic 1940Churchthe simply typedλ-calculus 1957Fortraninteger andfloating-pointvariables 1967Simula 67classes and subclasses 1972–74Girard, Reynoldsthe polymorphicλ-calculus mid-1970sMLin Edinburgh LCF,with Milner’s types 1978Milnertype inference; well-typedprograms cannot go wrong 1989Wadler, Blotttype classes: overloadingwith inferred types 2004Java 5generic classesand methods 2012TypeScriptoptional typesfor JavaScript 2015Rust 1.0ownership,checked atcompile time

types as proofs: some dates

theory

practice

1969Howard, after Curry: a proposition is a type, a proof is a program

1988Coquand, Huet: the calculus of constructions

a kernel, far smaller than the system, checks the proof, whoever or whatever wrote it: trust goes to the kernel, the logic and the statement

1989Coq, now Rocq: a proof assistant on that calculus

2005the four-color theorem, checked in Coq (Gonthier, Werner)

2009CompCert: a compiler for a large subset of C, proved correct in Coq (Leroy)

2013Lean (de Moura)

2024AlphaProof: proofs found by a machine, checked by Lean

what type systems check today

  • absence of null dereference: in Kotlin and Swift, a value that may be absent has another type than a value that is present
  • memory safety without a garbage collector: in safe Rust, a value has one owner; a use after the value was freed is a compile-time error
  • absence of data races: in safe Rust, two threads that write the same data without synchronization do not compile (session 7)
  • theorems: in Lean and Rocq, a proposition is a type, and a program of that type is its proof (session 1)
val name: String? = lookup(id)

name.length    // rejected
name?.length   // accepted

Kotlin: name may be absent; a direct access is rejected, an access that tests for absence is accepted

let v = vec![1, 2, 3];
let first = &v[0];
drop(v);               // rejected
println!("{}", first);

Rust: first refers into v; freeing v while it is still referred to is rejected

let mut n = 0;
thread::scope(|s| {
  s.spawn(|| n += 1);
  s.spawn(|| n += 1);  // rejected
});

Rust: two threads increment the same n; the second one is rejected

theorem swap (p q : Prop)
    (h : p ∧ q) : q ∧ p :=
  ⟨h.2, h.1⟩

Lean 4: the statement “\(p \wedge q\) implies \(q \wedge p\)” as a type, and its proof as a program that the checker accepts

typing

if we now add two arbitrary expressions

e1 + e2

how can we decide whether this is legal and which operation to perform?

the answer is typing, a program analysis that binds types to each sub-expression, to rule out inconsistent programs

when (to check/rule out inconsistencies)?

some languages are dynamically typed: types are bound to values and are used at runtime

examples: Lisp, PHP, Python, Julia

other languages are statically typed: types are bound to expressions and are used at compile time

examples: C, C++, Java, OCaml, Rust, Go

example

consider the following C and Python code snippets:

int id(int num) {
  return num; }

void main(){
  printf("%d", id(42,42));}
def id(num):
  return num

print(id(42,42))

the C code fails at the compile-time (compilation error) error: too many arguments to function 'id'

the Python code compiles to the VM and fails at runtime (runtime error) TypeError: id() takes 1 positional argument but 2 were given

static typing

slogan (Milner, 1978)

well-typed programs cannot “go wrong”

goals of typing

  • type checking must be decidable
  • type checking must reject programs whose evaluation would apply an operation to a value of the wrong type;
    this is type safety
  • type checking must not reject too many non-absurd programs;
    the type system must be expressive

several solutions

  1. any sub-expression is annotated with a type
    int f(int x) { int y = ((x:int)+(1:int):int); ... }
    type checking is easy but this is unmanageable for the programmer
  2. only annotate variable declarations (C, C++, Java, etc.)
    int f(int x) { int y = x+1; return y; }
  3. annotate parameters and results; local variables are inferred (auto in C++ 11, var in Java 10)
    int f(int x) { var y = x+1; return y; }
  4. no annotation at all \(\Rightarrow\) type inference (OCaml, Haskell, etc.)
    fun x -> x+1

type safety, informally

a type system is safe when no program that it accepts applies, at run time, an operation to a value of the wrong type

safety is a property of the pair (type system, semantics): it is a theorem, and it has to be proved

does a program accepted by the compiler run without a type error?

in C, no: its type system is not safe; block 2 states the property formally

#include <stdio.h>
union { long n; char *s; } u;

int main(void) {
  u.n = 42;
  printf("%s\n", u.s);
  return 0;
}

accepted by the C compiler without a warning; the run reads a text at address 42 and crashes

roadmap for today

  • block 1: typing of a functional language
    • the simply typed \(\lambda\)-calculus: syntax, evaluation, typing rules
    • exercises by hand
    • mini-ML, and a type checker for it in OCaml (demo)
  • block 2: typing of an imperative language
    • the while language and its big-step semantics
    • typing of expressions and statements; type safety, stated
  • lab: semantics and typing of break and continue

Simply Typed Lambda Calculus

  1. recall of the \(\lambda\)-calculus (session 4): syntax, call-by-value evaluation, constants and operators
  2. types, and the syntax with type annotations
  3. the typing judgment and the typing rules
  4. exercises

syntax of the \(\lambda\)-calculus (session 4, recall 1/3)

\[ e ::= x \mid \lam{x} e \mid e\,e \]
  • Variables: \(x, y, z, f, g, \dots\)
  • Abstraction (function): \(\lam{x} e\)
  • Application: \(e_1\ e_2\)
  • Application associates to the left: \(a\ b\ c = (a\ b)\ c\)
  • Session 4 wrote a term \(t\); this session writes \(e\), for expression

call-by-value evaluation (session 4, recall 2/3)

a value is a term whose evaluation is finished

\[ v ::= \lam{x} e \]

a step \(e \step e'\) is defined by three rules (small-step operational semantics)

\[ \infr{e_1 \step e_1'}{e_1\, e_2 \step e_1'\, e_2}\;\rname{E-App1} \qquad \infr{e_2 \step e_2'}{v\, e_2 \step v\, e_2'}\;\rname{E-App2} \]
\[ \infr{}{(\lam{x} e)\, v \step \subst{e}{x}{v}}\;\rname{E-AppAbs} \]
\[ (\lam{x} x)\,(\lam{y} y) \step \lam{y} y \qquad\qquad (\lam{x} \lam{y} x)\, v_1\, v_2 \step (\lam{y} v_1)\, v_2 \step v_1 \]

constants and operators (recall 3/3)

\(e ::= x \mid \lam{x} e \mid e\,e \mid{}\)\(n \mid b \mid e~op~e\)\(v ::= \lam{x} e \mid{}\)\(n \mid b\)

with \(n\) an integer, \(b \in \{\mathtt{true}, \mathtt{false}\}\) and \(op \in \{+, <, \dots\}\)

\[ \infr{e_1 \step e_1'}{e_1~op~e_2 \step e_1'~op~e_2}\;\rname{E-Op1} \qquad \infr{e_2 \step e_2'}{v~op~e_2 \step v~op~e_2'}\;\rname{E-Op2} \]
\[ \infr{n = n_1 + n_2}{n_1 + n_2 \step n}\;\rname{E-Plus} \qquad \infr{b = (n_1 < n_2)}{n_1 < n_2 \step b}\;\rname{E-Less} \]
\[ 1 + 2 < 4 \step 3 < 4 \step \mathtt{true} \]

stuck terms, neither a value nor able to step: \(\quad 1 + \mathtt{true} \qquad 1~2 \qquad \mathtt{true}~(\lam{x} x)\)

typing

we introduce types, with the following abstract syntax

\(\tau\)\(::=\)type \(|\)\(\mathtt{int}\)integer values \(|\)\(\mathtt{bool}\)Boolean values \(|\)\(\tau \rightarrow \tau\)functions

simply-typed lambda calculus : syntax

one possibility: no annotation, the syntax as before

\[ e ::= n \mid b \mid x \mid \lam{x} e \mid e\,e \mid e~op~e \]

the other possibility: each binder annotated with its type

\(e ::= n \mid b \mid x \mid \lambda x\)\(\,\pmb{:}\,\pmb{\tau}\)\(\,.\,e \mid e\,e \mid e~op~e\)

evaluation ignores the annotations: \((\lam{x : \tau} e)\, v \step \subst{e}{x}{v}\), and \(\lam{x : \tau} e\) is a value

what are the implications of which version we choose?

typing judgment

the type of a variable is given by a typing environment \(\Gamma\)
(a function from variables to types)

the typing judgment is written

\[ \Gamma \vdash e : \tau \]

and reads “in typing environment \(\Gamma\), expression \(e\) has type \(\tau\)”

\(\Gamma, x:\tau\) is \(\Gamma\) in which \(x\) now has the type \(\tau\) (an earlier type of \(x\) is hidden)

inference rules define \(\Gamma \vdash e : \tau\)

typing rules: constants and variables

\[ \infr{}{\Gamma\vdash n:\mathtt{int}}\;\rname{T-Int} \qquad\quad \infr{}{\Gamma\vdash b:\mathtt{bool}}\;\rname{T-Bool} \qquad\quad \infr{x\in\mathrm{dom}(\Gamma)}{\Gamma\vdash x:\Gamma(x)}\;\rname{T-Var} \]

a constant has its type in every environment

a variable receives the type that the environment records for it

with \(\Gamma = \{ x \mapsto \mathtt{int} \}\)

  • \(\Gamma \vdash x : \mathtt{int}\), by (T-Var)
  • \(\Gamma \vdash y : \tau\) for no type \(\tau\): no rule applies, since \(y \notin \mathrm{dom}(\Gamma)\)

typing rules: operators

\[ \infr{\Gamma\vdash e_1:\mathtt{int} \qquad \Gamma\vdash e_2:\mathtt{int}}{\Gamma\vdash e_1 + e_2:\mathtt{int}}\;\rname{T-Plus} \hspace{3em} \infr{\Gamma\vdash e_1:\mathtt{int} \qquad \Gamma\vdash e_2:\mathtt{int}}{\Gamma\vdash e_1 < e_2:\mathtt{bool}}\;\rname{T-Less} \]
\[ \infr{\infr{\infr{}{\emptyset \vdash 1 : \mathtt{int}} \quad \infr{}{\emptyset \vdash 2 : \mathtt{int}}}{\emptyset \vdash 1 + 2 : \mathtt{int}} \quad \infr{}{\emptyset \vdash 4 : \mathtt{int}}}{\emptyset \vdash 1 + 2 < 4 : \mathtt{bool}} \]

which rule could give a type to \(1 + \mathtt{true}\)?

  • only (T-Plus) concludes on a sum
  • it requires \(\emptyset \vdash \mathtt{true} : \mathtt{int}\)
  • the only rule for \(\mathtt{true}\), (T-Bool), gives \(\mathtt{bool}\)

\(1 + \mathtt{true}\) has no type

typing rules: application

\[ \infr{\Gamma\vdash e_1: \tau_1 \rightarrow \tau_2 \qquad \Gamma\vdash e_2:\tau_1}{\Gamma\vdash e_1\, e_2:\tau_2}\;\rname{T-App} \]
  • the function part has a function type \(\tau_1 \rightarrow \tau_2\)
  • the argument has the type \(\tau_1\) that the function expects
  • the application receives the type \(\tau_2\) of the result

with \(\Gamma = \{ f \mapsto \mathtt{int} \rightarrow \mathtt{bool} \}\)

  • \(\Gamma \vdash f~1 : \mathtt{bool}\)
  • \(f~\mathtt{true}\) has no type: \(f\) expects an \(\mathtt{int}\), and the only type of \(\mathtt{true}\) is \(\mathtt{bool}\)
  • \(1~2\) has no type: the only type of \(1\) is \(\mathtt{int}\), not a function type

typing rules: abstraction

\[ \infr{\Gamma, x : \tau_1 \vdash e : \tau_2 }{\Gamma\vdash (\lam{x : \tau_1} e) : \tau_1 \rightarrow \tau_2}\;\rname{T-Abs} \]

in which environment is the body typed?

  • in \(\Gamma\) extended with the parameter
  • the annotation \(\tau_1\) gives the type of the parameter
  • the extension holds for the body only: the body is the scope of \(x\)
\[ \infr{\infr{\infr{}{x : \mathtt{int} \vdash x : \mathtt{int}} \qquad \infr{}{x : \mathtt{int} \vdash 1 : \mathtt{int}}}{x : \mathtt{int} \vdash x + 1 : \mathtt{int}}}{\emptyset \vdash \lam{x : \mathtt{int}} x + 1 : \mathtt{int} \rightarrow \mathtt{int}} \]

exercises (1/2): derivations

1. Write typing derivation of \( \emptyset \vdash (\lam{x \, : \mathtt{bool}} x)\, \mathtt{true} : \mathtt{bool}\)
and \( \emptyset \vdash \lam{x \, : \mathtt{int}} \lam{y \, : \mathtt{int}} x < y : \mathtt{int} \rightarrow \mathtt{int} \rightarrow \mathtt{bool}\)

2. Are \((\lam{x \, : \mathtt{bool}} x)\) and \((\lam{x \, : \mathtt{int}} x)\) the same programs?

3. Redesign typing rules for the \(\lambda\)-calculus without explicit type parameters. What is the type of the identity function \(\lam{x} x\) now?

4. Write typing derivation for \( \emptyset \vdash \lam{x} \lam{y} x\, y : \tau\), for a type \(\tau\) to find

exercises (2/2): properties of typing

5. Define type-annotation erasure function \(\mathtt{erase}(\cdot)\) and state its correctness theorem.

6. What does it mean for a program \(e\) to be well-typed in an empty context, i.e. \( \emptyset \vdash e : \tau\) for some type \(\tau\)?

7. Is there any context \(\Gamma\) and type \(\tau\) such that \(\Gamma \vdash x\, x : \tau\)?

Monomorphic typing of mini-ML

  1. mini-ML: the simply typed \(\lambda\)-calculus with pairs, local bindings and primitives
  2. its types, its typing rules, examples
  3. its values and its evaluation steps
  4. typing rules and type-checking algorithm; demo in OCaml

mini-ML

a function \(\efun{x}{e}\) carries no annotation, and a primitive stands before its argument: \(+\,(x, 1)\)

\(e\)\(::=\)\(x\)identifier
\(|\)\(c\)constant (1, 2, …, true, …)
\(|\)\(op\)primitive (\(+\), \(\times\), fst, …)
\(|\)\(\efun{x}{e}\)function
\(|\)\(e~e\)application
\(|\)\((e,e)\)pair
\(|\)\(\elet{x}{e}{e}\)local binding

monomorphic typing of mini-ML

we are given simple types, whose abstract syntax is

\(\tau\)\(::=\)\(\mathtt{int} ~|~ \mathtt{bool} ~|~ \dots\)base types
\(|\)\(\tau \rightarrow \tau\)functions
\(|\)\(\tau\times\tau\)pairs

typing judgment

the typing judgment that we are going to define is written

\[ \Gamma \vdash e : \tau \]

and is read “in the environment \(\Gamma\), the expression \(e\) has type \(\tau\)”

the environment \(\Gamma\) associates a type \(\Gamma(x)\) with every variable \(x\) that is free in \(e\)

typing rules

\[ \infr{x\in\mathrm{dom}(\Gamma)}{\Gamma\vdash x:\Gamma(x)} \qquad \infr{}{\Gamma\vdash n:\mathtt{int}}... \qquad \infr{}{\Gamma\vdash +:\mathtt{int}\times\mathtt{int}\rightarrow\mathtt{int}} ... \]
\[ \infr{\Gamma+x:\tau_1\vdash e:\tau_2}{\Gamma\vdash\efun{x}{e}:\tau_1\rightarrow\tau_2} \qquad \infr{\Gamma\vdash e_1:\tau'\rightarrow\tau \quad \Gamma\vdash e_2:\tau'}{\Gamma\vdash e_1~e_2:\tau} \]
\[ \infr{\Gamma\vdash e_1:\tau_1 \quad \Gamma\vdash e_2:\tau_2}{\Gamma\vdash(e_1,e_2):\tau_1\times\tau_2} \qquad \infr{\Gamma\vdash e_1:\tau_1 \quad \Gamma+x:\tau_1\vdash e_2:\tau_2}{\Gamma\vdash\elet{x}{e_1}{e_2}:\tau_2} \]

\(\Gamma+x:\tau\) is the environment \(\Gamma'\) defined by

\(\Gamma'(x) = \tau\) and \(\Gamma'(y)=\Gamma(y)\) otherwise

example: a derivation with a local binding

\[ \infr{\infr{\infr{\vdots \quad \infr{\vdots}{x:\mathtt{int}\vdash(x,1):\mathtt{int}\times\mathtt{int}}}{x:\mathtt{int}\vdash+(x,1):\mathtt{int}}}{\emptyset\vdash\efun{x}{+(x,1)}:\mathtt{int}\rightarrow\mathtt{int}} \quad \infr{\infr{}{\dots\vdash f:\mathtt{int}\rightarrow\mathtt{int}} \quad \infr{}{\dots\vdash 2:\mathtt{int}}}{f:\mathtt{int}\rightarrow\mathtt{int}\vdash f~2:\mathtt{int}}}{\emptyset\vdash\elet{f}{\efun{x}{+(x,1)}}{f~2}:\mathtt{int}} \]

untypable expressions

on the other hand, we cannot type the program \(1~2\)

\(\Gamma\vdash{}\)\(1:\tau'\rightarrow\tau\)\(\qquad \Gamma\vdash 2:\tau'\)
\(\Gamma\vdash 1~2:\tau\)

nor the program \(\efun{x}{x~x}\)

\[ \infr{\infr{\Gamma+x:\tau_1\vdash x:\tau_3\rightarrow\tau_2 \qquad \Gamma+x:\tau_1\vdash x:\tau_3}{\Gamma+x:\tau_1\vdash x~x:\tau_2}}{\Gamma\vdash\efun{x}{x~x}:\tau_1\rightarrow\tau_2} \]

since \(\tau_1=\tau_1\rightarrow\tau_2\) has no solution (types are finite)

several possible types

we can show that

\[ \emptyset\vdash\efun{x}{x} : \mathtt{int} \rightarrow \mathtt{int} \]

but also

\[ \emptyset\vdash\efun{x}{x} : \mathtt{bool} \rightarrow \mathtt{bool} \]

beware: this is not polymorphism

for a given occurrence of \(\efun{x}{x}\) we must choose a type

several possible types

thus, the term \(\elet{f}{\efun{x}{x}}{(f~1,f~\mathtt{true})}\) is not typable

because there is no type \(\tau\) such that

\[ f : \tau\rightarrow\tau \vdash (f~1, f~\mathtt{true}) : \tau_1\times\tau_2 \]

on the other hand,

\[ \left((\efun{x}{x}) ~ (\efun{x}{x})\right) ~ 42 \]

is typable (exercise: show it)

primitives

in particular, we cannot give a single type that is satisfactory for a primitive such as fst; we would have to choose between

\(\mathtt{int} \times \mathtt{int} \rightarrow \mathtt{int}\)
\(\mathtt{int} \times \mathtt{bool} \rightarrow \mathtt{int}\)
\(\mathtt{bool} \times \mathtt{int} \rightarrow \mathtt{bool}\)
\((\mathtt{int}\rightarrow\mathtt{int})\times \mathtt{int} \rightarrow \mathtt{int} \rightarrow \mathtt{int}\)
etc.

but we can instead give a typing rule

for the application of fst:

\[ \infr{\Gamma\vdash e:\tau_1\times\tau_2}{\Gamma\vdash \mathtt{fst}~e:\tau_1} \]

likewise for snd; \(\times\) has the type of \(+\)

mini-ML: values and evaluation steps

\[ v ::= c \mid op \mid \efun{x}{e} \mid (v, v) \]
\[ \infr{}{(\efun{x}{e})~v \step \subst{e}{x}{v}} \qquad \infr{}{\elet{x}{v}{e} \step \subst{e}{x}{v}} \]
\[ \infr{n = n_1 + n_2}{+~(n_1, n_2) \step n} \qquad \infr{}{\mathit{fst}~(v_1, v_2) \step v_1} \qquad \infr{}{\mathit{snd}~(v_1, v_2) \step v_2} \]

and one rule for each place where a step is taken inside a term:

  • in an application, the function part; then the argument, once the function part is a value
  • in a pair, the left component; then the right one, once the left one is a value
  • in a local binding, the bound expression

difference between typing rules and typing algorithm

when we type \(\efun{x}{e}\), how do we find the type to give to \(x\)?

this is exactly the difference between the typing rules, which define the ternary relation

\[ \Gamma\vdash e:\tau \]

and the type-checking algorithm which checks that a given expression \(e\) is indeed typable in a given environment \(\Gamma\)

demo: a type checker for mini-ML

mini_ml_typing.ml

  • the types typ and expression; the parameter of Fun carries its type
  • the environment \(\Gamma\) as a persistent map (session 2)
  • type_expr: one case for each typing rule
  • programs accepted with their type, programs rejected with an error

the slides that follow show the same code

the checker: types and expressions

only the parameters of functions are annotated

type typ =
  | Tint
  | Tarrow   of typ * typ
  | Tproduct of typ * typ
type expression =
  | Var   of string
  | Const of int
  | Op    of string
  | Fun   of string * typ * expression
  | App   of expression * expression
  | Pair  of expression * expression
  | Let   of string * expression * expression

Fun carries the type of its parameter

the checker: the environment

the environment \(\Gamma\) is a persistent map from names to types (session 2)

module Smap = Map.Make(String)

type env = typ Smap.t

Smap.add x ty env returns a new map and leaves env unchanged:   \(\Gamma, x : \tau\)

lookup and insertion in \(O(\log n)\): the map is a balanced tree

the checker: constants, variables, primitives, pairs

let rec type_expr env = function
  | Const _ -> Tint
  | Var x -> Smap.find x env
  | Op "+" -> Tarrow (Tproduct (Tint, Tint), Tint)
  | Pair (e1, e2) ->
      Tproduct (type_expr env e1, type_expr env e2)

one case of the function for each typing rule

where is a type checked against another one?   not here: these cases only compute a type

the checker: functions and local bindings

for a function, the type of the parameter is given

  | Fun (x, ty, e) ->
      Tarrow (ty, type_expr (Smap.add x ty env) e)

for a local binding, it is computed

  | Let (x, e1, e2) ->
      type_expr (Smap.add x (type_expr env e1) env) e2

the map is persistent: after the body, env is still the environment without x

the checker: applications

the only checks are in the application

  | App (e1, e2) ->
      begin match type_expr env e1 with
      | Tarrow (ty2, ty) ->
          if type_expr env e2 = ty2 then ty
          else failwith "error: argument of wrong type"
      | _ ->
          failwith "error: function expected"
      end

the function part must have an arrow type, and the argument the type it expects

the checker: a program accepted

let f = fun x:int -> +(x, 1) in f 2

# typeof
    (Let ("f",
      Fun ("x", Tint, App (Op "+", Pair (Var "x", Const 1))),
      App (Var "f", Const 2)));;
- : typ = Tint

the checker: two programs rejected

fun x:int -> x x

# typeof (Fun ("x", Tint, App (Var "x", Var "x")));;
Exception: Failure "error: function expected".

(+ 1) 2

# typeof (App (App (Op "+", Const 1), Const 2));;
Exception: Failure "error: argument of wrong type".

Appendix

  1. the typing rules of the simply typed \(\lambda\)-calculus, on one slide
  2. static and dynamic typing in one language: Java
  3. Mars Climate Orbiter: the report of the investigation board

typing rules of the simply typed \(\lambda\)-calculus

\[ \infr{}{\Gamma\vdash n:\mathtt{int}}\;\rname{T-Int} \qquad \infr{}{\Gamma\vdash b:\mathtt{bool}}\;\rname{T-Bool} \qquad \infr{x\in\mathrm{dom}(\Gamma)}{\Gamma\vdash x:\Gamma(x)}\;\rname{T-Var} \]
\[ \infr{\Gamma\vdash e_1:\mathtt{int} \quad \Gamma\vdash e_2:\mathtt{int}}{\Gamma\vdash e_1 + e_2:\mathtt{int}}\;\rname{T-Plus} \qquad \infr{\Gamma\vdash e_1:\mathtt{int} \quad \Gamma\vdash e_2:\mathtt{int}}{\Gamma\vdash e_1 < e_2:\mathtt{bool}}\;\rname{T-Less} \]
\[ \infr{\Gamma, x : \tau_1 \vdash e : \tau_2 }{\Gamma\vdash (\lam{x : \tau_1} e) : \tau_1 \rightarrow \tau_2}\;\rname{T-Abs} \qquad \infr{\Gamma\vdash e_1: \tau_1 \rightarrow \tau_2 \quad \Gamma\vdash e_2:\tau_1}{\Gamma\vdash e_1\, e_2:\tau_2}\;\rname{T-App} \]

static and dynamic typing in one language: Java

a language may use both static and dynamic typing

Object o = "abc";
Integer n = (Integer) o;

accepted by the compiler: an Object may be an Integer

the cast is checked at run time: ClassCastException

Object[] a = new String[1];
a[0] = 42;

accepted by the compiler: an array of String is an array of Object

the store is checked at run time: ArrayStoreException

each object carries its class at run time, so the check is possible

Mars Climate Orbiter: the report of the investigation board

Mars Climate Orbiter Mishap Investigation Board, Phase I Report, NASA, 10 November 1999

  • page 6: the signal was “last seen at approximately 09:04:52 UTC on Thursday, September 23, 1999”
  • page 13: the thruster data were “delivered in lb-sec instead of the specified and expected units of Newton-sec”
  • page 13: the computed effect on the trajectory was “low by a factor of 4.45 (1 pound force=4.45 Newtons)”; estimated closest approach 226 km, reconstructed 57 km, “judged too low for spacecraft survival”
  • page 16: “The Angular Momentum Desaturation (AMD) file contained the output data from the SM_FORCES software”

the report prints no source code

llis.nasa.gov/llis_lib/pdf/1009464main1_0641-mr.pdf