I have a class called Foo
that owns a function gen :: Int -> [Foo]
. For instance, I could make an instance of Foo
that way:
data FooTest = FooTest Int
instance Foo FooTest where
gen n = replicate n (FooTest 0)
Now, let’s imagine I have another class called Bar
that defines a function bar :: Bar -> IO ()
. Each instance of Foo
has to be an instance of Bar
, but the Bar
implementation is quite the same for each instance. Here’s an example:
class Foo f where
gen :: Int -> [f]
class Bar b where
bar :: b -> IO ()
instance Bar Foo where -- obviously that doesn’t work
bar _ = putStrLn "bar through any Foo instance"
instance (Foo f) => Bar f where -- this needs the FlexibleInstance GHC extension first, then it still throws shit saying that the constraint is not smaller that I don’t shit
bar _ = putStrLn "bar through any Foo instance"
The problem here is I can’t find any ways to make a class an instance of another to mention the fact that any instance of the first class will share the same implementation for instancing the other class.
Any idea?
Thanks in advance.