« Previous | Next » 

Revision 8492daa3

ID8492daa3dcb54958d83dd4cac62797905cc999be
Parent ce93b4a0
Child 7022db83

Added by Iustin Pop over 11 years ago

Introduce a helper for simple Arbitrary instances

After getting really annoyed at yet another "<*> arbitrary", I thought
that we should be able to automate this, at least for types which are
simple enough and have already all the "prerequisites".

Hence the new genArbitrary function and its helpers, which can:

- build an arbitrary for Bounded types, using the regular "elements
[minBound..maxBound]" (20 manually defined cases right now)
- build an arbitrary instance for single-constructor data types,
e.g. "data A = A x y z", using a simple "arbitrary = pure A <*>
arbitrary <*> arbitrary <*> arbitrary"
- build an arbitrary instance for multi-constructor data types, using
"arbitrary = oneof [<arbitrary for each individual construct, per
the previous>]"

Both normal and record-based constructors are supported.

It can also build arbitrary instances for new types and type synonyms,
although for these last two I'm not so confident on the soundness of
the instances.

Note that this helper won't work for types which are not well behaved;
for example, Node has the name as String not as FQDN, so our manually
written arbitrary instance has just a few overrides as getFQDN instead
of arbitrary, so we can't automate this particular type yet; this only
means we get another push to use proper types, instead of primitive
ones, for fields which have any kinds of restrictions ("what's good
for arbitrary is good for regular code" too).

Signed-off-by: Iustin Pop <>
Reviewed-by: René Nussbaumer <>

Files

  • added
  • modified
  • copied
  • renamed
  • deleted

View differences