Other Agda modules in this folder (2)

Complete compiler-highlighted source. Follow a symbol to its declaration.

# Pasting comparison routes

A route through a commutative diagram can be normalized by its component
squares and a specified cancellation. The objects, arrows, and comparison
witnesses remain parameters; these calculations use no equality of proofs.

```agda
{-# OPTIONS --safe --without-K #-}
open import Agda.Primitive using (Level)
open import SCT.Calculus.Composition using (Composition; Laws)

module SCT.Calculus.Squares {o h p : Level}
  (C : Composition o h p) (L : Laws C) where

open Composition C
open Laws L
```

For two routes joined by an intermediate comparison, first normalize their
initial segment, then paste the input square, and finally cancel the
specified inverse pair. Each supplied square occurs in the resulting witness.

```agda
abstract
  comparison-route-pasting : {sβ‚€ s₁ sβ‚‚ s₃ sβ‚„ t zβ‚€ z₁ zβ‚‚ z : Obj}
    (T : Hom t z) (R : Hom sβ‚„ t) (A : Hom s₃ sβ‚„) (G : Hom sβ‚‚ s₃)
    (B : Hom s₁ sβ‚‚) (Ξ· : Hom sβ‚€ s₁)
    (n : Hom sβ‚„ zβ‚€) (W : Hom zβ‚€ z₁) (WQ : Hom sβ‚‚ zβ‚‚)
    (changed : Hom z₁ z) (corner : Hom zβ‚‚ z₁)
    (application : Hom zβ‚‚ z) (inverseApplication : Hom z zβ‚‚)
    (application-route : Hom sβ‚€ z)
    β†’ (T ∘ R) β‰ˆ ((changed ∘ W) ∘ n)
    β†’ (corner ∘ WQ) β‰ˆ (W ∘ (n ∘ (A ∘ G)))
    β†’ (changed ∘ corner) β‰ˆ application
    β†’ (WQ ∘ (B ∘ Ξ·)) β‰ˆ (inverseApplication ∘ application-route)
    β†’ (application ∘ inverseApplication) β‰ˆ id z
    β†’ (T ∘ (R ∘ (A ∘ (G ∘ (B ∘ Ξ·))))) β‰ˆ application-route
  comparison-route-pasting T R A G B Ξ· n W WQ changed corner application inverseApplication application-route
    normal inputChange cancelCorner cancelEvaluation cancelApplication =
    let tail = B ∘ η
        finish = trans (unitΛ‘ application-route)
          (trans (congr cancelApplication (refl application-route))
          (trans (sym (assoc application inverseApplication application-route))
            (congr (refl application) cancelEvaluation)))
        normalizeTail = trans (congr cancelCorner (refl (WQ ∘ tail)))
          (trans (sym (assoc changed corner (WQ ∘ tail)))
          (trans (congr (refl changed) (assoc corner WQ tail))
            (congr (refl changed) (congr (sym inputChange) (refl tail)))))
        regroupInner = trans (sym (assoc W (n ∘ (A ∘ G)) tail))
          (trans (congr (refl W) (sym (assoc n (A ∘ G) tail)))
            (congr (refl W) (congr (refl n) (sym (assoc A G tail)))))
        regroup = trans (congr (refl changed) regroupInner)
          (trans (assoc changed W (n ∘ (A ∘ (G ∘ tail))))
            (assoc (changed ∘ W) n (A ∘ (G ∘ tail))))
        normalizeStart = trans (congr normal (refl (A ∘ (G ∘ tail))))
          (sym (assoc T R (A ∘ (G ∘ tail))))
    in trans finish (trans normalizeTail (trans regroup normalizeStart))
```

A square may be pasted after a factored input. If the outer action cancels
the top comparison, the composite reduces to the remaining two sides.

```agda
abstract
  factored-square : {s a b c d z : Obj}
    (outer : Hom c z) (left : Hom b c) (right : Hom a d)
    (input : Hom a b) (action : Hom d c) (changed : Hom d z)
    (tail : Hom s a) (image : Hom s b)
    β†’ (outer ∘ action) β‰ˆ changed
    β†’ (left ∘ input) β‰ˆ (action ∘ right)
    β†’ image β‰ˆ (input ∘ tail)
    β†’ ((outer ∘ left) ∘ image) β‰ˆ ((changed ∘ right) ∘ tail)
  factored-square outer left right input action changed tail image cancelAction natural factor =
    trans (sym (assoc changed right tail))
      (trans (congr cancelAction (refl (right ∘ tail)))
      (trans (sym (assoc outer action (right ∘ tail)))
      (trans (congr (refl outer) (assoc action right tail))
      (trans (congr (refl outer) (congr natural (refl tail)))
      (trans (congr (refl outer) (sym (assoc left input tail)))
      (trans (assoc outer left (input ∘ tail))
        (congr (refl (outer ∘ left)) factor)))))))
```

For a common frame, paste the restriction square with the naturality square,
merge the two images, and cancel the supplied reverse frame comparison. The
fourfold reassociation and the frame cancellation are specified inputs, so
applications can retain their existing witnesses even when those arise from
a different realization of the elementary laws.

```agda
abstract
  common-frame-pasting : {sβ‚€ s₁ sβ‚‚ s₃ sβ‚„ rβ‚€ r₁ rβ‚‚ z : Obj}
    (outer : Hom sβ‚„ z) (G : Hom s₃ sβ‚„) (A : Hom sβ‚‚ s₃) (N : Hom sβ‚‚ z)
    (pre : Hom s₁ sβ‚‚) (W : Hom sβ‚€ s₁)
    (W₁ : Hom r₁ s₃) (Wβ‚‚ : Hom rβ‚‚ sβ‚„)
    (paired : Hom r₁ rβ‚‚) (pairPre : Hom rβ‚€ r₁) (postB : Hom rβ‚€ rβ‚‚)
    (frame : Hom sβ‚€ rβ‚€) (back : Hom rβ‚€ sβ‚€)
    β†’ N β‰ˆ (outer ∘ (G ∘ A))
    β†’ (A ∘ (pre ∘ W)) β‰ˆ (W₁ ∘ (pairPre ∘ frame))
    β†’ (G ∘ W₁) β‰ˆ (Wβ‚‚ ∘ paired)
    β†’ (paired ∘ pairPre) β‰ˆ postB
    β†’ ((Wβ‚‚ ∘ paired) ∘ (pairPre ∘ frame)) β‰ˆ (Wβ‚‚ ∘ ((paired ∘ pairPre) ∘ frame))
    β†’ ((((outer ∘ Wβ‚‚) ∘ postB) ∘ frame) ∘ back) β‰ˆ ((outer ∘ Wβ‚‚) ∘ postB)
    β†’ (N ∘ (pre ∘ (W ∘ back))) β‰ˆ ((outer ∘ Wβ‚‚) ∘ postB)
  common-frame-pasting outer G A N pre W W₁ Wβ‚‚ paired pairPre postB frame back
    splitN restriction natural merge regroupFour cancellation =
    let lower = trans
          (congr (refl outer)
            (trans (congr (refl Wβ‚‚) (congr merge (refl frame))) regroupFour))
          (congr (refl outer)
            (trans (congr natural (refl (pairPre ∘ frame)))
              (sym (assoc G W₁ (pairPre ∘ frame)))))
        beforeCancel = trans (sym (assoc (outer ∘ Wβ‚‚) postB frame))
          (trans (sym (assoc outer Wβ‚‚ (postB ∘ frame)))
          (trans lower
          (trans (congr (refl outer) (congr (refl G) restriction))
          (trans (congr (refl outer) (assoc G A (pre ∘ W)))
          (trans (assoc outer (G ∘ A) (pre ∘ W))
            (congr splitN (refl (pre ∘ W))))))))
        regroup = trans (sym (assoc N (pre ∘ W) back))
          (congr (refl N) (sym (assoc pre W back)))
    in trans cancellation (trans (congr beforeCancel (refl back)) regroup)
```

A normalized operation is a comparison of its inputs after restriction.
For its short route, use the substitution square, iterate restriction,
and combine the two input comparisons. The iteration and combination
witnesses are supplied by the operation under consideration.

```agda
abstract
  normalized-substitution-square : {sβ‚€ s₁ sβ‚‚ tβ‚€ t₁ b z : Obj}
    (paired : Hom t₁ z) (newPre : Hom sβ‚‚ t₁) (change : Hom s₁ sβ‚‚)
    (A : Hom sβ‚€ s₁) (changed : Hom tβ‚€ t₁) (oldPre : Hom s₁ tβ‚€)
    (action : Hom b tβ‚€) (base : Hom sβ‚€ b) (inner : Hom b t₁) (whole : Hom b z)
    β†’ (newPre ∘ change) β‰ˆ (changed ∘ oldPre)
    β†’ (oldPre ∘ A) β‰ˆ (action ∘ base)
    β†’ (changed ∘ (action ∘ base)) β‰ˆ (inner ∘ base)
    β†’ (paired ∘ (inner ∘ base)) β‰ˆ (whole ∘ base)
    β†’ ((paired ∘ newPre) ∘ (change ∘ A)) β‰ˆ (whole ∘ base)
  normalized-substitution-square paired newPre change A changed oldPre action base inner whole
    substituteSquare iteration combineInner combineOuter =
    let substitute = trans (assoc changed oldPre A)
          (trans (congr substituteSquare (refl A))
            (sym (assoc newPre change A)))
        iterate = congr (refl changed) iteration
    in trans combineOuter
      (trans (congr (refl paired) (trans combineInner (trans iterate substitute)))
        (assoc paired newPre (change ∘ A)))
```

For the long route, distribute restriction over the existing normalization,
use the input square, then combine its output comparisons.

```agda
abstract
  normalized-input-square : {s a b c e z : Obj}
    (outer : Hom e z) (middle : Hom c e) (input : Hom a c)
    (before : Hom s a) (output : Hom b e) (pre : Hom a b)
    (image : Hom s c) (whole : Hom b z)
    β†’ (middle ∘ input) β‰ˆ (output ∘ pre)
    β†’ image β‰ˆ (input ∘ before)
    β†’ (outer ∘ (output ∘ (pre ∘ before))) β‰ˆ (whole ∘ (pre ∘ before))
    β†’ (outer ∘ (middle ∘ image)) β‰ˆ (whole ∘ (pre ∘ before))
  normalized-input-square outer middle input before output pre image whole natural split combine =
    let exchange = trans (assoc output pre before)
          (trans (congr natural (refl before))
            (sym (assoc middle input before)))
    in trans combine
      (trans (congr (refl outer) exchange)
        (congr (refl outer) (congr (refl middle) split)))
```

Changing an input and restricting it give adjacent squares. Expand the
specified source comparison, paste the change square with the restriction
square, then combine the two actions on the output.

```agda
abstract
  change-restriction-square : {s a b x y zβ‚€ z₁ : Obj}
    (leftβ€² : Hom b z₁) (change : Hom a b) (middle : Hom a zβ‚€)
    (first : Hom zβ‚€ z₁) (second : Hom x zβ‚€) (pre : Hom y x)
    (retained : Hom s y) (tail : Hom s a) (source : Hom s b) (whole : Hom x z₁)
    β†’ (first ∘ second) β‰ˆ whole
    β†’ (middle ∘ tail) β‰ˆ (second ∘ (pre ∘ retained))
    β†’ (leftβ€² ∘ change) β‰ˆ (first ∘ middle)
    β†’ source β‰ˆ (change ∘ tail)
    β†’ (leftβ€² ∘ source) β‰ˆ ((whole ∘ pre) ∘ retained)
  change-restriction-square leftβ€² change middle first second pre retained tail source whole
    normalizeAction restriction natural expandSource =
    let normalizeEnd = trans (sym (assoc whole pre retained))
          (trans (congr normalizeAction (refl (pre ∘ retained)))
            (sym (assoc first second (pre ∘ retained))))
        useRestriction = trans (congr (refl first) restriction)
          (assoc first middle tail)
        useChange = trans (congr natural (refl tail))
          (sym (assoc leftβ€² change tail))
        expand = congr (refl leftβ€²) expandSource
    in trans normalizeEnd (trans useRestriction (trans useChange expand))
```

A factored input can be transported through a square and then combined
with an outer comparison. The factorization, naturality, and combination
witnesses are explicit; the calculation only supplies the reassociations.

```agda
abstract
  compose-input-square : {s a b c e z : Obj}
    (outer : Hom e z) (middle : Hom c e) (input : Hom a c)
    (before : Hom s a) (output : Hom b e) (pre : Hom a b)
    (image : Hom s c) (whole : Hom b z)
    β†’ (middle ∘ input) β‰ˆ (output ∘ pre)
    β†’ (outer ∘ output) β‰ˆ whole
    β†’ image β‰ˆ (input ∘ before)
    β†’ (outer ∘ (middle ∘ image)) β‰ˆ (whole ∘ (pre ∘ before))
  compose-input-square outer middle input before output pre image whole natural join split =
    let exchange = trans (assoc output pre before)
          (trans (congr natural (refl before))
            (sym (assoc middle input before)))
        expand = congr (refl middle) split
    in trans (congr join (refl (pre ∘ before)))
      (trans (sym (assoc outer output (pre ∘ before)))
        (congr (refl outer) (trans exchange expand)))
```

A square remains commutative after adjoining the same tail to both routes.
For a route with two initial comparisons, the second calculation includes
its additional reassociation explicitly.

```agda
abstract
  square-with-tail : {s a b c z : Obj}
    (left : Hom b z) (bottom : Hom a b)
    (top : Hom c z) (right : Hom a c) (tail : Hom s a)
    β†’ (left ∘ bottom) β‰ˆ (top ∘ right)
    β†’ (left ∘ (bottom ∘ tail)) β‰ˆ (top ∘ (right ∘ tail))
  square-with-tail left bottom top right tail square =
    trans (assoc top right tail)
      (trans (congr square (refl tail)) (sym (assoc left bottom tail)))

  composite-square-with-tail : {s a b c d z : Obj}
    (left : Hom c z) (middle : Hom b c) (bottom : Hom a b)
    (top : Hom d z) (right : Hom a d) (tail : Hom s a)
    β†’ ((left ∘ middle) ∘ bottom) β‰ˆ (top ∘ right)
    β†’ (left ∘ (middle ∘ (bottom ∘ tail))) β‰ˆ (top ∘ (right ∘ tail))
  composite-square-with-tail left middle bottom top right tail square =
    trans (assoc top right tail)
      (trans (congr square (refl tail))
        (trans (sym (assoc (left ∘ middle) bottom tail))
          (sym (assoc left middle (bottom ∘ tail)))))
```

Two successive input comparisons can be normalized along a naturality
square. Supply both combination comparisons separately, so the result
retains their prescribed witnesses.

```agda
abstract
  normalize-comparison-route : {s t a b c d z : Obj}
    (outer : Hom d z) (first : Hom c d) (second : Hom b c) (third : Hom a b)
    (after : Hom t c) (e : Hom s t) (sourceAfter : Hom s b) (tail : Hom s a)
    (inner : Hom a c) (whole : Hom a d)
    β†’ (second ∘ (third ∘ tail)) β‰ˆ (inner ∘ tail)
    β†’ (first ∘ (inner ∘ tail)) β‰ˆ (whole ∘ tail)
    β†’ (after ∘ e) β‰ˆ (second ∘ sourceAfter)
    β†’ sourceAfter β‰ˆ (third ∘ tail)
    β†’ ((outer ∘ (first ∘ after)) ∘ e) β‰ˆ (outer ∘ (whole ∘ tail))
  normalize-comparison-route outer first second third after e sourceAfter tail inner whole
    joinInner joinOuter natural restrict =
    let normalize = trans joinOuter
          (congr (refl first) (trans joinInner (congr (refl second) restrict)))
        expand = trans (congr (refl outer)
            (trans (congr (refl first) natural) (assoc first after e)))
          (assoc outer (first ∘ after) e)
    in trans (congr (refl outer) normalize) expand
```


For a nested input, keep the boundary routes as parameters while pasting
restriction, naturality, and the two input-combination comparisons. The
application-specific calculations supply these comparisons; the following
assembly only performs the displayed congruences and reassociations.

```agda
module NestedRoute {s n q r c d e u v z : Obj}
  (N : Hom n z) (Pg : Hom q n) (V : Hom s q)
  (changed : Hom c z) (restricted : Hom n c)
  (Cβ‚€ : Hom r q) (rest : Hom s r)
  (C₁ : Hom d c) (middle : Hom r d)
  (Cβ‚‚ : Hom e z) (Qact : Hom d e)
  (Ract : Hom u d) (Uact : Hom v u) (base : Hom s v)
  (inner : Hom v d) (whole : Hom v e) where

  abstract
    finish : (middle ∘ rest) β‰ˆ (Ract ∘ (Uact ∘ base))
      β†’ (Ract ∘ (Uact ∘ base)) β‰ˆ (inner ∘ base)
      β†’ (Qact ∘ (inner ∘ base)) β‰ˆ (whole ∘ base)
      β†’ (Cβ‚‚ ∘ (Qact ∘ (middle ∘ rest))) β‰ˆ (Cβ‚‚ ∘ (whole ∘ base))
    finish project combineInner combineOuter =
      congr (refl Cβ‚‚)
        (trans combineOuter (congr (refl Qact) (trans combineInner project)))

    begin : N β‰ˆ (changed ∘ restricted) β†’ V β‰ˆ (Cβ‚€ ∘ rest)
      β†’ (N ∘ (Pg ∘ V)) β‰ˆ (changed ∘ (restricted ∘ (Pg ∘ (Cβ‚€ ∘ rest))))
    begin split expand = trans (assoc changed restricted (Pg ∘ (Cβ‚€ ∘ rest)))
      (congr split (congr (refl Pg) expand))

    result : (Cβ‚‚ ∘ (Qact ∘ (middle ∘ rest))) β‰ˆ (Cβ‚‚ ∘ (whole ∘ base))
      β†’ (changed ∘ (C₁ ∘ (middle ∘ rest))) β‰ˆ (Cβ‚‚ ∘ (Qact ∘ (middle ∘ rest)))
      β†’ (restricted ∘ (Pg ∘ (Cβ‚€ ∘ rest))) β‰ˆ (C₁ ∘ (middle ∘ rest))
      β†’ (N ∘ (Pg ∘ V)) β‰ˆ (changed ∘ (restricted ∘ (Pg ∘ (Cβ‚€ ∘ rest))))
      β†’ (N ∘ (Pg ∘ V)) β‰ˆ (Cβ‚‚ ∘ (whole ∘ base))
    result finish natural restrict begin =
      trans finish (trans natural (trans (congr (refl changed) restrict) begin))
```


Naturality can be transported between two chosen normalization routes.
The four route squares and the central naturality square remain explicit.
The conclusion retains the common final comparison, which a caller may
subsequently cancel using its chosen reflection operation.

```agda
abstract
  normalization-naturality : {xβ‚€ x₁ yβ‚€ y₁ uβ‚€ u₁ vβ‚€ v₁ : Obj}
    (a : Hom xβ‚€ yβ‚€) (aβ€² : Hom x₁ y₁) (S : Hom xβ‚€ x₁) (T : Hom yβ‚€ y₁)
    (L : Hom xβ‚€ uβ‚€) (Lβ€² : Hom x₁ u₁) (R : Hom yβ‚€ vβ‚€) (Rβ€² : Hom y₁ v₁)
    (l : Hom uβ‚€ u₁) (r : Hom vβ‚€ v₁) (A : Hom uβ‚€ vβ‚€) (Aβ€² : Hom u₁ v₁)
    β†’ (R ∘ a) β‰ˆ (A ∘ L) β†’ (Rβ€² ∘ aβ€²) β‰ˆ (Aβ€² ∘ Lβ€²)
    β†’ (Lβ€² ∘ S) β‰ˆ (l ∘ L) β†’ (Rβ€² ∘ T) β‰ˆ (r ∘ R)
    β†’ (Aβ€² ∘ l) β‰ˆ (r ∘ A)
    β†’ (Rβ€² ∘ (aβ€² ∘ S)) β‰ˆ (Rβ€² ∘ (T ∘ a))
  normalization-naturality a aβ€² S T L Lβ€² R Rβ€² l r A Aβ€²
    route routeβ€² left right central =
    trans (assoc Rβ€² T a)
      (trans (congr (sym right) (refl a))
      (trans (sym (assoc r R a))
      (trans (congr (refl r) (sym route))
      (trans (assoc r A L)
      (trans (congr central (refl L))
      (trans (sym (assoc Aβ€² l L))
      (trans (congr (refl Aβ€²) left)
      (trans (assoc Aβ€² Lβ€² S)
      (trans (congr routeβ€² (refl S))
        (sym (assoc Rβ€² aβ€² S)))))))))))
```

Changing the source and target frames of a square uses the prescribed
normalization of its input and the prescribed cancellation of the target
frame. No inverse operation or uniqueness of comparison proofs is assumed.

```agda
abstract
  normalized-frame-square : {x a b y d e : Obj}
    (k : Hom a d) (kβ€² : Hom b e) (back : Hom x a)
    (v : Hom b y) (reverse : Hom y b) (z : Hom a b) (w : Hom d e)
    (input : Hom x y)
    β†’ (kβ€² ∘ z) β‰ˆ (w ∘ k)
    β†’ (reverse ∘ (v ∘ (z ∘ back))) β‰ˆ (z ∘ back)
    β†’ input β‰ˆ (v ∘ (z ∘ back))
    β†’ ((kβ€² ∘ reverse) ∘ input) β‰ˆ (w ∘ (k ∘ back))
  normalized-frame-square k kβ€² back v reverse z w input natural cancel normalize =
    trans (assoc w k back)
      (trans (congr natural (refl back))
      (trans (sym (assoc kβ€² z back))
      (trans (congr (refl kβ€²) cancel)
      (trans (assoc kβ€² reverse (v ∘ (z ∘ back)))
        (congr (refl (kβ€² ∘ reverse)) normalize)))))
```