Georgi Angelov
Georgi Angelov

Reputation: 4388

Haskell : multiple declarations of "<function name>" ...?

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

Upvotes: 2

Views: 6204

Answers (4)

user4945014
user4945014

Reputation:

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

Upvotes: 2

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

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

Upvotes: 11

Fred Foo
Fred Foo

Reputation: 363467

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.

Upvotes: 0

amalloy
amalloy

Reputation: 91837

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.

Upvotes: 0

Related Questions