Chapter 2

Programming with a fixed menu of types

Pie has no way to declare a new datatype. It offers a fixed menu of types, collected in Table 2.1. Here we use the menu as a programmer would, building values, taking them apart and reading what Pie prints. Two facts met here shape everything else. An expression can get stuck on a variable that has no value, and a definition cannot call itself.

TypeFormationConstructorsEliminatorsι-rulesη
Uis-type 96; not in U (synth 206–208)the types below (synth, in U)none——
Nat97 / 205zero 246, add1 247–249, numerals 639–647 (all synth)which-Nat 250–261, iter-Nat 262–274, rec-Nat 275–289, ind-Nat 290–303120–181no
Atom132–133 / 304'a 352–355 (synth)none—no
Π, →98–131 / 209–245λ 663–684 (check)application 599–629110–118yes
Σ, Pair134–159 / 305–333cons 685–697 (check)car, cdr 334–351183–197yes
List161–163 / 408–410nil 698–705 (check), :: 411–414 (synth)ind-List 360–386, rec-List 387–407199–236no
Vec171–174 / 502–505vecnil 718–732, vec:: 733–747 (check)head 506–521, tail 522–538, ind-Vec 539–572308–359length zero only
Either175–178 / 573–576left, right 748–767 (check)ind-Either 577–590361–378no
=165–170 / 421–426same 706–717 (check)replace 427–444, trans 445–458, cong 459–475, symm 476–484, ind-= 485–501245–306no
Trivial160 / 356–357sole 358–359 (synth)none—yes
Absurd164 / 415–416noneind-Absurd 417–420never reduces (238–243)yes
Table 2.1: Pie’s fixed menu of types. Line numbers in the Formation, Constructors and Eliminators columns are in typechecker.rkt; in a pair of ranges separated by a slash, the first is the clause of is-type (the type is a type) and the second the clause of synth (the type is an element of U). “check” and “synth” say whether a constructor only checks against a given type or also synthesizes its own. The ι-rules column gives the do-* functions in normalize.rkt. The η column says whether read-back has an η-rule for the type; Table 8.2 in Chapter 8 lists them. On a first reading the Type, Constructors and Eliminators columns are enough: is-type and synth are explained in Chapter 9, the ι- and η-rules in Chapter 8.

2.1Atoms and natural numbers

An atom is a quoted name, of type Atom. A natural number is zero or (add1 n), of type Nat. Each top-level expression is printed as (the T e), its type and its normal form (Section 1.2). The program atoms.pie and its output:

'rugbrød
'pea-soup
(the Atom 'grønkål)
4
(add1 (add1 zero))
(the Atom 'rugbrød)
(the Atom 'pea-soup)
(the Atom 'grønkål)
(the Nat 4)
(the Nat 2)

An atom may contain only letters (char-alphabetic?) and hyphens (typechecker.rkt:856–866), so 'at0m fails with Atoms consist of letters and hyphens. (atom-digit.pie). Atom has no eliminator, so no Pie program can branch on which atom it was given.

Numerals are sugar. The checker expands a literal nn into nn add1s around zero (typechecker.rkt:639–647), and the printer folds such a chain back (resugar.rkt:17–25).

2.2Functions

The type (Π ((x A)) B) describes functions that take an x of type A and return a B, where B may mention x. When it does not, the type can be written (→ A B). Functions are built with λ. The parser also accepts Pi and ∏ for Π, -> for → and lambda for λ (parser.rkt:253–262, 267–284). The program functions.pie:

((the (→ Atom Atom Atom) (λ (food beverage) food)) 'olive)
(claim id (Π ((A U)) (→ A A)))
(define id (λ (A a) a))
(id Atom 'kale)
(id Atom)
(the (→ Atom
       Atom)
  (λ (beverage)
    'olive))
(the Atom 'kale)
(the (→ Atom
       Atom)
  (λ (a)
    a))

Functions of several arguments are curried. The checker turns a λ with two names into nested λs, and an application to several arguments into a chain of one-argument applications (typechecker.rkt:612–629, 677–684), so the first output is the function left after one argument. The printer merges nested λs and prints a Π whose name is unused as → (resugar.rkt:32–43).

The type of id is our first dependent function type, since the result type (→ A A) mentions the argument A, a type passed like any other value (Section 1.1). So (id Atom) has type (→ Atom Atom), and its value is (λ (a) a). Pie prints every value of a function type as a λ, whatever expression produced it (Chapter 8; Section 7.6 shows the names it invents).

2.3Eliminating Nat without a motive

A value is used by an eliminator. Pie has four for Nat; this section uses the three whose result type is fixed. Each takes a target (the number taken apart), a base (the answer for zero) and a step (how to reach the answer for (add1 n)). In their computation rules, the ι-rules, read a≡ba \equiv b as “aa and bb compute to the same thing” (Chapter 4 makes this precise as definitional equality):

  • which-Nat, case analysis: (which-Nat zero b s) ≡\equiv b, (which-Nat (add1 n) b s) ≡\equiv (s n);
  • iter-Nat, iteration: (iter-Nat zero b s) ≡\equiv b, (iter-Nat (add1 n) b s) ≡\equiv (s (iter-Nat n b s));
  • rec-Nat, primitive recursion: (rec-Nat zero b s) ≡\equiv b, (rec-Nat (add1 n) b s) ≡\equiv (s n (rec-Nat n b s)).

The program nat-elims.pie finds the predecessor of 17, adds two five times, and doubles 3:

(which-Nat 17 0 (λ (smaller) smaller))
(iter-Nat 5 0 (λ (x) (add1 (add1 x))))
(rec-Nat 3 0 (λ (n-1 almost) (add1 (add1 almost))))
(the Nat 16)
(the Nat 10)
(the Nat 6)

The typing rule for iter-Nat, read as in Section 1.4, is

Γ⊢t⇐NatΓ⊢b⇒BΓ⊢s⇐(→ B B)Γ⊢(iter-Nat t b s)⇒B  (IterNat) \dfrac{\Gamma \vdash \texttt{t} \Leftarrow \texttt{Nat} \qquad \Gamma \vdash \texttt{b} \Rightarrow \texttt{B} \qquad \Gamma \vdash \texttt{s} \Leftarrow \texttt{(}{\to}\ \texttt{B B)}}{\Gamma \vdash \texttt{(iter-Nat t b s)} \Rightarrow \texttt{B}} \; (\text{IterNat})

Only the base fixes B, whose type the code synthesizes (typechecker.rkt:262–274), so the result type cannot depend on the target; ind-Nat lifts this restriction (Chapter 3).

Addition iterates add1. The program plus.pie:

(claim + (-> Nat Nat Nat))
(define + (λ (n j) (iter-Nat n j (λ (sum) (add1 sum)))))
(+ 2 3)
(the (-> Nat Nat) (λ (j) (+ 2 j)))
(the (-> Nat Nat) (λ (n) (+ n 2)))
(the Nat 5)
(the (→ Nat
       Nat)
  (λ (j)
    (add1 (add1 j))))
(the (→ Nat
       Nat)
  (λ (n)
    (iter-Nat n
       (the Nat 2)
       (λ (sum)
         (add1 sum)))))

The last two outputs show what sets a dependent type checker apart from an interpreter. To print a function in normal form, Pie computes inside the λ, where j and n have no value. In (+ 2 j) the target of iter-Nat is the numeral 2, so the ι-rule fires twice and j rides along as the base. In (+ n 2) the target is the variable n, and iter-Nat cannot tell whether it is zero or an add1. The expression is stuck, a neutral term, and Pie keeps it as it is. Where a Scheme eval would report an unbound variable, Pie’s evaluator returns the stuck expression as a value (Section 7.4).

The name + has disappeared. Definitions are always unfolded (basics.rkt:477–488), by the δ-rule, written just “δ” later. The base prints as (the Nat 2), though we wrote no the, because the internal syntax, Core, stores the base of these eliminators with its type (basics.rkt:145–147), which a stuck elimination needs for its result (normalize.rkt:139–144; Section 7.1).

2.4Pairs, lists, Either and Trivial

Pairs. (Pair A D) is the type of pairs built with cons and taken apart with car and cdr. The program pairs.pie:

(car (the (Pair Atom Atom) (cons 'olive 'oil)))
(the (→ (Pair Nat Atom) (Pair Nat Atom)) (λ (p) p))
(the Atom 'olive)
(the (→ (Pair Nat Atom)
       (Pair Nat Atom))
  (λ (p)
    (cons (car p)
      (cdr p))))

The identity on pairs prints by the η-rule for pairs (Chapter 8).

Pair is the dependent pair type (Σ ((x A)) D) with a D that does not mention the first component x. The checker and the printer convert between the two (typechecker.rkt:134–140, 305–312; resugar.rkt:44–51). In dep-pair.pie the second component is a vector whose length is the first (Section 3.4):

(claim dp (Σ ((n Nat)) (Vec Atom n)))
(define dp (cons 2 (vec:: 'a (vec:: 'b vecnil))))
(car dp)
(cdr dp)
(the Nat 2)
(the (Vec Atom 2)
  (vec:: 'a
    (vec:: 'b vecnil)))

The claim says only “a vector of some length n”, yet (cdr dp) has type (Vec Atom 2), because synthesizing cdr replaces n in D by the car of the pair (typechecker.rkt:342–351).

Lists. (List E) has the constructors nil and (:: e es). Its non-dependent eliminator rec-List takes a base for nil and a step that receives the head, the tail, and the answer for the tail (lists.pie):

(claim snoc (Π ((E U)) (→ (List E) E (List E))))
(define snoc
  (λ (E es e) (rec-List es (:: e nil) (λ (x xs so-far) (:: x so-far)))))
(claim reverse (Π ((E U)) (→ (List E) (List E))))
(define reverse
  (λ (E es)
    (rec-List es (the (List E) nil) (λ (x xs so-far) (snoc E so-far x)))))
(reverse Nat (:: 1 (:: 2 (:: 3 nil))))
(the (List Nat)
  (:: 3
    (:: 2
      (:: 1 nil))))

The base of rec-List must synthesize its type. (:: e nil) does, since :: takes the element type from its head; nil does not, so reverse writes (the (List E) nil).

Either, Trivial and Absurd. (Either L R) has the constructors (left l) and (right r), and one eliminator, ind-Either, which takes a motive (Section 3.5). Trivial has one value, sole, and Absurd none; both serve logic (Chapter 5).

2.5No recursion

A Scheme programmer’s first reflex is a recursive function. In Pie it fails. The program loop.pie:

(claim loop (→ Nat Nat))
(define loop (λ (n) (loop n)))
Unknown variable loop 
  location...:
   loop.pie:3:21

A claim records the type of a name without making the name usable. The lookup of a variable’s type skips claims (basics.rkt:420–429), and define checks the body before it binds the name (typechecker.rkt:849–853; Section 7.5 and Section 9.5). A definition can use only names defined before it, never itself.

All repetition goes through the eliminators, which take a number or a list apart one constructor at a time. This is the discipline of Gödel’s System TKurt Gödel. Über eine bisher noch nicht benützte Erweiterung des finiten Standpunktes. Dialectica, 12(3–4):280–287, 1958 extended with dependent types, and it is why every Pie program terminates, by Martin-Löf’s normalization argumentPer Martin-Löf. An Intuitionistic Theory of Types: Predicative Part. In H. E. Rose and J. C. Shepherdson, editors, Logic Colloquium ’73, volume 80 of Studies in Logic and the Foundations of Mathematics, pages 73–118. North-Holland, Amsterdam, 1975 for a theory of this shape. The evaluator assumes termination without proving it; normalize.rkt:15–18 reads “[…] because Pie is total, it is acceptable to choose any order of evaluation.”Totality also makes checking terminate, since checking evaluates expressions. Agda, Rocq (formerly Coq), Lean 4 and Idris 2 allow recursive definitions and use a termination checker instead (optional in Idris 2).

2.6When Pie needs an annotation

The first errors a new user meets are about missing types.

  • The constructors λ, cons, nil, same, vecnil, vec::, left and right cannot find their own type. They receive it from a claim before define, an enclosing (the T …), or an argument position of a function whose type is known.
  • Pie must find the type by itself for a top-level expression, the function in an application, the base of which-Nat, iter-Nat, rec-Nat and rec-List, the target of car, cdr, ind-List and rec-List, and the arguments of trans and cong.
  • A bare constructor in such a position fails with Can't determine a type (typechecker.rkt:648–650). The fix is to wrap it in the.

A bare (λ (x) x) at the top level fails this way (lambda-top.pie). Applying it does not help, because Pie needs the type of the function before it looks at the argument. ((λ (x) x) 1) fails with the same message at redex.pie:2:1, the λ. With the function annotated, ((the (-> Nat Nat) (λ (x) x)) 1) prints (the Nat 1) (redex-the.pie). Unlike miniKanren, Pie has no unification to solve for a missing type. Each typing rule either receives a type or produces one, and when neither happens Pie asks for an annotation (Section 9.2).

2.7Holes

The expression TODO stands for a part not yet written. It checks against any type, so the program around it is still checked and run, and Pie reports each hole with what must go there (Chapter 6). The program hole.pie:

(claim twice (→ Nat Nat))
(define twice (λ (n) TODO))
(twice 3)
(the Nat TODO)
hole.pie:3:21: TODO:
 n : Nat
---------
 Nat

The value of (twice 3) is the hole itself. The report, printed after all the results, is laid out like an inference rule, with the variables in scope above the dashed line and the goal, the type of the missing expression, below it.