The inspiration for this is creating a list of values that are instances of Show
. I found the following snippet that uses GADTs to create a concrete Showable
type.
data Showable where Showable :: Show a => a -> Showable
instance Show Showable where
show (Showable x) = show x
list :: [Showable]
list = [Showable 4, Showable "hello", Showable 'a']
Then, I tried to make Showable
more general by creating a type that could make any typeclass concrete.
data Concrete a where Concrete :: a b => b -> Concrete a
instance Show (Concrete Show) where
show (Concrete x) = show x
list :: [Concrete Show]
list = [Concrete 4, Concrete "hello", Concrete 'a']
This works with the ConstraintKinds and FlexibleInstances language extensions, but in order to use Concrete
to make concrete types for other typeclasses, each one would require a new instance.
Is there a way to create something similar to Concrete
such that, for example, Concrete Show
is automatically an instance of Show
?
Concrete
is a type constructor andShow
is a type class, the expressionConcrete Show
isn't even legal, much less an instance of anything. I might be wrong, but I'm going to suggest you won't ever get this to work. – MathematicalOrchidShow :: * -> Constraint
, and you can parametrize a type with such kind, as done above. – chiinstance c (Concrete c)
. Also, don't forget that[Showable]
is a known antipattern since it is an overcomplication of[String]
. – chi