Skip to content

several theorems about graphs#125

Open
muenchnerkindl wants to merge 15 commits into
masterfrom
graphs
Open

several theorems about graphs#125
muenchnerkindl wants to merge 15 commits into
masterfrom
graphs

Conversation

@muenchnerkindl

Copy link
Copy Markdown
Contributor

This PR adds a number of theorems about graphs, including those required for the safety proof of the Dijkstra-Scholten algorithm (https://github.com/tlaplus/Examples/tree/master/specifications/ewd687a). I'll do a PR for that proof once these theorems are in.

Besides changing some (LOCAL) INSTANCE to EXTENDS due to current limitations of the PM – including in some modules imported here – I also made a few changes to Graphs.tla:

  • simplified the definition of SimplePath and renamed the old definition to MCSimplePath: it applies only to finite graphs but can be evaluated by TLC,
  • similar changes to AreConnectedIn and IsStronglyConnected,
  • modified the definition of IsTreeWithRoot so that it requires the root to be a node in the graph: the previous definition was vacuously true for an graph with an empty set of nodes, for any root, and this requires case distinctions when adding a new leaf to a tree. IMHO the present definition is reasonable and more uniform.

The above changes may break some existing specification projects in that SimplePath should be overridden by MCSimplePath etc. when using TLC.

I also noticed that the definition of TransitiveClosure in Relations.tla is correct only for finite relations and suggest to eventually make analogous changes there, introducing an operator MCTransitiveClosure.

Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
@muenchnerkindl

Copy link
Copy Markdown
Contributor Author

OK, I should have realized that the operators must be renamed in the test module for the tests to work.

Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
@muenchnerkindl

Copy link
Copy Markdown
Contributor Author

@lemmy could you please have a look? I'd be particularly interested in your comments on my changes to existing operators.

@lemmy

lemmy commented Jul 8, 2026

Copy link
Copy Markdown
Member

LGTM. However, wouldn't it be better to override the new generic operators using Java module overrides instead of introducing separate MC* definitions? That would free users from redefining the operators while still giving TLC an efficient definition, and follow the same approach already used elsewhere in this repository.

@muenchnerkindl

Copy link
Copy Markdown
Contributor Author

Agreed, will try to do that.

@lemmy

lemmy commented Jul 8, 2026

Copy link
Copy Markdown
Member

I will open a new draft PR soon with the three overrides.

@muenchnerkindl

Copy link
Copy Markdown
Contributor Author

Also note that the definition of IsTreeWithRoot is slightly different from the previous one for graphs with an empty set of nodes. That's an edge case, and it's not fundamental, but I found that it avoids a few extra cases for the theorems.

@lemmy

lemmy commented Jul 8, 2026

Copy link
Copy Markdown
Member

Also note that the definition of IsTreeWithRoot is slightly different from the previous one for graphs with an empty set of nodes. That's an edge case, and it's not fundamental, but I found that it avoids a few extra cases for the theorems.

The amended definition now differs from https://github.com/tlaplus/Examples/blob/47b0e2cc0268836b89f5ce451f38e5df5f1cf773/specifications/SpecifyingSystems/AdvancedExamples/Graphs.tla#L29-L33. Perhaps, we should also amend SS def?

Also, we should check if https://github.com/tlaplus/Examples/blob/master/specifications/ewd687a/EWD687a_proof.tla#L789 still proves and TLC continues to check the corresponding spec.

@muenchnerkindl

Copy link
Copy Markdown
Contributor Author

Oh, I had not realized that Graphs was part of Specifying Systems ...

As for EWD687a, I have a complete proof of the safety property (amended for the changed definition of IsTreeWithRoot) and will open a PR as soon as this one is settled.

@lemmy

lemmy commented Jul 8, 2026

Copy link
Copy Markdown
Member

#126

@lemmy

lemmy commented Jul 8, 2026

Copy link
Copy Markdown
Member

lemmy and others added 9 commits July 9, 2026 06:25
Check the operators exhaustively against their pure TLA+ definitions and
the ConnectionsIn oracle over all graphs on up to three nodes, plus edge
cases (self-loops, arguments outside the node set, composite/non-normalized
node values).

[Tests]

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>
…nglyConnected

Register three Java overrides in TLCOverrides. SimplePath enumerates
simple paths via DFS instead of materializing SeqOf(node, Cardinality(node));
AreConnectedIn and IsStronglyConnected use BFS (the latter forward and on the
transpose) rather than the quadratic all-pairs pure-TLA+ definitions.

The overrides cut GraphsTests (exhaustive checks over all graphs on up to
four nodes `\cup Graphs({1, 2, 3})`) from ~15 to ~5 minutes.

TLC's code is not reused: its only Value-based graph algorithm,
TransitiveClosure.Warshall, is non-reflexive, drops isolated nodes and is
O(V^3), so it fits none of these operators.

[TLC]

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>
* Theorems about Contains and majority supersets

Add and prove membership theorems for SequencesExt!Contains
(empty/Append/Cons/Concat/Tail/singleton plus heterogeneous-type
Append/Concat variants) and SupersetOfMajorityIsMajority for
FiniteSetsExt. All obligations checked with TLAPS.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>

* IsSorted operator and sortedness theorems

Add SequencesExt!IsSorted(s, op(_,_)), which holds iff s is sorted with
respect to an arbitrary binary relation op, with a formal doc comment
and examples. Add and prove the accompanying theorems SortedEmpty,
SortedSingleton, SortedAppend, SortedConcat, SortedInjective and
SortedSubSeq, whose order hypotheses on op (transitivity, irreflexivity)
are stated locally so they apply to any relation. All obligations
checked with TLAPS.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>

* theorems about sequences, finite sets, and quorum systems

Signed-off-by: Stephan Merz <stephan.merz@loria.fr>

* fixing two apparently brittle proofs

Signed-off-by: Stephan Merz <stephan.merz@loria.fr>

* align theorem statements in SequencesExtTheorems with those in the _proofs module

Signed-off-by: Stephan Merz <stephan.merz@loria.fr>

---------

Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
@muenchnerkindl

Copy link
Copy Markdown
Contributor Author

@lemmy I believe that the Graphs and UndirectedGraphs modules are now in a state to be merged, could you please have a look?

As for https://github.com/tlaplus/Examples/blob/47b0e2cc0268836b89f5ce451f38e5df5f1cf773/specifications/SpecifyingSystems/AdvancedExamples/Graphs.tla#L29-L33, I suggest not to change the definitions there (which would rewrite history) but to add a comment that a more recent Graphs module can be found here.

Signed-off-by: Stephan Merz <stephan.merz@loria.fr>
Comment thread modules/Graphs.tla
/\ p # << >>
/\ Cardinality({ p[i] : i \in DOMAIN p }) = Len(p)
/\ \A i \in 1..(Len(p)-1) : <<p[i], p[i+1]>> \in G.edge}
\* NB: TLC uses a Java override instead of this definition,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TLA+ modules of the standard operators include a general note about overrides instead of repeating it.

https://github.com/tlaplus/tlaplus/blob/30cc3601321c3fc02e044d0ecb5c58d8921e18df/tlatools/org.lamport.tlatools/src/tla2sany/StandardModules/TLC.tla#L7-L11


-----------------------------------------------------------------------------
(****************************************************************************)
(* The set of all possible undirecteddirected graphs whose node set is S. *)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Comment thread modules/SequencesExt.tla
@@ -1,5 +1,5 @@
---------------------------- MODULE SequencesExt ----------------------------
EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions, Bags
EXTENDS Sequences, Naturals, FiniteSets, FiniteSetsExt, Folds, Functions, Bags, TLC

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker for this PR, but perhaps, we should consider adding preliminary support to TLAPS to handle the instantiation of constant-level/variable-free modules. This would prevent the namespace from becoming cluttered. Since CommunityModules are becoming TLA+’s standard library, EXTENDS will cause too many problems in the future.

}

/*
* Compute the strongly connected components of an undirected graph: initially

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should "strongly" be removed here?

Comment thread modules/GraphTheorems.tla
PROVE IsDirectedGraph(Transpose(G))

\* Note that the reverse implication does not hold in general.
\* Consider G = [node |-> {1}, edge |-> {<<1,1,1}>>, root |-> {1}]

@lemmy lemmy Jul 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}>> swapped

IN { S \in SUBSET G.node :
/\ IsCC(S)
/\ \A T \in (SUBSET S) \ {S} : ~ IsCC(T)
/\ \A T \in SUBSET G.node : S \subseteq T /\ S # T => ~ IsCC(T)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding theorems that fail to prove for the initial, broken definition.

… undirected graph modules

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Markus Alexander Kuppe <github.com@lemmster.de>

@lemmy lemmy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - I had AI refactor the two overrides to share code (last commit).

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