0 | module Data.AssocList
3 | import Data.Prim.Bits32
4 | import Data.Maybe.Upper
13 | 0 (<): Maybe Key -> Maybe Key -> Type
17 | 0 (<=): Maybe Key -> Maybe Key -> Type
18 | (<=) = ReflexiveClosure (<)
27 | data AL : (ix : Maybe Key) -> Type -> Type where
32 | -> (0 prf : Just (fst p) < ix)
33 | => AL (Just $
fst p) a
36 | Functor (AL ix) where
38 | map f ((k,v) :: t) = (k,f v) :: map f t
41 | foldlKV_ : (acc -> (Key,el) -> acc) -> acc -> AL m el -> acc
42 | foldlKV_ f x (p :: ps) = foldlKV_ f (f x p) ps
46 | traverseKV_ : Applicative f => ((Key,a) -> f b) -> AL m a -> f (AL m b)
47 | traverseKV_ g [] = pure []
48 | traverseKV_ g (p :: ps) =
49 | [| (\vb,ps' => (fst p,vb) :: ps') (g p) (traverseKV_ g ps) |]
53 | Foldable (AL ix) where
55 | foldr c n (x::xs) = c (snd x) (foldr c n xs)
58 | foldl f q (x::xs) = foldl f (f q $
snd x) xs
63 | foldMap f = foldl (\acc, elem => acc <+> f elem) neutral
66 | Traversable (AL ix) where
67 | traverse f [] = pure []
68 | traverse f (p :: ps) =
69 | (\v,qs => (fst p,v) :: qs) <$> f (snd p) <*> traverse f ps
73 | lookup_ : (k : Key) -> AL ix a -> Maybe a
74 | lookup_ k (p :: ps) = case comp k (fst p) of
76 | EQ _ _ _ => Just $
snd p
77 | GT _ _ _ => lookup_ k ps
78 | lookup_ _ [] = Nothing
81 | nonEmpty_ : AL m a -> Bool
82 | nonEmpty_ (p :: ps) = True
83 | nonEmpty_ [] = False
86 | isEmpty_ : AL m a -> Bool
87 | isEmpty_ (p :: ps) = False
92 | pairs_ : AL ix a -> List (Key,a)
93 | pairs_ (p :: ps) = p :: pairs_ ps
98 | keys_ : AL ix a -> List Key
99 | keys_ (p :: ps) = fst p :: keys_ ps
104 | values_ : AL ix a -> List a
105 | values_ (p :: ps) = snd p :: values_ ps
109 | length_ : AL ix a -> Nat
110 | length_ (p :: ps) = S $
length_ ps
115 | heq : Eq a => AL m a -> AL n a -> Bool
116 | heq (p :: ps) (q :: qs) = p == q && heq ps qs
120 | public export %inline
121 | Eq a => Eq (AL m a) where
125 | Show a => Show (AL m a) where
126 | showPrec p m = showPrec p (pairs_ m)
139 | record InsertRes (k : Key) (mk : Maybe Key) (a : Type) where
142 | pairs : AL (Just firstKey) a
143 | 0 prf : Either (firstKey === k) (Just firstKey === mk)
147 | -> {mk : Maybe Key}
148 | -> Either (x === k) (Just x === mk)
149 | -> {auto 0 ltyk : Just y < Just k}
150 | -> {auto 0 ltym : Just y < mk}
152 | prependLemma (Left Refl) = ltyk
153 | prependLemma (Right z) = rewrite z in ltym
158 | -> InsertRes k mk a
159 | -> {auto 0 lt : Just (fst p) < Just k}
160 | -> {auto 0 sm : Just (fst p) < mk}
161 | -> InsertRes k (Just $
fst p) a
162 | prepend p (IR ps prf1) =
163 | let 0 prf := prependLemma prf1
164 | in IR (p :: ps) (Right Refl)
173 | insert_ : (k : Key) -> (v : a) -> AL ix a -> InsertRes k ix a
174 | insert_ k v (p :: ps) = case comp k (fst p) of
175 | LT prf _ _ => IR ((k,v) :: p :: ps) (Left Refl)
176 | EQ _ prf _ => IR ((fst p, v) :: ps) (Right Refl)
177 | GT _ _ prf => prepend p $
insert_ k v ps
178 | insert_ k v [] = IR [(k,v)] (Left Refl)
188 | -> InsertRes k ix a
189 | insertWith_ f k v (p :: ps) = case comp k (fst p) of
190 | LT prf _ _ => IR ((k,v) :: p :: ps) (Left Refl)
191 | EQ _ prf _ => IR ((fst p, f v $
snd p) :: ps) (Right Refl)
192 | GT _ _ prf => prepend p $
insertWith_ f k v ps
193 | insertWith_ _ k v [] = IR [(k,v)] (Left Refl)
205 | record DeleteRes (m1 : Maybe Key) (a : Type) where
215 | -> {auto 0 lt : Just (fst p) < m}
216 | -> DeleteRes (Just $
fst p) a
217 | prependDR p (DR ps lte) =
218 | let 0 lt = strictLeft lt lte
219 | in DR (p :: ps) Refl
225 | delete_ : (k : Key) -> AL m a -> DeleteRes m a
226 | delete_ k (p :: ps) = case comp k (fst p) of
227 | LT _ _ _ => DR (p :: ps) %search
228 | EQ _ _ _ => DR ps %search
229 | GT _ _ _ => prependDR p $
delete_ k ps
230 | delete_ k [] = DR [] %search
235 | mapMaybe_ : (a -> Maybe b) -> AL m a -> DeleteRes m b
236 | mapMaybe_ f (p :: ps) = case f (snd p) of
237 | Just v => prependDR (fst p, v) $
mapMaybe_ f ps
239 | let DR qs prf := mapMaybe_ f ps
240 | in DR qs (transitive %search prf)
241 | mapMaybe_ f [] = DR [] %search
246 | mapMaybeK_ : (Key -> a -> Maybe b) -> AL m a -> DeleteRes m b
247 | mapMaybeK_ f ((n,va) :: ps) = case f n va of
248 | Just vb => prependDR (n,vb) $
mapMaybeK_ f ps
250 | let DR qs prf := mapMaybeK_ f ps
251 | in DR qs (transitive %search prf)
252 | mapMaybeK_ f [] = DR [] %search
260 | update_ : (k : Key) -> (a -> a) -> AL m a -> AL m a
261 | update_ k f (p :: ps) = case comp k (fst p) of
262 | LT _ _ _ => p :: ps
263 | EQ _ _ _ => (fst p, f $
snd p) :: ps
264 | GT _ _ _ => p :: update_ k f ps
265 | update_ k f [] = []
270 | updateA_ : Applicative f => (k : Key) -> (a -> f a) -> AL m a -> f (AL m a)
271 | updateA_ k g (p :: ps) = case comp k (fst p) of
272 | LT _ _ _ => pure $
p :: ps
273 | EQ _ _ _ => map (\v => (fst p, v) :: ps) (g $
snd p)
274 | GT _ _ _ => (p ::) <$> updateA_ k g ps
275 | updateA_ k g [] = pure []
285 | record UnionRes (m1,m2 : Maybe Key) (a : Type) where
289 | 0 prf : Either (m1 === mx) (m2 === mx)
291 | 0 trans_LT_EQ : {0 lt : _} -> Transitive a lt => lt x y -> y === z -> lt x z
292 | trans_LT_EQ w Refl = w
294 | public export %inline
297 | -> UnionRes m1 (Just k2) a
298 | -> {auto 0 prf1 : Just (fst p) < m1}
299 | -> {auto 0 prf2 : Just (fst p) < Just k2}
300 | -> UnionRes (Just $
fst p) (Just k2) a
301 | prepLT p (UR ps prf) =
302 | let 0 lt := either (trans_LT_EQ prf1) (trans_LT_EQ prf2) prf
303 | in UR (p :: ps) (Left Refl)
305 | public export %inline
308 | -> UnionRes (Just k1) m2 a
309 | -> {auto 0 prf1 : Just (fst p) < m2}
310 | -> {auto 0 prf2 : Just (fst p) < Just k1}
311 | -> UnionRes (Just k1) (Just $
fst p) a
312 | prepGT p (UR ps prf) =
313 | let 0 lt := either (trans_LT_EQ prf2) (trans_LT_EQ prf1) prf
314 | in UR (p :: ps) (Right Refl)
316 | public export %inline
320 | -> (0 eq : fst p === k)
321 | -> UnionRes m1 m2 a
322 | -> {auto 0 prf1 : Just (fst p) < m1}
323 | -> {auto 0 prf2 : Just k < m2}
324 | -> UnionRes (Just $
fst p) x a
325 | prepEQ p eq (UR ps prf) =
326 | let 0 fstp_lt_m2 := rewrite eq in prf2
327 | 0 lt = either (trans_LT_EQ prf1) (trans_LT_EQ fstp_lt_m2) prf
328 | in UR (p :: ps) (Left Refl)
336 | union_ : AL m1 a -> AL m2 a -> UnionRes m1 m2 a
337 | union_ xs@(p :: ps) ys@(q :: qs) =
338 | case comp (fst p) (fst q) of
339 | LT prf _ _ => prepLT p $
union_ ps ys
340 | EQ _ prf _ => prepEQ p prf $
union_ ps qs
341 | GT _ _ prf => prepGT q $
union_ xs qs
342 | union_ y [] = UR y (Left Refl)
343 | union_ [] y = UR y (Right Refl)
354 | -> UnionRes m1 m2 b
355 | unionMap_ f g xs@(p :: ps) ys@(q :: qs) =
356 | case comp (fst p) (fst q) of
357 | LT prf _ _ => prepLT (fst p, g $
snd p) $
unionMap_ f g ps ys
358 | EQ _ prf _ => prepEQ (fst p, f (snd p) (snd q)) prf $
unionMap_ f g ps qs
359 | GT _ _ prf => prepGT (fst q, g $
snd q) $
unionMap_ f g xs qs
360 | unionMap_ _ g y [] = UR (map g y) (Left Refl)
361 | unionMap_ _ g [] y = UR (map g y) (Right Refl)
367 | unionWith_ : (a -> a -> a) -> AL m1 a -> AL m2 a -> UnionRes m1 m2 a
368 | unionWith_ f = unionMap_ f id
378 | record IntersectRes (m1,m2 : Maybe Key) (a : Type) where
385 | 0 lteNothing : {m : Maybe Key} -> m <= Nothing
386 | lteNothing {m = Nothing} = Refl
387 | lteNothing {m = Just _} = Rel %search
389 | public export %inline
391 | {0 m1,m2 : Maybe Key}
393 | -> (0 prf : fst p === k)
394 | -> IntersectRes m1 m2 a
395 | -> {auto 0 lt : Just (fst p) < m1}
396 | -> IntersectRes (Just $
fst p) (Just k) a
397 | prependIS p prf (IS ps prf1 prf2) =
398 | let 0 ltp := strictLeft lt prf1
399 | in IS (p :: ps) Refl (fromEq $
cong Just (sym prf))
405 | intersect_ : AL m1 a -> AL m2 a -> IntersectRes m1 m2 a
406 | intersect_ xs@(p :: ps) ys@(q :: qs) =
407 | case comp (fst p) (fst q) of
409 | let IS res p1 p2 := intersect_ ps ys
410 | in IS res (transitive %search p1) p2
411 | EQ _ y _ => prependIS p y $
intersect_ ps qs
413 | let IS res p1 p2 := intersect_ xs qs
414 | in IS res p1 (transitive %search p2)
415 | intersect_ y [] = IS [] lteNothing %search
416 | intersect_ [] y = IS [] %search lteNothing
422 | intersectWith_ : (a -> a -> b) -> AL m1 a -> AL m2 a -> IntersectRes m1 m2 b
423 | intersectWith_ f xs@(p :: ps) ys@(q :: qs) =
424 | case comp (fst p) (fst q) of
426 | let IS res p1 p2 := intersectWith_ f ps ys
427 | in IS res (transitive %search p1) p2
428 | EQ _ y _ => prependIS (fst p, f (snd p) (snd q)) y $
intersectWith_ f ps qs
430 | let IS res p1 p2 := intersectWith_ f xs qs
431 | in IS res p1 (transitive %search p2)
432 | intersectWith_ _ y [] = IS [] lteNothing %search
433 | intersectWith_ _ [] y = IS [] %search lteNothing
440 | record AssocList a where
442 | {0 firstKey : Maybe Key}
443 | repr : AL firstKey a
445 | public export %inline
446 | Functor AssocList where
447 | map f (MkAL r) = MkAL $
map f r
449 | public export %inline
450 | foldlKV : (acc -> (Key,el) -> acc) -> acc -> AssocList el -> acc
451 | foldlKV f ini (MkAL r) = foldlKV_ f ini r
454 | traverseKV : Applicative f => ((Key,a) -> f b) -> AssocList a -> f (AssocList b)
455 | traverseKV f (MkAL r) = MkAL <$> traverseKV_ f r
457 | public export %inline
458 | Foldable AssocList where
459 | foldr c n (MkAL r) = foldr c n r
460 | foldl c n (MkAL r) = foldl c n r
461 | null (MkAL r) = null r
462 | foldMap f (MkAL r) = foldMap f r
464 | public export %inline
465 | Traversable AssocList where
466 | traverse f (MkAL r) = MkAL <$> traverse f r
469 | public export %inline
470 | lookup : (k : Key) -> AssocList a -> Maybe a
471 | lookup k (MkAL r) = lookup_ k r
473 | public export %inline
474 | nonEmpty : AssocList a -> Bool
475 | nonEmpty (MkAL r) = nonEmpty_ r
477 | public export %inline
478 | isEmpty : AssocList a -> Bool
479 | isEmpty (MkAL r) = isEmpty_ r
482 | public export %inline
483 | pairs : AssocList a -> List (Key,a)
484 | pairs (MkAL r) = pairs_ r
487 | public export %inline
488 | keys : AssocList a -> List Key
489 | keys (MkAL r) = keys_ r
492 | public export %inline
493 | values : AssocList a -> List a
494 | values (MkAL r) = values_ r
496 | public export %inline
497 | length : AssocList a -> Nat
498 | length (MkAL r) = length_ r
500 | public export %inline
501 | Eq a => Eq (AssocList a) where
502 | MkAL r1 == MkAL r2 = heq r1 r2
505 | Show a => Show (AssocList a) where
506 | showPrec p (MkAL r) = showCon p "MkAL" $
showArg r
514 | public export %inline
515 | insert : (k : Key) -> (v : a) -> AssocList a -> AssocList a
516 | insert k v (MkAL r) = MkAL $
pairs $
insert_ k v r
520 | public export %inline
527 | insertWith f k v (MkAL r) = MkAL $
pairs $
insertWith_ f k v r
530 | fromList : List (Key,a) -> AssocList a
531 | fromList = foldl (\al,(k,v) => insert k v al) (MkAL [])
536 | public export %inline
537 | delete : (k : Key) -> AssocList a -> AssocList a
538 | delete k (MkAL r) = MkAL $
pairs $
delete_ k r
542 | public export %inline
543 | mapMaybe : (a -> Maybe b) -> AssocList a -> AssocList b
544 | mapMaybe f (MkAL r) = MkAL $
pairs $
mapMaybe_ f r
548 | public export %inline
549 | mapMaybeK : (Key -> a -> Maybe b) -> AssocList a -> AssocList b
550 | mapMaybeK f (MkAL r) = MkAL $
pairs $
mapMaybeK_ f r
553 | public export %inline
554 | update : (k : Key) -> (a -> a) -> AssocList a -> AssocList a
555 | update k f (MkAL r) = MkAL $
update_ k f r
559 | public export %inline
560 | updateA : Applicative f => Key -> (a -> f a) -> AssocList a -> f (AssocList a)
561 | updateA k g (MkAL r) = MkAL <$> updateA_ k g r
568 | public export %inline
569 | union : AssocList a -> AssocList a -> AssocList a
570 | union (MkAL r1) (MkAL r2) = MkAL $
pairs $
union_ r1 r2
575 | public export %inline
582 | unionMap f g (MkAL r1) (MkAL r2) = MkAL $
pairs $
unionMap_ f g r1 r2
587 | public export %inline
588 | unionWith : (a -> a -> a) -> AssocList a -> AssocList a -> AssocList a
589 | unionWith f = unionMap f id
594 | public export %inline
595 | intersect : AssocList a -> AssocList a -> AssocList a
596 | intersect (MkAL r1) (MkAL r2) = MkAL $
pairs $
intersect_ r1 r2
602 | intersectWith : (a -> a -> b) -> AssocList a -> AssocList a -> AssocList b
603 | intersectWith f (MkAL r1) (MkAL r2) = MkAL $
pairs $
intersectWith_ f r1 r2