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? Or there is another kind of immutable boxed array somewhere in haskell libraries? 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 Ciao & thanks in advance ----- FB -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090226/d5c28007/attachment.htm |
Please, post the error messages.
Maybe all is needed is an explicit type declaration... On Thu, Feb 26, 2009 at 16:58, Francesco Bochicchio <[hidden email]>wrote: > 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? > Or there is another kind of immutable boxed array somewhere in haskell > libraries? > 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 > > Ciao & thanks in advance > ----- > FB > > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/beginners > > -- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090226/a80d9267/attachment.htm |
In reply to this post by Francesco Bochicchio
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 |
In reply to this post by Rafael Gustavo da Cunha Pereira Pinto-2
IArray define TONS of instances of IArray class...
You will surely have to define a type for your expression. For your example you could use: arr::Array Int Int On Thu, Feb 26, 2009 at 18:19, Daniel Fischer <[hidden email]>wrote: > Am Donnerstag, 26. Februar 2009 21:59 schrieb Rafael Gustavo da Cunha > Pereira > Pinto: > > Please, post the error messages. > > > > Maybe all is needed is an explicit type declaration... > > Yup! > Are you an experienced victim of the monomorphism restriction? > > Cheers, > Daniel > > -- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc. -- Rafael Gustavo da Cunha Pereira Pinto Electronic Engineer, MSc. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090226/ffcb5926/attachment-0001.htm |
Free forum by Nabble | Edit this page |