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: Performance problem with Haskell/OpenGL/GLFW
(Brandon Allbery)
2. Re: Trouble in (Fast)CGI land: matching `CGIT IO a0' with
actual type `IO ()' (David McBride)
3. OpenGLRaw Tutorial (Michael Baker)
4. Using stack inside a function without declaring it as input
(doaltan)
5. Re: OpenGLRaw Tutorial (Emanuel Koczwara)
6. Re: OpenGLRaw Tutorial (Mateusz Kowalczyk)
----------------------------------------------------------------------
Message: 1
Date: Sun, 10 Mar 2013 19:44:38 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Performance problem with
Haskell/OpenGL/GLFW
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<CAKFCL4XtTgNUUR9OXhG_2Pw7ZdUd+-i=uv7kygqrbman4ts...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Sun, Mar 10, 2013 at 6:27 PM, Hollister Herhold <[email protected]>wrote:
> I guess running X11 forces use of the NVidia chip. Interesting.
>
Yes; you can see some discussion about it on Apple's X11-Users list, if you
care.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130310/e2700f68/attachment-0001.htm>
------------------------------
Message: 2
Date: Sun, 10 Mar 2013 23:31:06 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] Trouble in (Fast)CGI land: matching
`CGIT IO a0' with actual type `IO ()'
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID:
<can+tr40awqq_uvrrcxnwv3zh7550jq2367a9ubwq6g+kk_3...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Let me put it plainly. I'm not going to explain the theory, just cold
hard practicality of how to do what you want to do when you see this
in the wild, which you will if you haven't already.
Let's call this a pattern in haskell. It is the monad transformer
pattern, which you'll see many (possibly even most) libraries use at
one time or another once they get past a certain amount of complexity.
When you don't understand it, you feel like you can't quite do
anything in haskell, at least not without copy paste or lots of random
fiddling. But then once you know it, suddenly most of the libraries in
the language open up to you. At least that is how it was for me.
Whenever you see a library that has a runBlah function which will have
a type: Blah a -> IO b. Examples:
shelly :: MonadIO m => Sh a -> m a (from the shelly library)
runStateT :: StateT s m a -> s -> m (a, s) (from control.monad.state)
runInputT :: MonadException m => Settings m -> InputT m a -> m a (from
haskeline)
runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a (from
the resourceT package)
atomically :: STM a -> IO a (from base, from the shared transactional
memory related functions)
runParserT :: Stream s m t => ParsecT s u m a -> u -> SourceName -> s
-> m (Either ParseError a) (from parsec)
runFastCGI :: CGI CGIResult -> IO () (from the fastcgi library)
So as you can see they are different, but they all have some
similarities in that they all return either IO a, or possibly m a
where m has some class restrictions on it. IO will almost always meet
those restrictions if you try it. Sometimes they have special
arguments to get their environments set up so that they have the info
they need to work. Sometimes they don't. What they all have in
common is that they all take an argument that sort of lives in a
specific type (InputT, ResourceT, ParsecT, Sh, STM, StateT, etc.).
Sometimes there are several alternatives with slightly different
functionality.
If you find a function that returns one of those types, then it was
meant to be run as an argument to a function like one of these.
Sometimes the types are simplified with type aliases so you don't
always recognize them off the bat. Parser a aliases to Parsec String
() a which is an alias for ParsecT String () Identity a. In your
original message it was the case that CGI CGIResult is an alias for
CGIT IO CGIResult, so the base monad was in that case CGIT, and not
CGI.
So you've figured this out, now you can run a procedure composed of
functions which return CGI a. Suddenly you realize in the middle of
the procedure you need to run an IO function. Perhaps you need to get
some info from a file. Well each of these monads uses the transformer
library. If you go onto hackage and you look up say, ParsecT for
example, you will see a whole lot of instances like these for each of
them.
MonadError e m => MonadError e (ParsecT s u m)
MonadReader r m => MonadReader r (ParsecT s u m)
MonadState s m => MonadState s (ParsecT s' u m)
MonadTrans (ParsecT s u)
Monad (ParsecT s u m)
Functor (ParsecT s u m)
MonadPlus (ParsecT s u m)
Applicative (ParsecT s u m)
Alternative (ParsecT s u m)
MonadIO m => MonadIO (ParsecT s u m)
MonadCont m => MonadCont (ParsecT s u m)
These instances all represent different tricks that can be performed
while stuck in the ParsecT monad. The one that concerns us right now
is that instance for MonadIO. Any type that is an instance of MonadIO
has the ability to use the liftIO function, which is defined in the
MonadIO class. All this lets you do is call any IO function in it.
Sometimes the instance is a little wonky and you might not recognize
it right off the bat, for example:
(Monad (CGIT m), MonadIO m) => MonadIO (CGIT m)
So, is CGIT an instance of MonadIO or not? Well it is if CGIT m is a
Monad and the m, whatever it is, is an instance MonadIO. If you look
at IO, IO happens to be an instance of MonadIO, so therefore you can
use liftIO in there whenever you please. Now lets look at the
instances for STM:
Monad STM
Functor STM
Typeable1 STM
MonadPlus STM
Applicative STM
Alternative STM
Huh there's no MonadIO instance here. How am I supposed to read my
file? Well the answer is that STM is meant for atomic actions and the
only way it can guarantee that no one tried to do anything funny while
it was in the STM monad is to restrict it so that you cannot do IO in
the middle of it. There is no real way to subvert it, since you don't
have access to the internals of STM, you could never write a MonadIO
instance for it. The designer intended it to be this way. That tells
you quite a bit about what that library was intended for.
So now notice all those other instances. What do they do? Well they
do lots of useful things the library writer thought you might like to
be able to do.
Applicative lets you use applicative style programming within the
monad. You can see tons of examples of that in Parsec, just google
it.
Alternative allows you to use the alternative operator when you are
within that monad, which I find super useful and I wish people used it
more. So you can try to do one thing and if it fails, try another (x
<- (tryA <|> tryB)). It can be used to greatly simplify code,
especially in parsers.
Functor allows you to fmap its return result to another type without
exiting the monad. (string <- fmap show $ (char 'a' :: Parser Char)
:: Parser String) from parsec for example. This is a really common
one and nearly all such types will have a Functor instance.
MonadState would allow you to get and set a variable in the monad's
environment. Sometimes that state is set by you in the run function
(which in fact it is for both StateT and ParsecT). Sometimes it is
the internal state of the library. If you can supply it you can use
it to carry some arbitrary state along the program for you, and access
and update it whenever you need to, and even return it at the end.
So when you see a mysterious monad somewhere in a library, give it a
look on hackage. You may find that it has some sensible functionality
for you to make use of. You may also get a sense of how the library
creator expects you to use his library.
On Sun, Mar 10, 2013 at 7:21 PM, emacstheviking <[email protected]> wrote:
>
> David,
>
> At times like this I think am not even fit to code PHP for my day job.
> I am going to have to read that very carefully when I wake up tomorrow.
>
> Thanks.
>
>
>
> On 10 March 2013 22:59, David McBride <[email protected]> wrote:
>>
>> Your ipCamExec is IO (), but you are running it in the CGI a monad which is
>> a type alias for CGIT IO a. CGIT is an instance of MonadIO, so try liftIO
>> ipCamExec. liftIO has a type MonadIO m => IO a -> m a, which means that if
>> you replace m with CGIT IO, you would get IO a -> CGIT IO a, which is
>> exactly what you need.
>>
>> On Sun, Mar 10, 2013 at 6:40 PM, emacstheviking <[email protected]> wrote:
>>>
>>> I am writing a stop-motion capture application using AngularJS and it's
>>> going OK. I was inspired to do so after installing "IPCamera" on my phone
>>> and Sony tablet. A typical IPCamera session lives on an internal address
>>> like this, this example will turn on the LED on the camera:
>>>
>>> http://192.168.0.5:8080/enabletorch
>>>
>>> Just because I can (or so I thought), I decided to write a tiny little
>>> FastCGI application in Haskell to act as a proxy using the PATH_INFO
>>> variable. This means that in to my Javascript code I have this code in a
>>> service file:
>>>
>>> angular.module('stomoServices', ['ngResource']).
>>> factory(
>>> 'IPCamera',
>>> function($resource, urlIPCameraAPI) {
>>> return $resource(
>>> urlIPCameraAPI,
>>> {}, {
>>> ledOn: { method: 'GET', params: {featureReq: 'enabletorch' }},
>>> ledOff: { method: 'GET', params: {featureReq: 'disabletorch' }},
>>> focusOn: { method: 'GET', params: {featureReq: 'focus' }},
>>> focusOff: { method: 'GET', params: {featureReq: 'nofocus'}}
>>> });
>>> });
>>>
>>> and I then issue commands like "IPCamera.ledOn()" etc. All very nice except
>>> that it doesn't work yet because I can't get the worlds seemingly simplest
>>> CGI application to compile yet! Here is the code that I have, it could be
>>> "cleared up" but this is what I have so far:
>>>
>>> main :: IO ()
>>> main = runFastCGI . handleErrors $ do
>>> command <- getVar "PATH_INFO"
>>> case command of
>>> Nothing ->
>>> outputError 400 "Missing IPCamera instruction (PATH_INFO)" []
>>> Just cmd ->
>>> ipCamExec (tail cmd) >> output "OK" -- tail drops the "/"
>>> where
>>> ipCamExec :: String -> IO ()
>>> ipCamExec url = do
>>> simpleHTTP (getRequest url) -- don't want or need response.
>>> return () -- to match the return type or so I thought.
>>>
>>> and the error message I cannot seem to understand as it fills me with
>>> monadic fear which I can't get out of:
>>>
>>> ipcamera.hs:16:7:
>>> Couldn't match expected type `CGIT IO a0' with actual type `IO ()'
>>> In the return type of a call of `ipCamExec'
>>> In the first argument of `(>>)', namely `ipCamExec (tail cmd)'
>>> In the expression: ipCamExec (tail cmd) >> output "OK"
>>>
>>> Please could some kind souls explain to me in simple terms just what is
>>> going on and why I am close to tears right now? I have read the definitions
>>> of CGIResult and CGI and they leave me cold. I am trying to understand
>>> monads more but at times like this I once again realise what a complete
>>> rank beginner I am!
>>>
>>> Thanks.
>>> Sean.
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Mon, 11 Mar 2013 01:45:26 -0500
From: Michael Baker <[email protected]>
Subject: [Haskell-beginners] OpenGLRaw Tutorial
To: [email protected]
Message-ID:
<CACwW0Ub0pwtJTi9Qs=4mxCkrMafie31MW8DGseeBz=hf9-x...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
I'm trying to follow this book on OpenGL:
http://www.arcsynthesis.org/gltut/Basics/Tut01%20Following%20the%20Data.html
I'm trying to follow the examples using the OpenGLRaw package as the OpenGL
package doesn't map very neatly to any of the examples. For instance, it is
not clear to me now to create and use the vertex array used in the example
that I linked to.
However, I'm struggling a bit because I haven't used Haskell's foreign
interface before. Here is an attempt which is expected to draw a triangle,
but instead draws nothing: http://hpaste.org/83837
Does anyone know of a tutorial for OpenGLRaw or the foreign interface that
might help me understand how to marshall data around? It seems like many
people turn to OpenGLRaw when they're learning OpenGL so that they can
follow the tutorials. I imagine it would be useful to have a guide out
there that covers how to actually use it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130311/44d67648/attachment-0001.htm>
------------------------------
Message: 4
Date: Mon, 11 Mar 2013 09:36:30 +0000 (GMT)
From: doaltan <[email protected]>
Subject: [Haskell-beginners] Using stack inside a function without
declaring it as input
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi I have a function like this :
myfunc :: [Char] -> [Char]
It? is supposed to work pretty much like this :
1. Take a string
2. Put some elements of this input string to output string and put
others to stack.
3. Pop elements to that output string too.
4. Do 2 and 3 recursively until stack is empty.
5. Print the output string when stack is empty.
I couldn't figure out where to define stack and output string. Can you help me
with that? I'm new to Haskell so I can't think in Haskell's logic very well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130311/585a4ba9/attachment-0001.htm>
------------------------------
Message: 5
Date: Mon, 11 Mar 2013 10:59:24 +0100
From: Emanuel Koczwara <[email protected]>
Subject: Re: [Haskell-beginners] OpenGLRaw Tutorial
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Message-ID: <1362995964.13571.2.camel@emanuel-Dell-System-Vostro-3750>
Content-Type: text/plain; charset="UTF-8"
Hi,
Please look at: http://code.haskell.org/GLUT/examples/RedBook/
The Red Book is the first resource you should try (with these examples
translated to Haskell).
Emanuel
------------------------------
Message: 6
Date: Mon, 11 Mar 2013 09:59:29 +0000
From: Mateusz Kowalczyk <[email protected]>
Subject: Re: [Haskell-beginners] OpenGLRaw Tutorial
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 11/03/13 06:45, Michael Baker wrote:
> I'm trying to follow this book on
> OpenGL:
> http://www.arcsynthesis.org/gltut/Basics/Tut01%20Following%20the%20Data.html
>
> I'm trying to follow the examples using the OpenGLRaw package as the
> OpenGL package doesn't map very neatly to any of the examples. For
> instance, it is not clear to me now to create and use the vertex array
> used in the example that I linked to.
>
> However, I'm struggling a bit because I haven't used Haskell's foreign
> interface before. Here is an attempt which is expected to draw a
> triangle, but instead draws nothing: http://hpaste.org/83837
>
> Does anyone know of a tutorial for OpenGLRaw or the foreign interface
> that might help me understand how to marshall data around? It seems like
> many people turn to OpenGLRaw when they're learning OpenGL so that they
> can follow the tutorials. I imagine it would be useful to have a guide
> out there that covers how to actually use it.
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
I don't think that it's the best idea to learn OpenGL using Haskell
simply because of how drastically different C and Haskell are and for
the reason you mentioned yourself.
Nevertheless, you can find many examples at [1]. You can find things
such as the red book examples translated into Haskell as well as links
to other resources.
[1] - http://www.haskell.org/haskellwiki/Opengl
--
Mateusz K.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 57, Issue 14
*****************************************