Chapter 1
Types that mention values
Pie is the small dependently typed language that accompanies The Little TyperDaniel P. Friedman and David Thrane Christiansen. The Little Typer. MIT Press, Cambridge, MA, 2018. In the terms of type theory it is a small intensional Martin-Löf type theoryPer 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; and Per Martin-Löf. Intuitionistic Type Theory. Bibliopolis, Naples, 1984. Notes by Giovanni Sambin: dependent functions and pairs, natural numbers, atoms, lists, vectors, sums, an identity type, the empty and unit types and a single universe, all built in, with no way to declare more (Chapter 10 lists what it leaves out). Its implementation is a Racket package of about 6700 lines, counting the top-level .rkt files and the language reference pie.scrbl (Table 1.1). We read it side by side with Pie programs that we run, assuming that the reader knows Scheme (closures, environments, an eval/apply interpreter) and miniKanren (relations, modes, unification) but not dependent types.
1.1What makes a type dependent#
We begin with an error. In Scheme, (car '()) fails only when the program runs. Pie rejects the analogous program, head-empty.pie, before it runs.
#lang pie
(claim h (-> (Vec Nat 0) Nat))
(define h (λ (es) (head es)))Expected a Vec with add1 at the top of the length, got zero
location...:
head-empty.pie:3:18The type (Vec Nat 0) describes lists of natural numbers whose length is exactly zero. head requires a vector whose length has add1 at the top, so the checker sees from the type alone that this vector has no first entry (typechecker.rkt:506–517). With the claim (-> (Vec Nat 1) Nat) the same definition is accepted, and (h (vec:: 7 vecnil)) prints (the Nat 7) (head-one.pie).
(Vec Nat 0) is a dependent type: a type that contains an expression which is not itself a type, here the number 0. Other examples are (Vec Atom 3), the vectors of exactly three atoms; (= Nat (+ 2 2) 4), the type of evidence that (+ 2 2) and 4 are equal, a statement written as a type; and (Π ((n Nat)) (Vec Atom n)), the type of functions that return, given n, a vector of length n, so that the result type depends on the value of the argument. The program dependent.pie uses all three. It defines + by iteration and peas by induction (Chapter 2 and Chapter 3); the comments ; (1) to ; (4) number the four results.
#lang pie
(claim + (-> Nat Nat Nat))
(define + (λ (n m) (iter-Nat n m (λ (k) (add1 k)))))
(the (= Nat (+ 2 2) 4) (same 4)) ; (1)
(Vec Atom (+ 2 2)) ; (2)
(claim peas (Π ((n Nat)) (Vec Atom n)))
(define peas
(λ (n)
(ind-Nat n (λ (k) (Vec Atom k)) vecnil
(λ (n-1 so-far) (vec:: 'pea so-far)))))
(peas 2) ; (3)
(the (Vec Atom (+ 2 2)) (peas 4)) ; (4)(the (= Nat 4 4)
(same 4))
(the U
(Vec Atom 4))
(the (Vec Atom 2)
(vec:: 'pea
(vec:: 'pea vecnil)))
(the (Vec Atom 4)
(vec:: 'pea
[…]Every type here is computed. (same 4) is evidence for (= Nat (+ 2 2) 4) because the checker evaluates (+ 2 2) to 4. The type (Vec Atom (+ 2 2)) is itself an expression, of type U (Section 5.4) and value (Vec Atom 4). The type of (peas 2) is (Vec Atom n) with 2 put in for n, and (peas 4) matches the annotation (Vec Atom (+ 2 2)) only once (+ 2 2) has been computed.With 5 in place of 4 in the claimed equation, the same file stops with the error The expressions 5 and 4 are not the same Nat (same-wrong.pie; the printer’s line breaks removed).
This is the theme of the tutorial. To check a term against a dependent type, the checker must evaluate expressions inside types, and some of them are open: they mention variables that have no value yet. When Pie checks the step (λ (n-1 so-far) (vec:: 'pea so-far)) of peas, it expects a vector of length (add1 n-1), where n-1 is bound by the λ, and must see add1 at the top of that length before it accepts vec:: (typechecker.rkt:733–747; Section 3.1 and Section 8.1).
Types and terms are therefore evaluated by the same machinery, and their values have a single representation. The comment that opens the values section of basics.rkt:187–199 explains the approach (η-rules come in Chapter 8):
187;; In order to type check Pie, it is necessary to find the normal
188;; forms of expressions and compare them with each other. The normal
189;; form of an expression is determined by its type - types that have
190;; η-rules (such as Π, Σ, Trivial, and Absurd) impose requirements on
191;; the normal form. For instance, every normal function has λ at the
192;; top, and every normal pair has cons at the top.
193
194;; Finding normal forms has two steps: first, programs are evaluated,
195;; much as they are with the Scheme interpreter at the end of The
196;; Little Schemer. Then, these values are "read back" into the syntax
197;; of their normal forms. This happens in normalize.rkt. This file
198;; defines the values that expressions can have. Structures or symbols
199;; that represent values are written in ALL-CAPS.A stuck computation, such as an elimination whose target is a variable with no value, is a value too, called neutral (Chapter 7).
1.2Running Pie#
A Pie program is a file that starts with #lang pie. We run every example with Racket 8.15 and the Pie package (raco pkg install pie), from the folder that holds the example:
racket file.pieA file is a sequence of top-level forms. claim gives a name a type, and a later define gives it a value, checked against the claimed type (Section 9.5). A bare expression is printed as (the T NF), its type T and its normal form NF, the value written back as syntax. Normal forms unfold definitions and compute even under λ, and the printers restore Pie’s own spelling, such as → for -> and 5 for a chain of five add1s (Chapter 2 and Section 9.6).
How programs and outputs are quoted. Every example is a file that was run, named in the sentence that introduces it or in small type above it. These files and their outputs are in the folder examples/ of the document’s sources, one subfolder per section. Its README.md tells how to install Racket and Pie, and its script run.sh runs every example and compares the outputs with the files. Programs are shown whole in this section. From Chapter 2 on, program blocks leave out the line #lang pie, which locations still count: loop.pie:3:21 is the second line shown. They also leave out definitions shown earlier when the text says so, and a line […] marks any other forms left out. A short one-line file is quoted inline instead. Outputs, marked by a rule on the left, are copied from the run with three edits only: context...: backtrace lines are deleted, absolute file names are shortened to base names, and any other omission is marked […]. Line breaks are the pretty-printer’s own. A block quoting a Racket script that calls Pie’s modules shows selected calls, so it may start or end partway through the script’s output. Statements about other systems (Agda, Rocq, Lean 4, Idris 2) come from their documentation and the literature; nothing was run on them for this tutorial.
Three rules of the top level are easy to trip over. toplevel.pie illustrates the first two.
#lang pie
Nat
U
(Π ((A U)) (-> A A))
(check-same Nat (add1 (add1 zero)) 2)
'done(the U Nat)
U
(Π ((A U))
(→ A
A))
(the Atom 'done)- A type such as
Nathas typeUand prints as(the U Nat).Uitself, and types withUas a component, such as(List U)or a Π overU, have no type (Section 5.4), so they print as types alone, withoutthe(Section 9.5). (check-same T a b)checks thataandbare the sameT(rep.rkt:50–58) and prints nothing when it succeeds.- The first error ends the file. The loop in
main.rkt:119–159raises an exception at the first failure (raise-pie-error,pie-err.rkt:25–30). Inabort.piethe fourth form is ill-typed.
#lang pie
(claim five Nat)
(define five 5)
five
(the Nat 'five)
(add1 five)Expected Nat but given Atom
location...:
abort.pie:5:9
(the Nat 5)(add1 five) is never reached. The error appears before (the Nat 5) because it goes to standard error and the result to standard output, captured in one file. So each error example has a file of its own.
1.3A map of the implementation#
Table 1.1 lists the files of the repository. Seven of them (basics, typechecker, normalize, alpha, fresh, rep and serialization) are in Typed Racket, and tests.rkt in typed/racket/no-check, which ignores type annotations. The parser, the #lang driver and the printers are plain Racket.
Reading the Racket. Excerpts keep the repository’s line numbers and use few Typed Racket idioms. (: f (-> A B)) declares the type of f. match with quasiquote patterns such as `(the ,t ,e) takes apart a Core S-expression, binding t and e. go-on is let* in an error monad: it stops at the first failed step (Section 7.7). (!! p) inside a pattern forces a delayed value before matching it against p (Section 7.3). Π-type is a macro that builds a Π type value whose body is a Racket procedure (Section 7.2).
wc -l; the nine small modules are locations, pie-err, pie-info, serialization, tooltip, interactive-editing, slideshow, info and test-todo-output. Not counted: the subdirectory gui, four Racket files of 1296 lines in all: an editor widget that checks Pie text on slides (used by slideshow.rkt), with its styles and printer, and the lexer that colors Pie syntax in DrRacket (loaded at main.rkt:345).| File | Lines | Role |
|---|---|---|
basics.rkt | 665 | Src and Core syntax, values, neutrals, closures, contexts, environments, fresh names, errors |
parser.rkt | 797 | syntax objects to Src |
typechecker.rkt | 897 | elaboration: is-type, synth, check; sameness: same-type, convert; claim/define |
normalize.rkt | 753 | call-by-need evaluator, eliminators, read-back |
alpha.rkt | 142 | α-equivalence of Core terms |
fresh.rkt | 126 | fresh variable names |
rep.rkt | 61 | drivers: norm, rep, check-same |
main.rkt | 355 | #lang pie: top-level loop, TODO reports, REPL |
resugar.rkt | 173 | Core back to readable Pie syntax |
pretty.rkt | 200 | pretty-printer for terms and error messages |
show-goal.rkt | 70 | layout of TODO goals |
tests.rkt | 1653 | 121 rackunit tests, all of which pass |
pie.scrbl | 543 | the language reference (Scribble) |
| nine small modules | 286 | locations, error structs, tooltips, editor support |
| total | 6721 |
Figure 1.1 follows an expression through these files. The parser turns the text into Src, a surface syntax in which every node carries its source location (basics.rkt:59–62). Elaboration checks the Src and, at the same time, translates it into Core, a smaller syntax without locations or abbreviations (basics.rkt:138–182). The evaluator turns Core into a value. Read-back turns the value into Core again, now in normal form, and the printers make that Core readable.

α-equiv?.1.4How to read this tutorial#
Chapter 2 and Chapter 3 use Pie as a programming language, Chapter 4, Chapter 5 and Chapter 6 prove with it, and Chapter 7, Chapter 8 and Chapter 9 read its implementation. Chapter 10 compares Pie with other systems, gives a recipe for building a checker of this kind and describes a port of Pie to CHICKEN Scheme. A reader mainly interested in the implementation can read Chapter 2, Section 3.1, Section 4.1, Section 4.2 and Section 4.3, Section 5.3 and Section 5.4 and Section 6.2 and Section 6.3, which the implementation sections refer back to, and then go to Chapter 7.
Reading a rule. From Section 2.3 on we state Pie’s typing rules as inference rules: if the premises above the line hold, the conclusion below it holds. Three arrows occur in them. , read “ checks against ”, is asked with the type given. , read “ synthesizes ”, finds the type . And after a judgment names its output, the Core term into which the expression is translated (Section 1.3). This is the rule for a type annotation (the T e):
In words: in the context of variables in scope and their types, if is a type that elaborates to the Core type , and checks against the value of and elaborates to , then (the T e) synthesizes and elaborates to . Here is the value of in the environment built from (ctx->env, basics.rkt:477–488).Pie follows the rules in the appendix of The Little Typer. Each judgment function in typechecker.rkt is headed by a comment that names its judgment form (lines 89, 198, 656, 780 and 793), and basics.rkt:534–540 records one deliberate departure: when it makes a fresh name, the implementation accepts a suggestion, so that elaborated code keeps the user’s names. Christiansen’s tutorial on checking dependent types with normalization by evaluation builds a smaller checker of the same design.Daniel P. Friedman and David Thrane Christiansen. The Little Typer. MIT Press, Cambridge, MA, 2018; and David Thrane Christiansen. Checking Dependent Types with Normalization by Evaluation: A Tutorial. https://davidchristiansen.dk/tutorials/nbe/, 2019. Online tutorial, first published 2018 The code is typechecker.rkt:591–594:
591 [`(the ,t ,e)
592 (go-on ((t-out (is-type Γ r t))
593 (e-out (check Γ r e (val-in-ctx Γ t-out))))
594 (go `(the ,t-out ,e-out)))]The two premises are the two bindings of go-on, in the same order, and the conclusion is the result, the list (the T' e') of type and term. The argument r is a renaming of bound variables (Section 7.6).
Modes. In the type is an input; in it is an output. Think of a single miniKanren relation (typeo Γ e T). Checking runs it with T ground, synthesis with T fresh. Pie writes each mode as its own Racket function (check and synth) and arranges the rules so that every premise receives inputs that are already known, so the checker needs no unification. Section 9.2 develops this, and Section 2.6 shows what it costs in annotations.