Page: replay is one fold over the entries

Job: explanation

Level: senior grug (level 3 of 6)

Source lines:

  • src/Replay.fs:5#replay
  • src/Oracle.fs:5#expected

replay is one fold over the entries

senior grug (level 3 of 6)explanationteaches: fold-carries-the-entry, replay-order

explanation

this page says why the code has this shape and what a reader must be careful of.

why this exists

Every run starts at a balance and applies entries in order. replay is that loop. src/Replay.fs:5#replay is the decisive line. src/Oracle.fs:5#expected is the answer that loop has to reach.

source anchor: this line is in the file

run sed -n '5,9p' src/Replay.fs

It prints:

let replay (start: int) (deltas: int list) =
  List.fold (+) start deltas
/// An empty list leaves the starting balance alone.
let emptyReplay (start: int) = List.fold (+) start []

The cited file src/Replay.fs ships with this site at the same path. Run the command from the school folder, or open src/Replay.fs.

what breaks

Keep the entries and change fold to foldBack. The same list then gives a different balance, because subtraction is not commutative.

where it fits

apply defines one step. The oracle predicts the result the fold must reach.

try it

Run grep -n 'let replay' src/Replay.fs. Change fold to foldBack in src/Replay.fs and run grugschool check .; the saved proof no longer matches the shipped line.

reference: every connection to this page
where this fits

This is reference. The next page in the reading order is at the bottom of the page.

path order: one entry changes a balance by a fixed amount

goes on to: the oracle is a closed form, not a transcription

flowchart LR
  A0["one entry changes a balance by a fixed amount"]
  H["you are here: replay is one fold over the entries"]
  B0["the oracle is a closed form, not a transcription"]
  A0 --> H
  H --> B0

grill: say it out loud, then tap

Grug asks. Say the answer out loud, then tap. Grug remembers what you miss and asks again in the pop quiz.

a badge says two things: the number is its level, the colour is the kind of answer it asks for. The six levels are level 1 of 6 recall a fact; level 2 of 6 recall a detail; level 3 of 6 trace a path; level 4 of 6 trace a longer path; level 5 of 6 predict or judge; level 6 of 6 judge a transfer. Colour and level are independent: a high-level question can still ask for a fact, and the colour tells you which it is.

level 3 of 6Q1 · What does replay preserve by folding left to right?
try first, then show answer

4. The order in which deltas reach the balance

why

A fold over a list visits entries in order. The balance after each step is the input to the next step.

level 4 of 6Q2 · If replay used foldBack with subtraction, what would a correct closed-form check show?
try first, then show answer

3. The replayed balance differs from the sum, so the check goes red

why

Subtraction over the same entries from the right gives a different answer. The closed-form oracle uses addition, which does not change with the implementation, so the mismatch is visible.