user268344
user268344

Reputation: 869

How to create a type bounded within a certain range

I would like to create a new integral type which is bounded to a certain range. I have tried:

data PitchClass = PC Int deriving (Ord, Eq, Show)

instance Bounded PitchClass where
  minBound = PC 0
  maxBound = PC 11

However, what I want is something that will fail if something like

PC 12

or

PC (-1)

is attempted.

Is the general approach for a situation in which you wish to place constraints on creating new types one in which the value constructors are not exported from the module, but rather functions which return instances of the type and which perform constraint checks are exported?

Upvotes: 23

Views: 5758

Answers (2)

Daniel Wagner
Daniel Wagner

Reputation: 153332

An alternate solution for cases where the number of total values is this small is to simply enumerate the possible constructors.

data PitchClass = A | Bb | B | C | Db | D | Eb | E | F | Gb | G | Ab
    deriving (Eq, Ord, Bounded, Show, Read)

There are half a dozen different hacks you can try from here to make it more convenient in various ways; for example, you can derive Enum to get toEnum . fromEnum = id (and toEnum (-1) = {- an exception -}), or you can write a custom Integral instance to get 0 = A (and your choice of behavior for -1).

Upvotes: 9

hammar
hammar

Reputation: 139930

Yes, not exporting the data constructor from the module is the way to go.

Instead, you export a function which does the checking as you said. This is often called a smart constructor.

Upvotes: 21

Related Questions