Skip to content

feat(CFG): Verified implementation of Kildall's algorithm - #782

Draft
quartztz wants to merge 6 commits into
leanprover:mainfrom
quartztz:qtz/kildall
Draft

feat(CFG): Verified implementation of Kildall's algorithm#782
quartztz wants to merge 6 commits into
leanprover:mainfrom
quartztz:qtz/kildall

Conversation

@quartztz

@quartztz quartztz commented Aug 7, 2026

Copy link
Copy Markdown

As discussed on Zulip, here is an implementation of Kildall's worklist algorithm for solving dataflow equations. The design features a small CFG api, as well as a characterization of correctness in terms of different flavors of fixpoints. Three main theorems show that

  • The result of the algorithm is a postfixpoint (valid solution)
  • If the transfer functions are monotone, the postfixpoint that is computed is the least postfixpoint
  • If the transfer functions are monotone, the result is a fixpoint

Definitions of the structures are in Analysis/Dataflow/CFG.lean, and the algorithm and proofs are in Analysis/Dataflow/Kildall.lean. I didn't feel like it fit semantically in any other directory, but I'll be glad to move it if there's better.

TODO: fix all lints :(

@quartztz
quartztz marked this pull request as draft August 7, 2026 16:07

@ctchou ctchou left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@fmontesi Do you have any opinions about the placement of these files?

Comment on lines +38 to +40
* [G. Kildall, *A Unified Approach to Global Program Optimization*][Kildall73]
* [F. Nielson, H.R. Nielson, C. Hankin, *Principles of Program Analysis*][Nielson99]
* [R. LaSpina, *Formal Verification of WTO-based Dataflow Solvers*][LaSpina25]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add these references to the file references.bib.

Comment on lines +27 to +44
/-- Abstract structure defining the necessary operations on a CFG to define a Control Flow Graph. -/
class CFG (Node Edge : Type) [DecidableEq Node] [DecidableEq Edge] where
/-- All of the nodes in the CFG. -/
nodes : List Node
/-- All of the edges in the CFG. -/
edges : List Edge
/-- A distinguished entry node in the CFG. -/
entry : Node
/-- A proof that the entry node is part of the graph's nodes. -/
entry_mem : entry ∈ nodes
/-- Extractor function for an edge's source node. -/
_srcOf : Edge → Node
/-- Proof of correctness for the source extractor. -/
srcOf_mem : ∀ e ∈ edges, _srcOf e ∈ nodes
/-- Extractor function for an edge's destination node. -/
_dstOf : Edge → Node
/-- Proof of correctness for the destination extractor. -/
dstOf_mem : ∀ e ∈ edges, _dstOf e ∈ nodes

@ctchou ctchou Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have some questions about the design of CFG:

  • Normally the nodes and edges of a graph are taken to be sets (which can be modeled by Set or Finset). Why do you define them to be lists and then build types from them (NodeOf and EdgeOf)? Scanning the code in Kildall.lean, I'm not sure you ever used the fact that nodes and edges are lists (rather than sets).
  • For that matter, do you really need the generality that nodes and edges are subsets of Node and Edge. Can they simply be the whole types? Then you won't need constraints like setOf_mem and dstOf_mem.
  • There is something called Quiver in mathlib:
    https://leanprover-community.github.io/mathlib4_docs/Mathlib/Combinatorics/Quiver/Basic.html
    Would it usable for your purpose?
  • Why did you put _ at the beginning of _srcOf and dstOf?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I agree with the fact that the current presentation is very roundabout. I did not know about Quiver, but I feel like applying it would fix these comments: using a single type for Nodes and a quiver to characterize the edges would both remove unneeded fields and make the projections easier.

The _ was a crude way to mark that field as "internal", since the preferred API (that returns a NodeOf g) is defined in terms of it. However, this problem disappears when applying the other changes, so i'll draft a new structure to hopefully fix everything in one go.

Thank you for your comments and for the pointer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants