Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing multiple declarations of data constructors

Tags:

haskell

I am currently experimenting with data types and I've ran into an issue involving multiple declarations of data constructors.

data DBPosition = Unknown
                | Omega Integer
                | Delta Integer
                deriving (Show, Eq, Ord)

data DBGeometry = Unknown | Cis | Trans
                deriving (Show, Eq, Ord)

data DoubleBond = DoubleBond DBPosition DBGeometry
                deriving (Show, Eq, Ord)

If I was to make a value such as - let bond = DoubleBond Unknown Unknown, then it could be inferred that the first Unknown has a type of DBPosition while the second Unknown has a type of DBPosition. Unfortunately this is not the case:

test.hs:6:27:
Multiple declarations of `Unknown'
Declared at: test.hs:1:27
             test.hs:6:27
Failed, modules loaded: none.

Are there any language extensions that can be used to get around this?

like image 289
Michael T Avatar asked Sep 04 '15 04:09

Michael T


1 Answers

As Carsten pointed out above, your definition does not work because you have two constructors with the same name. You'd need to use e.g. UnknownDBPosition and UnknownDBGeometry. However, I'd argue a better solution arises from recognising:

  • That the concept of an unknown value works in precisely the same way no matter if you are talking about double bond positions, geometries, or whatever else; and
  • That Unknown is not actually a variety of double bond geometry or position.

That being so, I recommend that you remove both Unknown and use Maybe to specify lack of knowledge.

data DBPosition = Omega Integer
                | Delta Integer
                deriving (Show, Eq, Ord)

data DBGeometry = Cis | Trans
                deriving (Show, Eq, Ord)

data DoubleBond = DoubleBond (Maybe DBPosition) (Maybe DBGeometry)
                deriving (Show, Eq, Ord)
like image 152
duplode Avatar answered Nov 15 '22 07:11

duplode