coder_bro
coder_bro

Reputation: 10773

Multiple data constructors using same pattern

I have the following data definitions:

data Listr a = Nil | Cons a (Listr a) deriving Show
data Listl a = Nil | Cons (Listl a) a deriving Show

Haskell complains about Multiple declarations of ‘Cons’. (It will complain about Nil as well). Is this not supported by Haskell? How would one go about declaring something like the above? Of course I could use:

data Listr a = Nill | Consl a (Listr a) deriving Show
data Listl a = Nilr | Consr (Listl a) a deriving Show

But I would prefer the first.

Upvotes: 0

Views: 229

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 153342

This is not supported by Haskell. If you absolutely must have two types with a constructor of the same name, you may do so by putting them in different modules. I strongly recommend that you do not get in the practice of doing this, as it can be quite confusing unless you reliably qualify any ambiguous names, even when they are technically not imported in an ambiguous way.

I have enjoyed defining

data Tsil a = Lin | Snoc (Tsil a) a

on occasion; you may like to steal my naming.

Upvotes: 3

Related Questions