-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | API for collection data structures. -- -- This package provides classes for a consistent API to data structures. -- The behaviour of the interface is specified by QuickCheck properties. -- It is intended as an evolution of the API of the data structures in -- the containers package. @package collections-api @version 1.0.0.0 -- | Class of data structures that can be folded to a summary value. module Data.Collections.Foldable -- | Data structures that can be folded. -- -- Minimal complete definition: foldMap or foldr. -- -- For example, given a data type -- --
--   data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
--   
-- -- a suitable instance would be -- --
--   instance Foldable Tree
--      foldMap f Empty = mempty
--      foldMap f (Leaf x) = f x
--      foldMap f (Node l k r) = foldMap f l `mappend` f k `mappend` foldMap f r
--   
-- -- This is suitable even for abstract types, as the monoid is assumed to -- satisfy the monoid laws. class Foldable t a | t -> a fold :: (Foldable t a, Monoid a) => t -> a foldMap :: (Foldable t a, Monoid m) => (a -> m) -> t -> m foldr :: Foldable t a => (a -> b -> b) -> b -> t -> b foldl :: Foldable t a => (b -> a -> b) -> b -> t -> b foldr1 :: Foldable t a => (a -> a -> a) -> t -> a foldl1 :: Foldable t a => (a -> a -> a) -> t -> a null :: Foldable t a => t -> Bool size :: Foldable t a => t -> Int isSingleton :: Foldable t a => t -> Bool -- | Fold over the elements of a structure, associating to the right, but -- strictly. foldr' :: Foldable t a => (a -> b -> b) -> b -> t -> b -- | Fold over the elements of a structure, associating to the left, but -- strictly. foldl' :: Foldable t b => (a -> b -> a) -> a -> t -> a -- | Monadic fold over the elements of a structure, associating to the -- right, i.e. from right to left. foldrM :: (Foldable t a, Monad m) => (a -> b -> m b) -> b -> t -> m b -- | Monadic fold over the elements of a structure, associating to the -- left, i.e. from left to right. foldlM :: (Foldable t b, Monad m) => (a -> b -> m a) -> a -> t -> m a -- | Map each element of a structure to an action, evaluate these actions -- from left to right, and ignore the results. traverse_ :: (Foldable t a, Applicative f) => (a -> f b) -> t -> f () -- | for_ is traverse_ with its arguments flipped. for_ :: (Foldable t a, Applicative f) => t -> (a -> f b) -> f () -- | Evaluate each action in the structure from left to right, and ignore -- the results. sequenceA_ :: (Foldable t (f a), Applicative f) => t -> f () -- | The sum of a collection of actions, generalizing concat. asum :: (Foldable t (f a), Alternative f) => t -> f a -- | Map each element of a structure to a monadic action, evaluate these -- actions from left to right, and ignore the results. mapM_ :: (Foldable t a, Monad m) => (a -> m b) -> t -> m () -- | forM_ is mapM_ with its arguments flipped. forM_ :: (Foldable t a, Monad m) => t -> (a -> m b) -> m () -- | Evaluate each monadic action in the structure from left to right, and -- ignore the results. sequence_ :: (Foldable t (m a), Monad m) => t -> m () -- | The sum of a collection of actions, generalizing concat. msum :: (Foldable t (m a), MonadPlus m) => t -> m a -- | List of elements of a structure. toList :: Foldable t a => t -> [a] -- | and returns the conjunction of a container of Bools. For the -- result to be True, the container must be finite; False, -- however, results from a False value finitely far from the left -- end. and :: Foldable t Bool => t -> Bool -- | or returns the disjunction of a container of Bools. For the -- result to be False, the container must be finite; True, -- however, results from a True value finitely far from the left -- end. or :: Foldable t Bool => t -> Bool -- | Determines whether any element of the structure satisfies the -- predicate. any :: Foldable t a => (a -> Bool) -> t -> Bool -- | Determines whether all elements of the structure satisfy the -- predicate. all :: Foldable t a => (a -> Bool) -> t -> Bool -- | The sum function computes the sum of the numbers of a -- structure. sum :: (Foldable t a, Num a) => t -> a -- | The product function computes the product of the numbers of a -- structure. product :: (Foldable t a, Num a) => t -> a -- | The largest element of the structure. maximum :: (Foldable t a, Ord a) => t -> a -- | The largest element of a non-empty structure with respect to the given -- comparison function. maximumBy :: Foldable t a => (a -> a -> Ordering) -> t -> a -- | The least element of a non-null structure. minimum :: (Foldable t a, Ord a) => t -> a -- | The least element of a non-empty structure with respect to the given -- comparison function. minimumBy :: Foldable t a => (a -> a -> Ordering) -> t -> a -- | Does the element occur in the structure? elem :: (Foldable t a, Eq a) => a -> t -> Bool -- | notElem is the negation of elem. notElem :: (Foldable t a, Eq a) => a -> t -> Bool -- | The find function takes a predicate and a structure and returns -- the leftmost element of the structure matching the predicate, or -- Nothing if there is no such element. find :: Foldable t a => (a -> Bool) -> t -> Maybe a instance Ix i => Foldable (Array i a) (i, a) instance Foldable [a] a instance Foldable (Maybe a) a -- | This module defines a class framework for collection types. It -- provides: -- -- -- -- Should you need a more precise documentation, -- Data.Collections.Properties lists laws that implementations are -- entitled to assume. -- -- The classes defined in this module are intended to give hints about -- performance. eg. if a function has a Map c k v -- context, this indicates that the function will perform better if -- c has an efficitent lookup function. -- -- This class framework is based on ideas found in Simon Peyton Jones, -- "Bulk types with class". -- http://research.microsoft.com/Users/simonpj/Papers/collections.ps.gz -- -- Another inspiration source are the examples of MPTC and fuctional -- dependencies in Oleg Kiselyov's many articles posted to the haskell -- mailing list. -- -- This module name-clashes with a lot of Prelude functions, subsuming -- those. The user is encouraged to import Prelude hiding the clashing -- functions. Alternatively, it can be imported qualified. module Data.Collections -- | Class of collection with unobservable elements. It is the dual of the -- Foldable class. class Unfoldable c i | c -> i insert :: Unfoldable c i => i -> c -> c empty :: Unfoldable c i => c singleton :: Unfoldable c i => i -> c insertMany :: (Unfoldable c i, Foldable c' i) => c' -> c -> c insertManySorted :: (Unfoldable c i, Foldable c' i) => c' -> c -> c -- | Class of collection types. class (Foldable c a, Unfoldable c a) => Collection c a | c -> a filter :: Collection c a => (a -> Bool) -> c -> c class Collection c o => SortingCollection c o minView :: SortingCollection c o => c -> Maybe (o, c) -- | Class of map-like types. (aka. for sparse associative types). -- -- In opposition of Indexed, Map supports unexisting value for some -- indices. class Monoid c => Map c k a | c -> k a delete :: Map c k a => k -> c -> c member :: Map c k a => k -> c -> Bool union :: Map c k a => c -> c -> c intersection :: Map c k a => c -> c -> c difference :: Map c k a => c -> c -> c isSubset :: Map c k a => c -> c -> Bool isProperSubset :: Map c k a => c -> c -> Bool lookup :: Map c k a => k -> c -> Maybe a alter :: Map c k a => (Maybe a -> Maybe a) -> k -> c -> c insertWith :: Map c k a => (a -> a -> a) -> k -> a -> c -> c fromFoldableWith :: (Map c k a, Foldable l (k, a)) => (a -> a -> a) -> l -> c foldGroups :: (Map c k a, Foldable l (k, b)) => (b -> a -> a) -> a -> l -> c mapWithKey :: Map c k a => (k -> a -> a) -> c -> c unionWith :: Map c k a => (a -> a -> a) -> c -> c -> c intersectionWith :: Map c k a => (a -> a -> a) -> c -> c -> c differenceWith :: Map c k a => (a -> a -> Maybe a) -> c -> c -> c isSubmapBy :: Map c k a => (a -> a -> Bool) -> c -> c -> Bool isProperSubmapBy :: Map c k a => (a -> a -> Bool) -> c -> c -> Bool -- | The expression (lookupWithDefault def k map) returns -- the value at key k or returns def when the key is -- not in the map. lookupWithDefault :: Map c k a => a -> k -> c -> a -- | Union of many (key) sets, with combining function unionsWith :: (Unfoldable s i, Map s k a, Foldable cs s) => (a -> a -> a) -> cs -> s -- | Same as intersectionWith, but with a more general type. intersectionWith' :: (Functor m, Map (m (O a b c)) k (O a b c)) => (a -> b -> c) -> m a -> m b -> m c -- | Same as differenceWith, but with a more general type. differenceWith' :: (Functor m, Map (m (O a b c)) k (O a b c)) => (a -> b -> Maybe c) -> m a -> m b -> m c mapWithKey' :: (Functor m, Map (m (Either a b)) k (Either a b)) => (k -> a -> b) -> m a -> m b -- | Infix version of index, with arguments swapped. (!) :: Indexed c k v => c -> k -> v -- | Class for set-like collection types. A set is really a map with no -- value associated to the keys, so Set just states so. class Map c k () => Set c k | c -> k haddock_candy :: Set c k => c -> k -- | Union of many (key) sets. unions :: (Unfoldable s i, Map s k a, Foldable cs s) => cs -> s -- | Tells whether a key is not a member of the keySet. notMember :: Map c k a => k -> c -> Bool -- | Infix version of difference. Difference of two (key) sets. (\\) :: Map c k a => c -> c -> c -- | Class of sequential-access types. In addition of the Collection -- services, it provides deconstruction and concatenation. class (Monoid c, Collection c a) => Sequence c a take :: Sequence c a => Int -> c -> c drop :: Sequence c a => Int -> c -> c splitAt :: Sequence c a => Int -> c -> (c, c) reverse :: Sequence c a => c -> c front :: Sequence c a => c -> Maybe (a, c) back :: Sequence c a => c -> Maybe (c, a) cons :: Sequence c a => a -> c -> c snoc :: Sequence c a => c -> a -> c isPrefix :: (Sequence c a, Eq a) => c -> c -> Bool head :: Sequence s a => s -> a tail :: Sequence s a => s -> s -- | Concatenate two sequences. append :: Sequence c a => c -> c -> c -- | The concatenation of all the elements of a container of sequences. concat :: (Sequence s a, Foldable t s) => t -> s -- | Map a function over all the elements of a container and concatenate -- the resulting sequences. concatMap :: (Sequence s b, Foldable t a) => (a -> s) -> t -> s -- | Infix version of cons: add an element to the left end of a -- sequence. Mnemonic: a triangle with the single element at the pointy -- end. (<|) :: Sequence c i => i -> c -> c -- | Infix version of snoc: add an element to the right end of a -- sequence. Mnemonic: a triangle with the single element at the pointy -- end. (|>) :: Sequence c i => c -> i -> c -- | Infix verion of append. Concatenate two sequences. (><) :: Sequence c a => c -> c -> c class (Ix k, Foldable c (k, v), Indexed c k v) => Array c k v | c -> k v bounds :: Array c k v => c -> (k, k) array :: (Array c k v, Foldable l (k, v)) => (k, k) -> l -> c -- | Class of indexed types. The collection is dense: there is no -- way to remove an element nor for lookup to return not -- found. -- -- In practice however, most shallow collection types will instanciate -- this class in addition of Map, and leave the responsibility of -- failure to the caller. class Indexed c k v | c -> k v index :: Indexed c k v => k -> c -> v adjust :: Indexed c k v => (v -> v) -> k -> c -> c inDomain :: Indexed c k v => k -> c -> Bool (//) :: (Indexed c k v, Foldable l (k, v)) => c -> l -> c accum :: (Indexed c k v, Foldable l (k, v')) => (v -> v' -> v) -> c -> l -> c -- | Conversion from a Foldable to a Collection. fromFoldable :: (Foldable f a, Collection c' a) => f -> c' -- | Conversion from a Foldable to a Collection, with the unchecked -- precondition that the input is sorted fromAscFoldable :: (Foldable f a, Collection c' a) => f -> c' -- | Converts a list into a collection. fromList :: Collection c a => [a] -> c -- | Specialized version of fromFoldableWith for lists. fromListWith :: Map c k a => (a -> a -> a) -> [(k, a)] -> c -- | Converts a list into a collection, with the precondition that the -- input is sorted. fromAscList :: Collection c a => [a] -> c -- | View to the keys of a dictionnary newtype KeysView m k v KeysView :: m -> KeysView m k v fromKeysView :: KeysView m k v -> m -- | View to the elements of a dictionnary newtype ElemsView m k v ElemsView :: m -> ElemsView m k v fromElemsView :: ElemsView m k v -> m withKeys :: Collection m (k, v) => T (KeysView m k v) -> T m withElems :: Collection m (k, v) => T (ElemsView m k v) -> T m instance Map m k v => Map (KeysView m k v) k v instance (Monoid m, Map m k v) => Monoid (KeysView m k v) instance Unfoldable m (k, v) => Unfoldable (ElemsView m k v) (k, v) instance Foldable m (k, v) => Foldable (ElemsView m k v) v instance Unfoldable m (k, v) => Unfoldable (KeysView m k v) (k, v) instance Foldable m (k, v) => Foldable (KeysView m k v) k -- | The purpose of this module is twofold: -- --
    --
  1. Check instances of the classes in the collection framework.
  2. --
  3. Give those classes more formal semantics.
  4. --
-- -- Therefore, this acts as a contract between the collections users and -- implementers. -- -- Each function in this module returns a list of (property_name, -- propterty) for a given class (or set of classes). Each function -- is parameterized on the type of the collection to check, so a value -- witnessing the type must be passed. This value is guaranteed not to be -- evaluated, so it can always be undefined. -- -- These properties allow to verify, with a high degree of confidence, -- that instances of the classes defined in Data.Collections -- satisfy the prescribed properties. -- -- You will note that properties depend on the Eq class. This -- means that -- -- module Data.Collections.Properties -- | unfoldable_properties returns the following properties: -- -- -- --
--   singleton a == insert a empty
--   
-- -- -- --
--   insertMany l c == Foldable.foldr insert c l
--   
-- -- -- --
--   insertManySorted l c == Foldable.foldr insert c l
--      where l = List.sort l0
--   
unfoldable_properties :: (Arbitrary c, Arbitrary a, Ord a, Show a, Show c, Eq c, Eq a, Unfoldable c a) => c -> [(Property, String)] -- | foldable_properties returns the following properties: -- -- -- --
--   size c == foldr (const (+1)) 0 c
--   
-- -- -- --
--   null c <==> all (const False) c
--   
-- -- -- --
--   isSingleton c <==> size c == 1
--   
-- -- -- --
--   c1 == c2 ==> elem x c1 == elem x c2 -- note that the order of folding is not enforced, and that the converse is not true
--   
foldable_properties :: (Arbitrary c, Arbitrary a, Show a, Show c, Eq c, Eq a, Foldable c a) => c -> [(Property, String)] -- | collection_properties returns the following properties: -- -- -- --
--   foldr insert empty c == c
--   
-- -- -- --
--   null empty
--   
-- -- -- --
--   a `elem` (insert a c)                                 -- insert puts the element in the collection
--   
-- -- -- --
--   a /= a' ==> (a' `elem` c <==  a' `elem` (insert a c)) -- insert does not insert other elements
--   
-- -- -- --
--   let c' = insert a c in x `elem` c && y `elem` c ==> x `elem` c' || y `elem` c' -- insert alters at most one element
--   
-- -- -- --
--   (a `elem` filter f c) <==> ((a `elem` c) && f a)
--   
collection_properties :: (CoArbitrary i, Arbitrary c, Arbitrary i, Show i, Show c, Eq c, Eq i, Collection c i) => c -> [(Property, String)] -- | map_properties returns the following properties: -- -- -- --
--   lookup k (alter f k m) == f (lookup k m)
--   
-- -- -- --
--   lookup k (mapWithKey f m) == fmap (f k) (lookup k m)
--   
-- -- -- --
--   lookup k (unionWith f m1 m2) == case (lookup k m1, lookup k m2) of
--      (Nothing,Nothing) -> Nothing
--      (Just x, Nothing) -> Just x
--      (Nothing,Just x)  -> Just x
--      (Just x, Just y)  -> Just (f x y)
--   
-- -- -- --
--   lookup k (intersectionWith f m1 m2) == case (lookup k m1, lookup k m2) of
--      (Just x, Just y) -> Just (f x y)
--      _ -> Nothing
--   
-- -- -- --
--   lookup k (differenceWith f m1 m2) == case (lookup k m1, lookup k m2) of
--      (Just x, Nothing) -> Just x
--      (Just x, Just y)  -> f x y
--      (Nothing, _)      -> Nothing
--   
-- -- -- --
--   isSubmapBy f m1 m2 <==> differenceWith (\x y->if f x y then Nothing else Just v) m1 m2 == mempty
--   
-- -- -- --
--   isProperSubmapBy f m1 m2 <==> isSubmapBy f m1 m2 && m1 /= m2
--   
-- -- -- --
--   insertWith f k a m == alter (\x -> Just $ case x of {Nothing->a;Just a' -> f a a'}) k m
--   
-- -- -- --
--   fromFoldableWith f l == foldr (uncurry (insertWith f)) mempty l
--   
-- -- -- --
--   delete k m == alter (const Nothing) k m
--   
-- -- -- --
--   member k m <==> isJust (lookup k m)
--   
-- -- -- --
--   union m1 m2 == unionWith const m1 m2
--   
-- -- -- --
--   intersection m1 m2 == intersectionWith const m1 m2 
--   
-- -- -- --
--   difference m1 m2 == differenceWith (\_ _ -> Nothing) m1 m2
--   
-- -- -- --
--   isSubset m1 m2 <==> isSubmapBy (\_ _ -> True) m1 m2
--   
-- -- -- --
--   isProperSubset m1 m2 <==> isProperSubmapBy (\_ _ -> True) m1 m2
--   
-- -- -- --
--   lookup k mempty == Nothing
--   
-- -- -- --
--   mappend m1 m2 == union m1 m2
--   
-- -- -- --
--   c1 == c2 ==> lookup x c1 == lookup x c2 -- should really be: c1 == c2 <==> forall x. lookup x c1 == lookup x c2
--   
map_properties :: (CoArbitrary v, CoArbitrary k, Arbitrary m, Arbitrary k, Arbitrary v, Show k, Show v, Show m, Eq m, Eq v, Map m k v) => m -> [(Property, String)] -- | map_unfold_properties returns the following properties: -- -- -- --
--   mempty == empty
--   
-- -- -- --
--   insert (k,v) m == insertWith (\x _ -> x) k v m
--   
map_unfold_properties :: (Arbitrary m, Arbitrary k, Arbitrary v, Show k, Show v, Show m, Eq m, Eq v, Eq k, Map m k v, Collection m (k, v)) => m -> [(Property, String)] -- | set_unfold_properties returns the following properties: -- -- -- --
--   mempty == empty
--   
-- -- -- --
--   insert k m == insertWith (\x _->x) k () m
--   
set_unfold_properties :: (Arbitrary m, Arbitrary k, Show k, Show m, Eq m, Eq k, Map m k (), Unfoldable m k) => m -> [(Property, String)] -- | map_fold_properties returns the following properties: -- -- -- --
--   maybeToList (lookup k m) == map snd (List.filter ((== k) . fst) (toList m))
--   
-- -- -- --
--   sizeExcept (alter f k m) == sizeExcept m
--     where sizeExcept m = size m - maybe 0 (const 1) (lookup k m)
--   
map_fold_properties :: (CoArbitrary v, Arbitrary m, Arbitrary k, Arbitrary v, Show k, Show v, Show m, Eq m, Eq v, Eq k, Map m k v, Collection m (k, v)) => m -> [(Property, String)] -- | set_fold_properties returns the following properties: -- -- -- --
--   maybeToList (lookup k m) == map (const ()) (List.filter (== k) (toList m))
--   
-- -- -- --
--   sizeExcept (alter f k m) == sizeExcept m
--     where sizeExcept m = size m - maybe 0 (const 1) (lookup k m)
--   
set_fold_properties :: (Arbitrary m, Arbitrary k, Show k, Show m, Eq m, Eq k, Map m k (), Foldable m k) => m -> [(Property, String)] -- | indexed_map_properties returns the following properties: -- -- -- --
--   k `inDomain` m <==> k `member` m
--   
-- -- -- --
--   case lookup k m of {Just x -> x == index k m; _ -> True}
--   
indexed_map_properties :: (Arbitrary m, Arbitrary k, Arbitrary v, Show k, Show v, Show m, Eq m, Eq v, Map m k v, Indexed m k v) => m -> [(Property, String)] -- | sequence_properties returns the following properties: -- -- -- --
--   foldMap f empty == mempty
--   
-- -- -- --
--   foldMap f (x <| s) == f x `mappend` foldMap f s
--   
-- -- -- --
--   foldMap f (s |> x) == foldMap f s `mappend` f x
--   
-- -- -- --
--   foldMap f (s >< t) == foldMap f s `mappend` foldMap f t
--   
-- -- -- --
--   front empty == Nothing
--   
-- -- -- --
--   front (x <| s) == Just (x,s)
--   
-- -- -- --
--   front (s |> x) == case front s of {Nothing -> Just (x, empty); Just (x',s') -> Just (x', s' |> x)}
--   
-- -- -- --
--   front (s >< t) == case front s of {Nothing -> front t;         Just (x',s') -> Just (x', s' >< t)}
--   
-- -- -- --
--   back empty == Nothing
--   
-- -- -- --
--   back (s |> x) == Just (s,x)
--   
-- -- -- --
--   back (x <| s) == case back s of {Nothing -> Just (empty, x); Just (s',x') -> Just (x <| s', x')}
--   
-- -- -- --
--   back (t >< s) == case back s of {Nothing -> back t;          Just (s',x') -> Just (t >< s', x')}
--   
-- -- -- --
--   drop 0     s == s
--   
-- -- -- --
--   n>0 ==> drop (n+1) s == case front (drop n s) of Nothing -> empty; Just (_,s') -> s'
--   
-- -- -- --
--   take 0     s == empty
--   
-- -- -- --
--   n>0 ==> take (n+1) s == case front s of Nothing -> empty; Just (x,s') -> x <| take n s'
--   
-- -- -- --
--   foldMap f (reverse s) == getDual (foldMap (Dual . f) s)
--   
-- -- -- --
--   mempty == empty
--   
-- -- -- --
--   s1 == s2 ==> foldMap f s1 == foldMap f s2
--   
sequence_properties :: (Arbitrary s, Arbitrary a, Show s, Show a, Eq s, Eq a, Sequence s a) => s -> [(Property, String)] -- | indexed_properties returns the following properties: -- -- -- --
--   k `inDomain` m ==> index k (adjust f k m) == f (index k m)
--   
indexed_properties :: (CoArbitrary v, Arbitrary m, Arbitrary k, Arbitrary v, Show k, Show v, Show m, Eq m, Eq v, Indexed m k v) => m -> [(Property, String)] -- | indexed_sequence_properties returns the following properties: -- -- -- --
--   k `inDomain` s <==> k >= 0 && k < size s
--   
-- -- -- --
--   k `inDomain` s ==> index (k+1)      (x <| s) == index k s
--   
-- -- -- --
--   index 0          (x <| s) == x
--   
-- -- -- --
--   k `inDomain` s ==> index k          (s |> x) == index k s
--   
-- -- -- --
--   index (size s)   (s |> x) == x
--   
-- -- -- --
--   k `inDomain` t ==> index (k+size s) (s >< t) == index k t
--   
-- -- -- --
--   k `inDomain` s ==> index k          (s >< t) == index k s
--   
indexed_sequence_properties :: (Arbitrary s, Arbitrary a, Show s, Show a, Eq s, Eq a, Sequence s a, Indexed s Int a) => s -> [(Property, String)] instance Show (a -> b)