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: merge two handles (Manfred Lotz)
2. Re: merge two handles (David McBride)
3. Re: merge two handles (Manfred Lotz)
4. Data.Foldable's foldr' (Federico Mastellone)
5. Re: Data.Foldable's foldr' (Daniel Fischer)
6. Re: Data.Foldable's foldr' (Yitzchak Gale)
----------------------------------------------------------------------
Message: 1
Date: Wed, 15 Jun 2011 15:23:03 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] merge two handles
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Tue, 14 Jun 2011 13:10:36 -0400
David McBride <[email protected]> wrote:
> It probably has to do more with parenthesis than anything:
>
> forever $ atomically $ readTchan chan >>= print
> forever $ (atomically $ readTchan chan) >>= print
>
> That might work. Once you get the types to line up, that should work.
>
Thanks for the hint. I finally got it compiled and almost working.
The code is now like this:
{-# LANGUAGE ScopedTypeVariables #-}
import GHC.Conc.Sync
import System.IO
import System.Environment
import System.Process
import Control.Monad
import Control.Concurrent.STM.TChan
import Control.Exception.Base
import Text.Printf
import Prelude hiding (catch)
makeThread :: Handle -> TChan String -> IO ThreadId
makeThread handle chan = forkIO $
forever
(do eof <- hIsEOF handle
unless eof $ hGetLine handle >>= atomically . writeTChan chan)
`catch` (\(e :: SomeException) -> return ())
issueCmd :: String -> [String] -> IO ()
issueCmd cmd parms = do
(_ ,Just hout ,Just herr ,_) <- createProcess (proc cmd parms) {
std_out = CreatePipe,
std_err = CreatePipe
}
chan <- newTChanIO :: IO (TChan String)
_ <- makeThread hout chan
_ <- makeThread herr chan
forever $ atomically (readTChan chan) >>= printf "%s\n"
main :: IO ()
main = do
args <- getArgs
let cmd = head args
let parms = tail args
issueCmd cmd parms
print "Done"
If I run this with a command the command's output will be printed but
after that the program is hanging, and top shows 100% cpu usage. "Done"
will never be printed.
Any idea what I have to add to prevent it from hanging?
--
Thanks,
Manfred
------------------------------
Message: 2
Date: Wed, 15 Jun 2011 10:33:21 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] merge two handles
To: Manfred Lotz <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
The problem is the "forever" in the main thread. It never has a way
to know that the two threads have died, but the stm knows that the
other two channels have disappeared, so they no longer block, and this
causes a busy loop.
So check this out. I would have rather done it with a state monad to
count the number of threads I spawn and then wait for the appropriate
number of messages to arrive, but this way works too.
This is one of those cases where datatypes are awesome. Now the
thread passes back either a line to be printed, or it tells the parent
thread that it has nothing left to print. That way the main thread
knows exactly when to die.
Also, if you are going to be making command line scripts, I highly
recommend the cmdargs package on hackage, as it is pretty cool for
doing commandline arguments in a safe way.
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import GHC.Conc.Sync
import System.IO
import System.Environment
import System.Process
import Control.Monad
import Control.Concurrent.STM.TChan
import Control.Exception.Base
import Text.Printf
import Prelude hiding (catch)
data Message = MString String | ImDone
makeThread :: Handle -> TChan Message -> IO ThreadId
makeThread handle chan = forkIO $ (loop `catch` (\(e :: SomeException)
-> writeDone chan))
where
loop = do
msg <- hGetLine handle
writeMsg chan msg
loop
writeMsg chan msg = (atomically . writeTChan chan) $ MString msg
writeDone chan = (atomically . writeTChan chan) ImDone
issueCmd :: String -> [String] -> IO ()
issueCmd cmd parms = do
(_ ,Just hout ,Just herr ,_) <- createProcess (proc cmd parms) {
std_out = CreatePipe,
std_err = CreatePipe
}
chan <- newTChanIO :: IO (TChan Message)
makeThread hout chan
makeThread herr chan
printCmd chan
printCmd chan
where
printCmd chan = do
msg <- atomically (readTChan chan)
case msg of
MString msg -> do
putStrLn msg
printCmd chan
ImDone -> return ()
main :: IO ()
main = do
args <- getArgs
let cmd = head args
let parms = tail args
issueCmd cmd parms
print "Done"
On Wed, Jun 15, 2011 at 9:23 AM, Manfred Lotz <[email protected]> wrote:
> On Tue, 14 Jun 2011 13:10:36 -0400
> David McBride <[email protected]> wrote:
>
>> It probably has to do more with parenthesis than anything:
>>
>> forever $ atomically $ readTchan chan >>= print
>> forever $ (atomically $ readTchan chan) >>= print
>>
>> That might work. ?Once you get the types to line up, that should work.
>>
>
> Thanks for the hint. I finally got it compiled and almost working.
>
> The code is now like this:
>
>
> {-# LANGUAGE ScopedTypeVariables #-}
>
> import GHC.Conc.Sync
> import System.IO
> import System.Environment
> import System.Process
> import Control.Monad
> import Control.Concurrent.STM.TChan
> import Control.Exception.Base
> import Text.Printf
> import Prelude hiding (catch)
>
>
>
> makeThread :: Handle -> TChan String -> IO ThreadId
> makeThread handle chan = forkIO $
> ?forever
> ? ?(do eof <- hIsEOF handle
> ? ? ? ?unless eof $ hGetLine handle >>= atomically . writeTChan chan)
> ? ? ? ? ? ? ? ? ? ? ? ? ? ?`catch` (\(e :: SomeException) -> return ())
>
> issueCmd :: String -> [String] -> IO ()
> issueCmd cmd parms = do
> ?(_ ,Just hout ,Just herr ,_) <- createProcess (proc cmd parms) {
> ? ?std_out = CreatePipe,
> ? ?std_err = CreatePipe
> ?}
> ?chan <- newTChanIO :: IO (TChan String)
> ?_ <- makeThread hout chan
> ?_ <- makeThread herr chan
> ?forever $ atomically (readTChan chan) >>= printf "%s\n"
>
> main :: IO ()
> main = do
> ?args <- getArgs
> ?let cmd = head args
> ?let parms = tail args
> ?issueCmd cmd parms
> ?print "Done"
>
>
>
> If I run this with a command the command's output will be printed but
> after that the program is hanging, and top shows 100% cpu usage. "Done"
> will never be printed.
>
> Any idea what I have to add to prevent it from hanging?
>
>
> --
> Thanks,
> Manfred
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Wed, 15 Jun 2011 17:15:35 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] merge two handles
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Wed, 15 Jun 2011 10:33:21 -0400
David McBride <[email protected]> wrote:
> The problem is the "forever" in the main thread. It never has a way
> to know that the two threads have died, but the stm knows that the
> other two channels have disappeared, so they no longer block, and this
> causes a busy loop.
>
> So check this out. I would have rather done it with a state monad to
> count the number of threads I spawn and then wait for the appropriate
> number of messages to arrive, but this way works too.
>
> This is one of those cases where datatypes are awesome. Now the
> thread passes back either a line to be printed, or it tells the parent
> thread that it has nothing left to print. That way the main thread
> knows exactly when to die.
>
Thanks a lot. I will study your solution carefully.
> Also, if you are going to be making command line scripts, I highly
> recommend the cmdargs package on hackage, as it is pretty cool for
> doing commandline arguments in a safe way.
>
Yes, you are right of course. But here I wanted to have just a minimal
example.
--
Manfred
------------------------------
Message: 4
Date: Wed, 15 Jun 2011 12:17:46 -0300
From: Federico Mastellone <[email protected]>
Subject: [Haskell-beginners] Data.Foldable's foldr'
To: Beginners Haskell Mailinglist <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
Looking at the source of Data.Foldable I found something I don't understand
The Foldable class declares foldl like this:
foldl :: (a -> b -> a) -> a -> t b -> a
Them, outside of the class, foldr' is defined like this:
-- | Fold over the elements of a structure,-- associating to the
right, but strictly.foldr' :: Foldable t => (a -> b -> b) -> b -> t a
-> bfoldr' f z0 xs = foldl f' id xs z0 where f' k x z = k $! f x z
I don't understand this definition, foldl receives 3 parameters and
here it is used with 4, how is it possible?
Even the function passed to foldl has 3 parameters when a function of
2 is needed.
What is the precedence and associativity involved here that makes
foldr' a valid expression?
Thanks!
--
Federico Mastellone
Computer Science Engineer - ITBA
".. there are two ways of constructing a software design: One way is to make
it so simple that there are obviously no deficiencies, and the other way is
to make it so complicated that there are no obvious deficiencies. The first
method is far more difficult."
Tony Hoare, 1980 ACM Turing Award Lecture.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110615/36fe3d6a/attachment-0001.htm>
------------------------------
Message: 5
Date: Wed, 15 Jun 2011 17:36:01 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Data.Foldable's foldr'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="us-ascii"
On Wednesday 15 June 2011, 17:17:46, Federico Mastellone wrote:
> Hi,
>
> Looking at the source of Data.Foldable I found something I don't
> understand
>
> The Foldable class declares foldl like this:
>
> foldl :: (a -> b -> a) -> a -> t b -> a
>
> Them, outside of the class, foldr' is defined like this:
>
> -- | Fold over the elements of a structure,-- associating to the
> right, but strictly.foldr' :: Foldable t => (a -> b -> b) -> b -> t a
> -> bfoldr' f z0 xs = foldl f' id xs z0 where f' k x z = k $! f x z
>
> I don't understand this definition, foldl receives 3 parameters and
> here it is used with 4, how is it possible?
The result is a function, which is then applied to the value z0
foldr' f z0 xs =
let res :: b -> b
res = foldl f' id xs
f' :: (b -> b) -> a -> b -> b
f' k x z = k $! f x z
in res z0
Since the function arrow (->) associates to the right, the type of f' can
also be written
f' :: (b -> b) -> a -> (b -> b)
This is an example illustrating that the concept of arity isn't easy in
Haskell (unless you resolve to "every function has arity 1").
Also
id id id id id ... id x
>
> Even the function passed to foldl has 3 parameters when a function of
> 2 is needed.
The third argument is the argument of the resulting function.
>
> What is the precedence and associativity involved here that makes
> foldr' a valid expression?
Function application has the highest precedence and associates to the left
foldl f' id xs z0
= (foldl f' id xs) z0
= ((foldl f' id) xs) z0
= (((foldl f') id) xs) z0
>
> Thanks!
------------------------------
Message: 6
Date: Wed, 15 Jun 2011 18:38:37 +0300
From: Yitzchak Gale <[email protected]>
Subject: Re: [Haskell-beginners] Data.Foldable's foldr'
To: Federico Mastellone <[email protected]>
Cc: Beginners Haskell Mailinglist <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Federico Mastellone wrote:
> foldl :: (a -> b -> a) -> a -> t b -> a
>
> Them, outside of the class, foldr' is defined like this:
>
> -- | Fold over the elements of a structure,
> -- associating to the right, but strictly.
> foldr' :: Foldable t => (a -> b -> b) -> b -> t a -> b
> foldr' f z0 xs = foldl f' id xs z0
> where f' k x z = k $! f x z
>
> I don't?understand?this definition, foldl receives 3 parameters and here it
> is used with 4, how is it possible?
> Even the function passed to foldl has 3 parameters when a function of 2 is
> needed.
In the type of foldl, the parameter 'a' can be any type - even
a function.
So let's see what we get when we substitute 'c -> d' for 'a'
in the type of foldl:
((c -> d) -> b -> (c -> d)) -> (c -> d) -> t b -> (c -> d)
Now, remembering that -> is right-associative
in type expressions, we can remove some parentheses:
((c -> d) -> b -> c -> d) -> (c -> d) -> t b -> c -> d
So when 'a' is a function, we see that foldl indeed takes
4 parameters, and its first parameter is a function that
takes 3 parameters.
Regards,
Yitz
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 39
*****************************************