[commit: packages/parallel] master: Minor refactoring of Functor/Applicative/Monad instances (0346f07)

git at git.haskell.org git at git.haskell.org
Sat Jan 2 08:17:01 UTC 2016


Repository : ssh://git@git.haskell.org/parallel

On branch  : master
Link       : http://git.haskell.org/packages/parallel.git/commitdiff/0346f07f1b74c6c25e24d4a9ff4ba6fbd3275bae

>---------------------------------------------------------------

commit 0346f07f1b74c6c25e24d4a9ff4ba6fbd3275bae
Author: Herbert Valerio Riedel <hvr at gnu.org>
Date:   Sat Oct 31 13:42:07 2015 +0100

    Minor refactoring of Functor/Applicative/Monad instances
    
    We may want to override `*>` at some point for the GHC 7.2+ code
    
    However, I'm a bit puzzled by the comment
    
      -- GHC 7.2.1 added the seq# and spark# primitives, that we use in
      -- the Eval monad implementation in order to get the correct
      -- strictness behaviour.
    
    while the actual `instance Monad` implementation below
    doesn't use any of those primitives


>---------------------------------------------------------------

0346f07f1b74c6c25e24d4a9ff4ba6fbd3275bae
 Control/Parallel/Strategies.hs | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/Control/Parallel/Strategies.hs b/Control/Parallel/Strategies.hs
index 05d5385..bded495 100644
--- a/Control/Parallel/Strategies.hs
+++ b/Control/Parallel/Strategies.hs
@@ -201,8 +201,15 @@ newtype Eval a = Eval (State# RealWorld -> (# State# RealWorld, a #))
 runEval :: Eval a -> a
 runEval (Eval x) = case x realWorld# of (# _, a #) -> a
 
+instance Functor Eval where
+  fmap = liftM
+
+instance Applicative Eval where
+  pure x = Eval $ \s -> (# s, x #)
+  (<*>)  = ap
+
 instance Monad Eval where
-  return x = Eval $ \s -> (# s, x #)
+  return = pure
   Eval x >>= k = Eval $ \s -> case x s of
                                 (# s', a #) -> case k a of
                                                       Eval f -> f s'
@@ -214,8 +221,15 @@ data Eval a = Done a
 runEval :: Eval a -> a
 runEval (Done x) = x
 
+instance Functor Eval where
+  fmap = liftM
+
+instance Applicative Eval where
+  pure = Done
+  (<*>) = ap
+
 instance Monad Eval where
-  return x = Done x
+  return = pure
   Done x >>= k = lazy (k x)   -- Note: pattern 'Done x' makes '>>=' strict
 
 {-# RULES "lazy Done" forall x . lazy (Done x) = Done x #-}
@@ -223,14 +237,6 @@ instance Monad Eval where
 #endif
 
 
-instance Functor Eval where
-  fmap = liftM
-
-instance Applicative Eval where
-  (<*>) = ap
-  pure  = return
-
-
 -- The Eval monad satisfies the monad laws.
 --
 -- (1) Left identity:



More information about the ghc-commits mailing list