# Write you some QuickCheck - Shrinking numbers

This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.

In this post I’ll be porting the function from QuickCheck that shrinks numbers.

## Prelude

When reporting back the actual input that caused a function to fail, QuickCheck first cleans the noise by reporting back the counterexample. — This is called shrinking. See how it works here.

All the shrinking functions have the same signature:

So in F# it can look like this:

## Shrinking numbers

QuickCheck shrinks towards smaller numeric values with the following generic function:

Let’s try this one out first:

Here’s the equivalent one in F#:

I’m going to try this one with the same values as the ones used in Haskell:

## Shrinking not only integers, but floats, decimals, and so on

Here is again the `shrinkNumber` number function that was ported from Haskell:

Because of `2`, and `0`, the F# compiler infers that this function works with integers.

Fortunately, there’s a way to make this polymorphic with compile-time generics:

Here’s the end result:

And now the type has become generic:

Let’s try this one out:

# Write you some QuickCheck - Generating random floats

This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.

In this post I’ll be generating random 32-bit floating-point numbers.

In Haskell’s QuickCheck 1.2.0.1 (but also in version 2.8), random 32-bit floating-point numbers are generated as shown below:

Two things are important in the above code:

Here’s the original `fraction` function in Haskell:

It takes 3 integers and returns an `a` which is a fractional (a numeric type that supports the ordinary division operator `/`).

Here are some sample values generated by fraction:

Now, what I want to do is to call the `fraction` function passing random integers, something like this (pseudo-code):

Here’s where `liftM3` comes into play:

• `a1`, `a2`, and `a3`, correspond to the arguments of `fraction`
• `r` corresponds to the return value of `fraction`
• `m a1`, `m a2`, `m a3` will be 3 generators for integers
• `m r` the return value (the random float).

So, in F#, a generator for floats can be written as:

And the `lift3` function in F# can be written in terms of `apply` and `return`:

Finally, here are some sample floats:

# Write you some QuickCheck - Generating random strings

This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.

In this post I’ll be generating random strings from randomly selected characters. I will also describe a way of customizing the whole creation process.

The following generators are required for this:

Given the above, a generator for strings can be written as:

Here are some sample strings:

## Customizations

Generarting upper-case strings

Sample output:

Generating lower-case strings

Sample output:

# Write you some QuickCheck - Generating random lists

This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.

In this post I’ll be generating lists of random length.

The maximum length of the list will depend on the runtime size parameter. See this previous post that explains how size works.

Two generators are required for this:

• a generator that picks numbers with absolute value bounded by the size (that’s Gen’s `choose`)
• a generator that creates lists of a given length (that’s Gen’s `vector`). The length will be the number obtained by the above generator

Gen’s `choose` is already available. Porting Gen’s `vector`, which essentially means porting replicateM

Creating a custom `gen` computation expression

Most, if not all, monadic computations on the `Gen<'a>` type can be written in terms of `Gen.bind` and `Gen.init` (return).

A computation expression just makes this a whole lot easier:

Given the above, a generator for lists of random length can be written as:

Finally, here are some sample lists of type `int` and `char`, but it can be any generator of type `'a` really

# Write you some QuickCheck - Generating random long integers

This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.

Two generators are required for this, which have been ported already on previous posts:

The long integers are going to be generated by the integer returned by the first generator and then multiplied by a 16-bit1 integer’s largest possible value.

Given the above, a long integer generator can be written as:

Finally, here are some sample long integers:

1. To be more precise, I should use a 32-bit integer’s largest possible value, but then the generated numbers become very big and less interesting.