[Haskell-beginners] Meaning of '!' in record defintion? UNPACK?

Magnus Therning magnus at therning.org
Thu Feb 26 06:36:29 EST 2009

On Thu, Feb 26, 2009 at 11:33 AM, Daniel Fischer
<daniel.is.fischer at web.de> wrote:
> Am Donnerstag, 26. Februar 2009 12:07 schrieb Colin Paul Adams:
>> >>>>> "Thomas" == Thomas Davie <tom.davie at gmail.com> 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
> Beginners at haskell.org
> 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
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.


Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe

More information about the Beginners mailing list