New Question
I'm not going to pretend that I know how to think or talk about haskell. In pseudo-java-oo-jargon:
What I want to do is have a "structure" that "implements" an "interface." Part of that interface is a function which returns an object which implements another interface.
interface IFiz {}
interface IBuz {
function IFiz getFiz()
}
class Foo implements IFiz { ... }
class Bar implements IBuz {
IFiz fiz = new Foo();
function getFiz() {
return fiz;
}
}
How can I do this in Haskell? My attempt at doing this is described below.
Old Question
How can I prove to GHC that (b ~ Foo)?
My understanding of the problem:
Foo is an instance of the type class Fiz.
I would like Bar to be an instance of the type class Buz.
However, the compiler is unable to deduce that (b ~ Foo) in the implementation of the punk
method. But what else could it be? I attempted to add data constraints using the deprecated direct way, as well as using GADTs, but neither seemed to work (I continued to get the exact same error.)
data Foo = Foo Int
data Bar = Bar Foo
class Fiz a where
funk :: a -> a -- Not important, I just wanted to put something in Fiz
class Buz a where
punk :: Fiz b => a -> b
instance Fiz Foo where
funk a = a
instance Buz Bar where
punk (Bar foo) = foo
Could not deduce (b ~ Foo)
from the context (Fiz b)
bound by the type signature for punk :: Fiz b => Bar -> b
at Test.hs:42:5-8
‘b’ is a rigid type variable bound by
the type signature for punk :: Fiz b => Bar -> b at Test.hs:42:5
Relevant bindings include punk :: Bar -> b (bound at Test.hs:42:5)
In the expression: foo
In an equation for ‘punk’: punk (Bar foo) = foo