Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for deprecated -XDatatypeContext?

Tags:

haskell

Let's say I want to define a tree like this:

{-# LANGUAGE DatatypeContexts #-}
class Node a where
  getContent :: (Num a) => a

data (Node a) => Tree a = Leaf a
                        | Branch a (Tree a) (Tree a)

-XDatatypeContexts is deprecated now. Is it possible to do something similar without it?

like image 305
Jake Avatar asked Dec 15 '12 02:12

Jake


People also ask

Is deprecated still used?

It can still be used, but eventually it will not work with other things because it is no longer being supported. Best practice is to use the requested alternative to any depreciated type.

Can I still use deprecated API?

We do not recommend that you use deprecated API methods. Although they may continue to be backwards compatible, there may be unexpected or unwanted behavior. Always replace the API methods that are no longer supported.

What are deprecated methods?

Deprecated methods are methods that used to be supported and safe to use, but no longer are safe to use. They can be unsafe to use for a multitude of reasons. The methods may or may not work the way you want.

Can I use a deprecated function?

Deprecated features. These deprecated features can still be used, but should be used with caution because they are expected to be removed entirely sometime in the future. You should work to remove their use from your code.


1 Answers

Are you sure datatype contexts actually did what you think it did? It was deprecated because it was basically useless and widely considered a misfeature, since all it did was force you to add extra constraints without providing any guarantees about types beyond what you'd have had without it.

The replacement, such as it is, that actually does something useful, is GADT syntax. The equivalent of your type would look like this:

data Tree a where
    Leaf :: (Node a) => a -> Tree a
    Branch :: (Node a) => a -> Tree a -> Tree a -> Tree a

In this case, you need the Node constraint when creating a Tree value, but when pattern matching on a Tree value you also get an automatic guarantee that a Node instance exists, making the instance available without even needing it in the type of the function receiving Tree a as an argument.

like image 160
C. A. McCann Avatar answered Sep 27 '22 22:09

C. A. McCann