Skip to content
BANG

10. Generation as an effect

Teaches: seeded, replayable nondeterminism — the DST warm-up

Nondeterminism is an effect too — Choice performs a pick, and the handler decides what "random" means. Give it a constant-returning handler and reruns are byte-identical; that determinism-by-construction is what makes seeded simulation testing possible: swap the handler for a scheduler policy, replay the same driver, and every run is reproducible. (The full picture — a driver replayed under different delivery-order policies — lives in examples/dst-rounds-const/, the tour's parting pointer past v0.)

examples/gen-seed-a/

effect Choice { pick : Int -> Int }
 
handle
  (sched.pick(6)) + (sched.pick(6)) + (sched.pick(6))
with Choice as sched {
  pick(n) => 2
}

Expected output (bang run stdout):

6

examples/gen-seed-b/

effect Choice { pick : Int -> Int }
 
handle
  (sched.pick(6)) + (sched.pick(6)) + (sched.pick(6))
with Choice as sched {
  pick(n) => 5
}

Expected output (bang run stdout):

15

Run it yourself:

curl -fsSL https://raw.githubusercontent.com/phibkro/bang/main/tools/install.sh | sh
bang run examples/gen-seed-a/main.bang
bang run examples/gen-seed-b/main.bang

← 9. Identity dispatch (the lexical cap)