1

I would like to know if there is a way to make GHC automatically derive the instance of integral from a data type of the form :

data = A | B | C | D

so that fromIntegral A = 0, fromIntegral B = 1, etc. ?

Using

data MyType = A | B | C | D deriving (Show, Eq, Ord, Enum, Real, Integral)

produce the error

Can't make a derived instance of ‘Integral MyType’: ‘Integral’ is not a derivable class

In the data declaration for ‘MyType’

(even with the extension GeneralizedNewtypeDeriving)

Jeremy Cochoy
  • 2,480
  • 2
  • 24
  • 39
  • Take a look [here](http://stackoverflow.com/questions/14755525/how-to-write-a-derivable-class) – Eugene Sh. Dec 29 '15 at 18:07
  • Yes, I had. It says that there is no general solution for deriving from any type, and that in the case of a newType you can use the GeneralizedNewtypeDeriving extension. But it doesn't says that there isn't an extension in GHC allowing automatic derivation for types of the form A|B|C. – Jeremy Cochoy Dec 29 '15 at 18:27
  • What is wrong with the newtype wrapper approach? – user2407038 Dec 29 '15 at 19:13
  • 1
    could `fromEnum :: Enum a => a -> Int` serve as a solution for your Problem – epsilonhalbe Dec 29 '15 at 20:00

1 Answers1

6

If all you want is a function such that f A == 0, f B == 1, etc., you can use fromEnum :: Enum a => a -> Int.

The Integral type class is for types you can actually add, subtract, divide... In your case, there isn't a very sensible implementation of these things. If you wanted to implement them as arithmetic modulo 1 + fromEnum maxBound... well, I don't recommend doing that, if your Enum represents anything but those numbers, and if they do, the modular-arithmetic package already implements what you need.

Lynn
  • 10,425
  • 43
  • 75