HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulequickcheck-arbitrary-adt-0.3.1.0Haskell2010

Test.QuickCheck.Arbitrary.ADT

Type classes to assist random generation of values for various types of abstract data types.

Contents
  1. How to use this library
  2. Data types
  3. Type classes
  4. Generic type classes
Synopsis

How to use this library

How to use ToADTArbitrary with Generic.

{-# LANGUAGE DeriveGeneric #-}

import Data.Proxy
import GHC.Generics
import Test.QuickCheck
import Test.QuickCheck.Arbitrary.ADT

-- Sum Type, multiple constructors with parameters
data Fruit
  = Apple Int
  | Orange String Int
  | PassionFruit Int String Int
  deriving (Generic, Show)

-- Product Type, single constructor
data Person =
  Person
    { name :: String
    , age  :: Int
    } deriving (Generic, Show)

Any type that implements ToADTArbitrary must also implement Arbitrary. These examples all require that the data type is an instance of Generic.

instance Arbitrary Fruit where
  arbitrary = genericArbitrary

instance ToADTArbitrary Fruit

instance Arbitrary Person where
  arbitrary = genericArbitrary

instance ToADTArbitrary Person

Now we can use toADTArbitrarySingleton to produce an arbitrary value of one random constructor along with some metadata. toADTArbitrary will produce an arbitrary value for each constructor and return it along with a String of the constructor name.

λ> generate (toADTArbitrarySingleton (Proxy :: Proxy Fruit))
ADTArbitrarySingleton {
  adtasModuleName = "Ghci1"
, adtasTypeName = "Fruit"
, adtasCAP = ConstructorArbitraryPair {
    capConstructor = "Apple", capArbitrary = Apple 30}}

λ> generate (toADTArbitrary (Proxy :: Proxy Fruit))
ADTArbitrary {
  adtModuleName = "Ghci1"
, adtTypeName = "Fruit"
, adtCAPs = [
    ConstructorArbitraryPair {
        capConstructor = "Apple"
      , capArbitrary = Apple 17}
  , ConstructorArbitraryPair {
        capConstructor = "Orange"
      , capArbitrary = Orange "abcdef" 18}
  , ConstructorArbitraryPair {
        capConstructor = "PassionFruit"
      , capArbitrary = PassionFruit 16 "datadata" 6}]}

λ> generate (toADTArbitrarySingleton (Proxy :: Proxy Person))
ADTArbitrarySingleton {
  adtasModuleName = "Ghci1"
, adtasTypeName = "Person"
, adtasCAP = ConstructorArbitraryPair {capConstructor = "Person", capArbitrary = Person {name = "John Doe", age = 30}}}

λ> generate (toADTArbitrary (Proxy :: Proxy Person))
ADTArbitrary {
  adtModuleName = "Ghci1"
, adtTypeName = "Person"
, adtCAPs = [ConstructorArbitraryPair {capConstructor = "Person", capArbitrary = Person {name = "Jane Doe", age = 15}}]}

Data types

ConstructorArbitraryPair holds the construct name as a string and an arbitrary instance of that constructor.

Instances7Functor, Eq, Read, Show, Generic, Arbitrary, …

ADTArbitrarySingleton holds the type name and one ConstructorArbitraryPair.

Instances7Functor, Eq, Read, Show, Generic, Arbitrary, …
datadata ADTArbitrary a

ADTArbitrary holds the type name and a ConstructorArbitraryPair for each constructor.

Instances7Functor, Eq, Read, Show, Generic, Arbitrary, …

Type classes

Generic type classes

classclass GToADTArbitrarySingleton (rep :: Type -> Type) where

GToADTArbitrarySingleton creates an arbitrary value and returns the name of the constructor that was used to create it and the type name.

Instances7GToADTArbitrarySingleton, …
classclass GToADTArbitrary (rep :: Type -> Type) where

GToADTArbitrary is a typeclass for generalizing the creation of a list of arbitrary values for each constructor of a type. It also returns the name of the constructor and the type name for reference and file creation.

Methods

Instances7GToADTArbitrary, …
classclass GArbitrary (rep :: Type -> Type) where

GArbitrary is a typeclass for generalizing the creation of single arbitrary product and sum types. It creates an arbitrary generating function of this style: TypeName <$> arbitrary <*> arbitrary.

Methods

Instances5GArbitrary