Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:51:34+00:00 2026-05-29T08:51:34+00:00

In Haskell, Is there a standard library/package for generating Random / Arbitrary enums? I

  • 0

In Haskell, Is there a “standard” library/package for generating Random/Arbitrary enums?

I wrote the following code, but I can’t believe I’m the first person to have this need or solve it (and I’m not certain my solution is totally correct).
Also, I hope that an existing solution has other nice functions alongside it.

Here’s a pair of functions to choose a random value from an Enum type:

enumRandomR :: (RandomGen g, Enum e) => (e, e) -> g -> (e, g)
enumRandomR  (lo,hi) gen = 
    let (int, gen') = randomR (fromEnum lo, fromEnum hi) gen in (toEnum int, gen')

enumRandom  :: (RandomGen g, Enum e) => g -> (e, g)
enumRandom gen = 
    let (int, gen') = random gen in (toEnum int, gen')

and here are instances for System.Random.Random and Test.QuickCheck.Arbitrary

{-# LANGUAGE FlexibleInstances, UndecidableInstances, OverlappingInstances #-}

instance (Enum a, Bounded a) => Random a where
   random = enumRandom
   randomR = enumRandomR

instance (Enum a, Bounded a) => Arbitrary a where
  arbitrary = choose (minBound, maxBound)

Here is an example Bounded, Enum type

data Dir = N | E | S | W
   deriving (Show, Enum, Bounded)

and here is a test of Random/Arbitrary methods

> import Test.QuickCheck
> sample (arbitrary:: Gen Dir)
N
E
N
S
N
E
W
N
N
W
W

I’m not delighted that my solution relies on these extensions:

{-# LANGUAGE FlexibleInstances, UndecidableInstances, OverlappingInstances #-}"

because:

- Constraint is no smaller than the instance head
  in the constraint: Enum a
(Use -XUndecidableInstances to permit this)

,

- Overlapping instances for Random Int
  arising from a use of `randomR'
Matching instances:
  instance Random Int -- Defined in System.Random
  instance (Enum a, Bounded a) => Random a

, and

- Illegal instance declaration for `Random a'
  (All instance types must be of the form (T a1 ... an)
   where a1 ... an are *distinct type variables*,
   and each type variable appears at most once in the instance head.
   Use -XFlexibleInstances if you want to disable this.)

Is there a better way? Does my solution fail for some (more “exotic”) Bounded Enum types than my simple example?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:51:34+00:00Added an answer on May 29, 2026 at 8:51 am

    The standard workaround in situations like this is to create a newtype wrapper and provide instances for that instead.

    {-# LANGUAGE GeneralizedNewtypeDeriving #-}  -- avoid some boilerplate
    
    newtype Enum' a = Enum' a
      deriving (Bounded, Enum, Show)
    
    instance (Enum a, Bounded a) => Random (Enum' a) where
      random = enumRandom
      randomR = enumRandomR
    
    instance (Enum a, Bounded a) => Arbitrary (Enum' a) where
      arbitrary = choose (minBound, maxBound)
    

    Of course, that approach requires some extra wrapping and unwrapping when using the new type, but for use with QuickCheck, that shouldn’t be too bad, as you typically only need to pattern match once per property:

    prop_foo (Enum' x) = ... -- use x as before here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Googled a little bit, but still not sure whether there is some standard library
Is there a valid way to do the following in Haskell: case n of
Why I can't construct large tuples in Haskell? Why there's a tuple size limit?
The Haskell prelude and Standard Library define a number of useful type classes. Is
Is there a standard function to sum all values in a Haskell map. My
I needed a String tokenizer in Haskell but there is apparently nothing already defined
Haskell's type safety is second to none only to dependently-typed languages. But there is
Is there some Haskell function that can take a list, say of doubles, like
Is there any way to view the reduction steps in haskell, i.e trace the
There is a lot of hype around Haskell, however, it is hard to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.