concept instance in category haskell

This is an excerpt from Manning's book Haskell in Depth.
If values of some type can be compared for equality, enumerated and have bounds (the type has instances of
Eq,EnumandBounded), then we can enumerate them in cycle. Let’s express this abstract concept in the form of aCyclicEnumtype class:class (Eq a, Enum a, Bounded a) => CyclicEnum a where cpred :: a -> a cpred d | d == minBound = maxBound | otherwise = pred d csucc :: a -> a csucc d | d == maxBound = minBound | otherwise = succ dThis class defines two methods with default implementations. We can use them instead of writing our implementation, or we can redefine them in instances. The former is usually done when it’s possible to define them more efficiently exploiting an internal representation of a type.
The default definitions in the
CyclicEnumtype class (and empty minimal complete definition) allow us to define an instance in one line (note the absence ofwhere):
To generate random
Directionor randomTurnwe need to provide an instance to theRandomtype class. Minimal complete definition requires two methods:randomandrandomR. The good news is that they can be implemented almost trivially given theEnumandBoundedinstances: an idea is to go to the correspondingInt, generate randomInt, and then get back. An instance forDirectionis presented in the following listing:instance Random Direction where randomR (lo, hi) g = (toEnum i, g') where (i, g') = randomR (fromEnum lo, fromEnum hi) g random = randomR (minBound, maxBound)An instance for
Turnis the same.
Figure 12.1. Required instances
![]()

This is an excerpt from Manning's book Get Programming with Haskell.
data FiveSidedDie = Side1 | Side2 | Side3 | Side4 | Side5 deriving (Enum, Eq, Show) class (Eq a, Enum a) => Die a where roll :: Int -> a instance Die FiveSidedDie where roll n = toEnum (n 'mod' 5)
The other important thing to notice is that the type parameter of your Maybe (or Either) type is constrained by the type class FromJSON. Making a type an instance of FromJSON enables you to convert raw JSON into a Maybe instance of your type. You’ll explore ways of making data an instance of FromJSON in the next section.