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

  • Home
  • SEARCH
  • 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 618751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:34:51+00:00 2026-05-13T18:34:51+00:00

I want to implement an algorithm using the ST monad and STUArray s, and

  • 0

I want to implement an algorithm using the ST monad and STUArrays, and I want it to be able to work with both Float and Double data.

I’ll demonstrate on a simpler example problem: calculating a memoized scanl (+) 0 (I know it can be solved without STUArray, just using as example).

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-}

import Control.Monad
import Control.Monad.ST
import Data.Array.Unboxed
import Data.Array.ST

accumST :: forall a. (IArray UArray a, Num a) => [a] -> Int -> a
accumST vals = (!) . runSTUArray $ do
  arr <- newArray (0, length vals) 0 :: ST s (STUArray s Int a)
  forM_ (zip vals [1 .. length vals]) $ \(val, i) ->
    readArray arr (i - 1)
    >>= writeArray arr i . (+ val)
  return arr

This fails with:

Could not deduce (MArray (STUArray s) a (ST s)) from the context ()
  arising from a use of 'newArray'
Possible fix:
  add (MArray (STUArray s) a (ST s)) to the context of
    an expression type signature
  or add an instance declaration for (MArray (STUArray s) a (ST s))

I can’t apply the suggested “Possible fix”. Because I need to add something like (forall s. MArray (STUArray s) a (ST s)) to the context, but afaik that’s impossible..

  • 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-05-13T18:34:51+00:00Added an answer on May 13, 2026 at 6:34 pm

    Unforunately, you can’t currently create a context that requires that an unboxed array be available for a specific type. Quantified Constraints aren’t allowed. However, you can still accomplish what you’re trying to do, (with the added advantage of having type-specific code versions.) For Longer functions, you could try to split out common expressions so that the repeated code is as small as possible.

    {-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-}
    module AccumST where 
    
    import Control.Monad
    import Control.Monad.ST
    import Data.Array.Unboxed
    import Data.Array.ST
    import Data.Array.IArray
    
    -- General one valid for all instances of Num.
    -- accumST :: forall a. (IArray UArray a, Num a) => [a] -> Int -> a
    accumST :: forall a. (IArray UArray a, Num a) => [a] -> Int -> a
    accumST vals = (!) . runSTArray $ do
      arr <- newArray (0, length vals) 0 :: (Num a) => ST s (STArray s Int a)
      forM_ (zip vals [1 .. length vals]) $ \(val, i) ->
        readArray arr (i - 1)
        >>= writeArray arr i . (+ val)
      return arr
    
    accumSTFloat vals = (!) . runSTUArray $ do
      arr <- newArray (0, length vals) 0 :: ST s (STUArray s Int Float)
      forM_ (zip vals [1 .. length vals]) $ \(val, i) ->
        readArray arr (i - 1)
        >>= writeArray arr i . (+ val)
      return arr
    
    accumSTDouble vals = (!) . runSTUArray $ do
      arr <- newArray (0, length vals) 0 :: ST s (STUArray s Int Double)
      forM_ (zip vals [1 .. length vals]) $ \(val, i) ->
        readArray arr (i - 1)
        >>= writeArray arr i . (+ val)
      return arr
    
    {-# RULES "accumST/Float" accumST = accumSTFloat #-}
    {-# RULES "accumST/Double" accumST = accumSTDouble #-}
    

    The Generic Unboxed version (which doesn’t work) would have a type constraint like the following:

    accumSTU :: forall a. (IArray UArray a, Num a, 
        forall s. MArray (STUArray s) a (ST s)) => [a] -> Int -> a
    

    You could simplify as follows:

    -- accumST :: forall a. (IArray UArray a, Num a) => [a] -> Int -> a
    accumST :: forall a. (IArray UArray a, Num a) => [a] -> Int -> a
    accumST vals = (!) . runSTArray $ do
      arr <- newArray (0, length vals) 0 :: (Num a) => ST s (STArray s Int a)
      accumST_inner vals arr
    
    accumST_inner vals arr = do
      forM_ (zip vals [1 .. length vals]) $ \(val, i) ->
        readArray arr (i - 1)
        >>= writeArray arr i . (+ val)
      return arr
    
    accumSTFloat vals = (!) . runSTUArray $ do
      arr <- newArray (0, length vals) 0 :: ST s (STUArray s Int Float)
      accumST_inner vals arr
    
    accumSTDouble vals = (!) . runSTUArray $ do
      arr <- newArray (0, length vals) 0 :: ST s (STUArray s Int Double)
      accumST_inner vals arr
    
    {-# RULES "accumST/Float" accumST = accumSTFloat #-}
    {-# RULES "accumST/Double" accumST = accumSTDouble #-}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I want to implement my own clustering algorithm using this Virtual Earth javascript API:
Suppose I want to implement a reasonably efficient 'keyword recognition algorithm', that is first
I want to implement in Java a class for handling graph data structures. I
I want to implement an ISAPI filter like feature using HttpModule in IIS7 running
I am trying to learn Ruby, and want to implement the Python algorithms from
I want to implement search functionality for a website (assume it is similar to
I want to implement an automatic update system for a windows application. Right now
I want to implement a paperless filing system and was looking to use WIA
I want to implement a two-pass cache system: The first pass generates a PHP
I want to implement a simple debug log, which consists of a table into

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.