Page: the oracle is a closed form, not a transcription

Job: explanation

Level: senior grug (level 3 of 6)

Source lines:

  • src/Oracle.fs:5#expected
  • src/Apply.fs:5#apply

the oracle is a closed form, not a transcription

senior grug (level 3 of 6)explanationteaches: oracle-source, change-detector

explanation

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

why this exists

A sum is the answer a correct replay must reach. The check compares the replayed balance with start + List.sum deltas instead of asking the replay what it got. src/Oracle.fs:5#expected is the decisive line. src/Apply.fs:5#apply is the implementation that may drift away from it.

source anchor: this line is in the file

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

It prints:

let expected (start: int) (deltas: int list) =
  start + List.sum deltas
/// Two runs with the same entries must reach the same total from the sum.
let same (left: int list) (right: int list) (start: int) =

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

what breaks

A test that computes its expected balance by calling apply goes green even after + becomes -. Both sides change together. That is a change detector wearing a proof's clothes.

where it fits

apply is the function under test. replay is the path that carries the defect.

try it

Run grep -n 'let expected' src/Oracle.fs. Change List.sum to List.length in src/Oracle.fs and run grugschool check .; the proof on this page refuses the drift.

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: replay is one fold over the entries

flowchart LR
  A0["replay is one fold over the entries"]
  H["you are here: the oracle is a closed form, not a transcription"]
  A0 --> H

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 · Where does the expected balance come from?
try first, then show answer

2. start plus List.sum deltas

why

start + List.sum deltas is independent of apply. It stays correct when apply changes.

level 5 of 6Q2 · A test computes its expected balance by calling apply. What happens after + becomes -?
try first, then show answer

1. The test goes green because both sides use the same wrong operation

why

Both sides change together when the oracle calls the implementation. A green result then says the two agree, not that either is correct.