[Haskell-cafe] Random pattern generation with list comprehensions

Michael Jones mike at proclivis.com
Sun Dec 14 18:54:12 UTC 2014


So, with ParallelListComp,  because it is basically a zip, the evaluation will take one item from each generator, where generator is the v <- generator to the right of the “|”.

The implication being that if each generator produces random data, it will not evaluate each generator one by one in a systematic order, like multi digit counting. Basically it forces evaluation of each generator for each round, which would be like multi digit number generation where each digit changes with its own algorithm.

If that is what it does, then this probably would work. I could create each generator within a State Monad, and use ParallelListComp to combine them, provided I can put a monadic operation in the comprehension.

This page https://www.haskell.org/haskellwiki/List_comprehension

seems to indicate it can't be directly done in a comprehension, which is limited to lists, and provides a do syntax alternative. I think that implies I can’t use ParallelListComp, and I would have to follow the examples on that page and zip things myself to force evaluation of each generator one by one.

The alternative might be to make a State Monad where the State is a tuple with each item holding the state for each generator. Given that the number of items is fixed by hardware, I would not have to manage arbitrary tuple sizes. Each evaluation round of State would produce a tuple, and I would have to use a looping construct to generate values.

QuickCheck is interesting, but I would prefer to draw on a simple language construct rather than take on a whole framework, unless there is no basic simple approach.

Mike

On Dec 12, 2014, at 8:16 PM, Michael Sloan <mgsloan at gmail.com> wrote:

> Hello!
> 
> It sounds like you're looking for the ParallelListComp language extension[1]
> 
> Another option might be to leverage the Arbitrary typeclass in
> QuickCheck[2].  It has instances for tuples, so if each component of
> the tuple has an Arbitrary instance, you can generate the whole tuple
> with one invocation of "arbitrary".  The CoArbitrary typeclass is also
> rather handy and clever, as it allows you to create random functions,
> as long as the arguments are instances of CoArbitrary and the result
> is an instance of Arbitrary.  Your concept of specifying run length is
> very similar to QuickCheck's "size parameter".
> 
> I hope that's helpful!
> 
> -Michael
> 
> [1] https://downloads.haskell.org/~ghc/7.8.3/docs/html/users_guide/syntax-extns.html#parallel-list-comprehensions
> [2] http://hackage.haskell.org/package/QuickCheck




More information about the Haskell-Cafe mailing list