323 Everything from \(a=a\): HoTT Univalence Derived, Foundation and AFA Unified

There is one move: read at the identity. Each link below uses only the links named after its title. The trunk is C0–C32; the branches A–H hang from the links named in their titles.

323.1 The source, the rule, joining

C0. The source.

\[ a=a . \]
Write \(\mathrm {id}\) for it: an arrow from \(a\) to \(a\).

C1. The rule. (C0) Leave one end open, \(a=x\). What is given for \(\mathrm {id}\) is given for every \(a=x\), in exactly one way, and at \(\mathrm {id}\) it is what was given.

C2. Join. (C1) For \(p:a=x\) and \(q:x=y\) an arrow \(a=y\) is wanted. Use C1 on \(q\): at \(\mathrm {id}\) take \(p\). This gives \(p\,q\), with \(p\,\mathrm {id}=p\).

C3. The laws; reverse. (C1, C2) Each is checked at \(\mathrm {id}\):

law C1 used on at \(\mathrm {id}\)
\(\mathrm {id}\,p=p\) \(p\) \(\mathrm {id}\,\mathrm {id}=\mathrm {id}\)
\((p\,q)\,r=p\,(q\,r)\) \(r\) \((p\,q)\,\mathrm {id}=p\,q=p\,(q\,\mathrm {id})\)
\(p\,p^{-1}=\mathrm {id}\) \(p\) \(\mathrm {id}\,\mathrm {id}=\mathrm {id}\)

Here \(p^{-1}:x=a\) is given by C1 on \(p\): at \(\mathrm {id}\) take \(\mathrm {id}\).

C4. Category. (C2, C3) Arrows, with a join that has the laws of C3 except possibly the reverse, are called a category. The arrows \(a=x\) of C0–C3 are the first one; write \(A\) for it and \(x:A\) for its ends.

C5. Isomorphism. (C3) An arrow \(p\) with a reverse: \(p\,p^{-1}=\mathrm {id}\), \(p^{-1}p=\mathrm {id}\). In \(A\) every arrow is one.

323.2 Reading at the identity

C6. Functor. (C1, C2) Let \(f\) send each \(x:A\) to some \(f(x):B\). For \(p:a=x\) an arrow \(f(a)=f(x)\) is wanted; at \(\mathrm {id}\) it is the source. So C1 gives \(f(p)\) with

\[ f(\mathrm {id})=\mathrm {id},\qquad f(p\,q)=f(p)\,f(q), \]
the second by C1 on \(q\). Whatever sends arrows to arrows and keeps \(\mathrm {id}\) and join is a functor. Between \(A\) and \(B\) these are the maps \(A\to B\); one after another they are again maps, and with this as join the \(A\)’s and their maps form a category (C4), in which C5 reads \(f\,g=\mathrm {id}\), \(g\,f=\mathrm {id}\).

C7. Natural family. (C6) For functors \(F\), \(G\), a family \(\theta _A:FA\to GA\) with \(G(f)\,\theta _A=\theta _B\,F(f)\) for every \(f\): it commutes with every filling.

C8. Yoneda. (C1, C7) Let \(\theta \) give something for every arrow \(u:C\to A\), naturally: \(\theta (u\,h)=G(h)(\theta (u))\). Then

\[ \theta (u)=\theta (\mathrm {id}\,u)=G(u)\bigl (\theta (\mathrm {id})\bigr ). \]
It is given once it is given at \(\mathrm {id}\): C1 for arrows of any category.

C9. The same readings give an isomorphism. (C8, C5) Let \(\varphi \) match the arrows into \(A\) with the arrows into \(B\), \(\varphi (u\,h)=\varphi (u)\,h\). Put \(f=\varphi (\mathrm {id}_A)\), \(g=\varphi ^{-1}(\mathrm {id}_B)\). By C8, \(\varphi (u)=f\,u\) and \(\varphi ^{-1}(v)=g\,v\), so

\[ g\,f=\varphi ^{-1}(\varphi (\mathrm {id}_A))=\mathrm {id}_A,\qquad f\,g=\varphi (\varphi ^{-1}(\mathrm {id}_B))=\mathrm {id}_B . \]

323.3 Adjoint

C10. Adjoint, transpose. (C8) Functors \(L\), \(R\) with a natural matching \(\varphi \) of the arrows \(LC\to B\) with the arrows \(C\to RB\). \(\varphi (t)\) is the transpose of \(t\).

C11. Unit and counit. (C10, C8) Read at the identity:

\[ \eta _C=\varphi (\mathrm {id}_{LC}):C\to RLC,\qquad \varepsilon _B=\varphi ^{-1}(\mathrm {id}_{RB}):LRB\to B . \]

C12. The transpose, written out. (C11, C8) Since \(\varphi \) is natural,

\[ \varphi (t)=\varphi (t\,\mathrm {id})=R(t)\,\eta _C,\qquad \varphi ^{-1}(s)=\varepsilon _B\,L(s). \]

C13. The triangles. (C12) \(\varphi ^{-1}(\varphi (\mathrm {id}))=\mathrm {id}\) and \(\varphi (\varphi ^{-1}(\mathrm {id}))=\mathrm {id}\), with C12 put in:

\[ \varepsilon _{LC}\,L(\eta _C)=\mathrm {id}_{LC},\qquad R(\varepsilon _B)\,\eta _{RB}=\mathrm {id}_{RB}. \]

C14. An adjoint is the only one. (C9, C10) Two right adjoints of \(L\) have the same arrows into them, namely the arrows out of \(LC\); by C9 they are isomorphic. The same on the left.

323.4 The adjoints of the three uses

C15. Three uses. (C0) In \(a=a\) the \(a\) can be used no times, once, or twice, as it stands:

use on \(A\) on a map \(f\)
none: \(!\) \(A\mapsto \ast \) \(f\mapsto \mathrm {id}\)
once: \(\mathrm {id}\) \(A\mapsto A\) \(f\mapsto f\)
twice: \(\Delta \) \(A\mapsto (A,A)\) \(f\mapsto (f,f)\)

Each keeps \(\mathrm {id}\) and join: a functor (C6).

C16. \(1\) and \(0\). (C10, C15) The right adjoint of \(!\): arrows \(C\to 1\) match the one arrow of \(\ast \), so there is exactly one map from every \(C\) to \(1\). The left adjoint: exactly one map from \(0\) to every \(C\).

C17. \(\times \) and \(\sqcup \). (C10, C15) The right adjoint of \(\Delta \): pairs of maps \((C\to A,\ C\to B)\) match maps \(C\to A\times B\). By C11 its counit is \((\pi _1,\pi _2)\) and its unit is \(C\to C\times C\), the diagonal; by C12–C13,

\[ \pi _1\langle f,g\rangle =f,\quad \pi _2\langle f,g\rangle =g,\quad \langle \pi _1h,\pi _2h\rangle =h . \]
The left adjoint: pairs \((A\to C,\ B\to C)\) match maps \(A\sqcup B\to C\); its unit is \((\mathrm {in}_1,\mathrm {in}_2)\), and
\[ [f,g]\,\mathrm {in}_1=f,\quad [f,g]\,\mathrm {in}_2=g,\quad [h\,\mathrm {in}_1,h\,\mathrm {in}_2]=h . \]

C18. Exponent, curry, evaluation, \(\beta \), \(\eta \). (C10, C17) The right adjoint of \(C\mapsto C\times A\): maps \(t:C\times A\to B\) match maps \(\hat t:C\to B^{A}\) (curry). Its counit is \(\mathrm {ev}:B^{A}\times A\to B\), and the triangles of C13 are

\[ \mathrm {ev}\,(\hat t\times \mathrm {id})=t\quad (\beta ),\qquad \widehat {\mathrm {ev}}=\mathrm {id}\quad (\eta ). \]

C19. Cartesian closed. (C16–C18) The \(A\)’s with their maps have \(1\), \(\times \) and exponents.

C20. All the laws. (C14) Each is a chain of transposes ending in C9; there is no induction. For example

\begin{align*} A\times (B\sqcup C)\to X &\ \leftrightarrow \ B\sqcup C\to X^{A} \ \leftrightarrow \ (B\to X^{A},\ C\to X^{A})\\ &\ \leftrightarrow \ (A\times B\to X,\ A\times C\to X) \ \leftrightarrow \ A\times B\sqcup A\times C\to X , \end{align*}

using C18, C17, C18, C17. In the same way:

\(A\sqcup B\cong B\sqcup A\), \((A\sqcup B)\sqcup C\cong A\sqcup (B\sqcup C)\), \(A\sqcup 0\cong A\)
\(A\times B\cong B\times A\), \((A\times B)\times C\cong A\times (B\times C)\), \(A\times 1\cong A\)
\(A\times (B\sqcup C)\cong A\times B\sqcup A\times C\), \(A\times 0\cong 0\)
\(X^{A\sqcup B}\cong X^{A}\times X^{B}\), \((X^{A})^{B}\cong X^{A\times B}\), \(X^{1}\cong X\), \(X^{0}\cong 1\)

323.5 Logic, equality, membership, relations

C21. Logic. (C16–C18) A statement is a part of \(1\): it holds or not. Write \(2=1\sqcup 1\) for the two values, \(1\) for \(a=a\) and \(0\) for its denial; \(1\neq 0\). On statements the operations of C16–C18 are logic, and every rule of inference is a unit, a counit or a transpose (C11, C12):

is rules
true, false \(1\), \(0\) the one map to \(1\); the one map from \(0\)
and \(\times \) pairing (transpose); \(\pi _1,\pi _2\) (counit)
or \(\sqcup \) \(\mathrm {in}_1,\mathrm {in}_2\) (unit); cases (transpose)
implies exponent discharge (transpose); modus ponens (\(\mathrm {ev}\))
not \(p\) \(p\Rightarrow 0\)

C22. All and some. (C10, C15) A statement about \(C\) is a map \(C\to 2\). One about \(C\) is also one about \(C\times A\) that does not use \(A\) (the discard): \(\pi ^{*}q\). Its two adjoints:

\[ \pi ^{*}q\le p\iff q\le \forall _A\,p,\qquad \exists _A\,p\le q\iff p\le \pi ^{*}q . \]
The counit of \(\forall \) is to instantiate; the unit of \(\exists \) is to give a witness.

C23. Equality. (C10, C15) A statement \(R\) about pairs becomes one about a single \(a\) by using it twice (the diagonal): \(\Delta ^{*}R(a)=R(a,a)\). The left adjoint, at \(1\), is a statement \(E\) with

\[ E\le R\iff R(a,a)\text { for every }a . \]
Its unit is \(\mathrm {refl}\). This is \(x=y\) with the rule C1: the source has returned.

C24. Membership, comprehension, extensionality. (C18) Write \(PX=2^{X}\). \(x\in S\) means \(\mathrm {ev}(S,x)=1\). \(\{x:\varphi (x)\}\) is the transpose of \(\varphi \). Then

\(a\in \{x:\varphi (x)\}\iff \varphi (a)\) \(\beta \)
\(\{x:x\in S\}=S\) \(\eta \)
\(S=T\iff \forall x\,(x\in S\iff x\in T)\) a map into an exponent is the transpose of its evaluation (\(\eta \))
\(\{a\}=(b\mapsto a=b)\) the transpose of C23

C25. Relation, graph, join of relations. (C17, C22, C23) A relation is a part of \(A\times B\), \(R:A\times B\to 2\); a graph is a relation on \(A\times A\), written \(x\to y\). Relations are joined by

\[ (R;S)(x,z)=\exists y\,\bigl (R(x,y)\wedge S(y,z)\bigr ). \]
The identity is equality: \((R;{=})(x,z)=\exists y\,(R(x,y)\wedge y=z)=R(x,z)\), by C23 (at \(y=z\) it is \(R(x,z)\)). Both \((R;S);T\) and \(R;(S;T)\) are \(\exists y\,\exists z\,(R\wedge S\wedge T)\), by C20 for \(\wedge \) and C22.

323.6 Numbers

C26. The source said of the \(A\)’s; Cantor. (C0, C18, C21) \(A=A\) can be written, so the \(A\)’s are the ends of some \(U\), one level up. For \(e:X\to PX\) let \(D=\{x:x\notin e(x)\}\) (copy, evaluate, deny). If \(D=e(d)\) then \(d\in D\iff d\notin D\), that is \(1=0\). So \(X\not \cong PX\).

C27. \(\mathbb N\). (C22, C26) In \(U\) call a part \(S\) closed when \(0\in S\) and \(K\in S\Rightarrow K\sqcup 1\in S\) (one more source). Put

\[ \mathbb N=\forall \{\,S : S\text { closed}\,\}. \]
It is cut out of \(U\) by one use of “all”; nothing is collected from below.

C28. Induction. (C22, C27) The counit of \(\forall \), at a closed \(S\): \(\mathbb N\le S\). A part containing \(0\) and closed under one more contains every number.

C29. \(\mathbb N\cong 1\sqcup \mathbb N\); Peano. (C27, C28, C9) The part “\(0\), or \(K\sqcup 1\) with \(K\in \mathbb N\)” is closed, so it is \(\mathbb N\) (C28): a number is \(0\) or one more than a number. \(0\neq K\sqcup 1\): the right side has \(\mathrm {in}_2\), a map from \(1\), and a map \(1\to 0\) would give \(1=0\). \(K\sqcup 1\cong K'\sqcup 1\Rightarrow K\cong K'\): exchange the image of the last thing with the last thing, then restrict. So \((n+1)-1=n\).

C30. Recursion. (C28, C25) For \(a:X\) and \(g:X\to X\) let \(G\) be the least relation between \(\mathbb N\) and \(X\) with \((0,a)\in G\) and \((n,x)\in G\Rightarrow (n+1,g\,x)\in G\) (C22, as in C27). The \(n\) with exactly one \(x\) related to them contain \(0\) and are closed under one more, by C29; so by C28 they are all of \(\mathbb N\). Hence \(G\) is a map \(r\) with

\[ r(0)=a,\qquad r(n+1)=g(r(n)), \]
and it is the only one, again by C28.

C31. Arithmetic. (C20, C30) \(m+n=m\sqcup n\), \(m\cdot n=m\times n\), \(m^{n}\) the exponent; each stays in \(\mathbb N\) by C28. All laws are C20, and the recursion equations are among them: \(a+0=a\), \(a+(b+1)=(a+b)+1\), \(a(b+1)=ab+a\), \(a^{b+1}=a^{b}a\). With C28 and C29 these are the Peano axioms.

C32. Church numerals. (C18, C30, C2) For \(f:X\to X\), C30 into \(X^{X}\) with \(r(0)=\mathrm {id}\), \(r(n+1)=f\,r(n)\) gives \(n\mapsto f^{n}\): the number counts joins. Then

\[ f^{m+n}=f^{m}f^{n},\qquad f^{mn}=(f^{n})^{m},\qquad f^{0}=\mathrm {id},\qquad f^{1}=f, \]
so sum is join and product is composition.

323.7 Branches

A. Equations between equations. (from C3) \(p=p\) can be written, so arrows stand at the ends of arrows. The laws of C3 are such arrows, and C1–C3 hold again there, at every height. An arrow \(p\) carries what is given at \(a\) to \(x\) (C1: at \(\mathrm {id}\) leave it), with \(\mathrm {id}_{*}\) doing nothing and \((p\,q)_{*}=q_{*}p_{*}\).

B. From the swap. (from C17) \(\sigma =\langle \pi _2,\pi _1\rangle \); \(\pi _1\sigma \sigma =\pi _2\sigma =\pi _1\) and \(\pi _2\sigma \sigma =\pi _2\), so \(\sigma \sigma =\mathrm {id}\), and \(\sigma \Delta =\Delta \).

pairs, the same when the swap is
integers \((a,b)\sim (c,d)\): \(a+d=c+b\) the negative
fractions \((a,b)\sim (c,d)\): \(a\,d=c\,b\) the inverse
complex numbers \((x,y)\), with \(K(x,y)=(x,-y)\) \(J=\sigma K\), \(J^{2}=-1\)

Rows are the things of \(2^{n}\); by C20, \(2^{n+1}\cong 2^{n}\sqcup 2^{n}\), so the number \(\binom nk\) of rows with \(k\) ones has \(\binom {n+1}{k+1}=\binom n{k+1}+\binom nk\), and \((x+y)^{n}=\sum _k\binom nkx^{k}y^{n-k}\) by distributing.

C. Maps among relations; kernel and image. (from C25, C11) For a map \(f\) let \(F(x,y)\) be \(f(x)=y\) and \(F^{\circ }(y,x)=F(x,y)\). Then

\[ (F;F^{\circ })(x,x')=(f(x)=f(x'))\ \ge \ (x=x'),\qquad (F^{\circ };F)(y,y')=\exists x\,(f(x)=y\wedge f(x)=y')\ \le \ (y=y'), \]
the first by C6, the second by C2 and C3. These are a unit and a counit, and \(F;F^{\circ };F=F\) is the triangle: a map is a relation with an adjoint. \(F;F^{\circ }\) is the kernel and \(F^{\circ };F\) the image. \(f\) is one-to-one when the kernel is equality, onto when the image is, and an isomorphism (C5) when both.

D. Least and greatest. (from C22, C28) For a functor \(F\) on parts put \(\mu =\forall \{X:FX\le X\}\) and \(\nu =\exists \{X:X\le FX\}\). If \(FX\le X\) then \(F\mu \le FX\le X\), so \(F\mu \le \mu \); then \(F\mu \) is one of them, so \(\mu \le F\mu \). The mirror gives \(F\nu =\nu \). Both are fixed. Induction is the counit of \(\forall \) (C28); coinduction, \(X\le FX\Rightarrow X\le \nu \), is the unit of \(\exists \). \(\mathbb N\) is \(\mu \) for one more.

E. \(\mathbb N_\infty \), \(\infty \), bisimulation. (from D) The greatest for one more is read by \(\mathrm {pred}\). On the kind \(1\) a structure is a map \(1\to 1\sqcup 1=2\), one of the two values: the value \(0\) gives \(0\), the value \(1\) gives the one \(\infty \) with \(\mathrm {pred}(\infty )=\infty \). A relation \(R\) is a bisimulation when related things are read into related pieces; the greatest one is \(\nu \), and on the greatest side it is equality.

F. Sets. (from D, C25) Let \(P_sX\) be the parts of \(X\) that are images of maps from one level down (C26). Membership on sets is a graph \(V\to P_sV\) (C25).

least greatest
sets are built from their members read into their members
rule induction on \(\in \) bisimulation
exactly one map recursion on \(\in \) decoration: \(d(x)=\{d(y):x\to y\}\) for every graph
name foundation anti-foundation

On the kind \(1\) a graph is one of the two values: no arrow gives \(\varnothing \); the arrow \(\ast \to \ast \), which is \(\{\cdot \}\) of C24, gives the one \(\Omega \) with \(\Omega =\{\Omega \}\). In both, \(V\cong P_sV\) (D), which with C24, C25 and C27 gives extensionality, empty set, pairing, union, power set, separation, replacement and infinity. The axiom of choice is not derived, and the chain does not choose between least and greatest.

G. From \(\beta \) and \(\eta \) alone. (from C18) Pairs are \(\lambda s.\,s\,a\,b\) with readings by \(\lambda x.\lambda y.x\) and \(\lambda x.\lambda y.y\). With the diagonal inside a term, \(Y=(\lambda x.f(x\,x))(\lambda x.f(x\,x))\) has \(Y=f(Y)\) by \(\beta \); for one more its value is \(\infty \) of E.

H. An isomorphism is an equation. (from C9, C26) Take equality of \(A\)’s from the reading side: the same when the arrows into them match. By C9 that is \(A\cong X\). It obeys C1 with \(\mathrm {id}\) for the source: every functor carries an isomorphism (C6), does nothing at \(\mathrm {id}\), and in one way (C8). So \((A=X)\cong (A\cong X)\). It is checked separately in Cubical Agda (Figure 281, Listing 389).

323.8 Verified in Lean and Cubical Agda

Checked in Lean 4. The chain is checked in one file, OneChain.lean (Figure 280, Listing 388); it is accepted with no messages.

links

in the file

C1–C3

rule, join, join_assoc, rev

C5, C6

Iso, ap, ap_join

C8, C9

yoneda, sameReadings

C10–C13

Adj, Adj.unit, Adj.counit, transpose_eq, triangle_left, triangle_right

C15–C19

bang, \(\Delta \), \(\sigma \), pair_unique, sum_unique, expAdj, beta, eta

C20

sumComm, distrib, expSum, expProd and the others

C21–C23

modus_ponens, forall_adj, exists_adj, eq_adj

C24, C25

mem_compr, compr_mem, part_ext, rjoin_eq, rjoin_assoc

C26

cantor

C27, C28, C30

IsNat, nat_induction, recursion, nat_recursion

C29, C31, C32

lambek, succ_inj, add_succ’, iter_add, Church

A, C, D

carry_join; map_unit, map_counit; mu_le_F, F_nu_le

B

namespace Swap

F, G

bisim_eq, bisim_rev; cpair

PIC

Figure 280: The file OneChain.lean in the Lean 4 web editor (Lean v4.36.0-rc2). The message panel reports no errors and no warnings for the trunk C0–C32 and the branches A, B, C, D, F (bisimulation) and G (pairs).

Listing 388: OneChain.lean: the chain C0–C32 and its branches in core Lean 4.

OneChain.lean

/-
  Everything from a = a: one chain.  A check of the trunk C0-C32 and of the
  branches A, B, C, D, F (bisimulation), G (pairs) in core Lean 4, no Mathlib.

  Not in this file: C14 in general, C26 (the kind of kinds), C27 for the kind
  of kinds (it is checked here for any kind with a zero and a "one more"),
  branch E, the sets of branch F, branch H, and the term Y of branch G
  (Lean accepts only terminating terms).
-/

namespace OneChain

section Trunk

variable {A B C D X : Type}

/-! ## C0-C3: the source, the rule, join, its laws, reverse -/

/-- C1: what is given at `a = a` is given at every `a = x` -/
theorem rule {a : A} (G : (x : A) → a = x → Prop) (h : G a rfl) :
    ∀ (x : A) (p : a = x), G x p := by
  intro x p
  cases p
  exact h

/-- C2: join, by the rule on `q`: at the identity take `p` -/
theorem join {a x y : A} (p : a = x) (q : x = y) : a = y := by
  cases q
  exact p

theorem join_id {a x : A} (p : a = x) : join p rfl = p := rfl
theorem id_join {a x : A} (p : a = x) : join rfl p = p := rfl
theorem join_assoc {a x y w : A} (p : a = x) (q : x = y) (r : y = w) :
    join (join p q) r = join p (join q r) := rfl

/-- C3: reverse, by the rule on `p`: at the identity take the identity -/
theorem rev {a x : A} (p : a = x) : x = a := by
  cases p
  rfl

theorem join_rev {a x : A} (p : a = x) : join p (rev p) = rfl := rfl
theorem rev_join {a x : A} (p : a = x) : join (rev p) p = rfl := rfl

/-! ## C5: isomorphism -/

structure Iso (α β : Type) where
  fwd : α → β
  bwd : β → α
  left : ∀ a, bwd (fwd a) = a
  right : ∀ b, fwd (bwd b) = b

/-! ## C6: functor -- a map keeps the identity and join -/

theorem ap (f : A → B) {a x : A} (p : a = x) : f a = f x := by
  cases p
  rfl

theorem ap_id (f : A → B) (a : A) : ap f (rfl : a = a) = rfl := rfl
theorem ap_join (f : A → B) {a x y : A} (p : a = x) (q : x = y) :
    ap f (join p q) = join (ap f p) (ap f q) := rfl

theorem comp_id' (f : A → B) : f ∘ id = f := rfl
theorem id_comp' (f : A → B) : id ∘ f = f := rfl
theorem comp_assoc' (h : C → X) (g : B → C) (f : A → B) : (h ∘ g) ∘ f = h ∘ (g ∘ f) := rfl

/-! ## Branch A: carrying along an equation -/

def carry (P : A → Type) {a x : A} (p : a = x) (u : P a) : P x := by
  cases p
  exact u

theorem carry_id (P : A → Type) {a : A} (u : P a) : carry P rfl u = u := rfl
theorem carry_join (P : A → Type) {a x y : A} (p : a = x) (q : x = y) (u : P a) :
    carry P (join p q) u = carry P q (carry P p u) := by
  cases p
  cases q
  rfl
/-- an equation between equations -/
theorem higher {a x : A} (p : a = x) : p = p := rfl

/-! ## C8: Yoneda -- what is natural is given at the identity -/

theorem yoneda (G : Type → Type)
    (Gmap : {Z W : Type} → (Z → W) → G Z → G W)
    (θ : (Z : Type) → (A → Z) → G Z)
    (nat : ∀ {Z W : Type} (h : Z → W) (u : A → Z), θ W (h ∘ u) = Gmap h (θ Z u))
    (Z : Type) (u : A → Z) : θ Z u = Gmap u (θ A id) :=
  nat u id

/-! ## C9: the same readings give an isomorphism -/

def sameReadings
    (φ : (Z : Type) → (Z → A) → (Z → B)) (ψ : (Z : Type) → (Z → B) → (Z → A))
    (φnat : ∀ {Z W : Type} (h : W → Z) (u : Z → A), φ W (u ∘ h) = φ Z u ∘ h)
    (ψnat : ∀ {Z W : Type} (h : W → Z) (v : Z → B), ψ W (v ∘ h) = ψ Z v ∘ h)
    (ψφ : ∀ (Z : Type) (u : Z → A), ψ Z (φ Z u) = u)
    (φψ : ∀ (Z : Type) (v : Z → B), φ Z (ψ Z v) = v) : Iso A B where
  fwd := φ A id
  bwd := ψ B id
  left := fun a => congrFun ((ψnat (φ A id) id).symm.trans (ψφ A id)) a
  right := fun b => congrFun ((φnat (ψ B id) id).symm.trans (φψ B id)) b

/-! ## C10-C13: adjoint, unit, counit, transpose, triangles -/

structure Fctr where
  obj : Type → Type
  map : {Z W : Type} → (Z → W) → obj Z → obj W
  map_id : ∀ {Z : Type}, map (id : Z → Z) = id
  map_comp : ∀ {Z W V : Type} (g : W → V) (f : Z → W), map (g ∘ f) = map g ∘ map f

/-- C10: a natural matching of the maps `L C → B` with the maps `C → R B` -/
structure Adj (L R : Fctr) where
  φ : {Z W : Type} → (L.obj Z → W) → (Z → R.obj W)
  ψ : {Z W : Type} → (Z → R.obj W) → (L.obj Z → W)
  ψφ : ∀ {Z W : Type} (t : L.obj Z → W), ψ (φ t) = t
  φψ : ∀ {Z W : Type} (s : Z → R.obj W), φ (ψ s) = s
  φnat : ∀ {Z W W' : Type} (b : W → W') (t : L.obj Z → W), φ (b ∘ t) = R.map b ∘ φ t
  ψnat : ∀ {Z Z' W : Type} (h : Z' → Z) (s : Z → R.obj W), ψ (s ∘ h) = ψ s ∘ L.map h

/-- C11: the unit is the image of the identity -/
def Adj.unit {L R : Fctr} (adj : Adj L R) (Z : Type) : Z → R.obj (L.obj Z) :=
  adj.φ (id : L.obj Z → L.obj Z)

/-- C11: the counit is the image of the identity the other way -/
def Adj.counit {L R : Fctr} (adj : Adj L R) (W : Type) : L.obj (R.obj W) → W :=
  adj.ψ (id : R.obj W → R.obj W)

/-- C12: the transpose, written out -/
theorem Adj.transpose_eq {L R : Fctr} (adj : Adj L R) {Z W : Type} (t : L.obj Z → W) :
    adj.φ t = R.map t ∘ adj.unit Z :=
  adj.φnat t id

theorem Adj.transpose_back_eq {L R : Fctr} (adj : Adj L R) {Z W : Type} (s : Z → R.obj W) :
    adj.ψ s = adj.counit W ∘ L.map s :=
  adj.ψnat s id

/-- C13: the first triangle -/
theorem Adj.triangle_left {L R : Fctr} (adj : Adj L R) (Z : Type) :
    adj.counit (L.obj Z) ∘ L.map (adj.unit Z) = (id : L.obj Z → L.obj Z) :=
  (adj.ψnat (adj.unit Z) (id : R.obj (L.obj Z) → R.obj (L.obj Z))).symm.trans
    (adj.ψφ (id : L.obj Z → L.obj Z))

/-- C13: the second triangle -/
theorem Adj.triangle_right {L R : Fctr} (adj : Adj L R) (W : Type) :
    R.map (adj.counit W) ∘ adj.unit (R.obj W) = (id : R.obj W → R.obj W) :=
  (adj.φnat (adj.counit W) (id : L.obj (R.obj W) → L.obj (R.obj W))).symm.trans
    (adj.φψ (id : R.obj W → R.obj W))

/-! ## C15: three uses -/

def bang (_ : A) : Unit := ()
def Δ (a : A) : A × A := (a, a)
def σ (p : A × B) : B × A := (p.2, p.1)

theorem swap_copy (a : A) : σ (Δ a) = Δ a := rfl
theorem swap_swap (p : A × B) : σ (σ p) = p := rfl

/-! ## C16, C17: 1 and 0, pairs and sums -/

theorem bang_unique (f g : A → Unit) : f = g := funext fun _ => rfl
theorem empty_unique (f g : Empty → A) : f = g := funext fun x => nomatch x

theorem pair_fst (f : C → A) (g : C → B) : (fun c => ((fun c => (f c, g c)) c).1) = f := rfl
theorem pair_snd (f : C → A) (g : C → B) : (fun c => ((fun c => (f c, g c)) c).2) = g := rfl
theorem pair_unique (h : C → A × B) : h = fun c => ((h c).1, (h c).2) := rfl

theorem sum_inl (f : A → X) (g : B → X) : Sum.elim f g ∘ Sum.inl = f := rfl
theorem sum_inr (f : A → X) (g : B → X) : Sum.elim f g ∘ Sum.inr = g := rfl
theorem sum_unique (h : Sum A B → X) : h = Sum.elim (h ∘ Sum.inl) (h ∘ Sum.inr) := by
  funext x; cases x <;> rfl

/-! ## C18, C19: exponent, curry, evaluation, beta, eta -/

@[reducible] def prodF (A : Type) : Fctr where
  obj := fun Z => Z × A
  map := fun {Z W} (f : Z → W) (p : Z × A) => (f p.1, p.2)
  map_id := rfl
  map_comp := fun _ _ => rfl

@[reducible] def expF (A : Type) : Fctr where
  obj := fun W => A → W
  map := fun {Z W} (f : Z → W) (h : A → Z) => f ∘ h
  map_id := rfl
  map_comp := fun _ _ => rfl

/-- the exponent is the right adjoint of `(-) × A` -/
def expAdj (A : Type) : Adj (prodF A) (expF A) where
  φ := fun {Z W} (t : Z × A → W) (c : Z) (a : A) => t (c, a)
  ψ := fun {Z W} (s : Z → A → W) (p : Z × A) => s p.1 p.2
  ψφ := fun _ => rfl
  φψ := fun _ => rfl
  φnat := fun _ _ => rfl
  ψnat := fun _ _ => rfl

theorem expAdj_unit (c : C) (a : A) : (expAdj A).unit C c a = (c, a) := rfl
theorem expAdj_counit (h : A → B) (a : A) : (expAdj A).counit B (h, a) = h a := rfl

def curry (t : C × A → B) : C → A → B := fun c a => t (c, a)
def uncurry (h : C → A → B) : C × A → B := fun p => h p.1 p.2
def ev : (A → B) × A → B := uncurry id

theorem beta (t : C × A → B) (c : C) (a : A) : ev (curry t c, a) = t (c, a) := rfl
theorem eta : (curry ev : (A → B) → A → B) = id := rfl

/-! ## C20: all the laws, with no induction -/

def sumComm : Iso (Sum A B) (Sum B A) where
  fwd := fun x => match x with
    | .inl a => .inr a
    | .inr b => .inl b
  bwd := fun x => match x with
    | .inl b => .inr b
    | .inr a => .inl a
  left := by intro x; cases x <;> rfl
  right := by intro x; cases x <;> rfl

def sumAssoc : Iso (Sum (Sum A B) C) (Sum A (Sum B C)) where
  fwd := fun x => match x with
    | .inl (.inl a) => .inl a
    | .inl (.inr b) => .inr (.inl b)
    | .inr c => .inr (.inr c)
  bwd := fun x => match x with
    | .inl a => .inl (.inl a)
    | .inr (.inl b) => .inl (.inr b)
    | .inr (.inr c) => .inr c
  left := by
    intro x
    cases x with
    | inl y => cases y <;> rfl
    | inr c => rfl
  right := by
    intro x
    cases x with
    | inl a => rfl
    | inr y => cases y <;> rfl

def sumEmpty : Iso (Sum A Empty) A where
  fwd := fun x => match x with
    | .inl a => a
    | .inr e => nomatch e
  bwd := Sum.inl
  left := by
    intro x
    cases x with
    | inl a => rfl
    | inr e => exact nomatch e
  right := fun _ => rfl

def prodComm : Iso (A × B) (B × A) where
  fwd := σ
  bwd := σ
  left := fun _ => rfl
  right := fun _ => rfl

def prodAssoc : Iso ((A × B) × C) (A × (B × C)) where
  fwd := fun p => (p.1.1, (p.1.2, p.2))
  bwd := fun p => ((p.1, p.2.1), p.2.2)
  left := fun _ => rfl
  right := fun _ => rfl

def prodUnit : Iso (A × Unit) A where
  fwd := fun p => p.1
  bwd := fun a => (a, ())
  left := by intro ⟨a, u⟩; cases u; rfl
  right := fun _ => rfl

def distrib : Iso (A × Sum B C) (Sum (A × B) (A × C)) where
  fwd := fun p => match p.2 with
    | .inl b => .inl (p.1, b)
    | .inr c => .inr (p.1, c)
  bwd := fun x => match x with
    | .inl q => (q.1, .inl q.2)
    | .inr q => (q.1, .inr q.2)
  left := by intro ⟨a, x⟩; cases x <;> rfl
  right := by intro x; cases x <;> rfl

def prodEmpty : Iso (A × Empty) Empty where
  fwd := fun p => p.2
  bwd := fun e => nomatch e
  left := fun p => nomatch p.2
  right := fun e => nomatch e

def expSum : Iso (Sum A B → X) ((A → X) × (B → X)) where
  fwd := fun h => (h ∘ Sum.inl, h ∘ Sum.inr)
  bwd := fun p => Sum.elim p.1 p.2
  left := by intro h; funext x; cases x <;> rfl
  right := fun _ => rfl

def expProd : Iso (B → A → X) (A × B → X) where
  fwd := fun h p => h p.2 p.1
  bwd := fun k b a => k (a, b)
  left := fun _ => rfl
  right := by intro k; funext ⟨a, b⟩; rfl

def expUnit : Iso (Unit → X) X where
  fwd := fun h => h ()
  bwd := fun x _ => x
  left := by intro h; funext u; cases u; rfl
  right := fun _ => rfl

def expEmpty : Iso (Empty → X) Unit where
  fwd := fun _ => ()
  bwd := fun _ e => nomatch e
  left := by intro h; funext e; exact nomatch e
  right := by intro u; cases u; rfl

/-! ## C21: logic -/

theorem one_ne_zero : true ≠ false := by decide
theorem and_idem (v : Bool) : (v && v) = v := by cases v <;> rfl
theorem not_not' (v : Bool) : (!(!v)) = v := by cases v <;> rfl
theorem modus_ponens (p q : Prop) : (p → q) ∧ p → q := fun h => h.1 h.2
theorem and_pair (p q r : Prop) : (r → p) → (r → q) → (r → p ∧ q) :=
  fun f g x => ⟨f x, g x⟩
theorem or_cases (p q r : Prop) : (p → r) → (q → r) → (p ∨ q → r) :=
  fun f g h => h.elim f g

/-! ## C22, C23: all, some, equality -/

theorem forall_adj (p : C → A → Prop) (q : C → Prop) :
    (∀ c a, q c → p c a) ↔ (∀ c, q c → ∀ a, p c a) :=
  ⟨fun h c hq a => h c a hq, fun h c a hq => h c hq a⟩
theorem forall_counit (p : C → A → Prop) (c : C) (a : A) : (∀ a, p c a) → p c a :=
  fun h => h a

theorem exists_adj (p : C → A → Prop) (q : C → Prop) :
    (∀ c a, p c a → q c) ↔ (∀ c, (∃ a, p c a) → q c) :=
  ⟨fun h c ⟨a, hp⟩ => h c a hp, fun h c a hp => h c ⟨a, hp⟩⟩
theorem exists_unit (p : C → A → Prop) (c : C) (a : A) : p c a → ∃ a, p c a :=
  fun h => ⟨a, h⟩

/-- equality is the left adjoint of reading along the diagonal; its unit is refl -/
theorem eq_adj (R : A → A → Prop) : (∀ a b, a = b → R a b) ↔ (∀ a, R a a) := by
  constructor
  · intro h a
    exact h a a rfl
  · intro h a b e
    subst e
    exact h a

/-! ## C24: membership, comprehension, extensionality -/

abbrev Part (X : Type) := X → Prop
abbrev mem (x : X) (S : Part X) : Prop := S x
abbrev compr (φ : X → Prop) : Part X := φ
abbrev single (a : X) : Part X := fun b => a = b

theorem mem_compr (φ : X → Prop) (a : X) : mem a (compr φ) = φ a := rfl
theorem compr_mem (S : Part X) : compr (fun x => mem x S) = S := rfl
theorem part_ext (S T : Part X) (h : ∀ x, mem x S ↔ mem x T) : S = T :=
  funext fun x => propext (h x)
theorem mem_single (a : X) : mem a (single a) := rfl

/-! ## C25: relations and their join -/

abbrev Rel (A B : Type) := A → B → Prop
abbrev rjoin (R : Rel A B) (S : Rel B C) : Rel A C := fun x z => ∃ y, R x y ∧ S y z
abbrev rrev (R : Rel A B) : Rel B A := fun y x => R x y
abbrev eqRel (A : Type) : Rel A A := fun x y => x = y

/-- the identity for the join of relations is equality -/
theorem rjoin_eq (R : Rel A B) : rjoin R (eqRel B) = R := by
  funext x z
  apply propext
  constructor
  · intro ⟨y, r, e⟩
    exact e ▸ r
  · intro r
    exact ⟨z, r, rfl⟩

theorem eq_rjoin (R : Rel A B) : rjoin (eqRel A) R = R := by
  funext x z
  apply propext
  constructor
  · intro ⟨y, e, r⟩
    exact e ▸ r
  · intro r
    exact ⟨x, rfl, r⟩

theorem rjoin_assoc (R : Rel A B) (S : Rel B C) (T : Rel C D) :
    rjoin (rjoin R S) T = rjoin R (rjoin S T) := by
  funext x w
  apply propext
  constructor
  · intro ⟨z, ⟨y, r, s⟩, t⟩
    exact ⟨y, r, z, s, t⟩
  · intro ⟨y, r, z, s, t⟩
    exact ⟨z, ⟨y, r, s⟩, t⟩

/-! ## Branch C: a map is a relation with a unit and a counit -/

abbrev graphOf (f : A → B) : Rel A B := fun x y => f x = y

/-- the unit: the kernel contains equality -/
theorem map_unit (f : A → B) (x x' : A) (e : x = x') :
    rjoin (graphOf f) (rrev (graphOf f)) x x' :=
  ⟨f x, rfl, (ap f e).symm⟩

/-- the counit: the image is inside equality -/
theorem map_counit (f : A → B) (y y' : B) (h : rjoin (rrev (graphOf f)) (graphOf f) y y') :
    y = y' :=
  match h with
  | ⟨_, p, q⟩ => p.symm.trans q

/-- the join of the relations of two maps is the relation of the composite -/
theorem graph_comp (f : A → B) (g : B → C) :
    rjoin (graphOf f) (graphOf g) = graphOf (g ∘ f) := by
  funext x z
  apply propext
  constructor
  · intro ⟨y, p, q⟩
    exact (ap g p).trans q
  · intro h
    exact ⟨f x, rfl, h⟩

/-! ## C26: Cantor -/

theorem cantor (e : A → A → Bool) : ¬ ∃ a0, e a0 = fun a => !(e a a) := by
  intro ⟨a0, h⟩
  have h2 : e a0 a0 = !(e a0 a0) := congrFun h a0
  cases h' : e a0 a0 <;> simp [h'] at h2

/-! ## C29: N is 1 + N; Peano -/

def lambek : Iso (Sum Unit Nat) Nat where
  fwd := fun x => match x with
    | .inl _ => 0
    | .inr n => n + 1
  bwd := fun n => match n with
    | 0 => .inl ()
    | n + 1 => .inr n
  left := by
    intro x
    cases x with
    | inl u => cases u; rfl
    | inr n => rfl
  right := by intro n; cases n <;> rfl

theorem zero_ne_succ (n : Nat) : 0 ≠ n + 1 := fun h => Nat.noConfusion h
theorem pred_succ : Nat.pred ∘ Nat.succ = id := rfl
theorem succ_inj {m n : Nat} (h : m + 1 = n + 1) : m = n := Nat.succ.inj h
/-- one more, then one fewer -/
theorem add_one_sub_one (n : Nat) : (n + 1) - 1 = n := rfl
/-- one more on both sides: refl goes to refl -/
theorem up {n m : Nat} (h : n = m) : n + 1 = m + 1 := ap (· + 1) h
theorem up_refl (n : Nat) : up (rfl : n = n) = rfl := rfl

/-! ## C31: the recursion equations are laws of C20 -/

theorem add_zero'' (a : Nat) : a + 0 = a := rfl
theorem add_succ' (a b : Nat) : a + (b + 1) = (a + b) + 1 := rfl
theorem mul_succ' (a b : Nat) : a * (b + 1) = a * b + a := rfl

/-! ## C32: Church numerals -- the number counts joins -/

def recN (a : X) (g : X → X) : Nat → X
  | 0 => a
  | n + 1 => g (recN a g n)

theorem recN_zero (a : X) (g : X → X) : recN a g 0 = a := rfl
theorem recN_succ (a : X) (g : X → X) (n : Nat) : recN a g (n + 1) = g (recN a g n) := rfl
theorem recN_unique (a : X) (g : X → X) (h : Nat → X)
    (h0 : h 0 = a) (hs : ∀ n, h (n + 1) = g (h n)) : h = recN a g := by
  funext n
  induction n with
  | zero => exact h0
  | succ n ih => exact (hs n).trans (congrArg g ih)

/-- `f` joined to itself `n` times -/
def iter (f : X → X) : Nat → X → X := recN id (fun h => f ∘ h)

theorem iter_zero (f : X → X) : iter f 0 = id := rfl
theorem iter_one (f : X → X) : iter f 1 = f := rfl
/-- sum is join -/
theorem iter_add (f : X → X) (m n : Nat) : iter f (m + n) = iter f n ∘ iter f m := by
  induction n with
  | zero => rfl
  | succ n ih => exact congrArg (fun h => f ∘ h) ih

def Church : Type 1 := (X : Type) → (X → X) → X → X

def czero : Church := fun _ _ x => x
def cone : Church := fun _ f x => f x
def csucc (n : Church) : Church := fun X f x => f (n X f x)
def cadd (m n : Church) : Church := fun X f x => m X f (n X f x)
def cmul (m n : Church) : Church := fun X f => m X (n X f)
def toNat (n : Church) : Nat := n Nat Nat.succ 0

def ctwo : Church := csucc cone
def cthree : Church := csucc ctwo

theorem cone_is_id : cone = (fun (X : Type) (f : X → X) => f : Church) := rfl
theorem toNat_succ (n : Church) : toNat (csucc n) = toNat n + 1 := rfl
example : toNat (cadd ctwo ctwo) = 4 := rfl
example : toNat (cmul ctwo cthree) = 6 := rfl

theorem czero_ne_cone : czero ≠ cone := by
  intro h
  have h2 : toNat czero = toNat cone := congrArg toNat h
  exact absurd h2 (by decide)

theorem cadd_zero (a : Church) : cadd a czero = a := rfl
theorem czero_add (a : Church) : cadd czero a = a := rfl
theorem cmul_one (a : Church) : cmul a cone = a := rfl
theorem cone_mul (a : Church) : cmul cone a = a := rfl
theorem cadd_assoc (a b c : Church) : cadd (cadd a b) c = cadd a (cadd b c) := rfl
theorem cmul_assoc (a b c : Church) : cmul (cmul a b) c = cmul a (cmul b c) := rfl
theorem cmul_is_comp (m n : Church) (X : Type) : cmul m n X = m X ∘ n X := rfl

/-! ## Branch G: pairs from beta alone -/

def cpair (a b : A) : (A → A → A) → A := fun s => s a b
def cfst (p : (A → A → A) → A) : A := p (fun x _ => x)
def csnd (p : (A → A → A) → A) : A := p (fun _ y => y)

theorem cfst_pair (a b : A) : cfst (cpair a b) = a := rfl
theorem csnd_pair (a b : A) : csnd (cpair a b) = b := rfl

/-! ## C27, C28, C30: N cut out by "all"; induction; recursion from induction -/

section Recursion

variable {M Y : Type} (z : M) (s : M → M)

/-- C27: in every part that contains `z` and is closed under one more -/
abbrev IsNat (x : M) : Prop :=
  ∀ S : M → Prop, S z → (∀ y, S y → S (s y)) → S x

theorem isNat_zero : IsNat z s z := fun _ h0 _ => h0
theorem isNat_succ {x : M} (h : IsNat z s x) : IsNat z s (s x) :=
  fun S h0 hs => hs x (h S h0 hs)

/-- C28: induction is the counit of "all": instantiate at `S` -/
theorem nat_induction (S : M → Prop) (h0 : S z) (hs : ∀ y, S y → S (s y)) :
    ∀ x, IsNat z s x → S x :=
  fun _ h => h S h0 hs

variable (a : Y) (g : Y → Y)

/-- C30: the least relation with `(z, a)` and closed under `(n, y) ↦ (s n, g y)` -/
abbrev RecGraph (n : M) (y : Y) : Prop :=
  ∀ R : M → Y → Prop, R z a → (∀ n y, R n y → R (s n) (g y)) → R n y

theorem graph_zero : RecGraph z s a g z a := fun _ h0 _ => h0
theorem graph_succ {n : M} {y : Y} (h : RecGraph z s a g n y) :
    RecGraph z s a g (s n) (g y) :=
  fun R h0 hs => hs n y (h R h0 hs)

theorem graph_inv {n : M} {y : Y} (h : RecGraph z s a g n y) :
    (n = z ∧ y = a) ∨ ∃ m y0, n = s m ∧ y = g y0 ∧ RecGraph z s a g m y0 := by
  apply h (fun n y => (n = z ∧ y = a) ∨ ∃ m y0, n = s m ∧ y = g y0 ∧ RecGraph z s a g m y0)
  · exact Or.inl ⟨rfl, rfl⟩
  · intro n y hR
    refine Or.inr ⟨n, y, rfl, rfl, ?_⟩
    cases hR with
    | inl h1 =>
      rw [h1.1, h1.2]
      exact graph_zero z s a g
    | inr h2 =>
      have ⟨m, y0, e1, e2, hG⟩ := h2
      rw [e1, e2]
      exact graph_succ z s a g hG

/-- C30: the relation is a map, by induction and the two Peano facts -/
theorem recursion (hz : ∀ m, z ≠ s m) (hs : ∀ m n, s m = s n → m = n) :
    ∀ n, IsNat z s n →
      ∃ y, RecGraph z s a g n y ∧ ∀ y', RecGraph z s a g n y' → y' = y := by
  intro n hn
  apply hn (fun n => ∃ y, RecGraph z s a g n y ∧ ∀ y', RecGraph z s a g n y' → y' = y)
  · refine ⟨a, graph_zero z s a g, ?_⟩
    intro y' h'
    cases graph_inv z s a g h' with
    | inl h1 => exact h1.2
    | inr h2 =>
      have ⟨m, _, e1, _, _⟩ := h2
      exact absurd e1 (hz m)
  · intro n ih
    have ⟨y, hG, hu⟩ := ih
    refine ⟨g y, graph_succ z s a g hG, ?_⟩
    intro y' h'
    cases graph_inv z s a g h' with
    | inl h1 => exact absurd h1.1.symm (hz n)
    | inr h2 =>
      have ⟨m, y0, e1, e2, hG'⟩ := h2
      have e3 : n = m := hs n m e1
      have e4 : y0 = y := hu y0 (by rw [e3]; exact hG')
      rw [e2, e4]

/-- every number is in the part cut out by "all" -/
theorem every_nat (n : Nat) : IsNat 0 Nat.succ n := by
  induction n with
  | zero => exact isNat_zero 0 Nat.succ
  | succ n ih => exact isNat_succ 0 Nat.succ ih

/-- recursion on the numbers, obtained from induction -/
theorem nat_recursion (n : Nat) :
    ∃ y, RecGraph 0 Nat.succ a g n y ∧ ∀ y', RecGraph 0 Nat.succ a g n y' → y' = y :=
  recursion 0 Nat.succ a g (fun _ h => Nat.noConfusion h) (fun _ _ h => Nat.succ.inj h)
    n (every_nat n)

end Recursion

/-! ## Branch D: least and greatest are both fixed -/

section Fix

variable {Z : Type} (F : (Z → Prop) → (Z → Prop))

abbrev sub (S T : Z → Prop) : Prop := ∀ x, S x → T x

/-- least: the "all" of the closed parts -/
abbrev mu : Z → Prop := fun x => ∀ S : Z → Prop, sub (F S) S → S x
/-- greatest: the "some" of the co-closed parts -/
abbrev nu : Z → Prop := fun x => ∃ S : Z → Prop, sub S (F S) ∧ S x

/-- induction: the counit of "all" -/
theorem mu_le (S : Z → Prop) (h : sub (F S) S) : sub (mu F) S := fun _ hx => hx S h
/-- coinduction: the unit of "some" -/
theorem le_nu (S : Z → Prop) (h : sub S (F S)) : sub S (nu F) := fun _ hx => ⟨S, h, hx⟩

theorem F_mu_le (mono : ∀ S T : Z → Prop, sub S T → sub (F S) (F T)) :
    sub (F (mu F)) (mu F) :=
  fun x hx S h => h x (mono (mu F) S (mu_le F S h) x hx)

theorem mu_le_F (mono : ∀ S T : Z → Prop, sub S T → sub (F S) (F T)) :
    sub (mu F) (F (mu F)) :=
  mu_le F (F (mu F)) (mono (F (mu F)) (mu F) (F_mu_le F mono))

theorem nu_le_F (mono : ∀ S T : Z → Prop, sub S T → sub (F S) (F T)) :
    sub (nu F) (F (nu F)) :=
  fun x hx =>
    match hx with
    | ⟨S, h, hS⟩ => mono S (nu F) (le_nu F S h) x (h x hS)

theorem F_nu_le (mono : ∀ S T : Z → Prop, sub S T → sub (F S) (F T)) :
    sub (F (nu F)) (nu F) :=
  le_nu F (F (nu F)) (mono (nu F) (F (nu F)) (nu_le_F F mono))

end Fix

/-! ## Branch F: bisimulation -- equality is one, and the reverse of one is one -/

section Bisim

variable {V V' : Type}

abbrev Bisim (c : Rel V V) (c' : Rel V' V') (Q : Rel V V') : Prop :=
  ∀ x x', Q x x' →
    (∀ y, c x y → ∃ y', c' x' y' ∧ Q y y') ∧ (∀ y', c' x' y' → ∃ y, c x y ∧ Q y y')

theorem bisim_eq (c : Rel V V) : Bisim c c (eqRel V) := by
  intro x x' e
  subst e
  exact ⟨fun y h => ⟨y, h, rfl⟩, fun y h => ⟨y, h, rfl⟩⟩

theorem bisim_rev {c : Rel V V} {c' : Rel V' V'} {Q : Rel V V'} (h : Bisim c c' Q) :
    Bisim c' c (rrev Q) :=
  fun x' x hb => ⟨(h x x' hb).2, (h x x' hb).1⟩

end Bisim

end Trunk

/-! ## Branch B: from the swap -/

namespace Swap

variable {A B C D : Type}

def pair (f : C → A) (g : C → B) : C → A × B := fun c => (f c, g c)

def Δ : A → A × A := pair id id
def σ : A × B → B × A := pair Prod.snd Prod.fst

theorem fst_swap (p : A × B) : (σ p).1 = p.2 := rfl
theorem snd_swap (p : A × B) : (σ p).2 = p.1 := rfl
theorem pair_unique (h : C → A × B) : h = pair (Prod.fst ∘ h) (Prod.snd ∘ h) := rfl
theorem swap_swap (p : A × B) : σ (σ p) = p := rfl
theorem swap_copy (a : A) : σ (Δ a) = Δ a := rfl
theorem swap_natural (f : A → C) (g : B → D) (p : A × B) :
    σ (f p.1, g p.2) = (g (σ p).1, f (σ p).2) := rfl

/-- rows -/
def allRows : Nat → List (List Bool)
  | 0 => [[]]
  | n + 1 => (allRows n).map (false :: ·) ++ (allRows n).map (true :: ·)

def ones : List Bool → Nat
  | [] => 0
  | b :: r => cond b 1 0 + ones r

theorem allRows_zero : allRows 0 = [[]] := rfl

theorem length_allRows (n : Nat) : (allRows n).length = 2 ^ n := by
  induction n with
  | zero => rfl
  | succ n ih =>
    show ((allRows n).map (false :: ·) ++ (allRows n).map (true :: ·)).length = 2 ^ (n + 1)
    rw [List.length_append, List.length_map, List.length_map, ih, Nat.pow_succ, Nat.mul_two]

theorem ones_neg (r : List Bool) : ones (r.map (!·)) + ones r = r.length := by
  induction r with
  | nil => rfl
  | cons b r ih =>
    cases b
    · show (1 + ones (r.map (!·))) + (0 + ones r) = r.length + 1
      omega
    · show (0 + ones (r.map (!·))) + (1 + ones r) = r.length + 1
      omega

/-- coefficients -/
def choose : Nat → Nat → Nat
  | _, 0 => 1
  | 0, _ + 1 => 0
  | n + 1, k + 1 => choose n k + choose n (k + 1)

theorem choose_zero (n : Nat) : choose n 0 = 1 := by cases n <;> rfl
theorem choose_succ (n k : Nat) :
    choose (n + 1) (k + 1) = choose n k + choose n (k + 1) := rfl

def count (n k : Nat) : Nat := ((allRows n).filter (fun r => ones r == k)).length

example : [count 4 0, count 4 1, count 4 2, count 4 3, count 4 4] = [1, 4, 6, 4, 1] := by
  decide
example : [choose 4 0, choose 4 1, choose 4 2, choose 4 3, choose 4 4] = [1, 4, 6, 4, 1] := by
  decide
example : choose 4 0 + choose 4 1 + choose 4 2 + choose 4 3 + choose 4 4 = 2 ^ 4 := by
  decide

def weight (x y : Nat) : List Bool → Nat
  | [] => 1
  | b :: r => cond b x y * weight x y r

def total (l : List Nat) : Nat := l.foldr (· + ·) 0

example : total ((allRows 3).map (weight 2 3)) = (2 + 3) ^ 3 := by decide
example : choose 3 0 * 3 ^ 3 + choose 3 1 * (2 * 3 ^ 2) + choose 3 2 * (2 ^ 2 * 3)
    + choose 3 3 * 2 ^ 3 = (2 + 3) ^ 3 := by decide

/-- integers: a pair read as a difference; the swap is the negative -/
abbrev intEq (p q : Nat × Nat) : Prop := p.1 + q.2 = q.1 + p.2

def addI (p q : Nat × Nat) : Nat × Nat := (p.1 + q.1, p.2 + q.2)
def mulI (p q : Nat × Nat) : Nat × Nat := (p.1 * q.1 + p.2 * q.2, p.1 * q.2 + p.2 * q.1)

theorem intEq_refl (p : Nat × Nat) : intEq p p := rfl
theorem intEq_symm {p q : Nat × Nat} (h : intEq p q) : intEq q p := Eq.symm h
theorem intEq_trans {p q r : Nat × Nat} (h1 : intEq p q) (h2 : intEq q r) : intEq p r := by
  show p.1 + r.2 = r.1 + p.2
  have h1' : p.1 + q.2 = q.1 + p.2 := h1
  have h2' : q.1 + r.2 = r.1 + q.2 := h2
  omega

theorem up_both (a b : Nat) : intEq (a + 1, b + 1) (a, b) := by
  show a + 1 + b = a + (b + 1)
  omega

theorem add_neg (a b : Nat) : intEq (addI (a, b) (σ (a, b))) (0, 0) := by
  show a + b + 0 = 0 + (b + a)
  omega

theorem neg_one_mul (a b : Nat) : intEq (mulI (0, 1) (a, b)) (σ (a, b)) := by
  show 0 * a + 1 * b + a = b + (0 * b + 1 * a)
  omega

example : mulI (0, 1) (0, 1) = (1, 0) := by decide
example : intEq (mulI (2, 5) (1, 3)) (6, 0) := by decide
theorem embed_add (m n : Nat) : addI (m, 0) (n, 0) = (m + n, 0) := rfl

/-- fractions: the same pair read with the product; the swap is the inverse -/
abbrev fracEq (p q : Nat × Nat) : Prop := p.1 * q.2 = q.1 * p.2

def mulF (p q : Nat × Nat) : Nat × Nat := (p.1 * q.1, p.2 * q.2)
def addF (p q : Nat × Nat) : Nat × Nat := (p.1 * q.2 + q.1 * p.2, p.2 * q.2)

theorem scale_both (a b c : Nat) : fracEq (a * c, b * c) (a, b) := by
  show a * c * b = a * (b * c)
  rw [Nat.mul_assoc, Nat.mul_comm c b]

theorem mul_inv (a b : Nat) : fracEq (mulF (a, b) (σ (a, b))) (1, 1) := by
  show a * b * 1 = 1 * (b * a)
  rw [Nat.mul_one, Nat.one_mul, Nat.mul_comm]

example : fracEq (2, 4) (1, 2) := by decide
example : addF (1, 2) (1, 3) = (5, 6) := by decide
example : fracEq (mulF (2, 3) (σ (2, 3))) (1, 1) := by decide

/-- complex numbers: the swap with one sign -/
def K (p : Int × Int) : Int × Int := (p.1, -p.2)
def J (p : Int × Int) : Int × Int := (-p.2, p.1)

theorem J_eq (p : Int × Int) : J p = σ (K p) := rfl
theorem J_sq (p : Int × Int) : J (J p) = (-p.1, -p.2) := rfl
theorem J_four (p : Int × Int) : J (J (J (J p))) = p := by
  show (- -p.1, - -p.2) = p
  rw [Int.neg_neg, Int.neg_neg]
theorem K_K (p : Int × Int) : K (K p) = p := by
  show (p.1, - -p.2) = p
  rw [Int.neg_neg]

def mulC (p q : Int × Int) : Int × Int := (p.1 * q.1 - p.2 * q.2, p.1 * q.2 + p.2 * q.1)

theorem J_is_mul (p : Int × Int) : J p = mulC (0, 1) p := by
  show (-p.2, p.1) = (0 * p.1 - 1 * p.2, 0 * p.2 + 1 * p.1)
  rw [Int.zero_mul, Int.zero_mul, Int.one_mul, Int.one_mul, Int.zero_sub, Int.zero_add]

example : mulC (0, 1) (0, 1) = ((-1, 0) : Int × Int) := by decide
example : mulC (1, 2) (3, 4) = ((-5, 10) : Int × Int) := by decide
example : mulC (1, 2) (K (1, 2)) = ((5, 0) : Int × Int) := by decide
example : mulC (3, 4) (1, 2) = mulC (1, 2) (3, 4) := by decide

/-- loops: words in one loop are the integers -/
def wind : List Bool → Nat × Nat
  | [] => (0, 0)
  | true :: w => ((wind w).1 + 1, (wind w).2)
  | false :: w => ((wind w).1, (wind w).2 + 1)

theorem cancel (w : List Bool) : intEq (wind (true :: false :: w)) (wind w) := by
  show (wind w).1 + 1 + (wind w).2 = (wind w).1 + ((wind w).2 + 1)
  omega
theorem cancel' (w : List Bool) : intEq (wind (false :: true :: w)) (wind w) := by
  show (wind w).1 + 1 + (wind w).2 = (wind w).1 + ((wind w).2 + 1)
  omega

theorem wind_append (u v : List Bool) :
    (wind (u ++ v)).1 = (wind u).1 + (wind v).1 ∧
    (wind (u ++ v)).2 = (wind u).2 + (wind v).2 := by
  induction u with
  | nil =>
    show (wind v).1 = 0 + (wind v).1 ∧ (wind v).2 = 0 + (wind v).2
    exact ⟨by omega, by omega⟩
  | cons b u ih =>
    cases b
    · show (wind (u ++ v)).1 = (wind u).1 + (wind v).1 ∧
        (wind (u ++ v)).2 + 1 = (wind u).2 + 1 + (wind v).2
      exact ⟨by omega, by omega⟩
    · show (wind (u ++ v)).1 + 1 = (wind u).1 + 1 + (wind v).1 ∧
        (wind (u ++ v)).2 = (wind u).2 + (wind v).2
      exact ⟨by omega, by omega⟩

theorem wind_flip (w : List Bool) : wind (w.map (!·)) = σ (wind w) := by
  induction w with
  | nil => rfl
  | cons b w ih =>
    cases b
    · show ((wind (w.map (!·))).1 + 1, (wind (w.map (!·))).2) = ((wind w).2 + 1, (wind w).1)
      rw [ih]
      rfl
    · show ((wind (w.map (!·))).1, (wind (w.map (!·))).2 + 1) = ((wind w).2, (wind w).1 + 1)
      rw [ih]
      rfl

def inv (w : List Bool) : List Bool := (w.map (!·)).reverse

example : wind [true, true, false, true] = (3, 1) := by decide
example : intEq (wind [true, true, false, true]) (2, 0) := by decide
example : wind (inv [true, true, false, true]) = σ (wind [true, true, false, true]) := by
  decide
example : intEq (addI (wind [true, false, true]) (wind (inv [true, false, true]))) (0, 0) := by
  decide

end Swap

end OneChain

In particular recursion derives the recursion map from induction and the two Peano facts, as in C30. Not in the file: C14 in general; the kind of kinds of C26, so that C27–C30 are proved for any kind with a zero and a “one more” and then applied to the numbers; branch E; the sets of branch F; and the term \(Y\) of branch G, since Lean accepts only terminating terms. Branch H cannot be stated in Lean, where any two proofs of an equation are the same; it is checked separately in Cubical Agda (Listing 389).

Branch H in Cubical Agda. The file BranchH.agda is accepted by Agda 2.8.0.2 with the cubical library, with no errors.

in the file

part of branch H

eqToEquiv, eqToEquiv-refl

an equation gives an equivalence; at \(A=A\) it is the identity

isoToEq

an isomorphism gives an equation

H

\((A=B)\simeq (A\simeq B)\)

onlyIdentity

the equivalences into \(A\) are given by the identity alone

flipEq, carry-true

not is an equation \(\mathrm {Bool}=\mathrm {Bool}\), and carrying along it sends true to false

There the statement is proved with a construction (Glue) that is not a link of the chain. So the file checks that the statement of branch H holds together with the rule “given by the identity alone”; it does not check the step of branch H that every functor carries an isomorphism.

PIC

Figure 281: The file BranchH.agda checked by Agda 2.8.0.2 with the cubical library. The checker ends with exit code \(0\) and reports no errors: branch H holds there.

Listing 389: BranchH.agda: branch H in Cubical Agda.

BranchH.agda

{-# OPTIONS --cubical --guardedness --safe #-}

-- Branch H: an isomorphism is an equation.
-- Needs Cubical Agda and the `cubical` library.

module BranchH where

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.Equiv
open import Cubical.Foundations.Isomorphism
open import Cubical.Foundations.Univalence
open import Cubical.Data.Sigma
open import Cubical.Data.Bool

private
  variable
    ℓ : Level
    A B : Type ℓ

-- C15 direction: an equation gives an equivalence;
-- by the rule, at A = A it is the identity.
eqToEquiv : A ≡ B → A ≃ B
eqToEquiv = pathToEquiv

eqToEquiv-refl : pathToEquiv (refl {x = A}) ≡ idEquiv A
eqToEquiv-refl = pathToEquivRefl

-- the other direction: an isomorphism gives an equation
isoToEq : Iso A B → A ≡ B
isoToEq = isoToPath

-- H: the two are the same
H : (A ≡ B) ≃ (A ≃ B)
H = univalence

-- the rule for equivalences: those into A are given by the identity alone
onlyIdentity : (A : Type ℓ) → isContr (Σ[ T ∈ Type ℓ ] (T ≃ A))
onlyIdentity A = EquivContr A

-- an example with content: `not` is an isomorphism of Bool with itself,
-- so it is an equation Bool = Bool, and carrying along it is `not`.
flipIso : Iso Bool Bool
flipIso = iso not not notnot notnot

flipEq : Bool ≡ Bool
flipEq = isoToPath flipIso

carry-true : transport flipEq true ≡ false
carry-true = refl

-- so there are two different equations Bool = Bool
twoEquations : transport flipEq true ≡ transport (refl {x = Bool}) false
twoEquations = refl

What is used. The rule C1, and in C26 the source said of the \(A\)’s. \(\mathbb N\) is cut out of \(U\) by “all” (C27) and is not declared separately.

323.9 Branch I: the numbers as turns of the source

(from C15, C10, branch A, branch H.) Nothing new is introduced. C15 listed three uses of \(a\). The source itself is a fourth: \(a\) used twice with the two uses tied by an equation. Its adjoint is a kind with one thing and one arrow, and the arrows of that kind are the integers. Recursion is carrying, and induction is the rule C1.

I1. The fourth use, and its adjoint. For a kind \(X\) let \(LX\) be the sources in \(X\): the pairs \((x,\ p:x=x)\). As in C16–C18, take the adjoint at \(1\): a kind \(S\) with

\[ \text {maps } S\to X\ \ \leftrightarrow \ \ \text {pairs } (x,\ p:x=x)\text { in } X . \]
Reading at the identity (C11), \(S\) has a thing \(b\) and an arrow \(\ell :b=b\), and it is given by these alone. By C14 it is the only such kind. \(S\) is finite to state: one thing, one arrow.

I2. The integers. Let \(\mathbb Z\) be the arrows \(b=b\) of \(S\).

is from
\(0\) \(\mathrm {id}\) C0
\(p+q\) \(p\,q\) join, C2
\(-p\) \(p^{-1}\) reverse, C3
one more \(p\mapsto p\,\ell \) C2
\((p+q)+r=p+(q+r)\),  \(p+0=p\),  \(p+(-p)=0\) C3

A number is how many times the source is gone around.

I3. A family over \(S\) is a kind with an isomorphism to itself. Put \(X=U\) in I1: a map \(S\to U\) is a kind \(K\) with an equation \(K=K\). By branch H an equation \(K=K\) is an isomorphism \(e:K\cong K\). So

\[ \text {families } P \text { over } S\ \ \leftrightarrow \ \ (K,\ e:K\cong K), \qquad P(b)=K,\quad \ell _{*}=e . \]

I4. Recursion is carrying. Let \((K,e)\) be as in I3 and \(k:K\). An arrow \(p:b=b\) carries \(k\) (branch A). Put

\[ r(p)=p_{*}(k). \]
Then, since \(\mathrm {id}_{*}\) does nothing and \((p\,q)_{*}=q_{*}\,p_{*}\),
\[ r(0)=k,\qquad r(p\,\ell )=\ell _{*}(p_{*}(k))=e(r(p)). \]

I5. There is exactly one such map: induction. Use C1 at \(b\): the pairs \((x,\ p:b=x)\) are given by \((b,\mathrm {id})\) alone. So there is exactly one way to give, for every such pair, a thing of \(P(x)\) that is \(k\) at \((b,\mathrm {id})\); it is \(p_{*}(k)\). By I3 such a giving is the same as a map \(h:\mathbb Z\to K\) with \(h(0)=k\) and \(h(p\,\ell )=e(h(p))\). Hence \(h=r\):

\[ \mathbb Z \text { is the least kind with a thing and an isomorphism to itself.} \]
Induction on \(\mathbb Z\) is this uniqueness, and it is the rule C1.

I6. The arrow is not the identity. Take \(K=2\), \(e\) the exchange of the two values, \(k=1\). Then \(r(\ell )=e(1)=0\) and \(r(0)=1\). Since \(1\neq 0\), \(\ell \neq \mathrm {id}\).

I7. The numbers. Let \(\mathbb N\) be the least part of \(\mathbb Z\) that contains \(0\) and, with \(p\), contains \(p\,\ell \) (C22). A part of \(\mathbb Z\) containing \(0\) and closed under one more contains \(\mathbb N\): induction, as in C28.

I8. The two derivations side by side.

trunk (C27–C30) this branch
a number is the source put side by side \(n\) times the source gone around \(n\) times
sum \(\sqcup \) join
negative pairs and the swap (branch B) reverse
recursion least relation, induction, Peano (C30) carrying (I4)
induction counit of “all” (C28) the rule C1 (I5)

I9. What is used. No infinite kind is declared, and neither recursion nor induction is assumed. What is used is the kind \(U\) of C26, in I3 and through branch H, and the step of branch H marked there. I6 shows that one turn is not the identity; that no number of turns is the identity uses the finite kinds of \(U\) (a kind of \(n+1\) things with its cyclic exchange, for each \(n\)).

323.10 Branch J: from \(0=1\), by splitting the point

(from C17, branch B, branch F, branch I.) Nothing new is introduced. Branch I started from an arrow that never returns. This branch starts from the other end: the world where one more returns at once, \(0=1\), and splits its one point. Every world on the way is finite. The reading is on the same side as anti-foundation, and J6 shows exactly where the two differ.

J1. \(0=1\): one point. If \(0=1\), one more does nothing: \(s=\mathrm {id}\), and the arrow of branch I is \(\ell =\mathrm {id}\). Every kind is \(1\), every statement holds, and induction and recursion hold with nothing to prove. Write \(C_1\) for this world. It cannot tell \(2\) from \(3\).

J2. Splitting: \(0=n\). Instead of “one turn returns”, let \(n\) turns return. \(C_n\) is the kind \(1\sqcup \cdots \sqcup 1\) (\(n\) times, C17) with one more as the cyclic exchange (branch B):

\[ s(0)=1,\quad s(1)=2,\quad \dots ,\quad s(n-1)=0 . \]

world says things
\(C_1\) \(0=1\) \(1\)
\(C_2\) \(0=2\) \(2\)
\(C_3\) \(0=3\) \(3\)
\(C_n\) \(0=n\) \(n\)

Each \(C_n\) is finite. No kind of kinds and no infinite kind is used to form it.

J3. Inside one world everything is a finite computation. In \(C_n\) put \(x+y=s^{y}(x)\) and \(x\cdot y\) the sum of \(x\) taken \(y\) times. The laws of C20 hold and are checked by going through the cases, and “for every \(x\)” means the \(n\) things. No induction is needed: there are only \(n\) things. For example \(2+2=4\) in \(C_5\) and \(2\cdot 3=6\) in \(C_7\).

J4. The worlds are joined. Reading more turns as the same gives a map \(C_{mn}\to C_n\) that keeps \(0\), one more, sum and product. Every world maps to \(C_1\). Going the other way is the splitting: in \(C_1\leftarrow C_2\leftarrow C_4\leftarrow \cdots \) each point becomes two.

J5. A number, read from this side. A number is read in every world: its place in each \(C_n\), with the places agreeing along the maps of J4. Two numbers are the same when no world tells them apart:

\[ p=q\quad \iff \quad p \text { and } q \text { have the same place in every } C_n . \]
Sum and product are computed world by world. For the turns of branch I this is their equality: \(\ell ^{a}\) and \(\ell ^{b}\) with \(a\neq b\) have different places in a \(C_n\) with \(n\) larger than both.

J6. The same side as anti-foundation, and the difference. “The same when no reading tells them apart” is the read side, as bisimulation is (branch F). But read each \(C_n\) as a graph, \(x\to s(x)\), and decorate it:

graph under bisimulation (branch F) in this branch
one point, \(\ast \to \ast \)  (\(C_1\)) \(\Omega \) one thing
two points, each to the other  (\(C_2\)) both \(\Omega \) two things
\(n\) points in a ring  (\(C_n\)) all \(\Omega \) \(n\) things

For the ring, the relation that relates every point to every point is a bisimulation, so every point is decorated by the one \(\Omega \) with \(\Omega =\{\Omega \}\). Anti-foundation therefore reads every \(0=n\) as \(0=1\): it keeps that there is a return and forgets after how many turns. This branch keeps the number of turns. That is the whole difference:

anti-foundation this branch
what is read the members one more
a ring of \(n\) \(\Omega \), for every \(n\) \(C_n\), different for each \(n\)
the same when a bisimulation relates them no \(C_n\) tells them apart
gives sets with \(\Omega =\{\Omega \}\) numbers

J7. Least inside greatest. All the readings of J5 together are the greatest side. Those reached from \(0\) by one more are the numbers \(\mathbb N\), the least side, and they lie inside it as \(\mathbb N\) lies in \(\mathbb N_\infty \) (branch E) and the well-founded sets in the sets of branch F. The greatest side has more: readings that agree in every world but are reached from \(0\) by no number of steps.

J8. The three ways to the numbers.

trunk (C27) branch I this branch
starts from \(0\), the source denied an arrow that never returns \(0=1\), an arrow that returns at once
a number is the source side by side \(n\) times \(n\) turns a place in every \(C_n\)
equality given given (C1) read
one finite computation uses \(U\) \(U\) one \(C_n\), nothing else

J9. What is used. A statement with fixed numbers, such as \(2+2=4\) or \(7\cdot 8=56\), is settled inside one sufficiently large \(C_n\) by a finite computation; nothing infinite, no \(U\) and no induction is used. A statement about every number becomes a statement about every \(C_n\), and that “every \(n\)” ranges over the numbers again. So the one step is not removed here; it takes the form: for any \(n\) the world \(C_n\) can be formed, that is, the point can always be split once more.

Branch J in Lean 4. The finite computations of branch J are checked in BranchJ.lean (Figure 282, Listing 390), accepted with no messages: \(C_1\) (J1), the return after \(n\) turns (J2), the laws inside \(C_5\) (J3), the maps \(C_6\to C_3\), \(C_6\to C_2\), \(C_6\to C_1\) (J4), that different numbers are told apart by some world (J5), that every ring is bisimilar to the one loop while the worlds differ in size (J6), and two statements with fixed numbers (J9). Only told_apart is about every pair of numbers; all the rest are computations in one world.

PIC

Figure 282: The file BranchJ.lean in the Lean 4 web editor (Lean v4.36.0-rc2): no errors and no warnings for branch J.

Listing 390: BranchJ.lean: branch J, the finite worlds \(C_n\).

BranchJ.lean

/-
  Branch J: from 0 = 1, by splitting the point.  Core Lean 4, no Mathlib.
  Every check here is a finite computation inside one world C_n.
-/

namespace BranchJ

/-! ## The world C_n: n things, and one more goes round -/

def things (n : Nat) : List Nat := List.range n

def suc (n x : Nat) : Nat := (x + 1) % n
def add (n x y : Nat) : Nat := (x + y) % n
def mul (n x y : Nat) : Nat := (x * y) % n

def iter (f : Nat → Nat) : Nat → Nat → Nat
  | 0, x => x
  | k + 1, x => f (iter f k x)

def all1 (n : Nat) (p : Nat → Bool) : Bool := (things n).all p
def all2 (n : Nat) (p : Nat → Nat → Bool) : Bool :=
  (things n).all fun x => (things n).all fun y => p x y
def all3 (n : Nat) (p : Nat → Nat → Nat → Bool) : Bool :=
  (things n).all fun x => (things n).all fun y => (things n).all fun z => p x y z

/-! ## J1: C_1 is the world 0 = 1 -/

theorem C1_one_thing : things 1 = [0] := by decide +kernel
/-- one more does nothing -/
theorem C1_suc : suc 1 0 = 0 := by decide +kernel
/-- 0 and 1 have the same place -/
theorem C1_zero_eq_one : 0 % 1 = 1 % 1 := by decide +kernel
/-- so do 2 and 3 -/
theorem C1_two_eq_three : 2 % 1 = 3 % 1 := by decide +kernel

/-! ## J2: in C_n, n turns return and fewer do not -/

theorem C6_returns : all1 6 (fun x => iter (suc 6) 6 x == x) = true := by decide +kernel
theorem C6_not_before :
    (List.range 5).all (fun k => iter (suc 6) (k + 1) 0 != 0) = true := by decide +kernel
theorem C6_zero_eq_six : 0 % 6 = 6 % 6 := by decide +kernel

/-! ## J3: inside one world everything is a finite computation -/

/-- the sum is one more, repeated -/
theorem C5_add_is_iter :
    all2 5 (fun x y => add 5 x y == iter (suc 5) y x) = true := by decide +kernel

theorem C5_add_comm : all2 5 (fun x y => add 5 x y == add 5 y x) = true := by decide +kernel
theorem C5_add_assoc :
    all3 5 (fun x y z => add 5 (add 5 x y) z == add 5 x (add 5 y z)) = true := by
  decide +kernel
theorem C5_add_zero : all1 5 (fun x => add 5 x 0 == x) = true := by decide +kernel
theorem C5_mul_comm : all2 5 (fun x y => mul 5 x y == mul 5 y x) = true := by decide +kernel
theorem C5_mul_assoc :
    all3 5 (fun x y z => mul 5 (mul 5 x y) z == mul 5 x (mul 5 y z)) = true := by
  decide +kernel
theorem C5_mul_one : all1 5 (fun x => mul 5 x 1 == x) = true := by decide +kernel
theorem C5_distrib :
    all3 5 (fun x y z => mul 5 x (add 5 y z) == add 5 (mul 5 x y) (mul 5 x z)) = true := by
  decide +kernel
/-- every thing has a negative -/
theorem C5_neg : all1 5 (fun x => (things 5).any (fun y => add 5 x y == 0)) = true := by
  decide +kernel

theorem two_plus_two : add 5 2 2 = 4 := by decide +kernel
theorem two_times_three : mul 7 2 3 = 6 := by decide +kernel

/-! ## J4: the worlds are joined -- C_{mn} → C_n keeps 0, one more, sum, product -/

def red (n x : Nat) : Nat := x % n

theorem C6_to_C3_zero : red 3 0 = 0 := by decide +kernel
theorem C6_to_C3_suc : all1 6 (fun x => red 3 (suc 6 x) == suc 3 (red 3 x)) = true := by
  decide +kernel
theorem C6_to_C3_add :
    all2 6 (fun x y => red 3 (add 6 x y) == add 3 (red 3 x) (red 3 y)) = true := by
  decide +kernel
theorem C6_to_C3_mul :
    all2 6 (fun x y => red 3 (mul 6 x y) == mul 3 (red 3 x) (red 3 y)) = true := by
  decide +kernel
theorem C6_to_C2_add :
    all2 6 (fun x y => red 2 (add 6 x y) == add 2 (red 2 x) (red 2 y)) = true := by
  decide +kernel
/-- every world maps to C_1 -/
theorem C6_to_C1 : all1 6 (fun x => red 1 x == 0) = true := by decide +kernel
/-- splitting: each point of C_2 is two points of C_4 -/
theorem C4_splits_C2 :
    all1 2 (fun y => ((things 4).filter (fun x => red 2 x == y)).length == 2) = true := by
  decide +kernel

/-! ## J5: the same when no world tells them apart -/

/-- 3 and 8 have the same place in C_5 and different places in C_7 -/
theorem same_in_C5 : 3 % 5 = 8 % 5 := by decide +kernel
theorem apart_in_C7 : 3 % 7 ≠ 8 % 7 := by decide +kernel

/-- two different numbers are told apart by some world -/
theorem told_apart (a b : Nat) (h : a ≠ b) : ∃ n, a % n ≠ b % n := by
  have ha : a % (a + b + 1) = a := Nat.mod_eq_of_lt (by omega)
  have hb : b % (a + b + 1) = b := Nat.mod_eq_of_lt (by omega)
  exact ⟨a + b + 1, by rw [ha, hb]; exact h⟩

/-- and the same number has the same place in every world -/
theorem same_everywhere (a b : Nat) (h : a = b) (n : Nat) : a % n = b % n := by
  rw [h]

/-! ## J6: anti-foundation reads every ring as the one loop -/

/-- the ring of n points: x → one more of x -/
def ring (n x y : Nat) : Bool := y == suc n x

/-- R is a bisimulation between a graph on m points and a graph on n points -/
def isBisim (m n : Nat) (c c' R : Nat → Nat → Bool) : Bool :=
  (things m).all fun x => (things n).all fun x' =>
    (!(R x x')) ||
      (((things m).all fun y =>
          (!(c x y)) || ((things n).any fun y' => (c' x' y') && (R y y'))) &&
       ((things n).all fun y' =>
          (!(c' x' y')) || ((things m).any fun y => (c x y) && (R y y'))))

/-- the relation that relates every point to every point -/
def full (_ _ : Nat) : Bool := true

/-- inside one ring: all points are bisimilar -/
theorem ring3_all_bisimilar : isBisim 3 3 (ring 3) (ring 3) full = true := by decide +kernel
theorem ring5_all_bisimilar : isBisim 5 5 (ring 5) (ring 5) full = true := by decide +kernel

/-- between rings: every ring is bisimilar to the one loop C_1 -/
theorem ring2_is_loop : isBisim 1 2 (ring 1) (ring 2) full = true := by decide +kernel
theorem ring3_is_loop : isBisim 1 3 (ring 1) (ring 3) full = true := by decide +kernel
theorem ring5_is_loop : isBisim 1 5 (ring 1) (ring 5) full = true := by decide +kernel
theorem ring2_ring5 : isBisim 2 5 (ring 2) (ring 5) full = true := by decide +kernel

/-- but this branch keeps the number of turns: the worlds have different sizes -/
theorem sizes_differ : (things 2).length ≠ (things 5).length := by decide +kernel
/-- and inside C_3 the three places are different -/
theorem C3_places_differ : all2 3 (fun x y => (x == y) || (red 3 x != red 3 y)) = true := by
  decide +kernel

/-- a graph that is not a ring is not the loop: a point with no arrow -/
def noArrow (_ _ : Nat) : Bool := false
theorem empty_is_not_loop : isBisim 1 1 (ring 1) noArrow full = false := by decide +kernel

/-! ## J9: a statement with fixed numbers is settled inside one world -/

theorem seven_times_eight : mul 57 7 8 = 56 := by decide +kernel
theorem a_sum : add 1000 123 456 = 579 := by decide +kernel

end BranchJ

323.11 A parallel: self-maps of the whole

Branch J and Q5 have the same shape as a known situation in set theory, the elementary embeddings of the universe into itself. The shapes agree; the theorems are not the same, and J6 below says where they differ.

K1. The situation in set theory. An elementary embedding \(j:V\to V\) is a map of the universe of sets into itself that keeps every statement. If \(j\neq \mathrm {id}\), the least ordinal it moves, \(\kappa \), is called a Reinhardt cardinal [270]. Kunen showed that, with the axiom of choice, there is none:

\[ j:V\to V \text { elementary}\ \Longrightarrow \ j=\mathrm {id} \]
[269]. His argument goes through the sequence
\[ \kappa ,\quad j(\kappa ),\quad j^{2}(\kappa ),\quad \dots ,\qquad \lambda =\sup _n j^{n}(\kappa ),\qquad j(\lambda )=\lambda , \]
and shows that already \(V_{\lambda +2}\) has no such \(j\). On the pieces below, \(j:V_\lambda \to V_\lambda \) and \(j:V_{\lambda +1}\to V_{\lambda +1}\) with \(j\neq \mathrm {id}\) are not known to be contradictory; they are the strongest large cardinal axioms in use [1032].

K2. The same shape in the chain.

set theory the chain
on the whole, only the identity \(j:V\to V\) is \(\mathrm {id}\) (K1) a map \(X\to X\) given for every \(X\) is \(\mathrm {id}\) (Q5)
on a piece, there is movement \(j:V_\lambda \to V_\lambda \) may differ from \(\mathrm {id}\) in \(C_n\) one more really turns (J2)
following the movement \(\kappa ,\ j(\kappa ),\ j^{2}(\kappa ),\dots \) \(0,\ 1,\ 2,\dots \)
its end is a fixed thing \(j(\lambda )=\lambda \) \(s(\infty )=\infty \),  \(\Omega =\{\Omega \}\) (branches E, F)
the whole is not reached no \(j\) on \(V\) from the \(j\) on the pieces “every \(n\)” stays one step (J9)

K3. The world \(0=1\) is the world \(j=\mathrm {id}\). In \(C_1\) one more does nothing (J1). That is the whole seen with only the identity on it. The worlds \(C_n\), \(n\ge 2\), are the pieces where a movement other than the identity lives, and the numbers are read off that movement (J5).

K4. Why only the identity, in the chain. Q5 is one line: a family \(\theta _X:X\to X\) given for every \(X\) and commuting with every map satisfies \(\theta _X(x)=x\,\theta _1=x\), because \(1\) has only one map to itself. So what is given for the whole, uniformly, is the source \(\mathrm {id}\) and nothing else. A movement needs something that is not uniform: a chosen \(f\) on a chosen \(X\) (Q6), or a chosen \(n\) (J2).

K5. Read from the fixed thing. In both columns the fixed thing is where the movement stops being told apart: \(j(\lambda )=\lambda \), \(s(\infty )=\infty \). Branch J starts there, at \(0=1\), and splits; the trunk and branch I start at \(0\) and go up. The two directions meet in the same picture: the least side inside the greatest side (J7).

K6. Where the two differ.

set theory the chain
the maps elementary: keep every statement natural: commute with every map
why only \(\mathrm {id}\) on the whole choice and a combinatorial argument [269] one computation (Q5)
without choice open [1746] nothing open
the pieces \(V_\lambda \): infinite; their \(j\) is a strong assumption \(C_n\): finite; computed (branch J)

So this subsection claims a likeness of shape and no more: on the whole only the identity, movement only on a piece, and at the end of the movement a fixed thing.

323.12 Branch K: abelian and non-abelian

(from C2, C3, C6, branches A, B, I.) Nothing new is introduced. Whether a join depends on the order is not assumed either way. It is computed, and the answer depends on how many arrows are given and at what height.

AB1. The question. For arrows \(p,q:a=a\) both \(p\,q\) and \(q\,p\) are arrows \(a=a\) (C2). The joins are called abelian when \(p\,q=q\,p\) for all of them, and non-abelian otherwise. The difference is measured by

\[ [p,q]=p\,q\,p^{-1}q^{-1}, \]
which is \(\mathrm {id}\) exactly when \(p\,q=q\,p\) (C3).

AB2. One arrow: abelian. In branch I the kind \(S\) has one arrow \(\ell \). Every arrow \(b=b\) is a join of \(\ell \)’s and \(\ell ^{-1}\)’s, and by C3

\[ \ell ^{m}\,\ell ^{n}=\ell ^{m+n}=\ell ^{n}\,\ell ^{m} . \]
With one letter there is no order to exchange. So \(\mathbb Z\) is abelian, and so is every \(C_n\) of branch J.

AB3. Two arrows: non-abelian. Take the adjoint of I1 for two sources at one thing: a kind \(S_2\) with

\[ \text {maps } S_2\to X\ \ \leftrightarrow \ \ (x,\ p:x=x,\ q:x=x), \]
given by a thing \(b\) and two arrows \(\ell ,m:b=b\). As in I3 and I4, a family over \(S_2\) is a kind \(K\) with two isomorphisms \(e_1,e_2:K\cong K\), and carrying \(k:K\) gives \(r\) with
\[ r(\ell \,m)=e_2(e_1(k)),\qquad r(m\,\ell )=e_1(e_2(k)). \]
Let \(K\) be the kind of three things \(0,1,2\), \(e_1\) the exchange of \(0,1\) and \(e_2\) the exchange of \(1,2\) (branch B), and \(k=0\):
\[ e_2(e_1(0))=e_2(1)=2,\qquad e_1(e_2(0))=e_1(0)=1 . \]
Since \(2\neq 1\), \(\ell \,m\neq m\,\ell \). The arrows of \(S_2\) are the words in two letters and their reverses, and their join is non-abelian.

AB4. Where it first happens: three places. On two things the only isomorphisms are \(\mathrm {id}\) and the swap \(\sigma \), and they commute. On three things the two exchanges of AB3 share a place and do not commute, but they satisfy

\[ e_1\,e_2\,e_1=e_2\,e_1\,e_2 , \]
both sides being the exchange of \(0\) and \(2\). So the first non-abelian join is that of two swaps that share a place.

AB5. Reading a word by counting makes it abelian. Read a word of \(S_2\) as the pair (turns of \(\ell \), turns of \(m\)), each an integer as in branch B. This reading keeps joins, sends \(\ell \,m\) and \(m\,\ell \) to the same pair \((1,1)\), and sends every \([p,q]\) to \((0,0)\). Its values are \(\mathbb Z\times \mathbb Z\), which is abelian (AB2, place by place). Counting forgets the order and keeps the numbers.

AB6. Two joins with one identity are one join, and abelian. Let a kind have two joins \(\circ \) and \(\star \) with the same identity \(1\) and

\[ (x\circ y)\star (z\circ w)=(x\star z)\circ (y\star w). \]
Then
\[ x\star y=(x\circ 1)\star (1\circ y)=(x\star 1)\circ (1\star y)=x\circ y, \]
\[ x\star y=(1\circ x)\star (y\circ 1)=(1\star y)\circ (x\star 1)=y\circ x . \]
So \(\circ \) and \(\star \) are the same, and \(x\circ y=y\circ x\).

AB7. At height two every join is abelian. By branch A arrows stand at the ends of arrows. Let \(\alpha ,\beta \) be arrows from \(\mathrm {id}_a\) to \(\mathrm {id}_a\). They have two joins: the join of C2 in the kind of arrows \(a=a\), and the join obtained because join itself keeps arrows (C6), which joins them side by side as arrows \(\mathrm {id}\,\mathrm {id}=\mathrm {id}\,\mathrm {id}\). Both have the identity \(\mathrm {id}_{\mathrm {id}_a}\), and the equation of AB6 is C6 for join. Hence

\[ \alpha \,\beta =\beta \,\alpha . \]
Non-abelian joins exist only at the first height.

AB8. Idempotent values: abelian. On the two values (C21) write \(x\,y\) for “and” and \(x+y\) for “differ”. Every value has \(x\,x=x\). Then \((x+y)(x+y)=x+y\) expands to \(x+x\,y+y\,x+y=x+y\), so \(x\,y+y\,x=0\), and since \(u+u=0\) this is \(x\,y=y\,x\). Here the order is lost because each thing joined to itself is itself: the source’s own equation \(1\,1=1\).

AB9. Summary.

what is joined order matters? why
arrows of \(S\): one arrow no one letter (AB2)
arrows of \(S_2\): two arrows yes three things, two exchanges (AB3)
isomorphisms of two things no only \(\mathrm {id}\) and \(\sigma \) (AB4)
isomorphisms of three things yes \(e_1e_2\neq e_2e_1\) (AB4)
words read by counting no AB5
arrows between arrows at \(\mathrm {id}\) no two joins, one identity (AB7)
the two values under “and” no \(x\,x=x\) (AB8)
\(\sqcup \) and \(\times \) of kinds no, up to \(\cong \) C20, by \(\sigma \)

Abelian has three sources in the chain: one letter, one identity shared by two joins, and idempotence. Non-abelian has one: two different arrows at one thing, seen first in three places.

323.13 Branch L: heights and directions — groupoids, categories, toposes

(from C1–C7, C16–C18, C24, C26, branches A, H, I.) Nothing new is introduced. The words below name what the chain already has, by two counts: up to what height there are arrows between arrows, and up to what height an arrow may lack a reverse.

HC1. Height. C0 gives arrows \(a=x\). By branch A the arrows \(p=q\) between arrows are again arrows, and so on. An arrow between things is at height \(1\), between arrows of height \(1\) at height \(2\), and so on without end. C1–C3 hold at each height.

HC2. A kind is an \(\infty \)-groupoid. In a kind every arrow has a reverse (C3), at every height (HC1). Things, arrows with reverses, arrows between those with reverses, without end: this is an \(\infty \)-groupoid. Its laws are not stated separately. Each of them, at each height, is an arrow given by C1: at \(\mathrm {id}\) it is \(\mathrm {id}\). For example the two ways of rebracketing a join of four,

\[ ((p\,q)\,r)\,t=p\,(q\,(r\,t)), \]
are equal by C1 on \(t\), then \(r\), then \(q\): at the identities both are \(\mathrm {id}\).

HC3. Levels. Count from the source. A kind is at level \(-2\) when it has exactly one thing (as the pairs \((x,p:a=x)\) of C1). It is at level \(n+1\) when each of its kinds of arrows \(a=b\) is at level \(n\).

level the kind is in the chain
\(-2\) exactly one thing \(1\) (C16); the rule C1
\(-1\) at most one thing: a statement C21
\(0\) a set: \(a=b\) is a statement \(\mathbb N\), \(2\), \(C_n\)
\(1\) a groupoid: \(a=b\) is a set \(S\) of branch I, with \((b=b)=\mathbb Z\)
\(n\) an \(n\)-groupoid
none an \(\infty \)-groupoid any kind (HC2)

HC4. Direction. A map between kinds (C6) need not have a reverse. So among kinds there are arrows of height \(1\) that go one way. Above them nothing new goes one way: two maps \(f,g:A\to B\) are things of the kind \(A\to B\) (C18), and an arrow \(f=g\) there has a reverse (HC2). Write \((n,r)\) when there are arrows up to height \(n\) and those above height \(r\) all have reverses.

HC5. The kinds form an \((\infty ,1)\)-category. Kinds, maps, and the arrows between maps at every height: one-way at height \(1\), reversible above. Identity and composition are C6, and their laws are equalities of what is written. The maps from \(A\) to \(B\) are not a bare collection but a kind, hence an \(\infty \)-groupoid: this is what the “\(1\)” in \((\infty ,1)\) says. An isomorphism (C5) is a map with a reverse, and by branch H the isomorphisms \(A\cong B\) are the arrows \(A=B\) of \(U\): the \(\infty \)-groupoid of kinds is the reversible part of this category.

HC6. Raising the direction. A category is itself a thing: the source can be said of categories. Between categories there are functors (C6), and between functors natural families (C7), which need not have reverses. So categories, functors and natural families have one-way arrows at heights \(1\) and \(2\): a \((2,2)\)-category, and \((\infty ,2)\) when the categories are \((\infty ,1)\). Saying the source of these again raises \(r\) by one each time. With no bound on \(r\) this is \((\infty ,\infty )\).

HC7. All of them in one table.

\((n,r)\) name from
\((0,0)\) set HC3, level \(0\)
\((1,0)\) groupoid C3
\((\infty ,0)\) \(\infty \)-groupoid C3 at every height (HC2)
\((1,1)\) category C4, with sets of arrows
\((\infty ,1)\) \((\infty ,1)\)-category C6 over HC2 (HC5)
\((2,2)\) \(2\)-category C6, C7 (HC6)
\((\infty ,2)\) \((\infty ,2)\)-category HC6 over HC5
\((\infty ,\infty )\) \((\infty ,\infty )\)-category HC6 without bound

The first number is how far C1 is repeated upward (branch A); the second is how many times a reverse is not asked for (C6, then HC6).

HC8. Topos. Take the kinds of level \(0\) with their maps. They have:

structure from
\(1\) and pairs C16, C17
the part where two maps agree C23, C24: \(\{x:f(x)=g(x)\}\)
exponents C18
a kind \(2\) of values, with parts of \(X\) \(\leftrightarrow \) maps \(X\to 2\) C21, C24
\(0\), \(\sqcup \), and the kind of numbers C16, C17, C27

The first four lines are what is called an elementary topos (finite limits, exponents, a classifier of parts); the last adds sums and the numbers. Since the values are the two of C21, it is a topos in which every part has a complement.

HC9. \((\infty ,1)\)-topos. Drop the restriction to level \(0\): all kinds, with the structure of HC5. Besides HC8 they have:

structure from
gluing along an arrow I1 (the kind \(S\)), with \(\sqcup \) of C17
a kind \(U\) with families over \(X\) \(\leftrightarrow \) maps \(X\to U\) C26, I3
in \(U\), an arrow \(A=B\) is an isomorphism \(A\cong B\) branch H

In HC8 the classifier \(2\) classifies parts; here \(U\) classifies all families, and branch H is what makes it do so correctly. Finite limits, finite gluings, exponents, a classifier of parts and a classifier of families are what is called an elementary \((\infty ,1)\)-topos. The kinds of level \(0\) inside it are the topos of HC8, and the kinds of level \(-1\) are its logic (C21).

HC10. One picture.

said of gives classifier
a thing: \(a=a\) at every height \(\infty \)-groupoid (a kind)
kinds, with maps \((\infty ,1)\)-category
level \(-1\) only logic
level \(0\) only topos \(2\)
all levels \((\infty ,1)\)-topos \(U\)
categories, with functors and natural families \((\infty ,2)\), and so on to \((\infty ,\infty )\)

HC11. What is and is not claimed. HC2–HC7 name what C1, C3, C6, C7 and branch A give; nothing is added. HC8 and HC9 list structure the chain has and match it with the usual definitions; they use \(U\) (C26) and the step of branch H marked there. Not claimed: that every law of an \(\infty \)-groupoid is written out (each is an instance of C1, and there is no end of them); and that the kinds are a topos in the larger sense that asks for all gluings of any size, which the chain does not give.

323.14 The same step, each time

The chain has one place that is not a computation: the whole of a level, \(U\) (C26). The same place has now appeared four times under four names. This subsection puts them side by side.

GS1. Four appearances.

the pieces: computed the whole: the step the fixed thing
numbers each \(n\); each world \(C_n\) (branch J) “every \(n\)”; \(\mathbb N\) as a kind (C27, J9) \(\infty =s(\infty )\)
sets each finite graph; each stage of the least side all graphs; \(V\) (branch F) \(\Omega =\{\Omega \}\)
self-maps \(j\) on a piece \(V_\lambda \) (K1) \(j\) on \(V\): only \(\mathrm {id}\) (K2, Q5) \(j(\lambda )=\lambda \)
toposes finite gluings: elementary (HC8, HC9) gluings of any size: Grothendieck \(U\), closed under its own operations

GS2. The fourth row is the first row. A Grothendieck topos has sums of any size. In particular it has

\[ 1\sqcup 1\sqcup 1\sqcup \cdots , \]
the source put side by side without end in one act, and this sum is its kind of numbers. So “gluings of any size” contains “\(\mathbb N\) is a kind”: the step from elementary to Grothendieck is the step of C27, taken for all shapes at once and not only for one more.

GS3. “Of any size” needs \(U\). To say “all gluings of small things” one must say what is small, and that is a kind of kinds closed under the operations: the \(U\) of C26. In set theory this is a universe in Grothendieck’s sense, introduced for exactly this purpose; that one exists is not provable there and amounts to a large cardinal. Above it stands the next, \(U\in U'\in \cdots \), the levels of C26. So the fourth row needs the same thing as the others, and names it openly.

GS4. The common shape. In each row:

the pieces are finite or bounded, and everything about one piece is a computation
the whole is not reached by any number of pieces
it enters as one step, the same in every row: the whole of a level
on the whole what is given uniformly is only the identity (Q5, K2)
at the end there is a thing fixed by the movement that built the pieces

GS5. What this says about the chain. The chain does not have several unexplained places. It has one, and \(\mathbb N\), \(V\), “\(j=\mathrm {id}\) on the whole” and Grothendieck’s universes are that one place seen from the numbers, from the sets, from the maps and from the toposes. Everything below it is computed from \(a=a\) by C1; branch J shows that even the numbers can be used below it, one world at a time.

323.15 Branch M: space — topology and geometry

(from C3, C6, C16–C18, C22–C24, branches A, B, C, H, I, J, K, L.) Nothing new is introduced. A kind already is a space, and the notions of topology and geometry are readings of what the chain has.

GT1. A kind is a space. By HC2 a kind has things, arrows between things, arrows between arrows, and so on. Read: points, paths, deformations of paths.

in topology in the chain from
point thing C4
path from \(a\) to \(x\) arrow \(a=x\) C0
constant path, reversed path, joined paths \(\mathrm {id}\), \(p^{-1}\), \(p\,q\) C2, C3
deformation of one path into another arrow \(p=q\) branch A
continuous map map: keeps paths, \(\mathrm {id}\) and join C6
equivalent spaces \(A\cong B\), that is, \(A=B\) C5, branch H
product, sum, space of maps \(\times \), \(\sqcup \), \(B^{A}\) C17, C18

GT2. The invariants.

invariant is abelian?
pieces of \(X\) things, the same when an arrow joins them
loops at \(a\) arrows \(a=a\), with join need not be (AB3)
higher loops at \(a\) arrows from \(\mathrm {id}\) to \(\mathrm {id}\), at height \(n\ge 2\) always (AB7)

That the higher loops commute and the first need not is AB7 and AB3: two joins with one identity, against two arrows at one thing.

GT3. Spheres by gluing. Two things with nothing between them is \(2=1\sqcup 1\). One thing with one arrow glued on is \(S\) of I1: the circle. Two things glued along a copy of the previous one gives the next:

\[ 2,\qquad S,\qquad \text {two things glued along } S,\qquad \dots \]
The loops of the circle are \(\mathbb Z\) (I2): a loop is its number of turns. The kind \(S_2\) of AB3, one thing with two arrows, has non-abelian loops.

GT4. Unwinding. A family of sets over \(X\) is a map \(X\to U\) (I3): over each point a set, carried along each path. Over the circle this is a set with an isomorphism to itself. Taking \(\mathbb Z\) with “one more” gives the family in which a turn below is a step above: the circle unwound into a line. The worlds \(C_n\) of branch J are the families in which \(n\) turns return.

GT5. Counting. Maps \(X\to S\), up to arrows, read off how \(X\) winds around a circle. Reading loops by counting (AB5) forgets their order and keeps the numbers: the loops made abelian. Counting pieces with alternating signs, points \(-\) arrows \(+\) arrows between arrows \(-\cdots \), uses the negative of branch B.

GT6. Open parts. Between the two values there is one arrow that has no reverse, \(0\to 1\) (C16). A part of \(X\) is a map \(X\to 2\) (C24), and parts are ordered by it (C21). A family of parts closed under “and” and under “some” (C22) is a topology; a map is continuous for it when taking preimages (C24) keeps the family. A family over \(X\) that can be given piece by piece on a cover and glued (HC9) is a sheaf. “Some” over a family of any size is the step of C26.

GT7. Geometry is a kind with its symmetries. A group is the arrows \(b=b\) of a kind with one thing (I1, with any arrows given). It acts on \(K\) when \(K\) is a family over that kind (I3): each arrow is carried to an isomorphism of \(K\). The things of \(K\) the same under the action are a kernel (branch C). A geometry is such a \(K\); what belongs to it is what the isomorphisms keep. Whether the group is abelian is branch K.

GT8. The plane. On pairs \((x,y)\) of numbers take the swap and one sign (branch B):

\[ \sigma (x,y)=(y,x),\qquad K(x,y)=(x,-y),\qquad J=\sigma K,\quad J(x,y)=(-y,x). \]
\(\sigma \) and \(K\) are reflections, \(\sigma ^{2}=K^{2}=\mathrm {id}\); \(J\) is a quarter turn, \(J^{2}=-1\), \(J^{4}=\mathrm {id}\). All keep \(x^{2}+y^{2}\), which is \((x+yJ)(x-yJ)\). A reflection and a turn do not commute:
\[ \sigma J(x,y)=(x,-y),\qquad J\sigma (x,y)=(-x,y),\qquad \sigma J=J^{-1}\sigma . \]

GT9. Angle is a turn; polygons are the worlds \(C_n\). The turns of the plane that return after \(n\) steps are \(C_n\) of branch J: the turns of a figure with \(n\) corners. With the reflections added they are no longer abelian for \(n\ge 3\); for \(n=3\) these are the isomorphisms of three things of AB4, the triangle. A turn that never returns is \(\mathbb Z\), and the circle of GT3 is the turns up to a full one.

GT10. Three planes, by the square of the unit. Write a pair as \(x+yu\). The square of \(u\) can be the negative, the source, or the denial:

\(u^{2}\) \(u\) is kept quantity \((x+yu)(x-yu)\) plane
\(-1\) \(J=\sigma K\) \(x^{2}+y^{2}\): circles Euclidean
\(+1\) \(\sigma \) \(x^{2}-y^{2}\): hyperbolas Minkowski
\(0\) \(\varepsilon \) \(x^{2}\): parallel lines Galilean

The three are one construction with the three possible values of one square.

GT11. The line with one point more. A fraction is a pair \((a,b)\) up to a common factor (branch B). Allowing \(b=0\) adds one thing, \((1,0)\): the points of a line together with a point at its end. The maps \((a,b)\mapsto (pa+qb,\ ra+sb)\) act on it and keep this description. With complex numbers in place of numbers the same is a sphere, and its part \(x^{2}+y^{2}<1\) with the maps that keep it is the hyperbolic plane.

GT12. The infinitely small. With \(\varepsilon ^{2}=0\) of GT10, a map of numbers is read at \(x+\varepsilon \):

\[ f(x+\varepsilon )=f(x)+f'(x)\,\varepsilon . \]
This gives \(f'\), and its rule for products is one line of C20:
\[ (f+f'\varepsilon )(g+g'\varepsilon )=f\,g+(f'g+f\,g')\,\varepsilon . \]
A thing \(x+v\,\varepsilon \) is a point with a direction.

GT13. Curvature. A family over \(X\) carries its things along paths (branch A). Carrying around a loop \(\ell \) is an isomorphism \(\ell _{*}\) (I3). The family is flat where this is \(\mathrm {id}\), and curved otherwise; for two directions the failure is the commutator \([p_{*},q_{*}]\) of AB1. So curvature is the non-abelian part of carrying.

GT14. Summary.

subject is, in the chain from
homotopy kinds, with arrows at every height GT1–GT3
coverings, sheaves families over a kind GT4, GT6
homology, counting loops made abelian; alternating count GT5
point-set topology parts, ordered by \(0\to 1\) GT6
symmetry, groups arrows \(b=b\) of a one-thing kind, acting on a family GT7
Euclidean, Minkowski, Galilean pairs with \(u^{2}=-1,+1,0\) GT8–GT10
projective, spherical, hyperbolic pairs up to a factor GT11
differential \(\varepsilon ^{2}=0\); carrying around loops GT12, GT13

GT15. What is used. GT1–GT5, GT7–GT10 and GT12 use only the chain and its branches. The continuous line (numbers given by all their approximations by fractions) is a kind of parts of an infinite kind, and “some” over families of any size in GT6 is the same: the step of C26 (Section 323.14). Individual theorems of topology and geometry are not derived here; what is shown is where each subject sits.

323.16 Branch N: from the identity to the triangle, and everything from the triangle

(from C0–C3, C6, C13, C20–C25, branch B.) Nothing new is introduced, and the triangle is not assumed: it is computed from \(\mathrm {id}\) in TR4. From it come symmetry and transitivity, kernel and image, membership, part and quotient, inclusion and exclusion, groups, classes, quotient groups and orbits. Each item names the items it uses.

From the identity to the triangle

TR0. The identity. \(a=a\), written \(\mathrm {id}\) (C0).

TR1. The rule. (TR0) What is given at \(\mathrm {id}\) is given at every \(a=x\) (C1).

TR2. Join and reverse. (TR1) For \(p:a=x\), \(q:x=y\): the rule on \(q\), with \(p\) at \(\mathrm {id}\), gives \(p\,;q\). The rule on \(p\), with \(\mathrm {id}\) at \(\mathrm {id}\), gives \(p^{\circ }:x=a\).

TR3. Two laws. (TR1, TR2) By the rule on \(p\), where at \(\mathrm {id}\) both read \(\mathrm {id};\mathrm {id}=\mathrm {id}\):

\[ p\,;p^{\circ }=\mathrm {id},\qquad \mathrm {id}\,;p=p . \]

TR4. The triangle, for equations. (TR3)

\[ p\,;p^{\circ };p=\mathrm {id}\,;p=p . \]
It is the two lines of TR3 joined.

TR5. Equality as a relation. (TR2, TR3) Write \(E(x,y)\) for \(x=y\): the diagonal, the pairs with both ends the same. Join and reverse give \(E;E=E\) and \(E^{\circ }=E\).

TR6. Join of relations. (TR1) \((R;S)(x,z)\) when \(R(x,y)\) and \(S(y,z)\) for some \(y\); \(R^{\circ }(y,x)\) when \(R(x,y)\). By the rule on the middle equation, where at \(\mathrm {id}\) it is \(R\) itself,

\[ R\,;E=R=E\,;R,\qquad (R;S)^{\circ }=S^{\circ };R^{\circ } . \]

TR7. One half holds always. (TR6) If \(R(x,y)\) then \(R(x,y)\), \(R^{\circ }(y,x)\), \(R(x,y)\): the same arrow used three times. So

\[ R\le R\,;R^{\circ };R \]
for every relation.

TR8. Map. For a map \(f\) (C6) let \(F(x,y)\) be \(f(x)=y\).

TR9. Unit. (TR1) \(x=x'\Rightarrow f(x)=f(x')\), by the rule on \(x=x'\): at \(\mathrm {id}\) it is \(f(x)=f(x)\). That is

\[ E\le F\,;F^{\circ } : \]
the diagonal of \(X\) lies in \(F;F^{\circ }\).

TR10. Counit. (TR2) \(f(x)=y\) and \(f(x)=y'\) give \(y=y'\), by reverse and join. That is

\[ F^{\circ };F\le E . \]

TR11. The triangle, for maps. (TR7, TR10) \(F;F^{\circ };F\le F;E=F\) by TR10 and TR6, and with TR7

\[ F\,;F^{\circ };F=F . \]
Written out: \(f(x)=y_1\), \(f(x')=y_1\), \(f(x')=y\) give \(f(x)=y_1=f(x')=y\): one reverse and two joins. It is TR4 read through \(f\).

TR12. It is the triangle of an adjoint. (TR9–TR11) With \(F\) on the left and \(F^{\circ }\) on the right, TR9 is the unit and TR10 the counit of C11, and the two triangles of C13 are \(F;F^{\circ };F=F\) and \(F^{\circ };F;F^{\circ }=F^{\circ }\).

TR13. Reversed, it is again a triangle. (TR11, TR6) \((x;x^{\circ };x)^{\circ }=x^{\circ };x;x^{\circ }\). So if \(x\) has the triangle, so has \(x^{\circ }\).

From the triangle

From here only \(x\,;x^{\circ };x=x\) is used.

TR14. \(x;x^{\circ }\) is fixed by join. (TR11)

\[ (x;x^{\circ });(x;x^{\circ })=(x;x^{\circ };x);x^{\circ }=x;x^{\circ } . \]

TR15. \(x;x^{\circ }\) is fixed by reverse. (TR6) \((x;x^{\circ })^{\circ }=x^{\circ \circ };x^{\circ }=x;x^{\circ }\).

TR16. The same for \(x^{\circ };x\). (TR13–TR15) Write

\[ \ker x=x\,;x^{\circ },\qquad \operatorname {im}x=x^{\circ };x . \]
For a map, \(\ker F\) is the diagonal of \(Y\) drawn back to \(X\) along \(f\): \(x\sim x'\) when \(f(x)=f(x')\).

TR17. Transitivity and symmetry. (TR14, TR15) A relation \(e\) with \(e;e=e\) and \(e^{\circ }=e\) is called a symmetric idempotent. \(e;e=e\) contains “\(e(x,y),e(y,z)\Rightarrow e(x,z)\)” and \(e^{\circ }=e\) is “\(e(x,y)\Rightarrow e(y,x)\)”. Kernel and image have both, and neither was assumed.

TR18. \(x\) is an isomorphism from its kernel to its image. (TR11, TR16)

\[ x;x^{\circ }=\ker x,\qquad x^{\circ };x=\operatorname {im}x,\qquad \ker x\,;x\,;\operatorname {im}x=x;x^{\circ };x;x^{\circ };x=x . \]
The third line says \(x\) goes from \(\ker x\) to \(\operatorname {im}x\); the first two say \(x^{\circ }\) is its reverse (C5).

TR19. The quotient by the kernel is the image. (TR18) For a map \(f\), TR18 reads

\[ X/\ker f\ \cong \ \operatorname {im}f . \]

TR20. Membership. (TR17) For a symmetric idempotent \(e\): \(x\in e\) means \(e(x,x)\), and two of its things are the same when \(e(x,y)\). So \(e\) is a kind cut out of \(X\), and membership is its diagonal.

TR21. Part and quotient. (TR9, TR10, TR20)

condition things for a map
part \(e\le E\) some of \(X\), the same when equal the image, by TR10
quotient \(E\le e\) all of \(X\), the same when related the kernel, by TR9

TR22. Reflexivity. (TR9) \(e(x,x)\) for every \(x\) is the condition \(E\le e\). For the kernel of a map it is the unit.

TR23. Parts: meet and order. (TR21) If \(i,j\le E\) then \(i;j\) holds at \((x,y)\) only when \(x=y\) is in both:

\[ i\,;j=j\,;i=i\cap j,\qquad i\le j\iff i\,;j=i . \]

TR24. Inclusion and exclusion. (TR23, C20, C21) The exclusion of \(i\) is \(E-i\). For two parts

\[ i\cup j=E-(E-i);(E-j)=i+j-i\,;j , \]
and counting a part by its diagonal, \(|i|=\sum _x i(x,x)\),
\[ |i\cup j|=|i|+|j|-|i\cap j| . \]
For \(i_1,\dots ,i_k\), expanding \(E-\prod _m(E-i_m)\) by C20,
\[ |i_1\cup \cdots \cup i_k|=\sum _{T\neq \varnothing }(-1)^{|T|+1}\Bigl |\bigcap _{m\in T}i_m\Bigr | . \]

TR25. Group. (TR2–TR4) A group is the arrows of a kind with one thing, with \(\mathrm {id}\), join and reverse. By TR3, \(g;g^{\circ }=\mathrm {id}=g^{\circ };g\): kernel and image of every \(g\) are \(\mathrm {id}\), so every \(g\) is an isomorphism (TR18). Its triangle is TR4.

TR26. The three laws and the three properties. (TR14, TR15, TR22) Let a group act on \(X\) and \(O(x,x')\) when \(x'=g\cdot x\) for some \(g\).

the group has so \(O\) has as in
\(\mathrm {id}\) \(E\le O\): reflexive TR22
reverse \(O^{\circ }=O\): symmetric TR15
join \(O;O=O\): transitive TR14

TR27. Classes. (TR14–TR16) For parts of a group put \(A;B=\{a\,b\}\) and \(A^{\circ }=\{a^{\circ }\}\). Let a part \(H\) with a thing have the triangle,

\[ H\,;H^{\circ };H=H . \]
By TR14–TR16, \(H^{\circ };H\) is fixed by join and by reverse, and it contains \(h^{\circ }h=\mathrm {id}\). For \(h\) in \(H\), \(H=h;(H^{\circ };H)\): one inclusion is \(h'=h\,(h^{\circ }h')\), the other is the triangle. So \(H\) is a class of \(H^{\circ };H\).

TR28. Subgroup. (TR27) A subgroup is such an \(H\) containing \(\mathrm {id}\). Then \(H^{\circ };H=H\), and so

\[ H^{\circ }=H,\qquad H\,;H=H . \]

TR29. Counting classes. (TR28, C20) For a subgroup \(H\) the classes \(g;H\) do not meet and cover the group, and \(h\mapsto g\,h\) is an isomorphism of \(H\) with \(g;H\), with reverse \(x\mapsto g^{\circ }x\). Hence

\[ |G|=|G/H|\cdot |H| . \]

TR30. Quotient group. (TR28) If \(g;H=H;g\) for every \(g\), then

\[ (g;H);(g';H)=g;(H;g');H=g;g';H;H=(g\,g');H , \]
so the classes are a group.

TR31. The kernel of a map of groups. (TR14, TR15, TR19) For \(\varphi \) keeping join, \(\ker \varphi =\varphi ;\varphi ^{\circ }\) is a symmetric idempotent with \(E\le \ker \varphi \). The class of \(\mathrm {id}\), \(K=\{g:\varphi (g)=\mathrm {id}\}\), is a subgroup with \(g;K=K;g\), since \(\varphi (g\,k\,g^{\circ })=\mathrm {id}\). By TR19,

\[ G/K\ \cong \ \operatorname {im}\varphi . \]

TR32. Orbits. (TR19, TR29) For \(k\) in \(X\) and \(f(g)=g\cdot k\): the image is the orbit of \(k\), and the kernel is the classes of the part \(G_k\) fixing \(k\) (the equation \(g\cdot k=k\) drawn back to the group). So

\[ \text {orbit of }k\ \cong \ G/G_k,\qquad |G|=|\text {orbit}|\cdot |G_k| . \]
The whole of this branch

items what comes out from
TR0–TR3 identity, rule, join, reverse, \(p;p^{\circ }=\mathrm {id}\) the source
TR4 the triangle, for equations TR3
TR5–TR7 equality, join of relations, \(R\le R;R^{\circ };R\) TR1, TR2
TR9, TR10 unit, counit TR1, TR2
TR11–TR13 the triangle for maps; the adjoint’s triangle TR7, TR10
TR14–TR17 kernel and image; transitivity and symmetry the triangle
TR18, TR19 \(X/\ker f\cong \operatorname {im}f\) the triangle
TR20–TR23 membership, part, quotient, reflexivity, meet, order TR17
TR24 inclusion and exclusion TR23
TR25, TR26 group; its laws as reflexive, symmetric, transitive TR3, TR14, TR15
TR27–TR29 classes, subgroups, \(|G|=|G/H|\,|H|\) the triangle
TR30–TR32 quotient groups, kernels of maps of groups, orbits TR19, TR28

What is put down is TR0 and TR1. TR7 holds for every relation, so what the triangle asks is the other half, \(x;x^{\circ };x\le x\); it is given by TR3 for equations and by the counit TR10 for maps.

323.17 Branch N in Lean 4.

Branch N is checked in BranchN.lean (Figure 283, Listing 391). TR3–TR23 are proved in general: in particular tri_map (a map has the triangle) and ker_trans, im_trans (from the triangle alone, kernel and image are transitive). TR24 is computed on the numbers below \(30\), and TR25–TR32 on the six isomorphisms of three things, where not_abelian and braid also check AB3 and AB4.

PIC

Figure 283: The file BranchN.lean in the Lean 4 web editor (Lean v4.36.0-rc2): no errors and no warnings for branch N.

Listing 391: BranchN.lean: branch N, from the identity to the triangle and from the triangle.

BranchN.lean

/-
  Branch N: from the identity to the triangle, and everything from the triangle.
  Core Lean 4, no Mathlib.  TR numbers refer to the items of the branch.
-/

namespace BranchN

section Relations

variable {A B C : Type}

abbrev Rel (A B : Type) := A → B → Prop
abbrev rjoin (R : Rel A B) (S : Rel B C) : Rel A C := fun x z => ∃ y, R x y ∧ S y z
abbrev rrev (R : Rel A B) : Rel B A := fun y x => R x y
abbrev eqRel (A : Type) : Rel A A := fun x y => x = y
abbrev sub (R S : Rel A B) : Prop := ∀ x y, R x y → S x y

/-! ## TR3, TR4: the triangle for equations -/

theorem tr3 {a x : A} (p : a = x) : p.trans p.symm = rfl := rfl
theorem tr3' {a x : A} (p : a = x) : (rfl : a = a).trans p = p := rfl
/-- TR4: p ; p° ; p = p -/
theorem tr4 {a x : A} (p : a = x) : (p.trans p.symm).trans p = p := rfl

/-! ## TR5, TR6: equality as a relation; join of relations -/

theorem eq_join_eq : rjoin (eqRel A) (eqRel A) = eqRel A := by
  funext x z
  apply propext
  constructor
  · intro ⟨y, e1, e2⟩
    exact e1.trans e2
  · intro e
    exact ⟨x, rfl, e⟩

theorem eq_rev : rrev (eqRel A) = eqRel A := by
  funext x y
  apply propext
  constructor
  · intro e
    exact e.symm
  · intro e
    exact e.symm

theorem rjoin_eq (R : Rel A B) : rjoin R (eqRel B) = R := by
  funext x z
  apply propext
  constructor
  · intro ⟨y, r, e⟩
    exact e ▸ r
  · intro r
    exact ⟨z, r, rfl⟩

theorem eq_rjoin (R : Rel A B) : rjoin (eqRel A) R = R := by
  funext x z
  apply propext
  constructor
  · intro ⟨y, e, r⟩
    exact e ▸ r
  · intro r
    exact ⟨x, rfl, r⟩

theorem rrev_rjoin (R : Rel A B) (S : Rel B C) :
    rrev (rjoin R S) = rjoin (rrev S) (rrev R) := by
  funext z x
  apply propext
  constructor
  · intro ⟨y, r, s⟩
    exact ⟨y, s, r⟩
  · intro ⟨y, s, r⟩
    exact ⟨y, r, s⟩

/-! ## TR7: one half holds for every relation -/

theorem half_always (R : Rel A B) : sub R (rjoin (rjoin R (rrev R)) R) :=
  fun x y h => ⟨x, ⟨y, h, h⟩, h⟩

/-- the other half: this is what "having the triangle" asks -/
abbrev Tri (R : Rel A B) : Prop := sub (rjoin (rjoin R (rrev R)) R) R

/-! ## TR8-TR13: maps; unit, counit, the triangle for maps -/

abbrev graphOf (f : A → B) : Rel A B := fun x y => f x = y

/-- TR9: the unit -/
theorem unit_map (f : A → B) : sub (eqRel A) (rjoin (graphOf f) (rrev (graphOf f))) := by
  intro x x' e
  cases e
  exact ⟨f x, rfl, rfl⟩

/-- TR10: the counit -/
theorem counit_map (f : A → B) : sub (rjoin (rrev (graphOf f)) (graphOf f)) (eqRel B) := by
  intro y y' h
  match h with
  | ⟨_, p, q⟩ => exact p.symm.trans q

/-- TR11: the triangle for maps, as the half that is asked -/
theorem tri_map (f : A → B) : Tri (graphOf f) := by
  intro x y h
  match h with
  | ⟨_, ⟨_, p, q⟩, r⟩ => exact p.trans (q.symm.trans r)

/-- TR11: with TR7, the equation -/
theorem tri_map_eq (f : A → B) :
    rjoin (rjoin (graphOf f) (rrev (graphOf f))) (graphOf f) = graphOf f := by
  funext x y
  apply propext
  constructor
  · exact tri_map f x y
  · exact half_always (graphOf f) x y

/-- TR13: reversed, it is again a triangle -/
theorem tri_rev (R : Rel A B) (h : Tri R) : Tri (rrev R) := by
  intro y x hh
  match hh with
  | ⟨y', ⟨x', r1, r2⟩, r3⟩ => exact h x y ⟨x', ⟨y', r3, r2⟩, r1⟩

/-! ## TR14-TR17: kernel and image are fixed by join and by reverse -/

abbrev ker (R : Rel A B) : Rel A A := rjoin R (rrev R)
abbrev im (R : Rel A B) : Rel B B := rjoin (rrev R) R

/-- TR15: symmetric -/
theorem ker_symm (R : Rel A B) (x y : A) : ker R x y → ker R y x := by
  intro ⟨b, r1, r2⟩
  exact ⟨b, r2, r1⟩

/-- TR14: transitive, from the triangle -/
theorem ker_trans (R : Rel A B) (h : Tri R) (x y z : A) :
    ker R x y → ker R y z → ker R x z := by
  intro ⟨b, r1, r2⟩ ⟨c, r3, r4⟩
  exact ⟨c, h x c ⟨y, ⟨b, r1, r2⟩, r3⟩, r4⟩

theorem im_symm (R : Rel A B) (y y' : B) : im R y y' → im R y' y := by
  intro ⟨a, r1, r2⟩
  exact ⟨a, r2, r1⟩

theorem im_trans (R : Rel A B) (h : Tri R) (y y' y'' : B) :
    im R y y' → im R y' y'' → im R y y'' := by
  intro ⟨a, r1, r2⟩ ⟨c, r3, r4⟩
  exact ⟨a, r1, h a y'' ⟨c, ⟨y', r2, r3⟩, r4⟩⟩

/-- TR22: for a map, the kernel is reflexive (the unit) -/
theorem ker_refl_map (f : A → B) (x : A) : ker (graphOf f) x x := ⟨f x, rfl, rfl⟩
/-- TR16: for a map, the kernel is "f x = f x'" -/
theorem ker_map (f : A → B) (x x' : A) : ker (graphOf f) x x' ↔ f x = f x' := by
  constructor
  · intro ⟨_, p, q⟩
    exact p.trans q.symm
  · intro e
    exact ⟨f x, rfl, e.symm⟩
/-- TR21: for a map, the image is a part: inside equality (the counit) -/
theorem im_part_map (f : A → B) : sub (im (graphOf f)) (eqRel B) := counit_map f

/-! ## TR18, TR19: a relation with the triangle is an isomorphism kernel → image -/

/-- things the same in the kernel go to the same things -/
theorem ker_respects (R : Rel A B) (h : Tri R) (x x' : A) (y : B) :
    ker R x x' → R x y → R x' y := by
  intro ⟨b, r1, r2⟩ r
  exact h x' y ⟨x, ⟨b, r2, r1⟩, r⟩

/-- and only those -/
theorem ker_exact (R : Rel A B) (x x' : A) (y : B) : R x y → R x' y → ker R x x' :=
  fun r r' => ⟨y, r, r'⟩

/-- the values are the same in the image -/
theorem im_exact (R : Rel A B) (x : A) (y y' : B) : R x y → R x y' → im R y y' :=
  fun r r' => ⟨x, r, r'⟩

/-- and whatever is the same in the image is also a value -/
theorem im_respects (R : Rel A B) (h : Tri R) (x : A) (y y' : B) :
    im R y y' → R x y → R x y' := by
  intro ⟨a, r1, r2⟩ r
  exact h x y' ⟨a, ⟨y, r, r1⟩, r2⟩

/-! ## TR20-TR23: membership, parts, meet -/

/-- a part, as a relation inside equality -/
abbrev partRel (S : A → Prop) : Rel A A := fun x y => x = y ∧ S x

/-- membership is the diagonal of the part -/
theorem mem_diag (S : A → Prop) (x : A) : partRel S x x ↔ S x :=
  ⟨fun h => h.2, fun h => ⟨rfl, h⟩⟩

theorem part_le_eq (S : A → Prop) : sub (partRel S) (eqRel A) := fun _ _ h => h.1

/-- TR23: the join of two parts is their meet -/
theorem part_join (S T : A → Prop) :
    rjoin (partRel S) (partRel T) = partRel (fun x => S x ∧ T x) := by
  funext x y
  apply propext
  constructor
  · intro ⟨m, ⟨e1, s⟩, ⟨e2, t⟩⟩
    cases e1
    cases e2
    exact ⟨rfl, s, t⟩
  · intro ⟨e, s, t⟩
    cases e
    exact ⟨x, ⟨rfl, s⟩, ⟨rfl, t⟩⟩

end Relations

/-! ## TR24: inclusion and exclusion, counted -/

def cnt (n : Nat) (p : Nat → Bool) : Nat := ((List.range n).filter p).length

def m2 (x : Nat) : Bool := x % 2 == 0
def m3 (x : Nat) : Bool := x % 3 == 0
def m5 (x : Nat) : Bool := x % 5 == 0

theorem incl_excl_two :
    cnt 30 (fun x => m2 x || m3 x) + cnt 30 (fun x => m2 x && m3 x)
      = cnt 30 m2 + cnt 30 m3 := by decide +kernel

theorem incl_excl_three :
    cnt 30 (fun x => m2 x || m3 x || m5 x) + cnt 30 (fun x => m2 x && m3 x)
        + cnt 30 (fun x => m2 x && m5 x) + cnt 30 (fun x => m3 x && m5 x)
      = cnt 30 m2 + cnt 30 m3 + cnt 30 m5 + cnt 30 (fun x => m2 x && m3 x && m5 x) := by
  decide +kernel

/-- the exclusion of a join of exclusions -/
theorem union_is_excl :
    (List.range 30).all (fun x => (m2 x || m3 x) == !((!m2 x) && (!m3 x))) = true := by
  decide +kernel

/-! ## TR25-TR32: groups, on the six isomorphisms of three things -/

abbrev Perm := List Nat

def e : Perm := [0, 1, 2]
def s01 : Perm := [1, 0, 2]
def s12 : Perm := [0, 2, 1]
def s02 : Perm := [2, 1, 0]
def c1 : Perm := [1, 2, 0]
def c2 : Perm := [2, 0, 1]

def perms : List Perm := [e, s01, s12, s02, c1, c2]

def act (p : Perm) (i : Nat) : Nat := p.getD i 0
/-- join: first q, then p -/
def mul (p q : Perm) : Perm := [0, 1, 2].map (fun i => act p (act q i))
def inv (p : Perm) : Perm := (perms.find? (fun q => mul p q == e)).getD e

def setMul (X Y : List Perm) : List Perm :=
  perms.filter (fun z => X.any fun x => Y.any fun y => mul x y == z)
def setInv (X : List Perm) : List Perm := perms.filter (fun z => X.contains (inv z))
def sameSet (X Y : List Perm) : Bool := perms.all (fun z => X.contains z == Y.contains z)

/-- TR25: the laws of a group -/
theorem grp_id : perms.all (fun g => mul e g == g && mul g e == g) = true := by decide +kernel
theorem grp_inv : perms.all (fun g => mul g (inv g) == e && mul (inv g) g == e) = true := by
  decide +kernel
theorem grp_assoc :
    perms.all (fun g => perms.all fun h => perms.all fun k =>
      mul (mul g h) k == mul g (mul h k)) = true := by decide +kernel
/-- TR4 for a group: g ; g° ; g = g -/
theorem grp_triangle : perms.all (fun g => mul (mul g (inv g)) g == g) = true := by
  decide +kernel
/-- the only idempotent is the identity -/
theorem grp_idem : perms.all (fun g => (mul g g != g) || (g == e)) = true := by decide +kernel

/-- two exchanges that share a place do not commute -/
theorem not_abelian : mul s01 s12 ≠ mul s12 s01 := by decide +kernel
theorem braid : mul s01 (mul s12 s01) = mul s12 (mul s01 s12) := by decide +kernel

/-- TR28: a subgroup is fixed by reverse and by join -/
def H : List Perm := [e, s01]
def A3 : List Perm := [e, c1, c2]

theorem H_rev : sameSet (setInv H) H = true := by decide +kernel
theorem H_join : sameSet (setMul H H) H = true := by decide +kernel
theorem A3_rev : sameSet (setInv A3) A3 = true := by decide +kernel
theorem A3_join : sameSet (setMul A3 A3) A3 = true := by decide +kernel

/-- TR27: every class has the triangle, and X° ; X is the subgroup -/
theorem class_triangle :
    perms.all (fun g =>
      sameSet (setMul (setMul (setMul [g] H) (setInv (setMul [g] H))) (setMul [g] H))
        (setMul [g] H)) = true := by decide +kernel
theorem class_gives_subgroup :
    perms.all (fun g => sameSet (setMul (setInv (setMul [g] H)) (setMul [g] H)) H) = true := by
  decide +kernel

/-- TR29: all classes have as many things as H, and 6 = 3 · 2 -/
theorem class_size : perms.all (fun g => (setMul [g] H).length == H.length) = true := by
  decide +kernel
theorem lagrange : perms.length = 3 * H.length := by decide +kernel

/-- TR30: A3 is normal and its classes are a group; H is not normal -/
theorem A3_normal :
    perms.all (fun g => sameSet (setMul [g] A3) (setMul A3 [g])) = true := by decide +kernel
theorem H_not_normal :
    perms.all (fun g => sameSet (setMul [g] H) (setMul H [g])) = false := by decide +kernel
theorem quotient_join :
    perms.all (fun g => perms.all fun g' =>
      sameSet (setMul (setMul [g] A3) (setMul [g'] A3)) (setMul [mul g g'] A3)) = true := by
  decide +kernel

/-- TR31: a map of groups to the two values; its kernel is A3 -/
def sgn (p : Perm) : Nat := if A3.contains p then 0 else 1

theorem sgn_keeps_join :
    perms.all (fun g => perms.all fun h => sgn (mul g h) == (sgn g + sgn h) % 2) = true := by
  decide +kernel
theorem sgn_kernel : sameSet (perms.filter (fun g => sgn g == 0)) A3 = true := by
  decide +kernel
/-- G / K has as many things as the image -/
theorem first_iso_count :
    perms.length = A3.length * ((List.range 2).filter (fun v => perms.any fun g => sgn g == v)).length := by
  decide +kernel

/-- TR32: orbit and the part fixing a thing -/
def orbit0 : List Nat := (List.range 3).filter (fun j => perms.any fun g => act g 0 == j)
def fix0 : List Perm := perms.filter (fun g => act g 0 == 0)

theorem orbit_stabilizer : perms.length = orbit0.length * fix0.length := by decide +kernel
theorem fix0_is_subgroup :
    (sameSet (setInv fix0) fix0 && sameSet (setMul fix0 fix0) fix0) = true := by decide +kernel

end BranchN

323.18 Branch N in Lean 4, without axioms.

BranchN2.lean proves the statements of BranchN.lean (TR3–TR32, AB3, AB4) under the same names, with pointwise equality of maps in place of funext and \(\leftrightarrow \) in place of propext.

Listing 392: BranchN2.lean: branch N without axioms.

BranchN2.lean

/-
  Branch N: from the identity to the triangle, and everything from the triangle.
  Core Lean 4, no Mathlib.  TR numbers refer to the items of the branch.

  found/user/BranchN2 — the axiom-free version of
  ~/Downloads/The_Totality_Theorem/BranchN.lean (original untouched): same
  theorem names and statements, except that the equations between relations
  (eq_join_eq, eq_rev, rjoin_eq, eq_rjoin, rrev_rjoin, tri_map_eq, part_join)
  are stated pointwise with ↔ instead of = (no funext, no propext), and the
  finite checks are closed by kernel computation (rfl) instead of
  `decide +kernel`; `act` looks up with a hand-written getD' because core's
  List.getD depends on propext.
-/

namespace BranchN

section Relations

variable {A B C : Type}

abbrev Rel (A B : Type) := A → B → Prop
abbrev rjoin (R : Rel A B) (S : Rel B C) : Rel A C := fun x z => ∃ y, R x y ∧ S y z
abbrev rrev (R : Rel A B) : Rel B A := fun y x => R x y
abbrev eqRel (A : Type) : Rel A A := fun x y => x = y
abbrev sub (R S : Rel A B) : Prop := ∀ x y, R x y → S x y

/-! ## TR3, TR4: the triangle for equations -/

theorem tr3 {a x : A} (p : a = x) : p.trans p.symm = rfl := rfl
theorem tr3' {a x : A} (p : a = x) : (rfl : a = a).trans p = p := rfl
/-- TR4: p ; p° ; p = p -/
theorem tr4 {a x : A} (p : a = x) : (p.trans p.symm).trans p = p := rfl

/-! ## TR5, TR6: equality as a relation; join of relations -/

theorem eq_join_eq : ∀ x z : A, rjoin (eqRel A) (eqRel A) x z ↔ eqRel A x z := by
  intro x z
  constructor
  · intro ⟨y, e1, e2⟩
    exact e1.trans e2
  · intro e
    exact ⟨x, rfl, e⟩

theorem eq_rev : ∀ x y : A, rrev (eqRel A) x y ↔ eqRel A x y := by
  intro x y
  constructor
  · intro e
    exact e.symm
  · intro e
    exact e.symm

theorem rjoin_eq (R : Rel A B) : ∀ x z, rjoin R (eqRel B) x z ↔ R x z := by
  intro x z
  constructor
  · intro ⟨y, r, e⟩
    exact e ▸ r
  · intro r
    exact ⟨z, r, rfl⟩

theorem eq_rjoin (R : Rel A B) : ∀ x z, rjoin (eqRel A) R x z ↔ R x z := by
  intro x z
  constructor
  · intro ⟨y, e, r⟩
    exact e ▸ r
  · intro r
    exact ⟨x, rfl, r⟩

theorem rrev_rjoin (R : Rel A B) (S : Rel B C) :
    ∀ z x, rrev (rjoin R S) z x ↔ rjoin (rrev S) (rrev R) z x := by
  intro z x
  constructor
  · intro ⟨y, r, s⟩
    exact ⟨y, s, r⟩
  · intro ⟨y, s, r⟩
    exact ⟨y, r, s⟩

/-! ## TR7: one half holds for every relation -/

theorem half_always (R : Rel A B) : sub R (rjoin (rjoin R (rrev R)) R) :=
  fun x y h => ⟨x, ⟨y, h, h⟩, h⟩

/-- the other half: this is what "having the triangle" asks -/
abbrev Tri (R : Rel A B) : Prop := sub (rjoin (rjoin R (rrev R)) R) R

/-! ## TR8-TR13: maps; unit, counit, the triangle for maps -/

abbrev graphOf (f : A → B) : Rel A B := fun x y => f x = y

/-- TR9: the unit -/
theorem unit_map (f : A → B) : sub (eqRel A) (rjoin (graphOf f) (rrev (graphOf f))) := by
  intro x x' e
  cases e
  exact ⟨f x, rfl, rfl⟩

/-- TR10: the counit -/
theorem counit_map (f : A → B) : sub (rjoin (rrev (graphOf f)) (graphOf f)) (eqRel B) := by
  intro y y' h
  match h with
  | ⟨_, p, q⟩ => exact p.symm.trans q

/-- TR11: the triangle for maps, as the half that is asked -/
theorem tri_map (f : A → B) : Tri (graphOf f) := by
  intro x y h
  match h with
  | ⟨_, ⟨_, p, q⟩, r⟩ => exact p.trans (q.symm.trans r)

/-- TR11: with TR7, the equation -/
theorem tri_map_eq (f : A → B) :
    ∀ x y, rjoin (rjoin (graphOf f) (rrev (graphOf f))) (graphOf f) x y ↔ graphOf f x y := by
  intro x y
  constructor
  · exact tri_map f x y
  · exact half_always (graphOf f) x y

/-- TR13: reversed, it is again a triangle -/
theorem tri_rev (R : Rel A B) (h : Tri R) : Tri (rrev R) := by
  intro y x hh
  match hh with
  | ⟨y', ⟨x', r1, r2⟩, r3⟩ => exact h x y ⟨x', ⟨y', r3, r2⟩, r1⟩

/-! ## TR14-TR17: kernel and image are fixed by join and by reverse -/

abbrev ker (R : Rel A B) : Rel A A := rjoin R (rrev R)
abbrev im (R : Rel A B) : Rel B B := rjoin (rrev R) R

/-- TR15: symmetric -/
theorem ker_symm (R : Rel A B) (x y : A) : ker R x y → ker R y x := by
  intro ⟨b, r1, r2⟩
  exact ⟨b, r2, r1⟩

/-- TR14: transitive, from the triangle -/
theorem ker_trans (R : Rel A B) (h : Tri R) (x y z : A) :
    ker R x y → ker R y z → ker R x z := by
  intro ⟨b, r1, r2⟩ ⟨c, r3, r4⟩
  exact ⟨c, h x c ⟨y, ⟨b, r1, r2⟩, r3⟩, r4⟩

theorem im_symm (R : Rel A B) (y y' : B) : im R y y' → im R y' y := by
  intro ⟨a, r1, r2⟩
  exact ⟨a, r2, r1⟩

theorem im_trans (R : Rel A B) (h : Tri R) (y y' y'' : B) :
    im R y y' → im R y' y'' → im R y y'' := by
  intro ⟨a, r1, r2⟩ ⟨c, r3, r4⟩
  exact ⟨a, r1, h a y'' ⟨c, ⟨y', r2, r3⟩, r4⟩⟩

/-- TR22: for a map, the kernel is reflexive (the unit) -/
theorem ker_refl_map (f : A → B) (x : A) : ker (graphOf f) x x := ⟨f x, rfl, rfl⟩
/-- TR16: for a map, the kernel is "f x = f x'" -/
theorem ker_map (f : A → B) (x x' : A) : ker (graphOf f) x x' ↔ f x = f x' := by
  constructor
  · intro ⟨_, p, q⟩
    exact p.trans q.symm
  · intro e
    exact ⟨f x, rfl, e.symm⟩
/-- TR21: for a map, the image is a part: inside equality (the counit) -/
theorem im_part_map (f : A → B) : sub (im (graphOf f)) (eqRel B) := counit_map f

/-! ## TR18, TR19: a relation with the triangle is an isomorphism kernel → image -/

/-- things the same in the kernel go to the same things -/
theorem ker_respects (R : Rel A B) (h : Tri R) (x x' : A) (y : B) :
    ker R x x' → R x y → R x' y := by
  intro ⟨b, r1, r2⟩ r
  exact h x' y ⟨x, ⟨b, r2, r1⟩, r⟩

/-- and only those -/
theorem ker_exact (R : Rel A B) (x x' : A) (y : B) : R x y → R x' y → ker R x x' :=
  fun r r' => ⟨y, r, r'⟩

/-- the values are the same in the image -/
theorem im_exact (R : Rel A B) (x : A) (y y' : B) : R x y → R x y' → im R y y' :=
  fun r r' => ⟨x, r, r'⟩

/-- and whatever is the same in the image is also a value -/
theorem im_respects (R : Rel A B) (h : Tri R) (x : A) (y y' : B) :
    im R y y' → R x y → R x y' := by
  intro ⟨a, r1, r2⟩ r
  exact h x y' ⟨a, ⟨y, r, r1⟩, r2⟩

/-! ## TR20-TR23: membership, parts, meet -/

/-- a part, as a relation inside equality -/
abbrev partRel (S : A → Prop) : Rel A A := fun x y => x = y ∧ S x

/-- membership is the diagonal of the part -/
theorem mem_diag (S : A → Prop) (x : A) : partRel S x x ↔ S x :=
  ⟨fun h => h.2, fun h => ⟨rfl, h⟩⟩

theorem part_le_eq (S : A → Prop) : sub (partRel S) (eqRel A) := fun _ _ h => h.1

/-- TR23: the join of two parts is their meet -/
theorem part_join (S T : A → Prop) :
    ∀ x y, rjoin (partRel S) (partRel T) x y ↔ partRel (fun x => S x ∧ T x) x y := by
  intro x y
  constructor
  · intro ⟨m, ⟨e1, s⟩, ⟨e2, t⟩⟩
    cases e1
    cases e2
    exact ⟨rfl, s, t⟩
  · intro ⟨e, s, t⟩
    cases e
    exact ⟨x, ⟨rfl, s⟩, ⟨rfl, t⟩⟩

end Relations

/-! ## TR24: inclusion and exclusion, counted -/

def cnt (n : Nat) (p : Nat → Bool) : Nat := ((List.range n).filter p).length

def m2 (x : Nat) : Bool := x % 2 == 0
def m3 (x : Nat) : Bool := x % 3 == 0
def m5 (x : Nat) : Bool := x % 5 == 0

theorem incl_excl_two :
    cnt 30 (fun x => m2 x || m3 x) + cnt 30 (fun x => m2 x && m3 x)
      = cnt 30 m2 + cnt 30 m3 := rfl

theorem incl_excl_three :
    cnt 30 (fun x => m2 x || m3 x || m5 x) + cnt 30 (fun x => m2 x && m3 x)
        + cnt 30 (fun x => m2 x && m5 x) + cnt 30 (fun x => m3 x && m5 x)
      = cnt 30 m2 + cnt 30 m3 + cnt 30 m5 + cnt 30 (fun x => m2 x && m3 x && m5 x) :=
  rfl

/-- the exclusion of a join of exclusions -/
theorem union_is_excl :
    (List.range 30).all (fun x => (m2 x || m3 x) == !((!m2 x) && (!m3 x))) = true :=
  rfl

/-! ## TR25-TR32: groups, on the six isomorphisms of three things -/

abbrev Perm := List Nat

def e : Perm := [0, 1, 2]
def s01 : Perm := [1, 0, 2]
def s12 : Perm := [0, 2, 1]
def s02 : Perm := [2, 1, 0]
def c1 : Perm := [1, 2, 0]
def c2 : Perm := [2, 0, 1]

def perms : List Perm := [e, s01, s12, s02, c1, c2]

/-- `List.getD` of core depends on propext, so the lookup is written out here -/
def getD' : List Nat → Nat → Nat → Nat
  | [], _, d => d
  | a :: _, 0, _ => a
  | _ :: l, n + 1, d => getD' l n d

def act (p : Perm) (i : Nat) : Nat := getD' p i 0
/-- join: first q, then p -/
def mul (p q : Perm) : Perm := [0, 1, 2].map (fun i => act p (act q i))
def inv (p : Perm) : Perm := (perms.find? (fun q => mul p q == e)).getD e

def setMul (X Y : List Perm) : List Perm :=
  perms.filter (fun z => X.any fun x => Y.any fun y => mul x y == z)
def setInv (X : List Perm) : List Perm := perms.filter (fun z => X.contains (inv z))
def sameSet (X Y : List Perm) : Bool := perms.all (fun z => X.contains z == Y.contains z)

/-- TR25: the laws of a group -/
theorem grp_id : perms.all (fun g => mul e g == g && mul g e == g) = true := rfl
theorem grp_inv : perms.all (fun g => mul g (inv g) == e && mul (inv g) g == e) = true :=
  rfl
theorem grp_assoc :
    perms.all (fun g => perms.all fun h => perms.all fun k =>
      mul (mul g h) k == mul g (mul h k)) = true := rfl
/-- TR4 for a group: g ; g° ; g = g -/
theorem grp_triangle : perms.all (fun g => mul (mul g (inv g)) g == g) = true :=
  rfl
/-- the only idempotent is the identity -/
theorem grp_idem : perms.all (fun g => (mul g g != g) || (g == e)) = true := rfl

/-- two exchanges that share a place do not commute -/
theorem not_abelian : mul s01 s12 ≠ mul s12 s01 := fun h =>
  have h' : (1 : Nat) = 2 := congrArg (fun l : List Nat => getD' l 0 0) h
  Nat.noConfusion (Nat.succ.inj h')
theorem braid : mul s01 (mul s12 s01) = mul s12 (mul s01 s12) := rfl

/-- TR28: a subgroup is fixed by reverse and by join -/
def H : List Perm := [e, s01]
def A3 : List Perm := [e, c1, c2]

theorem H_rev : sameSet (setInv H) H = true := rfl
theorem H_join : sameSet (setMul H H) H = true := rfl
theorem A3_rev : sameSet (setInv A3) A3 = true := rfl
theorem A3_join : sameSet (setMul A3 A3) A3 = true := rfl

/-- TR27: every class has the triangle, and X° ; X is the subgroup -/
theorem class_triangle :
    perms.all (fun g =>
      sameSet (setMul (setMul (setMul [g] H) (setInv (setMul [g] H))) (setMul [g] H))
        (setMul [g] H)) = true := rfl
theorem class_gives_subgroup :
    perms.all (fun g => sameSet (setMul (setInv (setMul [g] H)) (setMul [g] H)) H) = true :=
  rfl

/-- TR29: all classes have as many things as H, and 6 = 3 · 2 -/
theorem class_size : perms.all (fun g => (setMul [g] H).length == H.length) = true :=
  rfl
theorem lagrange : perms.length = 3 * H.length := rfl

/-- TR30: A3 is normal and its classes are a group; H is not normal -/
theorem A3_normal :
    perms.all (fun g => sameSet (setMul [g] A3) (setMul A3 [g])) = true := rfl
theorem H_not_normal :
    perms.all (fun g => sameSet (setMul [g] H) (setMul H [g])) = false := rfl
theorem quotient_join :
    perms.all (fun g => perms.all fun g' =>
      sameSet (setMul (setMul [g] A3) (setMul [g'] A3)) (setMul [mul g g'] A3)) = true :=
  rfl

/-- TR31: a map of groups to the two values; its kernel is A3 -/
def sgn (p : Perm) : Nat := if A3.contains p then 0 else 1

theorem sgn_keeps_join :
    perms.all (fun g => perms.all fun h => sgn (mul g h) == (sgn g + sgn h) % 2) = true :=
  rfl
theorem sgn_kernel : sameSet (perms.filter (fun g => sgn g == 0)) A3 = true :=
  rfl
/-- G / K has as many things as the image -/
theorem first_iso_count :
    perms.length = A3.length * ((List.range 2).filter (fun v => perms.any fun g => sgn g == v)).length :=
  rfl

/-- TR32: orbit and the part fixing a thing -/
def orbit0 : List Nat := (List.range 3).filter (fun j => perms.any fun g => act g 0 == j)
def fix0 : List Perm := perms.filter (fun g => act g 0 == 0)

theorem orbit_stabilizer : perms.length = orbit0.length * fix0.length := rfl
theorem fix0_is_subgroup :
    (sameSet (setInv fix0) fix0 && sameSet (setMul fix0 fix0) fix0) = true := rfl

end BranchN

#print axioms BranchN.tr3
#print axioms BranchN.tr3'
#print axioms BranchN.tr4
#print axioms BranchN.eq_join_eq
#print axioms BranchN.eq_rev
#print axioms BranchN.rjoin_eq
#print axioms BranchN.eq_rjoin
#print axioms BranchN.rrev_rjoin
#print axioms BranchN.half_always
#print axioms BranchN.unit_map
#print axioms BranchN.counit_map
#print axioms BranchN.tri_map
#print axioms BranchN.tri_map_eq
#print axioms BranchN.tri_rev
#print axioms BranchN.ker_symm
#print axioms BranchN.ker_trans
#print axioms BranchN.im_symm
#print axioms BranchN.im_trans
#print axioms BranchN.ker_refl_map
#print axioms BranchN.ker_map
#print axioms BranchN.im_part_map
#print axioms BranchN.ker_respects
#print axioms BranchN.ker_exact
#print axioms BranchN.im_exact
#print axioms BranchN.im_respects
#print axioms BranchN.mem_diag
#print axioms BranchN.part_le_eq
#print axioms BranchN.part_join
#print axioms BranchN.incl_excl_two
#print axioms BranchN.incl_excl_three
#print axioms BranchN.union_is_excl
#print axioms BranchN.grp_id
#print axioms BranchN.grp_inv
#print axioms BranchN.grp_assoc
#print axioms BranchN.grp_triangle
#print axioms BranchN.grp_idem
#print axioms BranchN.not_abelian
#print axioms BranchN.braid
#print axioms BranchN.H_rev
#print axioms BranchN.H_join
#print axioms BranchN.A3_rev
#print axioms BranchN.A3_join
#print axioms BranchN.class_triangle
#print axioms BranchN.class_gives_subgroup
#print axioms BranchN.class_size
#print axioms BranchN.lagrange
#print axioms BranchN.A3_normal
#print axioms BranchN.H_not_normal
#print axioms BranchN.quotient_join
#print axioms BranchN.sgn_keeps_join
#print axioms BranchN.sgn_kernel
#print axioms BranchN.first_iso_count
#print axioms BranchN.orbit_stabilizer
#print axioms BranchN.fix0_is_subgroup

#print axioms: 54 theorems, no axioms.

323.19 Branch O: number theory, analysis, linear algebra, Hilbert space, probability

(from C20, C22, C25, branches B, J, K, M, N.) Nothing new is introduced. These subjects are one construction, the join of relations (C25, TR6), with different values in place of the two values. After the table each subject is given by its few general theorems.

UN1. One construction: relations with values. In C25 a relation has values in \(2\), and \((R;S)(x,z)=\exists y\,(R(x,y)\wedge S(y,z))\). Let the values be numbers, with sum for “some” and product for “and”:

\[ (R;S)(x,z)=\sum _y R(x,y)\,S(y,z). \]
The laws of TR6 hold by C20: \(R;E=R\), \((R;S);T=R;(S;T)\), \((R;S)^{\circ }=S^{\circ };R^{\circ }\).

values

a relation is

its join is

subject

\(2\)

a relation

“some …and”

logic, sets (C25)

numbers

a table of counts

sum of products

counting, graphs (paths)

\(C_n\)

the same, up to \(n\)

the same in \(C_n\)

number theory

fractions in \([0,1]\), rows summing to \(1\)

a table of chances

chance of going through any \(y\)

probability

real or complex numbers

a matrix

matrix product

linear algebra, Hilbert space

In every row \(E\) is the identity, \(^{\circ }\) is the reverse (transpose; with the conjugate \(K\) of branch B for complex values), and a symmetric idempotent (TR17) is a part with its equality.

Number theory

UN2. Divisibility is a map of worlds. \(a\) divides \(b\) exactly when there is a map \(C_b\to C_a\) keeping one more (J4), that is, when \(b\) lies in \(a\mathbb Z=\{k\,a\}\), a part of \(\mathbb Z\) fixed by reverse and sum (TR28).

UN3. Every such part is \(d\,\mathbb Z\); the greatest common divisor. Let \(H\neq \{0\}\) be fixed by reverse and sum. It has a positive thing, hence a least one \(d\) (C28). For \(h\) in \(H\) write \(h=q\,d+r\) with \(0\le r<d\) (C30); then \(r=h-q\,d\in H\), so \(r=0\). Hence \(H=d\,\mathbb Z\). For \(H=a\mathbb Z+b\mathbb Z\):

\[ d=u\,a+v\,b,\qquad d\mid a,\quad d\mid b . \]

UN4. Primes. \(p\) is prime when \(C_p\) has no part fixed by reverse and sum except \(\{0\}\) and all. If \(p\mid a\,b\) and \(p\nmid a\), UN3 gives \(u\,a+v\,p=1\), so \(b=u\,a\,b+v\,p\,b\) and \(p\mid b\). In \(C_p\) this says every \(a\neq 0\) has an inverse for the product, \(u\).

UN5. \(C_{mn}\cong C_m\times C_n\) when \(m,n\) have no common divisor. \(x\mapsto (x\bmod m,\ x\bmod n)\) keeps sum and product (J4). If \(x\) is in its kernel, \(m\mid x\) and \(n\mid x\); with \(u\,m+v\,n=1\), \(x=x\,u\,m+x\,v\,n\) and both terms are multiples of \(m\,n\). So the kernel is \(\{0\}\), and by TR19 and counting the map is an isomorphism.

UN6. \(a^{p-1}=1\) in \(C_p\). The things of \(C_p\) other than \(0\) are a group of \(p-1\) things under the product (UN4). The powers of \(a\) are a subgroup with \(k\) things, \(a^{k}=1\), and \(k\) divides \(p-1\) by TR29.

Analysis and calculus

UN7. A real number is a number read by its approximations. A real number is a part of the fractions closed downwards: the fractions below it (C24). Two are the same when no fraction tells them apart, as in J5. A part of an infinite kind uses the step of C26.

UN8. Completeness is “some”. For a family of such parts with a common bound, their union (C22) is again closed downwards, and it is the least part containing them all: the least upper bound. No further property is assumed.

UN9. Limit and continuity are “all” and “some”. \(a_n\to a\) says: for every closeness, some \(N\), for all \(n\ge N\), \(a_n\) is that close to \(a\) (C22, three times). A map is continuous when preimages keep the open parts (GT6).

UN10. Derivative. \(f(x+\varepsilon )=f(x)+f'(x)\,\varepsilon \) with \(\varepsilon ^{2}=0\), and the sum, product and chain rules are TH9.

UN11. The sum of the differences is the difference of the ends. For any \(F\) on the numbers, by C28,

\[ \sum _{k<n}\bigl (F(k+1)-F(k)\bigr )=F(n)-F(0), \]
and for \(S(n)=\sum _{k<n}a_k\), \(S(n+1)-S(n)=a_n\) by C30. Summing undoes differencing and differencing undoes summing; it is the boundary of TH6 on a row of steps. Read with steps \(\varepsilon \) this is the fundamental theorem of calculus.

UN12. The exponential. The isomorphisms of \(n\) things number \(n!=n\cdot (n-1)!\), by TR32 (the orbit of one thing has \(n\) things, and those fixing it are the isomorphisms of the rest). Put \(\exp (x)=\sum _n x^{n}/n!\). By the coefficients of branch B,

\[ \exp (x+y)=\sum _n\sum _k\frac {x^{k}}{k!}\,\frac {y^{n-k}}{(n-k)!}=\exp (x)\,\exp (y), \]
and term by term \(\exp '=\exp \): it is the thing fixed by the derivative. With \(u^{2}=-1\) of GT10, \(\exp (u\,t)\) is a turn by \(t\).
Linear algebra

UN13. Vectors, linear maps, matrices. A vector is a thing of \(K^{n}\), a map \(n\to K\) (C18), added place by place. A map \(K^{n}\to K^{m}\) keeping sum and multiples is a relation between \(n\) and \(m\) with values in \(K\) (UN1): a matrix. One after another is the join, and the reverse is the transpose.

UN14. Kernel, image, and the count of dimensions. For a linear \(f:V\to W\), TR19 gives \(V/\ker f\cong \operatorname {im}f\), keeping sums. Counting dimensions (things in a basis) as in TR29,

\[ \dim V=\dim \ker f+\dim \operatorname {im}f . \]

UN15. Trace and determinant. The trace is the sum over the diagonal; for a symmetric idempotent it counts its things (TR24). The determinant uses the map of TR31 from isomorphisms of \(n\) things to the two signs:

\[ \det A=\sum _{\pi }\operatorname {sgn}(\pi )\prod _i A(i,\pi (i)),\qquad \det (A;B)=\det A\cdot \det B , \]
the second because \(\operatorname {sgn}\) keeps joins.
Hilbert space

UN16. The inner product and the adjoint. With the conjugate \(K\) of branch B put \(\langle x,y\rangle =\sum _i\overline {x_i}\,y_i\). Then \(\langle x,x\rangle =\sum _i\overline {x_i}x_i\ge 0\) is the kept quantity of TH8. For a matrix \(A\),

\[ \langle A\,x,\ y\rangle =\langle x,\ A^{\circ }y\rangle , \]
by exchanging the two sums: a matching of the kind of C10, with \(A^{\circ }\) the adjoint.

UN17. The four kinds of operator are the four of branch N.

operator equation in branch N
unitary \(U;U^{\circ }=E=U^{\circ };U\) isomorphism (TR25)
self-adjoint \(A^{\circ }=A\) fixed by reverse (TR15)
projection \(P^{\circ }=P\), \(P;P=P\) symmetric idempotent (TR17)
partial isometry \(x;x^{\circ };x=x\) the triangle (TR11)

For a partial isometry, \(x;x^{\circ }\) and \(x^{\circ };x\) are projections and \(x\) is unitary between their images (TR14–TR18).

UN18. Right angles, the nearest point, and the bound. If \(\langle x,y\rangle =0\) then \(\langle x+y,x+y\rangle =\langle x,x\rangle +\langle y,y\rangle \) (C20). For a projection \(P\),

\[ \langle P\,x,\ y-P\,y\rangle =\langle x,\ P\,y-P\,P\,y\rangle =0 , \]
so \(y=P\,y+(y-P\,y)\) at a right angle, and for any \(z\)
\[ \|y-P\,z\|^{2}=\|y-P\,y\|^{2}+\|P\,y-P\,z\|^{2}\ \ge \ \|y-P\,y\|^{2} : \]
\(P\,y\) is the nearest thing of the image. For real values and \(t=\langle x,y\rangle /\langle y,y\rangle \), \(0\le \langle x-t\,y,x-t\,y\rangle =\langle x,x\rangle -\langle x,y\rangle ^{2}/\langle y,y\rangle \), so
\[ \langle x,y\rangle ^{2}\le \langle x,x\rangle \,\langle y,y\rangle . \]
With infinitely many places the sums are limits (UN9) and the space is required to be complete (UN8).
Probability and statistics

UN19. Chance is counting. For a finite \(X\) and a part \(A\), \(P(A)=|A|/|X|\) (branch B). By TR24,

\[ P(A\cup B)=P(A)+P(B)-P(A\cap B),\qquad P(\text {not }A)=1-P(A). \]

UN20. Conditioning; the meet commutes. \(P(A\mid B)=|A\cap B|/|B|\): \(A\) as a part of the part \(B\). Since \(A\cap B=B\cap A\) (TR23),

\[ P(A\mid B)\,P(B)=P(A\cap B)=P(B\mid A)\,P(A). \]

UN21. Independence is a product. \(A\) and \(B\) are independent when \(P(A\cap B)=P(A)\,P(B)\). If \(X=X_1\times X_2\) and \(A\), \(B\) read different places, this is \(|A_1\times B_2|=|A_1|\,|B_2|\) (TH7). For \(n\) places with two values each, the chance of \(k\) ones is \(\binom nk/2^{n}\) (branch B).

UN22. Mean, variance, correlation are an inner product. For \(f,g:X\to \) numbers put \(\langle f,g\rangle =\frac 1{|X|}\sum _x f(x)\,g(x)\). Then the mean is \(\mathbb E f=\langle 1,f\rangle \), the variance is \(\|f-\mathbb E f\|^{2}\), the covariance is \(\langle f-\mathbb E f,\ g-\mathbb E g\rangle \), and by UN18 the correlation lies between \(-1\) and \(1\).

UN23. Conditional mean and least squares are a projection. Let \(e\) be a quotient of \(X\) (TR21) and let \(P f(x)\) be the mean of \(f\) over the class of \(x\). Then \(P;P=P\) (a mean of means over the same class) and \(P^{\circ }=P\). So \(P\) is a projection (UN17), and by UN18 \(P f\) is the nearest function that is constant on classes. The same holds for the projection onto any chosen functions: fitting by least squares.

UN24. Chains of chances. A table of chances with rows summing to \(1\) (UN1) takes a state to the chances of the next. Two steps are the join, and that the chance of going from \(x\) to \(z\) in \(m+n\) steps is the sum over the middle is the associativity of TR6.

The whole of this branch

subject is general theorems
number theory the worlds \(C_n\) and the parts of \(\mathbb Z\) fixed by reverse and sum UN3–UN6
analysis, calculus parts of the fractions; “all” and “some”; \(\varepsilon ^{2}=0\) UN8, UN11, UN12
linear algebra relations with number values UN14, UN15
Hilbert space the same with the conjugate; branch N for operators UN16–UN18
probability, statistics counting parts; the inner product of UN22 UN19–UN23

What is used beyond the chain: the real numbers, limits and infinitely many places are the step of C26 (Section 323.14). Everything stated for finite kinds is a computation.