In "Practical TLA+" by Hillel Wayne, Chapter 3 goes over an implementation of the Knapsack Problem in PlusCal.
Although I understand the implementation, I don't understand how to run it. He mentions:
Since we’re adding a PlusCal spec, remember to remove “evaluate constant expression” and set “What is the behavior spec?” to “Temporal formula.” When you run this, you should get something like what is shown in Figure 3-3.
What's the "Temporal Formula"? Where is it in the Model menu? Alternatively, how would I run this using the VSCode TLA+ plugin? I feel like I missed some details I need to actually run/verify this specification.
TLA+ specifications can specify different things, not only iterative algorithms. The
knapsack.tla
that you have linked specifies the problem and the definition of an optimal solution without describing a good way to really find it with a computer.[Items -> 0..4]
is the set of all possible mappings fromItem
to the sizes0..4
. The optimal solution is in the subset that fits in a knapsack of sizeCapacity
, hence the expressionKnapsackSize(sack, itemset) <= Capacity
.Another TLA+ definition defines which one of the knapsacks is the best of them all in an impressively high-level way:
It basically says: among all of the valid knapsacks, the best knapsack is the one that is better (higher
KnapsackValue
) than all others.What you are looking for is an specification of an iterative algorithm that matches this solution specification. The specification of the algorithm will have
Init
andNext
formulas and then you can check that it eventually terminates — usually calledTermination
— and the state variable storing the knapsack would then contain theBestKnapsack
.But you can "run" these definitions by typing an expression in the "Evaluate Constant Expression" box.
The author also wrote two refined definitions of
BestKnapsack
—BestKnapsackOne
andBestKnapsackTwo
. They are still functional and we call them refined because they are getting closer to something implementable in an iterative algorithm.You can evaluate them just like done above for
BestKnapsack(HardcodedItemSet)
. They are correct refinements if they match the output ofBestKnapsack
for all possible inputs. Since this is model checking, "all possible inputs" is qualified with "up to a certain size" and we assume (or prove) that the solution extends to any size.