[Haskell-beginners] OpenGL keyboardMouseCallback ...confused over type

Sean Charles sean at objitsu.com
Fri Jul 1 09:04:31 CEST 2011


Arlen,

thanks for that... I think it confirms that my head was on "the right 
track" but it was pitch-black in there and I had my eyes closed. It 
saddens me that the answer to one of my questions is harder to 
understand than the question was to write in the first place, LMAO, I 
love Haskell for this... it's making me think about stuff more and more 
and I like it. I am determined that Haskell is the end of the line until 
son of Haskell comes out! LOL.

Would you mind clarifying a point for me regarding partial application, 
currying and types as I am now a bit confused... I've written enough 
point free stuff to understand both partial application and currying so 
that's fine... what's confusing me is in your reply you say:

   "So the partial application returns a function with the first argument
   fixed -- and the resulting function's type is that which is used for the
   callbacks. "

Am I correct in thinking then that data types are constructors, *are* 
_just functions too_, I think that's correct and if so I will re-read 
RWH in certain places to see how it wasn't clear enough to me...

On page 42(!) of Real World Haskell it says: "We can create a new value 
of type BookInfo by treating Book as a function and applying it with 
arguments of types Int, String, and [String]".

On page 41 we have this definition:
   data BookInfo = Book Int String [String] deriving (Show)

So, is "Book" a function or is it not a function, as implied by the 
statement "by treating Book _as_ a function" ? That would help me 
greatly, to know if data constructors are just functions or something 
"special" (constructors of data types) but for all intents and purposes 
they smell, act, look and behave as functions. Singing: "La dee da La 
dee dee, Eric the half-a-boook...."

I created a file containing that definition and loaded it into ghci:

*Main> :type Book
Book :: Int -> String -> [String] -> BookInfo

This looks like a function to me! LOL, so I added another constructor 
like so:

data BookInfo = Book Int String [String]
                 | Paperback Int String [String]
                 deriving(Show)

...and was beheld unto me...

*Main> :type Paperback
Paperback :: Int -> String -> [String] -> BookInfo

Hmmm... another function returning the same -*-. So is the sentence a 
bit misleading to say "as a function" when it means, at least as far as 
I can see "because Book _is_ a function" ?

I think I am understanding things a little better now but some expert 
clarification would help!




Is this line of reasoning correct:

   o Haskell is functional therefore...
   o *everything* is a function
   o functions are automatically curried where required (to get partials)...
   o data constructors *are* functions (as opposed to being a separate
      thing like structs in C for example)...
   o data constructors can be partially applied (Eureka![?])


Man, I think your answer is more understandable just from typing the 
above questions, surely the sign of a great answer to a question.

Thanks!
Sean.

On 30/06/11 23:53, Arlen Cuss wrote:
>> confusing bit... to make myself learn things I typed out the full type
>> of the keyboardMouseCallback function:
> (you'll actually do this in production code too! It's a good habit.)
>
>>      myKeyMouse :: Key ->  KeyState ->  Modifiers ->  Position ->  IO ()
>>
>> and slotted it into the code like so:
>>
>>      keyboardMouseCallback $= Just myKeyMouse
> Okay -- so here we're seeing that keyboardMouseCallback itself is an IORef:
>
>> keyboardMouseCallback :: IORef (Maybe (Key ->  KeyState ->  Modifiers ->
> Position ->  IO ()))
>
> Keep in mind also:
>
>> ($=) :: IORef a ->  a ->  IO ()
>> In order to be able to pass the "IORef GameState" into the keyboard
>> handler I have to re-type my keyboardMouseCallback function like so:
>>
>>      myKeyMouse :: IORef GameState ->  Key ->  KeyState ->  Modifiers ->
>> Position ->  IO ()
>>
>> and modify the assignment in main to this:
>>
>>      keyboardMouseCallback $= Just (myKeyMouse gameState)
> Now let's look at the reason it works.
>
> Your types are:
>
>> keyboardMouseCallback :: IORef (Maybe (Key ->  KeyState ->  Modifiers ->
> Position ->  IO ()))
>> myKeyMouse :: IORef GameState ->  Key ->  KeyState ->  Modifiers ->
> Position ->  IO ()
>
> Now, what's the type of the expression `myKeyMouse gameState'? Well,
> you've assigned the first argument -- all functions are curried -- so
> you get a partial application:
>
>> myKeyMouse gameState :: Key ->  KeyState ->  Modifiers ->  Position ->  IO ()
> Now you wrap that in Just:
>
>> Just (myKeyMouse gameState) :: Maybe (Key ->  KeyState ->  Modifiers ->
> Position ->  IO ())
>
> What was the type of keyboardMouseCallback again?
>
>> keyboardMouseCallback :: IORef (Maybe (Key ->  KeyState ->  Modifiers ->
> Position ->  IO ()))
>
> So the partial application returns a function with the first argument
> fixed -- and the resulting function's type is that which is used for the
> callbacks.
>
> HTH a little.
>
> A

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110701/65ab83fd/attachment-0001.htm>


More information about the Beginners mailing list