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
76 | public export %inline
77 | hmap : ({0 v : k} -> f v -> g v) -> Any f ks -> Any g ks
82 | ({0 v : k} -> f v -> g v -> h v)
86 | hzipWith fun (v :: vs) (Here x) = Here (fun v x)
87 | hzipWith fun (v :: vs) (There x) = There $
hzipWith fun vs x
90 | collapse : ({0 v : k} -> f v -> x) -> Any f ks -> x
91 | collapse g (Here v) = g v
92 | collapse g (There v) = collapse g v
94 | public export %inline
95 | collapse' : Any (Prelude.const x) ks -> x
96 | collapse' = collapse id
99 | hsequence : Applicative f => Any (f . g) ks -> f (Any g ks)
100 | hsequence (Here x) = Here <$> x
101 | hsequence (There x) = There <$> hsequence x
104 | public export %inline
105 | hmap : ({0 v : k} -> f v -> g v) -> All f ks -> All g ks
108 | public export %inline
109 | hmapW : {ks : _} -> ((v : k) -> f v -> g v) -> All f ks -> All g ks
110 | hmapW {ks = []} _ [] = []
111 | hmapW {ks = v::vs} g (x::xs) = g v x :: hmapW g xs
115 | ({0 v : k} -> f v -> g v -> h v)
119 | hzipWith fun (x :: xs) (y :: ys) = fun x y :: hzipWith fun xs ys
120 | hzipWith fun [] [] = []
125 | -> ((v : k) -> f v -> g v -> h v)
129 | hzipWithW {ks = v::vs} fun (x :: xs) (y :: ys) = fun v x y :: hzipWithW fun xs ys
130 | hzipWithW {ks = []} fun [] [] = []
132 | public export %inline
133 | happly : All (\x => f x -> g x) ks -> Any f ks -> Any g ks
134 | happly = hzipWith id
136 | public export %inline
137 | happly' : All (\x => f x -> t) ks -> Any f ks -> t
138 | happly' fs = collapse' . happly fs
141 | hfill : {ks : _} -> ((v : k) -> f v) -> All f ks
142 | hfill {ks = []} g = []
143 | hfill {ks = v::vs} g = g v :: hfill g
145 | public export %inline
146 | hconst : {ks : _} -> t -> All (Prelude.const t) ks
147 | hconst = hfill . const
150 | hpure : All (Prelude.const ()) ks => ({0 v : k} -> f v) -> All f ks
151 | hpure @{[]} fun = []
152 | hpure @{_ :: _} fun = fun :: hpure fun
155 | hfoldl : ({0 v : k} -> acc -> f v -> acc) -> acc -> All f ks -> acc
157 | hfoldl g x (y :: ys) = hfoldl g (g x y) ys
159 | public export %inline
160 | hfoldMap : Monoid m => ({0 v : k} -> f v -> m) -> All f ks -> m
161 | hfoldMap g = hfoldl (\x,v => x <+> g v) neutral
164 | hsequence : Applicative f => All (f . g) ks -> f (All g ks)
165 | hsequence [] = pure []
166 | hsequence (x::xs) = [| x :: hsequence xs |]
173 | allEq : All (Ord . p) xs => All (Eq . p) xs
175 | allEq @{_ :: _} = %search :: allEq
178 | All (Ord . p) xs => Ord (Any p xs) where
179 | compare @{_ :: _} (Here x) (Here y) = compare x y
180 | compare @{_ :: _} (There x) (There y) = compare x y
181 | compare (Here _) (There _) = LT
182 | compare (There _) (Here _) = GT