module Data.List.Quantifiers where
Quantifiers over listsπ
data Some {β β'} {A : Type β} (P : A β Type β') : List A β Type (β β β') where here : β {x xs} β P x β Some P (x β· xs) there : β {x xs} β Some P xs β Some P (x β· xs) data All {β β'} {A : Type β} (P : A β Type β') : List A β Type (β β β') where [] : All P [] _β·_ : β {x xs} β P x β All P xs β All P (x β· xs)
Some-elim : β {P : A β Type β} (R : (xs : List A) β Some P xs β Type β') β (β {x} {xs} β (p : P x) β R (x β· xs) (here p)) β (β {x} {xs} β (p : Some P xs) β R xs p β R (x β· xs) (there p)) β β {xs : List A} (s : Some P xs) β R xs s Some-elim R h t (here p) = h p Some-elim R h t (there p) = t p (Some-elim R h t p) Some-rec : β {P : A β Type β} {X : Type β'} β (β {x} β P x β X) β (X β X) β β {xs : List A} β Some P xs β X Some-rec h t (here p) = h p Some-rec h t (there p) = t (Some-rec h t p) All-elim : β {P : A β Type β} (R : (xs : List A) β All P xs β Type β') β R [] [] β ( β {x} {xs} (px : P x) (pxs : All P xs) β R xs pxs β R (x β· xs) (px β· pxs)) β β {xs : List A} (a : All P xs) β R xs a All-elim R n e [] = n All-elim R n e (x β· a) = e x a (All-elim R n e a) All-rec : β {P : A β Type β} {X : Type β'} β X β (β {x} β P x β X β X) β β {xs : List A} β All P xs β X All-rec n e [] = n All-rec n e (p β· a) = e p (e p n)
β·-all-head : All P (x β· xs) β P x β·-all-head (px β· _) = px β·-all-tail : All P (x β· xs) β All P xs β·-all-tail (_ β· pxs) = pxs β·-all-Γ : All P (x β· xs) β P x Γ All P xs β·-all-Γ (px β· pxs) = px , pxs Β¬some-[] : Β¬ Some P [] Β¬some-[] () β·-some-β : Some P (x β· xs) β P x β Some P xs β·-some-β (here px) = inl px β·-some-β (there pxs) = inr pxs β·-Β¬some-head : Β¬ (Some P (x β· xs)) β Β¬ (P x) β·-Β¬some-head Β¬some px = Β¬some (here px) β·-Β¬some-tail : Β¬ (Some P (x β· xs)) β Β¬ (Some P xs) β·-Β¬some-tail Β¬some px = Β¬some (there px)
some-map : (β {x} β P x β Q x) β Some P xs β Some Q xs some-map f (here px) = here (f px) some-map f (there pxs) = there (some-map f pxs) all-map : (β {x} β P x β Q x) β All P xs β All Q xs all-map f [] = [] all-map f (px β· pxs) = f px β· all-map f pxs not-someβall-not : Β¬ (Some P xs) β All (Ξ» x β Β¬ (P x)) xs not-someβall-not {xs = []} Β¬some = [] not-someβall-not {xs = x β· xs} Β¬some = β·-Β¬some-head Β¬some β· not-someβall-not (β·-Β¬some-tail Β¬some) all-notβnot-some : All (Ξ» x β Β¬ P x) xs β Β¬ (Some P xs) all-notβnot-some (Β¬P β· ps) (here p) = Β¬P p all-notβnot-some (Β¬P β· ps) (there x) = all-notβnot-some ps x
some? : β {P : A β Type β} β (β x β Dec (P x)) β (xs : List A) β Dec (Some P xs) some? P? [] = no Β¬some-[] some? P? (x β· xs) with P? x ... | yes px = yes (here px) ... | no Β¬px with some? P? xs ... | yes pxs = yes (there pxs) ... | no Β¬pxs = no ([ Β¬px , Β¬pxs ] β β·-some-β) all? : β {P : A β Type β} β (β x β Dec (P x)) β (xs : List A) β Dec (All P xs) all? P? [] = yes [] all? P? (x β· xs) with P? x | all? P? xs ... | yes p | yes ps = yes (p β· ps) ... | yes p | no Β¬ps = no (Β¬ps β β·-all-tail) ... | no Β¬p | q = no (Β¬p β β·-all-head)
instance Dec-Some : β {P : A β Type β} β β¦ β {x} β Dec (P x) β¦ β {xs : List A} β Dec (Some P xs) Dec-Some {P = P} = some? (Ξ» x β holds? (P x)) _ Dec-All : β {P : A β Type β} β β¦ β {x} β Dec (P x) β¦ β {xs : List A} β Dec (All P xs) Dec-All {P = P} = all? (Ξ» x β holds? (P x)) _
all-β : All P xs β x β xs β P x all-β {P = P} (px β· pxs) (here p) = subst P (sym p) px all-β (px β· pxs) (there xβxs) = all-β pxs xβxs to-all : (β x β x β xs β P x) β All P xs to-all {xs = []} p = [] to-all {xs = x β· xs} p = p x (here refl) β· to-all (Ξ» i w β p i (there w))
instance H-Level-All : β {β β'} {A : Type β} {P : A β Type β'} {n} β¦ _ : β {x} β H-Level (P x) n β¦ {xs} β H-Level (All P xs) n H-Level-All {P = P} {xs = xs} = hlevel-instance (retractβis-hlevel {A = β x β x ββ xs β P x} _ to-all (Ξ» a _ β all-β a) inv (hlevel _)) where inv : β {xs} β is-left-inverse (to-all {xs = xs} {P = P}) (Ξ» a z β all-β a) inv {xs} [] = refl inv {x β· xs} (y β· ys) i = coe1βi (Ξ» _ β P x) i y β· inv ys i