What is the significance of algebraic datatypes with zero constructors?

后端 未结 4 1211
离开以前
离开以前 2020-12-19 10:31

This passage, which unfortunately lacks references, about the development of ADTs in Haskell, from A History of Haskell: Being Lazy With Class, section 5.1:

4条回答
  •  梦毁少年i
    2020-12-19 11:07

    Theoretically: the Curry-Howard isomorphism gives us an interpretation of this type as the "false" proposition. "false" is useful as a proposition on its own; but is also useful for constructing the "not" combinator (as type Not a = a -> False) and other similar constructions.

    Pragmatically: this type can be used to prevent certain branches of parameterized data types from coming into existence. For example, I've used this in a library for parsing various game trees something like this:

    data RuleSet a            = Known !a | Unknown String
    data GoRuleChoices        = Japanese | Chinese
    data LinesOfActionChoices -- there are none in the spec!
    type GoRuleSet            = RuleSet GoRuleChoices
    type LinesOfActionRuleSet = RuleSet LinesOfActionChoices
    

    The impact of this is that, when parsing a Lines of Action game tree, if there's a ruleset specified, we know its constructor will be Unknown, and can leave other branches off during pattern matches, etc.

提交回复
热议问题