[Haskell-beginners] Type declaration

Christian Sperandio christian.sperandio at gmail.com
Sun Apr 12 16:41:50 UTC 2015


Hi,

I’m currently playing with the mutable array to implement an heap sort sort (from The Art of Computer Programming) and I’m puzzled with an error.

When I code:

toHeap :: Ord a => [a] -> IO [a]
toHeap [] = return []
toHeap [x] = return [x]
toHeap xs = do
  arr <- newListArray (1, length xs) xs :: IO (IOArray Int a)
  getElems arr

I’ve got this error:

Could not deduce (a ~ a1)
from the context (Ord a)
  bound by the type signature for toHeap :: Ord a => [a] -> IO [a]
  at /var/folders/fk/339860r16j759wmsfmjkyr4h0000gn/T/flycheck797T5s/Heapsort.hs:9:11-32
  ‘a’ is a rigid type variable bound by
      the type signature for toHeap :: Ord a => [a] -> IO [a]
      at /var/folders/fk/339860r16j759wmsfmjkyr4h0000gn/T/flycheck797T5s/Heapsort.hs:9:11
  ‘a1’ is a rigid type variable bound by
       an expression type signature: IO (IOArray Int a1)
       at /var/folders/fk/339860r16j759wmsfmjkyr4h0000gn/T/flycheck797T5s/Heapsort.hs:14:10
Expected type: [a1]
  Actual type: [a]
Relevant bindings include
  xs :: [a]
    (bound at /var/folders/fk/339860r16j759wmsfmjkyr4h0000gn/T/flycheck797T5s/Heapsort.hs:12:8)
  toHeap :: [a] -> IO [a]
    (bound at /var/folders/fk/339860r16j759wmsfmjkyr4h0000gn/T/flycheck797T5s/Heapsort.hs:10:1)
In the second argument of ‘newListArray’, namely ‘xs’
In a stmt of a 'do' block:
  arr <- newListArray (1, length xs) xs :: IO (IOArray Int a)


But with the code below, all work:

toHeap :: Ord a => [a] -> IO [a]
toHeap [] = return []
toHeap [x] = return [x]
toHeap xs = do
  arr <- buildArray xs
  getElems arr


buildArray :: [a] -> IO (IOArray Int a)
buildArray xs = newListArray (1, length xs) xs

What do I miss ?

Thanks.

Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150412/a79ca0e6/attachment.html>


More information about the Beginners mailing list