|
| 1 | +The prisoner puzzle. A set of N prisoners, all in solitary cells, is |
| 2 | +offered a game. One of them is randomly selected and placed in a |
| 3 | +special cell. This cell has a lamp and prisoners can turn it off or |
| 4 | +on. Afterwards the prisoner is returned back to their cell. |
| 5 | + |
| 6 | +At any point a prisoner can announce to the warden that they have all |
| 7 | +been in the cell at least once. If they are right everyone is |
| 8 | +released. If they are wrong the game ends and they remain in prison |
| 9 | +forever. |
| 10 | + |
| 11 | +The prisoners are allowed to communicate and design a strategy before |
| 12 | +the game starts. Afterwards they can no longer communicate. |
| 13 | + |
| 14 | +There is a variant of the puzzle where the initial state of the light |
| 15 | +in the cell is not known. |
| 16 | + |
| 17 | +In either case the strategy is: |
| 18 | + |
| 19 | + * One prisoner is designated as the counter. |
| 20 | + |
| 21 | + * Other prisoners, when they enter the cell turn the light on if its |
| 22 | + off. They do this at most once (or twice in the variant). |
| 23 | + |
| 24 | + * If the designated counter sees the light, they turn it off and |
| 25 | + increment their count. Once they count to N (or 2N - 1 for the |
| 26 | + variant), they know that everyone (including themselves) must have |
| 27 | + entered the cell and can announce victory. |
| 28 | + |
| 29 | +Note: This puzzle is a variant of other prisoner puzzle here that I |
| 30 | +was not aware of when I wrote this model. |
| 31 | +https://github.com/tlaplus/Examples/tree/master/specifications/Prisoners |
| 32 | + |
| 33 | +The setup is different: here there is only one switch, and the initial |
| 34 | +status of it might be known, and the prisoners are not required to do |
| 35 | +anything if they enter the room. |
| 36 | + |
| 37 | +The solution is the same however (except we don't have the busy-work |
| 38 | +switch), and our models are very similar. |
| 39 | + |
| 40 | +---- MODULE Prisoner ---- |
| 41 | + |
| 42 | +EXTENDS FiniteSets, Naturals |
| 43 | + |
| 44 | +CONSTANTS |
| 45 | + \* The set of prisoners playing the game. Has to be at least one. |
| 46 | + Prisoner, |
| 47 | + |
| 48 | + \* Configuration if the initial state of the light is "off" or |
| 49 | + \* unknown. The puzzle is harder (takes more steps) if we don't know |
| 50 | + \* the initial state. |
| 51 | + Light_Unknown |
| 52 | + |
| 53 | +ASSUME Light_Unknown \in BOOLEAN |
| 54 | + |
| 55 | +VARIABLES |
| 56 | + \* The counter's current count |
| 57 | + count, |
| 58 | + |
| 59 | + \* If the announcement has been made |
| 60 | + announced, |
| 61 | + |
| 62 | + \* How often other prisoners have signalled |
| 63 | + signalled, |
| 64 | + |
| 65 | + \* Current status of the light |
| 66 | + light, |
| 67 | + |
| 68 | + \* The warden's view on who has actually been to the cell |
| 69 | + has_visited |
| 70 | + |
| 71 | +vars == <<count, announced, signalled, light, has_visited>> |
| 72 | + |
| 73 | +------------------------------------------------------------------------------ |
| 74 | + |
| 75 | +\* The strategy differs slightly if the initial state of the light is |
| 76 | +\* known or not. If it's "off" then a simple count to N is |
| 77 | +\* sufficient. If the state is unknown we count to 2N - 1, and each |
| 78 | +\* prisoner will signal up to two times. |
| 79 | + |
| 80 | +SignalLimit == IF Light_Unknown THEN 2 ELSE 1 |
| 81 | + |
| 82 | +VictoryThreshold == |
| 83 | + IF Light_Unknown |
| 84 | + THEN Cardinality(Prisoner) * 2 - 1 |
| 85 | + ELSE Cardinality(Prisoner) |
| 86 | + |
| 87 | +------------------------------------------------------------------------------ |
| 88 | + |
| 89 | +\* We pick somebody at random to be the counter |
| 90 | +DesignatedCounter == CHOOSE p \in Prisoner: TRUE |
| 91 | + |
| 92 | +\* The other prisoners |
| 93 | +NormalPrisoner == Prisoner \ {DesignatedCounter} |
| 94 | + |
| 95 | +\* The only thing to note here is that the count starts at one. Since |
| 96 | +\* only the counter will make an announcement if they are in the cell, |
| 97 | +\* this 1 means they have already counted themselves. We could also |
| 98 | +\* model this with a separate variable to note when the counter has |
| 99 | +\* visited the cell for the very first time, but this is not |
| 100 | +\* necessary. |
| 101 | +Init == |
| 102 | + /\ count = 1 |
| 103 | + /\ announced = FALSE |
| 104 | + /\ signalled = [ p \in NormalPrisoner |-> 0 ] |
| 105 | + /\ \/ Light_Unknown /\ light \in { "off", "on" } |
| 106 | + \/ ~Light_Unknown /\ light = "off" |
| 107 | + /\ has_visited = {} |
| 108 | + |
| 109 | +------------------------------------------------------------------------------ |
| 110 | + |
| 111 | +\* The action taken by the designated counter, if they are placed in |
| 112 | +\* the cell |
| 113 | +\* |
| 114 | +\* Note: we could simplify this change the IF to just be a |
| 115 | +\* precondition to the action, and while that would be a more elegant |
| 116 | +\* spec, I think this better models the decision procedure of the |
| 117 | +\* prisoner. |
| 118 | +CounterAction(p) == |
| 119 | + /\ p = DesignatedCounter |
| 120 | + /\ IF light = "on" |
| 121 | + THEN |
| 122 | + /\ light' = "off" |
| 123 | + /\ count' = count + 1 |
| 124 | + ELSE |
| 125 | + UNCHANGED <<light, count>> |
| 126 | + /\ announced' = (count' >= VictoryThreshold) |
| 127 | + /\ UNCHANGED <<signalled>> |
| 128 | + |
| 129 | +\* The action taken by the other prisoners, if they are placed in the |
| 130 | +\* cell |
| 131 | +\* |
| 132 | +\* Same note on the IF applies here. |
| 133 | +StandardAction(p) == |
| 134 | + /\ p \in NormalPrisoner |
| 135 | + /\ IF light = "off" /\ signalled[p] < SignalLimit |
| 136 | + THEN |
| 137 | + /\ light' = "on" |
| 138 | + /\ signalled' = [signalled EXCEPT ![p] = @ + 1] |
| 139 | + ELSE |
| 140 | + UNCHANGED <<light, signalled>> |
| 141 | + /\ UNCHANGED <<count, announced>> |
| 142 | + |
| 143 | +\* The action performed by the warden: place a prisoner in the cell |
| 144 | +\* and maintain our own view of who's been selected (so we can judge |
| 145 | +\* if a victory announcement is correct) |
| 146 | +WardenAction(p) == |
| 147 | + \* Put one of the prisoners in the cell |
| 148 | + /\ \/ CounterAction(p) |
| 149 | + \/ StandardAction(p) |
| 150 | + |
| 151 | + \* Maintain the warden's view |
| 152 | + /\ has_visited' = has_visited \union {p} |
| 153 | + |
| 154 | +Next == \E p \in Prisoner: WardenAction(p) |
| 155 | + |
| 156 | +Spec == |
| 157 | + /\ Init |
| 158 | + /\ [][Next]_vars |
| 159 | + |
| 160 | + \* Base assumption in the game: the warden eventually has to choose |
| 161 | + \* everyone in a way progress is possible |
| 162 | + /\ \A p \in Prisoner: WF_vars(WardenAction(p)) |
| 163 | + |
| 164 | +------------------------------------------------------------------------------ |
| 165 | + |
| 166 | +\* Goal of the game: eventually the prisoners will win |
| 167 | +Terminating == <>announced |
| 168 | + |
| 169 | +------------------------------------------------------------------------------ |
| 170 | + |
| 171 | +\* Type invariant. Note it's possible to over-count in the lights |
| 172 | +\* unknown version, if the initial state of the light is "on". Hence |
| 173 | +\* the slightly more relaxed upper bound on count. |
| 174 | +TypeOK == |
| 175 | + /\ count \in 1 .. VictoryThreshold + 1 |
| 176 | + /\ announced \in BOOLEAN |
| 177 | + /\ signalled \in [ NormalPrisoner -> 0 .. 2 ] |
| 178 | + /\ light \in {"off", "on"} |
| 179 | + /\ has_visited \subseteq Prisoner |
| 180 | + |
| 181 | +\* Invariant to make sure victory is never declared in error |
| 182 | +VictoryOK == announced => (has_visited = Prisoner) |
| 183 | + |
| 184 | +==== |
0 commit comments