- PVSM.RU - https://www.pvsm.ru -
Many programmers struggle when using formal methods to solve problems within their programs, as those methods, while effective, can be unreasonably complex. To understand why this happens, let’s use the model checking method [1] to solve a relatively easy puzzle:
You’re in a hallway with seven doors on one side leading to seven rooms. A cat is hiding in one of these rooms. Your task is to catch the cat. Opening a door takes one step. If you guess the correct door, you catch the cat. If you do not guess the correct door, the cat runs to the next room.
Some tools can solve problems like this. For example, SMT solvers [2], which are used frequently in these cases, use predicate logic to solve the problem, but that requires using an array, which is inconvenient for programmers and often results in an unnecessarily complex sequence of actions. TLA+ [3], on the other hand, uses temporal logic, which allows a program to use the state of the system both at the current and next step in one statement.
CatDistr' = CatDistr {n}
This condition states that after checking behind door N, the number of doors where the cat can be located, coincides with the set of rooms where N was taken from. (If the cat was behind door N, of course, he’s been caught.)
In an imperative programming language, this creates a new variable calculated from the old one.
The cat’s position is determined by a variable within the set of all possible rooms, rather than specific room as in a simulation modeling system. Formal methods of programming take into account the plurality of possible values, instead of the current value. This is often too complex for beginning developers, so simplifying this system would help those developers work more quickly.
The program consists of declarations and statements (predictors), which describe:
There may be also auxiliary predicates (e.g. parameters) involved.
---- MODULE cat -------------------------------------------------------------
EXTENDS Integers
CONSTANTS Doors
VARIABLES CatDistr, LastDoor
Init ==
/ CatDistr = 0..Doors
/ LastDoor = -1
OpenDoor(n) ==
/ CatDistr' =
0..Doors intersect UNION { {x-1, x+1} : x in CatDistr {n} }
/ LastDoor' = n
Next == E door in 0..Doors : OpenDoor(door)
CatWalk == CatDistr /= {}
=============================================================================
In that example:
Here is a more clear variant:
Next == OpenDoor(0) / OpenDoor(1) / ... / OpenDoor(Doors)
In this case, the code works with a fixed number of rooms, making the code parameterized.
CONSTANTS
Doors = 6
INIT Init
NEXT Next
INVARIANT CatWalk
This configuration includes the initial condition, the number of rooms, the conditions for a state change, and a verifiable test.
You can run this model from the command line using this command:
tlc2 -config cat.cfg cat.tla.
TLA+ has an advanced GUI and is launched by tla-toolbox command. Unfortunately, it does not understand .cfg files, so the model parameters must be configured through the menu.
Creating this particular program was fairly simple, but in many cases, applying formal methods will require much larger models and the use of various language constructions. We hope that solving simpler puzzles such as this will provide an easy and interesting way to apply formal methods the work projects.
Автор: potan
Источник [6]
Сайт-источник PVSM.RU: https://www.pvsm.ru
Путь до страницы источника: https://www.pvsm.ru/programmirovanie/327065
Ссылки в тексте:
[1] model checking method: https://en.wikipedia.org/wiki/Model_checking
[2] SMT solvers: https://en.wikipedia.org/wiki/Satisfiability_modulo_theories
[3] TLA+: https://en.wikipedia.org/wiki/TLA%E2%81%BA
[4] here: https://ellie-app.com/6h6hkjT6NzRa1
[5] here: https://learntla.com/introduction/
[6] Источник: https://habr.com/ru/post/462397/?utm_campaign=462397&utm_source=habrahabr&utm_medium=rss
Нажмите здесь для печати.