Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:09:44+00:00 2026-06-14T06:09:44+00:00

I stumbled upon a problem with Eval monad and rpar Strategy in Haskell. Consider

  • 0

I stumbled upon a problem with Eval monad and rpar Strategy in Haskell. Consider following code:

module Main where

import Control.Parallel.Strategies

main :: IO ()
main = print . sum . inParallel2 $ [1..10000]

inParallel :: [Double] -> [Double]
inParallel xss = runEval . go $ xss
    where
      go []  = return []
      go (x:xs) = do
        x'  <- rpar $ x + 1
        xs' <- go xs
        return (x':xs')

inParallel2 :: [Double] -> [Double]
inParallel2 xss = runEval . go $ xss
    where
      go []  = return []
      go [x] = return $ [x + 1]
      go (x:y:xs) = do
        (x',y') <- rpar $ (x + 1, y + 1)
        xs'     <- go xs
        return (x':y':xs'

I compile and run it like this:

ghc -O2 -Wall -threaded -rtsopts -fforce-recomp -eventlog eval.hs
./eval +RTS -N3 -ls -s

When I use inParallel function parallelism works as expected. In the output runtime statistics I see:

SPARKS: 100000 (100000 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)

When I switch to inParallel2 function all parallelism is gone:

SPARKS: 0 (0 converted, 0 overflowed, 0 dud, 0 GC'd, 0 fizzled)

Why doesn’t evaluation of tuples work in parallel? I tried forcing the tuple before passing it to rpar:

rpar $!! (x + 1, y + 1)

but still no result. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:09:45+00:00Added an answer on June 14, 2026 at 6:09 am

    The rpar strategy annotates a term for possible evaluation in parallel, but only up to weak head normal form, which essentially means, up to the outermost constructor. So for an integer or double, that means full evaluation, but for a pair, only the pair constructor, not its components, will get evaluated.

    Forcing the pair before passing it to rpar is not going to help. Now you’re evaluating the pair locally, before annotating the already evaluated tuple for possible parallel evaluation.

    You probably want to combine the rpar with the rdeepseq strategy, thereby stating that the term should be completely evaluated, if possible in parallel. You can do this by saying

    (rpar `dot` rdeepseq) (x + 1, y + 1)
    

    The dot operator is for composing strategies.

    There is, however, yet another problem with your code: pattern matching forces immediate evaluation, and therefore using pattern matching for rpar-annotated expressions is usually a bad idea. In particular, the line

    (x',y') <- (rpar `dot` rdeepseq) (x + 1, y + 1)
    

    will defeat all parallelism, because before the spark can be picked up for evaluation by another thread, the local thread will already start evaluating it in order to match the pattern. You can prevent this by using a lazy / irrefutable pattern:

    ~(x',y') <- (rpar `dot` rdeepseq) (x + 1, y + 1)
    

    Or alternatively use fst and snd to access the components of the pair.

    Finally, don’t expect actual speedup if you create sparks that are as cheap as adding one to an integer. While sparks themselves are relatively cheap, they are not cost-free, so they work better if the computation you are annotating for parallel evaluation is somewhat costly.

    You might want to read some tutorials on using strategies, such as Simon Marlow’s
    Parallel and Concurrent Programming using Haskell or my own Deterministic Parallel Programming in Haskell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stumbled upon the following problem. The below code snippet does not link
Im quite new to Java and i have stumbled upon the following problem. Im
I was testing a website: This Website When I stumbled upon the following problem.
I stumbled upon this problem and it took my a while to realise what
I stumbled upon a problem of how to make work together acts_as_taggable (on steroids)
I stumbled upon a problem. I'm using a simple AJAX script written in jQuery,
I've stumbled upon a problem I don't seem to be able to solve on
I have recently stumbled upon a problem with selecting relationship details from a 1
I've stumbled upon a problem. I'm using 'FormBorderStyle.None', and when I click the icon
I was writing some commented PHP classes and I stumbled upon a problem. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.