-
Notifications
You must be signed in to change notification settings - Fork 178
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
6
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.
+407
−0
Draft
Changes from all commits
Commits
Show all changes
6 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 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,72 @@ | ||
| /- | ||
| 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.DFinsupp.WellFounded | ||
|
|
||
|
|
||
| /-! | ||
| # 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 | ||
|
|
||
| variable {Node Edge : Type} [DecidableEq Node] [DecidableEq Edge] | ||
|
|
||
| /-- 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 | ||
|
|
||
| abbrev NodeOf (g : CFG Node Edge) : Type := {n // n ∈ g.nodes} | ||
| abbrev EdgeOf (g : CFG Node Edge) : Type := {e // e ∈ g.edges} | ||
|
|
||
| namespace CFG | ||
|
|
||
| /-- `g.nodes`, presented as `NodeOf g`. -/ | ||
| def nodesOf (g : CFG Node Edge) : List (NodeOf g) := g.nodes.attach | ||
|
|
||
| def edgesOf (g : CFG Node Edge) : List (EdgeOf g) := g.edges.attach | ||
|
|
||
| def dstOf (g : CFG Node Edge) (e : EdgeOf g) : NodeOf g := | ||
| ⟨g._dstOf e, g.dstOf_mem e e.property⟩ | ||
|
|
||
| def srcOf (g : CFG Node Edge) (e : EdgeOf g) : NodeOf g := | ||
| ⟨g._srcOf e, g.srcOf_mem e e.property⟩ | ||
|
|
||
| /-- All in-edges of a given node -/ | ||
| def inEdges (g : CFG Node Edge) (n : NodeOf g) : List (EdgeOf g) := | ||
| g.edgesOf.filter (g.dstOf · = n) | ||
|
|
||
| def succOf (g : CFG Node Edge) (n : NodeOf g) : List (NodeOf g) := | ||
| g.nodesOf.filter (fun m => (g.inEdges m).any (g.srcOf · = n)) | ||
|
|
||
| instance {g : CFG Node Edge} : Fintype (NodeOf g) := | ||
| List.Subtype.fintype g.nodes | ||
|
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I have some questions about the design of
CFG:SetorFinset). Why do you define them to be lists and then build types from them (NodeOfandEdgeOf)? Scanning the code inKildall.lean, I'm not sure you ever used the fact thatnodesandedgesare lists (rather than sets).nodesandedgesare subsets ofNodeandEdge. Can they simply be the whole types? Then you won't need constraints likesetOf_memanddstOf_mem.Quiverin mathlib:https://leanprover-community.github.io/mathlib4_docs/Mathlib/Combinatorics/Quiver/Basic.html
Would it usable for your purpose?
_at the beginning of_srcOfanddstOf?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.
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 forNodes 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 aNodeOf 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!