Haskell: How to extend a base type using data, value constructors, record syntax, and deriving

If you need to have multiple types extend a base type in Haskell, while using the data keyword, and while using Haskell’s record syntax, this approach seems to work:

data Product = Pizza { crustSize :: CrustSize
                     , crustType :: CrustType
                     , toppings  :: [Topping]
                     }
             | Breadsticks
             | SoftDrink
             deriving (Show)

Note that in addition to using the data keyword to define a type named Product and three value constructors (Pizza, Breadsticks, and SoftDrink) that have the same base type (Product), I also using the deriving keyword so my types will use the Show type class to print well.

To be clear:

  • The type I’m defining is named Product
  • The things on the right side of the = are value constructors
  • Pizza uses the Haskell record syntax
  • Pizza, Breadsticks, and SoftDrink are the different values the Product type can have
  • deriving (Show) is the thing that makes this things convert to strings nicely
  • indentation is important

In summary, if you wanted to see an example that showed all those things (data keyword, a new type, record syntax, multiple value constructors with the record syntax, and deriving), well, there you go.