Chapter 6
TODO-driven development
The Little Typer builds programs and proofs by leaving empty boxes and
filling them in one at a timeDaniel P. Friedman and David Thrane Christiansen. The Little Typer. MIT Press, Cambridge, MA, 2018. In Pie the empty box
is the expression TODO. A TODO checks against any type, so a
program with holes can still be checked, run and printed, and for each hole
Pie reports the type it must have and the variables in scope. This section uses those reports to develop
a proof (Section 6.1), then shows how holes are implemented
(Section 6.2), how they behave during computation
(Section 6.3), and how they compare with the holes of Agda
(Section 6.4).
6.1A proof, hole by hole#
We prove again (= Nat (+ n 0) n) for every n
(Section 4.2), this time without knowing the answer in
advance. The first draft fixes only the outer shape: induction on
n with the motive (λ (k) (= Nat (+ k 0) k)), and a hole for the
base and for the step (both-holes.pie):
(claim + (→ Nat Nat Nat))
(define + (λ (n m) (iter-Nat n m (λ (almost) (add1 almost)))))
(claim n+0=n (Π ((n Nat)) (= Nat (+ n 0) n)))
(define n+0=n
(λ (n)
(ind-Nat n
(λ (k) (= Nat (+ k 0) k))
TODO
(λ (n-1 ih) TODO))))
(n+0=n 2)The file is accepted. Pie prints the normal form of
(n+0=n 2) and then one report per hole:
(the (= Nat 2 2)
TODO)
both-holes.pie:9:6: TODO:
n : Nat
-------------
(= Nat 0 0)
both-holes.pie:10:18: TODO:
n : Nat
n-1 : Nat
ih : (= Nat (iter-Nat n-1 (the Nat 0) (λ (almost) (add1 almost))) n-1)
-------------------------------------------------------------------------
(= Nat
(add1 (iter-Nat n-1
(the Nat 0)
(λ (almost)
(add1 almost))))
(add1 n-1))Each report has the shape of an inference rule
(Section 1.4): the variables in scope above the line, the goal
below it. The location 9:6 is line 9, column 6 of the file, the
base; 10:18 is the TODO inside the step.
- The base goal is
(= Nat 0 0). It is the motive applied tozero, with(+ 0 0)already computed to0. The motive’s own binderkis not in scope: it is bound only inside the motive. - In the step,
ihhas the motive’s type atn-1. Pie has unfolded+into its definition (δ) and theiter-Natis stuck, becausen-1is a variable. The goal is the motive at(add1 n-1), whereiter-Natcould take one step:(+ (add1 n-1) 0)computes to(add1 (+ n-1 0)). - The normal form of
(n+0=n 2)is a bareTODO: the proof is unfinished, and its value says so. Section 6.3 shows that thisTODOis the step’s hole.
The base. Both sides of (= Nat 0 0) are the same, so
(same zero) fills the hole (Section 4.1), and Pie then
reports only the step goal (base-filled.pie).
The step. The goal is the type of ih with add1
around both sides. Giving ih itself (step-ih.pie, step
(λ (n-1 ih) ih)) fails with an error that places the goal
(“Expected”) next to the type of ih (“but given”). It comes from
the mode switch: ih is a variable, so it synthesizes its type, and
same-type compares that type with the expected one
(typechecker.rkt:772–774, 782–790). A function that adds
add1 on both sides of an equality is cong with
(+ 1), because (+ 1 x) computes to (add1 x)
(Section 4.2). With the step
(λ (n-1 ih) (cong ih (+ 1))) (step-cong.pie) there are no
holes left, and (n+0=n 3) prints the (same 3) already shown in
Section 4.2.
A hole can also stand for a whole definition or for a type. In
later.pie, (define T TODO) after (claim T U) reports the
goal TODO: U.
6.2How a hole is implemented#
TODO has a checking rule and no synthesis rule. The rule has no
premises; it records the hole and elaborates it to a Core TODO that
carries the hole’s source location and its expected type:
Here is the type value turned back into a Core
type in the context , by the function read-back-type
(Section 8.1).
The code is the 'TODO clause of check
(typechecker.rkt:768–771):
768 ['TODO
769 (let ((ty (read-back-type Γ tv)))
770 (begin (send-pie-info (src-loc e) `(TODO ,(read-back-ctx Γ) ,ty))
771 (go (ann `(TODO ,(location->srcloc (src-loc e)) ,ty) Core))))]Line 769 reads the expected type back to Core. The goal shown to
the user is this normal form, which is why + appears unfolded in the
goals of Section 6.1. Line 770 sends the context and the goal
to the info hook, a parameter (typechecker.rkt:48–67) that
#lang pie sets; read-back-ctx turns Γ into entries
(x (free T)), (x (def T e)) or (x (claim T))
(normalize.rkt:499–512). Line 771 returns the Core term
(TODO srcloc type) (basics.rkt:181), whose location is a
plain, serializable list (locations.rkt:17–23).
Since synth has no clause for TODO, a hole must sit in a
checking position, like the constructors of Section 2.6.
Evaluation. A Core TODO evaluates to a neutral
(normalize.rkt:479–480):
479 [`(TODO ,where ,type)
480 (NEU (later ρ type) (N-TODO where (later ρ type)))]The neutral N-TODO keeps the location and the type value,
and nothing else (basics.rkt:250). Its type is evaluated in the
environment ρ of the hole, so the same hole gets a different type each time the surrounding code is
instantiated (Section 6.3). Read-back turns the neutral back
into (TODO where type) (normalize.rkt:711), and resugaring
prints it as TODO (resugar.rkt:15–16).
Reporting the goals. The hook installed by main.rkt
attaches a DrRacket tooltip to the hole and pushes the location, the
context and the goal onto a list (main.rkt:103–110), while the
module is expanded and the normal forms are printed
(Section 9.5). Once every form is checked, main.rkt
places a (displayln "...") for each hole in the expanded module
(main.rkt:163–199). The goals therefore appear when the module
runs, after all the normal forms, in source order. The layout comes from
goal->strings (show-goal.rkt:12–56). Only the free
entries of the context are shown, the variables bound by λ, Π or Σ (17–24),
which is why + never appears above the line. The names are
right-aligned, the outermost variable comes first, and the dashed line is
longer than both the widest hypothesis and the goal. When the context has no
free variable, main.rkt uses the one-line form TODO: T
(main.rkt:174–188).
6.3Holes compute#
Because a hole is a neutral, evaluation does not stop at it. Eliminators
applied to it get stuck, constructors are built around it, and read-back
η-expands it like any other neutral (Section 8.4). The
following file (holes-compute.pie) puts holes in several
positions. The comments ; (1) to ; (5) number the forms that
print; their five results come first, in that order, and the hole reports
follow (we keep only the last):
(claim peas (Π ((n Nat)) (Vec Atom n)))
(define peas
(λ (n)
(ind-Nat n
(λ (k) (Vec Atom k))
vecnil
(λ (n-1 so-far) (vec:: TODO so-far)))))
(peas 2) ; (1)
(claim n Nat)
(define n TODO)
(the Nat (add1 n)) ; (2)
(the (→ Nat Nat) TODO) ; (3)
(the (Pair Nat Atom) TODO) ; (4)
(the Trivial TODO) ; (5)(the (Vec Atom 2)
(vec:: TODO
(vec:: TODO vecnil)))
(the Nat
(add1 TODO))
(the (→ Nat
Nat)
(λ (x)
(TODO x)))
(the (Pair Nat Atom)
(cons (car TODO)
(cdr TODO)))
(the Trivial sole)
[…]
holes-compute.pie:15:13: TODO: Trivial(peas 2)runs the step twice, so the one hole in the step appears twice in the vector.nis defined as a hole. Definitions are unfolded (δ), so(add1 n)becomesadd1on top of the neutral.- At a Π type, read-back applies the hole to a fresh variable and
gives
(λ (x) (TODO x)); at a Σ type it gives(cons (car TODO) (cdr TODO)). - At
Trivial, read-back givessolewhatever the value is, so the hole disappears from the normal form. Pie still reports its goal.
The resugared output hides the location and type of each hole.
To see them we call rep (rep.rkt:18–23) from Racket
(core.rkt), which prints Core before resugaring. Its last call
normalizes a simpler proof of the same shape, whose motive is
(λ (k) (= Nat k k)):
[…]
(the (= Nat 2 2) (ind-Nat 2 (λ (k) (= Nat k k)) TODO (λ (n-1 ih) TODO)))
=> (the
(= Nat (add1 (add1 zero)) (add1 (add1 zero)))
(TODO ("input" 1 65 66 4) (= Nat (add1 (add1 zero)) (add1 (add1 zero)))))This explains the bare TODO of Section 6.1.
Column 65 is the hole of the step, not the one of the base (column 48): ind-Nat on 2 applies the
step to 1 and to the result for 1, and the step ignores both and
returns its hole. The hole’s type is the step’s goal
(= Nat (add1 n-1) (add1 n-1)) evaluated with n-1 bound to
1.
When are two holes the same? α-equivalence has one rule for
holes (alpha.rkt:80–83):
80 ;; Holes from the same location are equal
81 [(`(TODO ,loc1 ,t1) `(TODO ,loc2 ,t2))
82 (and (equal? loc1 loc2)
83 (α-equiv-aux lvl b1 b2 t1 t2))]Two holes written in different places are therefore never the
same, even at the same type: (check-same Nat TODO TODO) fails with
are not the same Nat (two-holes.pie). One hole reached by
two paths is the same as itself (one-hole.pie):
(claim h Nat)
(define h TODO)
(check-same Nat h h)
(the (= Nat h h) (same h))(the (= Nat TODO TODO)
(same TODO))
one-hole.pie:3:10: TODO: NatThe check-same succeeds silently, and same proves
(= Nat h h). A hole thus behaves like an unknown constant whose name
is its location. This is why basics.rkt:32–33 warns that the
location type must not be trivial. Because the rule also compares the
types, two neutrals that come from the same hole are equal only when their
types are α-equivalent as well; the two copies of the hole in
(peas 2) above both have the type Atom.
6.4Limits, and holes in other systems#
The checker never fills a hole; only the user does, by editing the file and
running it again. The N-TODO neutral keeps no context, only a
location and a type, so the context in the report is for the user alone.
In AgdaUlf Norell. Towards a Practical Programming Language Based on Dependent Type Theory. PhD thesis, Chalmers University of Technology, 2007 a hole is a metavariable, typed in its context,
which the user can refine interactively and unification can solve. In
miniKanren terms, an Agda hole is like a logic variable, which a later
unification may bind; a Pie hole is like a fresh constant, equal only to
itself and never bound.
Notes in holes. The reference manual says that “Users may
optionally leave a note or other expression behind as a reminder”
(pie.scrbl:105–107), but the parser accepts only the bare
identifier TODO (parser.rkt:387–388). So
(define a (TODO "fill me")) after (claim a Nat)
(todo-note.pie) fails with bad syntax.In DrRacket each hole also gets the editing commands
“Auto” and “More Auto” (main.rkt:198–199;
interactive-editing.rkt:11–25), which propose an expression from
the shape of the goal alone, such as a λ for a Π or sole for
Trivial.
(TODO 'note)
parses as an application whose operator is the hole. An operator must synthesize its type and a
hole cannot, so it fails with Can't determine a type at the TODO
(todo-quote-note.pie). A Racket comment next to the hole does the
job of a note.