/// 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 = 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 list) : IO = failwith "TODO m07" /// An IO that draws one shape (a `Rect` or `Text` from m06) and produces `()`. let draw (shape: Shape) : IO = 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 = failwith "TODO m07" /// Draw every shape of a scene in order: `sequence (List.map draw scene)`. let drawScene (scene: Scene) : IO = 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 = 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 = 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"