Normalisation
How the kernel computes a term to its normal form by evaluating it and reading the value back, how far it computes with unknowns, and how normal forms decide equality.
Imagine stepping into a function call, but instead of opening a new window you replace the call with its body. Start with twice add2 m. You don't need a value for m to simplify the two calls around it. Step forward once per substitution, or press Play.
Step 1 of 7: Start inside the function
let twice = \(f : Natural -> Natural) (x : Natural) -> f (f x)
let add2 = \(n : Natural) -> n + 2
in \(m : Natural) -> twice add2 mFull example and walkthrough
let twice = \(f : Natural -> Natural) (x : Natural) -> f (f x)
let add2 = \(n : Natural) -> n + 2
in \(m : Natural) -> twice add2 mNormal form
\(m : Natural) -> m + 4- Start inside the function. The last line takes a number
m. Keep it as a name; only the body of the function will change. (lines 3:twice add2 m) - Give twice its first argument. In
twice add2, open the bodyf (f x). Replace the outerfand the innerfwithadd2to getadd2 (add2 x). The last argument,m, is still waiting. (lines 1:f (f x)) - Give that function its second argument. Pass
minto the waiting function. Only its input slot changes:add2 (add2 x)becomesadd2 (add2 m). The two calls stay put. (lines 1:f (f x)) - Expand only the inner call. The inner call takes
mas its argument. The body is nowadd2 (m + 2). The outeradd2has not run yet. (lines 2:n + 2) - Expand the outer call. This time the argument is the whole expression
m + 2. Put it in place ofninn + 2to get(m + 2) + 2. (lines 2:n + 2) - Add the literals. Combine the two known numbers to get
m + 4. The answer still depends onm; you have not assumed that it is zero. (lines 3:twice add2 m) - Read back. Keep the parameter and return the simplified body. This is the function printed by Normalize. These substitutions explain the answer; the kernel computes it by evaluation and read-back, not by animating text replacements. (lines 3:
\(m : Natural))
Computing with unknowns
The kernel does not edit the source. It evaluates the expression to a value, then reads the value back as a normal form. In this example it can simplify the body even though the parameter is unknown.
An eliminator applied to a variable cannot choose a case, so it stays in
the normal form. On k + 1, though, it runs its step once:
let double =
\(n : Natural) ->
Natural/elim
(\(_ : Natural) -> Natural)
0
(\(_ : Natural) (rest : Natural) -> rest + 2)
n
in \(k : Natural) -> double (k + 1)Normal form
\(k : Natural) ->
Natural/elim
(\(_ : Natural) -> Natural)
0
(\(_ rest : Natural) -> rest + 2)
k
+ 2The kernel knows the laws that let a computation go on with unknowns:
n + 0 and 0 + n are n, the literals of a sum are added, and an
eliminator computes on k + 1 as it does on a literal. It does not know
laws such as n * m = m * n: these are theorems, which
std:Arith proves.
Normal forms decide equality
The type checker considers two types equal when their normal forms are
equal, up to eta for functions, pairs and records with fields, and up to
the order of the fields. refl, the proof that a value equals itself,
therefore proves every equation whose two sides have the same normal form:
let eta =
\(g : Natural -> Natural) ->
(refl : (\(x : Natural) -> g x) === g)
let order = assert : { a = 1, b = True } === { b = True, a = 1 }
let sums = \(m : Natural) -> (refl : m + 2 + 2 === m + 4)
in { eta, order, sums }Type
{ eta : forall (g : Natural -> Natural) -> (\(x : Natural) -> g x) === g
, order : { a = 1, b = True } === { a = 1, b = True }
, sums : forall (m : Natural) -> m + 4 === m + 4
}There is no eta rule for the empty record: two variables of type {} are
not equal to the type checker. docs/design.md explains
why, and gives the whole equational theory.
assert : a === b holds when both sides have the same normal form. When
they do not, the error shows both:
let test = assert : 2 + 2 === 5
in testError E0015
error[E0015]: assertion failed
┌─ example.omg:1:12
│
1 │ let test = assert : 2 + 2 === 5
│ ^^^^^^^^^^^^^^^^^^^^ the two sides are not equal
│
= the left-hand side normalises to: 4
and the right-hand side to: 5
= an assertion holds when both sides have the same normal form
Normal forms of contracts
The contract library runs at compile time. The compiler evaluates a contract's term with the kernel, keeping the operations on words symbolic, so that the codecs, capabilities and layouts compute away and only the operations of the EVM remain (the pipeline).