Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fmap with Parallel.Strategies gives NFData error

I'm having some trouble with the NFData class in Control.DeepSeq. I'd like my type to implement its Functor fmap in parallel using the Control.Parallel.Strategies module. For example, in the following code, I get an error starting "Could not deduce (NFData b)...". If I use rseq instead of rdeepseq there is no problem, however I would like to experiment with rdeepseq. I have GHC 6.12.3 running under Ubuntu 11.04.

module Main where

import Control.Parallel.Strategies
import Control.DeepSeq

data Foo a = Foo Int [a]
  deriving (Show,Eq)

instance Functor Foo where
  fmap f (Foo i xs) = Foo i (map f xs `using` parList rdeepseq)
like image 503
user2023370 Avatar asked Dec 07 '25 12:12

user2023370


1 Answers

Sorry, that's not possible. If you look at your fmap you can see that it uses rdeepseq which needs the type a in the list inside Foo to be in class NFData. But there is simply nowhere to attach this constraint, because that type variable is not mentioned anywhere in the instance declaration.

There are various workarounds using non-standard versions of Functor, but none of them are pleasant.

Have a look at Haskell Type Constraints Unleashed for a suggested solution.

like image 71
augustss Avatar answered Dec 10 '25 11:12

augustss