haskell sum type multiple declaration error

前端 未结 4 1333
情歌与酒
情歌与酒 2020-12-22 03:55
data A=A
data B=B
data AB=A|B

Which makes a sum type AB from A and B.

but the last line induces a compile error \"multiple declarations of

4条回答
  •  梦毁少年i
    2020-12-22 04:41

    When you say data AB = A | B, you are not referring to the types A and B, but rather are defining data constructors A and B. These conflict with the constructors defined on the the previous lines.

    If you want to create a type AB that is the sum of A and B, you must provide data constructors that wrap the types A and B, e.g.:

    data AB = ABA A | ABB B
    

提交回复
热议问题