Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StatefulGen instance for Gen #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lehins
Copy link
Contributor

@lehins lehins commented Jul 25, 2021

This is a preliminary PR to see if this functionality is even desired. In case that maintainers will see this as useful addition I can add some haddock and examples. Also QC was just the very first thing that came to mind, it can be renamed to something totally different.

This small change allows to use all of the stateful interface from new version of >= random-1.2.0 directly from within the Gen monad. For example:

  • Generate anything with instance Uniform
λ> import System.Random.Stateful (uniformM, uniformRM, uniformShortByteString, uniformListM)
λ> sample (uniformM QC :: Gen Int)
-6952550179692266586
-118868272850968943
...
  • Generate anything with instance UniformRange
λ> import System.Random.Stateful (uniformRM)
λ> sample (uniformRM (0, 10) QC :: Gen Double)
8.829122155155709
7.057056838467508
9.1976303653651
...
  • Use other custom functions that generate random data:
λ> import System.Random.Stateful (uniformListM)
λ> sample (uniformListM 5 QC :: Gen [Int])
[-332073426443689,2129047816008679820,-7574432746071061604,-7820282781062474525,2921130720166821963]
[-449122374714186863,-6428736678288918214,5352387993016661666,2292716122803412839,-7086338435263495393]
[-1122107547762043612,9027060838827498759,6642931938644004524,5993303351323562059,-274895484945480833]
....
  • Efficient generation of binary blobs:
λ> import System.Random.Stateful (uniformShortByteString)
λ> sample (uniformShortByteString 10 QC :: Gen ShortByteString)
"\175~\v\174\141[/\\U{"
"\STXV\NAK\FS\SOH\165^\141\163\&4"
"\245\182\183\158\152.E\232\162:"
....
λ> sample (uniformByteStringM 10 QC :: Gen ByteString)
"+\185\145\STX\202\239\ETB0\190W"
"\STX\192})\241Cr\192\194\EOT"
...

Of course, instead of exporting QC we could just create specialized functions, i.e. uniform :: Uniform a => Gen a etc. but that is less useful IMHO, because it would prevent usage of other general functions that people could write for StatefulGen g m

@MaximilianAlgehed
Copy link
Collaborator

What's the rationale for having StatefulGen QC Gen and not StatefulGen QCGen Gen?

@MaximilianAlgehed
Copy link
Collaborator

What's the rationale for having StatefulGen QC Gen and not StatefulGen QCGen Gen?

Never mind, looking closer at the StatefulGen interface I get it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants