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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:05:03+00:00 2026-06-17T19:05:03+00:00

I am struggling to understand why the following attempts to find a minimum element

  • 0

I am struggling to understand why the following attempts to find a minimum element in STArray lead to stack space overflow when compiled with ghc (7.4.1, regardless of -O level), but work fine in ghci:

import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Data.Array.ST

n = 1000 :: Int

minElem = runST $ do
  arr <- newArray ((1,1),(n,n)) 0 :: ST s (STArray s (Int,Int) Int)
  let ixs = [(i,j) | i <- [1..n], j <- [1..n]]
  forM_ ixs $ \(i,j) -> writeArray arr (i,j) (i*j `mod` 7927)
--  readArray arr (34,56)  -- this works OK
--  findMin1 arr           -- stackoverflows when compiled
  findMin2 arr           -- stackoverflows when compiled

findMin1 arr = do
  es <- getElems arr
  return $ minimum es

findMin2 arr = do
  e11 <- readArray arr (1,1) 
  foldM (\m ij -> min m <$> readArray arr ij) e11 ixs
      where ixs = [(i,j) | i <- [1..n], j <- [1..n]]

main = print minElem

Note: switching to STUArray or ST.Lazy doesn’t seem to have any effect.

The main question though: What would be the proper way to implement such “fold-like” operation over big STArray while inside ST?

  • 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-17T19:05:05+00:00Added an answer on June 17, 2026 at 7:05 pm

    The big problem in findMin1 is getElems:

    getElems :: (MArray a e m, Ix i) => a i e -> m [e]
    getElems marr = do
      (_l, _u) <- getBounds marr      -- Hmm, why is that there?
      n <- getNumElements marr
      sequence [unsafeRead marr i | i <- [0 .. n - 1]]
    

    Using sequence on a long list is a common cause for stack overflows in monads whose (>>=) isn’t lazy enough, since

    sequence ms = foldr k (return []) ms
            where
              k m m' = do { x <- m; xs <- m'; return (x:xs) }
    

    then it has to build a thunk of size proportional to the length of the list. getElems would work with the Control.Monad.ST.Lazy, but then the filling of the array with

    forM_ ixs $ \(i,j) -> writeArray arr (i,j) (i*j `mod` 7927)
    

    creates a huge thunk that overflows the stack. For the strict ST variant, you need to replace getElems with something that builds the list without sequence or – much better – compute the minimum without creating a list of elements at all. For the lazy ST variant, you need to ensure that the filling of the array doesn’t build a huge thunk e.g. by forcing the result of the writeArray calls

    let fill i j
          | i > n     = return ()
          | j > n     = fill (i+1) 1
          | otherwise = do
              () <- writeArray arr (i,j) $ (i*j `mod` 7927)
              fill i (j+1)
    () <- fill 1 1
    

    The problem in findMin2 is that

    foldM (\m ij -> min m <$> readArray arr ij) e11 ixs
    

    is lazy in m, so it builds a huge thunk to compute the minimum. You can easily fix that by using seq (or a bang-pattern) to make it strict in m.

    The main question though: What would be the proper way to implement such “fold-like” operation over big STArray while inside ST?

    Usually, you’ll use the strict ST variant (and for types like Int, you should almost certainly use STUArrays instead of STArrays). Then the most important rule is that your functions be strict enough. The structure of findMin2 is okay, the implementation is just too lazy.

    If performance matters, you will often have to avoid the generic higher order functions like foldM and write your own loops to avoid allocating lists and control strictness exactly as the problem at hand requires.

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

Sidebar

Related Questions

I am struggling to understand the following code snippet in assembler: if ( EAX
I'm struggling to understand why the following problem appears. I have an input box
I'm struggling to understand the difference of the following 2 sets of code. The
I'm struggling to understand why nothing is output using the following: class Program {
I am really struggling to understand tail recursion in Erlang. I have the following
i'm struggling and trying to understand why the following code is not working. I
I'm struggling to understand why the following code is echoing out 'FOO2' when i'm
Perhaps an odd question.. I'm currently struggling to understand why the following equates to
I am struggling to get the following code past GHC: getFirstChild :: (WidgetClass w1,
I'm struggling to understand whow I could do the following. I have an abstract

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.