Skip to content

Add Dining Philosophers example - #28

Merged
muenchnerkindl merged 3 commits into
tlaplus:masterfrom
jthemphill:dp
Mar 8, 2021
Merged

muenchnerkindl merged 3 commits into
tlaplus:masterfrom
jthemphill:dp

Conversation

@jthemphill

Copy link
Copy Markdown
Contributor

I've been trying to learn TLA+ to suss out a rare bug in my code. On https://learntla.com/temporal-logic/operators/ , I came across a toy Dining Philosophers problem. It took me several hours to make everything work and build something free of deadlocks, livelocks, or starvation, so I'd like to add my knowledge to the bank.

This model solves the problem via Dijkstra's resource hierarchy solution. You should be able to run it with no changes via VSCode or the command-line, or with minimal changes via TLA toolbox.

@muenchnerkindl

Copy link
Copy Markdown
Collaborator

Thank you very much for the changes. A few more comments:

  • The PlusCal algorithm and the TLA+ are not in sync. In particular, there is an assignment to the variable `requesting' that is not declared in the algorithm but does appear in the TLA+ spec. However, since its value is never tested, it appears that it is unnecessary. Indeed, I removed it and the properties (see below) still hold.
  • The liveness property should be strengthened to \A p \in 1..NP: []<>(~hungry[p]), ensuring that philosophers will eat repeatedly (and it holds).
  • I suggest adding one or two invariants to check as well, for example type correctness and exclusive access between neighbors (even if it's pretty obvious from the protocol), as follows:

TypeOK ==
/\ forks \in [ 1..NP -> [holder : 1..NP, clean : BOOLEAN] ]
/\ hungry \in [ 1..NP -> BOOLEAN ]
/\ pc \in [ 1..NP -> {"Loop", "Eat", "Think"}]

ShareFork(p,q) == {LeftFork(p), RightFork(p)} \cap {LeftFork(q), RightFork(q)} # {}
ExclusiveAccess == \A p,q \in 1..NP : p # q /\ ShareFork(p,q) => ~(pc[p] = "Eat" /\ pc[q] = "Eat")

If you agree, please make this changes and I'll merge the PR.

Best regards,
Stephan

@muenchnerkindl

Copy link
Copy Markdown
Collaborator

Thank you very much for your contribution! Merged into tlaplus/Examples.

@muenchnerkindl
muenchnerkindl merged commit d4b60a1 into tlaplus:master Mar 8, 2021
@jthemphill
jthemphill deleted the dp branch March 8, 2021 08:19
lemmy added a commit that referenced this pull request Nov 5, 2026
The Summary of TLA+ gives SUBSET, UNION, \cup, and \ precedence 8-8.
Equal precedence leaves the binding unspecified, and TLA+ rejects the
expression. SANY has given SUBSET and UNION precedence 10-13, under
which SUBSET S \ T parses as (SUBSET S) \ T and A \cup UNION B as
A \cup (UNION B). tlaplus#1508 restores the documented ranges, so the
unparenthesized forms become syntax errors. The parentheses keep the
binding SANY has been using.

Related to Github RFC issue #28
tlaplus/rfcs#28

See Github pull request #1508
tlaplus/tlaplus#1508

Co-authored-by: Grok 4.7 <noreply@x.ai>
Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants