[英]It could refer to either `Data.Monoid.<>'
I have following declaration: 我有以下声明:
data Two a b = Two a b deriving (Eq, Show)
instance (Semigroup a, Semigroup b) => Semigroup (Two a b) where
(Two a b) <> (Two c d) = Two (a <> c) (b <> d)
And tried it in the prelude: 并在前奏中尝试过:
*Main First Lib MonoidLaws Semi> (Two a b) <> (Two c d) = Two (a <> c) (b <> d)
<interactive>:10:3: error:
* Occurs check: cannot construct the infinite type: t1 ~ Two t1 t1
Expected type: t1 -> t -> b
Actual type: Two t1 t1 -> Two t t -> Two b b
* Relevant bindings include
(<>) :: t1 -> t -> b (bound at <interactive>:10:3)
How can I use mappend
function from Semigroup
for Two
datatype in prelude? 如何使用
mappend
功能从Semigroup
的Two
在前奏数据类型?
Try it with something that's an instance of Semigroup, like List
. 尝试使用Semigroup实例的东西,例如
List
。
For example: 例如:
> (Two "1" "2") <> (Two "3" "4")
Two "13" "24"
In your attempt / example, a
and b
, c
and d
are not defined, so Haskell sees them as variables. 在您的尝试/示例中,未定义
a
和b
, c
和d
,因此Haskell将其视为变量。 Because you're using =
between them, it's assuming you want to do pattern matching, so it's trying to match them to themselves, which is causing an infinite loop (as that is perfectly valid Haskell — to define values in terms of themselves). 因为您在它们之间使用
=
,所以假设您要进行模式匹配,因此它试图将它们与自身进行匹配,这将导致无限循环(因为这完全有效,Haskell –根据自身定义值)。 This is causing an error, though, because it would imply an infinite type , which it definitely seems is not what you want. 但是,这将导致错误,因为这将暗示一个无限类型 ,这显然不是您想要的。
It might be worth starting with some simpler things, possibly. 可能应该从一些简单的事情开始。 A good basic book which I helped author is http://happylearnhaskelltutorial.com but that doesn't deal with instantiating your own typeclasses.
我帮助作者撰写的一本不错的基础书是http://happylearnhaskelltutorial.com,但是它与实例化您自己的类型类无关。 Having said that it'll give you a reasonably good understanding of pattern matching, variables, types and values which you'll need before you move on to understanding typeclasses enough to build your own instances.
话虽如此,它可以使您对模式匹配,变量,类型和值有一个相当好的了解,然后再继续理解足以构建自己的实例的类型类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.