Theory

A sum type provides alternatives (โ€œeither/orโ€ data).

data Shape2D
  = Rect Double Double          -- width height
  | Circ Double                 -- radius
  | Poly [Vertex]               -- โ‰ฅ3 vertices

Semantics

If has values, has , then Either A B has .

Exhaustive Pattern Matching

Compiler checks every constructor is handled.

area :: Shape2D -> Double
area (Rect w h) = w * h
area (Circ r)   = pi * r * r
area (Poly vs)  = polygonArea vs

Use sum types for error handling:

data Result a = Ok a | Err String

NOTE

Consuming code must examine both cases, preventing silent failures.