Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: OpenGL keyboardMouseCallback ...confused over type
(Arlen Cuss)
2. Re: OpenGL keyboardMouseCallback ...confused over type
(Sean Charles)
3. Re: OpenGL keyboardMouseCallback ...confused over type
(Daniel Fischer)
4. Re: OpenGL keyboardMouseCallback ...confused over type (Alexey G)
----------------------------------------------------------------------
Message: 1
Date: Fri, 01 Jul 2011 17:21:41 +1000
From: Arlen Cuss <[email protected]>
Subject: Re: [Haskell-beginners] OpenGL keyboardMouseCallback
...confused over type
To: Sean Charles <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Sean,
On 1/07/2011 5:04 PM, Sean Charles wrote:
> 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...
Such is typically my experience, too :D
> 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...
Yes, you're right. I don't mean to imply there's anything special about
the function you get as a return result of only a partial application.
After all, for some f where:
> f :: Int -> Char -> String -> a
Then:
> f 1 'a' "xyz"
is exactly the same as saying:
> ((f 1) 'a') "xyz"
i.e. each argument is bound and fills in the first type in the function
type. Thus:
> f :: Int -> Char -> String -> a
> f 1 :: Char -> String -> a
> f 1 'a' :: String -> a
> f 1 'a' "xyz" :: a
And thus all parameters 'assigned' (if you like) yield the eventual
result (of no type in particular in this example).
> On page 42(!)
:D
> 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" ?
I'm not sure how they're represented internally, but they (data
constructors) might as well be functions -- or values, in the case of
nullary constructors (no-argument constructors, like Nothing of the
Maybe type) -- except that you can also pattern match on them!
Outside of the content of pattern matching, they act just like functions
(or plain values) which can be used curried to all your heart's content.
Yum!
> *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" ?
The value you get when you say `Book' or `Paperback' in a value context
is indeed a function, but those words can also be used in pattern
matching. I think this more-or-less sums up dealing with them.
When you use record types (may be yet to come in RWH), you can also use
them as functions, or you can use the record syntax (which is just sugar
atop a normal function use).
> I think I am understanding things a little better now but some expert
> clarification would help!
In which case some experts' opinions on whether calling them functions
(or not) is just a game of semantics would be much appreciated!
> Is this line of reasoning correct:
>
> o Haskell is functional therefore...
> o *everything* is a function
Not quite - your plain values aren't (7 isn't a function; neither is
'Just 3'. 'Just' is, though! Comes in handy. e.g. `map Just' returns a
function that takes a list and returns the same list with all elements
in a `Just'. Of course there are nicer ways to do this more monadically ..).
But functions *are* first-class values.
> o functions are automatically curried where required (to get partials)...
Yep. I don't see it so much as 'automatic currying' (though it's
accurate), but more that
> f x y = x + y
is a syntax sugar for
> f = \x -> \y -> x + y
, thus currying. (I may be off on the technical details, but I think
evaluating to WHNF may imply such a conversion?)
> o data constructors *are* functions (as opposed to being a separate
> thing like structs in C for example)...
Yes, and pattern matchable. Note that structs also miss out on the
possibities of Haskell types -- there can be many data constructors to
one type (which you usually would use a tagged union for, in C, and
indeed it's the same thing in Haskell).
> o data constructors can be partially applied (Eureka![?])
Indeed! This can often come in handy.
> Man, I think your answer is more understandable just from typing the
> above questions, surely the sign of a great answer to a question.
I'm glad it helped you, and that you think so, but most of the work
happened in your head!
> Thanks!
> Sean.
Cheers,
Arlen
> 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
>
------------------------------
Message: 2
Date: Fri, 01 Jul 2011 09:19:04 +0100
From: Sean Charles <[email protected]>
Subject: Re: [Haskell-beginners] OpenGL keyboardMouseCallback
...confused over type
To: Arlen Cuss <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
On 30/06/11 23:53, Arlen Cuss wrote:
[snip]
>> and modify the assignment in main to this:
>>
>> keyboardMouseCallback $= Just (myKeyMouse gameState)
I think that part of my confusion was, on reflection, caused by the fact
that coming from a procedural background, the syntax of "Just
(myKeyMouse gameState) still reads as "function call arguments", so if I
may be permitted to expand on that to see if I now understand Haskell a
little more ...
The form: "Just (myKeyMouse gameState)" looks like its calling the Just
function with two parameters as the () is also used to define a tuple.
So is (myKeyMouse gameState) a tuple of one, the result of calling that
function with that parameter or is just a way of ensuring that the
function is performed before it is passed to Just as the one and only
parameter?
I know the correct answer but I did sometimes get confused over when is
a tuple "()" not a tuple, especially in things like this:
foo (x:xs) = ...
I *know* it is pattern matching using ':' and that ':' takes a thing and
a list of things and returns a list of same things but old habits.. DOH!
Back to the chase.
If I had rewritten the Just called like this I think it would have been
clearer about what was happening:
Just $ myKeyMouse gameState
as that quite clearly says, call myKeyMouseFirst and then hand the
result to Just. From that point, Arlens great description of how it
trundles through the type system, resulting in a partially evaluated
function that has the *same* type as the expected one for the
keyboardMouseCallback is really making it clearer for me now. That was a
loud Eureka moment in my kitchen over breakfast this morning I can tell you!
It just needs to settle in and join the rest of the facts jostling for
top position on the "Don't forget..." list.
Once again "list", I thank thee muchly for helping this troubador of
trouble travel the road to Haskell...
:)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110701/dd75b4e8/attachment-0001.htm>
------------------------------
Message: 3
Date: Fri, 1 Jul 2011 10:55:34 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] OpenGL keyboardMouseCallback
...confused over type
To: [email protected]
Cc: Sean Charles <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
Just to elaborate a bit:
On Friday 01 July 2011, 10:19:04, Sean Charles wrote:
> On 30/06/11 23:53, Arlen Cuss wrote:
> [snip]
>
> >> and modify the assignment in main to this:
> >> keyboardMouseCallback $= Just (myKeyMouse gameState)
>
> I think that part of my confusion was, on reflection, caused by the fact
> that coming from a procedural background, the syntax of "Just
> (myKeyMouse gameState) still reads as "function call arguments", so if I
> may be permitted to expand on that to see if I now understand Haskell a
> little more ...
>
> The form: "Just (myKeyMouse gameState)" looks like its calling the Just
> function with two parameters as the () is also used to define a tuple.
No, the tuple constructors have commas, "(a,b)" is a tuple, "(a b)" is a
grouping, meaning "the function a applied to the argument b". Putting such
in parentheses may be necessary if e.g., like here, that expression is the
argument of another function.
Just (myKeyMouse gameState) === Just $ myKeyMouse gameState
is "the function [value constructor] Just, applied to the result of
applying myKeyMouse to gameState".
> So is (myKeyMouse gameState) a tuple of one, the result of calling that
> function with that parameter or is just a way of ensuring that the
> function is performed before it is passed to Just as the one and only
> parameter?
Yes, except Haskell doesn't have a concept of one-tuples, they're just
plain values.
>
> I know the correct answer but I did sometimes get confused over when is
> a tuple "()" not a tuple, especially in things like this:
Parentheses are
a) a value [or type] constructor, when there's nothing in between: ()
b) grouping signifiers, like in (3+4)*5 or in
foo (x:xs) = ...
where they delimit the pattern to match; without the parentheses, it would
be parsed as
(foo x) : xs = ...
which would be a definition of (:), except it would be a syntax error
[foo x is not a valid patter] and (:) is an infix-constructor, which can't
be defined by a function binding]
c) a means to make prefix functions from infix operators, like (++)
Visually similar to parentheses, but different are tuple constructors:
(,) :: a -> b -> (a,b)
(,,) :: a -> b -> c -> (a,b,c)
...
the parentheses are part of the constructors, these constructors can be
applied prefix or roundfix (special wired in syntax, as for lists,
"[] a" is the same type as "[a]", but for tuples, it works for values too).
>
> foo (x:xs) = ...
>
> I *know* it is pattern matching using ':' and that ':' takes a thing and
> a list of things and returns a list of same things but old habits.. DOH!
>
> Back to the chase.
>
> If I had rewritten the Just called like this I think it would have been
> clearer about what was happening:
>
> Just $ myKeyMouse gameState
>
> as that quite clearly says, call myKeyMouseFirst and then hand the
> result to Just. From that point, Arlens great description of how it
> trundles through the type system, resulting in a partially evaluated
> function that has the *same* type as the expected one for the
> keyboardMouseCallback is really making it clearer for me now. That was a
> loud Eureka moment in my kitchen over breakfast this morning I can tell
> you!
>
> It just needs to settle in and join the rest of the facts jostling for
> top position on the "Don't forget..." list.
>
> Once again "list", I thank thee muchly for helping this troubador of
> trouble travel the road to Haskell...
>
> :)
------------------------------
Message: 4
Date: Fri, 1 Jul 2011 12:58:51 +0300
From: Alexey G <[email protected]>
Subject: Re: [Haskell-beginners] OpenGL keyboardMouseCallback
...confused over type
To: [email protected]
Cc: Sean Charles <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello. I didn't read the thread, but I think, that this code help you
https://gist.github.com/1031576.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110701/6ef9d51f/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 2
****************************************