liveBook · Manning

Atomic type

As explained in Functional Design and Architecture: Examples in Haskell

A data type is a type that defines a set of possible values together with the constructors used to build those values.

A data type defines the values that may exist in a domain and the constructors used to build them. Domain types therefore act as a small embedded domain-specific language: business concepts become explicit code rather than scattered primitives. section 5.1.1

A type also gives the compiler a design boundary. When functions declare and consume precise types, invalid combinations can fail during compilation instead of becoming debugging problems at runtime. section 1.5.2

Representing shapes

Shape makes the alternatives explicit: Circle carries a radius, while Rectangle carries width and height. The area function pattern-matches on those constructors, so its cases correspond directly to the type’s possible values. Calling area with an integer fails because the function requires Shape. section 1.5.2

data Shape = Circle Double
          | Rectangle Double Double

Inspect the type declaration that defines Shape and its constructors. section 1.5.2

area :: Shape -> Double
area (Circle r)      = pi * r^2
area (Rectangle w h) = w * h

Inspect the function whose pattern-matching cases consume the Shape values. section 1.5.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains values, constructors, pattern matching, and compile-time rejection, while only surveying advanced type-level design.
Examples
Uses Shape with Circle and Rectangle constructors, an area function, and an invalid integer call that produces a type error.
What you can do after
Build domain representations and functions whose invalid inputs can be rejected by the compiler.
How it connects
Connects data types with embedded domain-specific languages, pattern matching, static type systems, and type-level design.

Also explained in

Compare how 17 books explain atomic type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.