Re: [Haskell] Help in understanding a type error involving forall and class constraints
Erm, something I remember about needing MArrays... Here's something which does the same thing (cooks and MArray then freezes it - also using STUArray... The neat thing is MArray is a class, so you can swap between STUArray and STArray implementations without changing code. This is the classic dynamic programming version of string difference: distString :: String -> String -> Int distString s0 s1 = runST (difST s0 s1) difST :: MArray (STUArray s) Int (ST s) => String -> String -> ST s Int difST s0 s1 = do b@(_,br) <- return $ (\x1 y1 -> ((0,0),(x1,y1))) (length s0) (length s1) d <- newArray b 0 :: ST s (STUArray s (Int,Int) Int) mdiff d s0 s1 readArray d br mMin :: Int -> Int -> Int -> Int mMin i j k = min (min i j) k costDelete :: Char -> Int costDelete _ = 1 costInsert :: Char -> Int costInsert _ = 1 costReplace :: Char -> Char -> Int costReplace _ _ = 1 mdiff :: MArray a Int m => a (Int,Int) Int -> String -> String -> m () mdiff (d :: a e i) s0 s1 = do writeArray d (0,0) 0 foreach 1 s0 $ \x a -> do dx <- readArray d (x-1,0) writeArray d (x,0) (dx+costDelete a) foreach 1 s1 $ \y b -> do dy <- readArray d (0,y-1) writeArray d (0,y) (dy+costInsert b) foreach 1 s0 $ \x a -> do foreach 1 s1 $ \y b -> do dx <- readArray d (x-1,y) dy <- readArray d (x,y-1) dxy <- readArray d (x-1,y-1) writeArray d (x,y) $ mMin (dx+costDelete a) (dy+costInsert b) (if a==b then dxy else dxy+costReplace a b) where foreach :: MArray a Int m => Int -> String -> (Int -> Char -> m ()) -> m () foreach _ [] _ = return () foreach i (c0:cs) f = do f i c0 foreach (i+1) cs f ------------------------------------ Keean.
On Tue, 2004-06-29 at 18:42, MR K P SCHUPKE wrote:
Erm, something I remember about needing MArrays... Here's something which does the same thing (cooks and MArray then freezes it - also using STUArray... The neat thing is MArray is a class, so you can swap between STUArray and STArray implementations without changing code.
I tried generalising the type to not mention which of STUArray and STArray I'm using. Letting ghc infer the type gives this type, unfortunately annotating the function with this type gives a type error!! Compiler/typechecker bug perhaps? buildUArray bounds f = do arr <- buildUArray' bounds f unsafeFreeze arr ghc infers this type: buildUArray :: forall a i b s array. (MArray (array s) a (ST s), Ix i, IArray b a) => (i, i) -> (i -> a) -> ST s (b i a) however if one add this annotation I get the same error message I got originally: Could not deduce (MArray (array1 s) a (ST s)) from the context (MArray (array s) a (ST s), Ix i, IArray b a) arising from use of `unsafeFreeze' at BuildArray.hs:25 Probable fix: Add (MArray (array1 s) a (ST s)) to the type signature(s) for `buildUArray' Or add an instance declaration for (MArray (array1 s) a (ST s)) In the result of a 'do' expression: unsafeFreeze arr In the definition of `buildUArray': buildUArray bounds f = do arr <- buildUArray' bounds f unsafeFreeze arr
This is the classic dynamic programming version of string difference:
[snip] The difference with your example cod is that I return the array itself rather than a value calculated using the array. With your code, as soon as we try to return the array too, I run into the same problem. Duncan
participants (2)
-
Duncan Coutts -
MR K P SCHUPKE