Totality
Why a pure calculation finishes even though a running program can keep asking for input.
How can you add the numbers from 1 to 3 without writing a loop? Give
Natural/elim a starting value and a step. It takes the step once for each
successor in the input. The input determines how often the step runs, not how
much each step costs. This is about evaluating an expression,
not a bound on runtime or gas.
Step 1 of 6: No recursion by name
let sumTo
: Natural -> Natural
= \n ->
Natural/elim
(\(_ : Natural) -> Natural)
0
(\(k : Natural) (acc : Natural) -> acc + k + 1)
n
in sumTo 3Full example and walkthrough
let sumTo
: Natural -> Natural
= \n ->
Natural/elim
(\(_ : Natural) -> Natural)
0
(\(k : Natural) (acc : Natural) -> acc + k + 1)
n
in sumTo 3Normal form
6- No recursion by name. In the body of
sumTo, its own name is not in scope, so the function cannot call itself. It computes withNatural/elim. (lines 1–3:let sumTo) - The case of zero. For
n = 0, return the base argument0. Skip the step. (lines 6:0) - The first successor. The step receives a number
kandacc, the answer for that number, and gives the answer fork + 1. Fork = 0:0 + 0 + 1 = 1. (lines 7:acc + k + 1) - The second successor. The step runs again on the answer for
1. Fork = 1:1 + 1 + 1 = 3. (lines 7:acc + k + 1) - The third successor. For
k = 2:3 + 2 + 1 = 6. (lines 7:acc + k + 1) - The end of the number. The input
3is three steps from zero. The step runs three times and the result is6. At0there is no successor left to process. (lines 9:sumTo 3)
A definition cannot use itself
A definition sees the definitions before it, not itself. A function that calls itself is an unbound name:
let loop : Natural -> Natural = \n -> loop n
in loop 0Error E0001
error[E0001]: unbound name `loop`
┌─ example.omg:1:39
│
1 │ let loop : Natural -> Natural = \n -> loop n
│ ^^^^ not in scope
│
= help: bind it first with `let loop = ...`, or take it as an argument with `\(loop : ...) ->`
Rejecting a self-reference is a scope check, not by itself a proof of termination. For the connection between normalisation and using types as propositions, see dependent types and proofs. The trust reference distinguishes results for parts of the theory from assumptions about the full language.
Recursion on data
Each data type has an eliminator. The example below uses Natural/elim; the reference lists List/elim, Optional/elim, Bool/elim, Word/elim, Equal/elim and W/elim. W builds
well-founded trees, the general form of inductive data. The functions of
the standard library are built on the eliminators. This one takes 3 steps, appending the current k to the list each time:
Natural/elim
(\(_ : Natural) -> List Natural)
([] : List Natural)
(\(k : Natural) (acc : List Natural) -> acc # [ k ])
3Normal form
[ 0, 1, 2 ]Programs that run for a long time
Totality concerns evaluation. A value of type IO a describes an
interaction with the world, and omegyu run carries it out. IO/loop
repeats a step for as long as the step returns a new state:
IO/loop
3
(\(n : Natural) ->
if Natural/isZero n
then IO/pure (None Natural)
else do {
IO/putStr "${Natural/show n}\n";
IO/pure (Some (Natural/subtract 1 n))
})Output
3
2
1Here the state counts down to zero and the interpreter stops at None.
If the step always returned Some, the interpreter would keep going even
though each individual step finishes. See the IO loop rule.