{-# OPTIONS --without-K --cubical-compatible #-}
open import common
open import palg
open import inductive-repair.indspec
open import inductive-repair.config
open import inductive-repair.wtype-config
module inductive-repair.palg-config {l : Level} (A : Type l) (B : A → Type l) where
record AlgConfig (C : Type l) (D : Type l) : Type (lsuc l) where
field
initAlgC : InitAlgOn A B C
initAlgD : InitAlgOn A B D
module indAlgToInit {C : Type l} (indAlg : WTypeIndAlg {l} A B C) where
open IndAlg indAlg
sup : P A B C → C
sup = λ (a , t) → algebra tt (a , t , tt)
mpα : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → Cases (WTypeSignature {l} A B) C algebra (λ _ → Ycar)
mpα Ycar Ysup = λ c argsFunc ihArgsFunc → Ysup (fst argsFunc , fst ihArgsFunc)
ind-hom : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → (c : C) → Ycar
ind-hom Ycar Ysup = (ind (λ _ → Ycar) (mpα Ycar Ysup))
indIsHomPw : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → (x : P A B C) →
(ind-hom Ycar Ysup ∘ sup) x ≡ (Ysup ∘ P-map (ind-hom Ycar Ysup)) x
indIsHomPw Ycar Ysup x = beta (λ v → Ycar) (mpα Ycar Ysup) tt (fst x , snd x , tt)
indIsHom : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) →
(ind-hom Ycar Ysup ∘ sup) ≡ (Ysup ∘ P-map (ind-hom Ycar Ysup))
indIsHom Ycar Ysup = ext (indIsHomPw Ycar Ysup)
indIsUnqMp : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → ((h , p) : AlgHom (C , sup) (Ycar , Ysup)) →
Cases (WTypeSignature {l} A B) C algebra (λ y → h y ≡ ind-hom Ycar Ysup y)
indIsUnqMp Ycar Ysup (h , p) = (λ c argsFunc@(args , (rec , tt)) (ihArgsFunc , tt) →
happly p (args , rec)
• ap Ysup (Σ-≡-intro refl (ext ihArgsFunc))
• ! (indIsHomPw Ycar Ysup (args , rec))
)
indIsUnqMotive : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → (h : C → Ycar) → (y : C) → Type l
indIsUnqMotive Ycar Ysup h = (λ y → h y ≡ ind-hom Ycar Ysup y)
indIsUnqHomPw : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → ((h , p) : AlgHom (C , sup) (Ycar , Ysup)) →
(x : C) → h x ≡ ind-hom Ycar Ysup x
indIsUnqHomPw (Ycar) (Ysup) hom@(h , p) =
(λ x → ind (indIsUnqMotive Ycar Ysup h) (indIsUnqMp Ycar Ysup hom) x)
indIsUnqHom : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → ((h , p) :
AlgHom (C , sup) (Ycar , Ysup)) → h ≡ ind-hom Ycar Ysup
indIsUnqHom Ycar Ysup hom = ext (indIsUnqHomPw Ycar Ysup hom)
indHomPathUnq : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) → ((h , p) : AlgHom (C , sup) (Ycar , Ysup)) →
(tpt (λ f → isAlgHom (C , sup) (Ycar , Ysup) f) (indIsUnqHom Ycar Ysup ((h , p))) p) ≡ indIsHom Ycar Ysup
indHomPathUnq Ycar Ysup hom@(h , p) =
tpt-path (λ f → f ∘ sup) (λ f → Ysup ∘ P-map f) (indIsUnqHom Ycar Ysup (h , p)) p
• happly-inj (ext λ (x : P A B C) →
let leftPath = ! (ap (λ f → f ∘ sup) (indIsUnqHom Ycar Ysup (h , p)))
rightPath = ap (λ f → Ysup ∘ P-map f) (indIsUnqHom Ycar Ysup (h , p))
ihArgsFunc = mkIHs
(WTypeArity {l} A B tt) (ind (indIsUnqMotive Ycar Ysup h)
(indIsUnqMp Ycar Ysup (h , p)))
(fst x , snd x , tt {l})
in
happly-• leftPath (p • rightPath) x
• ap (λ z → happly leftPath x • z) ((happly-• p rightPath) x)
• ap (λ q → q • happly p x • happly rightPath x) (
happly-! (ap (λ f → f ∘ sup) (indIsUnqHom Ycar Ysup (h , p))) x
• ap ! (
happly-precomp {l} {l} (indIsUnqHom Ycar Ysup (h , p)) sup x
• happlyExt (indIsUnqHomPw Ycar Ysup hom) (sup x)
• beta (indIsUnqMotive Ycar Ysup h) (indIsUnqMp Ycar Ysup hom) tt (fst x , (snd x , tt))
)
•
!-comp (happly p x) (ap Ysup (Σ-≡-intro refl (ext (fst ihArgsFunc)))
• ! (indIsHomPw Ycar Ysup x))
• ap (λ z → z • ! (happly p x))
(!-comp (ap Ysup (Σ-≡-intro refl (ext (fst ihArgsFunc)))) (! (indIsHomPw Ycar Ysup x)))
)
• •assoc {p = (! (! (indIsHomPw Ycar Ysup x))
• ! (ap Ysup (Σ-≡-intro refl (ext (fst ihArgsFunc)))))}
{q = ! (happly p x)} {r = happly p x • happly rightPath x}
• ap (λ z → (! (! (indIsHomPw Ycar Ysup x))
• ! (ap Ysup (Σ-≡-intro refl (ext (fst ihArgsFunc))))) • z)
(! (•assoc {p = ! (happly p x)} {q = happly p x} {r = happly rightPath x})
• ap (λ z → z • happly rightPath x) •invl
• •unitl)
• ap (λ z → (! (! (indIsHomPw Ycar Ysup x)) • ! (ap Ysup
(Σ-≡-intro refl (ext (fst ihArgsFunc))))) • z)
( happly-ap-double-∘ {f = Ysup} {g = P-map} (ext (indIsUnqHomPw Ycar Ysup (h , p))) x)
• ! (ap (λ p → (! (! (indIsHomPw Ycar Ysup _))
• ! (ap Ysup (Σ-≡-intro refl (ext (fst ihArgsFunc))))) • p)
(ap (ap Ysup) (P-resp-is-ap-P-map (indIsUnqHomPw Ycar Ysup hom) x)))
• •assoc • ap (λ p → (! (! (indIsHomPw Ycar Ysup _)) • p)) •invl • •unitr
• !! (indIsHomPw Ycar Ysup _) • ! (happlyExt (indIsHomPw Ycar Ysup) _)
)
wIsInit-center : (Ycar : Type l) (Ysup : P A B Ycar → Ycar) →
AlgHom (C , sup) (Ycar , Ysup)
wIsInit-center Ycar Ysup =
((λ x → ind-hom Ycar Ysup x) , indIsHom Ycar Ysup)
abstract
wIsInit-uniq : (Ycar : Type l) (Ysup : P A B Ycar → Ycar)
(hom : AlgHom (C , sup) (Ycar , Ysup)) →
hom ≡ wIsInit-center Ycar Ysup
wIsInit-uniq Ycar Ysup (h , p) =
Σ-≡-intro (indIsUnqHom Ycar Ysup (h , p))
(indHomPathUnq Ycar Ysup (h , p))
wIsInit-snd-eq : (Ycar : Type l) (Ysup : P A B Ycar → Ycar)
(h : C → Ycar) (p : isAlgHom (C , sup) (Ycar , Ysup) h) →
wIsInit-uniq Ycar Ysup (h , p)
≡ Σ-≡-intro (indIsUnqHom Ycar Ysup (h , p))
(indHomPathUnq Ycar Ysup (h , p))
wIsInit-snd-eq Ycar Ysup h p = refl
wIsInit : isInitAlg (C , sup)
wIsInit (Ycar , Ysup) .fst = wIsInit-center Ycar Ysup
wIsInit (Ycar , Ysup) .snd hom = wIsInit-uniq Ycar Ysup hom
wIndAlgToInit : InitAlgOn A B C
wIndAlgToInit .InitAlgOn.sup = sup
wIndAlgToInit .InitAlgOn.isInit = wIsInit
wToAlg : {C D : Type l} → WTypeConfig {l} A B C D → AlgConfig C D
wToAlg config .AlgConfig.initAlgC = indAlgToInit.wIndAlgToInit (config .Config.indAlgC)
wToAlg config .AlgConfig.initAlgD = indAlgToInit.wIndAlgToInit (config .Config.indAlgD)
algConfigToAlgEquiv : {C D : Type l} (cfg : AlgConfig C D) →
AlgEquiv (C , InitAlgOn.sup (AlgConfig.initAlgC cfg))
(D , InitAlgOn.sup (AlgConfig.initAlgD cfg))
algConfigToAlgEquiv {C} {D} cfg =
fwd , ((bwd , left) , (bwd , right))
where
open AlgConfig cfg
palgC : P-Alg {ℓ₃ = l} A B
palgC = (C , InitAlgOn.sup initAlgC)
palgD : P-Alg {ℓ₃ = l} A B
palgD = (D , InitAlgOn.sup initAlgD)
isInitC = InitAlgOn.isInit initAlgC
isInitD = InitAlgOn.isInit initAlgD
fwd : AlgHom palgC palgD
fwd = isInitC palgD .fst
bwd : AlgHom palgD palgC
bwd = isInitD palgC .fst
left : AlgHom-∘ palgC palgD palgC fwd bwd ≡ id-hom palgC
left = homEqInitId palgC isInitC (AlgHom-∘ palgC palgD palgC fwd bwd)
right : AlgHom-∘ palgD palgC palgD bwd fwd ≡ id-hom palgD
right = homEqInitId palgD isInitD (AlgHom-∘ palgD palgC palgD bwd fwd)
foldSwap : {C D : Type l} (cfg : AlgConfig C D) (Y : P-Alg {ℓ₃ = l} A B) →
fst (fst (InitAlgOn.isInit (AlgConfig.initAlgC cfg) Y))
≡ fst (fst (InitAlgOn.isInit (AlgConfig.initAlgD cfg) Y))
∘ fst (fst (algConfigToAlgEquiv cfg))
foldSwap {C} {D} cfg Y =
! (ap fst (isInitC Y .snd composite))
where
open AlgConfig cfg
palgC = (C , InitAlgOn.sup initAlgC)
palgD = (D , InitAlgOn.sup initAlgD)
isInitC = InitAlgOn.isInit initAlgC
isInitD = InitAlgOn.isInit initAlgD
composite : AlgHom palgC Y
composite = AlgHom-∘ palgC palgD Y (fst (algConfigToAlgEquiv cfg))
(fst (isInitD Y))
module initToHalf {C : Type l} (initAlg : InitAlgOn A B C) where
open InitAlgOn initAlg
algebra : ConstrAlgebra (WTypeSignature {l} A B) C
algebra c (nonrec , rec , tt) = sup (nonrec , rec)
module ind (motive : C → Type l) (mp : Cases (WTypeSignature {l} A B) C algebra motive) where
assocAlg : P-Alg A B
assocAlg .fst = Σ C (λ c → motive c)
assocAlg .snd args .fst = sup (P-map (λ z → fst z) args)
assocAlg .snd (nonrec , rec) .snd = mp tt (nonrec , (fst ∘ rec , tt))
((λ x → snd (rec x)) , tt)
initMap : AlgHom (C , sup) assocAlg
initMap = isInit assocAlg .fst
projMap : AlgHom assocAlg (C , sup)
projMap .fst = fst
projMap .snd = refl
compMap : AlgHom (C , sup) (C , sup)
compMap = AlgHom-∘ (C , sup) assocAlg (C , sup) initMap projMap
compMapEqId : compMap ≡ id-hom (C , sup)
compMapEqId = homEqInitId (C , sup) isInit compMap
initMapSection : (x : C) → (fst initMap) x .fst ≡ x
initMapSection = happly (ap fst compMapEqId)
private
ind-body : Induction (WTypeSignature {l} A B) algebra
ind-body motive mp a =
tpt (λ z → motive z) (initMapSection a) (snd ((fst initMap) a))
where open ind motive mp
beta-body : BetaLaw (WTypeSignature {l} A B) ind-body
beta-body motive mp tt (a , t) =
ap (λ q → tpt (λ z → motive z) q (snd (initMapFn (sup args)))) (! compMap_eq_initMapSection)
• tpt-• motive (ap (λ r → fst r) (initMapPathArgs args)) (ap (sup ∘ (λ x → a , (λ z → x (t .fst z)))) compMapEqIdFn) (snd (initMapFn (sup args)))
• ap (λ z → tpt motive (ap (sup ∘ (λ x → a , (λ z → x (t .fst z)))) compMapEqIdFn) z) (tpt-ap motive fst (initMapPathArgs args) (snd (initMapFn (sup args))) • initMapToMP)
• tpt-ap motive (sup ∘ (λ x → a , (λ z → x (t .fst z)))) compMapEqIdFn (mp tt (a , ((λ r → fst r) ∘ (initMapFn ∘ t .fst)) , tt) ((λ x → (initMapFn ∘ t .fst) x .snd) , tt))
• ap (λ q → tpt (λ g → motive (sup (a , g ∘ t .fst))) q
(mp tt (a , ((λ r → fst r) ∘ (initMapFn ∘ t .fst) , tt))
((λ x → (initMapFn ∘ t .fst) x .snd) , tt)))
(! (Σ-≡-intro-fst compMapEqIdFn tpt_compMap_refl))
• tpt-ap (λ g → motive (sup (a , g ∘ t .fst))) fst
(Σ-≡-intro compMapEqIdFn tpt_compMap_refl)
(mp tt (a , ((λ r → fst r) ∘ (initMapFn ∘ t .fst) , tt)) ((λ x → (initMapFn ∘ t .fst) x .snd) , tt))
• apd (λ p → motive (sup (a , fst p ∘ t .fst))) apd_abstraction (Σ-≡-intro compMapEqIdFn tpt_compMap_refl)
where
open ind motive mp
supΣ = assocAlg .snd
initMapFn : C → Σ C (motive)
initMapFn = initMap .fst
compMapEqIdFn = ap fst compMapEqId
initMapPath : isAlgHom (C , sup) assocAlg (initMap .fst)
initMapPath = initMap .snd
args : P A B C
args = (a , t .fst)
initMapPathArgs : (args : P A B C) → (initMapFn ∘ sup) args ≡ (supΣ ∘ P-map (initMapFn)) args
initMapPathArgs args = happly initMapPath args
initMapToMP : tpt (λ z → motive (z .fst)) (initMapPathArgs args) (snd ((fst initMap ∘ sup) args))
≡ mp tt (a , ((fst ∘ (initMapFn ∘ t .fst) , tt))) ((λ x → (initMapFn ∘ t .fst) x . snd) , tt)
initMapToMP = apd (λ z → motive (z .fst)) snd (initMapPathArgs args)
apd_abstraction : (p : Σ (C → C) (λ g → fst ∘ initMapFn ≡ g)) → motive (sup (a , p .fst ∘ t .fst))
apd_abstraction (g , q) = mp tt (a , (g ∘ t .fst , tt))
((λ x → tpt motive (happly q (t .fst x)) (snd (initMapFn (t .fst x)))) , tt)
tpt-right-path-refl : ∀ {A : Type l} {a b : A} (q : a ≡ b) → tpt (λ g → a ≡ g) q refl ≡ q
tpt-right-path-refl refl = refl
Σ-≡-intro-fst : ∀ {A : Type l} {B : A → Type l} {a a' : A} {b : B a} {b' : B a'}
(p1 : a ≡ a') (p2 : tpt B p1 b ≡ b') → ap fst (Σ-≡-intro p1 p2) ≡ p1
Σ-≡-intro-fst refl refl = refl
tpt_compMap_refl : tpt (λ g → fst ∘ initMapFn ≡ g) compMapEqIdFn refl ≡ compMapEqIdFn
tpt_compMap_refl = tpt-right-path-refl compMapEqIdFn
compMap_eq_initMapSection : ap fst (initMapPathArgs args) • ap (sup ∘ λ x → a , λ z → x (t .fst z )) compMapEqIdFn ≡ initMapSection (sup args)
compMap_eq_initMapSection = ap (λ z → z • ap (sup ∘ (λ x → a , (λ z → x (t .fst z)))) compMapEqIdFn) (
! (ap (λ z → happly ((ap (_∘_ (λ r → fst r)) (isInit assocAlg .fst .snd)) • z) args) •unitl
• happly-• _ _ args
• ap (λ z → z • happly (ap (_∘_ sup) (ext _)) args) (
happly-ap-double-∘ {f = fst} {g = λ x → x} (initMap .snd) args
)
• ap (λ z → ap (λ r → fst r) (ap (λ z → z args) (initMap .snd)) • z) (
happly-ap-double-∘ {f = sup} {g = λ x → x} (ext (λ v → refl)) args
• ap (ap sup) (! (happly-ap (ext (λ v → refl)) args)
• happlyExt (λ v → refl) args)
)
• •unitr
• ap (λ z → ap (λ r → fst r) z) (! (happly-ap (isInit assocAlg .fst .snd) args)))
)
• ap (λ z → happly (compMap .snd) args • z) (
! (happly-ap-double-∘ {f = sup} {g = P-map ∘ fst} compMapEqId args
• ! (ap-∘ {f = sup} {g = (λ z → (P-map ∘ (λ r → fst r)) z args)} compMapEqId)
• ap-∘ compMapEqId)
)
• ! (happly-• (compMap .snd) (ap (λ f → sup ∘ P-map (f .fst)) compMapEqId) args)
• ap (λ z → happly z args)
(!• (! (tpt-path (λ hom → fst hom ∘ sup) (λ hom → sup ∘ P-map (fst hom)) compMapEqId (compMap .snd))
• apd (λ hom → isAlgHom (C , sup) (C , sup) (fst hom)) snd compMapEqId))
• ap (λ q → happly q args) (ap-∘ compMapEqId)
• happly-precomp {l} {l} (ap fst compMapEqId) sup args
abstract
initAlgToIndAlg-ind : Induction (WTypeSignature {l} A B) algebra
initAlgToIndAlg-ind = ind-body
initAlgToIndAlg-beta : BetaLaw (WTypeSignature {l} A B) initAlgToIndAlg-ind
initAlgToIndAlg-beta = beta-body
initAlgToIndAlg : WTypeIndAlg {l} A B C
initAlgToIndAlg .IndAlg.algebra = algebra
initAlgToIndAlg .IndAlg.ind = initAlgToIndAlg-ind
initAlgToIndAlg .IndAlg.beta = initAlgToIndAlg-beta