[jhc] How to add Num instance to Foreign.C.Types.CLong?

John Meacham john at repetae.net
Sun Sep 20 19:15:11 EDT 2009


On Sun, Sep 20, 2009 at 01:12:14PM -0400, David Roundy wrote:
> I'm wondering how one would go about adding a Num instance to CLong?

Ah. types like this have some (annoying) history behind them I have been
trying to rewrite, slowly but surely.

Basically, when I first started writing jhc, I figured I'd start with a
simple haskell 98 compiler then add extensions (like unboxed types)
later. Which turned out to be a big mistake. unboxed types should have
been there from day one. they made so many things way simpler. The whole
PrimitiveOperators generated code mess is due to me having to have the
compiler internally generate all these instances for
Int,Int32,Word,etc... instances which would have been perfectly
expressible in haskell had I had unboxed types. 

So, originally, primitive types were implemented as so

> data Int

and the compiler had some magic to add instances and a representation to
them. which was fairly complicated, because in order to have the
compiler add a 'Num' instance, it meant 'Num' had to have special
meaning to the compiler, so there was a cascade effect of adding way
more library specific code to the compiler than I liked.

The proper way to do such types nowadays would be

> data Int = Int Bits32_

or 

> newtype Int = Int Int32
>     deriving(...)

then manually create all the instaces. (well, I have m4 macros to make
this easy for many instances)

So. Long answer short, types like CLong need to be rewritten to be
concrete types. like Float and Double.

If you just want to get something to work, you can change CLong to be 

> newtype CLong = CLong IntMax

and then you can get at the underlying IntMax which has the right
instances defined. 

        John


-- 
John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/


More information about the jhc mailing list