[Haskell-cafe] open gl type conversions

24 views
Skip to first unread message

bri...@aracnet.com

unread,
May 22, 2015, 1:14:08 AM5/22/15
to haskel...@haskell.org
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
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Sven Panne

unread,
May 22, 2015, 2:57:15 AM5/22/15
to bri...@aracnet.com, haskel...@haskell.org
2015-05-22 7:13 GMT+02:00  <bri...@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 arguments, 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:

   fromIntegral :: (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.

bri...@aracnet.com

unread,
May 23, 2015, 12:23:53 AM5/23/15
to Sven Panne, haskel...@haskell.org
On Fri, 22 May 2015 08:56:59 +0200
Sven Panne <sven...@gmail.com> wrote:

> 2015-05-22 7:13 GMT+02:00 <bri...@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
> <http://hackage.haskell.org/package/OpenGLRaw-2.5.0.0/docs/Graphics-Rendering-OpenGL-Raw-Types.html#t:GLsizei>
> 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: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:
>
> fromIntegral
> <http://hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#v:fromIntegral>
> :: (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.
Reply all
Reply to author
Forward
0 new messages