module Cat.Allegory.Reasoning {o β„“ β„“'} (A : Allegory o β„“ β„“') where

Combinators for allegoriesπŸ”—

For reasoning about morphisms in allegories, the first thing we need is a convenient syntax for piecing together long arguments by transitivity. The following operators allow us to interweave throwing in an inequality, and rewriting by an equality:

private variable
  w x y z : Ob
  a b c d f f' g g' h i : Hom x y

_β‰€βŸ¨_⟩_ : βˆ€ (f : Hom x y) β†’ f ≀ g β†’ g ≀ h β†’ f ≀ h
_=⟨_⟩_ : βˆ€ (f : Hom x y) β†’ f ≑ g β†’ g ≀ h β†’ f ≀ h
_=˘⟨_⟩_ : βˆ€ (f : Hom x y) β†’ g ≑ f β†’ g ≀ h β†’ f ≀ h
_β‰€βˆŽ    : βˆ€ (f : Hom x y) β†’ f ≀ f

f β‰€βŸ¨ p βŸ©β‰€ q = ≀-trans p q
f =⟨ p ⟩= q = subst (_≀ _) (sym p) q
f =˘⟨ p ⟩=˘ q = subst (_≀ _) p q
f β‰€βˆŽ = ≀-refl

infixr 40 _β—€_ _β–Ά_
infixr 2 _=⟨_⟩_ _=˘⟨_⟩_ _β‰€βŸ¨_⟩_
infix  3 _β‰€βˆŽ

Additionally, we have whiskering operations, derived from the horizontal composition operation by holding one of the operands constant:

_β–Ά_ : (f : Hom x y) β†’ g ≀ h β†’ f ∘ g ≀ f ∘ h
_β—€_ : f ≀ g β†’ (h : Hom x y) β†’ f ∘ h ≀ g ∘ h

f β–Ά g = ≀-refl β—† g
g β—€ f = g β—† ≀-refl

A few lemmas about the meet operation are also in order. It, unsurprisingly, behaves like the binary operation of a meet-semilattice: We have that is equivalently and is a commutative, associative idempotent binary operation, which preserves ordering in both of its arguments.

∩-pres-r     : g ≀ g' β†’ f ∩ g ≀ f ∩ g'
∩-pres-l     : f ≀ f' β†’ f ∩ g ≀ f' ∩ g
∩-pres     : f ≀ f' β†’ g ≀ g' β†’ f ∩ g ≀ f' ∩ g'
∩-distribl   : f ∘ (g ∩ h) ≀ (f ∘ g) ∩ (f ∘ h)
∩-distribr   : (g ∩ h) ∘ f ≀ (g ∘ f) ∩ (h ∘ f)
∩-distrib   : (f ∩ g) ∘ (h ∩ i) ≀ (f ∘ h ∩ g ∘ h) ∩ (f ∘ i ∩ g ∘ i)
∩-assoc      : (f ∩ g) ∩ h ≑ f ∩ (g ∩ h)
∩-comm       : f ∩ g ≑ g ∩ f
∩-idempotent : f ∩ f ≑ f
modular'
  : βˆ€ {x y z} (f : Hom x y) (g : Hom y z) (h : Hom x z)
  β†’ (g ∘ f) ∩ h ≀ (g ∩ (h ∘ f †)) ∘ f
modular' f g h =
  (g ∘ f) ∩ h                     =˘⟨ dual _ ⟩=˘
  ⌜ ((g ∘ f) ∩ h) † ⌝ †           =⟨ ap! (dual-∩ A) ⟩=
  (⌜ (g ∘ f) † ⌝ ∩ h †) †         =⟨ ap! dual-∘ ⟩=
  ⌜ ((f † ∘ g †) ∩ h †) ⌝ †       β‰€βŸ¨ dual-≀ (modular (g †) (f †) (h †)) βŸ©β‰€
  (f † ∘ (g † ∩ (f † † ∘ h †))) † =⟨ dual-∘ ⟩=
  (g † ∩ (f † † ∘ h †)) † ∘ f † † =⟨ apβ‚‚ _∘_ (dual-∩ A) (dual f) ⟩=
  (g † † ∩ (f † † ∘ h †) †) ∘ f   =⟨ apβ‚‚ _∘_ (apβ‚‚ _∩_ (dual g) (ap _† (apβ‚‚ _∘_ (dual f) refl) Β·Β· dual-∘ Β·Β· ap (_∘ f †) (dual h))) refl ⟩=
  (g ∩ h ∘ f †) ∘ f               β‰€βˆŽ

IdentitiesπŸ”—

abstract
  ≀-eliml : f ≀ id β†’ f ∘ g ≀ g
  ≀-eliml {f = f} {g = g} w =
    f ∘ g  β‰€βŸ¨ w β—€ g βŸ©β‰€
    id ∘ g =⟨ idl _ ⟩=
    g β‰€βˆŽ

  ≀-elimr : g ≀ id β†’ f ∘ g ≀ f
  ≀-elimr {g = g} {f = f} w =
    f ∘ g  β‰€βŸ¨ f β–Ά w βŸ©β‰€
    f ∘ id =⟨ idr _ ⟩=
    f β‰€βˆŽ

  ≀-introl : id ≀ f β†’ g ≀ f ∘ g
  ≀-introl {f = f} {g = g} w =
    g      =⟨ sym (idl _) ⟩=
    id ∘ g β‰€βŸ¨ w β—€ g βŸ©β‰€
    f ∘ g  β‰€βˆŽ

  ≀-intror : id ≀ g β†’ f ≀ f ∘ g
  ≀-intror {g = g} {f = f} w =
    f      =⟨ sym (idr _) ⟩=
    f ∘ id β‰€βŸ¨ f β–Ά w βŸ©β‰€
    f ∘ g  β‰€βˆŽ

AssociationsπŸ”—

  ≀-pushl : a ≀ b ∘ c β†’ a ∘ f ≀ b ∘ c ∘ f
  ≀-pushl {a = a} {b = b} {c = c} {f = f} w =
    a ∘ f       β‰€βŸ¨ w β—€ f βŸ©β‰€
    (b ∘ c) ∘ f =⟨ sym (assoc b c f) ⟩=
    b ∘ c ∘ f   β‰€βˆŽ

  ≀-pushr : a ≀ b ∘ c β†’ f ∘ a ≀ (f ∘ b) ∘ c
  ≀-pushr {a = a} {b = b} {c = c} {f = f}  w =
    f ∘ a       β‰€βŸ¨ f β–Ά w βŸ©β‰€
    f ∘ b ∘ c   =⟨ assoc f b c ⟩=
    (f ∘ b) ∘ c β‰€βˆŽ

  ≀-pulll : a ∘ b ≀ c β†’ a ∘ b ∘ f ≀ c ∘ f
  ≀-pulll {a = a} {b = b} {c = c} {f = f} w =
    a ∘ b ∘ f   =⟨ assoc a b f ⟩=
    (a ∘ b) ∘ f β‰€βŸ¨ w β—€ f βŸ©β‰€
    c ∘ f       β‰€βˆŽ

  ≀-pullr : a ∘ b ≀ c β†’ (f ∘ a) ∘ b ≀ f ∘ c
  ≀-pullr {a = a} {b = b} {c = c} {f = f} w =
    (f ∘ a) ∘ b   =⟨ sym (assoc f a b) ⟩=
    f ∘ a ∘ b     β‰€βŸ¨ f β–Ά w βŸ©β‰€
    f ∘ c         β‰€βˆŽ

  ≀-extendl : a ∘ b ≀ c ∘ d β†’ a ∘ b ∘ f ≀ c ∘ d ∘ f
  ≀-extendl {a = a} {b = b} {c = c} {d = d} {f = f} w =
    a ∘ b ∘ f   β‰€βŸ¨ ≀-pulll w βŸ©β‰€
    (c ∘ d) ∘ f =⟨ sym (assoc c d f) ⟩=
    c ∘ d ∘ f   β‰€βˆŽ

  ≀-extendr : a ∘ b ≀ c ∘ d β†’ (f ∘ a) ∘ b ≀ (f ∘ c) ∘ d
  ≀-extendr {a = a} {b = b} {c = c} {d = d} {f = f} w =
    (f ∘ a) ∘ b β‰€βŸ¨ ≀-pullr w βŸ©β‰€
    f ∘ c ∘ d   =⟨ assoc f c d ⟩=
    (f ∘ c) ∘ d β‰€βˆŽ

  ≀-pull-inner : a ∘ b ≀ c β†’ (f ∘ a) ∘ (b ∘ g) ≀ f ∘ c ∘ g
  ≀-pull-inner w = ≀-pullr (≀-pulll w)

  ≀-pull-outer : a ∘ b ≀ f β†’ c ∘ d ≀ g β†’ a ∘ (b ∘ c) ∘ d ≀ f ∘ g
  ≀-pull-outer w v = ≀-trans (≀-pulll (≀-pulll w)) (≀-pullr v)

  ≀-extend-inner : a ∘ b ≀ c ∘ d β†’ (f ∘ a) ∘ (b ∘ g) ≀ (f ∘ c) ∘ (d ∘ g)
  ≀-extend-inner w = ≀-extendr (≀-extendl w)

CancellationsπŸ”—

  ≀-cancell : a ∘ b ≀ id β†’ a ∘ b ∘ f ≀ f
  ≀-cancell w = ≀-trans (≀-pulll w) (≀-eliml ≀-refl)

  ≀-cancelr : a ∘ b ≀ id β†’ (f ∘ a) ∘ b ≀ f
  ≀-cancelr w = ≀-trans (≀-pullr w) (≀-elimr ≀-refl)

DualsπŸ”—

  ≀-conj : βˆ€ (f : Hom x x) β†’ f ≀ f ∘ f † ∘ f
  ≀-conj f =
    f                    β‰€βŸ¨ ∩-univ (≀-introl ≀-refl) ≀-refl βŸ©β‰€
    (id ∘ f) ∩ f         β‰€βŸ¨ modular' f id f βŸ©β‰€
    (id ∩ (f ∘ f †)) ∘ f β‰€βŸ¨ ≀-pushl ∩-le-r βŸ©β‰€
    f ∘ f † ∘ f β‰€βˆŽ

  †-pulll : a ∘ b † ≀ c β†’ a ∘ (f ∘ b) † ≀ c ∘ f †
  †-pulll {a = a} {b = b} {c = c} {f = f} w =
    a ∘ (f ∘ b) † =⟨ ap (a ∘_) dual-∘ ⟩=
    a ∘ b † ∘ f † β‰€βŸ¨ ≀-pulll w βŸ©β‰€
    c ∘ f † β‰€βˆŽ

  †-pullr : a † ∘ b ≀ c β†’ (a ∘ f) † ∘ b ≀ f † ∘ c
  †-pullr {a = a} {b = b} {c = c} {f = f} w =
    (a ∘ f) † ∘ b =⟨ ap (_∘ b) dual-∘ ⟩=
    (f † ∘ a †) ∘ b β‰€βŸ¨ ≀-pullr w βŸ©β‰€
    f † ∘ c β‰€βˆŽ

  †-inner : (p : g ∘ g' † ≑ h) β†’ (f ∘ g) ∘ (f' ∘ g') † ≑ f ∘ h ∘ f' †
  †-inner p = apβ‚‚ _∘_ refl dual-∘ βˆ™ sym (assoc _ _ _)
            βˆ™ apβ‚‚ _∘_ refl (assoc _ _ _ βˆ™ apβ‚‚ _∘_ p refl)

  †-cancell : a ∘ b † ≀ id β†’ a ∘ (f ∘ b) † ≀ f †
  †-cancell w = ≀-trans (†-pulll w) (≀-eliml ≀-refl)

  †-cancelr : a † ∘ b ≀ id β†’ (a ∘ f) † ∘ b ≀ f †
  †-cancelr w = ≀-trans (†-pullr w) (≀-elimr ≀-refl)

  †-cancel-inner : a ∘ b † ≀ id β†’ (f ∘ a) ∘ (g ∘ b) † ≀ f ∘ g †
  †-cancel-inner w = †-pulll (≀-cancelr w)