Nim and the Sprague-Grundy theorem #
This file contains the definition for nim for any ordinal o
. In the game of nim o₁
both players
may move to nim o₂
for any o₂ < o₁
.
We also define a Grundy value for an impartial game G
and prove the Sprague-Grundy theorem, that
G
is equivalent to nim (grundyValue G)
.
Finally, we prove that the grundy value of a sum G + H
corresponds to the nimber sum of the
individual grundy values.
Implementation details #
The pen-and-paper definition of nim defines the possible moves of nim o
to be Set.Iio o
.
However, this definition does not work for us because it would make the type of nim
Ordinal.{u} → SetTheory.PGame.{u + 1}
, which would make it impossible for us to state the
Sprague-Grundy theorem, since that requires the type of nim
to be
Ordinal.{u} → SetTheory.PGame.{u}
. For this reason, we
instead use o.toType
for the possible moves. You can use to_left_moves_nim
and
to_right_moves_nim
to convert an ordinal less than o
into a left or right move of nim o
, and
vice versa.
The definition of single-heap nim, which can be viewed as a pile of stones where each player can take a positive number of stones from it on their turn.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Turns an ordinal less than o
into a left move for nim o
and vice versa.
Equations
- SetTheory.PGame.toLeftMovesNim = o.enumIsoToType.trans (Equiv.cast ⋯)
Instances For
Turns an ordinal less than o
into a right move for nim o
and vice versa.
Equations
- SetTheory.PGame.toRightMovesNim = o.enumIsoToType.trans (Equiv.cast ⋯)
Instances For
Alias of SetTheory.PGame.moveLeft_nim
.
Alias of SetTheory.PGame.moveRight_nim
.
A recursion principle for left moves of a nim game.
Equations
- SetTheory.PGame.leftMovesNimRecOn i H = ⋯.mpr (H ((Ordinal.typein fun (x1 x2 : o.toType) => x1 < x2).toRelEmbedding ((Equiv.cast ⋯).symm i)) ⋯)
Instances For
A recursion principle for right moves of a nim game.
Equations
- SetTheory.PGame.rightMovesNimRecOn i H = ⋯.mpr (H ((Ordinal.typein fun (x1 x2 : o.toType) => x1 < x2).toRelEmbedding ((Equiv.cast ⋯).symm i)) ⋯)
Instances For
nim 1
has exactly the same moves as star
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The Grundy value of an impartial game is recursively defined as the minimum excluded value (the infimum of the complement) of the Grundy values of either its left or right options.
This is the ordinal which corresponds to the game of nim that the game is equivalent to.
This function takes a value in Nimber
. This is a type synonym for the ordinals which has the same
ordering, but addition in Nimber
is such that it corresponds to the grundy value of the addition
of games. See that file for more information on nimbers and their arithmetic.
Instances For
The Sprague-Grundy theorem states that every impartial game is equivalent to a game of nim, namely the game of nim corresponding to the game's Grundy value.
The Grundy value of the sum of two nim games equals their nimber addition.