3 | module Data.CheckedEmpty.List.Quantifiers
5 | import Data.CheckedEmpty.List
6 | import Data.CheckedEmpty.List.Elem
16 | data Any : (0 p : a -> Type) -> Lst ne a -> Type where
18 | Here : {0 u0 : _} -> {0 u1 : _} ->
19 | p x -> Any p $
(x :: xs) @{u0} @{u1}
21 | There : {0 u0 : _} -> {0 u1 : _} ->
22 | Any p xs -> Any p $
(x :: xs) @{u0} @{u1}
25 | Uninhabited (Any p Nil) where
26 | uninhabited (Here
_) impossible
27 | uninhabited (There
_) impossible
30 | {0 p : a -> Type} -> {0 u0 : _} -> {0 u1 : _} ->
31 | Uninhabited (p x) => Uninhabited (Any p xs) => Uninhabited (Any p $
(x::xs) @{u0} @{u1}) where
32 | uninhabited $
Here y = uninhabited y
33 | uninhabited $
There y = uninhabited y
36 | mapProperty : (f : {0 x : a} -> p x -> q x) -> Any p l -> Any q l
37 | mapProperty f $
Here p = Here $
f p
38 | mapProperty f $
There p = There $
mapProperty f p
41 | any : ((x : a) -> Dec $
p x) -> (xs : Lst ne a) -> Dec $
Any p xs
42 | any _ Nil = No uninhabited
43 | any p $
x::xs with (p x)
44 | any p $
_::_ | Yes px = Yes $
Here px
45 | any p $
x::xs | No npx = case any p xs of
46 | Yes pxs => Yes $
There pxs
47 | No npxs => No $
\case
49 | There pxs => npxs pxs
52 | toExists : Any p xs -> Exists p
53 | toExists $
Here prf = Evidence _ prf
54 | toExists $
There prf = toExists prf
59 | data All : (0 p : a -> Type) -> Lst ne a -> Type where
61 | (::) : {0 u0 : _} -> {0 u1 : _} ->
62 | p x -> All p xs -> All p $
(x :: xs) @{u0} @{u1}
65 | {0 u0 : _} -> {0 u1 : _} ->
66 | Either (Uninhabited $
p x) (Uninhabited $
All p xs) => Uninhabited (All p $
(x::xs) @{u0} @{u1}) where
67 | uninhabited @{Left _} (px::pxs) = uninhabited px
68 | uninhabited @{Right _} (px::pxs) = uninhabited pxs
71 | mapProperty : (f : {0 x : a} -> p x -> q x) -> All p l -> All q l
72 | mapProperty f [] = []
73 | mapProperty f (p::ps) = f p :: mapProperty f ps
76 | zipPropertyWith : ({0 x : a} -> p x -> q x -> r x) ->
77 | All p xs -> All q xs -> All r xs
78 | zipPropertyWith f [] [] = []
79 | zipPropertyWith f (px::pxs) (qx::qxs) = f px qx :: zipPropertyWith f pxs qxs
81 | public export %inline
82 | imapProperty : (0 i : a -> Type) ->
83 | (f : {0 x : _} -> i x => p x -> q x) ->
84 | All i xs => All p xs -> All q xs
85 | imapProperty _ f @{ps} = zipPropertyWith (\_ => f) ps
88 | forget : All {ne} (const a) b -> Lst ne a
90 | forget (x::xs) = x :: forget xs
93 | all : ((x : a) -> Dec $
p x) -> (xs : Lst _ a) -> Dec $
All p xs
95 | all p $
x::xs with (p x)
96 | all p $
_::_ | No npx = No $
\(px::_) => npx px
97 | all p $
x::xs | Yes px = case all p xs of
98 | Yes pxs => Yes $
px :: pxs
99 | No npxs => No $
\(_::pxs) => npxs pxs
102 | index : (idx : Fin xs.length) -> All p xs -> p (index idx xs)
103 | index FZ (p::_ ) = p
104 | index (FS n) ( _::ps) = index n ps
108 | HLst : Lst ne Type -> Type
118 | negAnyAll : {xs : Lst ne a} -> Not (Any p xs) -> All (Not . p) xs
119 | negAnyAll {xs=[]} _ = []
120 | negAnyAll {xs=_::_} f = (f . Here) :: negAnyAll (f . There)
123 | anyNegAll : Any (Not . p) xs -> Not (All p xs)
124 | anyNegAll (Here nnp) (p::_) = nnp p
125 | anyNegAll (There np) (_::ps) = anyNegAll np ps
128 | allNegAny : All (Not . p) xs -> Not (Any p xs)
129 | allNegAny [] p = absurd p
130 | allNegAny (np::npxs) (Here p) = absurd $
np p
131 | allNegAny (np::npxs) (There p) = allNegAny npxs p
134 | indexAll : Elem x xs -> All p xs -> p x
135 | indexAll Here $
p :: _ = p
136 | indexAll (There e) $
_ :: ps = indexAll e ps
141 | pushIn : (xs : Lst ne a) -> (0 _ : All p xs) -> Lst ne $
Subset a p
143 | pushIn (x::xs) (p::ps) = Element x p :: pushIn xs ps
146 | pullOut : (xs : Lst ne $
Subset a p) -> Subset (Lst ne a) (All p)
147 | pullOut [] = Element [] []
148 | pullOut (Element x p :: xs) = let Element ss ps = pullOut xs in Element (x::ss) (p::ps)
151 | pushInOutInverse : (xs : Lst ne a) -> (0 prf : All p xs) -> pullOut (pushIn xs prf) = Element xs prf
152 | pushInOutInverse [] [] = Refl
153 | pushInOutInverse (x::xs) (p::ps) = rewrite pushInOutInverse xs ps in Refl
156 | pushOutInInverse : (xs : Lst ne $
Subset a p) -> uncurry Quantifiers.pushIn (pullOut xs) = xs
157 | pushOutInInverse [] = Refl
158 | pushOutInInverse (Element x p :: xs) with (pushOutInInverse xs)
159 | pushOutInInverse (Element x p :: xs) | subprf with (pullOut xs)
160 | pushOutInInverse (Element x p :: xs) | subprf | Element ss ps = rewrite subprf in Refl