concept instance in category haskell

appears as: instces, n instance, instce, An instce, instces, instce, instance
Haskell in Depth

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, Enum and Bounded), then we can enumerate them in cycle. Let’s express this abstract concept in the form of a CyclicEnum type 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 d

This 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 CyclicEnum type class (and empty minimal complete definition) allow us to define an instance in one line (note the absence of where):

To generate random Direction or random Turn we need to provide an instance to the Random type class. Minimal complete definition requires two methods: random and randomR. The good news is that they can be implemented almost trivially given the Enum and Bounded instances: an idea is to go to the corresponding Int, generate random Int, and then get back. An instance for Direction is 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 Turn is the same.

Figure 12.1. Required instances
strategies
Get Programming with Haskell

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.