2

Hey guys so here is my code on which I get the weird error of "Multiple Declarations of mirror". I have other functions before that but none of them are named mirror... Any ideas?

mirror :: BinTree a -> BinTree a
mirror = undefined
mirror (Node tL x tR) = Node x mirror tR mirror tL

4 Answers 4

11

Multiple definitions of a function must have the same number of arguments left of the equals sign. This is not required from a theory standpoint (note: one of the definitons could certainly be a lambda or return another function) but people seem to like it as such definitions typically indicate a bug.

Specifically, you have one definition with zero arguments:

mirror = undefined

And one definition with one argument:

mirror (Node tL x tR) = Node x mirror tR mirror tL

you probably want:

mirror _              = undefined
mirror (Node tL x tR) = Node x mirror tR mirror tL
Sign up to request clarification or add additional context in comments.

3 Comments

He would really want to swap the order, because the order of resolution for pattern matching is top to bottom, isn't it?
@behklilr I think people are reading too much into the actual code. undefined is often used as a placeholder as it allows type-checking of the rest of the code before putting in another case - in this case a case for some sort of Bin or Nil constructor.
I actually removed the undefined. It was completely my fault for not reading my assignment. The undefined was placed there to be replaced with the actual content...Thank you.
3

This is not the issue with this specific example, but since this is the first result on Google for "multiple definitions Haskell", I figured I should contribute what the problem was for me:

If you are defining the function multiple times using pattern-matching with some of the arguments, all of the definitions must be consecutive. If there is other code in between them, they are considered separate definitions.

Example: the following is invalid because the definition of b divides the definitions of a:

frobnicate :: Bool -> String
frobnicate True = "foo"
b = "bar"
frobnicate False = b

Comments

0

Line 2 and line 3 have conflicting types: you've defined mirror to be the constant undefined, and then attempt to define it as a one-argument function. Removing line 2 should fix the problem; it's not clear why you wrote it in the first place.

2 Comments

Actually the type is not the problem. undefined has type a, which matches any other type.
I think it's more accurate to say that undefined is a member of every type. This includes the type a -> b, i.e., one-argument functions.
0

You have conflicting definitions for mirror. The first clause,

mirror = undefined

is a catch-all, so the definition is considered finished by the compiler. The next clause is then considered to start a new definition. You should remove the undefined line.

1 Comment

@GeorgiAngelov: the undefined is probably intended to signal "fill in" while making sure the program compiles. You'll have to remove it to get a working program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.