strict-base-types 0.3.0 → 0.4.0
raw patch · 4 files changed
+62/−33 lines, 4 filesdep +hashabledep ~basedep ~bifunctors
Dependencies added: hashable
Dependency ranges changed: base, bifunctors
Files
- src/Data/Either/Strict.hs +13/−3
- src/Data/Maybe/Strict.hs +10/−4
- src/Data/Tuple/Strict.hs +24/−13
- strict-base-types.cabal +15/−13
src/Data/Either/Strict.hs view
@@ -40,7 +40,6 @@ import Prelude hiding (Either (..), either) import qualified Prelude as L -import Control.Applicative (pure, (<$>)) import Control.DeepSeq (NFData (..)) import Control.Lens.Iso (Strict (..), Swapped (..), iso) import Control.Lens.Prism (Prism, prism)@@ -48,18 +47,23 @@ import Data.Bifoldable (Bifoldable (..)) import Data.Bifunctor (Bifunctor (..)) import Data.Binary (Binary (..))-import Data.Foldable (Foldable (..))-import Data.Traversable (Traversable (..)) import Data.Bitraversable (Bitraversable (..)) #if MIN_VERSION_base(4,7,0) import Data.Data (Data (..), Typeable) #else import Data.Data (Data (..), Typeable2 (..)) #endif+#if !MIN_VERSION_base(4,8,0)+import Control.Applicative (pure, (<$>))+import Data.Foldable (Foldable (..))+import Data.Traversable (Traversable (..))+import Data.Monoid (Monoid (..))+#endif #if __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic (..)) #endif import Test.QuickCheck (Arbitrary (..))+import Data.Hashable (Hashable(..)) -- Utilities@@ -140,7 +144,9 @@ instance Bitraversable Either where bitraverse f _ (Left a) = fmap Left (f a) bitraverse _ g (Right b) = fmap Right (g b)+#if !MIN_VERSION_bifunctors(5,1,0) bisequenceA = either (fmap Left) (fmap Right)+#endif -- lens instance Strict (L.Either a b) (Either a b) where@@ -148,6 +154,10 @@ instance Swapped Either where swapped = either Right Left `iso` either Right Left++-- hashable+instance (Hashable a, Hashable b) => Hashable (Either a b) where+ hashWithSalt salt = hashWithSalt salt . toLazy -- missing functions --------------------
src/Data/Maybe/Strict.hs view
@@ -46,10 +46,11 @@ , _Nothing ) where +import Data.Strict.Maybe (Maybe (Nothing, Just), fromJust,+ fromMaybe, isJust, isNothing, maybe) import Prelude hiding (Maybe (..), maybe) import qualified Prelude as L -import Control.Applicative (pure, (<$>)) import Control.DeepSeq (NFData (..)) import Control.Lens.Iso (Strict (..), iso) import Control.Lens.Prism (Prism, Prism', prism, prism')@@ -60,15 +61,17 @@ #else import Data.Data (Data (..), Typeable1 (..)) #endif-import Data.Monoid (Monoid (..))+#if !MIN_VERSION_base(4,8,0)+import Control.Applicative (pure, (<$>)) import Data.Foldable (Foldable (..)) import Data.Traversable (Traversable (..))-import Data.Strict.Maybe (Maybe (Nothing, Just), fromJust,- fromMaybe, isJust, isNothing, maybe)+import Data.Monoid (Monoid (..))+#endif #if __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic (..)) #endif import Test.QuickCheck (Arbitrary (..))+import Data.Hashable (Hashable(..)) -- utilities@@ -135,6 +138,9 @@ instance Strict (L.Maybe a) (Maybe a) where strict = iso toStrict toLazy +-- hashable+instance Hashable a => Hashable (Maybe a) where+ hashWithSalt salt = hashWithSalt salt . toLazy -- | Analogous to 'L.listToMaybe' in "Data.Maybe". listToMaybe :: [a] -> Maybe a
src/Data/Tuple/Strict.hs view
@@ -38,7 +38,6 @@ import Prelude hiding (curry, fst, snd, uncurry, unzip, zip) -import Control.Applicative (Applicative ((<*>)), (<$>)) import Control.DeepSeq (NFData (..)) #if MIN_VERSION_lens(4,0,0)@@ -62,13 +61,22 @@ #else import Data.Data (Data (..), Typeable2 (..)) #endif+#if !MIN_VERSION_base(4,8,0)+import Control.Applicative (Applicative ((<*>)), (<$>))+import Data.Foldable (Foldable (..))+import Data.Traversable (Traversable (..)) import Data.Monoid (Monoid (..))-import qualified Data.Tuple as L () -- just for haddocks. Is there a better way?+#endif #if __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic (..)) #endif import Test.QuickCheck (Arbitrary (..))+import Data.Hashable (Hashable(..)) +#if __HADDOCK__+import Data.Tuple ()+#endif+ -- Utilities ------------ @@ -94,6 +102,15 @@ deriving instance Generic (Pair a b) #endif +instance Functor (Pair e) where+ fmap f = toStrict . fmap f . toLazy++instance Foldable (Pair e) where+ foldMap f (_ :!: x) = f x++instance Traversable (Pair e) where+ traverse f (e :!: x) = (:!:) e <$> f x+ instance (Monoid a, Monoid b) => Monoid (Pair a b) where mempty = mempty :!: mempty (x1 :!: y1) `mappend` (x2 :!: y2) = (x1 `mappend` x2) :!: (y1 `mappend` y2)@@ -133,7 +150,9 @@ instance Bitraversable Pair where bitraverse f g (a :!: b) = (:!:) <$> f a <*> g b+#if !MIN_VERSION_bifunctors(5,1,0) bisequenceA (a :!: b) = (:!:) <$> a <*> b+#endif -- lens instance Strict (a, b) (Pair a b) where@@ -161,14 +180,9 @@ {-# INLINE each #-} #endif -{- To be added once they make it to base--instance Foldable (Pair e) where- foldMap f (_,x) = f x--instance Traversable (Pair e) where- traverse f (e,x) = (,) e <$> f x--}+-- hashable+instance (Hashable a, Hashable b) => Hashable (Pair a b) where+ hashWithSalt salt = hashWithSalt salt . toLazy -- missing functions@@ -204,9 +218,6 @@ toStrict (a, b) = a :!: b toLazy (a :!: b) = (a, b)--instance Functor (Pair e) where- fmap f = toStrict . fmap f . toLazy -- | Extract the first component of a strict pair. fst :: Pair a b -> a
strict-base-types.cabal view
@@ -1,5 +1,5 @@ Name: strict-base-types-Version: 0.3.0+Version: 0.4.0 Synopsis: Strict variants of the types provided in base. Category: Data Description:@@ -17,12 +17,12 @@ 1. Make all fields of every constructor strict; i.e., add a bang to all fields. .- 2. Use only strict types for the fields of the constructors. + 2. Use only strict types for the fields of the constructors. . The second requirement is problematic as it rules out the use of the standard Haskell 'Maybe', 'Either', and pair types. This library solves this problem by providing strict variants of these types and their- corresponding standard support functions and type-class instances. + corresponding standard support functions and type-class instances. . Note that this library does currently not provide fully strict lists. They can be added if they are really required. However, in many cases one@@ -40,30 +40,30 @@ It is used in the following example to simplify the modification of strict fields. .- > (-# LANGUAGE TemplateHaskell #-) -- replace with curly braces, + > (-# LANGUAGE TemplateHaskell #-) -- replace with curly braces, > (-# LANGUAGE OverloadedStrings #-) -- the Haddock prologues are a P.I.T.A!- > + > > import Control.Lens ( (.=), Strict(strict), from, Iso', makeLenses) > import Control.Monad.State.Strict (State) > import qualified Data.Map as M > import qualified Data.Maybe.Strict as S > import qualified Data.Text as T- > + > > -- | An example of a state record as it could be used in a (very minimal) > -- role-playing game. > data GameState = GameState > ( _gsCooldown :: !(S.Maybe Int) > , _gsHealth :: !Int > ) -- replace with curly braces, *grmbl*- > + > > makeLenses ''GameState- > + > > -- The isomorphism, which converts a strict field to its lazy variant > lazy :: Strict lazy strict => Iso' strict lazy > lazy = from strict- > + > > type Game = State GameState- > + > > cast :: T.Text -> Game () > cast spell = > gsCooldown.lazy .= M.lookup spell spellDuration@@ -81,7 +81,7 @@ . - only provides the fully strict variants of types from 'base', .- - is in-sync with the current base library (base-4.6), + - is in-sync with the current base library (base-4.6), . - provides the missing instances for (future) Haskell platform packages, and .@@ -100,19 +100,21 @@ Homepage: https://github.com/meiersi/strict-base-types Cabal-Version: >= 1.6 Build-type: Simple+Tested-with: GHC==7.4.2, GHC==7.6.3, GHC==7.8.4, GHC==7.10.2 source-repository head type: git location: https://github.com/meiersi/strict-base-types.git library- build-depends: + build-depends: base >= 4.5 && < 5 , lens >= 3.9 , QuickCheck >= 2 , aeson >= 0.6 , binary >= 0.5 , deepseq >= 1.3+ , hashable >= 1.1.1.0 , strict == 0.3.* , bifunctors >= 3.0 , ghc-prim@@ -121,5 +123,5 @@ Data.Tuple.Strict Data.Maybe.Strict Data.Either.Strict- ghc-options: -Wall+ ghc-options: -Wall -fwarn-incomplete-uni-patterns