48

I was trying to understand what the FlexibleContexts extension is doing by searching for web pages that would explain it to mere mortals (people who have read LYHFGG, for example, like me) but I did not find any such resource.

Therefore I ask the experts on the topic: Could you please explain what this extension does, why it exists, and give one or two simple examples how and why one should use it?

Furthermore, If I am reading someone else's code which uses this extension, then what should I know about the extension in order to understand the code written using this extension?

1
  • 4
    Here is the relevant section in the ghc user guide. Flexible contexts allows class instance contexts to be something other than C0 a0, C1 a1 ... Cn an, where C is a class and a 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. Jul 6, 2015 at 17:28

4 Answers 4

67

Without FlexibleContexts all typeclass constraints on function definitions must have type variables. For example:

add :: Num a => a -> a -> a 
add = (+)

Where a is the type variable. With FlexibleContexts enabled you can have any type inside a typeclass.

intAdd :: Num Int => Int -> Int -> Int 
intAdd = (+)

This example is pretty contrived but it is the simplest I can think of. FlexibleContexts is usually only used with MultiParamTypeClasses. Here is an example:

class Shower a b where
  myShow :: a -> b

doSomething :: Shower a String => a -> String
doSomething = myShow

Here you can see we say that we only want a Shower a String. Without FlexibleContexts String would have to be a type variable instead of a concrete type.

3
  • 3
    I found the Show instance for the Free monad quite a good example of this extension's uses.
    – AJF
    Jul 6, 2015 at 20:58
  • 1
    So why is this apparently useful feature not enabled by default?
    – Sam
    Dec 30, 2015 at 11:03
  • 1
    It isn't part of the Haskell standard so it isn't enabled by default. It is a GHC specific extension.
    – DiegoNolan
    Dec 30, 2015 at 20:52
9

Commonly it's used with the MultiParamTypeClasses extension, for example when using the mtl library you might write

doSomethingWithState :: MonadState MyState m => m ()
doSomethingWithState = do
    current <- get
    let something1 = computeSomething1 current
        something2 = computeSomething2 current something1
    put something2

And similarly with MonadReader and MonadWriter, along with other similar typeclasses. Without FlexibleContexts you can't use this constraint.

(Note that this answer was based on @DiegoNolan's but rewritten to use an existing library that should make sense to LYAH readers).

7

I've discovered a use for it apart from those mentioned: it results in clearer error messages from GHC. E.g. normally,

Prelude> max (1, 2) 3

<interactive>:1:1: error:
    • Non type-variable argument in the constraint: Num (a, b)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a b.
              (Num (a, b), Num b, Num a, Ord b, Ord a) =>
              (a, b)

And with FlexibleContexts enabled:

Prelude> max (1, 2) 3

<interactive>:1:1: error:
    • No instance for (Num (Integer, Integer))
        arising from a use of ‘it’
    • In the first argument of ‘print’, namely ‘it’
      In a stmt of an interactive GHCi command: print it

Here's a discussion.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.