{-# OPTIONS_GHC -fglasgow-exts -fno-warn-orphans #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Trie.General.CollectionsInstances.UnitGT -- 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 'UnitGT' type. ----------------------------------------------------------------------------- module Data.Trie.General.CollectionsInstances.UnitGT ( ) where import Data.Trie.General.UnitGT 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 (UnitGT ((),a)) ((),a) where -- fold :: Monoid ((),a) => UnitGT ((),a) -> ((),a) fold mp = foldrElemsAscendingUnitGT (\assoc b -> M.mappend assoc b) mp M.mempty -- foldMap :: Monoid m => (((),a) -> m) -> UnitGT ((),a) -> m foldMap f mp = foldrElemsAscendingUnitGT (\assoc b -> M.mappend (f assoc) b) mp M.mempty -- foldr :: (((),a) -> b -> b) -> b -> UnitGT ((),a) -> b foldr f b0 mp = foldrElemsAscendingUnitGT (\assoc b -> f assoc b) mp b0 -- foldl :: (b -> ((),a) -> b) -> b -> UnitGT ((),a) -> b foldl f b0 mp = foldrElemsDescendingUnitGT (\assoc b -> f b assoc) mp b0 {- ToDo: Implement properly. Meantime Foldable class has suitable defaults via lists. -- foldr1 :: (((),a) -> ((),a) -> ((),a)) -> UnitGT ((),a) -> ((),a) -- foldl1 :: (((),a) -> ((),a) -> ((),a)) -> UnitGT ((),a) -> ((),a) -} -- null :: UnitGT ((),a) -> Bool null = isEmptyUnitGT -- size :: UnitGT ((),a) -> Int size mp = ASINT(addSizeUnitGT mp L(0)) -- isSingleton :: UnitGT ((),a) -> Bool isSingleton = isSingletonUnitGT ------------------------------- --------------------------------- -- Data.Collections.Unfoldable -- --------------------------------- instance Coll.Unfoldable (UnitGT ((),a)) ((),a) where -- insert :: ((),a) -> UnitGT ((),a) -> UnitGT ((),a) insert assoc@(k,_) mp = insertUnitGT' (const assoc) k assoc mp -- Note use of strict insertUnitGT' -- empty :: UnitGT ((),a) empty = emptyUnitGT -- singleton :: ((),a) -> UnitGT ((),a) singleton assoc@(k,_) = singletonUnitGT k assoc -- insertMany :: Foldable c' ((),a) => c' -> UnitGT ((),a) -> UnitGT ((),a) insertMany c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertUnitGT' (const assoc) k assoc mp) mp0 c -- ?? stricness?? l/r?? -- insertManySorted :: Foldable c' ((),a) => c' -> UnitGT ((),a) -> UnitGT ((),a) insertManySorted c mp0 = Coll.foldr (\assoc@(k,_) mp -> insertUnitGT' (const assoc) k assoc mp) mp0 c -- How to implement efficiently ?? --------------------------------- --------------------------------- -- Data.Collections.Collection -- --------------------------------- instance Coll.Collection (UnitGT ((),a)) ((),a) where -- filter :: (((),a) -> Bool) -> UnitGT ((),a) -> UnitGT ((),a) filter = filterUnitGT --------------------------------- -------------------------- -- Data.Collections.Map -- -------------------------- instance (M.Monoid a) => Coll.Map (UnitGT a) () a where -- delete :: () -> UnitGT a -> UnitGT a delete = deleteUnitGT -- member :: () -> UnitGT a -> Bool member k mp = MB.isJust (lookupUnitGT k mp) -- union :: UnitGT a -> UnitGT a -> UnitGT a union = unionUnitGT' (\x _ -> x) -- Note use of strict unionUnitGT' -- intersection :: UnitGT a -> UnitGT a -> UnitGT a intersection = intersectionUnitGT' (\x _ -> x) -- Note use of strict intersectionUnitGT' -- difference :: UnitGT a -> UnitGT a -> UnitGT a difference = differenceUnitGT -- isSubset :: UnitGT a -> UnitGT a -> Bool isSubset = isSubsetOfUnitGT -- lookup :: Monad m => () -> UnitGT a -> m a lookup k mp = case lookupUnitGT k mp of Just a -> return a Nothing -> fail "Data.Collections.Map.lookup: Key not found in UnitGT." -- alter :: (Maybe a -> Maybe a) -> () -> UnitGT a -> UnitGT a alter = alterUnitGT -- insertWith :: (a -> a -> a) -> () -> a -> UnitGT a -> UnitGT a insertWith f k a ogt = insertUnitGT (f a) k a ogt -- fromFoldableWith :: Foldable l ((),a) => (a -> a -> a) -> l -> UnitGT a fromFoldableWith f l = Coll.foldr insrt emptyUnitGT l -- Strictness ?? where insrt (k,a) ogt = insertUnitGT (f a) k a ogt -- foldGroups :: Foldable l ((),b) => (b -> a -> a) -> a -> l -> UnitGT a foldGroups f a0 l = Coll.foldr' insrt emptyUnitGT l where insrt (k,b) ogt = insertUnitGT (f b) k (f b a0) ogt -- mapWithKey :: (() -> a -> a) -> UnitGT a -> UnitGT a mapWithKey = mapWithKeyUnitGT -- unionWith :: (a -> a -> a) -> UnitGT a -> UnitGT a -> UnitGT a unionWith = unionUnitGT -- intersectionWith :: (a -> a -> a) -> UnitGT a -> UnitGT a -> UnitGT a intersectionWith = intersectionUnitGT -- differenceWith :: (a -> a -> Maybe a) -> UnitGT a -> UnitGT a -> UnitGT a differenceWith = differenceMaybeUnitGT -- isSubmapBy :: (a -> a -> Bool) -> UnitGT a -> UnitGT a -> Bool isSubmapBy = isSubmapOfUnitGT --------------------------