Dining Philosophers
The classic concurrency problem with 5 philosophers and 5 forks. The system declares three properties:
noDeadlock— some philosopher can always act.mutualExclusion— adjacent philosophers never eat simultaneously.progress— at least one philosopher can eventually eat.
Run#
saple analyze examples/dining_philosophers.splNo property is reported violated and the model declares no patterns, so the report ends with No vulnerabilities detected.
The + in TryForks nondeterministically chooses the fork order — the classic deadlock arises if every philosopher grabs the left fork first.
Known limitation
In expressions, Philosopher(i).state is read as Philosopher.state: the instance argument is dropped (see Expressions). Guards and properties of the model below that select an instance this way do not distinguish the instances today.
Complete model#
examples/dining_philosophers.spl, as shipped:
// Dining Philosophers — Deadlock and Starvation Analysis
//
// Classic concurrency problem modeled in SPL process equations: five
// philosophers sit at a round table with one fork between each pair; a
// philosopher needs both neighbouring forks to eat.
// Demonstrates: parallel composition, guarded choice, deadlock detection,
// behavioral equation recursion, multi-agent interaction via shared resources.
//
// What the analysis should find: the system declares three properties
// (noDeadlock, mutualExclusion, progress) and no patterns; `saple analyze`
// reports no violation ("No vulnerabilities detected.").
// What a philosopher is doing, and whether a fork is on the table.
type PhilState = { THINKING, HUNGRY, EATING };
type ForkState = { FREE, TAKEN };
// ─── Actions ───
// Each transition makes one small state change; a philosopher's meal is a
// sequence of these fine-grained steps.
// A thinking philosopher becomes hungry.
transition getHungry(id : Int) {
guard: Philosopher(id).state == THINKING ;
effect: Philosopher(id).state := HUNGRY ;
}
// A hungry philosopher takes the left fork (fork `id`) if it is free.
transition pickLeft(id : Int) {
guard: Philosopher(id).state == HUNGRY &&
Fork(id).state == FREE ;
effect: Fork(id).state := TAKEN ;
}
// A hungry philosopher takes the right fork (fork `id + 1`, wrapping) if it is free.
transition pickRight(id : Int) {
guard: Philosopher(id).state == HUNGRY &&
Fork((id + 1) % numPhil).state == FREE ;
effect: Fork((id + 1) % numPhil).state := TAKEN ;
}
// With both forks taken, the philosopher eats.
transition startEating(id : Int) {
guard: Fork(id).state == TAKEN &&
Fork((id + 1) % numPhil).state == TAKEN ;
effect: Philosopher(id).state := EATING ;
}
// An eating philosopher puts the left fork back.
transition putLeftFork(id : Int) {
guard: Philosopher(id).state == EATING ;
effect: Fork(id).state := FREE ;
}
// An eating philosopher puts the right fork back.
transition putRightFork(id : Int) {
guard: Philosopher(id).state == EATING ;
effect: Fork((id + 1) % numPhil).state := FREE ;
}
// The meal ends; the philosopher thinks again.
transition finishEating(id : Int) {
guard: Philosopher(id).state == EATING ;
effect: Philosopher(id).state := THINKING ;
}
// ─── Agents ───
// One philosopher; `id` is the seat number.
agent Philosopher(id : Int) {
attributes {
state : PhilState = THINKING ;
}
process {
// Cycle: think → hungry → acquire forks → eat → release → repeat
// Think: get hungry, then try for the forks.
Think = getHungry(id) . TryForks ;
// TryForks: pick the forks up in either order (nondeterministic choice).
TryForks = pickLeft(id) . pickRight(id) . Eat
+ pickRight(id) . pickLeft(id) . Eat ;
// Eat: start eating once both forks are held.
Eat = startEating(id) . Release ;
// Release: put both forks back, stop eating, and think again.
Release = putLeftFork(id) . putRightFork(id) . finishEating(id) . Think ;
}
}
// One fork: a passive resource whose state the philosophers change.
agent Fork(id : Int) {
attributes {
state : ForkState = FREE ;
}
process {
// A fork takes no actions of its own.
Idle = done ;
}
}
// ─── Environment ───
// Shared state: the number of seats.
schema DiningTable {
attributes {
numPhil : Int = 5 ;
}
invariants {
// A table needs at least two philosophers.
numPhil >= 2 ;
}
}
// ─── System ───
// Five philosophers and five forks around the table.
system DiningPhilosophers {
schema: DiningTable ;
agents {
Philosopher(i) * 5 ;
Fork(i) * 5 ;
}
init {
numPhil == 5 ;
}
properties {
// Deadlock freedom: some philosopher can always act
noDeadlock : exists (i : Int) in [0..numPhil) .
Philosopher(i).state == THINKING ||
Philosopher(i).state == EATING ||
(Philosopher(i).state == HUNGRY &&
(Fork(i).state == FREE || Fork((i + 1) % numPhil).state == FREE)) ;
// Mutual exclusion: adjacent philosophers never eat simultaneously
mutualExclusion : forall (i : Int) in [0..numPhil) .
!(Philosopher(i).state == EATING &&
Philosopher((i + 1) % numPhil).state == EATING) ;
// Progress: at least one philosopher can eventually eat
progress : exists (i : Int) in [0..numPhil) .
Philosopher(i).state != HUNGRY ||
Fork(i).state == FREE ;
}
}