gpt4 book ai didi

haskell - 菜鸟 "Duplicate instance declarations"(再次)

转载 作者:行者123 更新时间:2023-12-02 10:27:48 27 4
gpt4 key购买 nike

是的...抱歉,这个问题之前已经被问过,但通常是关于一些非常具体和复杂的事情,以至于难以理解

带着天真的面向对象的头脑......我们走......

class Animal a where

class Mammal m where

class Insect i where

instance (Mammal m) => Animal m

instance (Insect i) => Animal i

ghc 开始

重复的实例声明:

  instance forall (k :: BOX) (m :: k). Mammal m => Animal m
instance forall (k :: BOX) (i :: k). Insect i => Animal i

你查一下......有一个使用见证类型的解决方案,我可能可以开始工作但是......我不明白问题是什么?

据称编译器匹配右侧和 bla bla...?

我不明白......我想我是说......如果我有一种类型类狗......那么它也是类型类动物......所以如果我调用方法 foo 等那么这是如何做到的(就狗而言)

我错过了一些东西

最佳答案

是的,你错过了一些东西。以下是您应该如何定义类层次结构:

class Animal a where
class Animal a => Mammal a where
class Animal a => Insect a where

这就是表达父类(super class)关系的方式。实例声明用于实际创建类的类型实例。

instance Animal Ant where
instance Insect Ant where

原文出了什么问题

你写的

instance (Mammal m) => Animal m

instance (Insect i) => Animal i

Haskell 要求每个类和类型只有一个实例。因此只能从 => 右侧的部分确定。所以它看到两个声明

instance Animal a

并提示。你可以有

instance Animal (Maybe a)

还有

instance Animal Int

但是如果你有

instance Animal a

那么您就不能为 Animal 声明任何其他实例。

关于haskell - 菜鸟 "Duplicate instance declarations"(再次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32375983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com