Chapter 3

Motives, induction and indexed families

The eliminators of Section 2.3 have one fixed result type. In (iter-Nat t b s) the checker synthesizes a type BB for the base b, and BB is then the type of the whole expression, for every target. A dependent eliminator lifts this restriction: the type of the result may depend on the value of the target. The eliminator gets this type from an extra argument, the motive. This section shows how the motive determines the types of the base and the step, where Pie allows a type to be computed by recursion, and how Vec uses a natural number as an index that the checker computes while checking. In the rule below, typewriter letters such as t, mot, b and s stand for expressions, as in the appendix of The Little TyperDaniel P. Friedman and David Thrane Christiansen. The Little Typer. MIT Press, Cambridge, MA, 2018, and the elaborated Core output (⇝c\rightsquigarrow c) of the judgment is left out.

3.1From rec-Nat to ind-Nat

A motive answers one question: for this target, what type does the result have? For ind-Nat the motive is a function from Nat to U, written as an ordinary λ. If the motive is (λ (k) (Vec Atom k)), the result for target 3 has type (Vec Atom 3) and the result for target 0 has type (Vec Atom 0). The motive is a function from values to types, so the checker must apply it, and therefore evaluate it, while checking.

The rule fixes the type of every other argument in terms of the motive:

Γ⊢t⇐NatΓ⊢mot⇐(Π ((n Nat)) U)Γ⊢b⇐(mot zero)Γ⊢s⇐(Π ((n-1 Nat))(→ (mot n-1) (mot (add1 n-1))))Γ⊢(ind-Nat t mot b s)⇒(mot t)  (IndNat) \dfrac{\begin{array}{c} \Gamma \vdash \texttt{t} \Leftarrow \texttt{Nat} \qquad \Gamma \vdash \texttt{mot} \Leftarrow \texttt{(}\Pi\ \texttt{((n Nat)) U)} \\[1ex] \Gamma \vdash \texttt{b} \Leftarrow \texttt{(mot zero)} \\[1ex] \Gamma \vdash \texttt{s} \Leftarrow \begin{array}{l} \texttt{(}\Pi\ \texttt{((n-1 Nat))} \\ \quad\texttt{(}{\to}\ \texttt{(mot n-1) (mot (add1 n-1))))} \end{array} \end{array}}{\Gamma \vdash \texttt{(ind-Nat t mot b s)} \Rightarrow \texttt{(mot t)}} \; (\text{IndNat})

The base is the answer for zero. The step turns an answer for n-1, called the induction hypothesis, into an answer for (add1 n-1). The rule is implemented by this clause of synth, quoted from typechecker.rkt:290–303 (the paragraph “Reading the Racket” in Section 1.3 explains the notation):

290[`(ind-Nat ,tgt ,mot ,b ,s)
291 (go-on ((tgt-out (check Γ r tgt 'NAT))
292         (mot-out (check Γ r mot (PI 'n 'NAT (HO-CLOS (lambda (n) 'UNIVERSE)))))
293         (mot-val (go (val-in-ctx Γ mot-out)))
294         (b-out (check Γ r b (do-ap mot-val 'ZERO)))
295         (s-out (check
296                 Γ
297                 r
298                 s
299                 (Π-type ((n-1 'NAT)
300                          (ih (do-ap mot-val n-1)))
301                         (do-ap mot-val (ADD1 n-1))))))
302   (go `(the (,mot-out ,tgt-out)
303             (ind-Nat ,tgt-out ,mot-out ,b-out ,s-out))))]

Line 291 checks the target. Line 292 checks the motive against (Π ((n Nat)) U), built directly as a PI with a higher-order closure (HO-CLOS) that ignores its argument. Line 293 evaluates the motive, so that lines 294 and 299–301 can compute the expected types of the base and the step by applying it with do-ap. Line 302 is the synthesized type, a Core application (Section 2.3 and Section 7.1) written (,mot-out ,tgt-out). It is not a value: the motive is applied to the target only when someone evaluates the type. By contrast, replace and ind-=, the eliminators for equality of Section 4.4 and Section 4.6, read their result type back before returning it (typechecker.rkt:437–438 and 495–496).

Computation. do-ind-Nat (normalize.rkt:164–181) has the two ι-rules

(ind-Nat zero mot b s)≡b(ind-Nat (add1 n) mot b s)≡(s n (ind-Nat n mot b s)) \begin{gathered} \texttt{(ind-Nat zero mot b s)} \equiv \texttt{b}\\ \texttt{(ind-Nat (add1 n) mot b s)} \equiv \texttt{(s n (ind-Nat n mot b s))} \end{gathered}

(lines 167 and 168–170). On a neutral target it builds a neutral whose type is (mot t) (lines 171–181; Section 8.3).

The book’s first dependent example is peas, which builds a vector of peas of every length; Section 1.1 ran it. The motive alone fixes what the base and the step must be. In peas-todo.pie the claim and the motive of peas are kept, the base is replaced by TODO (Section 2.7) and the step by (λ (k ih) TODO), and Pie prints the two goals:

peas-todo.pie:7:6: TODO:
 n : Nat
--------------
 (Vec Atom 0)


peas-todo.pie:8:16: TODO:
  n : Nat
  k : Nat
 ih : (Vec Atom k)
-------------------
 (Vec Atom
   (add1 k))

The base must be a (Vec Atom 0), the motive applied to zero. The step receives ih : (Vec Atom k) and must produce a (Vec Atom (add1 k)). So vecnil and (vec:: 'pea ih) fit.

Why rec-Nat cannot do this. rec-Nat has no motive. Its base is synthesized, and the type found is used for the base, for ih and for the step’s result (typechecker.rkt:275–289). In peas-rec.pie the same step is given to rec-Nat with the base (the (Vec Atom zero) vecnil), and the program fails:

vec:: requires that the length have add1 on top, not zero 
  location...:
   peas-rec.pie:7:16

The base fixes the type (Vec Atom zero) for every step, and (vec:: 'pea ih) is not one: vec:: needs a length with add1 on top (column 16 of line 7 is the vec::).

3.2Types computed by recursion

A type can also be the result of a computation. The function Pick below returns Atom for zero and Nat for every successor. It is used as the motive of dep, so the type of (dep n) changes with n:

pick-dep.pie

(claim Pick (→ Nat U))
(define Pick (λ (n) (which-Nat n Atom (λ (k) Nat))))
(Pick 0)
(Pick 5)
(claim dep (Π ((n Nat)) (Pick n)))
(define dep (λ (n) (ind-Nat n Pick 'z (λ (k ih) k))))
(dep 0)
(dep 3)
(the U Atom)
(the U Nat)
(the Atom 'z)
(the Nat 2)

Pick is a which-Nat whose base Atom synthesizes (the U Atom) (typechecker.rkt:304), so its result type BB is U. In dep, the base 'z is checked against (Pick zero), which computes to Atom. The step (λ (k ih) k) is checked against (Π ((n-1 Nat)) (→ (Pick n-1) (Pick (add1 n-1)))), whose codomain computes to Nat even though n-1 is neutral, because which-Nat only needs the add1 on top. Any expression of the right type, such as the defined name Pick, can serve as a motive.

Where this is possible. Computing a type by eliminating a value is called large elimination. In Pie the precise situation is the following.

  • A type-valued function can be written by recursion with which-Nat, iter-Nat, rec-Nat and rec-List. Their base is synthesized, and the synthesized type may be U: Atom, Nat, Trivial and Absurd all synthesize U (typechecker.rkt:304, 205, 356–357, 415–416).
  • No ind-* eliminator can compute a type. Its result type is the motive applied to the targets (for ind-Absurd, the motive itself), and the motive is checked against a function type whose final codomain is U (for ind-Absurd, against U). To return types, the motive would have to return U itself, and U is not an element of U (typechecker.rkt:206–208; Section 5.4), nor is any other type whose elements are types, because a type former is in U only when its parts are (for Π, typechecker.rkt:226–235).

In the next program the motive returns U and is checked against (Π ((n Nat)) U). Its body U is then checked against U, which goes through the mode switch (typechecker.rkt:772–774) to synth on U, and that fails at the U inside the motive:

motive-U-nat.pie

(claim Vec-or-List (→ Nat U))
(define Vec-or-List
  (λ (n) (ind-Nat n (λ (k) U) (List Atom) (λ (n-1 ih) (Vec Atom n)))))
 U is a type, but it does not have a type.
  location...:
   motive-U-nat.pie:4:27

In motive-U-either.pie the same motive over Either fails in the same way. A consequence used in Chapter 5: type-valued case analysis is available on Nat and List, through their non-dependent eliminators, but not on Either or =, whose eliminators all take a motive (symm, trans and cong return proofs, not types).

3.3Lists and dependent results

Table 3.1: The eliminators that take a motive, as synth implements them. “Base”, “step”, “left” and “right” give the type each argument is checked against. ⇐\Leftarrow marks a target that is checked, ⇒\Rightarrow one that is synthesized. The result types of ind-Nat, ind-List, ind-Vec and ind-Either are returned as unevaluated Core applications of the motive; those of replace and ind-= are read back first. replace, ind-= and ind-Absurd are treated in Chapter 4 and Chapter 5.
(ind-Nat t mot b s), typechecker.rkt:290–303
targett ⇐\Leftarrow Nat
motive(Π ((n Nat)) U)
base(mot zero)
step(Π ((n-1 Nat)) (→ (mot n-1) (mot (add1 n-1))))
result(mot t)
(ind-List t mot b s), typechecker.rkt:360–386
targett ⇒\Rightarrow (List E)
motive(Π ((xs (List E))) U)
base(mot nil)
step(Π ((e E) (es (List E))) (→ (mot es) (mot (:: e es))))
result(mot t)
(ind-Vec n v mot b s), typechecker.rkt:539–572
targetsn ⇐\Leftarrow Nat, v ⇒\Rightarrow (Vec E n′), with n≡n′\texttt{n} \equiv \texttt{n}'
motive(Π ((k Nat) (es (Vec E k))) U)
base(mot zero vecnil)
step(Π ((k Nat) (e E) (es (Vec E k))) (→ (mot k es) (mot (add1 k) (vec:: e es))))
result(mot n v)
(ind-Either t mot l r), typechecker.rkt:577–590
targett ⇒\Rightarrow (Either L R)
motive(Π ((x (Either L R))) U)
left(Π ((x L)) (mot (left x)))
right(Π ((x R)) (mot (right x)))
result(mot t)
(replace t mot b), typechecker.rkt:427–444
targett ⇒\Rightarrow (= A from to)
motive(Π ((x A)) U)
base(mot from)
result(mot to)
(ind-= t mot b), typechecker.rkt:485–501
targett ⇒\Rightarrow (= A from to)
motive(Π ((to A) (p (= A from to))) U)
base(mot from (same from))
result(mot to t)
(ind-Absurd t mot), typechecker.rkt:417–420
targett ⇐\Leftarrow Absurd
motive⇐\Leftarrow U: a type, not a family
resultmot

ind-List follows the same pattern as ind-Nat, with one more argument in the step for the head of the list (typechecker.rkt:360–386; Table 3.1 gives the types). Unlike the Nat eliminators, whose target is checked against Nat (line 291), the target of ind-List is synthesized (line 361), because the element type E is read off the target’s type. A bare nil as target therefore fails with “Can’t determine a type” (ind-list-nil.pie; Section 2.6).

A function’s result type may depend on its input through a computation. list->vec turns a list into a vector whose length is the length of the list:

list-to-vec.pie

(claim length (Π ((E U)) (→ (List E) Nat)))
(define length
  (λ (E es) (rec-List es 0 (λ (e es len) (add1 len)))))
(claim list->vec (Π ((E U) (es (List E))) (Vec E (length E es))))
(define list->vec
  (λ (E es)
    (ind-List es
      (λ (xs) (Vec E (length E xs)))
      vecnil
      (λ (e es so-far) (vec:: e so-far)))))
(list->vec Atom (:: 'a (:: 'b (:: 'c nil))))
(the (Vec Atom 3)
  (vec:: 'a
    (vec:: 'b
      (vec:: 'c vecnil))))

The base vecnil is checked against (mot nil), that is (Vec E (length E nil)). The checker computes the length: the rec-List inside length meets nil and returns its base 0 (normalize.rkt:223), so vecnil fits. The step goal (mot (:: e es)) computes, by one ι-step of rec-List on :: (normalize.rkt:224–226), to add1 on top of the length in the type of so-far. That is exactly the type of (vec:: e so-far).

3.4Vec, an indexed family

(Vec E n) is a type when E is a type and n is a Nat (typechecker.rkt:171–174; in U when E is, 502–505). It is a family of types indexed by n: (Vec Atom 2) and (Vec Atom 3) are different types, and which constructor may be used depends on the index. The constructors are check-only, and they inspect the index as a value (typechecker.rkt:718–738). This is the clause for vec:::

733[`(vec:: ,h ,t)
734 (match (now tv)
735   [(VEC Ev (!! (ADD1 len-1)))
736    (go-on ((h-out (check Γ r h Ev))
737            (t-out (check Γ r t (VEC Ev len-1))))
738      (go `(vec:: ,h-out ,t-out)))]

(now tv) forces the expected type, and the pattern (!! (ADD1 len-1)) forces its length (Section 7.3); the clause for vecnil tests (!! 'ZERO) in the same way (line 720). The decision is taken on the length value: whatever expression the user wrote for the length has been evaluated, so an index such as (+ 1 1) is as good as 2. The tail is checked against (Vec E len-1), one less than the length (line 737). The length may also be open, as long as add1 is on top. head (typechecker.rkt:506–521) applies the same test to the synthesized type of its argument (line 510), so vec-index.pie accepts both (the (Vec Atom (+ 1 1)) (vec:: 'a (vec:: 'b vecnil))) and (λ (k v) (head v)) at the type (Π ((k Nat)) (→ (Vec Nat (add1 k)) Nat)). When the length is not known to be a successor, the corresponding clause of head (line 516) rejects the program:

head-open.pie

(claim f (Π ((n Nat)) (→ (Vec Nat n) Nat)))
(define f (λ (n v) (head v)))
Expected a Vec with add1 at the top of the length, got n 
  location...:
   head-open.pie:3:19

Here n is a neutral variable, so the checker cannot see an add1. The same check rejected the head of an empty vector in Section 1.1, where the length was zero.

ind-Vec. The eliminator for vectors takes the length as a separate first target and a motive of two arguments, the length and the vector (typechecker.rkt:539–572; Table 3.1).Inductive families such as Vec go back to Dybjer. Agda and Idris 2 provide dependent pattern matching, in which matching a vector against a cons pattern refines its length to a successor by unification. Pie has no unification, so every such refinement must be written into a motive.Peter Dybjer. Inductive Families. Formal Aspects of Computing, 6(4):440–465, 1994; and Thierry Coquand. Pattern Matching with Dependent Types. In Informal Proceedings of the 1992 Workshop on Types for Proofs and Programs, Båstad, Sweden, 1992 The length is checked at line 540 and the vector synthesized at line 542. Line 545 converts the two lengths, so the explicit length must agree with the vector’s own index. The motive type is built at 550–552 and the base type at 557, and the step type by the helper ind-Vec-step-type of the evaluator (normalize.rkt:324–329). As for ind-Nat, the synthesized type is a Core application, ((,mot-out ,len-out) ,vec-out) (line 563).

Appending two vectors shows how the choice of motive makes the types compute. With + recursing on its first argument, the motive (λ (k v) (Vec E (+ k m))) puts the index being eliminated first:

vec-append.pie

(claim + (→ Nat Nat Nat))
(define + (λ (n m) (iter-Nat n m (λ (almost) (add1 almost)))))
(claim vec-append
  (Π ((E U) (n Nat) (m Nat)) (→ (Vec E n) (Vec E m) (Vec E (+ n m)))))
(define vec-append
  (λ (E n m xs ys)
    (ind-Vec n xs
      (λ (k v) (Vec E (+ k m)))
      ys
      (λ (k h t so-far) (vec:: h so-far)))))
(vec-append Nat 2 1 (vec:: 1 (vec:: 2 vecnil)) (vec:: 3 vecnil))
(the (Vec Nat 3)
  (vec:: 1
    (vec:: 2
      (vec:: 3 vecnil))))

The base goal (mot zero vecnil) is (Vec E (+ zero m)), which computes to (Vec E m), the type of ys. The step goal (mot (add1 k) (vec:: h t)) computes to add1 on top of the type of so-far, so (vec:: h so-far) fits. If the claim says (Vec E (+ m n)) and the motive is (λ (k v) (Vec E (+ m k))), nothing computes, because iter-Nat is stuck on the neutral m, and already the base ys is rejected (vec-append-swap.pie):

Expected
  (Vec E
    (iter-Nat m
[…]
but given
  (Vec E m)
[…]
   vec-append-swap.pie:10:6

(+ m 0) and m are equal only propositionally, a distinction taken up in Chapter 4.

Pie never solves equations between indices. The checker evaluates the length and looks at its top constructor, and conversion compares two lengths by normalizing both (Chapter 8).

3.5Either

ind-Either has a motive over the sum and one function per constructor; there is no step, because Either is not recursive (typechecker.rkt:577–590; Table 3.1). The target is synthesized (line 578), the motive is checked at line 581 and the two branches at lines 583–584.

The book’s even-or-odd combines ind-Nat and ind-Either, and previews Chapter 4 and Chapter 5: Even and Odd are type-valued functions returning Σ-types of a witness and an equality, and the result says which of the two holds. The motive of the inner ind-Either ignores its argument, but each branch must still produce a value of the motive’s type. Between Odd and even-or-odd the program proves three lemmas, zero-is-even, add1-even->odd and add1-odd->even, left out here:

even-or-odd.pie

(claim double (→ Nat Nat))
(define double (λ (n) (iter-Nat n 0 (λ (d) (add1 (add1 d))))))
(claim Even (→ Nat U))
(define Even (λ (n) (Σ ((half Nat)) (= Nat n (double half)))))
(claim Odd (→ Nat U))
(define Odd (λ (n) (Σ ((haf Nat)) (= Nat n (add1 (double haf))))))
[…]
(claim even-or-odd (Π ((n Nat)) (Either (Even n) (Odd n))))
(define even-or-odd
  (λ (n)
    (ind-Nat n
      (λ (k) (Either (Even k) (Odd k)))
      (left zero-is-even)
      (λ (n-1 e-or-o)
        (ind-Either e-or-o
          (λ (x) (Either (Even (add1 n-1)) (Odd (add1 n-1))))
          (λ (e) (right (add1-even->odd n-1 e)))
          (λ (o) (left (add1-odd->even n-1 o))))))))
(even-or-odd 7)
(the (Either (Σ ((half Nat))
[…]
  (right (cons 3
           (same 7))))

The proof that 7 is odd computes to a witness 3 and the proof (same 7); the type, elided above, is the motive applied to 7, with Even, Odd and double unfolded.