Files
functional-programming-snake/fsharp/src/IO.fs
T
Lucas WintherandClaude Fable 5.2 edd57f0148 feat(course): audit fixes and twelve extensions (Modules 7b, 12, 13)
Audit pass:
- Frame rule counts frames since the last move, so every step lasts
  exactly movesEvery(score) frames, also right after a speed-up
- clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap
- Pong Restart keeps held keys
- Fact corrections (mulberry32 output coverage, Haskell IO, uniform)
- New fixture cases from a mutation analysis

Extensions (all three tracks, shared fixtures, course text):
- Turn queue (Exercise 10.6), persisted high score (10.7),
  replay viewer (10.8), play() end-to-end helper (9.5)
- Optional State (4.7) and optional Module 7b, stack-safe IO
- Module 12, property-based testing with generators, shrinking,
  invariants and a model-based oracle
- Module 13, Conway's Life with a Click event
- JS type checking via jsconfig, JSDoc and types.d.ts
- Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh,
  Playwright end-to-end suite (e2e/, scripts/e2e.sh)

Docs: docs/contract.md is the canonical contract; specs and build log
under docs/superpowers.

Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression
M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 12:05:29 +01:00

76 lines
3.7 KiB
FSharp

/// m07 io: effects as values. An `IO<'a>` is a description of a program that, when interpreted, produces an `'a`:
/// - `Pure a` finished, nothing left to do
/// - `Effect (op, k)` perform `op`, then continue with `k result` (which returns another IO)
/// (m10) Two more operations, for a best score that survives a reload: `LoadBest` (answered with `Best value`, the
/// stored best) and `SaveBest score` (answered with `Done`).
/// Building an IO never performs anything; only an interpreter (`trace` here, `Runtime.runIO` in m09) does.
module Snake.IO
open Snake.View
/// An operation an IO program can ask the outside world to perform.
type Op =
| Draw of Shape
| RandomSeed
/// (m10) Read the stored best score.
| LoadBest
/// (m10) Store `score` as the new best.
| SaveBest of int
/// What the outside world answers: `Done` for a `Draw`, `Seed value` (an unsigned 32-bit number) for a `RandomSeed`;
/// (m10) `Best value` for a `LoadBest`, `Done` for a `SaveBest`.
type OpResult =
| Done
| Seed of uint32
| Best of int
type IO<'a> =
| Pure of 'a
| Effect of Op * (OpResult -> IO<'a>)
/// The result of `trace`: the final value and the operations performed, in execution order.
type Traced<'a> = { Result: 'a; Ops: Op list }
/// An IO that does nothing and produces `a`.
let pure (a: 'a) : IO<'a> = failwith "TODO m07"
/// An IO that performs the single operation `op` and produces its result: `Effect (op, pure)`.
let effect (op: Op) : IO<OpResult> = failwith "TODO m07"
/// Run `io`, then feed its result to `f` to get the next IO.
/// `Pure a`: `f a`. `Effect (op, k)`: the same op, with the continuation extended by `f`.
let bind (f: 'a -> IO<'b>) (io: IO<'a>) : IO<'b> = failwith "TODO m07"
/// Transform the result with `f`. Must not call `f` until the IO is interpreted.
let map (f: 'a -> 'b) (io: IO<'a>) : IO<'b> = failwith "TODO m07"
/// Run the IOs one after another, left to right, producing `()`. `sequence []` performs nothing.
let sequence (ios: IO<unit> list) : IO<unit> = failwith "TODO m07"
/// An IO that draws one shape (a `Rect` or `Text` from m06) and produces `()`.
let draw (shape: Shape) : IO<unit> = failwith "TODO m07"
/// An IO that asks the outside world for a random seed and produces it (the `value` of the `Seed` answer).
/// It is a function of `unit` (use it as `randomSeed ()`) because F# evaluates module-level values as soon as
/// the module is used; every call returns the same description.
let randomSeed () : IO<uint32> = failwith "TODO m07"
/// Draw every shape of a scene in order: `sequence (List.map draw scene)`.
let drawScene (scene: Scene) : IO<unit> = failwith "TODO m07"
/// (m10) An IO that reads the stored best score and produces it (the `value` of the `Best` answer).
/// A function of `unit`, like `randomSeed ()`; an answer other than `Best` is a broken interpreter: fail loudly.
let loadBest () : IO<int> = failwith "TODO m10"
/// (m10) An IO that stores `score` as the new best and produces `()`: built like `draw`, from `SaveBest score`.
let saveBest (score: int) : IO<unit> = failwith "TODO m10"
/// (m10) `trace` that also answers every `LoadBest` with `Best best` and every `SaveBest` with `Done`.
let traceWith (seed: uint32) (best: int) (io: IO<'a>) : Traced<'a> = failwith "TODO m10"
/// A pure interpreter for tests. Answers every `RandomSeed` with `Seed seed` and every `Draw` with `Done`.
/// Returns the final value and the operations in the order they were executed.
/// Implemented by recursion, with no mutation.
/// (m10) Once `traceWith` (above) is written, `trace` becomes its "no stored best" case: `traceWith seed 0 io`.
let trace (seed: uint32) (io: IO<'a>) : Traced<'a> = failwith "TODO m07"