2

The source code for Control.Parallel.Strategies ( http://hackage.haskell.org/packages/archive/parallel/3.1.0.1/doc/html/src/Control-Parallel-Strategies.html#Eval ) contains a type Eval defined as:

data Eval a = Done a

which has the following Monad instance:

instance Monad Eval where
  return x = Done x
  Done x >>= k = k x   -- Note: pattern 'Done x' makes '>>=' strict

Note the comment in the definition of bind. Why is this comment true? My understanding of strictness is that a function is only strict if it must "know something" about its argument. Here, bind just applies k to x, thus it doesn't appear (to me) to need to know anything about x. Further, the comment suggests that strictness is "induced" in the pattern match, before the function is even defined. Can someone help me understand why bind is strict?

Also, it looks like Eval is just the identity Monad and that, given the comment in the definition of bind, bind would be strict for almost any Monad. Is this the case?

Benjamin Malley
  • 233
  • 4
  • 14
  • Also, `Identity` is a newtype wrapper, which means that pattern matches always succeed (`newtype`s don't create an additional layer of lifting and thus `Id ⊥ ≡ ⊥`). You could make `Eval` non-strict by writing bind as `m >>= k = let Done x = m in k x` or `~(Done x) >>= k = k x`. – Vitus Aug 06 '12 at 16:13

1 Answers1

7

It is strict in that m >> n evaluates m, unlike the Identity Monad:

Prelude Control.Parallel.Strategies Control.Monad.Identity> runIdentity (undefined >> return "end") 
"end"
Prelude Control.Parallel.Strategies Control.Monad.Identity> runEval (undefined >> return "end") 
"*** Exception: Prelude.undefined

It is not strict in the value that m produces,w which is what you are pointing out:

Prelude Control.Parallel.Strategies Control.Monad.Identity> runEval (return undefined >> return "end") 
"end"
Chris Kuklewicz
  • 8,123
  • 22
  • 33
  • Got it, thanks. I was doubly confused because I thought the Identity Monad had the same definition, but I see the difference now. – Benjamin Malley Aug 06 '12 at 16:16
  • Worth pointing out explicitly: bind *does* know something about its argument if the pattern match succeeds: it knows its argument contains a `Done x`, rather than simply `undefined`. – jpaugh Oct 07 '17 at 15:10