简体   繁体   English

Haskell类声明中的意外类型

[英]Haskell unexpected type in class declaration

I try to implement a function (Tree a) -> (Tree a) -> (Tree a). 我尝试实现一个功能(树a)->(树a)->(树a)。 The function should sum the node values and return a tree with the sums. 该函数应该对节点值求和,并返回一个带有总和的树。 Unfortunatley i got the following error message: Unfortunatley我收到以下错误消息:

Aufgabe10.hs:4:11: Unexpected type 'Tree a' In the class declaration for '+' A class declaration should have form class + abc where ... Aufgabe10.hs:4:11:意外的类型'Tree a'在'+'的类声明中,类声明的形式应为class + abc,其中...

This is my code: 这是我的代码:

data Tree a = Node a (Tree a) (Tree a) 
    |Empty

class (+) (Tree a) where
    (+) :: (Tree a) (Tree a) -> (Tree a)

instance (Num a) => (+) (Tree a) (Tree a) where
    (+) (Node a1 b1 c1)  (Node a2 b2 c2) = (Node (a1+a2) ((+) b1 b2)  ((+) c1 c2)) 
    (+) Empty (Node a b c) = (Node a b c) 
    (+) (Node a b c) Empty = (Node a b c)

Ok i changed now the class to Plus and named the function plus, cause i don't want to implement all the numb functions. 好的,我现在将类更改为Plus并将其命名为plus,因为我不想实现所有的numb函数。 This is the new code: 这是新代码:

data Tree a = Node a (Tree a) (Tree a) 
    |Empty

class Plus a where
    plus:: (Tree a)  -> (Tree a)  -> (Tree a) 

instance (Num a) => Plus (Tree a) where
    Plus (Node a1 b1 c1)  (Node a2 b2 c2) = (Node (a1+a2) (Plus b1 b2)  (Plus c1 c2)) 
    Plus Empty (Node a b c) = (Node a b c) 
    Plus (Node a b c) Empty = (Node a b c)

I get the following errors: 我收到以下错误:

Aufgabe10.hs:8:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus (Node a1 b1 c1) (Node a2 b2 c2) = (Node (a1 + a2) (Plus b1 b2) (Plus c1 c2)) Aufgabe10.hs:8:9:实例声明中不允许使用模式绑定(简单变量除外)Plus(Node a1 b1 c1)(Node a2 b2 c2)=(Node(a1 + a2)(Plus b1 b2)(Plus c1 c2 ))

Aufgabe10.hs:9:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus Empty (Node abc) = (Node abc) Aufgabe10.hs:9:9:实例声明中不允许使用模式绑定(简单变量除外),而且为空(Node abc)=(Node abc)

Aufgabe10.hs:10:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus (Node abc) Empty = (Node abc) Aufgabe10.hs:10:9:实例声明中不允许使用模式绑定(简单变量除外)Plus(Node abc)Empty =(Node abc)

Take a look at What are typeclasses? 看看什么是类型类? : there you gonna notice that typeclasses declaration takes: :在那里,您会注意到typeclasses声明需要:

  1. the typeclass name; 类型类名称;
  2. a type variable. 类型变量。

Hence, your declaration should start as 因此,您的声明应以

class (+) a where

not

class (+) (Tree a) where

although I would prefer 虽然我更喜欢

class Plus a where

That solves the first of your problems. 那解决了您的第一个问题。 :) :)


Answering your updated version... 回答您的更新版本...

Do you see the difference between Plus and plus ? 您看到Plusplus之间的区别了吗? Yes, one is capitalized and the other is not. 是的,一个是大写的,另一个不是大写的。 Do you know what it means in Haskell? 您知道Haskell的含义吗?

Plus , as every capitalized word in Haskell, refers to types (in this case, type class called Plus ). Plus ,就像Haskell中每个大写的单词一样,都指向类型 (在这种情况下,称为Plus 类型类 )。

By the other hand, functions must be always lowercase — and that is why you defined plus :: (Tree a) -> (Tree a) -> (Tree a) in lowercase. 另一方面,函数必须始终为小写字母,这就是为什么要以小写形式定义plus :: (Tree a) -> (Tree a) -> (Tree a)

Have you already gotten your problem? 你已经遇到问题了吗?

Instead of 代替

instance (Num a) => Plus (Tree a) where
    Plus (Node a1 b1 c1)  (Node a2 b2 c2) = (Node (a1+a2) (Plus b1 b2)  (Plus c1 c2)) 
    Plus Empty (Node a b c) = (Node a b c) 
    Plus (Node a b c) Empty = (Node a b c)

you should have written 你应该写

instance (Num a) => Plus (Tree a) where
    plus (Node a1 b1 c1)  (Node a2 b2 c2) = (Node (a1+a2) (Plus b1 b2)  (Plus c1 c2)) 
    plus Empty (Node a b c) = (Node a b c) 
    plus (Node a b c) Empty = (Node a b c)

because you are defining the behaviour of the function plus when Tree is an instance of Plus . 因为当TreePlus的实例时,您正在定义plus的行为。 Got it? 得到它了? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM