[Haskell-cafe] New OpenGL package: efficient way to convert datatypes?

Nick Bowler nbowler at elliptictech.com
Thu Mar 4 10:45:03 EST 2010

On 16:20 Thu 04 Mar     , Daniel Fischer wrote:
> Yes, without rules, realToFrac = fromRational . toRational.


> I think one would have to add {-# RULES #-} pragmas to 
> Graphics.Rendering.OpenGL.Raw.Core31.TypesInternal, along the lines of
> {-# RULES
> "realToFrac/CDouble->GLdouble"  realToFrac x = GLdouble x
> "realToFrac/GLdouble -> CDouble" realToFrac (GLdouble x) = x
>   #-}

These rules are, alas, *not* equivalent to fromRational . toRational.

Unfortunately, realToFrac is quite broken with respect to floating point
conversions, because fromRational . toRational is entirely the wrong
thing to do.  I've tried to start some discussion on the haskell-prime
mailing list about fixing this wart.  In the interim, the OpenGL package
could probably provide its own CDouble<=>GLDouble conversions, but sadly
the only way to "correctly" perform Double<=>CDouble is unsafeCoerce.

Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)

More information about the Haskell-Cafe mailing list