[Haskell-cafe] Wondering about c type in haskell.

Jason Dagit dagit at codersbase.com
Mon Jun 29 23:37:27 EDT 2009

On Mon, Jun 29, 2009 at 7:01 PM, Magicloud Magiclouds <
magicloud.magiclouds at gmail.com> wrote:

> Hi,
>  There are times, when we need to make some system calls, or call C
> library. So we have to deal with C data types.
>  For example, CPid, which is an integer, actually. So we can do
> "fromIntegral pid". Then why do not we define "type CPid = Integer",
> and convert Haskell Integer with C Int internally. So the "user" does
> not have to care about the type convert (everywhere, which is ugly).
> And, specially, when doing something like serialisation, for Haskell
> Integer, the user does not have to do things with precision. But for
> CPid, without the fromIntegral, we have to specify its precision,
> well, on different machine/OS, the precision may not be the same.

If you use Int, Integer, Word, etc to represent all your discrete values you
can *accidentally* reuse the values for different purposes.  So in Haskell
we like to put a 'newtype' wrapper around the integral type so that it has a
different identity to the type system even when the values are equal as
numbers.  C is notorious for allowing this type of value reuse and the way
that it leads to buggy programs is pretty well document at this point, I
believe (although, I take it for granted and I haven't checked for said

Also, in Haskell we don't like implicit, or even ambiguous, type conversions
and that's just a preference shared by the majority of Haskell programmers.
We like to believe that it saves us from ourselves in a sense.  So for these
reasons, CPid isn't just an Int32 or Word32.  We want to be type safe.  The
price we pay is that at key points in the program you have to explicitly
state the transformation you want (fromIntegral).

I hope that helps clarify the reasoning.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090629/c77f8d5d/attachment.html

More information about the Haskell-Cafe mailing list