{-# OPTIONS --safe #-}
module Cubical.HITs.Wedge.Base where

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.Pointed
open import Cubical.HITs.Pushout.Base
open import Cubical.Data.Sigma
open import Cubical.Data.Unit
open import Cubical.Foundations.GroupoidLaws

_⋁_ :  { ℓ'}  Pointed   Pointed ℓ'  Type (ℓ-max  ℓ')
_⋁_ (A , ptA) (B , ptB) = Pushout {A = Unit} {B = A} {C = B}  _  ptA)  _  ptB)

-- Arbitrary wedges
⋁gen :  { ℓ'} (A : Type ) (B : A  Pointed ℓ')  Type (ℓ-max  ℓ')
⋁gen A B = cofib {A = A} {B = Σ A λ a  fst (B a)}
                   a  a , snd (B a))

-- Pointed versions
_⋁∙ₗ_ :  { ℓ'}  Pointed   Pointed ℓ'  Pointed (ℓ-max  ℓ')
A ⋁∙ₗ B = (A  B) , (inl (snd A))

_⋁∙ᵣ_ :  { ℓ'}  Pointed   Pointed ℓ'  Pointed (ℓ-max  ℓ')
A ⋁∙ᵣ B = (A  B) , (inr (snd B))

⋁gen∙ :  { ℓ'} (A : Type ) (B : A  Pointed ℓ')  Pointed (ℓ-max  ℓ')
⋁gen∙ A B = ⋁gen A B , inl tt

-- Wedge sums of functions
_∨→_ :  { ℓ' ℓ''} {A : Pointed } {B : Pointed ℓ'} {C : Pointed ℓ''}
       (f : A →∙ C) (g : B →∙ C)
       A  B  fst C
(f ∨→ g) (inl x) = fst f x
(f ∨→ g) (inr x) = fst g x
(f ∨→ g) (push a i₁) = (snd f  sym (snd g)) i₁

-- Pointed version
∨→∙ :  { ℓ' ℓ''} {A : Pointed } {B : Pointed ℓ'} {C : Pointed ℓ''}
    (f : A →∙ C) (g : B →∙ C)  ((A ⋁∙ₗ B) →∙ C)
fst (∨→∙ {A = A} f g) = f ∨→ g
snd (∨→∙ {A = A} f g) = snd f

-- Wedge sum of Units is contractible
isContr-Unit⋁Unit : isContr ((Unit , tt)  (Unit , tt))
fst isContr-Unit⋁Unit = inl tt
snd isContr-Unit⋁Unit (inl tt) = refl
snd isContr-Unit⋁Unit (inr tt) = push tt
snd isContr-Unit⋁Unit (push tt i) j = push tt (i  j)

⋁↪ :  { ℓ'} {A : Pointed } {B : Pointed ℓ'}
       A  B  typ A × typ B
⋁↪ {B = B} (inl x) = x , pt B
⋁↪ {A = A} (inr x) = pt A , x
⋁↪ {A = A} {B = B} (push a i) = pt A , pt B

fold⋁ :  {} {A : Pointed }  A  A  typ A
fold⋁ (inl x) = x
fold⋁ (inr x) = x
fold⋁ {A = A} (push a i) = snd A

id∨→∙id :  {} {A : Pointed }  ∨→∙ (idfun∙ A) (idfun∙ A)  (fold⋁ , refl)
id∨→∙id {A = A} =
  ΣPathP ((funExt  { (inl x)  refl
                     ; (inr x)  refl
                     ; (push a i) j  rUnit  _  pt A) (~ j) i}))
                , refl)