我怎样才能向GHC证明(b~Foo)?

Rea*_*nor 2 haskell

新问题

我不会假装我知道如何思考或谈论哈斯克尔.在伪java-oo-jargon中:

我想要做的是有一个"实现""接口"的"结构".该接口的一部分是一个返回实现另一个接口的对象的函数.

interface IFiz {}

interface IBuz {
    function IFiz getFiz() 
}

class Foo implements IFiz { ... }
class Bar implements IBuz {
    IFiz fiz = new Foo();
    function getFiz() {
        return fiz;
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在Haskell中做到这一点?我这样做的尝试如下所述.


老问题

我怎样才能向GHC证明(b~Foo)?

我对这个问题的理解:

Foo是类型类Fiz的一个实例.

我希望Bar成为类型Buz的一个实例.

但是,编译器无法在punk方法的实现中推断出(b~Foo).但还有什么呢?我尝试使用不推荐的直接方式添加数据约束,以及使用GADT,但似乎都没有工作(我继续得到完全相同的错误.)

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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

Mic*_*ryn 6

此类型签名:

class Buz a where
  punk :: Fiz b => a -> b
Run Code Online (Sandbox Code Playgroud)

说它punk必须能够返回b任何类型的类型,b因为它是实例Fiz.所以问题不在于编译器不能推断出那Foo是实例Fiz,而是返回值不是Quux,这是一个实例Fiz.

data Quux = Quux 

instance Fiz Quux where
  funk a = a
Run Code Online (Sandbox Code Playgroud)

如果你想让函数返回任何实例的类型类Fiz,你可以使用ExistentionalQuantification扩展:

{-# LANGUAGE RankNTypes, ExistentialQuantification #-}   
data Foo = Foo Int
data Bar = Bar Foo
data SomeFiz = forall a . Fiz a => SomeFiz a
class Fiz a where
  funk :: a -> a

class Buz a where
  punk :: a -> SomeFiz

instance Fiz Foo where
  funk a = a

instance Buz Bar where
  punk (Bar foo) = SomeFiz foo
Run Code Online (Sandbox Code Playgroud)

否则,如果你真的想要实现这个类型类,你只能通过将底部传递给funk:

instance Buz Bar where
  punk _ = funk undefined
-- or for example:
instance Buz Bar where
  punk _ = funk (funk undefined)
Run Code Online (Sandbox Code Playgroud)

或者通过修复funk:

instance Buz Bar where
  punk _ = fix funk
Run Code Online (Sandbox Code Playgroud)

如果你能提供你想要达到的目标的更多细节,也许我会给出更多有用的答案.