79

I was just doing some Haskell development and I recompiled some old code on a new version of GHC:

The Glorious Glasgow Haskell Compilation System, version 7.2.1

And when I did I received the following error:

Warning: -XDatatypeContexts is deprecated: It was widely considered a misfeature, and has been removed from the Haskell language.

That appears when you have code in the following format:

data Ord a => MyType a
    = ConstructorOne a
    | ConstructorTwo a a

My question is: Why was this feature deprecated in the first place and what am I supposed to do instead to achieve the same or similar functionality?

3
  • 14
    Just remove the context and your code will still work.
    – augustss
    Sep 16, 2011 at 1:03
  • If you're getting a related error when trying cabal install <package>, then try cabal install --ghc-option '-XDataTypeContexts' <package>.
    – ntc2
    May 22, 2012 at 21:46
  • @ntc2 As an answer suggested, maybe the command should contain -XDatatypeContexts without a lowercase t. Nov 2, 2018 at 21:39

2 Answers 2

110

It's deprecated because it was a misfeature, and didn't actually have any useful functionality! All it did was force a bunch of extra constraints in other locations. In particular, when pattern matching on such a type, you'd be forced to add a constraint, rather than (as one might initially hope) get access to a context, based on the knowledge that one must have been available to construct the value in the first place.

The "replacement", which actually works the other way and tracks the known contexts for you, is to use GADT-style declarations instead:

data MyType a where
    ConstructorOne :: Ord a => a -> MyType a
    ConstructorTwo :: Ord a => a -> a -> MyType a

GADTs in general are more flexible in many other ways as well, but for this particular case what happens is that creating a value needs the Ord constraint, which is then carried along with the value, and pattern matching on the constructor pulls it back out. So you don't even need the context on the functions using it, because you know that by virtue of expecting something of type MyType a, you'll get an Ord a constraint with it.

4
  • 1
    The last sentence seems a bit unclear-what you pull from the GADT constructor is not the constraint but rather the instance(s) satisfying the constraint.
    – dfeuer
    Jun 26, 2015 at 12:59
  • 1
    But if there are too many constructors doesn't it become too verbose to repeat Ord a for each one?
    – fakedrake
    Oct 5, 2016 at 15:00
  • 3
    @fakedrake I mean this shouldn't come up often anyway. You almost always don't want to restrict your types like that. One example for why is that it makes it impossible to make your type a Functor, Foldable, Traversable, Applicative, Monad, Alternative, MonadPlus, Arrow and so on.
    – semicolon
    Feb 24, 2017 at 21:14
  • GADTs are very nice because I can have some more restricted constructors in certain cases, and I have found that to be useful in those cases. Thanks for the answer. Nov 3, 2020 at 17:02
13

In general, you still need to add the Ord a constraint to any function which uses your MyType type, and as such isn't as useful as it may seem. For more information about why they were removed, see http://hackage.haskell.org/trac/haskell-prime/wiki/NoDatatypeContexts

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.