755 The Hyperoperation Derived

Every operation of arithmetic, from the gates on two points to tetration and beyond, is computed here from \(a=a\). No recursion is assumed, and no operation is laid down: a number is what an arrow generates, and each level of the hierarchy counts in the system generated by the level below. Two steps are used and not computed:

With (A) alone everything is finite and everything is forced. (C26) only declares that the whole climb is there as one kind, \(\mathbb N\).

755.1 The floor: two places

The statement \(a=a\) has two places. Two acts on places generate all others:

\[ \Delta :\ \text {use one place twice},\qquad \pi :\ \text {leave one place unused}. \]
Reading the two places separately is the pair \((x,y)\); swapping them is \((\pi _2,\pi _1)\). A relation is a part of the pairs; an arrow is a relation with exactly one second place for each first place (Section 69.3).

The order is computed from one gate. Read \(P\le Q\) as \(P\wedge Q=P\), where \(\wedge \) is the gate of Section 755.5. Then

\begin{align} P\le P &&&\text {since }P\wedge P=P, \tag{26492}\\ P\le Q,\ Q\le R\ \Rightarrow \ P\le R &&&\text {since }P\wedge R=(P\wedge Q)\wedge R=P\wedge (Q\wedge R)=P\wedge Q=P, \tag{26493}\end{align}

and \(P\le Q\le P\) gives \(P=Q\) since \(\wedge \) commutes. The three laws are read off the table of \(\wedge \) on \(2\). The first of them, \(P\wedge P=P\), is \(A=A\) for parts.

755.2 Every adjoint is an adjoint of a diagonal

For a kind \(I\) let \(\Delta _I:2\to 2^{I}\) send a value to the constant family. Its two adjoints, with respect to the order above, are

\[ \exists _I\ \dashv \ \Delta _I\ \dashv \ \forall _I . \]

where left adjoint the diagonal right adjoint
statements, \(I=2\) OR \(\Delta :2\to 2\times 2\) AND
statements, any \(I\) \(\exists \) (some) \(\Delta _I:2\to 2^{I}\) \(\forall \) (all)
statements, places equality putting two places equal
kinds \(A+B\) \(\Delta \) \(A\times B\)

AND and OR. \(z\le x\wedge y\) exactly when \(z\le x\) and \(z\le y\); \(x\vee y\le z\) exactly when \(x\le z\) and \(y\le z\). Both are read off the tables.

All and some. The constant family \(1\) is a point of \(2^{I}\), hence a part, hence by (A) an arrow \(\forall _I:2^{I}\to 2\); and \(\exists _I=\neg \,\forall _I\,\neg \).

Implication. \(P\wedge (-)\) has the right adjoint \(P\Rightarrow (-)\): \(P\wedge U\le Q\) exactly when \(U\le P\Rightarrow Q\).

Equality. With a further place \(c\) carried along,

\[ P(x,c)\wedge x=y\ \le \ R(x,y,c)\qquad \text {exactly when}\qquad P(x,c)\ \le \ R(x,x,c). \]
This is (J) of Section 69.2: equality is the left adjoint of putting two places equal.

755.3 Composition and intersection are computed

\[ (R;S)(a,c)=\exists b\,\bigl (R(a,b)\wedge S(b,c)\bigr ): \]
the middle place is used twice (\(\Delta \)), then left out (\(\exists \)).

Some and and. If \(b\) does not occur in \(T\), then \(\exists b\,(T\wedge U)=T\wedge \exists b\,U\), by a chain of adjoints:

\[ \exists b\,(T\wedge U)\le Q\iff T\wedge U\le Q\iff U\le T\Rightarrow Q \iff \exists b\,U\le T\Rightarrow Q\iff T\wedge \exists b\,U\le Q . \]

Unit. \(R(a,c)\le \exists b\,(a=b\wedge R(b,c))\): put \(b:=a\) in the unit of \(\exists \) and use \(a=a\). Conversely, by the adjoint of \(\exists \) it is enough that \(a=b\wedge R(b,c)\le R(a,c)\), and by the adjoint of equality this is \(R(b,c)\le R(b,c)\).

Associativity. By the exchange above both \((R;S);T\) and \(R;(S;T)\) are \(\exists b\,\exists c\,(R\wedge S\wedge T)\).

Substitution in general is then computed as well: \(Q(f(x))=\exists y\,(f(x)=y\wedge Q(y))\). So the floor needs only \(\Delta \) and \(\pi \).

Intersection. For a family of parts \(R(i,x)\),

\[ \bigcap _iP_i=\{x:\forall i\,R(i,x)\}, \]
and the adjoint of \(\forall \) says: \(Q\subseteq \bigcap _iP_i\) exactly when \(Q\subseteq P_i\) for every \(i\).

755.4 What there is

kind how it is there
\(A\times B\) the two places of \(a=a\)
\(1\) \(\Sigma _x(a=x)\) (Section 69.2)
\(0\) the part \(\neg (a=a)\) of \(1\)
\(2\) the parts of \(1\): \(0\) and \(1\)
\(A+B\) the pairs in \(2^{A}\times 2^{B}\) with one side a single point and the other empty
\(B^{A}\) the relations in \(2^{A\times B}\) that are arrows

Each is given by an adjoint, so it is the only one up to exactly one isomorphism (Section 69.5), and every construction of the table carries isomorphisms to isomorphisms. Inside the chain two kinds are the same when they are isomorphic; nothing more is needed for sameness of kinds.

Two laws from one sum. For \(2=1+1\):

\begin{align} &\text {the two parts do not meet:} &&(\mathrm {inl}=\mathrm {inr})\simeq 0 &&\text {(no contradiction)}, \tag{26494}\\ &\text {the two parts cover:} &&\text {every point is }\mathrm {inl}\text { or }\mathrm {inr} &&\text {(excluded middle)}. \tag{26495}\end{align}

755.5 Gates: the parts of the diagonal

In \(2\times 2\) the diagonal has the two points \(\Delta (0)=(0,0)\) and \(\Delta (1)=(1,1)\). Every gate is the arrow of a part made from them.

gate the part values at \(00,01,10,11\)
XNOR the diagonal (\(x=y\)) \(1,0,0,1\)
XOR the rest of the diagonal (\(x\neq y\)) \(0,1,1,0\)
AND the point \(\Delta (1)\) \(0,0,0,1\)
NAND the rest of \(\Delta (1)\) \(1,1,1,0\)
OR the rest of \(\Delta (0)\) \(0,1,1,1\)
NOT NAND on the diagonal, \(N(x,x)\) \(1,0\)

One gate gives all. With \(N\) for NAND:

\begin{align} \neg x&=N(x,x), & x\wedge y&=N\bigl (N(x,y),N(x,y)\bigr ), \tag{26496}\\ x\vee y&=N\bigl (N(x,x),N(y,y)\bigr ), & x\Rightarrow y&=N\bigl (x,N(y,y)\bigr ), \tag{26497}\\ x\oplus y&=N\bigl (N(x,N(x,y)),\,N(y,N(x,y))\bigr ). \tag{26498}\end{align}

For example at \((1,0)\): \(N(1,0)=1\), \(N(1,1)=0\), \(N(0,1)=1\), and \(N(0,1)=1\); at \((1,1)\): \(N(1,1)=0\), \(N(1,0)=1\) twice, and \(N(1,1)=0\).

755.6 \(\mathbb {F}_2\) and \(B\)

By the sum, the arrows \(2\to 2\) are pairs of points of \(2\): four of them, \(\mathrm {id}\), the swap \(\sigma \), and the constants \(c_0,c_1\). The kernel of \(\sigma \) is \(\Delta \) and that of \(c_0\) is everything; so the isomorphisms \(2\cong 2\) are \(\mathrm {id}\) and \(\sigma \).

\(\mathbb F_2\): the ways in which \(2\) is \(2\). Composing them, \(\sigma \circ \sigma =\mathrm {id}\): this is \(1+1=0\), and as a table it is XOR.

\(B\): an arrow that forgets. \(c_0\circ c_0=c_0\): this is \(1+1=1\), and as a table it is OR.

\(B\): absorption is idempotent. The arrow \(c_0\) is idempotent,

\[ c_0\circ c_0=c_0 , \]
since after the first step nothing is left to forget. So the system it generates is \(\langle c_0\rangle =\{\mathrm {id},c_0\}\), with \(\mathrm {id}=0\) and \(c_0=1\), and composing in it is its sum:
\[ 0+0=0,\qquad 0+1=1+0=1,\qquad 1+1=c_0\circ c_0=c_0=1 . \]
This is the table of OR. The one new entry, \(1+1=1\), is the idempotence of \(c_0\), and conversely: a sum on two points with unit \(0\) is OR exactly when \(1+1=1\), and XOR exactly when \(1+1=0\). So \(B\) is the system of an idempotent arrow, as \(\mathbb F_2\) is the system of an invertible one.

Product. In both it is AND, and \(x(y+z)=xy+xz\): for \(x=0\) both sides are \(0\), for \(x=1\) both are \(y+z\).

Set-theoretic derivation: \(A\cap A^{c}=\varnothing \iff A=A\)

A part \(A\) is an arrow \(x:X\to \mathbb F_2\), and \(A^{c}\) is \(x+1\). Then

\[ A\cap A^{c}=\varnothing \iff x\cdot (x+1)=0\iff x\cdot x+x=0\iff x\cdot x=x , \]
so “no contradiction” is the same equation as “\(A\cdot A=A\)”, the reflexivity \(P\le P\) of Section 755.1. And
\[ x+(x+1)=1:\qquad A+\neg A=1 , \]
the excluded middle, read as a partition.

755.7 Numbers and systems

Let \(e:X\to X\) be any arrow. What \(e\) generates is

\[ \langle e\rangle =\bigl \{x:\ \forall P\,\bigl ((\mathrm {id}\in P\wedge e\circ P\subseteq P)\Rightarrow x\in P\bigr )\bigr \} =\{\mathrm {id},\,e,\,e\circ e,\dots \}, \]
a part of \(X^{X}\) computed by \(\forall \) and \(\Rightarrow \).

Closed, and induction. \(\mathrm {id}\) is in every such \(P\); if \(x\) is in every such \(P\), so is \(e\circ x\). Hence \(\langle e\rangle \) is closed. And every such \(P\) contains \(\langle e\rangle \): this is induction. Both are the two directions of the adjoint of \(\forall \).

A number is a point of some \(\langle e\rangle \). “\(e\) done \(b\) times” is the choice of the point \(b\); nothing is repeated and no recursion is used.

Systems. Take the first \(i\), and then the first \(p\ge 1\), with \(e^{i+p}=e^{i}\); from there on \(e\) returns with period \(p\). Write \(C_{i,p}\) for this system. If \(e\) never returns, the kernel of \(b\mapsto e^{b}\) is \(\Delta \).

the arrow its system what it says
\(\mathrm {id}\) \(C_{0,1}=1\) \(S=\mathrm {id}\): \(a=a\)
\(\sigma \) on \(2\) \(C_{0,2}=\mathbb F_2\) \(1+1=0\)
\(c_0\) on \(2\) \(C_{1,1}=B\) \(1+1=1\)
a turn of \(m\) points \(C_{0,m}=\mathbb Z/m\) \(m=0\)
a loop that never returns \(\mathbb N\) kernel \(\Delta \)

The systems are ordered by their kernels, from \(\Delta \) (\(\mathbb N\)) to everything (\(1\)). The kinds made from \(1\) by pairs, sums and \(2^{X}\) are finite, so with (A) alone every system is some \(C_{i,p}\). The last line of the table is (C26): the declaration that there is a kind with a loop \(s\) that never returns. Then \(\mathbb N=\langle s\rangle \), with \(0=\mathrm {id}\) and \(S=s\circ (-)\).

Why no recursion is needed. Since the kernel of \(\mathbb N\) is \(\Delta \), it lies in every kernel; so \(s\mapsto e\) is an arrow \(\mathbb N\to \langle e\rangle \) for every \(e\). The map \(h(b)=e^{b}(z)\) is this arrow followed by “apply to \(z\)”. The equations \(h(0)=z\) and \(h(Sb)=e(h(b))\) hold; they are not what \(h\) is made from.

755.8 The operations: counting in the system below

One rule. With \(f=H_n(a,-)\),

\[ H_{n+1}(a,b)=f^{\,b}(z),\qquad b\in \langle f\rangle . \]
The count \(b\) lives in the system generated by the level below. It may be taken in any system above \(\langle f\rangle \), in particular in \(\mathbb N\).

The starting point is forced. One copy of \(a\) is \(a\): \(H_{n+1}(a,1)=a\), that is, \(H_n(a,z)=a\). So

\[ a+0=a\ \Rightarrow \ a\cdot 0=0,\qquad a\cdot 1=a\ \Rightarrow \ a^{0}=1,\qquad a^{1}=a\ \Rightarrow \ H_4(a,0)=1, \]
and \(1\) from then on, since \(H_{n+1}(a,1)=H_n(a,H_{n+1}(a,0))=H_n(a,1)=a\). For the sum, no step of \(S\) applied to \(a\) is \(a\).

In \(\mathbb N\). Let \(\mu _a:\mathbb N\to \langle s^{a}\rangle \) be the arrow \(s\mapsto s^{a}\) above.

level what it is computed
\(a+b\) composing in \(\langle s\rangle \) \(s^{2}\circ s^{3}=s^{5}\)
\(a\cdot b\) \(\mu _a\) at \(s^{b}\) \(\mu _2(s^{3})=s^{2}s^{2}s^{2}=s^{6}\)
\(a^{b}\) the point \(b\) of \(\langle \mu _a\rangle \), at \(s\) \(\mu _2\mu _2\mu _2(s)=s^{8}\)
above the same rule \({}^{3}2=16\),  \({}^{4}2=65536\)

Distributivity is that \(\mu _a\) keeps composition, \(\mu _a(s^{b}\circ s^{c})=\mu _a(s^{b})\circ \mu _a(s^{c})\); associativity of the product is \(\mu _a\circ \mu _b=\mu _{ab}\); the commutative laws are uniqueness (Sections 69.7–69.8). And \(2\) is fixed on every level:

\[ H_{n+1}(2,2)=H_n\bigl (2,H_{n+1}(2,1)\bigr )=H_n(2,2) \qquad \Rightarrow \qquad 2+2=2\cdot 2=2^{2}={}^{2}2=\cdots =4 . \]

755.9 Climbing inside each system

The hierarchy does not belong to \(\mathbb N\). Each system has its own, by the same rule.

In \(\mathbb F_2\).

level the arrow that is counted its system result
\(0\) \(\sigma \)
\(1\) \(\sigma \) \(\mathbb F_2\) \(x+b=\sigma ^{b}(x)\): XOR
\(2\) \(x+(-)\): \(\mathrm {id}\) or \(\sigma \) \(\mathbb F_2\) \(x\cdot b\): AND
\(3\) \(x\cdot (-)\): \(\mathrm {id}\) or \(c_0\) \(B\) \(x^{0}=1\), \(x^{1}=x\), \(x^{1+1}=x\)
\(4\) \(y\mapsto x^{y}\): \(\sigma \) for \(x=0\), \(c_1\) for \(x=1\) \(\mathbb F_2\), \(B\) \(0\): \(1,0,1,0,\dots \);  \(1\): \(1,1,1,\dots \)
\(\ge 5\) the same two arrows the same as on level \(4\)

On level \(3\) the exponent lives in \(B\), not in \(\mathbb F_2\): the system changes as one climbs. On level \(4\) the arrow \(y\mapsto 0^{y}\) sends \(0\mapsto 1\) and \(1\mapsto 0\): it is \(\sigma \) again, the bottom of the table. The climb in \(\mathbb F_2\) returns to its start and goes round.

In \(\mathbb Z/10\).

\[ 2^{b}:\ 1,2,4,8,6,2,\dots \ (C_{1,4}),\qquad {}^{b}2:\ 1,2,4,6,6,\dots \ (C_{3,1}),\qquad {}^{b}3:\ 1,3,7,7,\dots \]
since \(2^{16}=65536\), and above that the exponent is a multiple of \(4\) with \(2^{4}=16\); and \(3^{27}\) has \(27\equiv 3\) modulo \(4\) with \(3^{3}=27\). Going up, the period of the count goes down, \(10\to 4\to 2\to 1\), and the climb stops in the system \(1\), where \(S=\mathrm {id}\).

In \(\mathbb N\). For \(a\ge 2\) every arrow \(H_n(a,-)\) is injective and never returns, so every count is \(\mathbb N\) again and the climb has no end.

755.10 Lifting from \(\mathbb F_2\), and coming back

One digit. For digits \(x,y\) and a carry \(c\) in \(\mathbb F_2\),

\[ \text {digit}=x\oplus y\oplus c,\qquad \text {carry}=(x\wedge y)\vee \bigl (c\wedge (x\oplus y)\bigr ), \]
written with the one gate of Section 755.5.

\(\mathbb Z/4\). On pairs, \((x_0,x_1)+(y_0,y_1)=(x_0+y_0,\ x_1+y_1+x_0y_0)\):

\[ 1+1=(0,1)=2,\qquad 2+2=(0,0)=0,\qquad 3+1=(0,\,1+0+1)=0 . \]
In the same way \(\mathbb Z/2^{n}\) is the words of \(n\) digits, and \(+1\) on it has the system \(C_{0,2^{n}}\). All of this needs only (A).

\(\mathbb Z_2\). With (C26) the words without end are the greatest kind for \(2\times X\) (Section 69.19), and the sum is the one arrow into it from “give a digit, pass the carry on”. Then

\[ -1=111\cdots ,\qquad \langle {+1}\rangle =\mathbb N\ \text { with kernel }\Delta , \]
so \(1+1+\cdots +1\) is never \(0\): the lift has characteristic \(0\), and \(\mathbb N\) is found inside it as the words with finitely many \(1\)’s. The product is AND and one shift, \(x\cdot y=x_0\cdot y+\mathrm {shift}(x'\cdot y)\).

Down. The quotients of \(\mathbb N\) are the systems of Section 755.7:

\[ \mathbb N\to \mathbb Z/m,\qquad \mathbb N\to \mathbb F_2\ (\text {odd or even}),\qquad \mathbb N\to B\ (\text {zero or not}),\qquad \mathbb N\to 1 . \]

Round. \(\mathbb F_2\to \mathbb Z_2\supset \mathbb N\to \mathbb F_2\) is “read the first digit”, the identity of \(\mathbb F_2\). One may start at \(\mathbb F_2\) and lift, or start at \(\mathbb N\) and take quotients; it is one circle.

755.11 Backward readings

With \(a\) fixed, \(H_n(a,-)\) is a relation (Section 755.1); the backward operation is the same pairs read from the other place. Where the kernel is \(\Delta \), the backward reading is an arrow on the image and the only one; no choice is made (Section 71.12). To have it everywhere is to invert:

forward backward what is inverted
\(a+b\) \(c-a\) \(s\), giving \(\mathbb Z\) (Section 69.9)
\(a\cdot b\) \(c/a\) \(\mu _a\), giving \(D\) and \(\mathbb Q\) (Section 69.10)
\(a^{b}\) \(\log _ac\) and \(\sqrt [b]{c}\) read in \(\mathbb R\) (Section 69.23)
\({}^{b}a\) super-logarithm, super-root the image, read by comparison

From the third level on the operation does not commute, \(2^{3}=8\) and \(3^{2}=9\), so there is one backward reading for each place. In \(\mathbb R\) the first two levels are one operation read through \(\exp \): \(a\cdot b=\exp (\log a+\log b)\).

755.12 What is used

\[ \begin {aligned} a=a\ &\to \ \Delta ,\ \pi \ \xrightarrow {\ (\mathrm A)\ }\ \text {adjoints of diagonals}\\ &\to \ \circ ,\ \cap \ \to \ \text {gates}\ \to \ \mathbb F_2,\ B\ \to \ C_{i,p}\\ &\to \ \mathbb Z/2^{n}\ \xrightarrow {\ (\mathrm {C26})\ }\ \mathbb Z_2\supset \mathbb N\\ &\to \ \mathbb F_2\ \to \ 1 \end {aligned} \]

what is used what comes out
\(a=a\) and (A) order, \(\exists \), \(\forall \), equality, composition, intersection;
gates, \(\mathbb F_2\), \(B\), every \(C_{i,p}\), \(\mathbb Z/2^{n}\), and the hierarchy inside each
(C26), once \(\mathbb N\), \(\mathbb Z_2\), the hierarchy without end

(A) contains the excluded middle for parts: its case \(X=1\) is “every part of \(1\) is \(0\) or \(1\)”. (C26) is not a construction; it says that the finite systems, each of which is computed, are together one kind. Choice is not used anywhere in this section.

755.13 Lean 4 — Verified

PIC

Figure 938: The file Hyper.lean in the Lean 4 web editor (Lean v4.36.0): no errors and no warnings. All thirty-three #print axioms lines report that no axioms are used.

The file checks, without (A): composition and intersection computed from \(\exists \), \(\wedge \) and equality; every gate from the one gate; the order and the adjoints of AND, OR and implication; \(\mathbb F_2\) and \(B\) from the four arrows \(2\to 2\); that what an arrow generates is closed, satisfies induction and consists of its powers; the one rule giving sum, product and power, the forced starting points, and \(H_{n+1}(2,2)=4\) on every level; the climb inside \(\mathbb F_2\) and inside \(\mathbb Z/10\); and the lift, with the full adder, \(\mathbb Z/4\) and \(111\cdots +1=0\). The adjoints over an arbitrary kind and the table of Section 755.4 rest on (A) and are not in the file.

Listing 394: Hyper.lean: the hyperoperation hierarchy from one rule, with no axioms.

Hyper.lean

/-
  Hyper.lean
  The hyperoperation section, checked in core Lean 4 (no Mathlib, no imports).
  No `axiom`, no `sorry`, no `propext`, no `Classical`, no `Quot.sound`,
  no `funext`.  The `#print axioms` lines at the end report this.
  The assumption (A) of the text is not used: every part here is an explicit
  map into `Bool`.
-/

universe u

/-! ## 1. Composition and intersection are computed -/

section Statements
variable {α β γ δ : Type u}

/-- Some and and: if `b` does not occur in `T`. -/
theorem some_and (T : Prop) (U : β → Prop) :
    (∃ b, T ∧ U b) ↔ (T ∧ ∃ b, U b) :=
  ⟨fun ⟨b, t, u⟩ => ⟨t, b, u⟩, fun ⟨t, b, u⟩ => ⟨b, t, u⟩⟩

/-- Unit law of composition: equality in the middle changes nothing. -/
theorem comp_unit (R : α → β → Prop) (a : α) (c : β) :
    (∃ b, a = b ∧ R b c) ↔ R a c :=
  ⟨fun ⟨_, h, r⟩ => by cases h; exact r, fun r => ⟨a, rfl, r⟩⟩

theorem comp_assoc (R : α → β → Prop) (S : β → γ → Prop) (T : γ → δ → Prop)
    (a : α) (d : δ) :
    (∃ c, (∃ b, R a b ∧ S b c) ∧ T c d) ↔ (∃ b, R a b ∧ ∃ c, S b c ∧ T c d) :=
  ⟨fun ⟨c, ⟨b, r, s⟩, t⟩ => ⟨b, r, c, s, t⟩,
   fun ⟨b, r, c, s, t⟩ => ⟨c, ⟨b, r, s⟩, t⟩⟩

/-- Substitution along an arrow is computed from `∃`, `∧` and `=`. -/
theorem subst_computed (f : α → β) (Q : β → Prop) (x : α) :
    (∃ y, f x = y ∧ Q y) ↔ Q (f x) :=
  ⟨fun ⟨_, h, q⟩ => by cases h; exact q, fun q => ⟨f x, rfl, q⟩⟩

/-- Intersection is `∀`: the adjoint statement. -/
theorem inter_adjoint {ι : Type u} (R : ι → α → Prop) (Q : α → Prop) :
    (∀ x, Q x → ∀ i, R i x) ↔ (∀ i, ∀ x, Q x → R i x) :=
  ⟨fun h i x q => h x q i, fun h x q i => h i x q⟩

end Statements

/-! ## 2. Gates: one gate gives all -/

namespace Gate

def N (x y : Bool) : Bool := !(x && y)

theorem not_eq : ∀ x : Bool, (!x) = N x x := by decide
theorem and_eq : ∀ x y : Bool, (x && y) = N (N x y) (N x y) := by decide
theorem or_eq : ∀ x y : Bool, (x || y) = N (N x x) (N y y) := by decide
theorem xor_eq : ∀ x y : Bool,
    (x != y) = N (N x (N x y)) (N y (N x y)) := by decide
theorem imp_eq : ∀ x y : Bool, ((!x) || y) = N x (N y y) := by decide

/-- XNOR is the diagonal; XOR is the rest of it. -/
theorem xnor_diag : ∀ x y : Bool, (x == y) = true ↔ x = y := by decide
theorem xor_offdiag : ∀ x y : Bool, (x != y) = true ↔ x ≠ y := by decide
/-- AND is the point `Δ(1)`; OR is the rest of `Δ(0)`. -/
theorem and_point : ∀ x y : Bool, (x && y) = true ↔ (x = true ∧ y = true) := by
  decide
theorem or_point : ∀ x y : Bool, (x || y) = false ↔ (x = false ∧ y = false) := by
  decide

/-! ### The order is computed from AND -/

abbrev le (p q : Bool) : Prop := (p && q) = p

theorem le_refl : ∀ p : Bool, le p p := by decide
theorem le_trans : ∀ p q r : Bool, le p q → le q r → le p r := by decide
theorem le_antisymm : ∀ p q : Bool, le p q → le q p → p = q := by decide

/-- AND is the right adjoint of the diagonal, OR the left adjoint. -/
theorem and_adjoint : ∀ z x y : Bool, le z (x && y) ↔ (le z x ∧ le z y) := by
  decide
theorem or_adjoint : ∀ x y z : Bool, le (x || y) z ↔ (le x z ∧ le y z) := by
  decide
/-- Implication is the right adjoint of AND. -/
theorem imp_adjoint : ∀ p u q : Bool, le (p && u) q ↔ le u ((!p) || q) := by
  decide

end Gate

/-! ## 3. `F₂` and `B` -/

namespace F2

/-- The four arrows `2 → 2`. -/
def sigma (x : Bool) : Bool := !x
def c0 (_ : Bool) : Bool := false
def c1 (_ : Bool) : Bool := true

theorem four_arrows (f : Bool → Bool) :
    (∀ x, f x = x) ∨ (∀ x, f x = sigma x) ∨ (∀ x, f x = c0 x) ∨ (∀ x, f x = c1 x) := by
  cases h0 : f false <;> cases h1 : f true
  · exact Or.inr (Or.inr (Or.inl (fun x => by cases x <;> assumption)))
  · exact Or.inl (fun x => by cases x <;> assumption)
  · exact Or.inr (Or.inl (fun x => by cases x <;> assumption))
  · exact Or.inr (Or.inr (Or.inr (fun x => by cases x <;> assumption)))

/-- `σ ∘ σ = id`: this is `1 + 1 = 0`. -/
theorem sigma_sigma : ∀ x : Bool, sigma (sigma x) = x := by decide
/-- `c₀ ∘ c₀ = c₀`: this is `1 + 1 = 1`. -/
theorem c0_c0 : ∀ x : Bool, c0 (c0 x) = c0 x := by decide
/-- The kernel of `σ` is the diagonal; that of `c₀` is not. -/
theorem sigma_inj : ∀ x y : Bool, sigma x = sigma y → x = y := by decide
theorem c0_not_inj : c0 false = c0 true ∧ false ≠ true := by decide

def add (x y : Bool) : Bool := x != y
def mul (x y : Bool) : Bool := x && y

theorem one_add_one : add true true = false := rfl
theorem add_comm : ∀ x y : Bool, add x y = add y x := by decide
theorem add_assoc : ∀ x y z : Bool, add (add x y) z = add x (add y z) := by
  decide
theorem mul_assoc : ∀ x y z : Bool, mul (mul x y) z = mul x (mul y z) := by
  decide
theorem distrib : ∀ x y z : Bool, mul x (add y z) = add (mul x y) (mul x z) := by
  decide

/-- No contradiction is the same equation as `A · A = A`. -/
theorem no_contradiction_iff_idempotent : ∀ x : Bool,
    mul x (add x true) = false ↔ mul x x = x := by decide
theorem no_contradiction : ∀ x : Bool, mul x (add x true) = false := by decide
/-- Excluded middle as a partition: `A + ¬A = 1`. -/
theorem partition : ∀ x : Bool, add x (add x true) = true := by decide

end F2

/-! ## 4. Numbers: what an arrow generates -/

section Numbers
variable {α : Type u}

/-- `e` done `b` times. -/
def it (e : α → α) : Nat → α → α
  | 0, x => x
  | b + 1, x => e (it e b x)

/-- The part generated by `e`: the intersection of all closed parts. -/
def Gen (e : α → α) (x : α → α) : Prop :=
  ∀ P : (α → α) → Prop, P id → (∀ y, P y → P (fun a => e (y a))) → P x

theorem gen_id (e : α → α) : Gen e id := fun _ h0 _ => h0

theorem gen_step (e : α → α) (x : α → α) (h : Gen e x) :
    Gen e (fun a => e (x a)) :=
  fun P h0 hs => hs x (h P h0 hs)

/-- Induction: every closed part contains what is generated. -/
theorem gen_ind (e : α → α) (P : (α → α) → Prop) (h0 : P id)
    (hs : ∀ y, P y → P (fun a => e (y a))) : ∀ x, Gen e x → P x :=
  fun _ h => h P h0 hs

/-- Every power is generated ... -/
theorem gen_it (e : α → α) : ∀ b, Gen e (it e b) := by
  intro b
  induction b with
  | zero => exact gen_id e
  | succ b ih => exact gen_step e (it e b) ih

/-- ... and what is generated is a power. -/
theorem gen_is_power (e : α → α) (x : α → α) (h : Gen e x) :
    ∃ b, ∀ a, x a = it e b a :=
  h (fun y => ∃ b, ∀ a, y a = it e b a)
    ⟨0, fun _ => rfl⟩
    (fun _ ⟨b, hb⟩ => ⟨b + 1, fun a => congrArg e (hb a)⟩)

/-- No recursion is needed: `h b = e^b z` is the only map with the two
    equations. -/
theorem it_unique (e : α → α) (z : α) (h : Nat → α)
    (h0 : h 0 = z) (hs : ∀ b, h (b + 1) = e (h b)) : ∀ b, h b = it e b z := by
  intro b
  induction b with
  | zero => exact h0
  | succ b ih => exact (hs b).trans (congrArg e ih)

end Numbers

/-! ## 5. The operations: counting in the system below -/

namespace Hyper

/-- The forced starting point of level `n + 1`. -/
def start : Nat → Nat → Nat
  | 0, a => a
  | 1, _ => 0
  | _ + 2, _ => 1

/-- One rule for every level. -/
def H : Nat → Nat → Nat → Nat
  | 0, _, b => b + 1
  | n + 1, a, b => it (H n a) b (start n a)

theorem H_succ (n a b : Nat) : H (n + 1) a (b + 1) = H n a (H (n + 1) a b) := rfl

theorem H1 (a b : Nat) : H 1 a b = a + b := by
  induction b with
  | zero => rfl
  | succ b ih => exact congrArg Nat.succ ih

theorem H2 (a b : Nat) : H 2 a b = a * b := by
  induction b with
  | zero => rfl
  | succ b ih =>
    exact (H1 a (H 2 a b)).trans
      ((congrArg (fun t => a + t) ih).trans (Nat.add_comm a (a * b)))

theorem H3 (a b : Nat) : H 3 a b = a ^ b := by
  induction b with
  | zero => rfl
  | succ b ih =>
    exact (H2 a (H 3 a b)).trans
      ((congrArg (fun t => a * t) ih).trans (Nat.mul_comm a (a ^ b)))

/-- Tetration: one more level is one more exponent. -/
theorem H4_succ (a b : Nat) : H 4 a (b + 1) = a ^ H 4 a b := H3 a (H 4 a b)

/-- One copy of `a` is `a`: this forces the starting points. -/
theorem H_one (n a : Nat) : H (n + 2) a 1 = a := by
  induction n with
  | zero => rfl
  | succ n ih =>
    cases n with
    | zero => exact ih
    | succ n => exact ih

/-- `2` is fixed on every level: `2 + 2 = 2 · 2 = 2² = ²2 = ⋯ = 4`. -/
theorem two_two (n : Nat) : H (n + 1) 2 2 = 4 := by
  induction n with
  | zero => rfl
  | succ n ih =>
    exact (H_succ (n + 1) 2 1).trans
      ((congrArg (H (n + 1) 2) (H_one n 2)).trans ih)

theorem ex_add : H 1 2 3 = 5 := by decide
theorem ex_mul : H 2 2 3 = 6 := by decide
theorem ex_pow : H 3 2 3 = 8 := by decide

theorem tet_2_3 : H 4 2 3 = 16 :=
  (H4_succ 2 2).trans ((congrArg (fun t => 2 ^ t) (two_two 3)).trans (by decide))

theorem tet_2_4 : H 4 2 4 = 65536 :=
  (H4_succ 2 3).trans ((congrArg (fun t => 2 ^ t) tet_2_3).trans (by decide))

theorem tet_3_2 : H 4 3 2 = 27 :=
  (H4_succ 3 1).trans ((congrArg (fun t => 3 ^ t) (H_one 2 3)).trans (by decide))

/-! ### In `ℤ/10` the climb stops -/

theorem pow2_mod10 :
    (2 ^ 0 % 10, 2 ^ 1 % 10, 2 ^ 2 % 10, 2 ^ 3 % 10, 2 ^ 4 % 10, 2 ^ 5 % 10)
      = (1, 2, 4, 8, 6, 2) := by decide

theorem tet2_mod10 : H 4 2 3 % 10 = 6 ∧ H 4 2 4 % 10 = 6 :=
  ⟨(congrArg (fun t => t % 10) tet_2_3).trans (by decide),
   (congrArg (fun t => t % 10) tet_2_4).trans (by decide)⟩

theorem tet_3_3 : H 4 3 3 = 3 ^ 27 :=
  (H4_succ 3 2).trans (congrArg (fun t => 3 ^ t) tet_3_2)

theorem tet3_mod10 : H 4 3 2 % 10 = 7 ∧ H 4 3 3 % 10 = 7 :=
  have m2 : 27 % 10 = 7 := rfl
  have m3 : 3 ^ 27 % 10 = 7 := rfl
  ⟨(congrArg (fun t => t % 10) tet_3_2).trans m2,
   (congrArg (fun t => t % 10) tet_3_3).trans m3⟩

end Hyper

/-! ## 6. Climbing inside `F₂` -/

namespace Climb
open F2

/-- Level 1: counting `σ` has period 2, and gives XOR. -/
theorem level1_period (b : Nat) (x : Bool) : it sigma (b + 2) x = it sigma b x :=
  sigma_sigma (it sigma b x)

theorem level1_xor : ∀ x : Bool, it sigma 0 x = add x false ∧ it sigma 1 x = add x true := by
  decide

/-- Level 2: counting `x + -` from `0` has period 2, and gives AND. -/
theorem level2_period (x : Bool) (b : Nat) :
    it (add x) (b + 2) false = it (add x) b false := by
  cases x
  · exact (by decide : ∀ y : Bool, add false (add false y) = y) _
  · exact (by decide : ∀ y : Bool, add true (add true y) = y) _

theorem level2_and : ∀ x : Bool,
    it (add x) 0 false = mul x false ∧ it (add x) 1 false = mul x true := by decide

/-- Level 3: counting `x · -` from `1` has index 1 and period 1: the
    exponent lives in `B`, where `1 + 1 = 1`. -/
theorem level3_period (x : Bool) (b : Nat) :
    it (mul x) (b + 2) true = it (mul x) (b + 1) true := by
  cases x
  · rfl
  · rfl

theorem level3_values : ∀ x : Bool,
    it (mul x) 0 true = true ∧ it (mul x) 1 true = x := by decide

/-- The power with exponent in `B`. -/
def pw (x y : Bool) : Bool := if y then x else true

/-- Level 4: `y ↦ 0^y` is `σ` again, and `y ↦ 1^y` is constant. -/
theorem level4_zero : ∀ y : Bool, pw false y = sigma y := by decide
theorem level4_one : ∀ y : Bool, pw true y = c1 y := by decide

end Climb

/-! ## 7. Lifting from `F₂` -/

namespace Lift
open Gate

def digit (x y c : Bool) : Bool := (x != y) != c
def carry (x y c : Bool) : Bool := (x && y) || (c && (x != y))

/-- Digit and carry, written with the one gate. -/
theorem carry_nand : ∀ x y c : Bool,
    carry x y c = N (N x y) (N c (N (N x (N x y)) (N y (N x y)))) := by decide

/-- The full adder is correct. -/
theorem full_adder : ∀ x y c : Bool,
    (digit x y c).toNat + 2 * (carry x y c).toNat
      = x.toNat + y.toNat + c.toNat := by decide

/-- `ℤ/4` on pairs of digits, from the operations of `F₂` alone. -/
def add4 (p q : Bool × Bool) : Bool × Bool :=
  (p.1 != q.1, (p.2 != q.2) != (p.1 && q.1))

def val4 (p : Bool × Bool) : Nat := p.1.toNat + 2 * p.2.toNat

theorem add4_correct : ∀ a b c d : Bool,
    val4 (add4 (a, b) (c, d)) = (val4 (a, b) + val4 (c, d)) % 4 := by decide

theorem one_plus_one : add4 (true, false) (true, false) = (false, true) := rfl
theorem two_plus_two : add4 (false, true) (false, true) = (false, false) := rfl
theorem three_plus_one : add4 (true, true) (true, false) = (false, false) := rfl

/-! ### `ℤ₂`: words without end, with the carry passed on -/

abbrev W := Nat → Bool

def car (u v : W) : Nat → Bool
  | 0 => false
  | n + 1 => carry (u n) (v n) (car u v n)

def sum (u v : W) : W := fun n => digit (u n) (v n) (car u v n)

def ones : W := fun _ => true
def one : W := fun n => match n with | 0 => true | _ + 1 => false

theorem car_ones_one (n : Nat) : car ones one (n + 1) = true := by
  induction n with
  | zero => rfl
  | succ n ih =>
    exact (congrArg (fun c => carry true false c) ih).trans rfl

/-- `111⋯ + 1 = 0`: the word of all ones is `-1`. -/
theorem neg_one (n : Nat) : sum ones one n = false := by
  cases n with
  | zero => rfl
  | succ n => exact (congrArg (fun c => digit true false c) (car_ones_one n)).trans rfl

end Lift

/-! ## Axiom report -/

#print axioms some_and
#print axioms comp_unit
#print axioms comp_assoc
#print axioms subst_computed
#print axioms inter_adjoint
#print axioms Gate.xor_eq
#print axioms Gate.le_trans
#print axioms Gate.and_adjoint
#print axioms Gate.imp_adjoint
#print axioms F2.four_arrows
#print axioms F2.distrib
#print axioms F2.no_contradiction_iff_idempotent
#print axioms F2.partition
#print axioms gen_ind
#print axioms gen_it
#print axioms gen_is_power
#print axioms it_unique
#print axioms Hyper.H1
#print axioms Hyper.H2
#print axioms Hyper.H3
#print axioms Hyper.H_one
#print axioms Hyper.two_two
#print axioms Hyper.tet_2_4
#print axioms Hyper.tet2_mod10
#print axioms Hyper.tet3_mod10
#print axioms Climb.level1_period
#print axioms Climb.level2_period
#print axioms Climb.level3_period
#print axioms Climb.level4_zero
#print axioms Lift.full_adder
#print axioms Lift.carry_nand
#print axioms Lift.add4_correct
#print axioms Lift.neg_one