Advanced

A checker that refuses before running

The checker is the pass that makes the compiler honest. It reads the tree and answers with a list, and the list being empty is the only claim of a good program.

What you will see

Three numbered lines. Check before eval is the written order.

About 1 min of reading

Try It

Prove an empty list

1

Run the checker on a good tree

printf 'nodes 3 fails 0\n' | awk '{print ($4==0) ? "accepted" : "refused"}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
accepted. The empty list is the claim, which is the whole contract.
2

Run it on a bad tree

printf 'nodes 3 fails 2\n' | awk '{print $4}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
2. Two failures is a list of two places, which is the answer.
3

Write the pass order

printf '1 parse\n2 check\n3 eval\n' > /tmp/o.txt; cat /tmp/o.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Three numbered lines. Check before eval is the written order.

Data Flow

A tree to an empty list
1 The tree is walked once
↓
2 Each node claim is checked
↓
3 Failures are collected as a list
↓
4 An empty list is the accepted claim
Why this works
Why the checker is run before the evaluator

The evaluator answers with a value. A value from a bad program is a value nobody can trust, so the order is written.

The cost of a pass

A pass walks the whole tree. The walk is the cost, and the cost is why passes are counted rather than added freely.

Examples

The answer is a list

Empty means accepted. A list is a contract, and a contract is what a test checks rather than a print.

The checker does not run the program

It reads claims only. Reading without running is what lets a bad program be answered at all.

The checker runs on every change

A change that fails is refused. The refusal is the gate, and the gate is the same shape as the suite gate.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Types as a claim about values
Previous Lesson
Types as a claim about values
Next Lesson
Constant folding as a rewrite
Constant folding as a rewrite →