Ich möchte einen Algorithmus mit dem ST
Monade und STUArray
s implementieren, und ich will es in der Lage sein, mit beiden Float
und Double
Daten zu arbeiten.STUArray mit polymorphen Typ
Ich demonstriere auf ein einfacheres Beispiel Problem: Berechnung eines Memo scanl (+) 0
(ich weiß, dass es ohne STUArray
gelöst werden kann, nur als Beispiel). Diese
{-# 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
nicht mit:
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))
Ich kann die vorgeschlagene "Mögliche fix" nicht anwendbar. Weil ich etwas wie (forall s. MArray (STUArray s) a (ST s))
zum Kontext hinzufügen muss, aber afaik, das ist unmöglich.
Die Regeln werden nur ausgelöst, wenn sie mit optimierten Optimierungen kompiliert wurden. –
Ich habe am Ende eine andere Problemumgehung für jetzt verwendet - siehe Antwort unten – yairchu