Advanced

Types as a claim about values

A type is a claim the compiler makes about a value. The claim is checked before anything runs, and checking before running is the reason a type error is cheaper than a runtime error.

What you will see

One line. The claim came from a rule, which is what makes it arguable.

About 1 min of reading

Try It

Refuse a mismatch

1

Write two claims

printf 'left number\nright text\n' > /tmp/c.txt; awk '{print $2}' /tmp/c.txt | sort -u | wc -l

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

What it prints
2. Two different claims is the mismatch to refuse.
2

Refuse with both names

printf 'left=number right=text\n' | awk '{print ($2==$3) ? "ok" : "refuse both claims named"}'

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

What it prints
refuse both claims named. The refusal names both, which is the whole answer.
3

Write the inference rule

printf 'rule=add then number\n' > /tmp/r.txt; cat /tmp/r.txt

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

What it prints
One line. The claim came from a rule, which is what makes it arguable.

Data Flow

A node to a checked claim
1 The children claims are read
↓
2 The node claim is inferred by rule
↓
3 The claim is written on the node
↓
4 A mismatch names the node and both claims
Why this works
Why the check is a separate pass

The parser answers without judging claims. A pass that judges is a pass that can be turned off, and turning off is a decision written down.

The one claim that must be refused

A value with two claims. Two claims is one value with two answers, and that is refused rather than chosen.

Examples

The claim is written on the node

The node carries what it answers with. A claim on the node is a claim the next pass can read.

A mismatch is refused at the node

The refusal names the node and both claims. Two claims and a place is the whole answer.

Inference is a rule, not a guess

The type comes from the children. Coming from the children is a rule, and a rule is what can be argued with.

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.

← Testing a compiler
Previous Lesson
Testing a compiler
Next Lesson
A checker that refuses before running
A checker that refuses before running →