Haskell with classic data types

I have some algebraic data types A, B and C, each implements a class:

class Dog a where dog :: a -> Bool 

If I create a new type of algebraic data:

 data D = A | B | C 

Is there an easy way to implement D to implement Dog without redefining each instance for A, B, and C?

thanks

+4
source share
1 answer

Before answering, I must point out that you might fall into the general misconception of beginners about ADT. Remember that Haskell has two separate namespaces for type and term levels. Therefore, if we write:

 data A = Foo data B = Bar data C = Baz data D = A | B | C 

... then there is no connection between type A and constructor A type D Therefore, I suspect (but not quite sure!) That the question you wanted to ask had the following format for type D , but instead:

 data D = AA | BB | CC 

In this case, the short answer is no. You might wish you could attack deriving Dog or something similar and do it, but this object is not provided by the language. However, there are a few packages for general programming that can help: just check the list of Hackage packages and find the β€œderivative”, and you'll get about ten hits.

+8
source

All Articles