0 | module Data.List.Quantifiers.Extra
2 | import public Data.List.Elem
3 | import public Data.List.Quantifiers
10 | 0 HSum : List Type -> Type
17 | 0 Has : (v : a) -> (vs : List a) -> Type
24 | 0 (-) : (ts : List a) -> (v : a) -> (prf : Has v ts) => List a
25 | (-) (_ :: vs) _ @{Here} = vs
26 | (-) (y :: x :: xs) v @{There k} = y :: (-) (x :: xs) v
30 | inject : (prf : Has t ts) => f t -> Any f ts
31 | inject @{Here} v = Here v
32 | inject @{There _} v = There $
inject v
36 | widen : All (`Has` ts) ss => Any f ss -> Any f ts
37 | widen @{_::_} (Here v) = inject v
38 | widen @{_::_} (There v) = widen v
42 | project : (0 t : k) -> (prf : Has t ts) => Any f ts -> Maybe (f t)
43 | project t @{Here} (Here v) = Just v
44 | project t @{There p} (There v) = project t @{p} v
45 | project t _ = Nothing
48 | public export %inline
49 | project' : (prf : Has t ts) => Any f ts -> Maybe (f t)
50 | project' = project t
53 | public export %inline
54 | project1 : Any f [t] -> f t
55 | project1 (Here v) = v
56 | project1 (There _) impossible
63 | -> {auto prf : Has t ts}
65 | -> Either (Any f (ts - t)) (f t)
66 | decomp @{Here} (Here v) = Right v
67 | decomp @{Here} (There v) = Left $
v
68 | decomp @{There _} {ts = _ :: _ :: _} (Here v) = Left $
Here v
69 | decomp @{There _} {ts = _ :: _ :: _} (There v) = mapFst There $
decomp v
72 | Replaced : (vs : List a) -> Elem v vs -> (w : a) -> List a
73 | Replaced (x :: xs) Here w = w :: xs
74 | Replaced (x :: xs) (There p) w = x :: Replaced xs p w
81 | public export %inline
82 | hmap : ({0 v : k} -> f v -> g v) -> Any f ks -> Any g ks
87 | ({0 v : k} -> f v -> g v -> h v)
91 | hzipWith fun (v :: vs) (Here x) = Here (fun v x)
92 | hzipWith fun (v :: vs) (There x) = There $
hzipWith fun vs x
95 | collapse : ({0 v : k} -> f v -> x) -> Any f ks -> x
96 | collapse g (Here v) = g v
97 | collapse g (There v) = collapse g v
99 | public export %inline
100 | collapse' : Any (Prelude.const x) ks -> x
101 | collapse' = collapse id
104 | hsequence : Applicative f => Any (f . g) ks -> f (Any g ks)
105 | hsequence (Here x) = Here <$> x
106 | hsequence (There x) = There <$> hsequence x
109 | replace : (h : Has t ts) -> f s -> Any f ts -> Any f (Replaced ts h s)
110 | replace Here v (Here x) = Here v
111 | replace Here v (There x) = There x
112 | replace (There p) v (Here x) = Here x
113 | replace (There p) v (There x) = There $
replace p v x
116 | update : (h : Has t ts) => (f t -> f s) -> Any f ts -> Any f (Replaced ts h s)
117 | update @{Here} fun (Here x) = Here (fun x)
118 | update @{Here} fun (There x) = There x
119 | update @{There p} fun (Here x) = Here x
120 | update @{There p} fun (There x) = There $
update fun x
123 | public export %inline
124 | hmap : ({0 v : k} -> f v -> g v) -> All f ks -> All g ks
127 | public export %inline
128 | hmapW : {ks : _} -> ((v : k) -> f v -> g v) -> All f ks -> All g ks
129 | hmapW {ks = []} _ [] = []
130 | hmapW {ks = v::vs} g (x::xs) = g v x :: hmapW g xs
134 | ({0 v : k} -> f v -> g v -> h v)
138 | hzipWith fun (x :: xs) (y :: ys) = fun x y :: hzipWith fun xs ys
139 | hzipWith fun [] [] = []
144 | -> ((v : k) -> f v -> g v -> h v)
148 | hzipWithW {ks = v::vs} fun (x :: xs) (y :: ys) = fun v x y :: hzipWithW fun xs ys
149 | hzipWithW {ks = []} fun [] [] = []
151 | public export %inline
152 | happly : All (\x => f x -> g x) ks -> Any f ks -> Any g ks
153 | happly = hzipWith id
155 | public export %inline
156 | happly' : All (\x => f x -> t) ks -> Any f ks -> t
157 | happly' fs = collapse' . happly fs
160 | hfill : {ks : _} -> ((v : k) -> f v) -> All f ks
161 | hfill {ks = []} g = []
162 | hfill {ks = v::vs} g = g v :: hfill g
164 | public export %inline
165 | hconst : {ks : _} -> t -> All (Prelude.const t) ks
166 | hconst = hfill . const
169 | hpure : All (Prelude.const ()) ks => ({0 v : k} -> f v) -> All f ks
170 | hpure @{[]} fun = []
171 | hpure @{_ :: _} fun = fun :: hpure fun
174 | hfoldl : ({0 v : k} -> acc -> f v -> acc) -> acc -> All f ks -> acc
176 | hfoldl g x (y :: ys) = hfoldl g (g x y) ys
178 | public export %inline
179 | hfoldMap : Monoid m => ({0 v : k} -> f v -> m) -> All f ks -> m
180 | hfoldMap g = hfoldl (\x,v => x <+> g v) neutral
183 | hsequence : Applicative f => All (f . g) ks -> f (All g ks)
184 | hsequence [] = pure []
185 | hsequence (x::xs) = [| x :: hsequence xs |]
188 | replace : (h : Has t ts) -> f s -> All f ts -> All f (Replaced ts h s)
189 | replace Here v (x::xs) = v :: xs
190 | replace (There p) v (x::xs) = x :: replace p v xs
193 | update : (h : Has t ts) => (f t -> f s) -> All f ts -> All f (Replaced ts h s)
194 | update @{Here} fun (x::xs) = fun x :: xs
195 | update @{There p} fun (x::xs) = x :: update fun xs
202 | allEq : All (Ord . p) xs => All (Eq . p) xs
204 | allEq @{_ :: _} = %search :: allEq
207 | All (Ord . p) xs => Ord (Any p xs) where
208 | compare @{_ :: _} (Here x) (Here y) = compare x y
209 | compare @{_ :: _} (There x) (There y) = compare x y
210 | compare (Here _) (There _) = LT
211 | compare (There _) (Here _) = GT