Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Haskell not allow two data constructors to share "Const" and other defined data attributes?

Tags:

haskell

data Prop = Const Bool
          | Var Char
          | Not Prop
          | And Prop Prop
          | Imply Prop Prop

I am implementing the above data contructor with a minor addition,

data Prop = Const Bool
          | Var Char
          | Not Prop
          | Or Prop Prop
          | And Prop Prop
          | Imply Prop Prop

When I try to make another data contructor below that uses the previously defined constructor,

data Formula = Const Bool
          | Var Prop
          | Not Formula
          | And Formula Formula
          | Or Formula Formula
          | Imply Formula Formula

I get this error: "Multiple declarations of 'Const'"

The same error follows with Not, And, Imply, and Or. Why does Haskell not allow this?

like image 523
ri_ji Avatar asked Mar 01 '23 22:03

ri_ji


1 Answers

Haskell doesn't allow this because it would be ambiguous. The value constructor Prop is effectively a function, which may be clearer if you ask GHCi about its type:

> :t Const
Const :: Bool -> Prop

If you attempt to add one more Const constructor in the same module, you'd have two 'functions' called Const in the same module. You can't have that.

like image 110
Mark Seemann Avatar answered May 19 '23 20:05

Mark Seemann