{-# OPTIONS --without-K --exact-split --two-level #-}

--------------------------------------------------------------------------------
-- Theorem 5.14: a weak Glue structure for (I,0,1,š“¤), together with the coercion operation and
-- its laws (c1)–(c3) of Lemma 5.8, implies that š“¤ is univalent.
--
-- Following the paper: given f : A ā‰ƒ B and the weak-glue data (G, bā‚€, b₁, u,
-- homotopies), set
--     s(f) ≔ b₀⁻¹ Ā· path(G) Ā· b₁ : A =_š“¤ B .
-- Using the groupoid action of idtoeqv and the coercion laws, the underlying map
-- of idtoeqv(s(f)) is
--     idtoeqv(b₁) ∘ coe^G ∘ idtoeqv(bā‚€)⁻¹
-- and the weak-glue homotopies (4) with naturality (c2) and degeneracy (c3)
-- collapse it to f.  Hence idtoeqv(s(f)) ~ f, so (isEquiv being a proposition)
-- idtoeqv(s(f)) = f; thus s is a section of idtoeqv, and `ua-from-section`
-- (Lemma 5.10) yields univalence.
--
-- The coercion operation `coe` and laws (c1) through (c3) form this module's
-- interface. `Extension.CoercionLaws` derives that interface from a path
-- interval.
--------------------------------------------------------------------------------

module Extension.WeakGlueUA where

open import Extension.Prelude
open import Extension.GlueRules using (WeakGlue)
open import Extension.UAFromSection using (ua-from-section)

private
  variable
    ā„“I : Level

module _ (I : UUᵉ ā„“I) (0I 1I : I) (š“¤ : Level)
         (coe       : (G : I → UU š“¤) → G 0I → G 1I)
         (path-univ : (G : I → UU š“¤) → Id (G 0I) (G 1I))
         (c1 : (G : I → UU š“¤) (x : G 0I) → Id (coe G x) (pr1 (idtoeqv (path-univ G)) x))
         (c2 : (G H : I → UU š“¤) (Īø : (t : I) → G t → H t) (x : G 0I)
             → Id (Īø 1I (coe G x)) (coe H (Īø 0I x)))
         (c3 : (Y : UU š“¤) (x : Y) → Id (coe (Ī» _ → Y) x) x)
       where

  private
    -- groupoid action of idtoeqv on a concatenation (path induction)
    idtoeqv-Ā· : {X Y Z : UU š“¤} (p : Id X Y) (q : Id Y Z) (x : X)
              → Id (pr1 (idtoeqv (p Ā· q)) x) (pr1 (idtoeqv q) (pr1 (idtoeqv p) x))
    idtoeqv-Ā· refl q x = refl

    -- idtoeqv of a path cancels idtoeqv of its inverse (path induction)
    idtoeqv-inv-sec : {X Y : UU š“¤} (p : Id X Y) (x : Y)
                    → Id (pr1 (idtoeqv p) (pr1 (idtoeqv (p ⁻¹)) x)) x
    idtoeqv-inv-sec refl x = refl

  weak-glue-implies-ua : WeakGlue I 0I 1I š“¤
                       → (A B : UU š“¤) → isEquiv (idtoeqv {A = A} {B = B})
  weak-glue-implies-ua wg = ua-from-section secmap sect
    where
    secmap : (A B : UU š“¤) → (A ā‰ƒ B) → Id A B
    secmap A B g = ((ic bā‚€) ⁻¹) Ā· (path-univ G Ā· ic b₁)
      where
      w  = wg A B (c g)
      G  = pr1ᵉ w
      bā‚€ = pr1ᵉ (pr2ᵉ w)
      b₁ = pr1ᵉ (pr2ᵉ (pr2ᵉ w))

    sect : (A B : UU š“¤) (g : A ā‰ƒ B) → Id (idtoeqv (secmap A B g)) g
    sect A B g = dep-pair⁼ _ _ (funext ptwise , pr1 (is-prop-isEquiv (pr1 g) _ _))
      where
      w  = wg A B (c g)
      G  = pr1ᵉ w
      bā‚€ = pr1ᵉ (pr2ᵉ w)
      b₁ = pr1ᵉ (pr2ᵉ (pr2ᵉ w))
      u  = pr1ᵉ (pr2ᵉ (pr2ᵉ (pr2ᵉ w)))
      Hā‚€ = ic (pr1ᵉ (pr2ᵉ (pr2ᵉ (pr2ᵉ (pr2ᵉ w)))))
      H₁ = ic (pr2ᵉ (pr2ᵉ (pr2ᵉ (pr2ᵉ (pr2ᵉ w)))))
      β₀ = ic bā‚€
      β₁ = ic b₁
      P  = path-univ G
      uf : (t : I) → G t → B
      uf t = ic (u t)

      ptwise : (x : A) → Id (pr1 (idtoeqv (secmap A B g)) x) (pr1 g x)
      ptwise x = chain0 Ā· (step1 Ā· (step2 Ā· (step3 Ā· (step4 Ā· step5))))
        where
        y : G 0I
        y = pr1 (idtoeqv (β₀ ⁻¹)) x
        chain0 : Id (pr1 (idtoeqv (secmap A B g)) x) (pr1 (idtoeqv β₁) (coe G y))
        chain0 = idtoeqv-Ā· (β₀ ⁻¹) (P Ā· β₁) x
               Ā· (idtoeqv-Ā· P β₁ y
                 Ā· ap (pr1 (idtoeqv β₁)) ((c1 G y) ⁻¹))
        step1 : Id (pr1 (idtoeqv β₁) (coe G y)) (uf 1I (coe G y))
        step1 = (H₁ (coe G y)) ⁻¹
        step2 : Id (uf 1I (coe G y)) (coe (Ī» _ → B) (uf 0I y))
        step2 = c2 G (Ī» _ → B) uf y
        step3 : Id (coe (Ī» _ → B) (uf 0I y)) (uf 0I y)
        step3 = c3 B (uf 0I y)
        step4 : Id (uf 0I y) (pr1 g (pr1 (idtoeqv β₀) y))
        step4 = Hā‚€ y
        step5 : Id (pr1 g (pr1 (idtoeqv β₀) y)) (pr1 g x)
        step5 = ap (pr1 g) (idtoeqv-inv-sec β₀ x)