-
Notifications
You must be signed in to change notification settings - Fork 185
feat(CFG): Verified implementation of Kildall's algorithm #782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
quartztz
wants to merge
10
commits into
leanprover:main
Choose a base branch
from
quartztz:qtz/kildall
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
80bf039
feat(CFG): CFG + Kildall with termination
quartztz d092b62
feat(CFG): Correctnesses!
quartztz f958173
chore(CFG): cleanup
quartztz ccd675c
chore(CFG): refactor
quartztz b6969db
feat(CFG): add minimality
quartztz 85af5c0
chore(CFG): fix lints
quartztz d3e1e38
chore(CFG): update references.bib
quartztz 5be94bd
feat(CFG): recharacterize based on quivers and finsets
quartztz 0237dc8
chore(CFG): fix lints
quartztz 9e8c302
chore(CFG): fix references.bib
quartztz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /- | ||
| Copyright (c) 2026 Jacopo Moretti. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Jacopo Moretti | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Init | ||
| public import Mathlib.Data.Fintype.List | ||
| public import Mathlib.Data.Fintype.Sigma | ||
| public import Mathlib.Data.Finset.Sort | ||
| public import Mathlib.Data.DFinsupp.WellFounded | ||
| public import Mathlib.Combinatorics.Quiver.Basic | ||
| public import Mathlib.Combinatorics.Quiver.Covering | ||
|
|
||
|
|
||
| /-! | ||
| # Control flow graphs | ||
|
|
||
| ## Main definitions | ||
|
|
||
| - `CFG` is a structure representing Control Flow Graphs on which the dataflow | ||
| algorithm defined in `Kildall.lean` runs. | ||
| -/ | ||
|
|
||
| @[expose] public section | ||
|
|
||
| /-- Abstract structure defining the necessary operations on a CFG to define a Control Flow Graph. -/ | ||
| structure CFG where | ||
| /-- All of the nodes in the CFG. -/ | ||
| Node : Type u | ||
| /-- A CFG contains a finite amount of nodes. -/ | ||
| [fintypeNode : Fintype Node] | ||
| /-- An ordering of nodes, to make the conversion to lists computable. -/ | ||
| [orderNode : LinearOrder Node] | ||
| /-- Decidable equality on nodes. -/ | ||
| [dEqNode : DecidableEq Node] | ||
| /-- Quiver structure for the edges of the CFG. -/ | ||
| quiver : Quiver Node | ||
| /-- A CFG contains a finite amount of edges. -/ | ||
| [fintypeEdges : ∀ a b, Fintype (@Quiver.Hom Node quiver a b)] | ||
| /-- Distinguished entry node in the CFG. -/ | ||
| entry : Node | ||
|
|
||
| namespace CFG | ||
|
|
||
| instance {g : CFG} : Fintype (g.Node) := | ||
| g.fintypeNode | ||
|
|
||
| instance {g : CFG} : LinearOrder (g.Node) := | ||
| g.orderNode | ||
|
|
||
| /-- Finite set of all of the nodes of `g` -/ | ||
| def nodesOf (g : CFG) : Finset g.Node := g.fintypeNode.elems | ||
|
|
||
| /-- List of all of the nodes of `g`, ordered by the ordering on `g.Node` -/ | ||
| def nodeList (g : CFG) : List g.Node := g.nodesOf.sort | ||
|
|
||
| /-- Any node of `g` is in `g.nodeList`. -/ | ||
| @[simp] theorem mem_nodeList (g : CFG) (n : g.Node) : n ∈ g.nodeList := by | ||
| rw [nodeList] | ||
| apply (Finset.mem_sort (· ≤ ·)).mpr | ||
| exact @Fintype.complete _ g.fintypeNode n | ||
|
|
||
| /-- Convenience type for edges of `g`: `Edge src dst` represents an edge between src and dst. -/ | ||
| abbrev Edge {g : CFG} (src dst : g.Node) := @Quiver.Hom g.Node g.quiver src dst | ||
| /-- Convenience type for incoming edges of `n` in `g`: `inEdge n` represents the type of edges | ||
| entering n. -/ | ||
| abbrev inEdge {g : CFG} (n : g.Node) := @Quiver.Costar g.Node g.quiver n | ||
| /-- Convenience type for outgoing edges of `n` in `g`: `outEdge n` represents the type of edges | ||
| entering n. -/ | ||
| abbrev outEdge {g : CFG} (n : g.Node) := @Quiver.Star g.Node g.quiver n | ||
|
|
||
| /-- All incoming edges of a given node, bundled with their source nodes. -/ | ||
| def inEdges {g : CFG} (n : g.Node) : Finset (inEdge n) := by | ||
| letI := g.quiver | ||
| letI := g.fintypeNode | ||
| letI := g.orderNode | ||
| letI (src dst : g.Node) := g.fintypeEdges src dst | ||
| exact Finset.univ | ||
|
|
||
| /-- All outgoing edges of a given node, bundled with their source nodes. -/ | ||
| def outEdges {g : CFG} (n : g.Node) : Finset (outEdge n) := by | ||
| letI := g.quiver | ||
| letI := g.fintypeNode | ||
| letI (src dst : g.Node) := g.fintypeEdges src dst | ||
| exact Finset.univ | ||
|
|
||
| /-- The set of successor nodes of node `n` in `g`. -/ | ||
| def succOf {g : CFG} (n : g.Node) : Finset g.Node := | ||
| letI := g.dEqNode | ||
| (outEdges n).image Sigma.fst | ||
|
|
||
| end CFG | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, in cslib (and mathlib), we prefer unbundled definitions. That is, we prefer that types like
Nodebe given as a parameter toCFG, rather than being bundled in the structure. Similarly, assumptions likeFinTypeshould be made only when needed and given as parameters. You can assume[DecidableEq Node]globally.Second, do you need to allow for the possibility of having more than one edge between two nodes? If not, then
Digraphwould be simpler and more appropriate thanQuiver:https://leanprover-community.github.io/mathlib4_docs/Mathlib/Combinatorics/Digraph/Basic.html#Digraph
Also, by using
Quiver, you have a separate type of edges for each pair of nodes. Do you have a need for a single typeEdgeof edges? If so, you may want to consider using cslib'sLTS:https://api.cslib.io/docs/Cslib/Foundations/Semantics/LTS/Basic.html
Thrid, I do not understand your comment about needing a
LinearOrderto makeCFGcomputable. Do you actually use this order in Kildall's algorithm? If so, you may consider substitutingFin nforNodeor assuming the existence of anEquiv:https://leanprover-community.github.io/mathlib4_docs/Mathlib/Logic/Equiv/Defs.html#Equiv
between
Nodeand some totally ordered finite type such asFin n. And you can make that assumption only when needed.