Changing the rules¶
In Snap, you changed a rule and watched the tests and proofs react. Lol adds
something new to watch: its rules are written twice, as Legal and as
check, with a proof that they agree. This chapter changes one of them
without the other, then both, to see what that proof is for.
As before, start from a clean copy, and put it back at the end:
Change one: a stricter Valkyrie, in the check only¶
The new rule: the Valkyrie must also be played if you hold her with the Holmgang. Here is the new test of whether she is forced, under a new name:
/-- Must the Valkyrie be played? Also when held with the Holmgang. -/
def valkyrieForcedWide (hand : List Card) : Bool :=
hand.contains .valkyrie && (hand.contains .loki || hand.contains .trader || hand.contains .holmgang)
Add it to Rules.lean, just above Card.aims. Then change check, and only
check, to use it. The changed check looks like this (here it is called
checkWide, so that this course can check it beside the real one):
def checkWide (s : State) (me : Nat) (a : Action) : Option String :=
if (s.player me).out then some "you are out of this round"
else if !(s.player me).hand.contains a.card then some "you do not hold that card"
else if valkyrieForcedWide (s.player me).hand && a.card != .valkyrie then some "you must play the Valkyrie"
else checkAim s me a
Only the third line differs. Now run:
When make proofs fails for no proof
make proofs also checks that docs/GUARANTEES.md, the guarantees in
plain English, is up to date. That page links to the line of each
definition, so a change that adds or removes lines in Rules.lean
makes it fail with docs/GUARANTEES.md is out of date, even when every
proof holds. That message isn't about your proofs. make guarantees
writes the page again.
The scenario tests pass. None of them happens to hold the Valkyrie with
the Holmgang. The random games fail, with offered play accepted: the
engine offered a player the Holmgang, and then refused it with you must
play the Valkyrie. The offered plays come from enabled, which still uses
valkyrieForced, so the engine is now offering one game and checking
another.
The proof that check and Legal agree fails. make proofs stops in
Proofs/LoL/Bookkeeping.lean, in check_none_iff_legal. Lean shows the
goals it couldn't finish. One of them is the case where valkyrieForced is
false. There, the goal says that check, which now refuses "you must play the
Valkyrie" whenever valkyrieForcedWide holds and the card isn't the
Valkyrie, accepts a play exactly when checkAim does. Run make proofs
yourself to see it in Lean's words.
That is: when Legal doesn't force the Valkyrie, check must not refuse
for her either. With the Holmgang in hand, it does. No proof could close
this goal, because it isn't true. Here is the play that shows it:
/-- Seat 0 holds the Valkyrie and the Holmgang. -/
def valkyrieTable : State :=
{ table with players := table.players.set 0 { hand := [.valkyrie, .holmgang] } }
/-- info: some "you must play the Valkyrie" -/
#guard_msgs in
#eval checkWide valkyrieTable 0 ⟨.holmgang, some 1, none⟩
example : Legal valkyrieTable 0 ⟨.holmgang, some 1, none⟩ := by decide
The changed check refuses the Holmgang, and yet the play is Legal. The
spec says one thing and the implementation does another, and the proof is
what noticed. Without it, every guarantee stated with Legal would now be
describing a game the engine doesn't play.
Change two: the same rule, in both¶
Put Rules.lean back (git restore Games/LoL/Rules.lean). This time, put
the new body into valkyrieForced itself: add || hand.contains .holmgang
inside its brackets, so it reads like valkyrieForcedWide above. Don't
touch check or Legal. Run the two commands again.
Everything passes. The tests, the random games, and every proof,
including check_none_iff_legal, valkyrie_must_be_played and
offered_plays_are_accepted.
That is because valkyrieForced is one definition, used in three places:
by Legal, the statement; by check, the implementation; and by enabled,
which decides what the engine offers. Change it, and all three change
together. And the proof of check_none_iff_legal doesn't depend on what
valkyrieForced says, only on both sides using it: it splits on
valkyrieForced … = true without ever looking inside.
What about the other way, giving Legal its own copy of the new rule, as
in change one, and changing Legal to use valkyrieForcedWide as well as
check? Try it. check_none_iff_legal still fails, even though what it
states is now true again: its proof splits on valkyrieForced, which no
longer appears in either side. Repair it by splitting on
valkyrieForcedWide instead, and the build gets further, to
Guarantees.lean, where offered_plays_are_accepted and
valkyrie_must_be_played fail. The first
is false again: enabled still uses the old rule, so the engine offers
plays it refuses. The second needs a new step, since it talks about the old
valkyrieForced. Keeping a rule in one place is what keeps all of these in
step.
What nothing checks
After change two, every test and proof passes, and yet the game tells
players the wrong rule. The card's ability text and the rules text in
Game.lean still say the Valkyrie is forced only with Loki or the
Trader. No proof is about words on screen. If you keep a rule change,
update Card.ability and rulesText yourself, and the table in
Rules.lean's summary.
Change three: fewer Raiders¶
Now change how many cards there are. In Card.count, make it
raider => 3. The deck has two fewer cards. Run the commands again.
Everything passes. Look at how the guarantee about cards is stated:
/-- All sixteen cards are in play, each as often as in the full deck. -/
def Complete (s : State) : Prop := ∀ c, s.cards.count c = fullDeck.count c
/-- **No card is ever created or destroyed.**
If all sixteen cards are in play before an accepted play, they are all still in
play afterwards: in hands, discard piles, the deck, or set aside. That includes
plays that end a round, after which a new round is dealt from the full deck,
and nods (the last of which ends the turn). -/
theorem accepted_moves_keep_the_full_deck (full : Complete s) (accepted : step s a m = .ok (s', evs)) :
Complete s' := by
rcases accepted_move accepted with ⟨act, -, -, played⟩ | ⟨-, -, -, rfl, -⟩
· -- A play is a legal play by a real player, carried out by `apply`, which
-- keeps every card (each part is checked in the bookkeeping).
obtain ⟨-, real, -, legal, result, -⟩ := accepted_play played
rw [result]
exact apply_complete full legal real
· -- A nod moves no cards; the last one ends the turn, which keeps them all.
exact finishPlay_complete _ full
It doesn't say "sixteen". It says every card is there as often as in
fullDeck, whatever fullDeck is. The tests are written the same way: they
compare with fullDeck, not with a number. So a guarantee stated in terms
of the rules' own definitions survives changes to them. Only the comments
and some test names still say "sixteen", and nothing checks those.
(Try raider => 0 too, if you like. The tests that build their own hands
with Raiders still pass: the rules never check that the cards on the table
came from the deck. They only promise to keep whatever cards are there.)
Exercises¶
- Let the Raider name the Raider. In
checkGuess, delete the line| some .raider => …. Predict what fails, then runmake testandmake proofs. -
Now change
GuessOkto match, as below, and run them again. What still fails, and why? (The two definitions are written here under other names, so that this course can check them.)def checkGuessAnyName (a : Action) : Option String := if a.card = .raider then match a.guess with | none => some "name a card" | some _ => none else if a.guess.isSome then some "only the Raider names a card" else none def GuessOkAnyName (a : Action) : Prop := if a.card = .raider then ∃ g, a.guess = some g else a.guess = none -
After exercise 2, can a player in the browser actually name the Raider? Look at
guessChoicesinGame.lean.
Put Rules.lean back afterwards.
Answers
- One scenario test fails, raider: cannot name raider: it was written
for the old rule. In the proofs,
checkGuess_none_ifffails, in the case where the card is the Raider and the guess is the Raider. The goal Lean is left with is⊢ False: in that case, the statement says something false, sincechecknow accepts the guess andGuessOkstill forbids it. A goal ofFalseis Lean's clearest way of telling you a statement doesn't hold. checkGuess_none_iffpasses again: the check and the statement agree. The scenario test still fails, since it's about the old rule; you'd change it to expect the guess to be accepted. And a proof in the bookkeeping fails,offered_aims_are_ok, which shows that every play the engine offers fitsAimOk. Its statement is still true. But its proof supplied a piece of evidence, that the guess isn't the Raider, that the newGuessOkno longer asks for, and Lean refuses the mismatched shape. Removing that piece fixes it. A weaker statement can still break a proof written for the stronger one.- No.
guessChoicesis the list of cards the engine offers the Raider to name, and it still leaves out the Raider. The rules would accept the guess, but nobody is offered it. That's allowed byoffered_plays_are_accepted, which promises only that what is offered is accepted, not that everything accepted is offered.