Ticket #5468: 0004-Containers-Implement-NFData-instances-for-container-types.patch

File 0004-Containers-Implement-NFData-instances-for-container-types.patch, 5.6 KB (added by hvr, 4 years ago)
  • Data/IntMap.hs

    From ecca0380a5012528dbd4ab7dc68f81558544f326 Mon Sep 17 00:00:00 2001
    From: Herbert Valerio Riedel <[email protected]>
    Date: Wed, 7 Sep 2011 09:57:54 +0200
    Subject: [PATCH] Implement NFData instances for container types
    
    This adds a dependancy on the `deepseq` package and implements `NFData`
    instances for `Map`, `IntMap`, `Set`, `IntSet` and `Tree`. Also the package
    version is bumped to `0.4.0.0` since instances are added.
    
    See #5468 for more information about this change.
    ---
     Data/IntMap.hs   |    6 ++++++
     Data/IntSet.hs   |   10 ++++++++++
     Data/Map.hs      |    5 +++++
     Data/Set.hs      |    9 +++++++++
     Data/Tree.hs     |    4 ++++
     containers.cabal |    4 ++--
     6 files changed, 36 insertions(+), 2 deletions(-)
    
    diff --git a/Data/IntMap.hs b/Data/IntMap.hs
    index aef81cd..b214d90 100644
    a b import qualified Data.Foldable as Foldable 
    196196import Data.Traversable (Traversable(traverse))
    197197import Control.Applicative (Applicative(pure,(<*>)),(<$>))
    198198import Control.Monad ( liftM )
     199import Control.DeepSeq (NFData(rnf))
    199200{-
    200201-- just for testing
    201202import qualified Prelude
    instance Traversable IntMap where 
    306307    traverse f (Tip k v) = Tip k <$> f v
    307308    traverse f (Bin p m l r) = Bin p m <$> traverse f l <*> traverse f r
    308309
     310instance NFData a => NFData (IntMap a) where
     311    rnf Nil = ()
     312    rnf (Tip _ v) = rnf v
     313    rnf (Bin _ _ l r) = rnf l `seq` rnf r
     314
    309315#if __GLASGOW_HASKELL__
    310316
    311317{--------------------------------------------------------------------
  • Data/IntSet.hs

    diff --git a/Data/IntSet.hs b/Data/IntSet.hs
    index 5679cfc..624ba66 100644
    a b import qualified Data.List as List 
    132132import Data.Monoid (Monoid(..))
    133133import Data.Maybe (fromMaybe)
    134134import Data.Typeable
     135import Control.DeepSeq (NFData(rnf))
    135136
    136137#if __GLASGOW_HASKELL__
    137138import Text.Read
    instance Read IntSet where 
    890891INSTANCE_TYPEABLE0(IntSet,intSetTc,"IntSet")
    891892
    892893{--------------------------------------------------------------------
     894  NFData
     895--------------------------------------------------------------------}
     896
     897-- The IntSet constructors consist only of strict fields of Ints and
     898-- IntSets, thus the default NFData instance which evaluates to whnf
     899-- should suffice
     900instance NFData IntSet
     901
     902{--------------------------------------------------------------------
    893903  Debugging
    894904--------------------------------------------------------------------}
    895905-- | /O(n)/. Show the tree that implements the set. The tree is shown
  • Data/Map.hs

    diff --git a/Data/Map.hs b/Data/Map.hs
    index faac867..73b50d0 100644
    a b import Control.Applicative (Applicative(..), (<$>)) 
    223223import Data.Traversable (Traversable(traverse))
    224224import qualified Data.Foldable as Foldable
    225225import Data.Typeable
     226import Control.DeepSeq (NFData(rnf))
    226227
    227228#if __GLASGOW_HASKELL__
    228229import Text.Read
    instance Foldable.Foldable (Map k) where 
    24682469  foldMap _ Tip = mempty
    24692470  foldMap f (Bin _ _ v l r) = Foldable.foldMap f l `mappend` f v `mappend` Foldable.foldMap f r
    24702471
     2472instance (NFData k, NFData a) => NFData (Map k a) where
     2473    rnf Tip = ()
     2474    rnf (Bin _ kx x l r) = rnf kx `seq` rnf x `seq` rnf l `seq` rnf r
     2475
    24712476{--------------------------------------------------------------------
    24722477  Read
    24732478--------------------------------------------------------------------}
  • Data/Set.hs

    diff --git a/Data/Set.hs b/Data/Set.hs
    index 37ce876..881e0d6 100644
    a b import qualified Data.List as List 
    142142import Data.Monoid (Monoid(..))
    143143import qualified Data.Foldable as Foldable
    144144import Data.Typeable
     145import Control.DeepSeq (NFData(rnf))
    145146
    146147{-
    147148-- just for testing
    instance (Read a, Ord a) => Read (Set a) where 
    747748INSTANCE_TYPEABLE1(Set,setTc,"Set")
    748749
    749750{--------------------------------------------------------------------
     751  NFData
     752--------------------------------------------------------------------}
     753
     754instance NFData a => NFData (Set a) where
     755    rnf Tip           = ()
     756    rnf (Bin _ y l r) = rnf y `seq` rnf l `seq` rnf r
     757
     758{--------------------------------------------------------------------
    750759  Utility functions that return sub-ranges of the original
    751760  tree. Some functions take a `Maybe value` as an argument to
    752761  allow comparisons against infinite values. These are called `blow`
  • Data/Tree.hs

    diff --git a/Data/Tree.hs b/Data/Tree.hs
    index 40de39a..4f00b0d 100644
    a b import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList, 
    3535import Data.Foldable (Foldable(foldMap), toList)
    3636import Data.Traversable (Traversable(traverse))
    3737import Data.Typeable
     38import Control.DeepSeq (NFData(rnf))
    3839
    3940#ifdef __GLASGOW_HASKELL__
    4041import Data.Data (Data)
    instance Traversable Tree where 
    7475instance Foldable Tree where
    7576    foldMap f (Node x ts) = f x `mappend` foldMap (foldMap f) ts
    7677
     78instance NFData a => NFData (Tree a) where
     79    rnf (Node x ts) = rnf x `seq` rnf ts
     80
    7781-- | Neat 2-dimensional drawing of a tree.
    7882drawTree :: Tree String -> String
    7983drawTree  = unlines . draw
  • containers.cabal

    diff --git a/containers.cabal b/containers.cabal
    index 80d9d40..fa30e5e 100644
    a b  
    11name: containers
    2 version: 0.4.1.0
     2version: 0.5.0.0
    33license: BSD3
    44license-file: LICENSE
    55maintainer: [email protected]
    source-repository head 
    2020    location: http://github.com/haskell/containers.git
    2121
    2222Library {
    23     build-depends: base >= 4.2 && < 6, array
     23    build-depends: base >= 4.2 && < 6, array, deepseq >= 1.3 && < 1.4
    2424    ghc-options: -O2
    2525    if impl(ghc>6.10)
    2626        Ghc-Options: -fregs-graph