Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 629
Incerteza Avatar asked Nov 02 '22 12:11

Incerteza


1 Answers

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:

like image 198
Rodrigo Taboada Avatar answered Nov 15 '22 08:11

Rodrigo Taboada