1 A First Look at AlgorithmsWe will now begin looking at PlusCal algorithms. The tutorial describes most of the PlusCal language, but you might want to download and save the PlusCal manual. It contains a few features that aren't described here and provides more detailed descriptions of ones that are.
Our first algorithm is in specification MODULE
line or put their specification after the end of the
module, where it's ignored.)
Again, ignore the EXTENDS statement. The algorithm is
contained in a comment, which is enclosed by the delimiters
(* and *) . (You can also put such comments
inside the algorithm.) The algorithm itself begins with:
The body of the algorithm begins with a declaration of all its variables. The statement
To execute the algorithm, we must first run the PlusCal to TLA+ translator. You can run the Translate PlusCal Algorithm command from the Toolbox's File menu, but it's much easier to just type Control-T. This will add the resulting TLA+ specification to the module right below the comment containing the algorithm. Don't try to read that specification.
As you've seen, TLC is run on a model. This time, you will
create a new model. As before, click on TLC Model Checker
in the Toolbox's top menu bar, but this time choose New
Model . This raises the New model window that
allows you to choose a name for the model. I'm usually happy
with the default name Model_1. Clicking
OK opens the Model Overview sub-tab of the new
model. Ignore what's on it. Run TLC by clicking on the
icon
in the upper-left corner of the sub-tab. TLC should run for a
few seconds and then raise the model's Model Checking Results
sub-tab. The output of the PlusCal treats all mathematical values and operations on them the same, which is the way most programming languages treat only numbers and a handful of other kinds of values. Check this out by doing the following exercise.
Exercise 1 Modify algorithm --algorithm AnyName { variable x = {"a","b"} ; { x := x \cup {"c"} ; print x } } "c" to the set
{"a", "b"} , it removes the "a" from that
set.
Show
the answer.
--algorithm AnyName { variable x = {"a","b"} ; { x := x \ {"a"} ; print x ; } } 2 Assigning to a Tuple
Now let's try setting variable x = <<1, 2, 3>> ; { x[3] := x[2] + 4 ; print x }
Save the module, run the translator, and run TLC. It should print
Now modify the algorithm by adding the green code shown here:
Were you surprised? You shouldn't have been.
PlusCal treats all mathematical values the same. If the
algorithm had initialized back to top
|