如何将公式转换为TLA +代码

时间:2016-04-03 23:08:19

标签: tla+

我已经写了一篇关于河内塔问题的TLA +规范:

TEX

enter image description here

ASCII

------------------------------- MODULE Hanoi -------------------------------

EXTENDS Sequences, Integers
VARIABLE A, B, C


CanMove(x,y) == /\ Len(x) > 0 
                /\ IF Len(y) > 0 THEN Head(y) > Head(x) ELSE TRUE

Move(x,y,z) == /\ CanMove(x,y)
               /\ x' = Tail(x)
               /\ y' = <<Head(x)>> \o y
               /\ z' = z

Invariant == C /= <<1,2,3>>   \* When we win!                           

Init == /\ A = <<1,2,3>>
        /\ B = <<>>
        /\ C = <<>>

Next == \/ Move(A,B,C) \* Move A to B
        \/ Move(A,C,B) \* Move A to C
        \/ Move(B,A,C) \* Move B to A
        \/ Move(B,C,A) \* Move B to C
        \/ Move(C,A,B) \* Move C to A
        \/ Move(C,B,A) \* Move C to B
=============================================================================

当我将Invariant公式指定为不变量时,TLA模型检查器将为我解决这个难题。

我想让它稍微冗长一点,理想情况下,我不想将未更改的变量传递给Move(),但我无法弄清楚如何。我想做的是写

Move(x,y) == /\ CanMove(x,y)
             /\ x' = Tail(x)
             /\ y' = <<Head(x)>> \o y
             /\ UNCHANGED (Difference of and {A,B,C} and {y,x})

我如何用TLA语言表达这一点?

1 个答案:

答案 0 :(得分:3)

而不是变量A,B,C,您应该有一个序列towers,其中塔是索引。这也具有塔的数量通用的优点。您的Next公式也会更短:

CanMove(i,j) == /\ Len(towers[i]) > 0 
                /\ Len(towers[j]) = 0 \/ Head(towers[j]) > Head(towers[i])

Move(i, j) == /\ CanMove(i, j)
              /\ towers' = [towers EXCEPT ![i] = Tail(@),
                                          ![j] = <<Head(towers[i])>> \o @]

Init == towers = << <<1,2,3>>, <<>>, <<>> >> \* Or something more generic

Next == \E i, j \in DOMAIN towers: i /= j /\ Move(i, j)

或者,如果您想继续使用字母,可以使用记录而不是towers的序列,而您在建议的规范中需要更改的是:

Init == towers = [a |-> <<1, 2, 3>>, b |-> <<>>, c |-> <<>>]