Given the following code
{-# LANGUAGE StandaloneDeriving #-}
type Date = Int
data Decision a = Decision a deriving (Show)
data InProgress a = InProgress {progress :: a, decision :: Decision a } deriving (Show)
data Finished a = Finished {finished :: a, initial :: a, timestamp :: Date } deriving (Show)
data LineItem a = LineItem {article :: a String, amount :: a Float, units :: a Int } deriving (Show)
I get the following error
source_file.hs:11:96:
No instance for (Show (a Float))
arising from the second field of ‘LineItem’ (type ‘a Float’)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Show (LineItem a))
I tried some variations like this below - but I cant get it run.
deriving instance Show a => Show (InProgress Float)
How can I fix this? (and some explanation would be really appreciated)
Edit
Okay, I have the solution
deriving instance Show (LineItem InProgress)
will do what I want. Still I dont get why its not
deriving instance Show a => Show (LineItem (InProgress a))
The
aisLineItem ais not a type, i.e. it has not kind*. Instead, it has kind* -> *.This means that
ais expected to be replaced with a type constructor. For instance,LineItem Maybeis well-kinded, butLineItem (Maybe Int)is not.The most general
Showinstance is then something likebut there the context involves more complex types than the head. This will surely require to turn on
UndecidableInstancesand possibly a few other extensions.Note that, since this instance does not "simplify" the head into smaller constraints, there's the chance that by adding further "bad" instances we can make the compiler to loop forever. In practice, this rarely happens, and I wouldn't worry too much about it.
If you know
ain subsequent uses has only a finite number of cases, you can list them one by one, providing an instance for each one, as you did in your edit.