Re: [Haskell-cafe] Haskell on Android

2009-06-18 Thread Vasili I. Galchin
On Thu, Jun 18, 2009 at 12:26 AM, Jason Dagit da...@codersbase.com wrote:



 On Wed, Jun 17, 2009 at 9:53 PM, Vasili I. Galchin vigalc...@gmail.comwrote:

 Hello,

  Let me change the subject ... I think everybody understood my
 thrust but let me make more provocative. Don, please let me expose my
 ignorance for the greater good and time my personal scorn ;^) ... EDSL
 time  = Embedded Domain-Specific Language?? If so, can you please be more
 specific! I don't mind to be a grunt for Haskell.


 Yes, EDSL is Embedded Domain-Specific Language.  Although, I'm not sure I
 understand what you are asking.  I looked at the wiki page which Conal
 created and he does mention using an EDSL in Haskell to generate code.
 Perhaps this is what you want to know more about?

 There is a paper linked from the wiki page that should help a lot with
 answering questions you have about the technique.  For a simple example of
 how it can work, I wrote a program called Autoproc that 'compiles' the
 haskell EDSL into a procmail recipe.  You can find the source code here:
 darcs get http://projects.codersbase.com/repos/autoproc/

^^^ cool .. thx.



 It's really not much code so it should be easy to wrap your mind around
 it.  I call the above code simple, but it works quite well and illustrates
 that a little bit of Haskell can go a long ways :)


   ;^)



 How it works is that the expressions you code up in Haskell build up values
 which correspond to the abstract syntax, or your intermediate
 representation.  You can then transform that representation and do whatever
 a compiler or translator would normally do and the target format is some
 other language or machine code.

  . let me read carefully .. thx.



 This technique allows you to reuse the facilites of the host language, such
 as strong static typing and laziness, the parser, standard libs and so on.
 It's a great way to prototype a language and work out the kinks before you
 invest in making a stand alone implementation.  And all that aside, it's
 just plain fun.


   again ;^)

Vasili




 I hope that helps,
 Jason

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] ANNOUNCE: hyena

2009-06-18 Thread Johan Tibell
I am pleased to announce the first release of hyena, a library for building
web servers, based on the work on iteratee style I/O by Oleg Kiselyov.

The library allows you to create web servers that consume their input
incrementally, without resorting to lazy I/O. This should lead to more
predictable resource usage.

This is an early alpha release so expect the API to change in the future. In
particular, I'm working on converting the current definition of
iteratees/enumerators used to the one in the iteratee package [1]. I decided
to release this version, based on a simple left fold, due to requests by
several people who already started using hyena.

Get it:

   cabal install hyena

And on Hackage:

   http://hackage.haskell.org//package/hyena

1. http://hackage.haskell.org/package/iteratee

-- Johan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: (fwd) Haskell logo fail

2009-06-18 Thread Jon Fairbairn
Jason Dusek jason.du...@gmail.com writes:

   Why don't we have a picture of a cool dinosaur instead?

Something cool because the last heat of life went out of it
65 million years ago?

-- 
Jón Fairbairn jon.fairba...@cl.cam.ac.uk

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: (fwd) Haskell logo fail

2009-06-18 Thread minh thu
2009/6/18 Jon Fairbairn jon.fairba...@cl.cam.ac.uk:
 Jason Dusek jason.du...@gmail.com writes:

   Why don't we have a picture of a cool dinosaur instead?

 Something cool because the last heat of life went out of it
 65 million years ago?

made with secret dinosaur technology

Thu
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] ANNOUNCE fmlist

2009-06-18 Thread Sjoerd Visscher
I am pleased to announce the first release of Data.FMList, lists  
represented by their foldMap function:
 newtype FMList a = FM { unFM :: forall b . Monoid b = (a - b) -  
b }

It has O(1) cons, snoc and append, just like difference lists.
Fusion is more or less built-in, for f.e. fmap and (=), but I'm not  
sure if this gives any advantages over what a compiler like GHC can do  
for regular lists.


My interest in this was purely the coding exercise, and I think there  
are some nice lines of code in there, for example:


 reverse l = FM $ \f - getDual $ unFM l (Dual . f)

If you like folds or monoids, you certainly should take a look.

One fun example:

 -- A right-infinite list
 c = 1 `cons` c
 -- A left-infinite list
 d = d `snoc` 2
 -- A middle-infinite list ??
 e = c `append` d

*Main head e
1
*Main last e
2

Install it with

  cabal install fmlist

Or go to

  http://hackage.haskell.org/package/fmlist-0.1

I owe a big thanks to Oleg Kiselyov, who wrote some of the more  
complicated folds in

http://okmij.org/ftp/Haskell/zip-folds.lhs
I don't think I could have come up with the zipWith code.

This is my first package on Hackage, so any comments are welcome!

greetings,
Sjoerd Visscher

PS. What happened to the traverse encoded containers (see below)? It  
turns out that it is a bit too generic, and functions like filter were  
impossible to implement. FMLists still have a Traversable instance,  
but only because the tree structure is (almost) undetectable, so they  
can simply be rebuilt using cons and empty.


On Jun 15, 2009, at 1:29 AM, Sjoerd Visscher wrote:


Hi,

While playing with Church Encodings of data structures, I realized  
there are generalisations in the same way Data.Foldable and  
Data.Traversable are generalisations of lists.


The normal Church Encoding of lists is like this:

 newtype List a = L { unL :: forall b. (a - b - b) - b - b }

It represents a list by a right fold:

 foldr f z l = unL l f z

List can be constructed with cons and nil:

 nil  = L $ \f - id
 cons a l = L $ \f - f a . unL l f

Oleg has written about this: http://okmij.org/ftp/Haskell/zip- 
folds.lhs


Now function of type (b - b) are endomorphisms which have a  
Data.Monoid instance, so the type can be generalized:


 newtype FM a = FM { unFM :: forall b. Monoid b = (a - b) - b }
 fmnil  = FM $ \f - mempty
 fmcons a l = FM $ \f - f a `mappend` unFM l f

Now lists are represented by (almost) their foldMap function:

 instance Foldable FM where
   foldMap = flip unFM

But notice that there is now nothing list specific in the FM type,  
nothing prevents us to add other constructor functions.


 fmsnoc l a = FM $ \f - unFM l f `mappend` f a
 fmlist = fmcons 2 $ fmcons 3 $ fmnil `fmsnoc` 4 `fmsnoc` 5

*Main getProduct $ foldMap Product fmlist
120

Now that we have a container type represented by foldMap, there's  
nothing stopping us to do a container type represented by traverse  
from Data.Traversable:


{-# LANGUAGE RankNTypes #-}

import Data.Monoid
import Data.Foldable
import Data.Traversable
import Control.Monad
import Control.Applicative

newtype Container a = C { travC :: forall f b . Applicative f = (a - 
 f b) - f (Container b) }


czero :: Container a
cpure :: a - Container a
ccons :: a - Container a - Container a
csnoc :: Container a - a - Container a
cpair :: Container a - Container a - Container a
cnode :: Container a - a - Container a - Container a
ctree :: a - Container (Container a) - Container a
cflat :: Container (Container a) - Container a

czero   = C $ \f - pure czero
cpure x = C $ \f - cpure $ f x
ccons x l   = C $ \f - ccons $ f x * travC l f
csnoc l x   = C $ \f - csnoc $ travC l f * f x
cpair l r   = C $ \f - cpair $ travC l f * travC r f
cnode l x r = C $ \f - cnode $ travC l f * f x * travC r f
ctree x l   = C $ \f - ctree $ f x * travC l (traverse f)
cflat l = C $ \f - cflat $ travC l (traverse f)

instance Functor Container where
 fmap g c = C $ \f - travC c (f . g)
instance Foldable Container where
 foldMap  = foldMapDefault
instance Traversable Container where
 traverse = flip travC
instance Monad Container where
 return   = cpure
 m = f  = cflat $ fmap f m
instance Monoid (Container a) where
 mempty   = czero
 mappend  = cpair

Note that there are all kinds of constructors, and they can all be  
combined. Writing their definitions is similar to how you would  
write Traversable instances.


So I'm not sure what we have here, as I just ran into it, I wasn't  
looking for a solution to a problem. It is also all quite abstract,  
and I'm not sure I understand what is going on everywhere. Is this  
useful? Has this been done before? Are there better implementations  
of foldMap and (=) for Container?


Finally, a little example. A Show instance (for debugging purposes)  
which shows the nesting structure.


newtype ShowContainer a = ShowContainer { doShowContainer :: String }
instance Functor ShowContainer where
 fmap _ (ShowContainer x) = ShowContainer $ ( ++ x 

Re[2]: [Haskell-cafe] Re: (fwd) Haskell logo fail

2009-06-18 Thread Bulat Ziganshin
Hello minh,

Thursday, June 18, 2009, 11:17:07 AM, you wrote:

   Why don't we have a picture of a cool dinosaur instead?

 Something cool because the last heat of life went out of it
 65 million years ago?

 made with secret dinosaur technology

made with dinosaur technology :)))


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Wiki user accounts

2009-06-18 Thread Ashley Yakeley

I wrote:
Rules for usernames are the same as rules for particle titles, 


erm, article titles

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Unicode workaround for getDirectoryContents under Windows?

2009-06-18 Thread Simon Marlow

On 16/06/2009 21:19, Bulat Ziganshin wrote:

Hello Simon,

Tuesday, June 16, 2009, 5:02:43 PM, you wrote:


I don't know how getArgs fits in here - should we be decoding argv using
the ACP?


myGetArgs = do
alloca $ \p_argc -  do
p_argv_w- commandLineToArgvW getCommandLineW p_argc
argc- peek p_argc
argv_w- peekArray (i argc) p_argv_w
mapM peekTString argv_w= return.tail

foreign import stdcall unsafe windows.h GetCommandLineW
   getCommandLineW :: LPTSTR

foreign import stdcall unsafe windows.h CommandLineToArgvW
   commandLineToArgvW :: LPCWSTR -  Ptr CInt -  IO (Ptr LPWSTR)


Presumably we'd also have to remove the +RTS ... -RTS in Haskell if we 
did this, correct?


Cheers,
Simon

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANNOUNCE fmlist

2009-06-18 Thread Sebastian Fischer

On Jun 18, 2009, at 9:57 AM, Sjoerd Visscher wrote:

I am pleased to announce the first release of Data.FMList, lists  
represented by their foldMap function: [...]

http://hackage.haskell.org/package/fmlist-0.1


cool!

Just for fun: a derivation translating between different formulations  
of monadic bind.


m = g
  = flatten (fmap g m)
  = FM $ \f - unFM (fmap g m) (foldMap f)
  = FM $ \f - unFM (FM $ \f' - unFM m (f' . g)) (foldMap f)
  = FM $ \f - (\f' - unFM m (f' . g)) (foldMap f)
  = FM $ \f - unFM m (folfMap f . g) -- your definition
  = FM $ \f - unFM m (flip unFM f . g)
  = FM $ \f - unFM m (\x - flip unFM f (g x))
  = FM $ \f - unFM m (\x - unFM (g x) f)-- like  
continuation monad


Cheers,
Sebastian


--
Underestimating the novelty of the future is a time-honored tradition.
(D.G.)





PGP.sig
Description: This is a digitally signed message part
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re[2]: Unicode workaround for getDirectoryContents under Windows?

2009-06-18 Thread Bulat Ziganshin
Hello Simon,

Thursday, June 18, 2009, 1:22:30 PM, you wrote:

 myGetArgs = do

 Presumably we'd also have to remove the +RTS ... -RTS in Haskell if we
 did this, correct?

yes, it's long-standing in my own to-do list :)


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANNOUNCE: hyena

2009-06-18 Thread Jochem Berndsen
Johan Tibell wrote:
 I am pleased to announce the first release of hyena, a library for building
 web servers, based on the work on iteratee style I/O by Oleg Kiselyov.
 
 The library allows you to create web servers that consume their input
 incrementally, without resorting to lazy I/O. This should lead to more
 predictable resource usage.
 
 This is an early alpha release so expect the API to change in the future. In
 particular, I'm working on converting the current definition of
 iteratees/enumerators used to the one in the iteratee package [1]. I decided
 to release this version, based on a simple left fold, due to requests by
 several people who already started using hyena.
 
 Get it:
 
cabal install hyena
 
 And on Hackage:
 
http://hackage.haskell.org//package/hyena
 
 1. http://hackage.haskell.org/package/iteratee

Cool! I will certainly look into it.

Cheers,

-- 
Jochem Berndsen | joc...@functor.nl
GPG: 0xE6FABFAB
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Profiling/cost centre question

2009-06-18 Thread Sebastian Reese

Hi there,

I mailed to this list in May  
(http://www.haskell.org/pipermail/haskell-cafe/2009-May/062126.html)  
with no answer at all. So I wrote a smaller program to demonstrate my  
problem/question. Maybe now someone can help me now.


I wrote a small program that does nothing but listening on a TCP port.
After connection is done it simply terminates.

# cat Test.hs
module Main where

import Network

main :: IO ()
main =
  (do
  servSock - {-# SCC cc1 #-}listenOn . PortNumber $ 1
  (handle, host, port) - {-# SCC cc2 #-}accept servSock
  return ()
  )`catch` (putStrLn . show)
#

I compile, run (connection from elsewhere, so program terminates) and  
watch the .prof output


# ghc --make -threaded -O2 -prof -caf-all -auto-all Test.hs
Linking Test ...
# ./Test +RTS -p
# cat Test.prof
Thu Jun 18 12:24 2009 Time and Allocation Profiling Report  (Final)

   Test +RTS -p -RTS

total time  =0.32 secs   (16 ticks @ 20 ms)
total alloc =  32,384 bytes  (excludes profiling overheads)

COST CENTREMODULE   %time %alloc

MAIN   MAIN 100.07.8
CAFGHC.Conc   0.04.0
CAFGHC.Handle 0.0   26.8
cc1Main   0.03.8
cc2 Main   0.0   55.8

 ... snip ...

#

My actual question is, where does the 100% individual time from MAIN  
come from, how can I debug that in other programs and where does this  
MAIN cost centre come from?


thanks for your help
Sebastian


This message was sent using IMP, the Internet Messaging Program.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Hans van Thiel

On Wed, 2009-06-17 at 21:26 -0500, Jake McArthur wrote:
 Jon Strait wrote:
  I'm reading the third (bind associativity) law for monads in this form:
  
  m = (\x - k x = h)  =  (m = k) = h
 
 Arguably, that law would be better stated as:
 
  (h = k) = m  =  h = (k = m)
 
 This wouldn't be so unintuitive.
Hi, 
The only place I've ever seen Kleisli composition, or its flip, used is
in demonstrating the monad laws. Yet it is so elegant and, even having
its own name, it must have some practical use. Do you, or anybody else,
have some pointers?

Best Regards,

Hans van Thiel
 
 - Jake
 

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] curious about sum

2009-06-18 Thread Keith Sheppard
OK, I think I went off on a tangent that isn't very useful anyway

thanks
-Keith

On Wed, Jun 17, 2009 at 6:32 PM, Lennart
Augustssonlenn...@augustsson.net wrote:
 The creators of Haskell didn't pick any particular representation for numbers.
 (Well, literals are kind of In..tegers.)  You can pick what types you
 make instances of Num.
 Some of them are lazy, some of them are strict.

 On Wed, Jun 17, 2009 at 11:05 PM, Keith Sheppardkeiths...@gmail.com wrote:
 In lambda calculus numbers are just functions and you evaluate them
 just like any other function. Haskell could have chosen the same
 representation for numbers and all evaluation on numbers would be lazy
 (assuming normal order evaluation). I think that would have been the
 Purist Lazy way to go. That is not the way the creators of Haskell
 designed language though... am i missing something?

 On Wed, Jun 17, 2009 at 4:05 PM, Lennart
 Augustssonlenn...@augustsson.net wrote:
 What do you mean by literals are strict?  Strictness is a semantic
 property of functions, and while literals can be overloaded to be
 functions I don't know what you mean.

 On Wed, Jun 17, 2009 at 9:50 PM, Keith Sheppardkeiths...@gmail.com wrote:
 Haskell's numeric literals are strict. You wouldn't want that to
 change right? It seems to me that having sum and product be strict is
 consistent with this.

 -Keith

 On Wed, Jun 17, 2009 at 11:15 AM, Thomas Davietom.da...@gmail.com wrote:

 On 17 Jun 2009, at 13:32, Yitzchak Gale wrote:

 Henk-Jan van Tuyl wrote:

 reverse
 maximum
 minimum

 Oh yes, please fix those also!

 import Prelude.Strict?

 Honestly, these functions are ones that I've *deffinately* used lazy
 versions of, in fact, in the cases of minimum/maximum I've even used ones
 that are super-lazy and parallel using unamb.

 It would be extremely odd to randomly decide most people would want this 
 to
 be strict based on no knowledge of what they're actually doing.  Instead,
 why don't we stand by the fact that haskell is a lazy language, and that 
 the
 functions we get by default are lazy, and then write a strict prelude as I
 suggest above to complement the lazy version.

 Bob
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




 --
 keithsheppard.name
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





 --
 keithsheppard.name
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





-- 
keithsheppard.name
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Runtime strictness analysis for polymorphic HOFs?

2009-06-18 Thread Stefan Holdermans

Paul,

Did you mean to say that const is strict in its first param and  
lazy in its second (since const _|_ y = _|_)? Also, can you explain  
your notation, how does a - {S} - b -{L} a indicate the  
strictness? Why not just {S} a - {L} b - a?


I'm sorry for the confusion. Indeed, const, as the type was intended  
to reflect, const is strict in its first argument and lazy in its  
second.


Ah, sorry. Now I see where I really confused you. Someone pointed out  
that I erroneously inserted an extra - in the type. The type should  
read


  a -{S} b -{L} a

HTH,

  Stefan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] curious about sum

2009-06-18 Thread Thomas Davie
No, I think it's extremely useful.  It highlights that numbers can  
both be lazy and strict, and that the so called useless lazy sum, is  
in fact, useful.


Bob

On 18 Jun 2009, at 13:29, Keith Sheppard wrote:


OK, I think I went off on a tangent that isn't very useful anyway

thanks
-Keith

On Wed, Jun 17, 2009 at 6:32 PM, Lennart
Augustssonlenn...@augustsson.net wrote:
The creators of Haskell didn't pick any particular representation  
for numbers.

(Well, literals are kind of In..tegers.)  You can pick what types you
make instances of Num.
Some of them are lazy, some of them are strict.

On Wed, Jun 17, 2009 at 11:05 PM, Keith  
Sheppardkeiths...@gmail.com wrote:

In lambda calculus numbers are just functions and you evaluate them
just like any other function. Haskell could have chosen the same
representation for numbers and all evaluation on numbers would be  
lazy

(assuming normal order evaluation). I think that would have been the
Purist Lazy way to go. That is not the way the creators of Haskell
designed language though... am i missing something?

On Wed, Jun 17, 2009 at 4:05 PM, Lennart
Augustssonlenn...@augustsson.net wrote:
What do you mean by literals are strict?  Strictness is a  
semantic

property of functions, and while literals can be overloaded to be
functions I don't know what you mean.

On Wed, Jun 17, 2009 at 9:50 PM, Keith  
Sheppardkeiths...@gmail.com wrote:

Haskell's numeric literals are strict. You wouldn't want that to
change right? It seems to me that having sum and product be  
strict is

consistent with this.

-Keith

On Wed, Jun 17, 2009 at 11:15 AM, Thomas  
Davietom.da...@gmail.com wrote:


On 17 Jun 2009, at 13:32, Yitzchak Gale wrote:


Henk-Jan van Tuyl wrote:


reverse
maximum
minimum


Oh yes, please fix those also!


import Prelude.Strict?

Honestly, these functions are ones that I've *deffinately* used  
lazy
versions of, in fact, in the cases of minimum/maximum I've even  
used ones

that are super-lazy and parallel using unamb.

It would be extremely odd to randomly decide most people would  
want this to
be strict based on no knowledge of what they're actually  
doing.  Instead,
why don't we stand by the fact that haskell is a lazy language,  
and that the
functions we get by default are lazy, and then write a strict  
prelude as I

suggest above to complement the lazy version.

Bob
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe





--
keithsheppard.name
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe







--
keithsheppard.name
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe







--
keithsheppard.name
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] how to #include files within parsec ... without unsafePerformIO?

2009-06-18 Thread Leonard Siebeneicher

Dear reader,

I wonder whether there is a 'general' working solution to include files
within a parsec parser. Without the need of unsafePerformIO.

Appending an example program, using unsafePerformIO.

Thanx for reading.

Greetings,
Leonard Siebeneicher


--- Begin: experiment.hs ---
import Text.ParserCombinators.Parsec
import System.IO.Unsafe

my_str :: Parser String
my_str = many1 anyToken

wrap_input :: Parser String - Parser String
wrap_input p = 
do
  i - getInput
  setInput readI
  a - my_str
  setInput i
  b - my_str
  return $ a ++  //\n\n  ++ b
where 
  {- Aaaah ... any solution without unsafePerformIO? -}
  readI = unsafePerformIO (readFile experiment.hs)


main = 
case parse (wrap_input my_str)  eintest of
  Left err - putStrLn Error raised
  Right ostr - putStrLn ostr
--- End: experiment.hs ---


___
Thinking about a special type like

data MyInclude = PlainText String
   | IncludeFile String

the parser could generate [MyInclude] data, but it does not work
generally. 



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] how to #include files within parsec ... without unsafePerformIO?

2009-06-18 Thread Neil Brown

Leonard Siebeneicher wrote:

Dear reader,

I wonder whether there is a 'general' working solution to include files
within a parsec parser. Without the need of unsafePerformIO.
  
At least in parsec 2, I don't think so.  Our solution was to read in the 
main file, tokenise it (using Alex), preprocess it (using some 
regex-like pattern matching on the token stream) in the IO monad and 
include the new files then (also tokenising and preprocessing them).  
Then after preprocessing we feed the entire resulting token stream to 
Parsec.  Whether a two-phase approach (preprocess then parse) works 
depends on whether your include syntax is simple enough that you can 
spot the includes without parsing.


Thanks,

Neil.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: curious about sum

2009-06-18 Thread Gleb Alexeyev

Thomas Davie wrote:
No, I think it's extremely useful.  It highlights that numbers can both 
be lazy and strict, and that the so called useless lazy sum, is in 
fact, useful.


But lazy sum should have beed defined in terms of foldr, not foldl. And 
foldl is not strict enough for strict sum. Therefore the current choice 
in the worst of both worlds.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] curious about sum

2009-06-18 Thread Keith Sheppard
I don't think anyone is calling it useless at this point. I could not
see a use for it initially and it was quickly pointed out that there
are in fact some infrequent use cases where a lazy sum is the best
option. I think this is more a discussion about principle of least
surprise or which use case is most frequent.

I am pretty new to haskell so I may just be missing something basic (I
welcome an explaination for why I am looking at this the wrong way),
but if your argument is on consistency then doesn't it follow that
number litterals should be defined using a church encoding or some
equivalent?

-Keith

On Thu, Jun 18, 2009 at 7:53 AM, Thomas Davietom.da...@gmail.com wrote:
 No, I think it's extremely useful.  It highlights that numbers can both be
 lazy and strict, and that the so called useless lazy sum, is in fact,
 useful.

 Bob

 On 18 Jun 2009, at 13:29, Keith Sheppard wrote:

 OK, I think I went off on a tangent that isn't very useful anyway

 thanks
 -Keith

 On Wed, Jun 17, 2009 at 6:32 PM, Lennart
 Augustssonlenn...@augustsson.net wrote:

 The creators of Haskell didn't pick any particular representation for
 numbers.
 (Well, literals are kind of In..tegers.)  You can pick what types you
 make instances of Num.
 Some of them are lazy, some of them are strict.

 On Wed, Jun 17, 2009 at 11:05 PM, Keith Sheppardkeiths...@gmail.com
 wrote:

 In lambda calculus numbers are just functions and you evaluate them
 just like any other function. Haskell could have chosen the same
 representation for numbers and all evaluation on numbers would be lazy
 (assuming normal order evaluation). I think that would have been the
 Purist Lazy way to go. That is not the way the creators of Haskell
 designed language though... am i missing something?

 On Wed, Jun 17, 2009 at 4:05 PM, Lennart
 Augustssonlenn...@augustsson.net wrote:

 What do you mean by literals are strict?  Strictness is a semantic
 property of functions, and while literals can be overloaded to be
 functions I don't know what you mean.

 On Wed, Jun 17, 2009 at 9:50 PM, Keith Sheppardkeiths...@gmail.com
 wrote:

 Haskell's numeric literals are strict. You wouldn't want that to
 change right? It seems to me that having sum and product be strict is
 consistent with this.

 -Keith

 On Wed, Jun 17, 2009 at 11:15 AM, Thomas Davietom.da...@gmail.com
 wrote:

 On 17 Jun 2009, at 13:32, Yitzchak Gale wrote:

 Henk-Jan van Tuyl wrote:

 reverse
 maximum
 minimum

 Oh yes, please fix those also!

 import Prelude.Strict?

 Honestly, these functions are ones that I've *deffinately* used lazy
 versions of, in fact, in the cases of minimum/maximum I've even used
 ones
 that are super-lazy and parallel using unamb.

 It would be extremely odd to randomly decide most people would want
 this to
 be strict based on no knowledge of what they're actually doing.
  Instead,
 why don't we stand by the fact that haskell is a lazy language, and
 that the
 functions we get by default are lazy, and then write a strict prelude
 as I
 suggest above to complement the lazy version.

 Bob
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




 --
 keithsheppard.name
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





 --
 keithsheppard.name
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





 --
 keithsheppard.name
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





-- 
keithsheppard.name
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Jake McArthur

Hans van Thiel wrote:

The only place I've ever seen Kleisli composition, or its flip, used is
in demonstrating the monad laws. Yet it is so elegant and, even having
its own name, it must have some practical use. Do you, or anybody else,
have some pointers?


I only just started finding places to use it myself, admittedly, but I 
now think it has common use and it fairly easy to spot. I'll take it 
slow, if not for you, as you seem to have a grasp on what these 
operators are already, then for other readers. Consider a function of 
this form:


foo x = a $ b $ c $ d $ e $ f x

The obvious thing to do here is to simply drop the `x` from both sides 
by using `(.)` instead of `($)`:


==

foo x = a . b . c . d . e . f $ x

==

foo = a . b . c . d . e . f

Now, consider this:

bar x = a = b = c = d = e = f x

If you compare that to the original version of `foo` above, you see that 
it is similar. In fact, looking at the types for `($)` and `(=)`:


($)   ::(a -   b) - (  a -   b)
(=) :: Monad m = (a - m b) - (m a - m b)

So, `(=)` is just like `($)` except for the information carried along 
by the monad.


Anyway, the obvious thing to do is to drop the `x` from both sides of 
the definition for `bar`. To do that with `foo` earlier, we had to 
substitute `($)` with `(.)`. What we are looking for is an equivalent 
operator for monads:


(.)   ::(b  c) - (a -   b) - (a -   c)
(=) :: Monad m = (b - m c) - (a - m b) - (a - m c)

So we now can do this:

==

bar x = a = b = c = d = e = f $ x

==

bar = a = b = c = d = e = f

And we're done.

Generally, you can transform anything of the form:

baz x1 = a = b = ... = z x1

into:

baz = a = b = ... = z

If you aren't already using `(=)` much more than `(=)` or 
do-notation then you will have a harder time finding opportunities to 
use `(=)` because only `(=)` has the same flow as function 
application, which allows your mind to play the appropriate association 
games. I suppose you could also replace `(=)` with `()`, but this 
would likely require more mental adjustment than replacing `(=)` with 
`(=)`.


- Jake
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Sjoerd Visscher

I had seen it before, and a bit of Googling turned up this:

  The monad laws can be written as
  return = g == g
  g = return == g
  (g = h) = k == g= (h = k)

  So, functions of type a - m b are the arrows of a category with  
(=) as composition,

  and return as identity.

  http://sites.google.com/site/haskell/category-theory/thekleislicategory

Although I think I saw them somewhere else.

Sjoerd

On Jun 18, 2009, at 1:23 PM, Hans van Thiel wrote:



On Wed, 2009-06-17 at 21:26 -0500, Jake McArthur wrote:

Jon Strait wrote:
I'm reading the third (bind associativity) law for monads in this  
form:


m = (\x - k x = h)  =  (m = k) = h


Arguably, that law would be better stated as:

(h = k) = m  =  h = (k = m)

This wouldn't be so unintuitive.

Hi,
The only place I've ever seen Kleisli composition, or its flip, used  
is

in demonstrating the monad laws. Yet it is so elegant and, even having
its own name, it must have some practical use. Do you, or anybody  
else,

have some pointers?

Best Regards,

Hans van Thiel


- Jake



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


--
Sjoerd Visscher
sjo...@w3future.com



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Jake McArthur

Jake McArthur wrote:

Generally, you can transform anything of the form:

baz x1 = a = b = ... = z x1

into:

baz = a = b = ... = z


I was just looking through the source for the recently announced Hyena 
library and decided to give a more concrete example from a real-world 
project. Consider this function from the project's Data.Enumerator 
module[1]:


compose enum1 enum2 f initSeed = enum1 f1 (Right initSeed) = k
where
  f1 (Right seed) bs = ...
  k (Right seed) = ...

First, I would flip the `(=)` into a `(=)` (and I will ignore the 
`where` portion of the function from now on):


compose enum1 enum2 f initSeed = k = enum1 f1 (Right initSeed)

Next, transform the `(=)` into a `(=)`:

compose enum1 enum2 f initSeed = k = enum1 f1 $ Right initSeed

We can move the `($)` to the right by using `(.)`:

compose enum1 enum2 f initSeed = k = enum1 f1 . Right $ initSeed

Finally, we can drop the `initSeed` from both sides:

compose enum1 enum2 f = k = enum1 f1 . Right

I didn't test that my transformation preserved the semantics of the 
function or even that the type is still the same, but even if it's wrong 
it should give you the idea.


- Jake

[1] 
http://github.com/tibbe/hyena/blob/9655e9e6473af1e069d22d3ee75537ad3b88a732/Data/Enumerator.hs#L117

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Colin Adams
What is enum2 doing in all of this - it appears to be ignored.

2009/6/18 Jake McArthur jake.mcart...@gmail.com:
 Jake McArthur wrote:

 Generally, you can transform anything of the form:

    baz x1 = a = b = ... = z x1

 into:

    baz = a = b = ... = z

 I was just looking through the source for the recently announced Hyena
 library and decided to give a more concrete example from a real-world
 project. Consider this function from the project's Data.Enumerator
 module[1]:

    compose enum1 enum2 f initSeed = enum1 f1 (Right initSeed) = k
        where
          f1 (Right seed) bs = ...
          k (Right seed) = ...

 First, I would flip the `(=)` into a `(=)` (and I will ignore the
 `where` portion of the function from now on):

    compose enum1 enum2 f initSeed = k = enum1 f1 (Right initSeed)

 Next, transform the `(=)` into a `(=)`:

    compose enum1 enum2 f initSeed = k = enum1 f1 $ Right initSeed

 We can move the `($)` to the right by using `(.)`:

    compose enum1 enum2 f initSeed = k = enum1 f1 . Right $ initSeed

 Finally, we can drop the `initSeed` from both sides:

    compose enum1 enum2 f = k = enum1 f1 . Right

 I didn't test that my transformation preserved the semantics of the function
 or even that the type is still the same, but even if it's wrong it should
 give you the idea.

 - Jake

 [1]
 http://github.com/tibbe/hyena/blob/9655e9e6473af1e069d22d3ee75537ad3b88a732/Data/Enumerator.hs#L117
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Neil Brown
Clicking on the source code link reveals that enum2 is used in the where 
clause.  It's not important to the transformation that Jake was performing.


In essence, = is the monadic version of . (function composition) and 
as explained, it can be used to do some pointfree-like programming in 
the presence of monads.  It's also handy in the arguments to things like 
mapM.  E.g.


f = mapM (\x - foo x = bar)

becomes:

f = mapM (bar = foo)

Neil.

Colin Adams wrote:

What is enum2 doing in all of this - it appears to be ignored.

2009/6/18 Jake McArthur jake.mcart...@gmail.com:
  

Jake McArthur wrote:


Generally, you can transform anything of the form:

   baz x1 = a = b = ... = z x1

into:

   baz = a = b = ... = z
  

I was just looking through the source for the recently announced Hyena
library and decided to give a more concrete example from a real-world
project. Consider this function from the project's Data.Enumerator
module[1]:

   compose enum1 enum2 f initSeed = enum1 f1 (Right initSeed) = k
   where
 f1 (Right seed) bs = ...
 k (Right seed) = ...

First, I would flip the `(=)` into a `(=)` (and I will ignore the
`where` portion of the function from now on):

   compose enum1 enum2 f initSeed = k = enum1 f1 (Right initSeed)

Next, transform the `(=)` into a `(=)`:

   compose enum1 enum2 f initSeed = k = enum1 f1 $ Right initSeed

We can move the `($)` to the right by using `(.)`:

   compose enum1 enum2 f initSeed = k = enum1 f1 . Right $ initSeed

Finally, we can drop the `initSeed` from both sides:

   compose enum1 enum2 f = k = enum1 f1 . Right

I didn't test that my transformation preserved the semantics of the function
or even that the type is still the same, but even if it's wrong it should
give you the idea.

- Jake

[1]
http://github.com/tibbe/hyena/blob/9655e9e6473af1e069d22d3ee75537ad3b88a732/Data/Enumerator.hs#L117
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
  


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Unicode workaround for getDirectoryContents under Windows?

2009-06-18 Thread Duncan Coutts
On Thu, 2009-06-18 at 04:47 +0300, Yitzchak Gale wrote:
 I wrote:
  OK, would you like me to reflect this discussion in tickets?
  Let's see, so far we have #3300, I don't see anything else.
 
  Do you want two tickets, one each for WIndows/Unix? Or
  four, separating the FilePath and getArgs issues?
 
 Simon Marlow wrote:
  One for each issue is usually better, so four.
 
 OK, they are: #3300, #3307, #3308, #3309.

Could we please make clear in those tickets that they only affect
Windows. I do hope we are only proposing that FilePath be interpreted as
Unicode on Window and OSX. It would break things to decode to Unicode on
Unix systems. On Unix filepaths really are strings of bytes, not an
encoding of Unicode code points. It's true that this is not reflected
accurately in the type FilePath = String.

The FilePath should be an opaque type that allows decoding into a human
readable Unicode String.

I wonder how much code would actually break if FilePath became an opaque
type, eg if we make it an instance of IsString. It only need change in
System.IO and System.FilePath, not in the old H98 modules.

Duncan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: curious about sum

2009-06-18 Thread Edward Kmett
On Thu, Jun 18, 2009 at 9:14 AM, Gleb Alexeyev gleb.alex...@gmail.comwrote:

 Thomas Davie wrote:

 No, I think it's extremely useful.  It highlights that numbers can both be
 lazy and strict, and that the so called useless lazy sum, is in fact,
 useful.

 But lazy sum should have beed defined in terms of foldr, not foldl. And
 foldl is not strict enough for strict sum. Therefore the current choice in
 the worst of both worlds.



I definitely agree with that sentiment.

-Edward Kmett
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Creating a new Haskell mailing list

2009-06-18 Thread Henning Thielemann
Ryan Trinkle schrieb:
 Hi all,
 
 I'm interested in starting a mailing list on haskell.org
 http://haskell.org.  Who should I talk to about such things?

Is it a mailing list related to a project? Then you may request a
project on community.haskell.org, then you can start a mailing list at
yourproj...@project.haskell.org
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Runtime strictness analysis for polymorphic HOFs?

2009-06-18 Thread Edward Kmett
I have been exploring a weak form of runtime strictness analysis in a
tracing JIT project of my own in the spirit of TraceMonkey. Basically a
tracing JIT doesn't compile blocks of code but instead once a tracing point
has been passed often enough, it starts a trace from that point logging the
series of instructions executed to a bounded log. If you make it back to the
starting point before the log fills up then you've identified a superblock
with a bunch of side exits for handling when your assumptions are violated.

What is interesting is in a lazy setting, if you are tracing a bytecode
representation that knows about allocation and thunks, you can do some
additional optimizations in here. If on every path to a side exit or the end
of the loop you find that the thunk is evaluated you can evaluate it
strictly and move its execution earlier in the trace. This gives you a weak
form of runtime strictness analysis. If the pointer to that thunk never
escapes, then you can unbox the contents of the thunk and operate on its
members in registers. Add constant folding, polyinline caching to improve
branch prediction for spineless tagless g-machine thunk evaluation, and code
migration to the side exits and it becomes an interesting runtime system.

But the key concern for the sake of the current discussion is that it models
strictness analysis and unboxing quite naturally as an artifact of the
jitting process. So that said, a form of conservative strictness analysis at
runtime is possible.

-Edward Kmett
On Sun, Jun 14, 2009 at 7:42 PM, Paul Chiusano paul.chius...@gmail.comwrote:

 Hello,
 I was recently trying to figure out if there was a way, at runtime, to do
 better strictness analysis for polymorphic HOFs, for which the strictness of
 some arguments might depend on the strictness of the strictness of function
 types that are passed as arguments [1]. As an example, consider foldl. The
 'seed' parameter of foldl can be made strict as long as the binary function
 used for the fold is strict in its arguments. Unfortunately, because
 strictness analysis is done statically, Haskell can't assume anything about
 the strictness of the binary function - assuming we only compile one
 instance of foldl, it must be the most conservative version possible, and
 that means making the seed parameter lazy. :-(

 I started thinking about ways you could to a check at runtime for this sort
 of thing, something to the effect of asking foldl, before heap-allocating a
 thunk for the seed parameter, whether that parameter could be made strict.
 foldl could then inspect other arguments that have been supplied, and based
 on these arguments, evaluate or go ahead with creating a thunk the seed
 parameter. It's a runtime cost, sure, but would it be more than the cost of
 having to do an additional heap allocation? In any case a small runtime cost
 might be worth it if the analysis becomes more uniform.

 So, my question is: does doing this sort of runtime analysis seem like a
 horrible idea? Is it even possible? Has anyone tried this in the past? (So
 far I haven't found anything, but would love references if people have
 them.)

 Note that I'm not suggesting Haskell should do anything like this. I'm
 playing around with the ideas because I'm interesting in creating a lazy
 language and I was hoping to have strictness analysis be very predictable
 and uniform, something the programmer can count on and use to simply reason
 about space usage ... which might be hopelessly unrealistic goal! I guess
 the more general question is - is perfect strictness analysis (however
 that is defined) possible, if we're willing to incur some runtime cost? What
 would that look like?

 Best,
 Paul

 [1]:
 More background on my thinking here - a bit half-baked, so bear with me!

 http://pchiusano.blogspot.com/2009/06/perfect-strictness-analysis-part-1.html

 http://pchiusano.blogspot.com/2009/06/perfect-strictness-analysis-part-2.html

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] how to #include files within parsec ... without unsafePerformIO?

2009-06-18 Thread Daniel Schüssler
Hi,

ParsecT with m=IO? Your 'do' block would become:

 do
   i - getInput
   included - liftIO readI -- import Control.Monad.Trans for liftIO
   setInput included
   a - my_str
   setInput i
   b - my_str
   return $ a ++  //\n\n  ++ b
 where
   readI = readFile experiment.hs


Maybe I'm misunderstanding the problem.

Greetings,
Daniel


On Thursday 18 June 2009 13:58:53 Leonard Siebeneicher wrote:
 Dear reader,

 I wonder whether there is a 'general' working solution to include files
 within a parsec parser. Without the need of unsafePerformIO.

 Appending an example program, using unsafePerformIO.

 Thanx for reading.

 Greetings,
 Leonard Siebeneicher


 --- Begin: experiment.hs ---
 import Text.ParserCombinators.Parsec
 import System.IO.Unsafe

 my_str :: Parser String
 my_str = many1 anyToken

 wrap_input :: Parser String - Parser String
 wrap_input p =
 do
   i - getInput
   setInput readI
   a - my_str
   setInput i
   b - my_str
   return $ a ++  //\n\n  ++ b
 where
   {- Aaaah ... any solution without unsafePerformIO? -}
   readI = unsafePerformIO (readFile experiment.hs)


 main =
 case parse (wrap_input my_str)  eintest of
   Left err - putStrLn Error raised
   Right ostr - putStrLn ostr
 --- End: experiment.hs ---


 ___
 Thinking about a special type like

 data MyInclude = PlainText String

| IncludeFile String

 the parser could generate [MyInclude] data, but it does not work
 generally.



 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Hans van Thiel

On Thu, 2009-06-18 at 08:34 -0500, Jake McArthur wrote:
[snip]
 So, `(=)` is just like `($)` except for the information carried along 
 by the monad.
 
 Anyway, the obvious thing to do is to drop the `x` from both sides of 
 the definition for `bar`. To do that with `foo` earlier, we had to 
 substitute `($)` with `(.)`. What we are looking for is an equivalent 
 operator for monads:
 
  (.)   ::(b  c) - (a -   b) - (a -   c)
Just to show I'm paying attention, there's an arrow missing, right?
   (.)   ::(b  -  c) - (a -   b) - (a -   c)

Many thanks, also to the others who've replied. I've wondered about
(=) usage for a long time too, and this is all very illuminating. I'll
work this through and put it in my monad tutorial, if I may (without
implicating you guys in any way, of course, unless you insist...)

Regards,

Hans van Thiel
[snip]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Need some help with an infinite list - Ouch

2009-06-18 Thread Lee Duhem
On Wed, Jun 17, 2009 at 7:30 PM, GüŸnther Schmidtgue.schm...@web.de wrote:
 Hi all,

 you have come up with so many solutions it's embarrassing to admit that I
 didn't come up with even one.

I have the similarly difficulties, but I found to understand some of
these answers,
equational reasoning is a very useful tool, I have prepared a blog post for how
I worked out some of these answers, here is the draft of it, I hope it
can help you
too.

Oh, if it doesn't help you at all, please let know why :-)

lee



Understanding Functions Which Use 'instance Monad []' by Equational Reasoning

GüŸnther Schmidt asked in Haskell-Cafe how to get a stream like this:

[a, ... , z, aa, ... , az, ba, ... , bz, ... ]

and people in Haskell-Cafe offer some interesting answer for this question.
On the one hand, these answers show the power of Haskell and GHC base libraries,
but on the other hand, understanding them is a challenge for Haskell
newbie like me.
But I found to understand these answers, equational reasoning is very helpful,
here is why I think so.

Answer 1 (by Matthew Brecknell):

concat $ tail $ iterate (map (:) ['a' .. 'z'] *) [[]]

Well, how does this expression do what we want? concat, tail, iterate,
map, are easy,
looks like the magic is in (*).

What's this operator mean? (*) comes from class Applicative of
Control.Applicative,

class Functor f = Applicative f where
-- | Lift a value.
pure :: a - f a

-- | Sequential application.
(*) :: f (a - b) - f a - f b

and 'instance Applicative []' is

instance Applicative [] where
pure = return
(*) = ap

ap comes from Control.Monad

ap :: (Monad m) = m (a - b) - m a - m b
ap =  liftM2 id

liftM2  :: (Monad m) = (a1 - a2 - r) - m a1 - m a2 - m r
liftM2 f m1 m2 = do { x1 - m1; x2 - m2; return (f x1 x2) }

so the key to understand (*) is understanding the meaning of liftM2.

liftM2 uses, hum, do-notation, so by Haskell 98 report, this can be
translated to

  liftM2 f m1 m2
(1.0)   = m1 = \x1 -
  m2 = \x2 -
  return (f x1 x2)

When it is applied to list (you can convince yourself of this by type
inference),
wee need 'instance Monad []'

instance  Monad []  where
m = k = foldr ((++) . k) [] m
m  k  = foldr ((++) . (\ _ - k)) [] m
return x= [x]
fail _  = []

so
  liftM2 f m1 m2
= m1 = \x1 -
  m2 = \x2 -
  return (f x1 x2)

let
  f1
=\x1 -
  m2 = \x2 -
  return (f x1 x2)

  f2
= \x2 - return (f x1 x2)

we can write

  m1 = f1
= foldr ((++) . f1) [] m1

  m2 = f2
= foldr ((++) . f2) [] m2

Now we can see for list m1, m2, how does 'liftM2 f m1 m2' work

z1 = []
foreach x1 in (reverse m1); do  -- foldr ((++) . f1) [] m1
z2 = []
foreach x2 in (reverse m2); do  -- foldr ((++) . f2) [] m2
z2 = [f x1 x2] ++ z2
done
z1 = z2 ++ z1
done

Now we are ready to see how to apply (*):

  map (:) ['a' .. 'z'] * [[]]
= (map (:) ['a' .. 'z']) * [[]]
= [('a':), ..., ('z':)] * [[]]-- misuse of [...] notation
= ap [('a':), ..., ('z':)] [[]]
= liftM2 id [('a':), ..., ('z':)] [[]]
= [('a':), ..., ('z':)] = \x1 -
  [[]]  = \x2 -
  return (id x1 x2)

Here x1 bind to ('z':), ..., ('a':) in turn, x2 always bind to [], and
noticed that

  return (id ('z':) []) -- f = id; x1 = ('a':); x2 = []
= return (('z':) [])
= return ((:) 'z' [])
= return z
= [z]

we have
  map (:) ['a', .., 'z'] * [[]]
= liftM2 id [('a':), ..., ('z':)] [[]]
= [a, ..., z]

(If you can't follow the this, work through the definition of foldr
step by step will be very helpful.)

  map (:) ['a', .., 'z'] * (map (:) ['a', .., 'z'] * [[]])
= map (:) ['a', .., 'z'] * [a, .., z]
= liftM2 id [('a':), ..., ('z':)] [a, ..., z]
= [aa, ..., az, ba, ..., bz, ..., za, ..., zz]

Now it's easy to know what we get from

  iterate (map (:) ['a' .. 'z'] *) [[]]
= [[], f [[]], f (f [[]]), ...] -- f = map (:) ['a' .. 'z'] *

so
concat $ tail $ iterate (map (:) ['a' .. 'z'] *) [[]]

is exactly what we want.

Understanding Haskell codes by equational reasoning could be a very
tedious process, but it's also
a very helpful and instructive process for the beginners, because it
make you think slowly, check
the computation process step by step, just like the compiler does. And
in my opinion, this is exactly
what a debugger does.

Answer 2 (by Reid Barton):

concatMap (\n - replicateM n ['a'..'z']) 

Re: [Haskell-cafe] Runtime strictness analysis for polymorphic HOFs?

2009-06-18 Thread Max Bolingbroke
2009/6/18 Edward Kmett ekm...@gmail.com:
 What is interesting is in a lazy setting, if you are tracing a bytecode
 representation that knows about allocation and thunks, you can do some
 additional optimizations in here. If on every path to a side exit or the end
 of the loop you find that the thunk is evaluated you can evaluate it
 strictly and move its execution earlier in the trace. This gives you a weak
 form of runtime strictness analysis. If the pointer to that thunk never
 escapes, then you can unbox the contents of the thunk and operate on its
 members in registers. Add constant folding, polyinline caching to improve
 branch prediction for spineless tagless g-machine thunk evaluation, and code
 migration to the side exits and it becomes an interesting runtime system.

This sounds absolutely awesome! Is the source code for your prototype
publicly available anywhere? I'd love to take a look at the basic
structure of something like this - trace JITing is something I keep
meaning to look at in more depth.

Cheers,
Max
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Fwd: [Haskell-cafe] Re: curious about sum

2009-06-18 Thread Alberto G. Corona
However it does not work as I expected. I ´m interested in memory
management.
I though that

ghci let l= [1..100]
ghci foldl' (+) 0 l

would produce a stack overflow, since the list can not be freed, because l
points to the beginning of the list
however it succeed

My conclussion is that, in the case of sum, with the lazy evaluation,  isn´t
the list what is the cause of the stack overflow, but the lazy structure

0 + 1 +2 + 3 +4

created by foldl before the final evaluation

I tried to test how I can force a stack overflow, in the strict case. Since
foldl' is strict,  the only way is to create a long long initial list.

ghci let l= [1..1]  -- 100 times larger, two 0's added
ghci foldl' (+) 0 l

as in the previous case, l will not garbage collect, and foldl' must build
the real list [1,2,3,4..]  . at the end perhaps a stack overflow willl be
produced

This is not the case in my windows system.ghc 6.10.1.   instead of that,
ghci grow to gigabyte size and more. I stopped the process or my machine
would be irresponsive.

My question is: Why the process does not grow also in the lazy case and
instead produces a stack overflow inmediately?

Yes I know that all of this is bizantine since strictness analysis would
solve this when compiled, but I think that there are somethig that i don´t
understand.

 It´s  a bug perhaps???

2009/6/18 Edward Kmett ekm...@gmail.com

 On Thu, Jun 18, 2009 at 9:14 AM, Gleb Alexeyev gleb.alex...@gmail.comwrote:

 Thomas Davie wrote:

 No, I think it's extremely useful.  It highlights that numbers can both
 be lazy and strict, and that the so called useless lazy sum, is in fact,
 useful.

 But lazy sum should have beed defined in terms of foldr, not foldl. And
 foldl is not strict enough for strict sum. Therefore the current choice in
 the worst of both worlds.



 I definitely agree with that sentiment.

 -Edward Kmett

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: curious about sum

2009-06-18 Thread Chaddaï Fouché
On Thu, Jun 18, 2009 at 6:38 PM, Alberto G. Coronaagocor...@gmail.com wrote:
 My question is: Why the process does not grow also in the lazy case and
 instead produces a stack overflow inmediately?

This question is answered in detail on the Wiki
http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27
As you thought, it is not the evaluation of foldl that overflow the
stack, since foldl is terminally recursive it won't ever overflow the
stack. On the other hand when the thunk created by foldl is finally
evaluated, if the argument function is strict in its first argument it
will overflow the stack if the list was too long.

It is important to note that the size of the list itself or even the
thunk doesn't guarantee a stack overflow : both of those structure are
in the heap and if the argument function can produce output before
evaluating its first argument, there will be no stack overflow,
whatever the size of the thunk.

As evidenced by this expression :
ghci take 10 . foldl (flip (:)) [] $ [1..100]
[100,99,98,97,96,95,94,93,92,91]

-- 
Jedaï
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: curious about sum

2009-06-18 Thread Alberto G. Corona
Very informative. The list is in the heap but the lazy sum of foldl  is in
the stack. ok.I suppose that all tail recursive functions are detected by
the strictness analysis.

2009/6/18 Chaddaï Fouché chaddai.fou...@gmail.com

 On Thu, Jun 18, 2009 at 6:38 PM, Alberto G. Coronaagocor...@gmail.com
 wrote:
  My question is: Why the process does not grow aleso in the lazy case and
  instead produces a stack overflow inmediately?

 This question is answered in detail on the Wiki
 http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27
 As you thought, it is not the evaluation of foldl that overflow the
 stack, since foldl is terminally recursive it won't ever overflow the
 stack. On the other hand when the thunk created by foldl is finally
 evaluated, if the argument function is strict in its first argument it
 will overflow the stack if the list was too long.

 It is important to note that the size of the list itself or even the
 thunk doesn't guarantee a stack overflow : both of those structure are
 in the heap and if the argument function can produce output before
 evaluating its first argument, there will be no stack overflow,
 whatever the size of the thunk.

 As evidenced by this expression :
 ghci take 10 . foldl (flip (:)) [] $ [1..100]
 [100,99,98,97,96,95,94,93,92,91]

 --
 Jedaï

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confusion on the third monad law when using lambda abstractions

2009-06-18 Thread Jake McArthur

Hans van Thiel wrote:

Just to show I'm paying attention, there's an arrow missing, right?
   (.)   ::(b  -  c) - (a -   b) - (a -   c)


Correct. I noticed that after I sent it but I figured that it would be 
noticed.


I also used () where I meant (=) at the bottom. They are 
semantically the same, of course, but () requires the Kleisli newtype. :(



Many thanks, also to the others who've replied. I've wondered about
(=) usage for a long time too, and this is all very illuminating. I'll
work this through and put it in my monad tutorial, if I may (without
implicating you guys in any way, of course, unless you insist...)


You're welcome. I do not insist on anything either way. ;)

- Jake
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Running a sub-process which dies with the main program

2009-06-18 Thread Deniz Dogan
Hi

I couldn't come up with a better subject than this one, so anyways...

I have a small program which spawns a subprocess. However, when I hit
C-c, the subprocess won't die, instead it will just keep running until
it's done or until I kill it. I've looked around in System.Process for
something suitable for my needs, but I can't seem to find it. Any
ideas?

-- 
Deniz Dogan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Use MySQL from Haskell

2009-06-18 Thread Maciej Podgurski

Hello,

I'm trying to use MySQL from Haskell but it seems impossible for me to 
install one of the MySQL packages on my Windows XP machine.


First I tired to install hsql-mysql-1.7.1 on GHC 6.10.3 but installing 
haskelldb-hsql failed with a hidden package error. So I added the 
old-time package to the cabal file but there still was a compile error 
(something due to the change from Exception to Exception e = e).


So I switched to GHC 6.8.3 and tried it again. Now it says:

Configuring hsql-mysql-1.7.1...
Warning: 'extra-lib-dirs: /usr/lib/mysql' directory does not exist.
Warning: 'include-dirs: /usr/include/mysql' directory does not exist.
Warning: This package indirectly depends on multiple versions of the same
package. This is highly likely to cause a compile failure.
package process-1.0.0.1 requires filepath-1.1.0.0
package directory-1.0.0.1 requires filepath-1.1.0.0
package Cabal-1.6.0.3 requires filepath-1.1.0.2
Setup: Missing dependency on a foreign library:
* Missing C library: mysqlclient
This problem can usually be solved by installing the system package that
provides this library (you may need the -dev version). If the library is
already installed but in a non-standard location then you can use the flags
--extra-include-dirs= and --extra-lib-dirs= to specify where it is.

There's no Haskell package mysqlclient and I don't know how to install a 
C library in Haskell. So I switched to HDBC-2.1.1 and got the next 
compile error:


Building convertible-1.0.5...

Data/Convertible/Instances/Num.hs:671:0:
warning: no newline at end of file
[...]
[5 of 8] Compiling Data.Convertible.Instances.C ( 
Data/Convertible/Instances/C.hs, dist\build/Data/C

onvertible/Instances/C.o )
[6 of 8] Compiling Data.Convertible.Instances.Time ( 
Data/Convertible/Instances/Time.hs, dist\build/

Data/Convertible/Instances/Time.o )

Data/Convertible/Instances/Time.hs:64:0:
   Duplicate instance declarations:
 instance Typeable NominalDiffTime
   -- Defined at Data/Convertible/Instances/Time.hs:(64,0)-(65,42)
 instance Typeable NominalDiffTime
   -- Defined in time-1.1.3:Data.Time.Clock.UTC

Data/Convertible/Instances/Time.hs:67:0:
   Duplicate instance declarations:
 instance Typeable UTCTime
   -- Defined at Data/Convertible/Instances/Time.hs:(67,0)-(68,34)
 instance Typeable UTCTime
   -- Defined in time-1.1.3:Data.Time.Clock.UTC

So please help me, what GHC/package configuration will I need to use 
MySQL from my Haskell programs on Windows? I really like Haskell but all 
those broken packages are really discouraging. :(



Best wishes,
Maciej
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Running a sub-process which dies with the main program

2009-06-18 Thread Donn Cave
Quoth Deniz Dogan deniz.a.m.do...@gmail.com,

 I have a small program which spawns a subprocess. However, when I hit
 C-c, the subprocess won't die, instead it will just keep running until
 it's done or until I kill it. I've looked around in System.Process for
 something suitable for my needs, but I can't seem to find it. Any
 ideas?

What you want, is the normal behavior for Berkeley/UNIX/POSIX ttys:
signals generated by the (pseudo)tty handler from control characters
are delivered to all processes in the foreground process group, which
is a hereditary distinction where you have to actively opt out.

Berkeley job control shells (bash, ksh et al.) reset process group on
commands issuing from the terminal, so the foreground process group is
whatever you invoked, and any subprocesses thereof (barring further
process group resets), and that's what will abort if you press ctrl-C.

If it isn't working that way, possibilities might include:

 - not a POSIX operating system

 - subprocess set its process group and is no longer in the terminal
   foreground process group.

 - a signal handler caught/ignored ctrl-C.

Along with process group stuff in System.Process, if both processes are
Haskell you might look at System.Posix.Terminal getTerminalProcessGroupID,
as a way to directly check the second item above (and I suppose indirectly
the first.)  Unfortunately, none of these explanations come with any
suggested remedy - they're just three ways to say platform [OS or Haskell 
implementation] doesn't support POSIX ttys.

Donn

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Need some help with an infinite list

2009-06-18 Thread Tom Pledger
Daniel Peebles pumpkingod at gmail.com writes:
 My solution attempted to exploit this using Numeric.showIntAtBase but
 failed because of the lack of 0 prefixes in the numbers. If you can
 find a simple way to fix it without duplicating the showIntAtBase
 code, I'd be interested!


Another advantage of the integer  base method is that it doesn't require a
fast-growing amount of memory to keep track of everything between two points in
the list.

e.g.

Hugs let mywords = :[w++[ch] | w - mywords, ch - ['a'..'z']] in
mywords!!100

ERROR - Garbage collection fails to reclaim sufficient space

or

Hugs let sss = [] : [ [ c:s | c - ['a'..'z'], s - ss ] | ss - sss ] in
concat (tail sss) !! 100

ERROR - Garbage collection fails to reclaim sufficient space


I'm not sure offhand why Reid Barton's replicateM solution doesn't have the same
problem.  Is it a benefit of the lack of sharing Matthew Brecknell mentioned?

Control.Monad concatMap (\n - replicateM n ['a'..'z']) [1..] !! 500
jxlks

Regards,
Tom


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Use MySQL from Haskell

2009-06-18 Thread Michael Snoyman
Marciej,

I went the HDBC route and got the same problem. Although it does not seem to
be officially blessed, try installing the time-1.1.3 package. It's working
for me at least, which I know is a dubious recommendation.

Also, I am currently using the hdbc-odbc package for accessing MySQL. I
couldn't get hdbc-mysql to work properly. I hope that once I get this
project working right, I'll have a chance to dig into the hdbc-mysql issue
itself.

Good luck!

Michael

On Fri, Jun 19, 2009 at 12:26 AM, Maciej Podgurski 
maciej.podgur...@googlemail.com wrote:

 Hello,

 I'm trying to use MySQL from Haskell but it seems impossible for me to
 install one of the MySQL packages on my Windows XP machine.

 First I tired to install hsql-mysql-1.7.1 on GHC 6.10.3 but installing
 haskelldb-hsql failed with a hidden package error. So I added the old-time
 package to the cabal file but there still was a compile error (something due
 to the change from Exception to Exception e = e).

 So I switched to GHC 6.8.3 and tried it again. Now it says:

 Configuring hsql-mysql-1.7.1...
 Warning: 'extra-lib-dirs: /usr/lib/mysql' directory does not exist.
 Warning: 'include-dirs: /usr/include/mysql' directory does not exist.
 Warning: This package indirectly depends on multiple versions of the same
 package. This is highly likely to cause a compile failure.
 package process-1.0.0.1 requires filepath-1.1.0.0
 package directory-1.0.0.1 requires filepath-1.1.0.0
 package Cabal-1.6.0.3 requires filepath-1.1.0.2
 Setup: Missing dependency on a foreign library:
 * Missing C library: mysqlclient
 This problem can usually be solved by installing the system package that
 provides this library (you may need the -dev version). If the library is
 already installed but in a non-standard location then you can use the flags
 --extra-include-dirs= and --extra-lib-dirs= to specify where it is.

 There's no Haskell package mysqlclient and I don't know how to install a C
 library in Haskell. So I switched to HDBC-2.1.1 and got the next compile
 error:

 Building convertible-1.0.5...

 Data/Convertible/Instances/Num.hs:671:0:
warning: no newline at end of file
 [...]
 [5 of 8] Compiling Data.Convertible.Instances.C (
 Data/Convertible/Instances/C.hs, dist\build/Data/C
 onvertible/Instances/C.o )
 [6 of 8] Compiling Data.Convertible.Instances.Time (
 Data/Convertible/Instances/Time.hs, dist\build/
 Data/Convertible/Instances/Time.o )

 Data/Convertible/Instances/Time.hs:64:0:
   Duplicate instance declarations:
 instance Typeable NominalDiffTime
   -- Defined at Data/Convertible/Instances/Time.hs:(64,0)-(65,42)
 instance Typeable NominalDiffTime
   -- Defined in time-1.1.3:Data.Time.Clock.UTC

 Data/Convertible/Instances/Time.hs:67:0:
   Duplicate instance declarations:
 instance Typeable UTCTime
   -- Defined at Data/Convertible/Instances/Time.hs:(67,0)-(68,34)
 instance Typeable UTCTime
   -- Defined in time-1.1.3:Data.Time.Clock.UTC

 So please help me, what GHC/package configuration will I need to use MySQL
 from my Haskell programs on Windows? I really like Haskell but all those
 broken packages are really discouraging. :(


 Best wishes,
 Maciej
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Need some help with an infinite list

2009-06-18 Thread Matthew Brecknell
On Thu, 2009-06-18 at 23:57 +0800, Lee Duhem wrote:
 [...] I have prepared a blog post for how
 I worked out some of these answers, here is the draft of it, I hope it
 can help you too.

Nice post! Certainly, pen-and-paper reasoning like this is a very good
way to develop deeper intuitions.

   Answer 1 (by Matthew Brecknell):
 
   concat $ tail $ iterate (map (:) ['a' .. 'z'] *) [[]]

I actually said tail $ concat $ iterate ..., because I think the
initial empty string is logically part of the sequence. Tacking tail
on the front then produces the subsequence requested by the OP.

I should have given more credit to Reid for this solution. I'm always
delighted to see people using monadic combinators (like replicateM) in
the list monad, because I so rarely think to use them this way. Sadly,
my understanding of these combinators is still somewhat stuck in IO,
where I first learned them. I never would have thought to use * this
way if I had not seen Reid's solution first.

Also, for many applications, a non-sharing version like Reid's is really
what you want. Sharing versions have to keep references to old strings
around to reuse later, and so are really only appropriate for
applications which would keep them in memory anyway.

Regards,
Matthew



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: ANN: haskell-src-exts 1.0.0 rc1 (aka 0.5.2)

2009-06-18 Thread Niklas Broberg
 I have just uploaded haskell-src-exts-0.5.4 to hackage, which is 1.0.0
 rc2. Thanks a lot to those who tested the previous version, and please
 continue to test and report!

Another day, another release candidate. Please see
haskell-src-exts-0.5.5, 1.0.0 rc3. Thanks a lot to all reports, and
please keep up the good work!

Changes in 0.5.5:


* BangPatterns are now correctly handled everywhere (I think - tricksy
little imps they are). I would like to put out a special call for
tests of files that use bang patterns, in particular if they appear in
strange locations inside patterns.

* TypeFamilies now implies KindSignatures, as in GHC.

* Chained contexts, e.g. foo :: Eq a = Show a = a, are now handled correctly.

* . is no longer considered a reserved operator but a special operator
when explicit forall is enabled, which means Prelude.. now parses
correctly.

* Parenthesised patterns inside list patterns no longer require
RegularPatterns enabled.

* List expressions are no longer translated to tuple expressions by
the fixity mangler (yes, it did that)...


Cheers,

/Niklas
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Need some help with an infinite list

2009-06-18 Thread Lee Duhem
On Fri, Jun 19, 2009 at 6:17 AM, Matthew Brecknellhask...@brecknell.org wrote:
 On Thu, 2009-06-18 at 23:57 +0800, Lee Duhem wrote:
 [...] I have prepared a blog post for how
 I worked out some of these answers, here is the draft of it, I hope it
 can help you too.

 Nice post! Certainly, pen-and-paper reasoning like this is a very good
 way to develop deeper intuitions.

       Answer 1 (by Matthew Brecknell):

       concat $ tail $ iterate (map (:) ['a' .. 'z'] *) [[]]

 I actually said tail $ concat $ iterate ..., because I think the
 initial empty string is logically part of the sequence. Tacking tail
 on the front then produces the subsequence requested by the OP.

Yes, I changed your solution from tail $ concat $ iterate ... to
concat $ tail $ iterate ..., because I think cut useless part out early
is good idea, forgot to mention that, sorry.


 I should have given more credit to Reid for this solution. I'm always
 delighted to see people using monadic combinators (like replicateM) in
 the list monad, because I so rarely think to use them this way. Sadly,
 my understanding of these combinators is still somewhat stuck in IO,
 where I first learned them. I never would have thought to use * this
 way if I had not seen Reid's solution first.

Actually, I first figure out how Reid's solution works, then figure out yours.
After that, I found, for me, your solution's logic is easier to understand,
so I take it as my first example. As I said at the end, or as I'll
said at the end,
Reid' solution and yours are the same (except effective)

lee
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Compiling to C

2009-06-18 Thread Greg Santucci
Hi all,

I'd like to call Haskell functions from C and receive its output as a
string. Is compiling a Haskell program to C using ghc -C HaskellSource.hs
the preferred method of doing this?

Regards,
Greg
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] About the Monad Transformers

2009-06-18 Thread Brandon S. Allbery KF8NH

On Jun 17, 2009, at 11:54 , .shawn wrote:

mapTreeM action (Leaf a) = do
lift (putStrLn (Leaf ++ show a))
b - action a
return (Leaf b)

mapTreeM :: (MonadTrans t, Monad (t IO), Show a) = (a - t IO a1) - 
 Tree a - t IO (Tree a1)


Why does the type signature of mapTreeM look like this?


A monad transformer wraps one monad around another.  To get at the  
wrapped monad, you use (lift).  The context (MonadTrans t) specifies  
that the type (t) is a monad transformer, meaning it provides (lift).   
The type (t) actually looks like (StateT s), but this isn't needed to  
write (mapTreeM) (it will, however, be needed when you write functions  
to be passed as (action)).


The context (Monad (t IO)), given the MonadTrans context above, then  
declares that the transformer (t) is wrapped around the IO monad, so  
Haskell knows what you are (lift)ing *to*.


You use this when you want to build a monad based on IO but with extra  
functionality, such as (StateT IO) which wraps an IO monad in a StateT  
transformer so you can carry around extra information in the state.


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com
system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu
electrical and computer engineering, carnegie mellon universityKF8NH




PGP.sig
Description: This is a digitally signed message part
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Compiling to C

2009-06-18 Thread Greg Santucci
Never mind, I Found The Manual. (FTFM)

http://www.haskell.org/ghc/docs/latest/html/users_guide/win32-dlls.html



On Fri, Jun 19, 2009 at 12:51 PM, Greg Santucci thecodewi...@gmail.comwrote:

 Hi all,

 I'd like to call Haskell functions from C and receive its output as a
 string. Is compiling a Haskell program to C using ghc -C HaskellSource.hs
 the preferred method of doing this?

 Regards,
 Greg


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] IORef memory leak

2009-06-18 Thread Jim Snow


I'm having some trouble with excessive memory use in a program that uses 
a lot of IORefs.  I was able to write a much simpler program which 
exhibits the same sort of behavior.  It appears that modifyIORef and 
writeIORef leak memory; perhaps they keep a reference to the old 
value.  I tried both ghc-6.8.3 and ghc-6.10.1.


Is this a known limitation, or is this a ghc bug, or am I using IORefs 
in the wrong way?


-jim


module Main where

import Data.IORef
import Control.Monad

-- Leaks memory
leakcheck1 ior =
do go 10
where
   go 0 = return ()
   go n = do modifyIORef ior (+1)
 go (n-1)

-- Leaks memory
leakcheck2 ior =
do go 10
where
   go 0 = return ()
   go n = do x - readIORef ior
 writeIORef ior (x+1)
 go (n-1)

-- Runs in constant memory
leakcheck3 ior =
do go 10
where
   go 0 = return ()
   go n = do x - readIORef ior
 go (n-1)

main :: IO ()
main =
do ior - newIORef 0
   leakcheck2 ior


compiled with: ghc -O2 --make Leak.hs -o Leak
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IORef memory leak

2009-06-18 Thread Ross Mellgren
It looks offhand like you're not being strict enough when you put  
things back in the IORef, and so it's building up thunks of (+1)...


With two slight mods:

   go 0 = return ()
   go n = do modifyIORef ior (+1)
 go (n-1)

--

   go 0 = return ()
   go n = do modifyIORef ior (\ x - let x' = x+1 in x `seq` x')
 go (n-1)

and

   go n = do x - readIORef ior
 writeIORef ior (x+1)
 go (n-1)

--

   go n = do x - readIORef ior
 writeIORef ior $! x+1
 go (n-1)

It runs much better (with loop count = 10,000,000) -- leak1 is the  
code you posted, leak2 is with these changes:


r...@hugo:~$ ./leak1 +RTS -s
./leak1 +RTS -s
 200,296,364 bytes allocated in the heap
 365,950,896 bytes copied during GC
  66,276,472 bytes maximum residency (7 sample(s))
   1,906,448 bytes maximum slop
 131 MB total memory in use (1 MB lost due to  
fragmentation)

snip
  %GC time  75.9%  (79.2% elapsed)

  Alloc rate977,656,335 bytes per MUT second

  Productivity  24.0% of total user, 20.5% of total elapsed

r...@hugo:~$ ./leak2 +RTS -s
./leak2 +RTS -s
 160,006,032 bytes allocated in the heap
  11,720 bytes copied during GC
   1,452 bytes maximum residency (1 sample(s))
   9,480 bytes maximum slop
   1 MB total memory in use (0 MB lost due to  
fragmentation)

snip
  %GC time   0.5%  (0.8% elapsed)

  Alloc rate626,590,037 bytes per MUT second

  Productivity  99.2% of total user, 97.8% of total elapsed


-Ross


On Jun 18, 2009, at 10:46 PM, Jim Snow wrote:



I'm having some trouble with excessive memory use in a program that  
uses a lot of IORefs.  I was able to write a much simpler program  
which exhibits the same sort of behavior.  It appears that  
modifyIORef and writeIORef leak memory; perhaps they keep a  
reference to the old value.  I tried both ghc-6.8.3 and ghc-6.10.1.


Is this a known limitation, or is this a ghc bug, or am I using  
IORefs in the wrong way?


-jim


module Main where

import Data.IORef
import Control.Monad

-- Leaks memory
leakcheck1 ior =
do go 10
where
  go 0 = return ()
  go n = do modifyIORef ior (+1)
go (n-1)

-- Leaks memory
leakcheck2 ior =
do go 10
where
  go 0 = return ()
  go n = do x - readIORef ior
writeIORef ior (x+1)
go (n-1)

-- Runs in constant memory
leakcheck3 ior =
do go 10
where
  go 0 = return ()
  go n = do x - readIORef ior
go (n-1)

main :: IO ()
main =
do ior - newIORef 0
  leakcheck2 ior


compiled with: ghc -O2 --make Leak.hs -o Leak
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IORef memory leak

2009-06-18 Thread Tim Docker

 I'm having some trouble with excessive memory use in a program that uses
 a lot of IORefs.  I was able to write a much simpler program which
 exhibits the same sort of behavior.  It appears that modifyIORef and
 writeIORef leak memory; perhaps they keep a reference to the old
 value.  I tried both ghc-6.8.3 and ghc-6.10.1.

modifyIORef and writeIORef are not sufficiently strict for your needs. See
this recent thread:

http://www.nabble.com/Stack-overflow-td23746120.html

Tim


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] packages on Hackage?

2009-06-18 Thread Vasili I. Galchin
Hello,

  Haskell packages on Hackage can be hosted anywhere, yes?

  If a Haskell package is hosted on Hackage, how often is it backed up?

Vasili
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IORef memory leak

2009-06-18 Thread Luke Palmer
On Thu, Jun 18, 2009 at 9:55 PM, Ross Mellgren rmm-hask...@z.odi.ac wrote:

 It looks offhand like you're not being strict enough when you put things
 back in the IORef, and so it's building up thunks of (+1)...

 With two slight mods:

   go 0 = return ()
   go n = do modifyIORef ior (+1)
 go (n-1)

 --

   go 0 = return ()
   go n = do modifyIORef ior (\ x - let x' = x+1 in x `seq` x')
 go (n-1)


Just a slight prettification of that line:

modifyIORef ior ((1+) $!)

Or applied prefix if you prefer.  Prefix ($!) has the nice interpretation as
the HOF that makes its argument into a strict function.

Luke
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IORef memory leak

2009-06-18 Thread Ross Mellgren
D'oh, yeah that is better. You know, I actually had that and had  
expanded it because I was going to seq both the input and the result  
of the (+1), but punted on it and didn't switch back to the more  
compact format.


-Ross

On Jun 19, 2009, at 12:45 AM, Luke Palmer wrote:

On Thu, Jun 18, 2009 at 9:55 PM, Ross Mellgren rmm- 
hask...@z.odi.ac wrote:
It looks offhand like you're not being strict enough when you put  
things back in the IORef, and so it's building up thunks of (+1)...


With two slight mods:


  go 0 = return ()
  go n = do modifyIORef ior (+1)
go (n-1)

--

  go 0 = return ()
  go n = do modifyIORef ior (\ x - let x' = x+1 in x `seq` x')
go (n-1)

Just a slight prettification of that line:

modifyIORef ior ((1+) $!)

Or applied prefix if you prefer.  Prefix ($!) has the nice  
interpretation as the HOF that makes its argument into a strict  
function.


Luke



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] IORef memory leak

2009-06-18 Thread Jim Snow


Luke Palmer wrote:
On Thu, Jun 18, 2009 at 9:55 PM, Ross Mellgren rmm-hask...@z.odi.ac 
mailto:rmm-hask...@z.odi.ac wrote:


It looks offhand like you're not being strict enough when you put
things back in the IORef, and so it's building up thunks of (+1)...

With two slight mods:


  go 0 = return ()
  go n = do modifyIORef ior (+1)
go (n-1)

--

  go 0 = return ()
  go n = do modifyIORef ior (\ x - let x' = x+1 in x `seq` x')
go (n-1)


Just a slight prettification of that line:

modifyIORef ior ((1+) $!)

Or applied prefix if you prefer.  Prefix ($!) has the nice 
interpretation as the HOF that makes its argument into a strict function.


Luke


   do modifyIORef ior (\ x - let x' = x+1 in x `seq` x')

and

   do modifyIORef ior ((1+) $!)

both still leak memory for me.  However,

   do x - readIORef ior
writeIORef ior $! x+1


runs in constant space.  I was able to fix my original program, and now 
it uses a predictable amount of memory.


Thanks!


-jim
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Could someone give me a sample about haskelldb?

2009-06-18 Thread Magicloud Magiclouds
Hi,
  I am learning it following the very few documents on its site. Well,
I failed, with the import modules, I still cannot compile it. The
error is on T.*.

 6 import Database.HaskellDB.HDBC.SQLite3
 7 import Database.HaskellDB
 8 import Database.HaskellDB.DBSpec
 9 import Database.HaskellDB.DBSpec.PPHelpers
10 import Database.HaskellDB.Query

48 q = do
49   t - table $ Table notes []
50   restrict ( t!T.done .. False )
51   r - project ( T.subject  t!T.subject )
52   order [ desc r T.priority
53 , desc r T.dt ]
54   return r
-- 
竹密岂妨流水过
山高哪阻野云飞
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe