{-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Trie.General.CollectionsInstances.ListGT -- Copyright : (c) Adrian Hey 2007 -- License : BSD3 -- -- Maintainer : http://homepages.nildram.co.uk/~ahey/em.png -- Stability : provisional -- Portability : Multi-parameter type classes, Functional dependencies -- -- Instances of the Collections package Classes for the 'ListGT' type. ----------------------------------------------------------------------------- module Data.Trie.General.CollectionsInstances.ListGT ( ) where import Data.Trie.General.Types (GT(..)) import Data.Trie.General.ListGT import qualified Data.Monoid as M (Monoid(..)) import qualified Data.Collections as Coll (Foldable(..),foldr',Unfoldable(..),Collection(..),Map(..)) import qualified Data.Maybe as MB (isJust) #ifdef __GLASGOW_HASKELL__ import GHC.Base #include "ghcdefs.h" #else #include "h98defs.h" #endif ------------------------------- -- Data.Collections.Foldable -- ------------------------------- instance GT map k => Coll.Foldable (ListGT map k ([k],a)) ([k],a) where -- fold :: Monoid ([k],a) => ListGT map k ([k],a) -> ([k],a) fold mp = foldrElemsAscendingListGT (\assoc b -> M.mappend assoc b) mp M.mempty -- foldMap :: Monoid m => (([k],a) -> m) -> ListGT map k ([k],a) -> m foldMap f mp = foldrElemsAscendingListGT (\assoc b -> M.mappend (f assoc) b) mp M.mempty -- foldr :: (([k],a) -> b -> b) -> b -> ListGT map k ([k],a) -> b foldr f b0 mp = foldrElemsAscendingListGT (\assoc b -> f assoc b) mp b0 -- foldl :: (b -> ([k],a) -> b) -> b -> ListGT map k ([k],a) -> b foldl f b0 mp = foldrElemsDescendingListGT (\assoc b -> f b assoc) mp b0 {- ToDo: Implement properly. Meantime Foldable class has suitable defaults via lists. -- foldr1 :: (([k],a) -> ([k],a) -> ([k],a)) -> ListGT map k ([k],a) -> ([k],a) -- foldl1 :: (([k],a) -> ([k],a) -> ([k],a)) -> ListGT map k ([k],a) -> ([k],a) -} -- null :: ListGT map k ([k],a) -> Bool null = isEmptyListGT -- size :: ListGT map k ([k],a) -> Int size mp = ASINT(addSizeListGT mp L(0)) -- isSingleton :: ListGT map k ([k],a) -> Bool isSingleton = isSingletonListGT ------------------------------- --------------------------------- -- Data.Collections.Unfoldable -- --------------------------------- instance GT map k => Coll.Unfoldable (ListGT map k ([k],a)) ([k],a) where -- insert :: ([k],a) -> ListGT map k ([k],a) -> ListGT map k ([k],a) insert assoc@(k,_) mp = insertListGT' (const assoc) k assoc mp -- Note use of strict insertListGT' -- empty :: ListGT map k ([k],a) empty = emptyListGT -- singleton :: ([k],a) -> ListGT map k ([k],a) singleton assoc@(k,_) = singletonListGT k assoc -- insertMany :: Foldable c' ([k],a) => c' -> ListGT map k ([k],a) -> ListGT map k ([k],a) insertMany c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertListGT' (const assoc) k assoc mp) mp0 c -- ?? stricness?? l/r?? -- insertManySorted :: Foldable c' ([k],a) => c' -> ListGT map k ([k],a) -> ListGT map k ([k],a) insertManySorted c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertListGT' (const assoc) k assoc mp) mp0 c -- How to implement efficiently ?? --------------------------------- --------------------------------- -- Data.Collections.Collection -- --------------------------------- instance GT map k => Coll.Collection (ListGT map k ([k],a)) ([k],a) where -- filter :: (([k],a) -> Bool) -> ListGT map k ([k],a) -> ListGT map k ([k],a) filter = filterListGT --------------------------------- -------------------------- -- Data.Collections.Map -- -------------------------- instance (GT map k, M.Monoid a) => Coll.Map (ListGT map k a) [k] a where -- delete :: [k] -> ListGT map k a -> ListGT map k a delete = deleteListGT -- member :: [k] -> ListGT map k a -> Bool member k mp = MB.isJust (lookupListGT k mp) -- union :: ListGT map k a -> ListGT map k a -> ListGT map k a union = unionListGT' (\x _ -> x) -- Note use of strict unionListGT' -- intersection :: ListGT map k a -> ListGT map k a -> ListGT map k a intersection = intersectionListGT' (\x _ -> x) -- Note use of strict intersectionListGT' -- difference :: ListGT map k a -> ListGT map k a -> ListGT map k a difference = differenceListGT -- isSubset :: ListGT map k a -> ListGT map k a -> Bool isSubset = isSubsetOfListGT -- lookup :: Monad m => [k] -> ListGT map k a -> m a lookup k mp = case lookupListGT k mp of Just a -> return a Nothing -> fail "Data.Collections.Map.lookup: Key not found in ListGT." -- alter :: (Maybe a -> Maybe a) -> [k] -> ListGT map k a -> ListGT map k a alter = alterListGT -- insertWith :: (a -> a -> a) -> [k] -> a -> ListGT map k a -> ListGT map k a insertWith f ks a lgt = insertListGT (f a) ks a lgt -- fromFoldableWith :: Coll.Foldable l ([k],a) => (a -> a -> a) -> l -> ListGT map k a fromFoldableWith f l = Coll.foldr insrt emptyListGT l -- Strictness ?? where insrt (ks,a) lgt = insertListGT (f a) ks a lgt -- foldGroups :: Coll.Foldable l ([k],b) => (b -> a -> a) -> a -> l -> ListGT map k a foldGroups f a0 l = Coll.foldr' insrt emptyListGT l where insrt (ks,b) lgt = insertListGT (f b) ks (f b a0) lgt -- mapWithKey :: ([k] -> a -> a) -> ListGT map k a -> ListGT map k a mapWithKey = mapWithKeyListGT -- unionWith :: (a -> a -> a) -> ListGT map k a -> ListGT map k a -> ListGT map k a unionWith = unionListGT -- intersectionWith :: (a -> a -> a) -> ListGT map k a -> ListGT map k a -> ListGT map k a intersectionWith = intersectionListGT -- differenceWith :: (a -> a -> Maybe a) -> ListGT map k a -> ListGT map k a -> ListGT map k a differenceWith = differenceMaybeListGT -- isSubmapBy :: (a -> a -> Bool) -> ListGT map k a -> ListGT map k a -> Bool isSubmapBy = isSubmapOfListGT --------------------------