Concept 2 of 8

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.

Natural/elim computing sumTo 3, one successor at a time
1 / 6

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 3
Your result
Complete example · with imports
Full example and walkthrough
Normal form
let sumTo
    : Natural -> Natural
    = \n ->
        Natural/elim
          (\(_ : Natural) -> Natural)
          0
          (\(k : Natural) (acc : Natural) -> acc + k + 1)
          n
in  sumTo 3
Your result
Complete example · with imports

Normal form

6
  1. No recursion by name. In the body of sumTo, its own name is not in scope, so the function cannot call itself. It computes with Natural/elim. (lines 1–3: let sumTo)
  2. The case of zero. For n = 0, return the base argument 0. Skip the step. (lines 6: 0)
  3. The first successor. The step receives a number k and acc, the answer for that number, and gives the answer for k + 1. For k = 0: 0 + 0 + 1 = 1. (lines 7: acc + k + 1)
  4. The second successor. The step runs again on the answer for 1. For k = 1: 1 + 1 + 1 = 3. (lines 7: acc + k + 1)
  5. The third successor. For k = 2: 3 + 2 + 1 = 6. (lines 7: acc + k + 1)
  6. The end of the number. The input 3 is three steps from zero. The step runs three times and the result is 6. At 0 there 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:

Rejected
let loop : Natural -> Natural = \n -> loop n
in  loop 0
Your result
Complete example · with imports

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

Normal form
Natural/elim
  (\(_ : Natural) -> List Natural)
  ([] : List Natural)
  (\(k : Natural) (acc : List Natural) -> acc # [ k ])
  3
Your result
Complete example · with imports

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

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

Output

3
2
1

Here 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.