HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

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.

  • 3 types
  • 4 classes
  • 1 value

How to use this library

0 declarations

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

3 declarations

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

Instances7Functor, Eq, Read, Show, Generic, Arbitrary, …
datadata ADTArbitrarySingleton a
#

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

1 declaration

Generic type classes

4 declarations
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
valuegenericArbitrary :: (Generic a, GArbitrary (Rep a)) => Gen a
#

Create a arbitrary generator for a specified a type in a naive way. Please be careful when using this function, particularly for recursive types.