Why can't i re-use same value constructor among different data types?

有些话、适合烂在心里 提交于 2020-02-15 07:37:38

问题


i am new to Haskell and probably missing something really basic here, but i am not able to re-use same value constructor among different data types.

data Colour = Red | Pink | Orange | Yellow
data Fruit  = Apple | Orange | Banana

This produces error saying

Multiple declarations of ‘Orange’

Not sure why this isn't allowed, i have been using OCaml before learning Haskell and was able to define types like this


回答1:


As a quick exercise try just defining one of your data types and then opening up GHCi to inspect it.

data Colour = Red | Pink | Orange | Yellow

If you use :t in GHCi, it will tell you the type of anything.

> :t Red
Red :: Colour
> :t Orange 
Orange :: Colour 

So this tells you that your data constructor Orange is really just a function that takes no arguments and produces a value of type Colour.

So what happens if you add a duplicate declaration?

data Colour = Red | Pink | Orange | Yellow
data Fruit  = Apple | Orange | Banana

Now you have defined a function Orange that takes no arguments and produces a value of type Colour or a value of type Fruit. This won't work at all! It would be the same as defining your own custom function foo and giving it multiple type signatures:

foo :: Int 
foo :: String
foo = "6"

Which obviously doesn't work either.

To get around this, you can define each data type in its own module, and use a qualified import to scope them correctly:

import qualified Colour as C -- Module Colour.hs 
import qualified Fruit as F -- Module Fruit.hs 

orange1 = C.Orange :: C.Colour 
orange2 = F.Orange :: F.Fruit

Now, you might be thinking "The compiler is smart, it should know what Orange I'm talking about when I'm using it." and you'd be partially right. There is an ongoing effort to bring Overloaded or Duplicate record fields into Haskell. There are various other questions of that ilk already defined here, but I'll list a few references for further reading.

  • Why DuplicateRecordFields cannot have type inference?
  • https://github.com/adamgundry/ghc-proposals/blob/overloaded-record-fields/proposals/0000-overloaded-record-fields.rst
  • https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/DuplicateRecordFields



回答2:


There is no particular reason, that is how language was designed. I think the idea was to make sure compiler can infer type for as many expressions as possible. Note that if language will allow to reuse constructors, then you'll have to specify type for show Orange expression - compiler can't infer it anymore. Though now a lot of people don't take this reason seriously, and a lot of modern language extentions do break compiler's ability to infer types for many expressions. So I guess in few years you'll find that your example works already :)



来源:https://stackoverflow.com/questions/51597063/why-cant-i-re-use-same-value-constructor-among-different-data-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!