open gl type conversions

Following code GLFW.setWindowSizeCallback w (Just (\window w h -> do GL.viewport $= (GL.Position 0 0, GL.Size w h) emo1.hs:42:50: Couldn't match type ‘Int’ with ‘Foreign.C.Types.CInt’ Expected type: GLsizei Actual type: Int In the first argument of ‘Size’, namely ‘w’ In the expression: Size w h I can never seem to figure out if i really need to do a type conversion, or just the make the appropriate type declarations. Sometimes it seems to be a combination of both. Part of the complication here is that w and h are, in fact, `Int` because that's there type in the type signature of WindowSizeCallback (Window -> Int -> Int -> IO () ). 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. Humorously i tried putting Int->CInt in hoogle, and the only thing that looked like it might be useful was unsafeCoerce, but I'm pretty sure that's not the right answer. I was wondering if someone could enlighten me on exactly how you can trace out the solution to this sort of problem because it seeems to come up often (see, for example my confusion over FFTW r). Brian

2015-05-22 7:13 GMT+02:00
[...] 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 http://hackage.haskell.org/package/OpenGLRaw-2.5.0.0/docs/Graphics-Rendering... arguments, and GLsizei is a type synonym for CInt http://hackage.haskell.org/package/base-4.8.0.0/docs/Foreign-C-Types.html#t:..., 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: fromIntegral http://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#v:fromInte... :: (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 no-op. Cheers, S.

On Fri, 22 May 2015 08:56:59 +0200
Sven Panne
2015-05-22 7:13 GMT+02:00
: [...] 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 http://hackage.haskell.org/package/OpenGLRaw-2.5.0.0/docs/Graphics-Rendering... arguments, and GLsizei is a type synonym for CInt http://hackage.haskell.org/package/base-4.8.0.0/docs/Foreign-C-Types.html#t:..., 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:
fromIntegral http://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#v:fromInte... :: (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 no-op.
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 and 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. Brian
participants (2)
-
briand@aracnet.com
-
Sven Panne