{-# OPTIONS --rewriting #-}

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

module Contractible where

  contr-extend-partial : {l : Level} {A : Set l} 
          hasHCom A
          Contractible A
          ContractibleFill A
  contr-extend-partial hcomA cA α t = fst c , (\   fst (snd c)   ! (snd (snd (snd cA (t ))))) where
    c = hcomA `0 `1 α (\ z   fst (snd cA (t )) z) (fst cA ,    fst (snd (snd cA (t )))))

  contr-implies-hprop : {l : Level} {A : Set l} 
                       hasHCom A
                       Contractible A
                       (x y : A)  Path A x y
  contr-implies-hprop hcomA cA x y = (\ z  fst (p z) ) , (! ((snd (p `0)) (inl id))) , (! ((snd (p `1)) (inr id))) where
    p :  z  _
    p z = contr-extend-partial hcomA cA ((z == `0)  (z == `1)) (case01 (\ _  x) (\ _  y)) 

  contr-extend-partial-path : {l : Level} {A : Set l} 
                              (hcomA : hasHCom A)
                              (cA : Contractible A)
                              (α : Set) {{ : Cofib α}} (t : α  A)
                              Path A (fst (contr-extend-partial hcomA cA α t)) (fst cA)
  contr-extend-partial-path hcomA cA α t = contr-implies-hprop hcomA cA _ _

  HProp : {l : Level}  Set l  Set l
  HProp A = (x y : A)  Path A x y

  HSet : {l : Level}  Set l  Set l
  HSet A = (x y : A)  HProp (Path A x y)

  ContractibleFill-hprop : {l : Level} (A : Set l)  (c1 c2 : ContractibleFill A)  Path (ContractibleFill A) c1 c2
  ContractibleFill-hprop A c1 c2 = (\ z  λ α t  fst (c z α t) , (\   snd (c z α t) (inl ))) , (λ= \ α  λ=inf \   λ= \ t  pair= (! (snd (c `0 α {{}} t) (inr (inl id))) ) (λ= \ _  uip)) , (λ= \ α  λ=inf \   λ= \ t  pair= (! (snd (c `1 α {{}} t) (inr (inr id))) ) (λ= \ _  uip)) where
    c :  z α {{ : Cofib α}} t  _
    c z α t = c1 (α  ((z == `0)  (z == `1)))
              (case (\   t )
                    (case01 (\ z=0  fst (c1 α t)) (\ z=1  fst (c2 α t)))
                    (\   ∨-elim01 _
                            (\ z=0  snd (c1 α t) )
                            ((\ z=1  snd (c2 α t) ))))

  inhabited-hprop-contractible : {l : Level} (A : Set l) 
                                hasHCom A
                                A
                                ((x y : A)  Path A x y)
                                ContractibleFill A
  inhabited-hprop-contractible A hcomA a p = contr-extend-partial hcomA (a , (\ b  p a b))


  hcomContractibleFill : {l : Level} (A : Set l)  hasHCom (ContractibleFill A)
  hcomContractibleFill A r r' α t b =  β s  fst (c β s) , (\   snd (c β s) (inl (inl )))) , ((\   λ= (\ β  λ=inf \   λ= \ s  pair= (snd (c β {{}} s) (inl (inr ))) (λ= \ _  uip)))) , (\ r=r'  λ= (\ β  λ=inf \   λ= \ s  pair= (snd (c β {{}} s) (inr r=r')) (λ= \ _  uip))) where
    c :  β {{ : Cofib β}} s  _
    c β s = (fst b ((β  α)  (r == r'))
                 (∨-elim _ (case s (\   fst ((t r' ) β s)) (\    snd ((t r' ) β s) ))
                 (\ r=r'  fst (fst b β s))
                 (∨-elim _ (\  r=r'  snd (fst b β s)   ap= (transport-constant trunc)) (\  r=r'  ((ap (\ h  fst (h β s))) (snd b )  ap (\ h  fst (t h  β s)) (! r=r'))  ap= (transport-constant trunc) ) (\ _ _  λ= \ _  uip))))