[Haskell-beginners] Array and IArray
Daniel Fischer
daniel.is.fischer at web.de
Thu Feb 26 16:17:31 EST 2009
Am Donnerstag, 26. Februar 2009 20:58 schrieb Francesco Bochicchio:
> Hello all,
>
> I'm reading stuff about the different types of arrays in Haskell ...
>
> One of the things that I don't understand
> If I do:
>
> import Data.Array
> arr = listArray (1,10) [1..]
>
> the type of arr is Array. My question is: is this array an instance of the
> type class defined in Data.Array.IArray?
Yes, it is.
> Or there is another kind of immutable boxed array somewhere in haskell
> libraries?
There are many array libraries, I wouldn't be surprised if one of them also
contained an immutable boxed array type.
> The documentation makes a reference to 'the Array type exported by
> Data.Array.IArray' but if I do:
>
> import Data.Array.IArray
> arr = listArray (1,10) [1..]
>
> I get a couple of compiler errors
like:
Ambiguous type variables `t', `a' in the constraint:
`IArray a t' arising from a use of `listArray' at ArrayI.hs:5:6-27
Possible cause: the monomorphism restriction applied to the following:
arr :: a Integer t (bound at ArrayI.hs:5:0)
Probable fix: give these definition(s) an explicit type signature
or use -fno-monomorphism-restriction
Ambiguous type variable `t' in the constraints:
`Enum t'
arising from the arithmetic sequence `1 .. ' at ArrayI.hs:5:23-27
`Num t' arising from the literal `1' at ArrayI.hs:5:24
Possible cause: the monomorphism restriction applied to the following:
arr :: a Integer t (bound at ArrayI.hs:5:0)
Probable fix: give these definition(s) an explicit type signature
or use -fno-monomorphism-restriction
Note that ghci gives two probable fixes, both work.
The point is that Data.Array.IArray also exports the typeclass IArray.
If you import Data.Array, the type of listArray is
listArray :: (Ix i) => (i, i) -> [e] -> Array i e
So
arr :: (Ix i, Num i, Num e, Enum e) => Array i e
(the index type must belong to Num because of the literals 1 and 10 for the
array bounds, the element type must belong to Num because of the literal 1
and to Enum because of the use of enumFrom - [1 .. ])
Now defaulting (section 4.3.4 of the Haskell report) is used to determine a
monomorphic type for arr.
arr must have a monomorphic type because it is defined by a simple pattern
binding (just a variable name on the left of '=') and we have the
monomorphism restriction (section 4.5.5 of the Haskell report, see also
http://www.haskell.org/haskellwiki/Monomorphism_restriction)
Both i and e default to Integer, giving
arr :: Array Integer Integer
Disabling the monomorphism restriction, arr retains its polymorphic type
*ArrayI> :t arr
arr :: (Num t, Num t1, Enum t1, Ix t) => Array t t1
Now if you import Data.Array.IArray, listArray has a more general type:
listArray :: (Ix i, IArray a e) => (i, i) -> [e] -> a i e
Then type inference determines the type of arr as
*ArrayI> :t arr
arr :: (Num t, Num t1, Enum t1, IArray a t1, Ix t) => a t t1
But, arr is still defined by a simple pattern binding, so the monomorphism
restriction kicks in again, and GHC tries to give arr a monomorphic type,
fixing a, t and t1
Alas, says the report in section 4.3.4:
an ambiguous type variable, v, is defaultable if:
* v appears only in constraints of the form C v, where C is a class, and
* at least one of these classes is a numeric class, (that is, Num or a
subclass of Num), and
* all of these classes are defined in the Prelude or a standard library
- all classes defined in the Prelude or standard library? Yes
- at least one of these classes is a numeric class? Not for a!
- I'm not quite sure, but the fact that IArray is a multiparameter type class
may also hinder defaulting.
So a monomorphic type for arr could not be determined, hence the errors.
If you give an explicit type signature or disable the monomorphism
restriction, al goes well whether you import Data.Array or Data.Array.IArray.
>
> Ciao & thanks in advance
> -----
> FB
HTH,
Daniel
More information about the Beginners
mailing list