FlexibleContexts
is often used with type families. For example, when using GHC.Generics
, it's common to see signatures like
foo :: (Generic a, GFoo (Rep a)) => Int -> a -> a
This can be seen as a variation of the MultiParamTypeClasses
usage:
class (Generic a, rep ~ Rep a) => MPGeneric rep a
instance (Generic a, rep ~ Rep a) => MPGeneric rep a
mpFoo :: (MPGeneric rep a, GFoo rep) => Int -> a -> a
As AJFarmar pointed out, FlexibleContexts
is also useful with neither MPTCs nor type families. Here's a simple example:
newtype Ap f a = Ap (f a)
deriving instance Show (f a) => Show (Ap f a)
The alternative approach using Show1
is significantly more awkward.
A more involved example is provided by AJFarmar's comment:
data Free f a = Pure a | Free (f (Free f a))
deriving instance (Show a, Show (f (Free f a))) => Show (Free f a)
This brings in UndecidableInstances
as well, since it's recursive, but it does a good job of explaining just what it needs to be able to show Free f a
. In bleeding-edge GHC Haskell, an alternative would be to use QuantifiedConstraints
:
deriving instance (Show a, forall x. Show x => Show (f x)) => Show (Free f a)
but this is overkill because we only need to show f
applied to Free f a
.
C0 a0, C1 a1 ... Cn an
, whereC
is a class anda
is a type variable mentioned in the instance head. It exists because sometimes very complex constraints are needed to properly express your program logic - this is essentially the motivation behind all typechecker extensions.