Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cslib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module -- shake: keep-all --deprecated_module: ignore

public import Cslib.Algorithms.Lean.MergeSort.MergeSort
public import Cslib.Algorithms.Lean.TimeM
public import Cslib.Analysis.Dataflow.CFG
public import Cslib.Analysis.Dataflow.Kildall
public import Cslib.Computability.Automata.Acceptors.Acceptor
public import Cslib.Computability.Automata.Acceptors.OmegaAcceptor
public import Cslib.Computability.Automata.DA.Basic
Expand Down
72 changes: 72 additions & 0 deletions Cslib/Analysis/Dataflow/CFG.lean
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
Comment on lines +27 to +44

@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!


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
Loading
Loading