[Haskell-cafe] Data.Foldable UArray
Marcus D. Gabriel
marcus at gabriel.name
Fri Feb 21 20:30:12 UTC 2014
Hello Tom,
Basically, I follow what you have written but ...
We have
> Prelude> :k Data.Array.Unboxed.UArray
> Data.Array.Unboxed.UArray :: * -> * -> *
which is why I tried my first naive solution. I note that we have also
> Prelude> :k Data.Array.IArray.IArray
> Data.Array.IArray.IArray :: (* -> * -> *) -> * -> Constraint
This last one I do not follow. A short explanation and/or a pointer
to some documentation would help.
Thanks,
- Marcus
On 21/02/2014 09:47, Tom Ellis wrote:
> 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