Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given the legendary helpfulness of the Haskell community, the lack of an answer at this point is a strong indication that there's no good solution in the current type system.</p> <p>I've already outlined the flawed solutions in the question, so I'll just post a complete version of my example. This is basically what I used to solve most alignment problems on Rosalind:</p> <pre><code>{-# LANGUAGE FlexibleContexts, RankNTypes, ScopedTypeVariables #-} import Control.Applicative import Control.Monad import Control.Monad.ST import Data.Maybe import Data.Array.ST import Data.Array.Unboxed class IArray UArray e =&gt; Unboxable e where newSTUArray_ :: forall s i. Ix i =&gt; (i, i) -&gt; ST s (STUArray s i e) readSTUArray :: forall s i. Ix i =&gt; STUArray s i e -&gt; i -&gt; ST s e writeSTUArray :: forall s i. Ix i =&gt; STUArray s i e -&gt; i -&gt; e -&gt; ST s () instance Unboxable Bool where newSTUArray_ = newArray_ readSTUArray = readArray writeSTUArray = writeArray instance Unboxable Double where newSTUArray_ = newArray_ readSTUArray = readArray writeSTUArray = writeArray {- Same for Char, Float, (Int|Word)(|8|16|32|64)... -} {-# INLINE dynamicProgramming2DSTU #-} dynamicProgramming2DSTU :: forall e i j . ( Unboxable e, Ix i, Ix j, Enum i, Enum j ) =&gt; (forall m . (Monad m, Applicative m) =&gt; (i -&gt; j -&gt; m e) -&gt; (i -&gt; j -&gt; m e)) -&gt; (i -&gt; j -&gt; Maybe e) -&gt; (i, i) -&gt; (j, j) -&gt; (i -&gt; j -&gt; e) dynamicProgramming2DSTU program boundaryConditions (xl, xh) (yl, yh) = arrayLookup where arrayLookup :: i -&gt; j -&gt; e arrayLookup xi yj = fromMaybe (resultArray ! (xi, yj)) $ boundaryConditions xi yj arrB :: ((i, j), (i, j)) arrB = ((xl, yl), (xh, yh)) resultArray :: UArray (i, j) e resultArray = runSTUArray resultArrayST resultArrayST :: forall s. ST s (STUArray s (i, j) e) resultArrayST = do arr &lt;- newSTUArray_ arrB let acc xi yj = maybe (readSTUArray arr (xi, yj)) return $ boundaryConditions xi yj forM_ [xl..xh] $ \xi -&gt; do forM_ [yl..yh] $ \yj -&gt; do result &lt;- program acc xi yj writeSTUArray arr (xi, yj) result return arr </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload