How to group typeclass in Haskell

Viewed 52

The intention is to make the type class constraint code cleaner.

type CanThrowDice = (Monad m, MonadIO m, Random a)

throwDice :: CanThrowDice m a => (a, a) -> m a
throwDice (r1, r2) = ...

instead of writing:

throwDice ::  (Monad m, MonadIO m, Random a) => (a, a) -> m a
throwDice (r1, r2) = ...

I remember seeing this usage somewhere but can't recall. The above code warns to me add some extra pragma such as ConstraintKinds which I can't make it work.

1 Answers

The only thing that is missing are the type parameters m and a and the ConstraintKinds language pragma [ghc-doc]:

{-# LANGUAGE ConstraintKinds #-}

--                ↓ ↓ type parameters
type CanThrowDice m a = (Monad m, MonadIO m, Random a)

throwDice :: CanThrowDice m a => (a, a) -> m a
throwDice (r1, r2) = …
Related