0
votes

I try to implement a function (Tree a) -> (Tree a) -> (Tree a). The function should sum the node values and return a tree with the sums. Unfortunatley i got the following error message:

Aufgabe10.hs:4:11: Unexpected type ‘Tree a’ In the class declaration for ‘+’ A class declaration should have form class + a b c where ...

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. 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:9:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus Empty (Node a b c) = (Node a b c)

Aufgabe10.hs:10:9: Pattern bindings (except simple variables) not allowed in instance declarations Plus (Node a b c) Empty = (Node a b c)

1
What are you trying to achieve by writing class (+) (Tree a) where?jub0bs
My understanding of your homework is that you have to write a Num instance for Tree a. You probably do not want to write a class declaration, here. However, if my guess is correct, you will also have to implement the other methods required for a Num instance declaration; see hackage.haskell.org/package/base-4.8.0.0/docs/Prelude.html#g:7jub0bs
(Tree a) (Tree a) -> ... should also be (Tree a) -> (Tree a) -> ...chi
And instance (Num a) => (+) (Tree a) (Tree a) should probably be instance (Num a) => Num (Tree a).jub0bs
The exercise is just to implement a function (+)::(Tree a)->(Tree a) -> (Tree a)Xeriotek

1 Answers

2
votes

Take a look at What are typeclasses?: there you gonna notice that typeclasses declaration takes:

  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? Yes, one is capitalized and the other is not. Do you know what it means in Haskell?

Plus, as every capitalized word in Haskell, refers to types (in this case, type class called 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.

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. Got it? :)