Chapter 4
Equality: definitional and propositional
Pie has two notions of sameness, and most of the difficulty of writing proofs comes from keeping them apart.
Definitional equality, written , is a judgment: the checker decides it by computing both sides to normal form and comparing the results (Chapter 8).The literature also calls it judgmental equality. The two words mean the same thing; we use “definitional” throughout.
Nobody writes a proof of ; either the machine sees it or it does not.
Propositional equality is the type (= A from to). Its inhabitants are proofs, ordinary values that other programs take as arguments, return and compute with.
This section shows how same turns definitional equality into a proof, what the machine can and cannot see, and how the eliminators cong, replace, symm, trans and ind-= are checked and computed.
Several programs use addition, defined as in Section 2.3; we show it once and omit it afterwards, together with lemmas proved earlier in the section:
(claim + (→ Nat Nat Nat))
(define + (λ (n j) (iter-Nat n j (λ (sum) (add1 sum)))))4.1The = type and same#
(= A from to) is a type when A is a type and from and to are both As (typechecker.rkt:165–170; as an element of U, typechecker.rkt:421–426).
It has one constructor, and the constructor only checks:
The two premises with are where definitional equality enters a proof: (same c) is evidence that from and to are equal exactly when the checker can compute both of them to c.
The code is a direct transcription. It forces the expected type with now, matches it against EQUAL, checks and evaluates c, and hands the value to convert twice, once per endpoint.typechecker.rkt:706–717. convert (typechecker.rkt:795–807) reads both values back at A and compares the normal forms up to -equivalence (Section 8.6). We already ran (the (= Nat (+ 2 2) 4) (same 4)) in Section 1.1: both (+ 2 2) and 4 compute to 4, so it checks.
When an endpoint computes to something else, convert reports the two normal forms, as in the next subsection.
4.2What the machine can see#
Addition recurses on its first argument. With 0 first, iter-Nat meets the constructor zero and returns its base, so (+ 0 n) computes to n even though n is a variable, and same proves the equation:
zero-left.pie
(the (Π ((n Nat)) (= Nat (+ 0 n) n))
(λ (n) (same n)))(the (Π ((n Nat))
(= Nat n n))
(λ (n)
(same n)))The printed type is (= Nat n n): the checker normalizes types before printing them.
Swapping the arguments breaks the proof. (+ n 0) is (iter-Nat n 0 step) with the variable n as target, and iter-Nat cannot choose between its zero and add1 cases. The expression is stuck, a neutral term (Section 2.3 and Section 7.4), and a stuck iter-Nat is not the variable n:
zero-right.pie
(the (Π ((n Nat)) (= Nat (+ n 0) n))
(λ (n) (same n)))The expressions
(iter-Nat n
(the Nat 0)
(λ (sum)
(add1 sum)))
and n are not the same Nat
[…]The error shows + unfolded into its iter-Nat body, because definitions are always unfolded (δ; Section 2.3).
The statement is still true, but proving it needs induction. The base needs (+ 0 0) 0, which the machine sees; the step turns a proof ih for n-1 into one for (add1 n-1):
plus-zero.pie
(claim n+0=n (Π ((n Nat)) (= Nat (+ n 0) n)))
(define n+0=n
(λ (n)
(ind-Nat n
(λ (k) (= Nat (+ k 0) k))
(same zero)
(λ (n-1 ih) (cong ih (+ 1))))))
(n+0=n 3)(the (= Nat 3 3)
(same 3))The step must prove (mot (add1 n-1)) (Section 3.1), and (+ (add1 n-1) 0) computes one step, to (add1 (+ n-1 0)), because the target (add1 n-1) is a constructor.
So the step must prove the equation of ih with add1 applied to both sides, which (cong ih (+ 1)) does (Section 4.3).
The function (+ 1) fits because (+ 1 x) computes to (add1 x): its first argument 1 is canonical, built from a constructor, so iter-Nat runs.
Applied to the closed numeral 3, the proof computes to (same 3), an instance of canonicity: every closed proof of an equation without holes normalizes to same. This is a theorem about the type theory, not about the implementation (Section 10.1).
4.3cong#
The language reference states cong for non-dependent functions (pie.scrbl:472–479):typechecker.rkt:459–475. Core cong has three arguments (basics.rkt:168): the elaborator inserts the result type Y it computed (line 472), which the evaluator passes on to do-cong (normalize.rkt:451–452). The printer drops it again (resugar.rkt:87–92).
Both premises synthesize, so f must be something whose type Pie can find: a name, an application such as (+ 1), or a the.
An unannotated λ is a check-only form, so (cong (the (= Nat 2 2) (same 2)) (λ (x) (add1 x))) fails with “Can’t determine a type” at the λ (cong-lambda.pie:2:33; see Section 2.6).
Once both are synthesized (lines 460–461), the implementation reads:
462 (match (val-in-ctx Γ p-t-out)
463 [(EQUAL Av from-v to-v)
464 (match (val-in-ctx Γ f-t-out)
465 [(PI x Bv c)
466 (go-on ((_ (same-type Γ (src-loc e) Av Bv))
467 (Cv (go (val-of-closure c from-v)))
468 (f-v (go (val-in-ctx Γ f-out))))
469 (go `(the (= ,(read-back-type Γ Cv)
470 ,(read-back Γ Cv (do-ap f-v from-v))
471 ,(read-back Γ Cv (do-ap f-v to-v)))
472 (cong ,p-out ,(read-back-type Γ Cv) ,f-out))))]It computes as one would hope: (cong (same v) f) (same (f v)), and on a neutral proof it builds a neutral of type (= Y (f from) (f to)) (normalize.rkt:275–282):
cong.pie
(cong (the (= Nat 2 2) (same 2)) (+ 1))
(the (Π ((n Nat) (p (= Nat n 0))) (= Nat (add1 n) 1))
(λ (n p) (cong p (+ 1))))(the (= Nat 3 3)
(same 3))
[…]
(λ (n p)
(cong p
(λ (j)
(add1 j)))))In the second expression cong synthesizes (= Nat (+ 1 n) (+ 1 0)), which the read-backs at lines 469–471 normalize to the annotation (= Nat (add1 n) 1). The stuck cong prints (+ 1) as (λ (j) (add1 j)), because read-back at a Π type always produces a λ (Section 8.4); the name j comes from the definition of +.
Implementation note. The checker does not enforce the → in the rule. Line 465 matches any Π type, line 467 instantiates its codomain at from only, and lines 470–471 read back both (f from) and (f to) at that one type, which is wrong for a dependent function.
The function dep of Section 3.2, whose type is (Π ((n Nat)) (Pick n)), returns an Atom at 0 and a Nat elsewhere ((dep 1) prints (the Nat 0), cong-dep-values.pie).
For p : (= Nat a b), (cong p dep) synthesizes (= (Pick a) (dep a) (dep b)) (cong-dep-synth.pie), which is not even a type, since (dep b) is a (Pick b); Pie rejects it in a claim (cong-dep-type.pie).
With a closed hypothesis p : (= Nat 0 1), line 471 reads back (dep 1), which is 0, at the type (Pick 0), which is Atom, and read-back has no case for that pair (the program starts with the definitions of Pick and dep):
cong-dep-closed.pie
(claim g (→ (= Nat 0 1) Trivial))
(define g
(λ (p) (car (the (Pair Trivial Trivial) (cons sole (cong p dep))))))match*: no matching clause for '(ATOM ZERO)
location...:
normalize.rkt:569:2This is an internal Racket error from the match* at the top of read-back (normalize.rkt:569), a missing check rather than a demonstrated inconsistency: we have not used it to build a proof of Absurd.
The fix is to require a codomain that does not depend on the argument, which do-cong already assumes (normalize.rkt:282 builds a constant Π type); Section 10.5 leaves it as an exercise, and the port of Section 10.4 makes this change.
4.4replace is transport#
replace moves a value along an equation. If from and to are equal, anything true of from is true of to; the motive says what “true of” means:
The checker follows the rule line by line (typechecker.rkt:427–444) and reads back the motive applied to tov as the result type.
Like every eliminator (Section 8.3), do-replace (normalize.rkt:245–257) has two cases. On same it returns the base, (replace (same v) mot b) b; on a neutral proof it builds a neutral of type (mot to).
The typical use is a cast. A vector of length (+ n 0) has the wrong type where a vector of length n is expected, because the two lengths are not definitionally equal; n+0=n from Section 4.2 lets replace fix the type:
replace.pie
(claim vec-cast (Π ((E U) (n Nat)) (→ (Vec E (+ n 0)) (Vec E n))))
(define vec-cast
(λ (E n v) (replace (n+0=n n) (λ (k) (Vec E k)) v)))
(vec-cast Atom 2 (vec:: 'a (vec:: 'b vecnil)))
[…](the (Vec Atom 2)
(vec:: 'a
(vec:: 'b vecnil)))
[…]With the motive (λ (k) (Vec E k)), the base is a (Vec E (+ n 0)) and the result a (Vec E n). The motive sees only the endpoint k, never the proof; Section 4.6 lifts that restriction.
The rest of the file, left out above, derives cong from replace and proves (= Nat 3 3) with it.
4.5symm and trans#
Symmetry and transitivity are built in:
symm: typechecker.rkt:476–484. trans: typechecker.rkt:445–458; the two equivalence premises are same-type and convert at lines 450–451.Both arguments of trans synthesize (lines 446–447). A bare (same a) therefore cannot be an argument: (λ (a b p) (trans (same a) p)) fails with “Can’t determine a type” (trans-same.pie:3:20), and the argument must be written (the (= Nat a a) (same a)).
symm-trans.pie uses both, with cong and n+0=n, to prove commutativity of addition, (= Nat (+ n m) (+ m n)).
symm and trans compute only when their arguments are same.do-trans: normalize.rkt:259–273; do-symm: normalize.rkt:284–291. The three neutral cases of do-trans are the three ways two targets can be stuck (Section 7.4).
In do-trans, only two sames give same (normalize.rkt:262–263). With same on the left and a neutral on the right, the result is a new neutral, N-trans2 (lines 264–267), not the right argument. So (trans (same a) p) is not p:
trans-refl.pie
(check-same (Π ((a Nat) (b Nat)) (→ (= Nat a b) (= Nat a b)))
(λ (a b p) (trans (the (= Nat a a) (same a)) p))
(λ (a b p) p))The expressions
(λ (a b p)
(trans (same a)
p))
[…]A transitivity defined with ind-= on its first argument behaves differently. In trans-by-J.pie, trans-J uses the motive (λ (w q) (→ (= A w z) (= A x z))) and the base (λ (p2) p2). On a first proof same, ind-= returns the base, so (trans-J Nat a a b (same a) p) computes to p, and the same check-same with trans-J succeeds. Which equations between proofs hold definitionally depends on how trans is defined, not only on what it proves.
4.6ind-=, based path induction#
ind-= is the induction principle for equality. Its motive ranges over both the right endpoint and the proof, with the left endpoint from fixed:
typechecker.rkt:485–501: the motive type at 489–491, the base type at 493–494, and the result type, mot applied to to and to the value of the target, at 495–496. Evaluation is normalize.rkt:293–306: (ind-= (same v) mot b) b.To prove something about every to and every proof t : (= A from to), it suffices to prove it for from and (same from). This is the based form of the eliminator for equality, due to Paulin-MohringChristine Paulin-Mohring. Inductive Definitions in the System Coq: Rules and Properties. In Typed Lambda Calculi and Applications (TLCA ’93), volume 664 of Lecture Notes in Computer Science, pages 328–345. Springer, 1993; replace behaves like the special case of ind-= whose motive ignores the proof.
Symmetry, and a motive that mentions the proof:
ind-eq.pie
(claim symm-by-J (Π ((A U) (x A) (y A)) (→ (= A x y) (= A y x))))
(define symm-by-J
(λ (A x y p) (ind-= p (λ (w q) (= A w x)) (same x))))
(symm-by-J Atom 'a 'a (same 'a))
[…]
(claim proof-is-same
(Π ((A U) (x A) (y A) (p (= A x y))) (= (= A x y) p p)))
(define proof-is-same
(λ (A x y p) (ind-= p (λ (w q) (= (= A x w) q q)) (same (same x)))))(the (= Atom 'a 'a)
(same 'a))
[…]symm-by-J picks the motive (λ (w q) (= A w x)): at w x it is (= A x x), which (same x) inhabits, and at w y it is the goal (= A y x).
proof-is-same, whose motive mentions the proof q, is accepted without output. It proves that every proof equals itself, which says nothing about whether two different proofs are equal (Section 4.7).In Martin-Löf type theory, symm, trans and cong are derivable from this eliminator, as symm-by-J and trans-J show for two of them. Pie makes them primitives, each with its own computation rule (Section 4.5).
4.7What = does not give you#
No uniqueness of proofs. Read-back has -rules for Π, Σ, Trivial, Absurd and empty vectors (Table 8.2), but none for =. A variable of an equality type reads back as itself, so two proofs of the same equation are not definitionally equal:
no-uip.pie
(check-same (Π ((n Nat)) (→ (= Nat n n) (= Nat n n) (= Nat n n)))
(λ (n p q) p)
(λ (n p q) q))The expressions
(λ (n p q)
p)
[…]The same holds propositionally, as a result about the theory rather than the code: Hofmann and StreicherMartin Hofmann and Thomas Streicher. The Groupoid Interpretation of Type Theory. In Giovanni Sambin and Jan M. Smith, editors, Twenty-Five Years of Constructive Type Theory, volume 36 of Oxford Logic Guides, pages 83–111. Oxford University Press, 1998 built a model of type theory in which some equality types have distinct proofs, so uniqueness of identity proofs (UIP, or Streicher’s axiom K) cannot be derived from ind-=.Agda accepts K by default and drops it with --without-K; Lean 4 gets proof irrelevance from its universe Prop.
No function extensionality. Two functions that agree on every argument need not be definitionally equal. Normalizing (λ (n) (+ n 0)) gives a stuck iter-Nat under the λ, which is not n:
funext-def.pie
(check-same (→ Nat Nat) (λ (n) (+ n 0)) (λ (n) n))The expressions
(λ (n)
(iter-Nat n
[…]That the two functions agree on every n is provable: it is the type of n+0=n (Section 4.2).
The step from it to a proof of (= (→ Nat Nat) (λ (n) (+ n 0)) (λ (n) n)), function extensionality, is not derivable in intensional Martin-Löf type theory; the HoTT book adds it as an axiomThe Univalent Foundations Program. Homotopy Type Theory: Univalent Foundations of Mathematics. Institute for Advanced Study, Princeton, 2013. https://homotopytypetheory.org/book, Section 2.9.