小编Rea*_*nor的帖子

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

新问题

我不会假装我知道如何思考或谈论哈斯克尔.在伪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 …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
132
查看次数

标签 统计

haskell ×1