简体   繁体   中英

Single Constructor Algebraic Data Type: what does it mean?

If I make an algebraic data type like this:

data Foo = String

what is it? It appears to not be a sum type (there's nothing next to it to choose from), but also not a product type (there's no argument to combine it with). Is there some default implied here, like "an ADT with a single constructor is a Sum type"?

Your type Foo is equivalent to the unit type () ; it's a type with a single value written String , just like () is a type with a single value written () .

Any ADT is a sum-of-products. So:

data Foo
    = One Integer String
    | Two String Integer

declares Foo to be (warning: not actually Haskell!) Integer * String + String * Integer .

If you have a product of 0 types, that's just the empty product () (just like product [] is 1), so

data Bar
    = Three
    | Four String Integer

declares Bar to be () + String * Integer .

And, of course, if you have a sum of 1 type, it's just that type (just like sum [x] is x :

data Baz = Five

declares Baz to be () .

So it is a sum type, but trivially, since it's a sum of only one type; and it's a product type, but again trivially, since it's a product of 0 types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM