I'm writing some parser code, which involves a parameterized type:
data Power i = Power1 { charges :: i } | Power2 { charges :: i }
Where I know the type i will always be an Int (but is parameterized because I need it for another type class).
Now I want to make my Power type derive from Show (in a specific formatting, so I need to override it myself). As I know that i will only ever be Int, I tried:
instance Show (Power Int) where
show (Power1 0) = ""
show (Power1 i) = "some formatting involving i\n"
show (Power2 0) = ""
show (Power2 i) = "some other formatting involving i\n"
However, this won't compile, with the message
• Illegal instance declaration for ‘Show (Power Int)’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
• In the instance declaration for ‘Show (Power Int)’
|
22 | instance Show (Power Int) where
Why isn't this possible?
Why is this so different to the compiler than?
instance (Show i) => Show (Power i) where