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: foldl' vs seq (Ertugrul Soeylemez)
2. Monadic composition without throwing genericity under the
bus? (Dave Bayer)
3. Re: Monadic composition without throwing genericity under
the bus? (Daniel Fischer)
4. Re: Performance of Idiomatic lazy Haskell (Tim Perry)
5. Re: Using Debug.Trace (legajid)
----------------------------------------------------------------------
Message: 1
Date: Tue, 2 Feb 2010 14:05:23 +0100
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: foldl' vs seq
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Gabi <[email protected]> wrote:
> Thank you Ertugrul,
>
> I think I got it now.
> My lesson here is to avoid recursion as much possible and use foldr'
> instead.There are too many pitfalls.
That's not what I wanted to say. It's good to understand recursion and
non-strict semantics, so that you can use them together to write great
programs. =)
However, as soon as you start speaking Haskell fluently, you won't write
out many recursions explicitly anymore. For most things there are
combinators of some type, which you can use to express what you want.
Finally you will like monads, because most programming patterns can be
expressed conveniently in terms of them. That includes stateful
computation, aborting, resuming, undoing, nondeterminism, failability,
probabilistic computation and an arbitrary mixture of them.
Greets
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/
------------------------------
Message: 2
Date: Tue, 2 Feb 2010 10:06:52 -0500
From: Dave Bayer <[email protected]>
Subject: [Haskell-beginners] Monadic composition without throwing
genericity under the bus?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
I've been playing with parsing, for text filtering applications such as an
alternate GHC literate preprocessor. Here, it pays to have one's monad handle
both the input and output streams out of sight. Using ShowS-valued monads, one
can express most grammatical constructs as simple composition. I'm sure many
people have had this idea, but the resulting parsers that I write end up much
shorter than any demo code I've seen. For example, the "code is indented,
comments are flush, periods delimit block comments" literate preprocessor that
I depend on daily (leaving out the heredoc code) is just
dot, comment, dotLine, commentLine, codeLine, dotBlock, delit â· Parser
dot = char '.'
comment = place "-- "
codeLine = white â till (heredoc ⨠whiteLine) word
commentLine = comment â line
dotLine = comment â dot â whiteLine
dotBlock = dotLine â till (dotLine ⨠eof) (whiteLine ⨠commentLine)
delit = till (skip (many whiteLine) â eof)
(whiteLine ⨠dotBlock ⨠codeLine ⨠commentLine)
However, I've struggled mightily to cleanly implement composition of
function-valued monads. In the end I had to throw genericity under the bus to
use the above notation. I'm asking for help, in case I'm missing something.
I cannot use Control.Category for a Monad, because of kind arity: Monad
instances have one unbound *, while Category instances have two. Here are some
toy experiments:
mcompose â· Monad m â m (b â c) â m (a â b) â m (a â c)
mcompose x y = do
f â x
g â y
return $ f . g
-- adapted from Control.Category
class Category cat where
unit â· cat a a
compose â· cat b c â cat a b â cat a c
instance Category (â) where
unit = id
compose = (.)
-- Monad wrapper
newtype Wrap m a b = Wrap { unwrap â· m (a â b) }
instance Monad m â Category (Wrap m) where
unit = Wrap $ return id
compose x y = Wrap $ mcompose (unwrap x) (unwrap y)
-- Other tries that fail
instance Monad m â Category (m (â)) where
unit = return id
compose = mcompose
-- Error:
-- The first argument of `Category' should have kind `* -> * -> *',
-- but `m (->)' has kind `*'
-- In the instance declaration for `Category (m (->))'
type MonadMap m a b = m (a â b)
instance Monad m â Category (MonadMap m) where
unit = return id
compose = mcompose
-- Error:
-- Type synonym `MonadMap' should have 3 arguments, but has been given 1
-- In the instance declaration for `Category (MonadMap m)'
I can see why each of these fail, but I also crave a language that allows
ambiguity if exactly one interpretation compiles. For example, one could scrape
the leaves of the tree (m (â)) to find the two *'s one wants, and one would
think that my MonadMap type synonym would be a standard trick for exposing the
two *'s without giving up on the other form. (I want to use the same type as
both a monad and a category, as the goal here is very concise code with no
gunk packing and unpacking crutch types for the compiler.)
So I threw "id" under the bus. Here are later experiments:
mcompose â· Monad m â m (b â c) â m (a â b) â m (a â c)
mcompose x y = do
f â x
g â y
return $ f . g
class Composable a b c | a b â c where
compose â· a â b â c
instance Composable (b â c) (a â b) (a â c) where
compose f g = f . g
instance Monad m â Composable (m (b â c)) (m (a â b)) (m (a â c))
where
compose = mcompose
unit â· Monad m â m (a â a)
unit = return id
tab â· Maybe ShowS
tab = Just (" " ++)
test1, test2 â· Monad m â m (a â a)
test1 = mcompose unit unit
test2 = compose unit unit
-- test2 error:
-- Could not deduce (Composable
-- (m (a -> a)) (m1 (a1 -> a1)) (m2 (a2 -> a2)))
-- from the context (Monad m2)
-- arising from a use of `compose' at Issue2.lhs:26:10-27
test3, test4 â· Maybe ShowS
test3 = mcompose tab tab
test4 = compose tab tab
It appears to me that type inference in type classes is broken. How else to
explain why mcompose has no trouble figuring out that the monads are the same,
but compose is stumped?
In the end, I threw genericity under the bus, and chose the parser type
type Parser = StateT String Maybe ShowS
so the second approach would work in practice.
I don't need to do this "my way" if there's an idiom (or -XAllowAliens compiler
flag) that I need to learn. How does one do this sort of thing cleanly?
Thanks!
------------------------------
Message: 3
Date: Tue, 2 Feb 2010 17:08:44 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Monadic composition without throwing
genericity under the bus?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Dienstag 02 Februar 2010 16:06:52 schrieb Dave Bayer:
> test1, test2 â· Monad m â m (a â a)
> Â test1 = mcompose unit unit
>  test2 = compose  unit unit
>
> -- test2 error:
> -- Could not deduce (Composable
> -- (m (a -> a)) (m1 (a1 -> a1)) (m2 (a2 -> a2)))
> -- from the context (Monad m2)
> -- arising from a use of `compose' at Issue2.lhs:26:10-27
>
> Â test3, test4 â· Maybe ShowS
> Â test3 = mcompose tab tab
>  test4 = compose  tab tab
>
> It appears to me that type inference in type classes is broken. How else
> to explain why mcompose has no trouble  figuring out that the monads are
> the same, but compose is stumped?
Try removing the type signature for test2 and see what that gives:
Compose.hs:28:8:
No instance for (Composable (m (a -> a)) (m1 (a1 -> a1)) c)
arising from a use of `compose' at Compose.hs:28:8-25
Possible fix:
add an instance declaration for
(Composable (m (a -> a)) (m1 (a1 -> a1)) c)
In the expression: compose unit unit
In the definition of `test2': test2 = compose unit unit
Failed, modules loaded: none.
commenting out test2 and querying the type at the prompt:
*Compose> :t compose unit unit
compose unit unit
:: (Monad m,
Monad m1,
Composable (m (a -> a)) (m1 (a1 -> a1)) c) =>
c
In mcompose, you specified exactly which types to use, in particular that
there's only one monad involved.
In test2, the type checker must determine the types from scratch.
compose :: forall a b c. Composable a b c => a -> b -> c
test2 = compose unit unit
unit :: forall m a. Monad m => m (a -> a)
The type checker can't assume that both unit's in test2 have the same type,
so we have two monads (m, m1) and two types (a, a1) which are to be
composed to give a third type (c).
compose can only work when it knows the types of both arguments. It doesn't
know the type of unit (since that's polymorphic), so it can't work with
unit.
You can help it somewhat by adding more FunDeps to Composable,
class Composable a b c | a b â c, a c -> b, b c -> a where
compose â· a â b â c
, then it can work when it knows
a) the types of both arguments
or
b) the type of one argument and the type of the result.
Doesn't help with test2, though.
------------------------------
Message: 4
Date: Tue, 2 Feb 2010 09:34:38 -0800 (PST)
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
The links are confusing, but unxutils is still available:
http://sourceforge.net/projects/unxutils/
MinGW/MSYS may be a better and/or more fully supported package but I have no
experience there.
Anyhow, time is built into the unxutils "sh" shell:
C:\Documents and Settings\perry>sh
WREN# time sleep 3
sleep 3 0.00s user 0.00s system 0% cpu 3.125 total
WREN#
However, I'm pretty sure the shell is actually zsh, not sh.
----- Original Message ----
From: Henk-Jan van Tuyl <[email protected]>
To: Tim Perry <[email protected]>; Daniel Fischer <[email protected]>;
Markus Böhm <[email protected]>
Cc: [email protected]
Sent: Mon, February 1, 2010 3:05:58 PM
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
unxutils.zip seems to be gone; another option would be, to use MinGW/MSYS.
You need this anyway for installing several Haskell packages that bind to
C-software. If you start an MS-DOS shell and give command "sh", you are in
a unixlike shell, where you can give the "time" command.
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--
On Mon, 01 Feb 2010 20:15:25 +0100, Tim Perry <[email protected]> wrote:
> CygWin would be a good option.
>
> Here are some other ideas:
>
> There is a timeit.exe utility in the Windows Server downloads which is
> supposed to be similar to unix's time command. Try the links here
> http://channel9.msdn.com/forums/Coffeehouse/258979-Windows-equivalent-of-UnixLinux-time-command/
>
> Windows does support piping. It is similar to bash with 2>&1 to redirect
> stderr to stdout and stuff.
> http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
>
> The new windows shell is almost a real shell. Almost.
>
> If you don't have that, then this port of unix utilities makes an XP box
> tolerable.
> http://unxutils.sourceforge.net/
>
> Hope that helps.
> --Tim
>
>
>
> ----- Original Message ----
> From: Daniel Fischer <[email protected]>
> To: Markus Böhm <[email protected]>
> Cc: [email protected]
> Sent: Mon, February 1, 2010 6:40:52 AM
> Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
>
> Am Montag 01 Februar 2010 15:22:45 schrieb Markus Böhm:
>
>> Daniel, I use GHC 6.12.1 and Windows XP.
>
> *sigh*
>
> Have you a CygWin installed? It should run as given from a CygWin shell,
> I
> believe.
>
>> The time command doesn't seem
>> to work. It says in German: specified time can't be read. Give a new
>> time.
>
> Replace "time" with however you get the CPU time used by a process on
> Windows.
> But I think Windows doesn't allow piping, and I'm not sure whether it has
> echo, so see below.
>
>> Daniel, here another feedback:
>>
>> F:\MeineUebungen>ghc -O2 -fexcess-precision -fvia-C -optc-O3 -o luaLoop
>> --make p 085-pi_lualoop.hs
>> [1 of 1] Compiling Main ( p085-pi_lualoop.hs,
>> p085-pi_lualoop.o ) Linking luaLoop.exe ...
>>
>> F:\MeineUebungen>echo '0.00000001' | time ./luaLoop
>> Eingegebene Zeit kann nicht übernommen werden.
>> Geben Sie die neue Zeit ein:
>>
>> F:\MeineUebungen>luaLoop +RTS -sstderr -RTS
>> luaLoop +RTS -sstderr
>> EPS:
>> 0.00000001
>> 3.1415926485894725
>> 61,428 bytes allocated in the heap
>> 1,316 bytes copied during GC
>> 4,564 bytes maximum residency (1 sample(s))
>> 11,820 bytes maximum slop
>> 1 MB total memory in use (0 MB lost due to fragmentation)
>>
>> Generation 0: 0 collections, 0 parallel, 0.00s, 0.00s
>> elapsed Generation 1: 1 collections, 0 parallel, 0.00s, 0.00s
>> elapsed
>>
>> INIT time 0.02s ( 0.00s elapsed)
>> MUT time 8.06s ( 16.83s elapsed)
>> GC time 0.00s ( 0.00s elapsed)
>> EXIT time 0.00s ( 0.00s elapsed)
>> Total time 8.08s ( 16.83s elapsed)
>>
>> %GC time 0.0% (0.0% elapsed)
>>
>> Alloc rate 7,604 bytes per MUT second
>>
>> Productivity 99.8% of total user, 47.9% of total elapsed
>
> What?????
>
> Please try the following:
>
> 1) hardcode eps = 1e-8 in the source (or get it via getArgs and pass it
> as
> a command line argument)
> 2) ghc -O2 -fforce-recomp -fexcess-precision -fvia-C -optc-O3 -o cloop --
> make Source.hs
> 3) ghc -O2 -fforce-recomp -o nloop --make Source.hs
> 4) cloop +RTS -sstderr
> 5) nloop +RTS -sstderr
>
>>
>> -- Markus
>>
> _______________________________________________
> 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: 5
Date: Tue, 02 Feb 2010 22:21:25 +0100
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] Using Debug.Trace
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi,
it works fine, except
(xv1,ok1) = calcul'' pxv c vallib `debug` (" " ++ show xv1)
`debug` (" Calcul' ok1=" ++ show ok1 ++ "
c:cs= " ++ show (c:cs))
that causes an infinite loop, cause xv1 and ok1 are just being calculated.
Thank you for your clear explanation of do statements.
Didier.
Daniel Fischer a écrit :
> Am Sonntag 31 Januar 2010 10:25:58 schrieb legajid:
>
>> Hi,
>>
>> Starting with trace, i have trouble with my calcul'' function while
>> calcul' is ok.
>> When afftrace in calcul'' is commented, the program compiles.
>> When uncommented ( afftrace (" calcul'' vide") ), i get
>> the following messages :
>>
>> ------------------------------------------------------------------------
>> ------------------- *Main> :r
>> [1 of 1] Compiling Main ( sud3c.hs, interpreted )
>>
>> sud3c.hs:62:3:
>> Couldn't match expected type `[Char]'
>> against inferred type `(Plateau, Char)'
>> In a stmt of a 'do' expression: afftrace (" calcul'' vide")
>> In the expression:
>> do afftrace (" calcul'' vide")
>> (pxv, False)
>> In the definition of `calcul''':
>> calcul'' pxv _ []
>> = do afftrace (" calcul'' vide")
>> (pxv, False)
>> Failed, modules loaded: none.
>> ------------------------------------------------------------------------
>> -------------------
>>
>> I don't understand why, in calcul', it's ok and why, in calcul'', it's
>> problematic. Because return value of calcul'' is a tuple ?
>>
>
> You defined
>
>
>> afftrace x= if modetrace then trace x " "
>> else " "
>>
>
> If you ask ghci the type of that, you'll get
>
> ghci> :t afftrace
> afftrace :: String -> [Char]
>
> (or afftrace :: [Char] -> [Char], or String -> String)
>
> since
>
> ghci> :t trace
> trace :: String -> a -> a
>
> Now you use afftrace in a do-block, which means "afftrace x" must have type
>
> (Monad m) => m a
>
> for some m and a.
> Well, afftrace x has type [Char], so m is [] and a is Char, fine.
>
> That means you can use afftrace in any calculation returning a list of some
> kind (outside of do-blocks, also in other calculations).
>
> But calcul'' doesn't return a list, it returns a pair. So
>
> calcul'' pxv _ [] = afftrace (" calcul'' vide") >> (pxv,False)
>
> , which is what the first equation of calcul'' is desugared to, isn't well
> typed.
>
> (>>) :: Monad m => m a -> m b -> m b
>
> afftrace " calcul'' vide" :: [] Char -- m === [], a === Char
>
> (pxv,False) :: (,) Plateau Bool -- m === ((,) Plateau), b === Bool
>
>
> (actually, ((,) Plateau) is indeed a monad, but it's a different one from
> [], so the expression is not well typed).
>
> You could
> - modify calcul'' to return [(Plateau,Bool)]
> - not use do-blocks just for the sake of tracing and restructure your code
> (I recommend the second)
>
> infixl 0 `debug`
>
> debug = flip trace
>
> calcul' pxv [] = pxv `debug` " Calcul' vide"
> calcul' pxv (c:cs)
> | ok1 = calcul' xv1 cs `debug` " Calcul' suite"
> | otherwise = pxv `debug` " Calcul' pas de valeur"
> where
> vallib = [1 .. length pxv] ++ [5 .. 7]
> nbvlib = length vallib
> (xv1,ok1) = calcul'' pxv c vallib `debug` (" " ++ show xv1)
> `debug` (" Calcul' ok1=" ++ show ok1 ++ "
> c:cs= " ++ show (c:cs))
>
> calcul'' pxv _ [] = (pxv,False) `debug` " calcul'' vide"
> calcul'' pxv c (li:lis)
> | False `debug` " " ++ show pvx ++ "..." = undefined
> | li == 4 || li `elem` pxv = calcul'' pxv c lis `debug` "quoi?"
> | otherwise = (calcul'' pxv c li,True) `debug` " calcul'''"
>
> Now the code reads more natural (except for the "False `debug` ... " to
> produce general debugging output), and removing the debugging output isn't
> any harder.
>
>
>> When my program is ok, should i remove all trace instructions (and
>> associated do commands too) or just set my modetrace value to False ?
>>
>>
>
> Remove, resp. comment out.
>
>
>> Thanks for helping,
>> Didier
>>
>> Below my code :
>>
>>
>> calcul' :: Plateau -> [Cellule] -> Plateau
>> calcul' pxv [] = do
>> afftrace (" Calcul' vide")
>> pxv
>> calcul' pxv (c:cs)= do
>> afftrace (" Calcul' ok1="++show ok1++" c:cs= "++show
>> (c:cs)) afftrace (" "++show xv1)
>>
>> if ok1 then do
>> afftrace (" Calcul' suite")
>> calcul' xv1 cs
>> else do
>> afftrace (" Calcul' pas de valeur")
>> pxv
>> where
>> vallib=[1..length pxv]++[5..7]
>> nbvlib=length vallib
>> (xv1,ok1)=calcul'' pxv c vallib
>>
>>
>>
>> calcul'' :: Plateau -> Cellule -> [Valeur] -> (Plateau, Bool)
>> calcul'' pxv _ [] = do
>> --afftrace (" calcul'' vide")
>> (pxv, False)
>>
>> calcul'' pxv c (li : lis) = do
>> --afftrace (" "++show pxv)
>> --afftrace (" "++show c ++ " "++show(li:lis))
>> v2
>> where
>> v2= if (elem li pxv || li==4)
>> then
>> calcul'' pxv c lis
>> else do
>> --afftrace (" calcul'''")
>> (calcul''' pxv c li, True)
>>
>> calcul''' :: Plateau -> Cellule -> Valeur -> Plateau
>> calcul''' pxv c li =
>> take (c-1) pxv ++ [li] ++ drop c pxv
>>
>> afftrace x= if modetrace then trace x " "
>> else " "
>>
>
>
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 20, Issue 5
****************************************