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

--------------------------------------------------------------------------------
-- Theorem 5.15, with all five nodes and all five cyclic implications.
--
--   (1) ๐“ค is univalent;
--   (2) every full 2LTT cofibration has contractible Glue data;
--   (3) the boundary cofibration iโˆ‚ has contractible Glue data;
--   (4) iโˆ‚ has a strictly coherent Glue structure;
--   (5) (I, 0, 1, ๐“ค) has a weak Glue structure.
--
-- The implications use an explicit witness of node (1).  No ambient
-- univalence constant is used.  Node (2) is the universe-polymorphic scheme
-- `AllStrongGlue`, which lives in SSetฯ‰ because it quantifies over levels.
-- `ExactPathInterval` packages the theorem for the full PathInterval record.
-- The FibrantInterval and GeneralInterval modules retain useful auxiliary
-- variants, including fixed-level versions of node (2).
--------------------------------------------------------------------------------

module Extension.GlueSandwichFull where

open import Extension.Prelude
open import Extension.CofibFibration using (is-cofib)
open import Extension.RelFunext using (satisfies-rel-funext ; is-cofib-2LTT)
open import Extension.Univalence using (Univalence)
open import Extension.GlueStructure
  using (has-strictly-coherent-glue ; has-homotopy-coherent-glue ;
         strict-coh-to-homotopy-coh)
open import Extension.GlueDataFibrant
  using (has-strong-glue-structure ; strong-to-strict)
open import Extension.GlueRules using (WeakGlue)
open import Extension.GlueSandwich using (ua-strong-glue)
open import Extension.GlueStrengthChain3 using (strength-chain-3)
open import Extension.WeakGlueUAFibrant using (fibrant-weak-glue-implies-ua)
open import Extension.CoercionLaws
  using (PathStructure ; general-weak-glue-implies-ua)
import Extension.Conservativity as PathIntervalDef
open import Extension.GlueSandwichComplete using () renaming (iโˆ‚ to iโˆ‚-fib)
open import Extension.GlueSandwichGeneral using () renaming (iโˆ‚ to iโˆ‚-path)

private
  variable
    โ„“I ๐“ค' : Level

--------------------------------------------------------------------------------
-- The paper's implication (1) to (2) in full generality, at arbitrary shape
-- levels.  Its univalence witness and full cofibration witness are explicit.
-- The result cannot be placed in a fixed UUแต‰ because it quantifies over Level.
--------------------------------------------------------------------------------

strong-glue-any-cofibration :
    {โ„“ฮฆ โ„“ฮจ : Level} (๐“ค : Level) {ฮฆ : UUแต‰ โ„“ฮฆ} {ฮจ : UUแต‰ โ„“ฮจ} {i : ฮฆ โ†’ ฮจ}
    (U : Univalence ๐“ค)
    (c2 : is-cofib-2LTT i (lsuc ๐“ค))
  โ†’ has-strong-glue-structure i ๐“ค (pr1แต‰ c2)
strong-glue-any-cofibration ๐“ค U c2 = ua-strong-glue U (pr1แต‰ c2) (pr2แต‰ c2)

-- The paper's second node is a universe-polymorphic scheme.  It therefore
-- lives in the large outer universe rather than in one fixed UUแต‰ level.
AllStrongGlue : (๐“ค : Level) โ†’ SSetฯ‰
AllStrongGlue ๐“ค =
  {โ„“ฮฆ โ„“ฮจ : Level} {ฮฆ : UUแต‰ โ„“ฮฆ} {ฮจ : UUแต‰ โ„“ฮจ} {i : ฮฆ โ†’ ฮจ} โ†’
  (c2 : is-cofib-2LTT i (lsuc ๐“ค)) โ†’
  has-strong-glue-structure i ๐“ค (pr1แต‰ c2)

--------------------------------------------------------------------------------
-- Variant A: fibrant interval  I = C ๐•€,  0 = c aโ‚€,  1 = c aโ‚,  with a segment
-- seg : aโ‚€ = aโ‚  (the hypotheses of `GlueSandwichComplete`).
--------------------------------------------------------------------------------

module FibrantInterval
         (๐•€ : UU โ„“I) (aโ‚€ aโ‚ : ๐•€) (๐“ค : Level) (seg : Id aโ‚€ aโ‚)
         (cof  : is-cofib (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) (lsuc ๐“ค))
         (triv : satisfies-rel-funext cof)
       where

  -- (1)  ๐“ค is univalent.
  Cond1 : UU (lsuc ๐“ค)
  Cond1 = (A B : UU ๐“ค) โ†’ isEquiv (idtoeqv {A = A} {B = B})

  -- (2)  every full 2LTT cofibration (between exo-types at the level of iโˆ‚)
  --      carries a strong Glue structure.
  Cond2 : UUแต‰ (lsuc (โ„“I โŠ” lsuc ๐“ค))
  Cond2 = {ฮฆ ฮจ : UUแต‰ โ„“I} (i : ฮฆ โ†’ ฮจ) (c2 : is-cofib-2LTT i (lsuc ๐“ค))
        โ†’ has-strong-glue-structure i ๐“ค (pr1แต‰ c2)

  -- (3)  (iโˆ‚, ๐“ค) has a strong Glue structure.
  Cond3 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค)
  Cond3 = has-strong-glue-structure (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) ๐“ค cof

  -- (4)  the paper's strictly coherent Glue structure for (iโˆ‚, ๐“ค).
  Cond4 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค)
  Cond4 = has-strictly-coherent-glue (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) ๐“ค

  -- (5)  weak Glue structure for (I, 0, 1, ๐“ค).
  Cond5 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค)
  Cond5 = WeakGlue (C ๐•€) (c aโ‚€) (c aโ‚) ๐“ค

  -- (1)โ‡’(2): Theorem 5.4.
  1โ‡’2 : Cond1 โ†’ Cond2
  1โ‡’2 U i c2 = ua-strong-glue U (pr1แต‰ c2) (pr2แต‰ c2)

  -- (2)โ‡’(3): instantiate at iโˆ‚, a full 2LTT cofibration by hypothesis.
  2โ‡’3 : Cond2 โ†’ Cond3
  2โ‡’3 s2 = s2 (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) (cof ,แต‰ triv)

  -- (3)โ‡’(4): Lemma 5.13(1)+(2), extract the centre (a strictly
  -- coherent output) and coerce its strict coherence to a path.
  3โ‡’4 : Cond3 โ†’ Cond4
  3โ‡’4 = strong-to-strict (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) ๐“ค cof

  -- (4)โ‡’(5): Lemma 5.13(3).
  4โ‡’5 : Cond4 โ†’ Cond5
  4โ‡’5 scg = strength-chain-3 (C ๐•€) (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) ๐“ค hcg
    where
    hcg : has-homotopy-coherent-glue (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) ๐“ค
    hcg A T e =
      pr1แต‰ gd ,แต‰
      strict-coh-to-homotopy-coh (iโˆ‚-fib ๐•€ aโ‚€ aโ‚) ๐“ค A T e (pr1แต‰ gd) (pr2แต‰ gd)
      where
      gd = scg A T e

  -- (5)โ‡’(1): Theorem 5.14 (fibrant-interval form).
  5โ‡’1 : Cond5 โ†’ Cond1
  5โ‡’1 = fibrant-weak-glue-implies-ua ๐•€ aโ‚€ aโ‚ ๐“ค seg

  -- Theorem 5.15, the complete five-node cycle: all five conditions are
  -- equivalent.
  glue-sandwich-full :
       (Cond1 โ†’ Cond2)
    ร—แต‰ ((Cond2 โ†’ Cond3)
    ร—แต‰ ((Cond3 โ†’ Cond4)
    ร—แต‰ ((Cond4 โ†’ Cond5)
    ร—แต‰  (Cond5 โ†’ Cond1))))
  glue-sandwich-full = 1โ‡’2 ,แต‰ (2โ‡’3 ,แต‰ (3โ‡’4 ,แต‰ (4โ‡’5 ,แต‰ 5โ‡’1)))

  -- Exact paper statement.  Node (2) quantifies over arbitrary universe
  -- levels and hence the package lives in SSetฯ‰.
  record ExactFiveNodeCycle : SSetฯ‰ where
    field
      univalence-to-all : Cond1 โ†’ AllStrongGlue ๐“ค
      all-to-boundary : AllStrongGlue ๐“ค โ†’ Cond3
      boundary-to-structure : Cond3 โ†’ Cond4
      structure-to-weak : Cond4 โ†’ Cond5
      weak-to-univalence : Cond5 โ†’ Cond1

  exact-five-node-cycle : ExactFiveNodeCycle
  exact-five-node-cycle = record
    { univalence-to-all = ฮป U {โ„“ฮฆ} {โ„“ฮจ} {ฮฆ} {ฮจ} {i} c2 โ†’
        strong-glue-any-cofibration ๐“ค {ฮฆ = ฮฆ} {ฮจ = ฮจ} {i = i} U c2
    ; all-to-boundary = ฮป all โ†’
        all (cof ,แต‰ triv)
    ; boundary-to-structure = 3โ‡’4
    ; structure-to-weak = 4โ‡’5
    ; weak-to-univalence = 5โ‡’1
    }

--------------------------------------------------------------------------------
-- Variant B: general (not necessarily fibrant) path interval, presented by a
-- `PathStructure` (the hypotheses of `GlueSandwichGeneral`).
--------------------------------------------------------------------------------

module GeneralInterval
         (P : PathStructure โ„“I ๐“ค')
         (cof  : is-cofib (iโˆ‚-path P) (lsuc ๐“ค'))
         (triv : satisfies-rel-funext cof)
       where

  Cond1 : UU (lsuc ๐“ค')
  Cond1 = (A B : UU ๐“ค') โ†’ isEquiv (idtoeqv {A = A} {B = B})

  Cond2 : UUแต‰ (lsuc (โ„“I โŠ” lsuc ๐“ค'))
  Cond2 = {ฮฆ ฮจ : UUแต‰ โ„“I} (i : ฮฆ โ†’ ฮจ) (c2 : is-cofib-2LTT i (lsuc ๐“ค'))
        โ†’ has-strong-glue-structure i ๐“ค' (pr1แต‰ c2)

  Cond3 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค')
  Cond3 = has-strong-glue-structure (iโˆ‚-path P) ๐“ค' cof

  Cond4 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค')
  Cond4 = has-strictly-coherent-glue (iโˆ‚-path P) ๐“ค'

  Cond5 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค')
  Cond5 = WeakGlue (PathStructure.I P) (PathStructure.0I P) (PathStructure.1I P) ๐“ค'

  1โ‡’2 : Cond1 โ†’ Cond2
  1โ‡’2 U i c2 = ua-strong-glue U (pr1แต‰ c2) (pr2แต‰ c2)

  2โ‡’3 : Cond2 โ†’ Cond3
  2โ‡’3 s2 = s2 (iโˆ‚-path P) (cof ,แต‰ triv)

  3โ‡’4 : Cond3 โ†’ Cond4
  3โ‡’4 = strong-to-strict (iโˆ‚-path P) ๐“ค' cof

  4โ‡’5 : Cond4 โ†’ Cond5
  4โ‡’5 scg = strength-chain-3 (PathStructure.I P) (iโˆ‚-path P) ๐“ค' hcg
    where
    hcg : has-homotopy-coherent-glue (iโˆ‚-path P) ๐“ค'
    hcg A T e =
      pr1แต‰ gd ,แต‰
      strict-coh-to-homotopy-coh (iโˆ‚-path P) ๐“ค' A T e (pr1แต‰ gd) (pr2แต‰ gd)
      where
      gd = scg A T e

  5โ‡’1 : Cond5 โ†’ Cond1
  5โ‡’1 = general-weak-glue-implies-ua P

  glue-sandwich-full :
       (Cond1 โ†’ Cond2)
    ร—แต‰ ((Cond2 โ†’ Cond3)
    ร—แต‰ ((Cond3 โ†’ Cond4)
    ร—แต‰ ((Cond4 โ†’ Cond5)
    ร—แต‰  (Cond5 โ†’ Cond1))))
  glue-sandwich-full = 1โ‡’2 ,แต‰ (2โ‡’3 ,แต‰ (3โ‡’4 ,แต‰ (4โ‡’5 ,แต‰ 5โ‡’1)))

  record ExactFiveNodeCycle : SSetฯ‰ where
    field
      univalence-to-all : Cond1 โ†’ AllStrongGlue ๐“ค'
      all-to-boundary : AllStrongGlue ๐“ค' โ†’ Cond3
      boundary-to-structure : Cond3 โ†’ Cond4
      structure-to-weak : Cond4 โ†’ Cond5
      weak-to-univalence : Cond5 โ†’ Cond1

  exact-five-node-cycle : ExactFiveNodeCycle
  exact-five-node-cycle = record
    { univalence-to-all = ฮป U {โ„“ฮฆ} {โ„“ฮจ} {ฮฆ} {ฮจ} {i} c2 โ†’
        strong-glue-any-cofibration ๐“ค' {ฮฆ = ฮฆ} {ฮจ = ฮจ} {i = i} U c2
    ; all-to-boundary = ฮป all โ†’
        all (cof ,แต‰ triv)
    ; boundary-to-structure = 3โ‡’4
    ; structure-to-weak = 4โ‡’5
    ; weak-to-univalence = 5โ‡’1
    }

--------------------------------------------------------------------------------
-- The exact statement for the paper's PathInterval record.
--------------------------------------------------------------------------------

module ExactPathInterval
    (๐•€ : PathIntervalDef.PathInterval โ„“I (lsuc ๐“ค')) where

  private
    c2 = PathIntervalDef.iโˆ‚-cofib ๐•€
    cof = pr1แต‰ c2
    triv = pr2แต‰ c2
    iโˆ‚ = PathIntervalDef.iโˆ‚ ๐•€
    P = PathIntervalDef.PathInterval-to-PathStructure ๐•€

  Cond1 : UU (lsuc ๐“ค')
  Cond1 = Univalence ๐“ค'

  Cond3 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค')
  Cond3 = has-strong-glue-structure iโˆ‚ ๐“ค' cof

  Cond4 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค')
  Cond4 = has-strictly-coherent-glue iโˆ‚ ๐“ค'

  Cond5 : UUแต‰ (โ„“I โŠ” lsuc ๐“ค')
  Cond5 = WeakGlue (PathIntervalDef.I ๐•€)
    (PathIntervalDef.0I ๐•€) (PathIntervalDef.1I ๐•€) ๐“ค'

  3โ‡’4 : Cond3 โ†’ Cond4
  3โ‡’4 = strong-to-strict iโˆ‚ ๐“ค' cof

  4โ‡’5 : Cond4 โ†’ Cond5
  4โ‡’5 scg = strength-chain-3 (PathIntervalDef.I ๐•€) iโˆ‚ ๐“ค' hcg
    where
    hcg : has-homotopy-coherent-glue iโˆ‚ ๐“ค'
    hcg A T e =
      pr1แต‰ gd ,แต‰
      strict-coh-to-homotopy-coh iโˆ‚ ๐“ค' A T e (pr1แต‰ gd) (pr2แต‰ gd)
      where
      gd = scg A T e

  5โ‡’1 : Cond5 โ†’ Cond1
  5โ‡’1 = general-weak-glue-implies-ua P

  record ExactFiveNodeCycle : SSetฯ‰ where
    field
      univalence-to-all : Cond1 โ†’ AllStrongGlue ๐“ค'
      all-to-boundary : AllStrongGlue ๐“ค' โ†’ Cond3
      boundary-to-structure : Cond3 โ†’ Cond4
      structure-to-weak : Cond4 โ†’ Cond5
      weak-to-univalence : Cond5 โ†’ Cond1

  exact-five-node-cycle : ExactFiveNodeCycle
  exact-five-node-cycle = record
    { univalence-to-all = ฮป U {โ„“ฮฆ} {โ„“ฮจ} {ฮฆ} {ฮจ} {i} c2' โ†’
        strong-glue-any-cofibration ๐“ค' {ฮฆ = ฮฆ} {ฮจ = ฮจ} {i = i} U c2'
    ; all-to-boundary = ฮป all โ†’ all c2
    ; boundary-to-structure = 3โ‡’4
    ; structure-to-weak = 4โ‡’5
    ; weak-to-univalence = 5โ‡’1
    }