Lists of data types: "could not deduce (a ~ SomeType) from the context (SomeTypeclass a)"
the offending line is stList :: (Show a) => [a]
. You're declaring that stList is a polymorphic list that holds any element which satisfies the show constraint. But stList isn't a polymorphic list! It's a list of SampleType
s. So remove the signature and see what ghci infers, or just give it the correct signature: :: [SampleType]
.
stList :: (Show a) => [a]
is saying that given any Show
instance for any type a
, you are going return a list of elements of that type.
stList = [SampleTypeConstructor]
is returning list of SampleType
s, while that is a list of elements for which there exists a Show
instance, it is not a list that works for every choice of a
.
In reality the only inhabitant you are likely to find for this type that doesn't involve bottoms is []
, because Show a
doesn't provide any mechanism to construct an a
.
To fix this you can do one of a few things, depending on your end goal.
You may just want to let stList
have the more narrow type:
stList :: [SampleType]
You may want to build some kind of type like
newtype Showable = Showable (Int -> String -> String)
which explicitly captures the relevant portion of the Show instance. (You can also do this with an existential type, but this version is Haskell 98.)
instance Show Showable where
showsPrec d (Showable f) = f d
showable :: Show a => a -> Showable
showable a = Showable (\d -> showsPrec d a)
Then you could make a list of Showables.
stList :: [Showable]
stList = [showable SampleTypeConstructor]
But ultimately it depends on what you are trying to accomplish.