Incerteza
Incerteza

Reputation: 34934

Parallelism in Haskell

I'm trying to create a simple example doing a parallel evaluations:

import Control.Parallel.Strategies

main = do
  runEval $ do
    a <- rpar (\x -> x + 5)
    b <- rseq (\x -> x + 15)
    return (a, b)

It says

Couldn't match expected type `IO t0'
                with actual type `(Integer -> Integer, Integer -> Integer)'

I know, it's not related to parallelism in Haskell, but nonetheless, how do I build such a simple example in Haskell?

Upvotes: 5

Views: 395

Answers (1)

Rodrigo Taboada
Rodrigo Taboada

Reputation: 2727

The problem is with the way you are using the lambdas. The types of rpar and rseq is a -> Eval a but the lambdas you passed has obviously type Integer -> Integer because you didn't passed an argument to the lambdas.

Something like this compiles (also note that you need to print the result):

main = do
    print $ runEval $ do
        a <- rpar $ (\x -> x + 5) 4
        b <- rseq $ (\x -> x + 15) 4
        return (a, b)

To learn more about parallel processing in Haskell. There is a great book called "Parallel and Concurrent Programming in Haskell" by Simon Marlow, the HTML version is available for free here:

Upvotes: 2

Related Questions