feat(fsharp): m07 io and m08 stream with tests

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-29 00:52:44 +01:00
co-authored by Claude Opus 5.5
parent 9cbefeb2be
commit e27372f017
9 changed files with 463 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
/// 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)
/// 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
/// What the outside world answers: `Done` for a `Draw`, `Seed value` (an unsigned 32-bit number) for a `RandomSeed`.
type OpResult =
| Done
| Seed of uint32
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"
/// 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.
let trace (seed: uint32) (io: IO<'a>) : Traced<'a> = failwith "TODO m07"
+2
View File
@@ -13,5 +13,7 @@
<Compile Include="Spawn.fs" />
<Compile Include="Game.fs" />
<Compile Include="View.fs" />
<Compile Include="IO.fs" />
<Compile Include="Stream.fs" />
</ItemGroup>
</Project>
+49
View File
@@ -0,0 +1,49 @@
/// m08 stream: an event stream as a pure automaton (a Mealy machine).
/// A step feeds one event and returns the (possibly empty) list of outputs it produced and the stream to use for
/// the next event. Nothing is mutated, so a stream value can be run any number of times with the same results.
module Snake.Stream
/// Something that happens to the program: time passing (ms since start) or a key going down or up.
type Event =
| Time of int64
| KeyDown of string
| KeyUp of string
type Stream<'a> = Stream of (Event -> 'a list * Stream<'a>)
/// Feed one event to `s`: the outputs it produced and the stream for the next event.
let step (e: Event) (s: Stream<'a>) : 'a list * Stream<'a> = failwith "TODO m08"
/// Emits the `key` of every KeyDown event; ignores everything else.
/// (The stub is wrapped in `Stream` so the TODO only fires when the stream is stepped.)
let keyDowns: Stream<string> = Stream(fun _ -> failwith "TODO m08")
/// Emits the `key` of every KeyUp event; ignores everything else.
let keyUps: Stream<string> = Stream(fun _ -> failwith "TODO m08")
/// On `Time t`, emits every multiple `k*ms` with `last < k*ms <= t`, ascending, where `last` starts at 0
/// and becomes `t` afterwards. A big time jump can emit several values. Ignores key events.
/// Hint: write a private helper that carries `last`.
let interval (ms: int64) : Stream<int64> = failwith "TODO m08"
/// Apply `f` to every output.
let map (f: 'a -> 'b) (s: Stream<'a>) : Stream<'b> = failwith "TODO m08"
/// Keep only the outputs for which `p` is true.
let filter (p: 'a -> bool) (s: Stream<'a>) : Stream<'a> = failwith "TODO m08"
/// Apply `f` to every output; `Some b` emits `b`, `None` is dropped (like `List.choose`).
let choose (f: 'a -> 'b option) (s: Stream<'a>) : Stream<'b> = failwith "TODO m08"
/// Both streams step on every event; the outputs of `a` come before the outputs of `b`.
let merge (a: Stream<'a>) (b: Stream<'a>) : Stream<'a> = failwith "TODO m08"
/// Fold the outputs into an accumulator, starting from `z`; emits each new accumulator (not `z` itself).
let scan (f: 'b -> 'a -> 'b) (z: 'b) (s: Stream<'a>) : Stream<'b> = failwith "TODO m08"
/// Emit `v` once, before the outputs of the first step; later steps are unaffected.
/// Note: streams only act when an event arrives, so if no event ever arrives, `v` is never emitted.
let startWith (v: 'a) (s: Stream<'a>) : Stream<'a> = failwith "TODO m08"
/// Feed all events through the stream and collect every output, in order. A pure fold.
let run (events: Event list) (s: Stream<'a>) : 'a list = failwith "TODO m08"