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 7782811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:39:26+00:00 2026-06-01T19:39:26+00:00

I’m trying to do computation in parallel, and write the results to an STArray.

  • 0

I’m trying to do computation in parallel, and write the results to an STArray. I think this code shows what I’m trying to do. However, I’m getting compile errors.

import Control.Monad
import Control.Monad.ST
import Control.Parallel
import Data.Array.ST

main = do
    arr <- newArray ((0,0), (5,5)) 0 :: ST s (STArray s (Int, Int) Int)
    runSTArray $ do
        par (writeArray arr (1,1) 17) (writeArray arr (2,2) 23)
        return arr
    print arr

How should I do this?

  • 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-01T19:39:28+00:00Added an answer on June 1, 2026 at 7:39 pm

    You use newArray, which has the type ST s (STArray s (Int, Int) Int). However, you use it in the body of the main function, which means that everything you do must have an IO type. ST is not IO, so the types cannot match.

    You should first move the newArray into a context where you have access to the ST monad. This context is of course available in the body of runSTArray, so change the body to:

        runSTArray $ do
            arr <- newArray ((0,0), (5,5)) 0 :: ST s (STArray s (Int, Int) Int)
            par (writeArray arr (1,1) 17) (writeArray arr (2,2) 23)
            return arr
    

    Then, you need to rethink how par behaves. par is for creating parallel pure computations, and cannot be used for monadic actions; monads cannot generally be parallelized at all. In particular, the ST monad doesn’t even offer any alternatives for parallel computations; since parallel writes to an array can lead to race conditions (what happens if you overwrite the same cell? Which write will count, and which one won’t?), it is unsafe to allow parallelism here. You must change the array in sequence:

        runSTArray $ do
            arr <- newArray ((0,0), (5,5)) 0 :: ST s (STArray s (Int, Int) Int)
            writeArray arr (1,1) 17
            writeArray arr (2,2) 23
            return arr
    

    However, the writes aren’t expensive; it’s the calculations of the values that might be expensive. Suppose that you want to calculate 17 and 23 on the fly; you can then do the following:

    let a = someLongCalculation 12534
        b = a `par` (someLongCalculation 24889)
    writeArray arr (1, 1) a
    writeArray arr (2, 2) b
    

    Finally, you must realize that runSTArray returns the result array, so you must store it like this:

    import Control.Monad
    import Control.Monad.ST
    import Control.Parallel
    import Data.Array.ST
    
    main =
      let pureArr =
            runSTArray $ do
              arr <- newArray ((0,0), (5,5)) 0 :: ST s (STArray s (Int, Int) Int)
              writeArray arr (1,1) 17
              writeArray arr (2,2) 23
              return arr
      in print pureArr
    

    I don’t think that STArrays are the correct solution here. You should use a more powerful array library like repa in situations where you need parallel symmetrical array computations.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

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.