nix-playgraound/notes/010-haskell-adts.md
2026-04-23 11:15:05 +02:00

1.2 KiB

Haskell Algebraic Data Types

This note covers 08-haskell-adt/, which models a build plan with sum types, a record type, and pattern matching.


1. Why This Example Matters

Haskell programs often start by turning vague strings into precise domain types.

This example does that with:

  • Target as a sum type,
  • Mode as a sum type,
  • Output as a sum type, and
  • BuildPlan as a product type with record fields.

That is one of the most important intermediate Haskell habits: model the domain first, then write functions over the constructors.


2. Pattern Matching in Two Places

The example uses pattern matching in both parsing and behavior:

  • parseTarget, parseMode, and parseOutput turn strings into constructors, and
  • describePlan matches on the BuildPlan value to decide what to print.

That shows two common styles:

  • pattern matching on one constructor at a time in small helper functions, and
  • pattern matching on a whole record value when several fields matter together.

3. Commands to Try

cd 08-haskell-adt

nix develop
cabal run
cabal run -- executable release quiet
cabal test

nix build
./result/bin/mini-plan executable release quiet

nix run . -- executable release quiet
nix flake check