nix-playgraound/notes/038-haskell-monad-chaining.md

66 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

# Haskell Monad Chaining
This note covers `35-haskell-monad-chaining/`, which sequences several dependent rollout checks with `Either` and `do` notation.
---
## 1. Why Monad Chaining Matters
Some workflows cannot be expressed as independent field checks.
In this example, later steps depend on earlier successful results:
- find the service profile first,
- then check whether the environment is allowed,
- then validate the production change ticket,
- then choose the image tag for the requested track, and
- finally choose the approver.
That dependency chain is the point. Each step needs the result of the previous one.
---
## 2. Why `Either` Still Fits
This example does not need accumulated errors. It needs short-circuiting business logic.
That makes `Either String` a good fit:
```haskell
approveRollout :: Catalog -> RolloutRequest -> Either String ApprovedRollout
```
`do` notation keeps the happy path readable while preserving the fail-fast semantics.
---
## 3. How This Complements the Validation Example
`28-haskell-applicative-validation/` teaches independent checks that all run so several errors can be reported together.
This example teaches the opposite shape:
- one decision unlocks the next, and
- the workflow stops once a prerequisite fails.
That contrast is useful. It shows why “Applicative versus Monad” is not just theory. The control flow shape changes the design.
---
## 4. Commands to Try
```bash
cd 35-haskell-monad-chaining
nix develop
cabal run
cabal run -- api:production:stable:CHG-2048
cabal test
nix build
./result/bin/mini-monad-chain api:production:stable:CHG-2048
nix run . -- api:production:stable:CHG-2048
nix flake check
```