{-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Trie.General.CollectionsInstances.BoolGT -- 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 'BoolGT' type. ----------------------------------------------------------------------------- module Data.Trie.General.CollectionsInstances.BoolGT ( ) where import Data.Trie.General.BoolGT 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 Coll.Foldable (BoolGT (Bool,a)) (Bool,a) where -- fold :: Monoid (Bool,a) => BoolGT (Bool,a) -> (Bool,a) fold mp = foldrElemsAscendingBoolGT (\assoc b -> M.mappend assoc b) mp M.mempty -- foldMap :: Monoid m => ((Bool,a) -> m) -> BoolGT (Bool,a) -> m foldMap f mp = foldrElemsAscendingBoolGT (\assoc b -> M.mappend (f assoc) b) mp M.mempty -- foldr :: ((Bool,a) -> b -> b) -> b -> BoolGT (Bool,a) -> b foldr f b0 mp = foldrElemsAscendingBoolGT (\assoc b -> f assoc b) mp b0 -- foldl :: (b -> (Bool,a) -> b) -> b -> BoolGT (Bool,a) -> b foldl f b0 mp = foldrElemsDescendingBoolGT (\assoc b -> f b assoc) mp b0 {- ToDo: Implement properly. Meantime Foldable class has suitable defaults via lists. -- foldr1 :: ((Bool,a) -> (Bool,a) -> (Bool,a)) -> BoolGT (Bool,a) -> (Bool,a) -- foldl1 :: ((Bool,a) -> (Bool,a) -> (Bool,a)) -> BoolGT (Bool,a) -> (Bool,a) -} -- null :: BoolGT (Bool,a) -> Bool null = isEmptyBoolGT -- size :: BoolGT (Bool,a) -> Int size mp = ASINT(addSizeBoolGT mp L(0)) -- isSingleton :: BoolGT (Bool,a) -> Bool isSingleton = isSingletonBoolGT ------------------------------- --------------------------------- -- Data.Collections.Unfoldable -- --------------------------------- instance Coll.Unfoldable (BoolGT (Bool,a)) (Bool,a) where -- insert :: (Bool,a) -> BoolGT (Bool,a) -> BoolGT (Bool,a) insert assoc@(k,_) mp = insertBoolGT' (const assoc) k assoc mp -- Note use of strict insertBoolGT' -- empty :: BoolGT (Bool,a) empty = emptyBoolGT -- singleton :: (Bool,a) -> BoolGT (Bool,a) singleton assoc@(k,_) = singletonBoolGT k assoc -- insertMany :: Foldable c' (Bool,a) => c' -> BoolGT (Bool,a) -> BoolGT (Bool,a) insertMany c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertBoolGT' (const assoc) k assoc mp) mp0 c -- ?? stricness?? l/r?? -- insertManySorted :: Foldable c' (Bool,a) => c' -> BoolGT (Bool,a) -> BoolGT (Bool,a) insertManySorted c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertBoolGT' (const assoc) k assoc mp) mp0 c -- How to implement efficiently ?? --------------------------------- --------------------------------- -- Data.Collections.Collection -- --------------------------------- instance Coll.Collection (BoolGT (Bool,a)) (Bool,a) where -- filter :: ((Bool,a) -> Bool) -> BoolGT (Bool,a) -> BoolGT (Bool,a) filter = filterBoolGT --------------------------------- -------------------------- -- Data.Collections.Map -- -------------------------- instance (M.Monoid a) => Coll.Map (BoolGT a) Bool a where -- delete :: Bool -> BoolGT a -> BoolGT a delete = deleteBoolGT -- member :: Bool -> BoolGT a -> Bool member k mp = MB.isJust (lookupBoolGT k mp) -- union :: BoolGT a -> BoolGT a -> BoolGT a union = unionBoolGT' (\x _ -> x) -- Note use of strict unionBoolGT' -- intersection :: BoolGT a -> BoolGT a -> BoolGT a intersection = intersectionBoolGT' (\x _ -> x) -- Note use of strict intersectionBoolGT' -- difference :: BoolGT a -> BoolGT a -> BoolGT a difference = differenceBoolGT -- isSubset :: BoolGT a -> BoolGT a -> Bool isSubset = isSubsetOfBoolGT -- lookup :: Monad m => Bool -> BoolGT a -> m a lookup k mp = case lookupBoolGT k mp of Just a -> return a Nothing -> fail "Data.Collections.Map.lookup: Key not found in BoolGT." -- alter :: (Maybe a -> Maybe a) -> Bool -> BoolGT a -> BoolGT a alter = alterBoolGT -- insertWith :: (a -> a -> a) -> Bool -> a -> BoolGT a -> BoolGT a insertWith f k a ogt = insertBoolGT (f a) k a ogt -- fromFoldableWith :: Foldable l (Bool,a) => (a -> a -> a) -> l -> BoolGT a fromFoldableWith f l = Coll.foldr insrt emptyBoolGT l -- Strictness ?? where insrt (k,a) ogt = insertBoolGT (f a) k a ogt -- foldGroups :: Foldable l (Bool,b) => (b -> a -> a) -> a -> l -> BoolGT a foldGroups f a0 l = Coll.foldr' insrt emptyBoolGT l where insrt (k,b) ogt = insertBoolGT (f b) k (f b a0) ogt -- mapWithKey :: (Bool -> a -> a) -> BoolGT a -> BoolGT a mapWithKey = mapWithKeyBoolGT -- unionWith :: (a -> a -> a) -> BoolGT a -> BoolGT a -> BoolGT a unionWith = unionBoolGT -- intersectionWith :: (a -> a -> a) -> BoolGT a -> BoolGT a -> BoolGT a intersectionWith = intersectionBoolGT -- differenceWith :: (a -> a -> Maybe a) -> BoolGT a -> BoolGT a -> BoolGT a differenceWith = differenceMaybeBoolGT -- isSubmapBy :: (a -> a -> Bool) -> BoolGT a -> BoolGT a -> Bool isSubmapBy = isSubmapOfBoolGT --------------------------