[Haskell-cafe] Data.Foldable UArray
Tom Ellis
tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Fri Feb 21 08:47:29 UTC 2014
On Thu, Feb 20, 2014 at 10:12:54PM +0100, Marcus D. Gabriel wrote:
> Hello,
>
> I wanted to make a simple Data.Foldable UArray, and I naively modelled
> it on
>
> > instance Ix i => Foldable (Array i) where
> > foldr f z = Prelude.foldr f z . elems
>
> with, of course,
>
> > instance Ix i => Foldable (UArray i) where
> > foldr f z = Prelude.foldr f z . elems
>
> which did not work yielding the following type message
>
> Could not deduce (IArray UArray a) arising from a use of `elems'
[...]
The problem is that the argument 'e' in 'UArray i e' is a phantom type
argument used only for looking up the relevant instance of 'IArray'. It
doesn't actually have anything to do with the underlying contents of the
array, which is basically just a 'ByteString'.
Since all 'Foldable' functions factor through 'toList', you can't go too
wrong by using '(foldableFunction . toList) myArray' wherever you would have
wanted to use 'foldableFunction myArray'.
Tom
More information about the Haskell-Cafe
mailing list