35 One reading: categories, paths, adjunctions, Heyting algebras

Everything in this section is obtained from one statement, R0. It is the source S of the chain read in its most general form: a family that can be read along maps is fixed by what it gives at the identity.

R0: the reading at the identity

Let \(O\) be a kind of objects and \(\mathrm {El}(X)\) the kind of points of \(X\in O\), so that a map \(X\to Y\) is a map \(\mathrm {El}(X)\to \mathrm {El}(Y)\).

Covariant. Let \(F\) be read along maps, \(F(h):F(Y)\to F(Y')\) for \(h:Y\to Y'\), and let \(\eta _Y:(X\to Y)\to F(Y)\) satisfy

\begin{equation} \eta _{Y'}(h\circ f)=F(h)\bigl (\eta _Y(f)\bigr ). \tag{676}\end{equation}
Then for every \(f:X\to Y\)
\begin{equation} \eta _Y(f)=F(f)\bigl (\eta _X(\mathrm {id}_X)\bigr ). \tag{677}\end{equation}
Proof. Put \(Y\mapsto X\), \(Y'\mapsto Y\), \(h\mapsto f\), \(f\mapsto \mathrm {id}_X\) in the condition; \(f\circ \mathrm {id}_X=f\).

Contravariant. Let \(G\) be read backwards along maps, \(G(g):G(A')\to G(A)\) for \(g:A'\to A\), and let \(\eta _A:(A\to Y)\to G(A)\to \mathrm {Res}\) satisfy

\begin{equation} \eta _{A'}(k\circ g)(x)=\eta _A(k)\bigl (G(g)\,x\bigr ). \tag{678}\end{equation}
Then
\begin{equation} \eta _A(k)(x)=\eta _Y(\mathrm {id}_Y)\bigl (G(k)\,x\bigr ). \tag{679}\end{equation}
Proof. The same substitution, with \(k\) in the place of \(g\) and \(\mathrm {id}_Y\) in the place of \(k\).

Neither proof uses anything but the condition and \(f\circ \mathrm {id}=f\). Every later unit, counit, pairing, evaluation, refl is this reading.

R1: Yoneda on kinds

Take \(O\) the kinds and \(\mathrm {El}(X)=X\). For \(F\) with \(F(\mathrm {id})=\mathrm {id}\) put

\begin{equation} \mathrm {read}(\eta )=\eta _A(\mathrm {id}_A),\qquad \mathrm {place}(x)_B(f)=F(f)(x). \tag{680}\end{equation}

triangle statement reason
(i) \(\mathrm {read}(\mathrm {place}(x))=x\) \(F(\mathrm {id})=\mathrm {id}\)
(ii) \(\mathrm {place}(\mathrm {read}(\eta ))=\eta \) for natural \(\eta \) R0, covariant

R2: kinds and maps form a category

With \(\mathrm {id}_A(a)=a\) and \((g\circ f)(a)=g(f(a))\),

\begin{equation} \mathrm {id}\circ f=f,\qquad f\circ \mathrm {id}=f,\qquad h\circ (g\circ f)=(h\circ g)\circ f, \tag{681}\end{equation}
each by reading both sides at a point. No law is added: the identity is what composition reads as nothing.

R3: paths

An equality \(a=b\) is a family over \(b\) whose witness at \(b=a\) is \(\mathrm {refl}_a\). Its reading at \(\mathrm {refl}\) is the rule

\begin{equation} J:\ Q(a,\mathrm {refl}_a)\ \longrightarrow \ Q(b,e)\qquad \text {for every } e:a=b,\qquad J(d)(\mathrm {refl}_a)=d . \tag{682}\end{equation}
Substitution is \(J\) for a family that ignores the path: \(\mathrm {transport}_P(e)=J_{(b,\_)\mapsto P(b)}\). Paths compose by \(J\), and they form a groupoid:

law proof
\(\mathrm {refl}\cdot p=p\),  \(p\cdot \mathrm {refl}=p\) \(J\) on \(p\)
\(p\cdot (q\cdot s)=(p\cdot q)\cdot s\) \(J\) on \(s\)
\(p\cdot p^{-1}=\mathrm {refl}\),  \(p^{-1}\cdot p=\mathrm {refl}\) \(J\) on \(p\)

This is the lowest level of the identity type of R0 read as a family over pairs.

R4: an adjunction, and its triangles as theorems

An adjunction \(L\dashv R\) is a bijection

\begin{equation} \varphi _{A,C}:\ (LA\to C)\ \rightleftarrows \ (A\to RC)\ :\psi _{A,C}, \tag{683}\end{equation}
natural on both sides:

condition statement
\(\varphi \) natural in \(C\) \(\varphi (h\circ f)=R(h)\circ \varphi (f)\)
\(\psi \) natural in \(A\) \(\psi (k\circ g)=\psi (k)\circ L(g)\)
inverse \(\varphi (\psi (k))=k\),  \(\psi (\varphi (f))=f\)

Unit and counit are readings at the identity.

\begin{equation} \eta _A=\varphi (\mathrm {id}_{LA}):A\to RLA,\qquad \varepsilon _C=\psi (\mathrm {id}_{RC}):LRC\to C . \tag{684}\end{equation}

Factorisation. R0 covariant, with \(X=LA\), \(F(C)=RC\), \(\eta _C(f)=\varphi (f)\), gives \(\varphi (f)=R(f)\circ \eta _A\). R0 contravariant, with \(Y=RC\), \(G(A)=LA\), \(\eta _A(k)=\psi (k)\), gives \(\psi (k)=\varepsilon _C\circ L(k)\).

Triangles.

\begin{align} \varepsilon _{LA}\circ L(\eta _A)&=\psi (\eta _A)=\psi (\varphi (\mathrm {id}_{LA}))=\mathrm {id}_{LA}, \tag{685}\\ R(\varepsilon _C)\circ \eta _{RC}&=\varphi (\varepsilon _C)=\varphi (\psi (\mathrm {id}_{RC}))=\mathrm {id}_{RC}. \tag{686}\end{align}

The first equality in each line is the factorisation, the last is the inverse condition. So the triangle identities of the source are not assumed: they follow from a natural bijection by R0.

R5: currying

On kinds, take \(L(X)=X\times B\) and \(R(Y)=B\to Y\), with

\begin{equation} \varphi (f)(a)(b)=f(a,b),\qquad \psi (k)(a,b)=k(a)(b). \tag{687}\end{equation}
The four conditions hold by reading; the second inverse condition uses that a pair is its two components (E1). R4 then computes:

reading at the identity value
unit \(\eta _A\) \(\varphi (\mathrm {id}_{A\times B})\) pairing, \(a\mapsto (b\mapsto (a,b))\)
counit \(\varepsilon _C\) \(\psi (\mathrm {id}_{B\to C})\) evaluation, \((k,b)\mapsto k(b)\)
first triangle \(\varepsilon \circ L\eta =\mathrm {id}\) \(\mathrm {ev}\bigl (b\mapsto (a,b),\,b\bigr )=(a,b)\)
second triangle \(R\varepsilon \circ \eta =\mathrm {id}\) \(\bigl (b\mapsto \mathrm {ev}(k,b)\bigr )=k\)

The kind of maps \(B\to Y\) (E3) is therefore the right adjoint of \(-\times B\), and evaluation and pairing are its counit and unit.

R6: implication

On propositions, take \(L(X)=X\wedge p\) and \(R(Y)=p\to Y\), with \(\varphi \), \(\psi \) as in R5 and \(\wedge \) in the place of \(\times \). R4 applies unchanged, and the bijection reads

\begin{equation} (r\wedge p\to q)\iff (r\to (p\to q)). \tag{688}\end{equation}
Implication is currying read on propositions.

R7: Heyting algebras

A Heyting algebra is an order \((H,\leq )\) with

element condition
\(\top \), \(\bot \) \(a\leq \top \),  \(\bot \leq a\)
\(a\wedge b\) \(c\leq a\wedge b\iff c\leq a\) and \(c\leq b\)
\(a\vee b\) \(a\vee b\leq c\iff a\leq c\) and \(b\leq c\)
\(p\Rightarrow q\) \(r\wedge p\leq q\iff r\leq (p\Rightarrow q)\)

Each row is an adjunction: \(\wedge \) is right adjoint to the diagonal, \(\vee \) left adjoint to it, \(\Rightarrow \) right adjoint to \(-\wedge p\). This is a definition of a structure, not an assumption.

Propositions. With \(\leq \) read as \(\to \): \(\wedge \) is the pair (E1), \(\vee \) the sum (E2), \(\Rightarrow \) the map kind with its adjunction from R6. So propositions form a Heyting algebra.

Worlds. Let \((P,\preceq )\) be an order. The parts \(U\subseteq P\) closed upward form a Heyting algebra with \(\cap \), \(\cup \), and

\begin{equation} x\in (U\Rightarrow V)\iff \text {for all }y\succeq x:\ y\in U\text { gives }y\in V . \tag{689}\end{equation}
The adjunction uses reflexivity of \(\preceq \) in one direction and transitivity (for upward closure) in the other. These are the Kripke worlds.

R8: intuitionistic laws

Put \(\neg p=(p\Rightarrow \bot )\). In every Heyting algebra:

law

statement

from

modus ponens

\(p\wedge (p\Rightarrow q)\leq q\)

counit of \(-\wedge p\dashv p\Rightarrow -\)

distributivity

\(p\wedge (q\vee r)\leq (p\wedge q)\vee (p\wedge r)\)

a left adjoint keeps joins

non-contradiction

\(p\wedge \neg p\leq \bot \)

modus ponens with \(q=\bot \)

double negation in

\(p\leq \neg \neg p\)

adjunction applied to non-contradiction

contraposition

\(p\leq q\Rightarrow \neg q\leq \neg p\)

adjunction

triple negation

\(\neg \neg \neg p\leq \neg p\)

contraposition of \(p\leq \neg \neg p\)

excluded middle is never refuted

\(\top \leq \neg \neg (p\vee \neg p)\)

below

For the last row, let \(J=p\vee \neg p\). Since \(p\leq J\), contraposition gives \(\neg J\leq \neg p\leq J\). So \(\top \wedge \neg J\leq J\wedge \neg J\leq \bot \), and the adjunction gives \(\top \leq \neg \neg J\). Read on propositions, these are the laws of intuitionistic logic, e.g. \(\neg \neg (p\vee \neg p)\).

R9: excluded middle is not a law

Take the order with two stages \(\ell \preceq r\) and nothing else (\(r\not \preceq \ell \)): the world of one arrow \(0\to 1\) (HA4). It satisfies every condition of R7. Let \(U=\{r\}\), closed upward. At the stage \(\ell \):

\begin{equation} \ell \in U\iff \ell =r,\qquad \ell \in \neg U\iff \text {no }y\succeq \ell \text { lies in }U . \tag{690}\end{equation}
The first fails since \(\ell \neq r\) (E2). The second fails since \(r\succeq \ell \) and \(r\in U\). Hence \(\ell \notin U\cup \neg U\), and \(U\vee \neg U\neq \top \). Excluded middle does not follow from the Heyting laws.

R10: the Boolean part

A statement that is a cut, \(t\in \mathbf 2\) (E2), is \(\ell \) or \(r\) and not both. For it

\begin{equation} \neg \neg (t=\ell )\ \Rightarrow \ t=\ell . \tag{691}\end{equation}
If \(t=r\), then \(t\neq \ell \), against \(\neg \neg (t=\ell )\). So double negation is removed exactly on the decided part, as in HA7.

35.1 Machine check in Rocq

R0–R10 were checked in Rocq 9.3.0, in a file Source.v of 532 lines that imports nothing. Each of its 26 commands Print Assumptions returns Closed under the global context: no axiom is used.

step

statement

Rocq name

R0

reading at the identity, both variances

read_cov, read_contra

R1

Yoneda: both triangles

read_place, place_read

R2

category of kinds

kcomp_assoc

R3

\(J\) at \(\mathrm {refl}\); groupoid

Jrule_refl, path_assoc, path_inv_r

R4

triangles from a natural bijection

triangle_L, triangle_R

R5

currying: evaluation and pairing

curryAdj, ev_after_pair, pair_then_ev

R6

implication

implAdj, impl_adj

R7

propositions, worlds

PropHA, UpHA

R8

intuitionistic laws

h_mp, h_distr, h_tneg, h_nn_em

R9

excluded middle fails

no_excluded_middle

R10

Boolean part

dne_cut

In the file, R4’s factorisations are proved by applying read_cov and read_contra, and R5, R6 are proved by applying R4 to the two instances. So the kernel checks that pairing, evaluation and implication are readings of R0.

The file Source.v.

Source.v

(* ======================================================================
   Source.v - one reading, and what it gives: Yoneda, the category of
   kinds, paths (J), adjunctions (unit and counit read at the identity),
   currying, implication, Heyting algebras, intuitionistic logic.
   Rocq, prelude only. No axioms: every Print Assumptions line at the end
   must print "Closed under the global context".
   ====================================================================== *)

(* ======================================================================
   R0  the source: a family read along maps is fixed by its reading at
       the identity. Every later "unit", "counit", "eval", "pairing",
       "refl", "Yoneda" below is this one reading.
   ====================================================================== *)

Section Root.
  Variable O : Type.
  Variable El : O -> Type.

  (* covariant: eta out of maps from X *)
  Section Cov.
    Variable F : O -> Type.
    Variable fmap : forall Y Y' : O, (El Y -> El Y') -> F Y -> F Y'.
    Variable X : O.
    Variable eta : forall Y : O, (El X -> El Y) -> F Y.
    Hypothesis eta_nat : forall (Y Y' : O) (h : El Y -> El Y') (f : El X -> El Y),
        eta Y' (fun x => h (f x)) = fmap Y Y' h (eta Y f).

    Theorem read_cov (Y : O) (f : El X -> El Y) : eta Y f = fmap X Y f (eta X (fun x => x)).
    Proof. exact (eta_nat X Y f (fun x => x)). Qed.
  End Cov.

  (* contravariant: eta out of maps into Y *)
  Section Contra.
    Variable G : O -> Type.
    Variable gmap : forall A A' : O, (El A' -> El A) -> G A' -> G A.
    Variable Res : Type.
    Variable Y : O.
    Variable eta : forall A : O, (El A -> El Y) -> G A -> Res.
    Hypothesis eta_nat : forall (A A' : O) (g : El A' -> El A) (k : El A -> El Y) (x : G A'),
        eta A' (fun y => k (g y)) x = eta A k (gmap A A' g x).

    Theorem read_contra (A : O) (k : El A -> El Y) (x : G A) :
      eta A k x = eta Y (fun y => y) (gmap Y A k x).
    Proof. exact (eta_nat Y A k (fun y => y) x). Qed.
  End Contra.
End Root.

(* ----------------------------------------------------------------------
   E1  Yoneda on kinds: reading and placing are inverse
   ---------------------------------------------------------------------- *)

Section Yoneda.
  Variable F : Type -> Type.
  Variable fmap : forall X Y : Type, (X -> Y) -> F X -> F Y.
  Hypothesis fmap_id : forall (X : Type) (x : F X), fmap X X (fun a => a) x = x.
  Variable A : Type.

  (* reading a natural family at the identity *)
  Definition readId (eta : forall B : Type, (A -> B) -> F B) : F A :=
    eta A (fun a => a).

  (* placing an element along every arrow *)
  Definition place (x : F A) : forall B : Type, (A -> B) -> F B :=
    fun B f => fmap A B f x.

  (* triangle (i) *)
  Lemma read_place (x : F A) : readId (place x) = x.
  Proof. unfold readId, place. apply fmap_id. Qed.

  (* triangle (ii), for natural families: the reading R0 *)
  Lemma place_read (eta : forall B : Type, (A -> B) -> F B)
    (nat : forall (B C : Type) (g : B -> C) (f : A -> B),
             eta C (fun a => g (f a)) = fmap B C g (eta B f))
    (B : Type) (f : A -> B) : place (readId eta) B f = eta B f.
  Proof.
    unfold place, readId. symmetry.
    exact (read_cov Type (fun X => X) F fmap A eta nat B f).
  Qed.
End Yoneda.

(* ----------------------------------------------------------------------
   9a  kinds and maps form a category
   ---------------------------------------------------------------------- *)

Definition kid (A : Type) : A -> A := fun a => a.
Definition kcomp {A B C : Type} (g : B -> C) (f : A -> B) : A -> C := fun a => g (f a).

Lemma kcomp_id_l {A B : Type} (f : A -> B) : kcomp (kid B) f = f.
Proof. reflexivity. Qed.

Lemma kcomp_id_r {A B : Type} (f : A -> B) : kcomp f (kid A) = f.
Proof. reflexivity. Qed.

Lemma kcomp_assoc {A B C E : Type} (f : A -> B) (g : B -> C) (h : C -> E) :
  kcomp h (kcomp g f) = kcomp (kcomp h g) f.
Proof. reflexivity. Qed.

(* ----------------------------------------------------------------------
   9b  paths: a statement about every path is read at refl (J);
       paths compose into a groupoid
   ---------------------------------------------------------------------- *)

Definition Jrule {A : Type} {a : A} (Q : forall b : A, a = b -> Type)
  (d : Q a eq_refl) {b : A} (e : a = b) : Q b e :=
  match e as e0 in _ = b0 return Q b0 e0 with eq_refl => d end.

Lemma Jrule_refl {A : Type} {a : A} (Q : forall b : A, a = b -> Type) (d : Q a eq_refl) :
  Jrule Q d eq_refl = d.
Proof. reflexivity. Qed.

Lemma path_refl_l {A : Type} {x y : A} (p : x = y) : eq_trans eq_refl p = p.
Proof. destruct p. reflexivity. Qed.

Lemma path_refl_r {A : Type} {x y : A} (p : x = y) : eq_trans p eq_refl = p.
Proof. reflexivity. Qed.

Lemma path_assoc {A : Type} {x y w v : A} (p : x = y) (q : y = w) (q2 : w = v) :
  eq_trans p (eq_trans q q2) = eq_trans (eq_trans p q) q2.
Proof. destruct q2. reflexivity. Qed.

Lemma path_inv_r {A : Type} {x y : A} (p : x = y) : eq_trans p (eq_sym p) = eq_refl.
Proof. destruct p. reflexivity. Qed.

Lemma path_inv_l {A : Type} {x y : A} (p : x = y) : eq_trans (eq_sym p) p = eq_refl.
Proof. destruct p. reflexivity. Qed.



(* ----------------------------------------------------------------------
   two marks, l <> r, and evaluation (from E2, E3 of Ent.v)
   ---------------------------------------------------------------------- *)

Inductive Two : Type := l | r.

Definition P (t : Two) : Type :=
  match t with l => unit | r => Empty_set end.

(* l <> r: otherwise the mark of 1 would be an element of 0 *)
Lemma l_ne_r : l <> r.
Proof.
  intro h.
  assert (e : P r) by (rewrite <- h; exact tt).
  simpl in e. destruct e.
Qed.

Definition ev {A B : Type} (p : (B -> A) * B) : A := (fst p) (snd p).

(* ----------------------------------------------------------------------
   9c  an adjunction is a natural bijection; unit and counit are its
       readings at the identity; the triangles are theorems
   ---------------------------------------------------------------------- *)

Record Adj : Type := {
  aO : Type;
  aEl : aO -> Type;
  aL : aO -> aO;
  aR : aO -> aO;
  almap : forall X Y : aO, (aEl X -> aEl Y) -> aEl (aL X) -> aEl (aL Y);
  armap : forall X Y : aO, (aEl X -> aEl Y) -> aEl (aR X) -> aEl (aR Y);
  aphi : forall A C : aO, (aEl (aL A) -> aEl C) -> aEl A -> aEl (aR C);
  apsi : forall A C : aO, (aEl A -> aEl (aR C)) -> aEl (aL A) -> aEl C;
  aphi_nat : forall (A C C' : aO) (h : aEl C -> aEl C') (f : aEl (aL A) -> aEl C) (a : aEl A),
      aphi A C' (fun x => h (f x)) a = armap C C' h (aphi A C f a);
  apsi_nat : forall (A A' C : aO) (g : aEl A' -> aEl A) (k : aEl A -> aEl (aR C))
                    (x : aEl (aL A')),
      apsi A' C (fun y => k (g y)) x = apsi A C k (almap A' A g x);
  aphi_psi : forall (A C : aO) (k : aEl A -> aEl (aR C)) (a : aEl A),
      aphi A C (apsi A C k) a = k a;
  apsi_phi : forall (A C : aO) (f : aEl (aL A) -> aEl C) (x : aEl (aL A)),
      apsi A C (aphi A C f) x = f x
}.

Section AdjLaws.
  Variable J : Adj.

  (* unit: read the bijection at the identity of L A *)
  Definition aunit (A : aO J) : aEl J A -> aEl J (aR J (aL J A)) :=
    aphi J A (aL J A) (fun x => x).

  (* counit: read the inverse at the identity of R C *)
  Definition acounit (C : aO J) : aEl J (aL J (aR J C)) -> aEl J C :=
    apsi J (aR J C) C (fun y => y).

  (* phi is fixed by its reading at the identity: covariant reading R0 *)
  Lemma phi_by_unit (A C : aO J) (f : aEl J (aL J A) -> aEl J C) (a : aEl J A) :
    aphi J A C f a = armap J (aL J A) C f (aunit A a).
  Proof.
    exact (read_cov (aO J) (aEl J) (fun C0 => aEl J (aR J C0)) (armap J) (aL J A)
             (fun C0 f0 => aphi J A C0 f0 a) (fun C0 C1 h f0 => aphi_nat J A C0 C1 h f0 a) C f).
  Qed.

  (* psi is fixed by its reading at the identity: contravariant reading R0 *)
  Lemma psi_by_counit (A C : aO J) (k : aEl J A -> aEl J (aR J C)) (x : aEl J (aL J A)) :
    apsi J A C k x = acounit C (almap J A (aR J C) k x).
  Proof.
    exact (read_contra (aO J) (aEl J) (fun A0 => aEl J (aL J A0))
             (fun A0 A1 => almap J A1 A0) (aEl J C) (aR J C)
             (fun A0 k0 x0 => apsi J A0 C k0 x0) (fun A0 A1 g k0 x0 => apsi_nat J A0 A1 C g k0 x0)
             A k x).
  Qed.

  (* first triangle:  counit . L unit = id *)
  Theorem triangle_L (A : aO J) (x : aEl J (aL J A)) :
    acounit (aL J A) (almap J A (aR J (aL J A)) (aunit A) x) = x.
  Proof.
    rewrite <- (psi_by_counit A (aL J A) (aunit A) x).
    exact (apsi_phi J A (aL J A) (fun y => y) x).
  Qed.

  (* second triangle:  R counit . unit = id *)
  Theorem triangle_R (C : aO J) (y : aEl J (aR J C)) :
    armap J (aL J (aR J C)) C (acounit C) (aunit (aR J C) y) = y.
  Proof.
    rewrite <- (phi_by_unit (aR J C) C (acounit C) y).
    exact (aphi_psi J (aR J C) C (fun z => z) y).
  Qed.
End AdjLaws.

(* ----------------------------------------------------------------------
   9d  currying is an adjunction:  (A x B -> C)  =~  (A -> (B -> C))
   ---------------------------------------------------------------------- *)

Definition curryAdj (B : Type) : Adj.
Proof.
  refine {| aO := Type; aEl := fun X => X;
            aL := fun X => (X * B)%type; aR := fun Y => B -> Y;
            almap := fun X Y g q => (g (fst q), snd q);
            armap := fun X Y h k b => h (k b);
            aphi := fun A C f a b => f (a, b);
            apsi := fun A C k q => k (fst q) (snd q);
            aphi_nat := _; apsi_nat := _; aphi_psi := _; apsi_phi := _ |}.
  - intros. reflexivity.
  - intros. reflexivity.
  - intros. reflexivity.
  - intros A C f [x b]. reflexivity.
Defined.

(* its unit, read at the identity, is pairing; its counit is evaluation *)
Lemma curry_unit (B A : Type) (a : A) : aunit (curryAdj B) A a = fun b => (a, b).
Proof. reflexivity. Qed.

Lemma curry_counit (B C : Type) (q : (B -> C) * B) : acounit (curryAdj B) C q = ev q.
Proof. reflexivity. Qed.

(* the triangles of currying, obtained from 9c *)
Corollary ev_after_pair (A B : Type) (x : A * B) : ev (fun b => (fst x, b), snd x) = x.
Proof. exact (triangle_L (curryAdj B) A x). Qed.

Corollary pair_then_ev (B C : Type) (k : B -> C) : (fun b => ev (k, b)) = k.
Proof. exact (triangle_R (curryAdj B) C k). Qed.

(* ----------------------------------------------------------------------
   9e  implication is the same adjunction read on propositions:
       (r /\ p -> q)  <->  (r -> p -> q)
   ---------------------------------------------------------------------- *)

Definition implAdj (p : Prop) : Adj.
Proof.
  refine {| aO := Prop; aEl := fun X => X;
            aL := fun X => X /\ p; aR := fun Y => p -> Y;
            almap := fun X Y g h => match h with conj a b => conj (g a) b end;
            armap := fun X Y g k x => g (k x);
            aphi := fun A C f a x => f (conj a x);
            apsi := fun A C k h => match h with conj a b => k a b end;
            aphi_nat := _; apsi_nat := _; aphi_psi := _; apsi_phi := _ |}.
  - intros. reflexivity.
  - intros A A' C g k [x1 x2]. reflexivity.
  - intros. reflexivity.
  - intros A C f [h1 h2]. reflexivity.
Defined.

Lemma impl_adj (rr p q : Prop) : (rr /\ p -> q) <-> (rr -> p -> q).
Proof. split; [exact (aphi (implAdj p) rr q) | exact (apsi (implAdj p) rr q)]. Qed.

(* ----------------------------------------------------------------------
   9f  Heyting algebras. The record is a definition, not an assumption:
       an order with top, bottom, meet, join, and implication right
       adjoint to meet.
   ---------------------------------------------------------------------- *)

Record Heyting : Type := {
  hc : Type;
  hle : hc -> hc -> Prop;
  htop : hc;
  hbot : hc;
  hmeet : hc -> hc -> hc;
  hjoin : hc -> hc -> hc;
  himp : hc -> hc -> hc;
  hle_refl : forall a, hle a a;
  hle_trans : forall a b c, hle a b -> hle b c -> hle a c;
  htop_max : forall a, hle a htop;
  hbot_min : forall a, hle hbot a;
  hmeet_l : forall a b, hle (hmeet a b) a;
  hmeet_r : forall a b, hle (hmeet a b) b;
  hmeet_univ : forall c a b, hle c a -> hle c b -> hle c (hmeet a b);
  hjoin_l : forall a b, hle a (hjoin a b);
  hjoin_r : forall a b, hle b (hjoin a b);
  hjoin_univ : forall a b c, hle a c -> hle b c -> hle (hjoin a b) c;
  himp_adj : forall rr p q, hle (hmeet rr p) q <-> hle rr (himp p q)
}.

(* propositions form a Heyting algebra: meet is the pair (E1), join the sum
   (E2), implication the map kind (E3), and its adjunction is 9e *)
Definition PropHA : Heyting.
Proof.
  refine {| hc := Prop; hle := fun a b => a -> b; htop := True; hbot := False;
            hmeet := and; hjoin := or; himp := fun a b => a -> b;
            hle_refl := _; hle_trans := _; htop_max := _; hbot_min := _;
            hmeet_l := _; hmeet_r := _; hmeet_univ := _;
            hjoin_l := _; hjoin_r := _; hjoin_univ := _;
            himp_adj := impl_adj |}; cbn.
  - intros a x. exact x.
  - intros a b c f g x. exact (g (f x)).
  - intros a _. exact I.
  - intros a x. destruct x.
  - intros a b [x _]. exact x.
  - intros a b [_ y]. exact y.
  - intros c a b f g x. exact (conj (f x) (g x)).
  - intros a b x. left. exact x.
  - intros a b x. right. exact x.
  - intros a b c f g [x | y]; [exact (f x) | exact (g y)].
Defined.

(* up-closed parts of an order (Kripke worlds) form a Heyting algebra;
   implication holds at x when it holds at every later stage *)
Section UpSets.
  Variable Pt : Type.
  Variable Rel : Pt -> Pt -> Prop.
  Hypothesis Rel_refl : forall x, Rel x x.
  Hypothesis Rel_trans : forall x y w, Rel x y -> Rel y w -> Rel x w.

  Record Up : Type := {
    up : Pt -> Prop;
    up_cl : forall x y, Rel x y -> up x -> up y
  }.

  Definition upTop : Up.
  Proof. refine {| up := fun _ => True; up_cl := _ |}. intros. exact I. Defined.

  Definition upBot : Up.
  Proof. refine {| up := fun _ => False; up_cl := _ |}. intros x y _ h. exact h. Defined.

  Definition upMeet (U V : Up) : Up.
  Proof.
    refine {| up := fun x => up U x /\ up V x; up_cl := _ |}.
    intros x y h [a b]. split; [exact (up_cl U x y h a) | exact (up_cl V x y h b)].
  Defined.

  Definition upJoin (U V : Up) : Up.
  Proof.
    refine {| up := fun x => up U x \/ up V x; up_cl := _ |}.
    intros x y h [a | b]; [left; exact (up_cl U x y h a) | right; exact (up_cl V x y h b)].
  Defined.

  Definition upImp (U V : Up) : Up.
  Proof.
    refine {| up := fun x => forall y, Rel x y -> up U y -> up V y; up_cl := _ |}.
    intros x y h f w h' u. exact (f w (Rel_trans x y w h h') u).
  Defined.

  Definition UpHA : Heyting.
  Proof.
    refine {| hc := Up; hle := fun U V => forall x, up U x -> up V x;
              htop := upTop; hbot := upBot; hmeet := upMeet; hjoin := upJoin; himp := upImp;
              hle_refl := _; hle_trans := _; htop_max := _; hbot_min := _;
              hmeet_l := _; hmeet_r := _; hmeet_univ := _;
              hjoin_l := _; hjoin_r := _; hjoin_univ := _; himp_adj := _ |}; cbn.
    - intros U x h. exact h.
    - intros U V W' f g x h. exact (g x (f x h)).
    - intros U x _. exact I.
    - intros U x h. destruct h.
    - intros U V x [a _]. exact a.
    - intros U V x [_ b]. exact b.
    - intros W' U V f g x h. exact (conj (f x h) (g x h)).
    - intros U V x h. left. exact h.
    - intros U V x h. right. exact h.
    - intros U V W' f g x [a | b]; [exact (f x a) | exact (g x b)].
    - intros W' U V. split.
      + intros f x w y hr u. exact (f y (conj (up_cl W' x y hr w) u)).
      + intros g x [w u]. exact (g x w x (Rel_refl x) u).
  Defined.
End UpSets.

(* ----------------------------------------------------------------------
   9g  intuitionistic laws, in every Heyting algebra
   ---------------------------------------------------------------------- *)

Section HeytingLaws.
  Variable H : Heyting.

  Definition hneg (p : hc H) : hc H := himp H p (hbot H).

  Lemma h_meet_comm (a b : hc H) : hle H (hmeet H a b) (hmeet H b a).
  Proof. apply hmeet_univ; [apply hmeet_r | apply hmeet_l]. Qed.

  (* modus ponens: the counit of the implication adjunction *)
  Lemma h_mp (p q : hc H) : hle H (hmeet H p (himp H p q)) q.
  Proof.
    apply (hle_trans H _ (hmeet H (himp H p q) p)).
    - apply h_meet_comm.
    - apply (proj2 (himp_adj H _ _ _)). apply hle_refl.
  Qed.

  (* meet distributes over join: a left adjoint keeps joins *)
  Lemma h_distr (p q r0 : hc H) :
    hle H (hmeet H p (hjoin H q r0)) (hjoin H (hmeet H p q) (hmeet H p r0)).
  Proof.
    apply (hle_trans H _ (hmeet H (hjoin H q r0) p)); [apply h_meet_comm |].
    apply (proj2 (himp_adj H _ _ _)).
    apply hjoin_univ; apply (proj1 (himp_adj H _ _ _)).
    - apply (hle_trans H _ (hmeet H p q)); [apply h_meet_comm | apply hjoin_l].
    - apply (hle_trans H _ (hmeet H p r0)); [apply h_meet_comm | apply hjoin_r].
  Qed.

  Lemma h_noncontra (p : hc H) : hle H (hmeet H p (hneg p)) (hbot H).
  Proof. exact (h_mp p (hbot H)). Qed.

  Lemma h_dn_intro (p : hc H) : hle H p (hneg (hneg p)).
  Proof.
    unfold hneg. apply (proj1 (himp_adj H _ _ _)). exact (h_mp p (hbot H)).
  Qed.

  Lemma h_neg_anti (p q : hc H) : hle H p q -> hle H (hneg q) (hneg p).
  Proof.
    intro hpq. unfold hneg. apply (proj1 (himp_adj H _ _ _)).
    apply (hle_trans H _ (hmeet H q (himp H q (hbot H)))).
    - apply hmeet_univ.
      + apply (hle_trans H _ p); [apply hmeet_r | exact hpq].
      + apply hmeet_l.
    - exact (h_mp q (hbot H)).
  Qed.

  Lemma h_tneg (p : hc H) : hle H (hneg (hneg (hneg p))) (hneg p).
  Proof. exact (h_neg_anti _ _ (h_dn_intro p)). Qed.

  (* excluded middle is never refuted *)
  Lemma h_nn_em (p : hc H) : hle H (htop H) (hneg (hneg (hjoin H p (hneg p)))).
  Proof.
    change (hle H (htop H) (himp H (hneg (hjoin H p (hneg p))) (hbot H))).
    apply (proj1 (himp_adj H _ _ _)).
    apply (hle_trans H _ (hmeet H (hjoin H p (hneg p)) (hneg (hjoin H p (hneg p))))).
    - apply hmeet_univ.
      + apply (hle_trans H _ (hneg p)).
        * apply (hle_trans H _ (hneg (hjoin H p (hneg p)))).
          -- apply hmeet_r.
          -- apply h_neg_anti. apply hjoin_l.
        * apply hjoin_r.
      + apply hmeet_r.
    - apply h_noncontra.
  Qed.
End HeytingLaws.

(* the same laws as statements of intuitionistic logic *)
Corollary prop_nn_em (p : Prop) : ~ ~ (p \/ ~ p).
Proof. exact (h_nn_em PropHA p I). Qed.

Corollary prop_tneg (p : Prop) : ~ ~ ~ p -> ~ p.
Proof. exact (h_tneg PropHA p). Qed.

(* ----------------------------------------------------------------------
   9h  excluded middle is not a law: it fails in the world of one arrow
       l -> r (HA4), which satisfies every law of 9f
   ---------------------------------------------------------------------- *)

Definition R2 (x y : Two) : Prop :=
  match x, y with r, l => False | _, _ => True end.

Lemma R2_refl (x : Two) : R2 x x.
Proof. destruct x; exact I. Qed.

Lemma R2_trans (x y w : Two) : R2 x y -> R2 y w -> R2 x w.
Proof. destruct x, y, w; cbn; intros h1 h2; first [exact I | contradiction]. Qed.

Definition UH : Heyting := UpHA Two R2 R2_refl R2_trans.

(* the part {r}: true only at the later stage *)
Definition U0 : Up Two R2.
Proof.
  refine {| up := fun x => x = r; up_cl := _ |}.
  intros x y h e. subst x. destruct y; [cbn in h; contradiction | reflexivity].
Defined.

Theorem no_excluded_middle : ~ hle UH (htop UH) (hjoin UH U0 (hneg UH U0)).
Proof.
  intro h. pose proof (h l I) as H0. cbn in H0.
  destruct H0 as [e | n].
  - exact (l_ne_r e).
  - exact (n r I eq_refl).
Qed.

(* ----------------------------------------------------------------------
   9i  the Boolean part: statements that are cuts (E2) satisfy double
       negation elimination
   ---------------------------------------------------------------------- *)

Lemma dne_cut (t : Two) : ~ ~ (t = l) -> t = l.
Proof.
  destruct t.
  - intros _. reflexivity.
  - intro h. exfalso. apply h. intro e. exact (l_ne_r (eq_sym e)).
Qed.

(* ======================================================================
   Check: each line should print "Closed under the global context".
   ====================================================================== *)

Print Assumptions read_cov.
Print Assumptions read_contra.
Print Assumptions read_place.
Print Assumptions place_read.
Print Assumptions kcomp_assoc.
Print Assumptions Jrule_refl.
Print Assumptions path_assoc.
Print Assumptions path_inv_r.
Print Assumptions l_ne_r.
Print Assumptions triangle_L.
Print Assumptions triangle_R.
Print Assumptions curryAdj.
Print Assumptions ev_after_pair.
Print Assumptions pair_then_ev.
Print Assumptions implAdj.
Print Assumptions impl_adj.
Print Assumptions PropHA.
Print Assumptions UpHA.
Print Assumptions h_mp.
Print Assumptions h_distr.
Print Assumptions h_dn_intro.
Print Assumptions h_tneg.
Print Assumptions h_nn_em.
Print Assumptions prop_nn_em.
Print Assumptions no_excluded_middle.
Print Assumptions dne_cut.

Output of the check. The first two lines are the output of coqc –version; the remaining lines are the 26 results of Print Assumptions.

source_check.txt

The Rocq Prover, version 9.3.0
compiled with OCaml 5.5.2
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context
Closed under the global context

PIC