Concept 3 of 8

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.

The term twice add2 m evaluated, and its value read back as a normal form
1 / 7

Step 1 of 7: Start inside the function

Original definitions and call
let twice = \(f : Natural -> Natural) (x : Natural) -> f (f x)
let add2 = \(n : Natural) -> n + 2
in  \(m : Natural) -> twice add2 m
Your result
Complete example · with imports
Full example and walkthrough
Normal form
let twice = \(f : Natural -> Natural) (x : Natural) -> f (f x)
let add2 = \(n : Natural) -> n + 2
in  \(m : Natural) -> twice add2 m
Your result
Complete example · with imports

Normal form

\(m : Natural) -> m + 4
  1. 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)
  2. Give twice its first argument. In twice add2, open the body f (f x). Replace the outer f and the inner f with add2 to get add2 (add2 x). The last argument, m, is still waiting. (lines 1: f (f x))
  3. Give that function its second argument. Pass m into the waiting function. Only its input slot changes: add2 (add2 x) becomes add2 (add2 m). The two calls stay put. (lines 1: f (f x))
  4. Expand only the inner call. The inner call takes m as its argument. The body is now add2 (m + 2). The outer add2 has not run yet. (lines 2: n + 2)
  5. Expand the outer call. This time the argument is the whole expression m + 2. Put it in place of n in n + 2 to get (m + 2) + 2. (lines 2: n + 2)
  6. Add the literals. Combine the two known numbers to get m + 4. The answer still depends on m; you have not assumed that it is zero. (lines 3: twice add2 m)
  7. 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:

Normal form
let double =
      \(n : Natural) ->
        Natural/elim
          (\(_ : Natural) -> Natural)
          0
          (\(_ : Natural) (rest : Natural) -> rest + 2)
          n
in  \(k : Natural) -> double (k + 1)
Your result
Complete example · with imports

Normal form

\(k : Natural) ->
  Natural/elim
    (\(_ : Natural) -> Natural)
    0
    (\(_ rest : Natural) -> rest + 2)
    k
  + 2

The 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:

Type
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 }
Your result
Complete example · with imports

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:

Rejected
let test = assert : 2 + 2 === 5
in  test
Your result
Complete example · with imports

Error 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).