module Data.Int.Universal whereUniversal property of the integersš
We define and prove a type-theoretic universal property of the integers, which characterises them as the initial pointed set equipped with an auto-equivalence, in much the same way that the natural numbers are characterised as being the initial pointed type equipped with an endomorphism.
record Integers (⤠: Type) : TypeĻ where
no-eta-equality
field
point : ā¤
rotate : ⤠ā ā¤We start by giving a mapping-out property: If is any other type with and then there is a map which sends our point (which may as well be called zero) to and commutes with our equivalence. Note that commuting with the equivalence implies commuting with its inverse, too.
map-out : ā {ā} {X : Type ā} ā X ā X ā X ā ⤠ā X
map-out-point : ā {ā} {X : Type ā} (p : X) (r : X ā X) ā map-out p r point ā” p
map-out-rotate
: ā {ā} {X : Type ā} (p : X) (r : X ā X) (i : ā¤)
ā map-out p r (rotate .fst i) ā” r .fst (map-out p r i)We obtain a sort of initiality by requiring that map-out be unique among functions with
these properties.
map-out-unique
: ā {ā} {X : Type ā} (f : ⤠ā X) {p : X} {r : X ā X}
ā f point ā” p
ā (ā x ā f (rotate .fst x) ā” r .fst (f x))
ā ā x ā f x ā” map-out p r xBy a standard categorical argument, existence and uniqueness together give us an induction principle for the integers: to construct a section of a type family it is enough to give an element of and a family of equivalences
ā¤-Ī· : ā z ā map-out point rotate z ā” z
ā¤-Ī· z = sym (map-out-unique id refl (Ī» _ ā refl) z)
induction
: ā {ā} {P : ⤠ā Type ā}
ā P point
ā (ā z ā P z ā P (rotate .fst z))
ā ā z ā P z
induction {P = P} pp pr = section where
tot : ⤠ā Ī£ ⤠P
tot = map-out (point , pp) (Ī£-ap rotate pr)
is-section : ā z ā tot z .fst ā” map-out point rotate z
is-section = map-out-unique (fst ā tot)
(ap fst (map-out-point _ _))
(Ī» z ā ap fst (map-out-rotate _ _ z))
section : ā z ā P z
section z = subst P (is-section z ā ā¤-Ī· z) (tot z .snd) map-out-rotate-inv
: ā {ā} {X : Type ā} (p : X) (r : X ā X) (i : ā¤)
ā map-out p r (Equiv.from rotate i)
ā” Equiv.from r (map-out p r i)
map-out-rotate-inv p r i =
sym (Equiv.Ī· r _)
āā ap (Equiv.from r) (sym (map-out-rotate p r _))
āā ap (Equiv.from r ā map-out p r) (Equiv.ε rotate i)We now prove that the integers are a set of integers. This isnāt as tautological as it sounds, sadly, because our integers were designed to be convenient for algebra, and this specific universal property is rather divorced from algebra. Fortunately, itās still not too hard, so join me.
open Integers
HIT-Int-integers : Integers HIT.Int
HIT-Int-integers = r where
module map-out {ā} {X : Type ā} (l : X ā X) whereWe start by making a simple observation: Exponentiation commutes with difference, where by exponentiation we mean iterated composition of equivalences. That is: if is an integer expressed as a formal difference of naturals then we can compute the power as the difference of equivalences
n-power : Nat ā X ā X
n-power zero = (Ī» x ā x) , id-equiv
n-power (suc x) = n-power x āe l
private
lemma : ā m n x
ā (n-power n eā»Ā¹) .fst (n-power m .fst x)
ā” (n-power n eā»Ā¹) .fst (Equiv.from (l) (l .fst (n-power m .fst x)))
lemma m n x = ap ((n-power n eā»Ā¹) .fst) (sym (Equiv.Ī· l _))
go : HIT.Int ā X ā X
go (HIT.diff x y) = n-power x āe (n-power y eā»Ā¹)
go (HIT.quot m n i) = Σ-prop-path!
{x = n-power m āe (n-power n eā»Ā¹)}
{y = n-power (suc m) āe (n-power (suc n) eā»Ā¹)}
(funext (lemma m n)) iTo show that this computation respects the quotient, we must calculate that is which follows almost immediately from the properties of equivalences, cancelling the critical pair in the middle.
negatives : ā k x ā Equiv.from (n-power k) (l .fst x) ā” l .fst (Equiv.from (n-power k) x)
negativesā»Ā¹ : ā k x ā Equiv.from (n-power k) (Equiv.from l x) ā” Equiv.from l (Equiv.from (n-power k) x)
negatives zero x = refl
negatives (suc k) x =
ap (Equiv.from (n-power k)) (Equiv.Ī· l x)
ā sym (ap (l .fst) (negativesā»Ā¹ k x) ā Equiv.ε l _)
negativesā»Ā¹ zero x = refl
negativesā»Ā¹ (suc k) x = negativesā»Ā¹ k _
abstract
map-suc : ā i x ā go (HIT.suc⤠i) .fst x ā” l .fst (go i .fst x)
map-suc = HIT.Int-elim-by-sign
(Ī» i ā ā x ā go (HIT.suc⤠i) .fst x ā” l .fst (go i .fst x))
(Ī» _ _ ā refl)
negatives
(Ī» _ ā refl) r : Integers HIT.Int
r .point = 0
r .rotate = HIT.suc⤠, HIT.sucā¤-is-equiv
r .map-out point rot int = map-out.go rot int .fst point
r .map-out-point p _ = refl
r .map-out-rotate p rot i = map-out.map-suc rot i _Using elimination by sign, we can divide the proof of uniqueness to the case where is a positive natural number, where is a negated natural number, and when is zero. The case is one of the assumptions, the other cases follow by induction (on naturals).
r .map-out-unique {X = X} f {point} {rot} path htpy =
HIT.Int-elim-by-sign (Ī» z ā f z ā” r .map-out _ _ z) unique-pos unique-neg path
where abstract
unique-pos : ā k ā f (HIT.diff k 0) ā” map-out.n-power rot k .fst point
unique-pos zero = path
unique-pos (suc k) = htpy (HIT.diff k 0) ā ap (rot .fst) (unique-pos k)
unique-neg : ā k ā f (HIT.diff 0 k) ā” Equiv.from (map-out.n-power rot k) point
unique-neg zero = path
unique-neg (suc k) =
sym (Equiv.Ī· rot _)
āā ap (Equiv.from rot) (
sym (htpy (HIT.diff 0 (suc k)))
āā ap f (sym (HIT.quot 0 k))
āā unique-neg k)
āā sym (map-out.negativesā»Ā¹ rot k _)Inductive integers are integersš
In the 1Lab, we have another implementation of the integers, in
addition to the ones defined by quotient, which we have already
characterised as satisfying the universal property, above. These are the
inductive integers: defined as a particular binary coproduct of
natural numbers. To avoid the problem of having ātwo zeroesā, one of the
summands is tagged ānegative successor,ā rather than āsuccessorā, so
that negsuc 0 indicates the number
We have already proven that the inductive integers have a successor equivalence: What we now do is prove this equivalence is universal.
Int-integers : Integers Ind.Int
Int-integers = r where
module map-out {ā} {X : Type ā} (l : X ā X) where
pos : Nat ā X ā X
pos zero = _ , id-equiv
pos (suc x) = pos x āe l
neg : Nat ā X ā X
neg zero = l eā»Ā¹
neg (suc x) = neg x āe (l eā»Ā¹)
to : Ind.Int ā X ā X
to (Ind.pos x) = pos x
to (Ind.negsuc x) = neg x
r : Integers Ind.Int
r .point = Ind.pos 0
r .rotate = Ind.suc-equiv
r .map-out p e i = map-out.to e i .fst p
r .map-out-point p _ = refl
r .map-out-rotate p e = go where
go : ā x ā r .map-out p e (r .rotate .fst x)
ā” e .fst (r .map-out p e x)
go (Ind.pos x) = refl
go (Ind.negsuc zero) = sym (Equiv.ε e _)
go (Ind.negsuc (suc x)) = sym (Equiv.ε e _)
r .map-out-unique f {p} {rot} fz fr = go where
pos : ā n ā f (Ind.pos n) ā” map-out.pos rot n .fst p
pos zero = fz
pos (suc n) = fr (Ind.pos n) ā ap (rot .fst) (pos n)
map-pred : ā n ā f (Ind.pred⤠n) ā” Equiv.from rot (f n)
map-pred n = sym (Equiv.Ī· rot _)
āā ap (Equiv.from rot) (sym (fr _))
āā ap (Equiv.from rot ā f) (Ind.suc-pred⤠n)
neg : ā n ā f (Ind.negsuc n) ā” map-out.neg rot n .fst p
neg zero = map-pred (Ind.pos 0) ā ap (Equiv.from rot) fz
neg (suc n) = map-pred (Ind.negsuc n) ā ap (Equiv.from rot) (neg n)
go : ā i ā f i ā” r .map-out _ _ i
go (Ind.pos x) = pos x
go (Ind.negsuc x) = neg xmodule _ {A B : Type} (ia : Integers A) (ib : Integers B) where
private
module A = Integers ia
module B = Integers ib
to : A ā B
to = A.map-out B.point B.rotate
from : B ā A
from = B.map-out A.point A.rotate
to-from : ā x ā to (from x) ā” x
to-from x =
B.map-out-unique (Ī» x ā to (from x)) {r = B.rotate}
(ap to (B.map-out-point _ _) ā A.map-out-point _ _)
(Ī» x ā ap to (B.map-out-rotate A.point A.rotate _) ā A.map-out-rotate _ _ _)
x
ā sym (B.map-out-unique (Ī» x ā x) refl (Ī» x ā refl) x)
from-to : ā x ā from (to x) ā” x
from-to x =
A.map-out-unique (Ī» x ā from (to x)) {r = A.rotate}
(ap from (A.map-out-point _ _) ā B.map-out-point _ _)
(Ī» x ā ap from (A.map-out-rotate B.point B.rotate _) ā B.map-out-rotate _ _ _)
x
ā sym (A.map-out-unique (Ī» x ā x) refl (Ī» x ā refl) x)
Integers-unique : A ā B
Integers-unique .fst = to
Integers-unique .snd = is-isoāis-equiv (iso from to-from from-to)