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

--------------------------------------------------------------------------------
-- Remark 5.9 + Theorem 5.14, fully closed.
--
-- For a *fibrant* interval the path presentation collapses to a single segment
-- seg : 0 = 1, and the coercion laws of Lemma 5.8 become the standard
-- transport facts:
--   coe^G โ‰” transport^G(seg),   path(G) โ‰” ap_G(seg),
--   (c1) idtoeqv(ap_G p) ~ transport^G(p);
--   (c2) fibrewise maps commute with transport;
--   (c3) transport in a constant family is the identity.
-- Discharging the coercion interface of `Extension.WeakGlueUA` with these gives a
-- fully self-contained theorem: a weak Glue structure over a fibrant interval
-- implies univalence.
--
-- We present the fibrant interval as  I โ‰” C ๐•€  for an inner ๐•€ : UU, with points
-- c aโ‚€, c aโ‚ and segment seg : aโ‚€ = aโ‚ (the inner identity of ๐•€).
--------------------------------------------------------------------------------

module Extension.WeakGlueUAFibrant where

open import Extension.Prelude
open import Extension.GlueRules using (WeakGlue)
open import Extension.CoercionLaws using (PathStructure)
open import Extension.WeakGlueUA using (weak-glue-implies-ua)

private
  variable
    โ„“ ๐“ค : Level

-- (c1) idtoeqv of ap is transport.
idtoeqv-ap-tr : {A : UU โ„“} {a b : A} (F : A โ†’ UU ๐“ค) (p : Id a b) (x : F a)
              โ†’ Id (pr1 (idtoeqv (ap F p)) x) (tr F p x)
idtoeqv-ap-tr F refl x = refl

-- (c2) fibrewise maps commute with transport.
tr-nat : {A : UU โ„“} {a b : A} (F H : A โ†’ UU ๐“ค) (ฮธ : (z : A) โ†’ F z โ†’ H z)
         (p : Id a b) (x : F a) โ†’ Id (ฮธ b (tr F p x)) (tr H p (ฮธ a x))
tr-nat F H ฮธ refl x = refl

-- (c3) transport in a constant family is the identity.
tr-const : {A : UU โ„“} {a b : A} (Y : UU ๐“ค) (p : Id a b) (x : Y)
         โ†’ Id (tr (ฮป _ โ†’ Y) p x) x
tr-const Y refl x = refl

-- A fibrant interval carries the standard line-to-identity structure obtained
-- by applying a line to its distinguished segment.  Degeneracy on constant
-- lines is not part of the interface: it is derived from naturality in
-- `Extension.CoercionLaws`.
fibrant-path-structure : {โ„“I : Level} (๐•€ : UU โ„“I) (aโ‚€ aโ‚ : ๐•€)
                         (๐“ค : Level) (seg : Id aโ‚€ aโ‚) โ†’ PathStructure โ„“I ๐“ค
fibrant-path-structure ๐•€ aโ‚€ aโ‚ ๐“ค seg = record
  { I = C ๐•€
  ; 0I = c aโ‚€
  ; 1I = c aโ‚
  ; path = ฮป g โ†’ ap (ฮป z โ†’ ic (g (c z))) seg
  ; path-nat = ฮป h g โ†’ ap-comp h (ฮป z โ†’ ic (g (c z))) seg
  }

module _ {โ„“I : Level} (๐•€ : UU โ„“I) (aโ‚€ aโ‚ : ๐•€) (๐“ค : Level) (seg : Id aโ‚€ aโ‚) where

  private
    I : UUแต‰ โ„“I
    I = C ๐•€
    0I 1I : I
    0I = c aโ‚€
    1I = c aโ‚

    coeF : (G : I โ†’ UU ๐“ค) โ†’ G 0I โ†’ G 1I
    coeF G = tr (ฮป z โ†’ G (c z)) seg

    pathF : (G : I โ†’ UU ๐“ค) โ†’ Id (G 0I) (G 1I)
    pathF G = ap (ฮป z โ†’ G (c z)) seg

    c1F : (G : I โ†’ UU ๐“ค) (x : G 0I) โ†’ Id (coeF G x) (pr1 (idtoeqv (pathF G)) x)
    c1F G x = (idtoeqv-ap-tr (ฮป z โ†’ G (c z)) seg x) โปยน

    c2F : (G H : I โ†’ UU ๐“ค) (ฮธ : (t : I) โ†’ G t โ†’ H t) (x : G 0I)
        โ†’ Id (ฮธ 1I (coeF G x)) (coeF H (ฮธ 0I x))
    c2F G H ฮธ x = tr-nat (ฮป z โ†’ G (c z)) (ฮป z โ†’ H (c z)) (ฮป z โ†’ ฮธ (c z)) seg x

    c3F : (Y : UU ๐“ค) (x : Y) โ†’ Id (coeF (ฮป _ โ†’ Y) x) x
    c3F Y x = tr-const Y seg x

  -- The fully-closed theorem: weak glue over a fibrant interval โ‡’ univalence.
  fibrant-weak-glue-implies-ua : WeakGlue I 0I 1I ๐“ค
                               โ†’ (A B : UU ๐“ค) โ†’ isEquiv (idtoeqv {A = A} {B = B})
  fibrant-weak-glue-implies-ua =
    weak-glue-implies-ua I 0I 1I ๐“ค coeF pathF c1F c2F c3F