{-# OPTIONS --rewriting #-}

open import Agda.Primitive using (Level;lsuc;_⊔_;lzero)
open import Lib
open import Interval
open import Path
open import Equiv
open import Proposition
open import Cofibs
open import Kan
open import Glue

module Glue-Weak where

  Glue-to-fiber :  {l} {α : Set} {{ : Cofib α}} {T : α  Set l} {B : Set l} {f : (u : α)  T u  B}
                     (g : Glue α T B f)
                     (( : α)  HFiber (f ) (unglue g))
  Glue-to-fiber g = (\   (coe (Glue-α _ _ _ _ ) g ) , (\ _  unglue g) , ! (unglue-α g ) , id ) 

  -- repackaging of CCHM final steps
  -- note: this is *almost* rearrangeable into
  --       unglue, glue-to-fiber : Glue α T B f ≃ Σ (x : B). α → HFiber f b
  -- but the equality is strict in the input
  -- and not proved in the output (though it looks like it would be weak)

  glue-weak-better :  {l} {α : Set} {{ : Cofib α}} {T : α  Set l} {B : Set l} {f : (u : α)  T u  B}
                    hasHCom B
                    (β : Set) {{ : Cofib β}}
                    (g : β  (Glue α T B f)) -- already have a partial answer
                    (b : B [ β  (\   unglue (g )) ]) 
                    (t : ( : α)  HFiber (f ) (fst b)
                       [ β  (\   transport (HFiber (f ))
                                               (snd b )
                                               (Glue-to-fiber (g ) )) ])
                    (Glue α T B f) [ β  g ]
  glue-weak-better {α = α} {{  }} {T} {f = f} hcomB β g b t =
      g' , (\   glue-cong _ _ _ _ _ _ (λ= \   ap fst (snd (t ) )  ! (fst-transport-HFiber-base (f ) (snd b ) (Glue-to-fiber (g ) )))
                                         (fst (snd fix) (inr ))  Glueη (g )) where
       fix = hcomB `1 `0
                      (α  β)
                      (\ x  case (\    fst (snd (fst (t ))) x )
                                  (\   unglue (g ))
                                  (\     ap= (fst-snd-transport-HFiber-base (f ) (snd b ) (Glue-to-fiber (g ) ))
                                               ! (ap (\ H  fst (snd H) x) (snd (t ) )))) 
                      (fst b ,  ∨-elim _
                                       (\   snd (snd (snd (fst (t )))) )
                                       (\   snd b )
                                       (\ _ _  uip)  )

       g' = glue _ _ _ _ (\    fst (fst (t )) )
                         (fst fix ,
                          (\   fst (snd fix) (inl )  ! (fst (snd (snd (fst (t )))))))

  -- this version makes the η definitional if you specify up front that you want it
  glue-weak-better-η :  {l} {α : Set} {{ : Cofib α}} {T : α  Set l} {B : Set l} {f : (u : α)  T u  B}
                      (hb : hasHCom B)
                      (g : Glue α T B f)
                      fst (glue-weak-better {α = α} {{}}{T}{B}{f} hb (`0 == `0) (\ _  g) (unglue g , (\ _  id)) (\   (Glue-to-fiber g  , (\ _  id))))
                       == g
  glue-weak-better-η hb g = ! (snd (glue-weak-better hb (`0 == `0) (\ _  g) (unglue g , (\ _  id)) (\   (Glue-to-fiber g  , (\ _  id)))) id)