[Haskell-cafe] open gl type conversions
briand at aracnet.com
briand at aracnet.com
Sat May 23 04:23:45 UTC 2015
On Fri, 22 May 2015 08:56:59 +0200
Sven Panne <svenpanne at gmail.com> wrote:
> 2015-05-22 7:13 GMT+02:00 <briand at aracnet.com>:
> > [...] GL.Size really needs a CInt so it seems like I have to do some sort
> of explicit conversion, but I can never seem to figure out exactly how to
> find the necessary function.
> To be exact, Size expects 2 GLsizei
> and GLsizei is a type synonym for CInt
> but what this latter type actually is shouldn't matter most of the time.
> The only thing you need to know here is that it's an integral number, and
> your swiss army knife for conversion between integral numerical types is:
> :: (Integral a, Num b) => a -> b
> Don't be scared by its canonical definition (fromInteger . toInteger), GHC
> has enough RULES to make this very efficient most of the time or even a
aha. thanks very much.
I wanted to get a complete end-to-end understanding of this, so forgive the blabbering, but it might be useful to some random reader of the list.
viewport :: StateVar (Position, Size)
data Size = Size !GLsizei !GLsizei
type GLsizei = CInt
as you said- Size eventually resolves to
Size CInt CInt
and then, I think this is the part that has been tripping me up in general, because i'm still not quite getting how the whole typeclass qualification of arguments thing,
CInt : Instances -> Integral CInt
Int : Instances -> Integral Int
so Int and CInt are both instances of Integral.
fromIntegral :: (Integral a, Num b) => a -> b
but wait! it's not over. fromIntegral restricts (is that the right way to phrase it ?) the first argument to be instance of Integral and the second argument to an instance of Num.
So checking the Num typeclass I find that both Int AND CInt are instances. So truly fromIntegral is a swiss army knife...
It turns out hoogle is not your friend. It doesn't figure out that fromIntegral will work, unless of course, you put in the _exact_ type signature. Given the number of other options it provides it's really kind of weird that it doesn't list fromIntegral too. Is that a bug ?
Eventually if you keep perusing the Numeric section of the prelude you will run across it.
Quite a bit of work to convert an int to an int. lol.
Thanks again for your help.
More information about the Haskell-Cafe