--------------------------------------------------------------------------------
Ettore Aldrovandi, ealdrovandi@fsu.edu

Started: June 2023
--------------------------------------------------------------------------------

Port of [HoTT-Agda](https://github.com/HoTT/HoTT-Agda) `PathSeq`
library to TypeTopology.

\begin{code}

{-# OPTIONS --without-K --safe #-}

open import MLTT.Spartan
open import UF.Base
open import PathSequences.Type
open import PathSequences.Reasoning

module PathSequences.Ap where

\end{code}

ap-seq is the extension of ap to path sequences.

\begin{code}

module _ {X : 𝓀 Μ‡ } {Y : π“₯ Μ‡ } (f : X β†’ Y) where

 ap-seq : {x x' : X} β†’ x ≑ x' β†’ f x ≑ f x'
 ap-seq [] = []
 ap-seq (p β—ƒβˆ™ s) = ap f p β—ƒβˆ™ ap-seq s

\end{code}

≑-to-= can be interpreted as a transformation between ap and ap-seq. In
other words, with f : X β†’ Y, we have a commutative diagram (pointwise
on PathSeq x x', of course).

          ap-seq f
x ≑ x' ----------> f x ≑ f x'
  |                    |
  | [ _↓]              | [_ ↓]
  ↓                    ↓
x = x' ---------> f x = f x'
          ap f

\begin{code}
 ≑-=-nat : {x x' : X} (s : x ≑ x')
          β†’ ap f [ s ↓] = [ (ap-seq s) ↓]
 ≑-=-nat [] = refl
 ≑-=-nat (p β—ƒβˆ™ s) =
      ap f (p βˆ™ [ s ↓])        =⟨ ap-βˆ™ f p ([ s ↓]) ⟩
      ap f p βˆ™ ap f [ s ↓]     =⟨ ap (Ξ» v β†’ ap f p βˆ™ v ) (≑-=-nat s) ⟩
      ap f p βˆ™ [ (ap-seq s) ↓] ∎

\end{code}

The original uses the following namesβ€”it is not clear why.

\begin{code}

 ap-seq-βˆ™-= = ≑-=-nat

\end{code}


On the other hand, we have

          ap-seq f
x ≑ x' ----------> f x ≑ f x'
  |                    ↑
  | [ _↓]              | _β—ƒβˆŽ
  ↓                    |
x = x' ---------> f x = f x'
          ap f

in two ways:

\begin{code}

 ap-seq-βˆ™ : {x x' : X} (s : x ≑ x')
          β†’ ap f [ s ↓] β—ƒβˆŽ =ₛ ap-seq s
 ap-seq-βˆ™ s = =ₛ-in (ap-seq-βˆ™-= s)

 βˆ™-ap-seq : {x x' : X} (s : x ≑ x')
          β†’ ap-seq s =ₛ ap f [ s ↓] β—ƒβˆŽ
 βˆ™-ap-seq s = (ap-seq-βˆ™ s) ⁻¹ₛ

\end{code}

from which we can prove that ap-seq preserves =ₛ between path
sequences:

\begin{code}

 ap-seq-=ₛ : {x x' : X} {s t : x ≑ x'}
           β†’ s =ₛ t
           β†’ ap-seq s =ₛ ap-seq t
 ap-seq-=ₛ {s = s} {t} (=ₛ-in p) = ap-seq s        οΌβ‚›βŸ¨ βˆ™-ap-seq s ⟩
                                    ap f [ s ↓] β—ƒβˆŽ οΌβ†“βŸ¨ ap (ap f) p ⟩
                                    ap f [ t ↓] β—ƒβˆŽ οΌβ‚›βŸ¨ ap-seq-βˆ™ t ⟩
                                    ap-seq t βˆŽβ‚›

\end{code}

Two-variable version of the above

\begin{code}

module _ {X : 𝓀 Μ‡ } {Y : π“₯ Μ‡ } {Z : 𝓦 Μ‡ } (f : X β†’ Y β†’ Z) where

 apβ‚‚-seq : {x x' : X} {y y' : Y}
         β†’ x ≑ x' β†’  y ≑ y'
         β†’ f x y ≑ f x' y'
 apβ‚‚-seq [] [] = []
 apβ‚‚-seq {x} [] (p β—ƒβˆ™ t) = ap-seq (f x) (p β—ƒβˆ™ t)
 apβ‚‚-seq {y = y} (p β—ƒβˆ™ s) [] = ap-seq (Ξ» x β†’ f x y) (p β—ƒβˆ™ s)
 apβ‚‚-seq (p β—ƒβˆ™ s) (q β—ƒβˆ™ t) = apβ‚‚ f p q β—ƒβˆ™ apβ‚‚-seq s t

\end{code}

Once again, [_↓] acts as a natural transformation as

                 apβ‚‚-seq f
x ≑ x' Γ— y ≑ y' ----------> f x y ≑ f x' y'
       |                          |
       | [ _↓]                    | [_ ↓]
       ↓                          ↓
x = x' Γ— y = y' ---------> f x y = f x' y'
                  apβ‚‚ f

and then as a lift

\begin{code}

 ≑-=-natβ‚‚ : {x x' : X} {y y' : Y}
           β†’ (s : x ≑ x') (t : y ≑ y')
           β†’ apβ‚‚ f [ s ↓] [ t ↓] = [ apβ‚‚-seq s t ↓]
 ≑-=-natβ‚‚ [] [] = refl
 ≑-=-natβ‚‚ {x} [] (p β—ƒβˆ™ t) =
    apβ‚‚ f refl (p βˆ™ [ t ↓])              =⟨ apβ‚‚-refl-left f _ ⟩
    ap (f x) (p βˆ™ [ t ↓])                =⟨ ap-βˆ™ (f x) p ([ t ↓]) ⟩
    ap (f x) p βˆ™ (ap (f x) [ t ↓])
               =⟨ ap (Ξ» v β†’ (ap (f x) p) βˆ™ v) (ap-seq-βˆ™-= (f x) t) ⟩
    ap (f x) p βˆ™ [ ap-seq (f x) t ↓] ∎
 ≑-=-natβ‚‚ {y = y} (p β—ƒβˆ™ s) [] =
    apβ‚‚ f (p βˆ™ [ s ↓]) refl  =⟨ apβ‚‚-refl-right f _ ⟩
    ap fβ‚‚ (p βˆ™ [ s ↓])       =⟨ ap-βˆ™ fβ‚‚ p ([ s ↓]) ⟩
    ap fβ‚‚ p βˆ™ ap fβ‚‚ [ s ↓]   =⟨ ap (Ξ» v β†’ ap fβ‚‚ p βˆ™ v) (ap-seq-βˆ™-= fβ‚‚ s) ⟩
    ap fβ‚‚ p βˆ™ [ (ap-seq fβ‚‚ s) ↓] ∎
      where
        fβ‚‚ = Ξ» v β†’ f v y
 ≑-=-natβ‚‚ (p β—ƒβˆ™ s) (q β—ƒβˆ™ t) =
    apβ‚‚ f (p βˆ™ [ s ↓]) (q βˆ™ [ t ↓]) =⟨ apβ‚‚-βˆ™ f p ([ s ↓]) q ([ t ↓]) ⟩
    apβ‚‚ f p q βˆ™ apβ‚‚ f [ s ↓] [ t ↓] =⟨ ap (Ξ» v β†’ apβ‚‚ f p q βˆ™ v) (≑-=-natβ‚‚ s t) ⟩
    apβ‚‚ f p q βˆ™ [ (apβ‚‚-seq s t) ↓] ∎

 apβ‚‚-seq-βˆ™-= = ≑-=-natβ‚‚

 apβ‚‚-seq-βˆ™ : {x x' : X} {y y' : Y}
           β†’ (s : x ≑ x') (t : y ≑ y')
           β†’ apβ‚‚ f [ s ↓] [ t ↓] β—ƒβˆŽ =ₛ apβ‚‚-seq s t
 apβ‚‚-seq-βˆ™ s t = =ₛ-in (apβ‚‚-seq-βˆ™-= s t)

 βˆ™-apβ‚‚-seq : {x x' : X} {y y' : Y}
           β†’ (s : x ≑ x') (t : y ≑ y')
           β†’ apβ‚‚-seq s t =ₛ apβ‚‚ f [ s ↓] [ t ↓] β—ƒβˆŽ
 βˆ™-apβ‚‚-seq s t = (apβ‚‚-seq-βˆ™ s t) ⁻¹ₛ

\end{code}