DatatypeContexts Deprecated in the latest GHC: Why?

I was just developing Haskell, and I recompiled the old code in the new version of GHC:

The Glorious Glasgow Haskell Compilation System, version 7.2.1 

And when I did this, I got the following error:

Warning: -XDatatypeContexts is deprecated: it has been widely recognized as invalid and has been removed from the Haskell language.

This appears when you have code in the following format:

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

My question is: why was this feature irrelevant in the first place and what should I do instead to achieve the same or similar functionality?

+60
deprecated haskell language-extension
Sep 15 '11 at 23:28
source share
3 answers

It is deprecated because it was a mistake, and in fact there was no useful functionality! All that was done was associated with many additional restrictions elsewhere. In particular, when matching patterns for this type, you would have to add a constraint, and not (as you might hope) gain access to the context, based on the knowledge that it was necessary to build the value in the first place.

A โ€œreplacement,โ€ which actually works differently and tracks known contexts for you, is instead of using GADT style ads

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

GADTs are generally more flexible in many other cases, but for this particular case, the creation of a value requires an Ord constraint, which is then carried along with the value and the pattern matching on the constructor pulls it back. This way, you donโ€™t even need a context for functions using it, because you know that waiting for something like MyType a you will get an Ord a constraint with it.

+86
Sep 15 2018-11-11T00:
source share

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

+8
Sep 15 2018-11-11T00:
source share

I also got this error. The ntc2 suggestion worked for me, except that it should be with a little "t" in the "TypeContext", i.e. cabal install --ghc-option '-XDatatypeContexts' <package>

-one
Aug 14 '13 at 9:03 on
source share



All Articles