module Data.Set.Eliminators where
open import Prelude
open import Data.Set.Definition
open import HLevels
data π {p} (A : Type a) (P : π¦ A β Type p) : Type (a ββ p) where
β
: π A P
_β·_β¨_β© : A β (xs : π¦ A) β (Pβ¨xsβ© : P xs) β π A P
private
variable
p : Level
P : π¦ A β Type p
embed : π A P β π¦ A
embed β
= β
embed (x β· xs β¨ Pβ¨xsβ© β©) = x β· xs
Alg : (P : π¦ A β Type p) β Type _
Alg P = (xs : π _ P) β P (embed xs)
record Coherent {A : Type a} {P : π¦ A β Type p} (Ο : Alg P) : Type (a ββ p) where
field
c-trunc : β xs β isSet (P xs)
c-com : β x y xs Pβ¨xsβ© β
Ο (x β· (y β· xs) β¨ Ο (y β· xs β¨ Pβ¨xsβ© β©) β©)
β‘[ i β P (com x y xs i) ]β‘
Ο (y β· (x β· xs) β¨ Ο (x β· xs β¨ Pβ¨xsβ© β©) β© )
c-dup : β x xs Pβ¨xsβ© β
Ο (x β· (x β· xs) β¨ Ο (x β· xs β¨ Pβ¨xsβ© β©) β©)
β‘[ i β P (dup x xs i) ]β‘
Ο (x β· xs β¨ Pβ¨xsβ© β©)
open Coherent public
π¦-foldr : (A β B β B) β B β Alg (const B)
π¦-foldr f b β
= b
π¦-foldr f b (x β· xs β¨ Pβ¨xsβ© β©) = f x Pβ¨xsβ©
Ξ¨ : (π¦ A β Type p) β Type _
Ξ¨ P = Ξ£[ Ο β¦ Alg P ] Γ Coherent Ο
Ξ¨-syntax : (A : Type a) β (π¦ A β Type p) β Type _
Ξ¨-syntax _ = Ξ¨
syntax Ξ¨-syntax A (Ξ» x β e) = Ξ¨[ x β¦ π¦ A ] β¦ e
Ο : Type a β Type b β Type _
Ο A B = Ξ¨ {A = A} (const B)
β¦_β§ : {P : π¦ A β Type p} β Ξ¨ P β (xs : π¦ A) β P xs
β¦ alg β§ β
= alg .fst β
β¦ alg β§ (x β· xs) = alg .fst (x β· xs β¨ β¦ alg β§ xs β©)
β¦ alg β§ (com x y xs i) = alg .snd .c-com x y xs (β¦ alg β§ xs) i
β¦ alg β§ (dup x xs i) = alg .snd .c-dup x xs (β¦ alg β§ xs) i
β¦ alg β§ (trunc xs ys p q i j) =
isOfHLevelβisOfHLevelDep 2
(alg .snd .c-trunc)
(β¦ alg β§ xs) (β¦ alg β§ ys)
(cong β¦ alg β§ p) (cong β¦ alg β§ q)
(trunc xs ys p q)
i j
prop-coh : {A : Type a} {P : π¦ A β Type p} {alg : Alg P} β (β xs β isProp (P xs)) β Coherent alg
prop-coh P-isProp .c-trunc xs = isPropβisSet (P-isProp xs)
prop-coh {P = P} {alg = alg} P-isProp .c-dup x xs Οβ¨xsβ© =
toPathP (P-isProp (x β· xs) (transp (Ξ» i β P (dup x xs i)) i0 (alg (x β· (x β· xs) β¨ alg (x β· xs β¨ Οβ¨xsβ© β©) β©))) (alg (x β· xs β¨ Οβ¨xsβ© β©)))
prop-coh {P = P} {alg = alg} P-isProp .c-com x y xs Οβ¨xsβ© =
toPathP (P-isProp (y β· x β· xs) (transp (Ξ» i β P (com x y xs i)) i0 (alg (x β· (y β· xs) β¨ alg (y β· xs β¨ Οβ¨xsβ© β©) β©))) (alg (y β· (x β· xs) β¨ alg (x β· xs β¨ Οβ¨xsβ© β©) β©)))
infix 4 _β_
record AnEquality (A : Type a) : Type a where
constructor _β_
field lhs rhs : π¦ A
open AnEquality public
EqualityProof-Alg : {B : Type b} (A : Type a) (P : π¦ A β AnEquality B) β Type (a ββ b)
EqualityProof-Alg A P = Alg (Ξ» xs β let Pxs = P xs in lhs Pxs β‘ rhs Pxs)
eq-coh : {A : Type a} {B : Type b} {P : π¦ A β AnEquality B} {alg : EqualityProof-Alg A P} β Coherent alg
eq-coh {P = P} = prop-coh Ξ» xs β let Pxs = P xs in trunc (lhs Pxs) (rhs Pxs)