Eric Dedieu | 1 Dec 2009 01:43
Picon
Favicon

Re: Help mixing pure and IO code

> > Still more importantly to me, I understand that anyhow  if I intend
> > to use IO or random numbers, I must design my strategy from the
> > beginning as "encapsulated in a monad". Something like:
> >
> > class (Monad m) => Strategy m a where ...
> >   
> That's not true at all, you can always pass this data to your strategy
> entry points and let haskell get it lazily, though it is not as
> intuitive as other aproaches,

That seems exactly what I had tried to do (and
failed) in my original code.

The Fixed type was to provide the list of moves:

> data Fixed = Fixed [Move] deriving Show

and instanciate a strategy that ignores the game to
make decisions, just return the provided moves, then zeroes if ever
exhausted:

> instance Strategy Fixed where
>     proposeNext game s = case s of
>                            Fixed [] -> (0, Fixed [])
>                            Fixed (x:xs) -> (x, Fixed xs)

but when using an IO Fixed, the questions were not repeated lazily as
needed, as if the list of moves was entirely evaluated; so this failed:

> askUntil :: String -> IO Fixed
(Continue reading)

Tom Tobin | 1 Dec 2009 03:56
Gravatar

Re: Re: ANNOUNCE: haskell-mode 2.7.0

On Nov 30, 2009, at 5:06 PM, Ivan Lazar Miljenovic wrote:
> The "default" Hlint keybinding is C-c l:

Doesn't that violate the emacs proscription against taking "C-c (letter)" bindings, as they are intended
to be reserved for the user?

From the GNU Emacs manual:

"Don't define C-c letter as a key in Lisp programs. Sequences consisting of C-c and a letter (either upper or
lower case) are reserved for users; they are the *only* sequences reserved for users, so do not block them."
leledumbo | 1 Dec 2009 04:07
Picon
Favicon
Gravatar

Re: Function composition questions from a newbie


> This doesnt. But why?

Back to the definition of function composition: f . g is possible if g
returns a value that's compatible with f's argument. Now, let's check the
type of square and add3:

square :: Num a => a -> a
add3 :: Num a => a -> a -> a -> a

(square . add3 1 2) is actually seen by the compiler / interpreter as
(square . (add3 1 2)) due to precedence of . operator.

So, what's the type of add3 1 2?

add3 :: Num a => a -> a

Hmm... seems compatible with square.

what about (square . add3) 1 2?

It doesn't work since add3, when curried (arguments of square "blended" with
add3's) with 1 argument becomes:

add3 :: Num a => a -> a -> a

which is a function that accepts 2 arguments and it's not compatible with
square's a.
--

-- 
View this message in context: http://old.nabble.com/Function-composition-questions-from-a-newbie-tp26570201p26585840.html
(Continue reading)

Ivan Lazar Miljenovic | 1 Dec 2009 04:07
Picon
Gravatar

Re: Re: ANNOUNCE: haskell-mode 2.7.0

Tom Tobin <korpios <at> korpios.com> writes:

> On Nov 30, 2009, at 5:06 PM, Ivan Lazar Miljenovic wrote:
>> The "default" Hlint keybinding is C-c l:
>
> Doesn't that violate the emacs proscription against taking "C-c
> (letter)" bindings, as they are intended to be reserved for the user?

Well, the Emacs integration that comes with Hlint suggests that users
use that key binding and provides the relevant hook, etc. to use it.

--

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic <at> gmail.com
IvanMiljenovic.wordpress.com
Matt Arsenault | 1 Dec 2009 04:12
Picon
Favicon
Gravatar

Re: Re: ANNOUNCE: Clutterhs 0.1

On Mon, 2009-11-30 at 09:22 +0100, Gour wrote:

> Do you have some public repo for the project's code?

I thought I mentioned this somewhere, but I've been using this git repo:

http://jayne.hortont.com/git/cgit.cgi/clutterhs.git/
Hector Guilarte | 1 Dec 2009 04:36
Picon
Gravatar

Re: Help mixing pure and IO code

One time I needed to do use a random number in some places of a completly pure program so I made a infinite list of random numbers and passed it around all the time in the functions as they where called, using the head of the list I passed to the function whenever I needed a random number and returning a tuple which it's second element was the tail of the random numbers list e.g.

f:: [int] -> (a,[Int])
f randomList =
    let usedRandomNumber = g $ head randomList
    in (usedRandomNumber,(tail randomList))

or even something like:
f:: [int] -> (a,[Int])
f randomList =
    let (usedRandomNumber,newRandomList) = g randomList
    in (usedRandomNumber,newRandomList)

where g:: [int] -> (a,[Int])


The actuall code for doing this is: (Warning, it uses unsafePerformIO, just for the seed of the random Generator, I really don't think it would do any harm)

rand :: (RandomGen g, Random a) => (a,a) -> g -> [a]
rand range gen = as
     where  (a,b) = split gen      -- create two separate generators
            as = randomRs range a  -- one infinite list of randoms

seed :: Int
seed = fromInteger (unsafePerformIO getCPUTime)

mygen  = mkStdGen seed

infinito:: (Num t,Random t) => [t]
infinito = [ x | x <- rand (1,1000000) mygen]


infinito is the function that you need to call in your code that will give you the infinite list of random numbers which will be evaluated lazyly...

Hope you can use this...

About the prompt thing, that you'll have to wait for another answer or use what they have already told you,


Greetings,

Hector Guilarte




On Mon, Nov 30, 2009 at 8:13 PM, Eric Dedieu <papa.eric <at> free.fr> wrote:
> > Still more importantly to me, I understand that anyhow  if I intend
> > to use IO or random numbers, I must design my strategy from the
> > beginning as "encapsulated in a monad". Something like:
> >
> > class (Monad m) => Strategy m a where ...
> >
> That's not true at all, you can always pass this data to your strategy
> entry points and let haskell get it lazily, though it is not as
> intuitive as other aproaches,

That seems exactly what I had tried to do (and
failed) in my original code.

The Fixed type was to provide the list of moves:

> data Fixed = Fixed [Move] deriving Show

and instanciate a strategy that ignores the game to
make decisions, just return the provided moves, then zeroes if ever
exhausted:

> instance Strategy Fixed where
>     proposeNext game s = case s of
>                            Fixed [] -> (0, Fixed [])
>                            Fixed (x:xs) -> (x, Fixed xs)

but when using an IO Fixed, the questions were not repeated lazily as
needed, as if the list of moves was entirely evaluated; so this failed:

> askUntil :: String -> IO Fixed
> askUntil name = liftM Fixed (sequence $ repeat askio)
>     where askio = putStr (name ++ ", pick a number: ") >> readLn

I thought that sequencing [IO Move] to IO [Move] was what breaked
lazyness, so I tried other ways to turn an [IO Move] into a IO
Strategy, and failed.

If I did not interpret correctly why this was not lazy, or if it is
indeed possible to do otherwise can you please show me how?

Thanks,
Eric
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Derek Elkins | 1 Dec 2009 04:47
Picon

Re: Great Programs to Read?

On Mon, Nov 30, 2009 at 6:22 AM, Michael Lesniak <mlesniak <at> uni-kassel.de> wrote:
> Hello,
>
> In terms of
>
>  "to become a great programmer, you need to read great programs"[1]
>
> what are "great" programs written in Haskell (for your personal
> definition of great), which source code is freely available on hackage
> or somewhere else on the net?
>
> I'm personally also interested in your definitions of great; for me, a
> great programs is defined by one of
>
> * good and well-written documentation
>  (literate Haskell helps a lot)
> * novel ideas to use functional programming
> * elegance
> * showing how functional programming can ease tasks that
>  are difficult to achieve in an imperative style
>
> Maybe we should create a Page on haskell.org (which I would do if I
> had write-access) mirroring the pages [2,3]?
>
> Kind regards,
> Michael
>
> [1] http://c2.com/cgi/wiki/Wiki?ReadGreatPrograms
> [2] http://c2.com/cgi/wiki/Wiki?GreatProgramsToRead
> [3] http://c2.com/cgi/wiki/Wiki?ProgramsToRead

The functional pearls are pretty much specifically designed to do all
the things you mention.  See
http://haskell.org/haskellwiki/Research_papers/Functional_pearls
Luke Palmer | 1 Dec 2009 04:53
Picon
Gravatar

Re: Help mixing pure and IO code

On Mon, Nov 30, 2009 at 8:36 PM, Hector Guilarte <hectorg87 <at> gmail.com> wrote:
> One time I needed to do use a random number in some places of a completly
> pure program so I made a infinite list of random numbers and passed it
> around all the time in the functions as they where called, using the head of
> the list I passed to the function whenever I needed a random number and
> returning a tuple which it's second element was the tail of the random
> numbers list e.g.
>
> f:: [int] -> (a,[Int])
> f randomList =
>     let usedRandomNumber = g $ head randomList
>     in (usedRandomNumber,(tail randomList))
>
> or even something like:
> f:: [int] -> (a,[Int])
> f randomList =
>     let (usedRandomNumber,newRandomList) = g randomList
>     in (usedRandomNumber,newRandomList)

This pattern can be encapsulated in a monad:

newtype RandM a = RandM { unRandM :: [Int] -> (a,[Int]) }

instance Monad RandM where
    return x = RandM $ \randomList -> (x, randomList)
    m >>= g = RandM $ \randomList ->
      let (x, newRandomList) = unRandM m randomList
      in unRandM (g x) newRandomList

getRandom :: RandM Int
getRandom = RandM $ \(randomNumber:randomList) -> (randomNumber, randomList)

See the similarity?

Of course, there is no need to implement this yourself.  It is already
implemented as State [Int].  And as long as you are doing that, you
might as well use Rand from the MonadRandom package.  In fact, I have
argued that you should use MonadRandom instead of the lower-level
System.Random whenever possible:
http://lukepalmer.wordpress.com/2009/01/17/use-monadrandom/

Luke
John Millikin | 1 Dec 2009 05:10
Picon
Gravatar

Re: Are there standard idioms for lazy, pure error handling?

On Mon, Nov 30, 2009 at 03:02, Duncan Coutts
<duncan.coutts <at> googlemail.com> wrote:
> On Mon, 2009-11-30 at 06:08 +0000, Malcolm Wallace wrote:
>> However, if you really want to terminate the stream at
>> the first error, and to reflect this in the type, then I guess you can
>> define your own list type:
>>
>> data ListThenError e a = Cons a (ListThenError e a)
>>                         | Error e
>>
>> Of course this has the disadvantage that then your consumer must
>> change to use this type too.
>
> I've been using this list type quite a lot recently. It's in the 'tar'
> package for example. It comes with variants of the standard functions
> foldl, foldr, unfoldr that take into account the error possibility.
>
> At some point we should probably make a package to standardise and
> document this lazy error handling idiom.

Wow, this is perfect! I've extracted that type out into the
"failable-list" library[1], with a few added instances for common
classes (Monad, Applicative, Traversable, etc).

[1] http://hackage.haskell.org/package/failable-list
Paulo Tanimoto | 1 Dec 2009 05:26
Picon

Optimizing Parsec 3 -- was: Wiki software?

Hello!

On Mon, Nov 23, 2009 at 12:29 PM, Antoine Latter <aslatter <at> gmail.com> wrote:
> On Sun, Nov 22, 2009 at 11:56 AM, Antoine Latter <aslatter <at> gmail.com> wrote:
>>
>> Running 'pandoc --strict' over the Markdown readme.text takes:
>>
>> ~0.09s with pandoc built against parsec-2
>> ~0.19s with pandoc built against parsec-3
>>
>> on my machine.
>>
>> I have a branch of parsec-3 which seems to brings us back to parsec-2
>> numbers, but also fails the rst-reader test-case in the pandoc testing
>> suite:
>>
>> http://community.haskell.org/~aslatter/code/parsec/cps
>
> In reply to my own post, the branch of parsec posted now passes all of
> the pandoc test cases.
>
> If there are any other consumers of the parsec library that have tests
> I can run let me know.
>
> The 'many' combinator is one of those things that can look right, be
> wrong, yet work for almost everything.
>

I finally had some time to test it.  After running it multiple times
(of course, it would be nice to use criterion here), I'm getting
numbers in this neighborhood:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.12.0.20091121

$ time pandoc --strict README > /dev/null

parsec 2:
---
real	0m0.140s
user	0m0.130s
sys	0m0.010s

parsec 3 - antoine's:
---
real	0m0.151s
user	0m0.150s
sys	0m0.010s

parsec 3 - hackage:
---
real	0m0.243s
user	0m0.240s
sys	0m0.010s

Nice work, Antoine!

Paulo

Gmane