Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Meaning of '!' in record defintion? UNPACK? (Magnus Therning)
2. Re: Defeating type inference (Daniel Fischer)
3. Re: Meaning of '!' in record defintion? UNPACK? (Daniel Fischer)
4. Array and IArray (Francesco Bochicchio)
5. Re: Array and IArray (Rafael Gustavo da Cunha Pereira Pinto)
6. Re: Array and IArray (Daniel Fischer)
7. Fwd: [Haskell-beginners] Array and IArray
(Rafael Gustavo da Cunha Pereira Pinto)
----------------------------------------------------------------------
Message: 1
Date: Thu, 26 Feb 2009 11:36:29 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] Meaning of '!' in record defintion?
UNPACK?
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, Feb 26, 2009 at 11:33 AM, Daniel Fischer
<[email protected]> wrote:
> Am Donnerstag, 26. Februar 2009 12:07 schrieb Colin Paul Adams:
>> >>>>> "Thomas" == Thomas Davie <[email protected]> writes:
>>
>> Â Â Thomas> The {-# UNPACK #-} tells the compiler that it can unpack
>> Â Â Thomas> the Int â meaning that a Position will be neatly packed
>> Â Â Thomas> into 12 bytes.
>>
>> What would be the difference if there was no UNPACK pragma?
>
> Section 8.12.10 of the users' guide says:
> "The UNPACK indicates to the compiler that it should unpack the contents of a
> constructor field into the constructor itself, removing a level of
> indirection."
>
> It has more, and also says when it's not a good idea to use it.
>
> Int is defined as
>
> data Int = I# Int#
>
> where Int# is a raw machine int. If you use the {-# UNPACK #-} pragma, you
> tell GHC that you'd very much like Position to be stored as constructor +
> three contiguous raw machine integers. Mostly, it will do so.
> If you don't use the pragma, i.e. have
>
> Â Â data Position =
> Â Â Â Â Â Position { posOffset :: !Int
> Â Â Â Â Â Â Â Â Â Â , posRow :: !Int
> Â Â Â Â Â Â Â Â Â Â , posColumn :: !Int
> Â Â Â Â Â Â Â Â Â Â }
>
> , GHC may or may not decide to store it thus, with -O2 it's not too unlikely,
> I think. But it's also not unlikely that it will be stored as constructor +
> three pointers to three evaluated Ints, which is much better than pointers to
> thunks, but not as good as having the raw values directly by the constructor.
>
> Cheers,
> Daniel
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
Are there any guarantees here, or is it just me telling the compiler
to please do what I say, but the compiler can decide not to follow my
wishes?
Is address aligning predictable?
I'm basically wondering if it'd be possible to to use this mechanism
to avoid using FFI when reading data that basically is a dump of
structs in a C program.
/M
--
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org
http://therning.org/magnus identi.ca|twitter: magthe
------------------------------
Message: 2
Date: Thu, 26 Feb 2009 12:45:30 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Defeating type inference
To: Philip Scott <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Donnerstag, 26. Februar 2009 12:26 schrieb Philip Scott:
> Hi ho,
>
> > One more improvement, include also Enum among the derived classes, then
> > you can write
> >
> > months = cycle [Jan .. Dec]
> >
> > Oh, and cycle is also exported from the Prelude, so it's not necessary to
> > import Data.List for that (though you will probably want to imoprt it
> > anyway).
>
> Ohh so many little tasty titbits of Haskell - I was trying for ages to
> get the '..' operator to work. I guess I will start to know where to
> look for these guys as time goes on.
The '..' things are syntactic sugar for enumerations:
[a .. ] === enumFrom a
[a, b .. ] === enumFromThen a b
[a .. b] === enumFromTo a b
[a, b .. c] === enumFromThenTom a b c
they are methods of class Enum.
:i Enum
in ghci or hugs gives the class information (methods and instances currently
in scope).
One place to look for such things is the Haskell report, also many tutorials
and books have a sectioned 'Overview of standard type classes' or some such,
there you should find this and similarly important things.
Another method is to ask ghci or hugs - you have to do it the right way, which
is not always obvious - as in
Prelude> :t \a b -> [a .. b]
\a b -> [a .. b] :: (Enum t) => t -> t -> [t]
so you know you have to look at Enum
Prelude> :i Enum
class Enum a where
succ :: a -> a
pred :: a -> a
toEnum :: Int -> a
fromEnum :: a -> Int
enumFrom :: a -> [a]
enumFromThen :: a -> a -> [a]
enumFromTo :: a -> a -> [a]
enumFromThenTo :: a -> a -> a -> [a]
-- Defined in GHC.Enum
instance Enum Integer -- Defined in GHC.Num
instance Enum Float -- Defined in GHC.Float
instance Enum Double -- Defined in GHC.Float
instance Enum Bool -- Defined in GHC.Enum
instance Enum Ordering -- Defined in GHC.Enum
instance Enum Char -- Defined in GHC.Enum
instance Enum () -- Defined in GHC.Enum
instance Enum Int -- Defined in GHC.Enum
looking at this, you don't see '..', but you see two functions with the
correct type, that it's more likely to be enumFromTo rather than enumFromThen
can easily be inferred from the functions' names.
>
> Cheers,
>
> Philip
Cheers,
Daniel
------------------------------
Message: 3
Date: Thu, 26 Feb 2009 12:55:56 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Meaning of '!' in record defintion?
UNPACK?
To: Magnus Therning <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Donnerstag, 26. Februar 2009 12:36 schrieb Magnus Therning:
> Are there any guarantees here, or is it just me telling the compiler
> to please do what I say, but the compiler can decide not to follow my
> wishes?
I don't think there are guarantees, as the section ends with:
"If a field cannot be unpacked, you will not get a warning, so it might be an
idea to check the generated code with -ddump-simpl.
See also the -funbox-strict-fields flag, which essentially has the effect of
adding {-# UNPACK #-} to every strict constructor field."
But it should be very likely to do what you want (of course, always compile
with -O or -O2).
> Is address aligning predictable?
Absolutely no idea, sorry.
>
> I'm basically wondering if it'd be possible to to use this mechanism
> to avoid using FFI when reading data that basically is a dump of
> structs in a C program.
I doubt it, as I understand it, with {-# UNPACK #-}, you'll have the layout
constuctor rawdata
and the C structs won't have the constructor.
>
> /M
Cheers,
Daniel
------------------------------
Message: 4
Date: Thu, 26 Feb 2009 20:58:02 +0100
From: Francesco Bochicchio <[email protected]>
Subject: [Haskell-beginners] Array and IArray
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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-0001.htm
------------------------------
Message: 5
Date: Thu, 26 Feb 2009 17:59:39 -0300
From: Rafael Gustavo da Cunha Pereira Pinto
<[email protected]>
Subject: Re: [Haskell-beginners] Array and IArray
To: Francesco Bochicchio <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Please, post the error messages.
Maybe all is needed is an explicit type declaration...
On Thu, Feb 26, 2009 at 16:58, Francesco Bochicchio <[email protected]>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
> [email protected]
> 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-0001.htm
------------------------------
Message: 6
Date: Thu, 26 Feb 2009 22:17:31 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Array and IArray
To: Francesco Bochicchio <[email protected]>, beginners
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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
------------------------------
Message: 7
Date: Thu, 26 Feb 2009 18:55:31 -0300
From: Rafael Gustavo da Cunha Pereira Pinto
<[email protected]>
Subject: Fwd: [Haskell-beginners] Array and IArray
To: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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 <[email protected]>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.htm
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 8, Issue 26
****************************************