05-01

Chapter 05 · Just enough set theory

Sets and quantifiers as queries

  • 45 min core
  • Core track
  • Skills: abstraction, intent

00Recall

One card from chapter 02 on reading /\ and \/ as filters, one from 03-05 on what a cfg that names only TypeOK has checked, and one from 03-03 on a pass with nothing in it. All three come back today: \E and \A filter a whole set the way /\ and \/ filter states, a cfg that names only TypeOK returns in the state counts, and a pass with nothing in it returns in the note on HouseFull in a theatre with no seats.

02-04-a from Just enough logic

Read /\ and \/ as filters on candidate states or steps. What does each do?

Show answer

P /\ Q lets a candidate through only if both pass it, so adding a conjunct can only narrow the set. P \/ Q lets it through if either passes it, so adding a disjunct can only widen it. P => Q lets through every candidate that P rejects.

03-05-a from TypeOK, and the check you didn't configure

The cfg names only INVARIANT TypeOK, and TLC finds no violation. May you say the thermostat keeps the room comfortable?

Show answer

No, for two reasons. TLC never checked Comfortable, because the cfg did not name it. And even a cfg that names it gives "no violation found in this model": one finite model, not a proof.

03-03-a from Break it on purpose

TLC reports no violation and zero distinct states. What has it shown about your invariants?

Show answer

Nothing. With no reachable states, every invariant holds vacuously.

Skip recall Recall is never required.

01Commit

A box office sells seats. A teammate modelled the sales as one set of pairs: <<aisle, g1>> in the set means guest g1 holds the aisle seat. Here is their spec, specs/SeatBooking.tla, cut to the parts that matter:

TLA+
TypeOK is defined as bookings subset of or equal to Seats cross Guests

Init is defined as bookings = {}

\* The teammate's guard: "this guest has not booked this seat yet".
Book(s, g) is defined as
    and tuple ofs, gend tuple not in bookings
    and bookingsprime = bookings union {tuple ofs, gend tuple}

Cancel(s, g) is defined as
    and tuple ofs, gend tuple in bookings
    and bookingsprime = bookings set minus {tuple ofs, gend tuple}

Six pieces of notation, each with its spoken reading. {} is the empty set. <<s, g>> is a pair, read "the tuple s, g". \X is read "cross": Seats \X Guests is the set of every pair whose first part is a seat and whose second part is a guest. \cup is read "union", S \ T is read "S minus T", and \notin is read "is not an element of". So Book(s, g) adds the pair, as long as that exact pair is not there yet, and Cancel(s, g) takes it out again.

The cfg uses two seats, aisle and window, and two guests, g1 and g2. It checks NoDoubleBooking: no seat is sold to two different guests.

Commit a

TLC checks NoDoubleBooking on the teammate's SeatBooking. What will it report?

  1. No violation: the guard stops a seat from being sold twice.
  2. A violation: one seat ends up sold to both guests.
  3. An error: TLC cannot check a claim about pairs inside a set.

Answer A violation: one seat ends up sold to both guests.

Now the fix the lesson derives. The guard should ask whether anyone holds the seat: ~\E h \in Guests : <<s, h>> \in bookings. specs/GuardedSeatBooking.tla is the same spec with that guard.

Commit b

TLC checks GuardedSeatBooking with the same two seats and two guests. How many distinct states will it report?

Your answer is a whole number.

Answer 9

02Lab

Run the teammate's box office, then the fixed one:

shell
./tla check chapters/05-just-enough-set-theory/specs/SeatBooking.cfg
./tla check chapters/05-just-enough-set-theory/specs/GuardedSeatBooking.cfg
Expected output 05-just-enough-set-theory/specs/SeatBooking
05-just-enough-set-theory/specs/SeatBooking  SeatBooking.cfg  (Seats={aisle, window}, Guests={g1, g2})
parse ok
model check: invariant NoDoubleBooking violated; trace has 3 states (2 steps); 7 distinct states
expected outcome met: violation NoDoubleBooking
trace (changed variables only; ./tla trace --full shows all):
  1  Init  bookings = {}
  2  Book  bookings: {} -> {<<aisle, g1>>}
  3  Book  bookings: {<<aisle, g1>>} -> {<<aisle, g1>>, <<aisle, g2>>}

Reveal for commit a

NoDoubleBooking is violated, with a trace of 3 states: two Book steps sell the same seat to two different guests. The teammate's guard let the second sale through, because that guest had not bought that seat before.

Reveal for commit b

TLC reports 9 distinct states and no violation, with both actions firing and HouseFull reached. The teammate's version, checked with only TypeOK (specs/SeatBooking.typeok.cfg), reaches 16: every set of pairs there is.

03Reason

If you picked "no violation", you read <<s, g>> \notin bookings as "seat s is free". It says something smaller: this guest has not bought this seat. Here is how TLC took that apart:

  1. State 1 Initial state bookings = {}
  2. State 2 Book bookings {} becomes {<<aisle, g1>>}
  3. State 3 Book bookings {<<aisle, g1>>} becomes {<<aisle, g1>>, <<aisle, g2>>}

The first sale is fine. The second asks the guard about a different pair, finds it missing, and goes ahead. The set now holds two pairs with the same seat, and nothing in the spec ever looked.

So what does "is the seat taken?" look like, written against a set of pairs? A set answers exactly one question directly: is this thing an element? Every other question is a query you build from that one. "Is seat s taken?" means "is there some guest h with <<s, h>> in the set?".

Write something before openingwrite "seat s is taken" as a formula over bookings.

\E h \in Guests : <<s, h>> \in bookings. \E is read "there exists": "there exists an h in Guests such that the pair is in the set". If you know SQL, it is EXISTS (SELECT 1 FROM bookings WHERE seat = s). The fixed guard is its negation, ~\E h \in Guests : <<s, h>> \in bookings, read "no guest holds seat s".

Its partner is \A, read "for all". NoDoubleBooking is built from it:

TLA+
NoDoubleBooking is defined as
    for all s in Seats :
        for all g, h in Guests :
            (tuple ofs, gend tuple in bookings and tuple ofs, hend tuple in bookings) implies g = h

Read it as a query that must come back empty: look at every seat and every two guests; if both hold the seat, they are the same guest. => is read "implies". TLC evaluates it the way you would by hand, one combination at a time. That only works because Seats and Guests are small finite sets, which is why every constant in this course comes with a comment saying how big the cfg makes it.

One detail repays a second look. \A g, h \in Guests runs over every way of picking two guests, and that includes picking the same guest twice. Nothing about two names makes them name two different things. That is why the requirement ends in => g = h. Try writing "no seat is held by two guests" the obvious way instead, as ~(<<s, g>> \in bookings /\ <<s, h>> \in bookings) under the same quantifiers. Now let g and h both be g1, and let g1 hold the aisle seat. Both conjuncts now ask about the same pair, which is in the set, so the inner formula is false, and the invariant fails in a state that is perfectly fine. You would have written "no seat is ever sold", which is a claim about a theatre with no customers.

Write something before openingthe fixed spec has a guard that refuses a double booking and an invariant that forbids one. Is one of them redundant?

They do different jobs. The guard is part of the system: it says what the box office does. The invariant is part of the claim: it says what must never happen, and TLC checks it against whatever the actions do. The teammate's guard was wrong, and NoDoubleBooking is what caught it. Delete the invariant and a broken guard passes in silence. That is why the Core asks for both, and why the course runs your invariant against a box office with a looser rule, where it has to fail.

Sets also build new sets. {x \in S : P} keeps the elements of S that satisfy P; it is a filter, like WHERE. {e : x \in S} computes e for each element; it is a map, like SELECT e. For example, the guests who hold some seat:

output
{g \in Guests : \E s \in Seats : <<s, g>> \in bookings}

Cardinality, from the standard module FiniteSets, counts the elements of a finite set. The Warm-up uses all three.

Write something before openingthe fixed spec reaches fewer distinct states than the teammate's spec with only TypeOK. Why, and which count does TypeOK describe?

The four possible pairs make 2 × 2 × 2 × 2 = 16 sets of pairs, and TypeOK allows all of them. The teammate's spec reaches every one (16 distinct states), because its guard never refuses a new pair. With the fixed guard, each seat is free or held by one guest, three choices per seat: 3 × 3 = 9 (9). TypeOK describes what values are allowed. The actions decide what is reachable.

Notice what the fix cost. The guard went from one membership test to a quantified query, and the representation still allows a double booking: you can write the set {<<aisle, g1>>, <<aisle, g2>>} down, and only the guard keeps it out. That is the question the chapter opened with. If one seat must have at most one guest, a set of pairs is the wrong shape, because it says nothing about "at most one". The next lesson stores the seats so that two guests in one seat cannot even be written down.

04In your editor

The specs are in chapters/05-just-enough-set-theory/specs/. Check this lesson's exercises with:

shell
./tla check 05-01

The fixed box office, with its witness and both actions:

Output 05-just-enough-set-theory/specs/GuardedSeatBooking
05-just-enough-set-theory/specs/GuardedSeatBooking  GuardedSeatBooking.cfg  (Seats={aisle, window}, Guests={g1, g2})
parse ok
model check: no violation found in 9 distinct states (25 generated, depth 3)
expected outcome met: pass
coverage: all 2 actions fired
witness HouseFull reached (trace of 3 states)

When you write a quantifier, say the query in words first, out loud or in a comment. Most requirements fall into a handful of shapes:

In wordsAs a query over bookings
some seat is free\E s \in Seats : ~\E g \in Guests : <<s, g>> \in bookings
no seat has two guests\A s \in Seats : \A g, h \in Guests : ... => g = h
the seats that are sold{s \in Seats : \E g \in Guests : <<s, g>> \in bookings}
how many seats are soldCardinality of that set
everyone is seated\A g \in Guests : \E s \in Seats : <<s, g>> \in bookings

"Some" becomes \E, "every" and "no" become \A, "the ones that" becomes a filter, and "how many" becomes Cardinality. Swapping \E and \A is easy to miss on the page (05-review is built on that slip); reading the formula back in words catches it. Then check which variable each name is bound to. In \A s \in Seats : \E g \in Guests : ..., s and g exist only inside the formula. A name used outside its quantifier is an error SANY catches at once.

05Practice

Warm-up. Write the set of seats that some guest holds, and TLC checks that it has as many elements as the set of pairs.

Core. The venue adds a rule: a guest may hold only one seat at a time. Write it as a guard and as an invariant. The course checks your invariant against a box office that lets a guest hold two seats, where it must fail.

05-01-ex1Warm-up5 min

05-01-ex1 · Taken seats

Tier: warm-up. Time: about 5 minutes.

The fixed box office from the lesson, with one new claim. With no double booking, every sold seat is exactly one pair in bookings, so the set of taken seats has as many elements as the set of pairs. CountsAgree says so, using a set Taken that you write.

  1. Run ./tla check 05-01-ex1. The starter's Taken is the empty set, and TLC refutes CountsAgree after the first sale. Read the trace: what does Cardinality count on each side?
  2. Fill gap g1 in GuardedSeatBooking.tla: Taken is the set of seats that some guest holds. Write it as a filter, {s \in Seats : ...}, with a quantifier inside.
  3. Run the check again.

The cfg also checks TakenAreSeats: Taken must hold seats. The set of guests who hold a seat is a different set, with a different size, since a guest may hold both seats here; and bookings itself has the right size but holds pairs. Either answer fails TakenAreSeats.

Hints: HINTS.md, under "05-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/01-ex1-taken-seats/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Done when: ./tla check 05-01-ex1 prints "goal met: pass".

./tla check 05-01-ex1

Repo chapters/05-just-enough-set-theory/exercises/01-ex1-taken-seats/

Hints

Hint 1

Taken is a set of seats, so it starts with {s \in Seats : ...}. After the colon goes the condition a seat must meet to be kept.

Hint 2

A seat is taken when some guest holds it. "Some guest" is a quantifier, and "holds it" is membership of a pair in bookings.

Hint 3 (nearly the answer)

Keep each seat s for which there exists a guest g in Guests such that the pair of s and g is in bookings. Use \E inside the filter.

05-01-ex2Core8 min

05-01-ex2 · One seat each

Tier: core. Time: about 8 minutes.

The fixed box office from the lesson works: no seat is ever sold twice. Now the venue adds a rule: a guest may hold only one seat at a time. You state it twice, once in Book, which must enforce it, and once as the invariant OneSeatPerGuest, which checks it. Both cfgs already name OneSeatPerGuest; the starter defines it as TRUE.

  1. Run ./tla check 05-01-ex2. Both cfgs pass, and TLC warns that OneSeatPerGuest is a constant-level formula: it is TRUE, so it checks nothing.
  2. Write OneSeatPerGuest (gap g2) first, so that you know what you are aiming for. Run the check: GuardedSeatBooking.cfg should now fail, with one guest holding both seats.
  3. Change Book (gap g1) so that it refuses a guest who already holds a seat. Delete the \* GAP marker on its first line when you are done.
  4. Run the check.

The check also runs MCGuardedSeatBooking.cfg, the course's test of your invariant: a box office with three seats whose rule is looser than the venue's, so a guest can hold two seats but never all three. There your OneSeatPerGuest must fail. An invariant that passes there, such as "no guest holds every seat", says less than the rule. Leave the two MC files as they are.

Hints: HINTS.md, under "05-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/05-just-enough-set-theory/exercises/01-ex2-one-seat-each/; open it after you have a passing answer of your own.

If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.

Both cfgs must meet their goals, each in its own block of output: "goal met: pass" for GuardedSeatBooking.cfg and "goal met: violation OneSeatPerGuest" for MCGuardedSeatBooking.cfg. Read each block's goal line, not only the last line.

Done when: ./tla check 05-01-ex2 prints "goal met: pass".

./tla check 05-01-ex2

Repo chapters/05-just-enough-set-theory/exercises/01-ex2-one-seat-each/

Hints

Hint 1

Look at the guard already in Book: "nobody holds this seat". The new guard is its mirror image: "this guest holds no seat". It goes in Book as one more conjunct.

Hint 2

For the guard, quantify over Seats and ask whether the pair of that seat and g is in bookings. For the invariant, copy the shape of NoDoubleBooking and swap what is fixed and what varies.

Hint 3 (nearly the answer)

The invariant says: for every guest g, and every two seats s and t, if both pairs with g are in bookings, then s and t are the same seat. The guard is a negated \E over seats.

06Explain it

Open NOTES.md and answer the three prompts under 05-01, about the Core exercise.

output
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:

Compare your answers with "05-01 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "The guard ~\E t \in Seats : <<t, g>> \in bookings refuses a guest who already holds a seat, and OneSeatPerGuest checks that no guest holds two different seats. The course's check showed my invariant fails on a box office where a guest may hold two of three seats, so in that model it is not weaker than the rule. It relies on the guard being the only way into bookings; a second action that adds pairs would need the same guard. TLC checked two seats and two guests, with HouseFull reached; nothing about larger theatres or concurrent sales."

A weak answer: "I added the rule and TLC passed." It does not say what the invariant claims, why a pass on the guarded spec is not enough, or what the model leaves out.

Done?