[commit: containers] master: Define deepseq instances; part of #5468 (6d68ed7)
Ian Lynagh
igloo at earth.li
Thu Sep 8 15:52:10 CEST 2011
Repository : ssh://darcs.haskell.org//srv/darcs/packages/containers
On branch : master
http://hackage.haskell.org/trac/ghc/changeset/6d68ed7f7b72ca925a06eab63a83d765b4aaf404
>---------------------------------------------------------------
commit 6d68ed7f7b72ca925a06eab63a83d765b4aaf404
Author: Ian Lynagh <igloo at earth.li>
Date: Thu Sep 8 12:19:19 2011 +0100
Define deepseq instances; part of #5468
>---------------------------------------------------------------
Data/IntMap.hs | 6 ++++++
Data/IntSet.hs | 10 ++++++++++
Data/Map.hs | 5 +++++
Data/Set.hs | 9 +++++++++
Data/Tree.hs | 4 ++++
containers.cabal | 2 +-
6 files changed, 35 insertions(+), 1 deletions(-)
diff --git a/Data/IntMap.hs b/Data/IntMap.hs
index aef81cd..b214d90 100644
--- a/Data/IntMap.hs
+++ b/Data/IntMap.hs
@@ -196,6 +196,7 @@ import qualified Data.Foldable as Foldable
import Data.Traversable (Traversable(traverse))
import Control.Applicative (Applicative(pure,(<*>)),(<$>))
import Control.Monad ( liftM )
+import Control.DeepSeq (NFData(rnf))
{-
-- just for testing
import qualified Prelude
@@ -306,6 +307,11 @@ instance Traversable IntMap where
traverse f (Tip k v) = Tip k <$> f v
traverse f (Bin p m l r) = Bin p m <$> traverse f l <*> traverse f r
+instance NFData a => NFData (IntMap a) where
+ rnf Nil = ()
+ rnf (Tip _ v) = rnf v
+ rnf (Bin _ _ l r) = rnf l `seq` rnf r
+
#if __GLASGOW_HASKELL__
{--------------------------------------------------------------------
diff --git a/Data/IntSet.hs b/Data/IntSet.hs
index 5679cfc..f19243f 100644
--- a/Data/IntSet.hs
+++ b/Data/IntSet.hs
@@ -132,6 +132,7 @@ import qualified Data.List as List
import Data.Monoid (Monoid(..))
import Data.Maybe (fromMaybe)
import Data.Typeable
+import Control.DeepSeq (NFData)
#if __GLASGOW_HASKELL__
import Text.Read
@@ -890,6 +891,15 @@ instance Read IntSet where
INSTANCE_TYPEABLE0(IntSet,intSetTc,"IntSet")
{--------------------------------------------------------------------
+ NFData
+--------------------------------------------------------------------}
+
+-- The IntSet constructors consist only of strict fields of Ints and
+-- IntSets, thus the default NFData instance which evaluates to whnf
+-- should suffice
+instance NFData IntSet
+
+{--------------------------------------------------------------------
Debugging
--------------------------------------------------------------------}
-- | /O(n)/. Show the tree that implements the set. The tree is shown
diff --git a/Data/Map.hs b/Data/Map.hs
index faac867..73b50d0 100644
--- a/Data/Map.hs
+++ b/Data/Map.hs
@@ -223,6 +223,7 @@ import Control.Applicative (Applicative(..), (<$>))
import Data.Traversable (Traversable(traverse))
import qualified Data.Foldable as Foldable
import Data.Typeable
+import Control.DeepSeq (NFData(rnf))
#if __GLASGOW_HASKELL__
import Text.Read
@@ -2468,6 +2469,10 @@ instance Foldable.Foldable (Map k) where
foldMap _ Tip = mempty
foldMap f (Bin _ _ v l r) = Foldable.foldMap f l `mappend` f v `mappend` Foldable.foldMap f r
+instance (NFData k, NFData a) => NFData (Map k a) where
+ rnf Tip = ()
+ rnf (Bin _ kx x l r) = rnf kx `seq` rnf x `seq` rnf l `seq` rnf r
+
{--------------------------------------------------------------------
Read
--------------------------------------------------------------------}
diff --git a/Data/Set.hs b/Data/Set.hs
index 37ce876..881e0d6 100644
--- a/Data/Set.hs
+++ b/Data/Set.hs
@@ -142,6 +142,7 @@ import qualified Data.List as List
import Data.Monoid (Monoid(..))
import qualified Data.Foldable as Foldable
import Data.Typeable
+import Control.DeepSeq (NFData(rnf))
{-
-- just for testing
@@ -747,6 +748,14 @@ instance (Read a, Ord a) => Read (Set a) where
INSTANCE_TYPEABLE1(Set,setTc,"Set")
{--------------------------------------------------------------------
+ NFData
+--------------------------------------------------------------------}
+
+instance NFData a => NFData (Set a) where
+ rnf Tip = ()
+ rnf (Bin _ y l r) = rnf y `seq` rnf l `seq` rnf r
+
+{--------------------------------------------------------------------
Utility functions that return sub-ranges of the original
tree. Some functions take a `Maybe value` as an argument to
allow comparisons against infinite values. These are called `blow`
diff --git a/Data/Tree.hs b/Data/Tree.hs
index 40de39a..4f00b0d 100644
--- a/Data/Tree.hs
+++ b/Data/Tree.hs
@@ -35,6 +35,7 @@ import Data.Sequence (Seq, empty, singleton, (<|), (|>), fromList,
import Data.Foldable (Foldable(foldMap), toList)
import Data.Traversable (Traversable(traverse))
import Data.Typeable
+import Control.DeepSeq (NFData(rnf))
#ifdef __GLASGOW_HASKELL__
import Data.Data (Data)
@@ -74,6 +75,9 @@ instance Traversable Tree where
instance Foldable Tree where
foldMap f (Node x ts) = f x `mappend` foldMap (foldMap f) ts
+instance NFData a => NFData (Tree a) where
+ rnf (Node x ts) = rnf x `seq` rnf ts
+
-- | Neat 2-dimensional drawing of a tree.
drawTree :: Tree String -> String
drawTree = unlines . draw
diff --git a/containers.cabal b/containers.cabal
index 80d9d40..94390ce 100644
--- a/containers.cabal
+++ b/containers.cabal
@@ -20,7 +20,7 @@ source-repository head
location: http://github.com/haskell/containers.git
Library {
- build-depends: base >= 4.2 && < 6, array
+ build-depends: base >= 4.2 && < 6, array, deepseq >= 1.2 && < 1.3
ghc-options: -O2
if impl(ghc>6.10)
Ghc-Options: -fregs-graph
More information about the Cvs-libraries
mailing list