考慮解決100 prisoners and a lightbulb問題的標準策略。這是我嘗試它Dafny型號:用Dafny證明100個囚犯和一個燈泡
method strategy<T>(P: set<T>, Special: T) returns (count: int)
requires |P| > 1 && Special in P
ensures count == (|P| - 1)
decreases *
{
count := 0;
var I := {};
var S := {};
var switch := false;
while (count < (|P|-1))
invariant count <= (|P|-1)
invariant count > 0 ==> Special in I
invariant Special !in S && S < P && S <= I && I <= P
decreases *
{
var c :| c in P;
I := I + {c};
if c == Special {
if switch == true {
switch := false;
count := count + 1;
}
} else {
if c !in S && switch == false {
S := S + {c};
switch := true;
}
}
}
assert(I == P);
}
它失敗了,但是,要證明到底I == P
。爲什麼?我可能需要加強循環不變甚至更進一步,但無法想象從哪裏開始...