Léon Gondelman · Aalborg University Copenhagen · 2026
a statement has no value: its run changes a state
the state is an environment \(E\): the value of each variable
▶a ← 1;
▶b ← a + 1;
▶a ← a + b
before line 1: \(E = \emptyset\)
after line 1: \(E = \{\)\(a \mapsto 1\)\(\}\)
after line 2: \(E = \{a \mapsto 1,\ \)\(b \mapsto 2\)\(\}\)
after line 3: \(E = \{\)\(a \mapsto 3\)\(,\ b \mapsto 2\}\)
the semantics relates a statement and an environment to a new environment:
what does line 3 add 1 to?
a Boolean in the runs where \(y < 0\), an integer in the other runs
a typing environment \(\Gamma\) gives one type to each variable, for the whole program
x ← 1;
▶if y < 0 then x ← true else skip;
z ← x + 1
with \(\Gamma = \{x \mapsto \mathtt{int},\ y \mapsto \mathtt{int},\ z \mapsto \mathtt{int}\}\):
line 2 is rejected: \(\mathtt{true}\) is not of type \(\mathtt{int}\)
an assignment \(\eaff{x}{e}\) is accepted only if \(e\) has the type of \(x\): the value of a variable changes, its type does not
small-step semantics (block 1): one judgment for one step; a run is a sequence of steps
big-step semantics, or natural semantics: one judgment for a whole run, from the program to its result
| statement, run from \(\emptyset\) | big-step | small-step |
|---|---|---|
| \(\eaff{a}{1 + 2}\) | \(E' = \{a \mapsto 3\}\) | steps, then the run ends |
| \(\eaff{a}{1 + \mathtt{true}}\) | no \(E'\) | stuck: not finished, and no step applies |
| \(\ewhile{\mathtt{true}}{\eskip}\) | no \(E'\) | one more step, forever |
what is the result of a run that crashes, and of a run that never ends?
big-step gives no result to both; small-step tells them apart
block 2 uses big-step: one rule for each construct, read as an interpreter
break and continuea ← 0;
b ← 1;
while b < 100 do
b ← a + b;
a ← b - a
what does this program compute?
\(a\) and \(b\) are two consecutive Fibonacci numbers
the run ends in the environment
| \(e\) | \(::=\) | expression | |
| \(|\) | \(c\) | integer or Boolean constant | |
| \(|\) | \(x\) | variable | |
| \(|\) | \(e~op~e\) | binary operator (\(+\), \(<\), …) | |
| \(s\) | \(::=\) | statement | |
| \(|\) | \(\eaff{x}{e}\) | assignment | |
| \(|\) | \(\eif{e}{s}{s}\) | conditional | |
| \(|\) | \(\ewhile{e}{s}\) | loop | |
| \(|\) | \(s;~s\) | sequence | |
| \(|\) | \(\eskip\) | do nothing |
we seek to define a relation between some expression \(e\) and a value \(v\)
here, values are limited to integer and Boolean constants
| \(v\) | \(::=\) | value | |
| \(|\) | \(n\) | integer | |
| \(|\) | \(b\) | Boolean |
caveat: in most languages, values do not coincide with constants (Java)
in the environment \(E = \{ a \mapsto 34, ~ b \mapsto 55 \}\), the expression \(a+b\) has the value \(89\):
an environment \(E\) is a function from variables to values: it gives the value of each variable
the relation \(E,e \redv v\) reads “in the environment \(E\), the expression \(e\) has the value \(v\)”
the relation \(E,e \redv v\) is defined by inference rules:
with \(E = \{ a \mapsto 34, ~ b \mapsto 55 \}\), we have
such a tree is a proof: here, that the expression has the value 89
a statement changes the value of some variables, through assignments
its semantics is therefore a relation between two environments:
it reads “in the environment \(E\), the run of the statement \(s\) ends, in the environment \(E'\)”
\(\{a \mapsto 1\},\ \ \eaff{a}{a+1};\ \eaff{a}{a \times 2}\ \ \redv\ \ \{a \mapsto 4\}\) by (Seq), with \(E_1 = \{a \mapsto 2\}\)
\(\{a \mapsto 21\},\ \ \eif{a>0}{\eaff{a}{2\times a}}{\eskip}\ \ \redv\ \ \{a \mapsto 42\}\) by (IfTrue)
\(\{a \mapsto 0\},\ \ \ewhile{a<2}{\eaff{a}{a+1}}\ \ \redv\ \ \{a \mapsto 2\}\) by (WhileTrue) twice, then (WhileFalse)
with \(E = \{a\mapsto 21\}\), we have
the premises \(21 > 0\) and \(42 = 2 \times 21\) of the two operator rules are not drawn
for some expressions \(e\), there is no value \(v\) such that \(E,e\redv v\)
| \(1 + \mathtt{true}\) | no rule adds a Boolean |
for some statements \(s\), there is no environment \(E'\) such that \(E,s \redv E'\)
| \(\ewhile{\mathtt{true}}{\eskip}\) | the run never ends |
| \(\ewhile{(42 + 0)}{\eskip}\) | the value of the guard is not a Boolean |
| \(\ewhile{(42 / 0)}{\eskip}\) | the guard has no value |
break and continuewe introduce types, with the following abstract syntax
| \(\tau\) | \(::=\) | type | |
| \(|\) | \(\mathtt{int}\) | integer values | |
| \(|\) | \(\mathtt{bool}\) | Boolean values |
with \(\Gamma = \{a \mapsto \mathtt{int}\}\): \(\Gamma \vdash a + 1 < 5 : \mathtt{bool}\) holds; \(a + \mathtt{true}\) has no type
with \(\Gamma=\{a \mapsto \mathtt{int}\}\), we have
this derivation is a proof that a+1 is a well-typed expression
with \(\Gamma=\{a \mapsto \mathtt{int}\}\), two expressions have no type:
| \(b\) | \(b \notin \mathrm{dom}(\Gamma)\): (T-Var) does not apply |
| \(1+\mathtt{true}\) | (T-Plus) requires \(\Gamma \vdash \mathtt{true} : \mathtt{int}\) |
is anything lost?
no: \(1+\mathtt{true}\) has no value in any environment, and \(b\) has none where only \(a\) has a value
the safety theorem of this block states the link between having a type and having a value
a statement has no value, hence no type: it is well typed, or it is not
the judgment for statements is
it reads “in the typing environment \(\Gamma\), the statement \(s\) is well typed”
with \(\Gamma = \{a \mapsto \mathtt{int}\}\): \(\Gamma \vdash \eaff{a}{a+1}\) but not \(\Gamma \vdash \eaff{a}{\mathtt{true}}\): \(\mathtt{true}\) is not of type \(\mathtt{int}\)
with \(\Gamma = \{a \mapsto \mathtt{int}\}\): \(\Gamma \vdash \ewhile{a < 100}{\eaff{a}{a+a}}\) but not \(\Gamma \vdash \ewhile{a}{\eskip}\)
we have conditional at the level of statements:
we could add conditional in the expressions:
what is different, and what is common, in the two rules?
an extension of While with pairs: a constructor \((e, e)\), and the left and right components \(\fst{e}\) and \(\snd{e}\)
1. Define the pair types. Define typing rules for each of the expressions \((e_1, e_2)\), \(\fst{e}\) and \(\snd{e}\).
2. Consider the program
Explain why this program is rejected by the static type system.
an extension of While with two statements:
\(\mathtt{repeat}~s~\mathtt{until}~e\) and \(\mathtt{for}~x=e~\mathtt{to}~e~\mathtt{do}~s\)
3. Define the typing rule for the repeat loop \(\mathtt{repeat}~s~\mathtt{until}~e\).
4. Define the typing rule for the for loop \(\mathtt{for}~i=e_1~\mathtt{to}~e_2~\mathtt{do}~s\).
break and continuewell-typed programs cannot “go wrong”
- what does it mean formally?
- why does it matter in practice?
- how is type safety enforced?
Safety = Progress + Preservation
Progress: a well-typed program is not stuck: either it is a final result or it can take a step of evaluation
Preservation: if a well-typed program takes a step of evaluation, then the resulting program is also well-typed
a formal statement needs a mathematical definition of “step of evaluation”, “final result” and “well-typed”
a type system designed without a proof of safety can be flawed
a proof of safety needs three definitions, each one precise:
with \(\Gamma = \{a \mapsto \mathtt{int},\ b \mapsto \mathtt{bool}\}\):
| \(E_1 = \{a \mapsto 34,\ b \mapsto \mathtt{true}\}\) | agrees with \(\Gamma\) |
| \(E_2 = \{a \mapsto 34,\ b \mapsto \mathtt{true},\ c \mapsto 7\}\) | agrees with \(\Gamma\): \(\Gamma\) says nothing of \(c\) |
| \(E_3 = \{a \mapsto \mathtt{true},\ b \mapsto \mathtt{true}\}\) | does not agree: the value of \(a\) is not an integer |
| \(E_4 = \{a \mapsto 34\}\) | does not agree: \(b\) has no value |
definition
an environment \(E\) agrees with a typing environment \(\Gamma\), written \(\Gamma \vdash E\), when for every variable \(x \in \mathrm{dom}(\Gamma)\):
theorem (safety, expressions)
if \(\Gamma \vdash e : \tau\) and \(\Gamma \vdash E\), then there is a value \(v\) such that \(E, e \redv v\), and \(v\) is of type \(\tau\)
a well-typed expression has a value, and the value is of its type
with \(\Gamma = \{a \mapsto \mathtt{int}\}\) and \(E = \{a \mapsto 21\}\):
\(\Gamma \vdash 2 \times a : \mathtt{int}\) and \(E,\ 2 \times a \redv 42\), an integer
the operators are \(+\), \(-\), \(\times\), \(<\), \(>\), \(=\) on integers, each defined for all integers
theorem (safety, statements)
if \(\Gamma \vdash s\) and \(\Gamma \vdash E\) and \(E, s \redv E'\), then \(\Gamma \vdash E'\)
a well-typed statement preserves the agreement: when its run ends, every variable of \(\Gamma\) still has a value of its type
with \(\Gamma = \{a \mapsto \mathtt{int}\}\) and \(E = \{a \mapsto 21\}\):
\(E,\ \eif{a>0}{\eaff{a}{2\times a}}{\eskip} \ \redv\ \{a \mapsto 42\}\) and \(\Gamma \vdash \{a \mapsto 42\}\)
does every well-typed statement have a result?
this statement is well typed, with \(\Gamma = \emptyset\)
its run never ends: no \(E'\) exists
the theorem on statements covers only the runs that end
while true do skip
\(42 / 0\) has the type \(\mathtt{int}\), with \(/\) typed as \(+\), and it has no value
with division, the theorem on expressions fails
the type checkers of OCaml and Java accept this division too: the error is raised at run time
x ← 42 / 0
progress: “a well-typed program is never stuck”
progress speaks of steps: the judgment \(E, s \redv E'\) cannot state it
a remedy within big-step semantics:
the unwanted end of a run receives a result of its own
the theorem then states that no well-typed program ends with it
the lab does so for a break or a continue outside a loop
break and continuebreak and continuea static analysis establishes a property of all the runs of a program from its text alone, without running it
each of these properties is undecidable in general (by reduction from the halting problem), so an analysis that always answers and never misses an error rejects some correct programs
a type system can also check an effect of a statement, not only the type of a value: in the lab, that break and continue occur only inside a loop
break and continuetwo new statements:
\(s ::= \dots \mid\) \(\mathtt{break}\) \(\mid\) \(\mathtt{continue}\)
the run of a statement now ends with an outcome:
\(\omega ::= \mathtt{normal} \mid \mathtt{broken} \mid \mathtt{continued}\)
the judgment for statements becomes
\(E, s \redv (E',\)\(\omega\)\()\)
x ← 1;
break;
x ← 2
in which environment, and with which outcome, does the run from \(\{x \mapsto 0\}\) end?
to write: the rules of \(\mathtt{break}\) and \(\mathtt{continue}\), three rules for \(s_1; s_2\), four for \(\ewhile{e}{s}\)
break and continuethe judgment for statements carries a flag:
\((\Gamma,\)\(\ell\)\() \vdash s \qquad \ell \in \{\mathtt{true}, \mathtt{false}\}\)
\(\ell\) records whether \(s\) stands inside a loop
a whole program is checked with \(\ell = \mathtt{false}\)
to write: the typing rules of \(\mathtt{break}\), \(\mathtt{continue}\) and \(\ewhile{e}{s}\)
if x > 0 then break else skip
while true do
if x > 0 then break else skip
which of the two do the rules accept, with \((\{x \mapsto \mathtt{int}\}, \mathtt{false})\)?
the property that the typing guarantees
if \((\Gamma, \mathtt{false}) \vdash s\) and \(E, s \redv (E', \omega)\), then \(\omega = \mathtt{normal}\)
the other operators have a rule of the form of (Plus)
in Java (or Python, OCaml, etc.), a value may be an address, even if we do not have addresses among the literal constants of the language
int[] a = new int[4];
...
int[] b = a;
b[2] = 42;
...