Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Algo.graph.Facts definitions

Definition
Algo_graph_Facts

by lunjia · Oct 10, 2026 · Mathlib 0df444a (Lean v4.33.1)

algo2leanalgorithmsclrsdata-structuresrunning-timetextbook

Definitions and required structural lemmas from Algo.graph.Facts. Source declarations are retained by compiler-reported ranges; imports are redirected to the corresponding definition bundles. This bundle contains no theorem stubs.

<!-- algo2lean-provenance-v0.1.4 -->

Algo2Lean provenance and reproduction. These definitions target Algo2Lean v0.1.4, using the compiler source at public commit 0f79d5e4242e6477531c9ee4410409f5b1c92f76, and Lean 4.33.1. The versioned compiler location is Algo2Lean v0.1.4. Compile all supplied .algo files and imports to reproduce the raw generated definitions. Release publication and exact website parity require their separately recorded checks; this metadata alone does not assert them. Prove2Me imports and declaration packaging are mechanically adapted, with original hashes, compiler-reported source ranges, and adaptations recorded in transplant provenance. Exact reproduction does not prove the compiler correct.

Definition code
import Definitions.Def_Algo_graph_Generated

set_option autoImplicit true
set_option backward.isDefEq.respectTransparency false
set_option backward.defeqAttrib.useBackward true


-- Generated by Algo2Lean 0.1.4 https://github.com/lunjiah/algo2lean

-- Optional proof certificate; execution remains the generated Step relation.
namespace «Algo».«graph»

open Algo2LeanCore _root_.Algo2LeanGenerated

namespace Algo2LeanGenerated

private def next_Edge___init__ (h : Heap) (s : State_Edge___init__) (stack : Stack) : Nat → Configuration
  | 1 => (Algo2LeanCore.resumeConfig h (Result.fn_Edge___init__ (s.t4)) stack)
  | 2 => (.active h (Frame.fn_Edge___init__ 1 { s with t4 := () }) stack)
  | 3 => (.active (set_Edge_next h (s.v0) (s.v3)) (Frame.fn_Edge___init__ 2 s) stack)
  | 4 => (.active (set_Edge_weight h (s.v0) (s.v2)) (Frame.fn_Edge___init__ 3 s) stack)
  | 5 => (.active (set_Edge_target h (s.v0) (s.v1)) (Frame.fn_Edge___init__ 4 s) stack)
  | pc => .active h (.fn_Edge___init__ pc s) stack

private def next_Graph___init__ (h : Heap) (s : State_Graph___init__) (stack : Stack) : Nat → Configuration
  | 1 => (Algo2LeanCore.resumeConfig h (Result.fn_Graph___init__ (s.t2)) stack)
  | 2 => (.active h (Frame.fn_Graph___init__ 1 { s with t2 := () }) stack)
  | 3 => (.active (set_Graph_heads h (s.t4) (s.t3)) (Frame.fn_Graph___init__ 2 s) stack)
  | 4 => (.active h (Frame.fn_Graph___init__ 3 { s with t4 := s.v0 }) stack)
  | 5 => (.active (seq_Edge_optional.const h s.t5).1 (Frame.fn_Graph___init__ 4 { s with t3 := h.nextCollection }) stack)
  | 6 => (.active h (Frame.fn_Graph___init__ 5 { s with t5 := none }) stack)
  | 7 => (.active (set_Graph_size h (s.v0) ((0 : Int))) (Frame.fn_Graph___init__ 6 s) stack)
  | 8 => if s.t6 then (.active h (Frame.fn_Graph___init__ 7 s) stack) else (.active h (Frame.fn_Graph___init__ 6 s) stack)
  | 9 => (.active h (Frame.fn_Graph___init__ 8 { s with t6 := (decide ((s.v1 < (0 : Int)))) }) stack)
  | 10 => (.active (set_Graph_size h (s.v0) (s.v1)) (Frame.fn_Graph___init__ 9 s) stack)
  | pc => .active h (.fn_Graph___init__ pc s) stack

private def next_Graph_add_edge (h : Heap) (s : State_Graph_add_edge) (stack : Stack) : Nat → Configuration
  | 1 => (Algo2LeanCore.resumeConfig h (Result.fn_Graph_add_edge (s.t4)) stack)
  | 2 => (.active h (Frame.fn_Graph_add_edge 1 { s with t4 := true }) stack)
  | 3 => (.active (seq_Edge_optional.set h s.t7 s.t6 s.t5) (Frame.fn_Graph_add_edge 2 s) stack)
  | 4 => (.active h (Frame.fn_Graph_add_edge 3 { s with t6 := s.v1 }) stack)
  | 5 => (.active h (Frame.fn_Graph_add_edge 4 { s with t7 := ((field_Graph_heads h (s.v0)).getD 0) }) stack)
  | 6 => (.active h (Frame.fn_Edge 9 ({ v0 := s.t8, v1 := s.t9, v2 := s.t10 } : State_Edge)) ((fun value => match value with | .fn_Edge result => (Frame.fn_Graph_add_edge 5 { s with t5 := (some result) }) | _ => .invalid) :: stack))
  | 7 => (.active h (Frame.fn_Graph_add_edge 6 { s with t10 := ((seq_Edge_optional.get h ((field_Graph_heads h (s.v0)).getD 0) s.v1).getD none) }) stack)
  | 8 => (.active h (Frame.fn_Graph_add_edge 7 { s with t9 := s.v3 }) stack)
  | 9 => (.active h (Frame.fn_Graph_add_edge 8 { s with t8 := s.v2 }) stack)
  | 10 => (Algo2LeanCore.resumeConfig h (Result.fn_Graph_add_edge (s.t12)) stack)
  | 11 => (.active h (Frame.fn_Graph_add_edge 10 { s with t12 := false }) stack)
  | 12 => if s.t11 then (.active h (Frame.fn_Graph_add_edge 11 s) stack) else (.active h (Frame.fn_Graph_add_edge 9 s) stack)
  | 13 => (.active h (Frame.fn_Graph_add_edge 12 { s with t11 := ((decide ((s.v1 < (0 : Int)))) || (decide ((s.v1 ≥ ((field_Graph_size h (s.v0)).getD 0)))) || (decide ((s.v2 < (0 : Int)))) || (decide ((s.v2 ≥ ((field_Graph_size h (s.v0)).getD 0)))) || (decide ((s.v3 < (0 : Int))))) }) stack)
  | pc => .active h (.fn_Graph_add_edge pc s) stack

private def next_Edge (h : Heap) (s : State_Edge) (stack : Stack) : Nat → Configuration
  | 1 => (Algo2LeanCore.resumeConfig h (Result.fn_Edge (s.t4)) stack)
  | 2 => (.active h (Frame.fn_Edge 1 { s with t4 := s.v3 }) stack)
  | 3 => (.active h (Frame.fn_Edge___init__ 5 ({ v0 := s.t6, v1 := s.t7, v2 := s.t8, v3 := s.t9 } : State_Edge___init__)) ((fun value => match value with | .fn_Edge___init__ result => (Frame.fn_Edge 2 { s with t5 := result }) | _ => .invalid) :: stack))
  | 4 => (.active h (Frame.fn_Edge 3 { s with t9 := s.v2 }) stack)
  | 5 => (.active h (Frame.fn_Edge 4 { s with t8 := s.v1 }) stack)
  | 6 => (.active h (Frame.fn_Edge 5 { s with t7 := s.v0 }) stack)
  | 7 => (.active h (Frame.fn_Edge 6 { s with t6 := s.v3 }) stack)
  | 8 => (.active h (Frame.fn_Edge 7 { s with v3 := s.t10 }) stack)
  | 9 => (.active (h.allocateObject "graph.Edge") (Frame.fn_Edge 8 { s with t10 := h.nextObject }) stack)
  | pc => .active h (.fn_Edge pc s) stack

private def next_Graph (h : Heap) (s : State_Graph) (stack : Stack) : Nat → Configuration
  | 1 => (Algo2LeanCore.resumeConfig h (Result.fn_Graph (s.t2)) stack)
  | 2 => (.active h (Frame.fn_Graph 1 { s with t2 := s.v1 }) stack)
  | 3 => (.active h (Frame.fn_Graph___init__ 10 ({ v0 := s.t4, v1 := s.t5 } : State_Graph___init__)) ((fun value => match value with | .fn_Graph___init__ result => (Frame.fn_Graph 2 { s with t3 := result }) | _ => .invalid) :: stack))
  | 4 => (.active h (Frame.fn_Graph 3 { s with t5 := s.v0 }) stack)
  | 5 => (.active h (Frame.fn_Graph 4 { s with t4 := s.v1 }) stack)
  | 6 => (.active h (Frame.fn_Graph 5 { s with v1 := s.t6 }) stack)
  | 7 => (.active (h.allocateObject "graph.Graph") (Frame.fn_Graph 6 { s with t6 := h.nextObject }) stack)
  | pc => .active h (.fn_Graph pc s) stack

/-- A total projection used only to certify uniqueness of actual transitions. -/
def next : Configuration → Configuration
  | .done h result => .done h result
  | .active h frame stack => match frame with
    | .invalid => .active h .invalid stack
    | .fn_Edge___init__ pc s => next_Edge___init__ h s stack pc
    | .fn_Graph___init__ pc s => next_Graph___init__ h s stack pc
    | .fn_Graph_add_edge pc s => next_Graph_add_edge h s stack pc
    | .fn_Edge pc s => next_Edge h s stack pc
    | .fn_Graph pc s => next_Graph h s stack pc

/-- Each generated rule agrees with the projection; guards remain in Step. -/
theorem step_next {before after : Configuration} (transition : Step before after) :
    next before = after := by
  cases transition with
  | Edge___init___pc1 h s stack => rfl
  | Edge___init___pc2 h s stack => rfl
  | Edge___init___pc3 h s stack defined => rfl
  | Edge___init___pc4 h s stack defined => rfl
  | Edge___init___pc5 h s stack defined => rfl
  | Graph___init___pc1 h s stack => rfl
  | Graph___init___pc2 h s stack => rfl
  | Graph___init___pc3 h s stack defined => rfl
  | Graph___init___pc4 h s stack => rfl
  | Graph___init___pc5 h s stack => rfl
  | Graph___init___pc6 h s stack => rfl
  | Graph___init___pc7 h s stack defined => rfl
  | Graph___init___pc8_true h s stack condition => simp [next, next_Graph___init__, condition]
  | Graph___init___pc8_false h s stack condition => simp [next, next_Graph___init__, condition]
  | Graph___init___pc9 h s stack => rfl
  | Graph___init___pc10 h s stack defined => rfl
  | Graph_add_edge_pc1 h s stack => rfl
  | Graph_add_edge_pc2 h s stack => rfl
  | Graph_add_edge_pc3 h s stack defined => rfl
  | Graph_add_edge_pc4 h s stack => rfl
  | Graph_add_edge_pc5 h s stack defined => rfl
  | Graph_add_edge_pc6 h s stack => rfl
  | Graph_add_edge_pc7 h s stack defined => rfl
  | Graph_add_edge_pc8 h s stack => rfl
  | Graph_add_edge_pc9 h s stack => rfl
  | Graph_add_edge_pc10 h s stack => rfl
  | Graph_add_edge_pc11 h s stack => rfl
  | Graph_add_edge_pc12_true h s stack condition => simp [next, next_Graph_add_edge, condition]
  | Graph_add_edge_pc12_false h s stack condition => simp [next, next_Graph_add_edge, condition]
  | Graph_add_edge_pc13 h s stack defined => rfl
  | Edge_pc1 h s stack defined => rfl
  | Edge_pc2 h s stack => rfl
  | Edge_pc3 h s stack => rfl
  | Edge_pc4 h s stack => rfl
  | Edge_pc5 h s stack => rfl
  | Edge_pc6 h s stack => rfl
  | Edge_pc7 h s stack => rfl
  | Edge_pc8 h s stack => rfl
  | Edge_pc9 h s stack => rfl
  | Graph_pc1 h s stack defined => rfl
  | Graph_pc2 h s stack => rfl
  | Graph_pc3 h s stack => rfl
  | Graph_pc4 h s stack => rfl
  | Graph_pc5 h s stack => rfl
  | Graph_pc6 h s stack => rfl
  | Graph_pc7 h s stack => rfl

/-- Imported determinism composes without reopening imported transition rules. -/
theorem deterministic : Deterministic Step := by
  intro before left right first second
  exact (step_next first).symm.trans (step_next second)

end Algo2LeanGenerated

end «Algo».«graph»

namespace Algo.«graph»

open Algo2LeanCore _root_.Algo2LeanGenerated Algo2LeanGenerated

theorem Edge.__init__.Run.eq_of_call {before after expectedHeap input output expected}
    (run : _root_.Algo.«graph».Edge.__init__.Run before input after output)
    (call : _root_.Algo.«graph».Edge.__init__.Call before input expectedHeap expected) :
    after = expectedHeap ∧ output = expected := by
  have same := Reaches.eq_of_terminal (step := Step) deterministic
    run.2 call.2.root (terminal _ _) (terminal _ _)
  cases same
  exact ⟨rfl, rfl⟩

theorem Graph.__init__.Run.eq_of_call {before after expectedHeap input output expected}
    (run : _root_.Algo.«graph».Graph.__init__.Run before input after output)
    (call : _root_.Algo.«graph».Graph.__init__.Call before input expectedHeap expected) :
    after = expectedHeap ∧ output = expected := by
  have same := Reaches.eq_of_terminal (step := Step) deterministic
    run.2 call.2.root (terminal _ _) (terminal _ _)
  cases same
  exact ⟨rfl, rfl⟩

theorem Graph.add_edge.Run.eq_of_call {before after expectedHeap input output expected}
    (run : _root_.Algo.«graph».Graph.add_edge.Run before input after output)
    (call : _root_.Algo.«graph».Graph.add_edge.Call before input expectedHeap expected) :
    after = expectedHeap ∧ output = expected := by
  have same := Reaches.eq_of_terminal (step := Step) deterministic
    run.2 call.2.root (terminal _ _) (terminal _ _)
  cases same
  exact ⟨rfl, rfl⟩

theorem Edge.Run.eq_of_call {before after expectedHeap input output expected}
    (run : _root_.Algo.«graph».Edge.Run before input after output)
    (call : _root_.Algo.«graph».Edge.Call before input expectedHeap expected) :
    after = expectedHeap ∧ output = expected := by
  have same := Reaches.eq_of_terminal (step := Step) deterministic
    run.2 call.2.root (terminal _ _) (terminal _ _)
  cases same
  exact ⟨rfl, rfl⟩

theorem Graph.Run.eq_of_call {before after expectedHeap input output expected}
    (run : _root_.Algo.«graph».Graph.Run before input after output)
    (call : _root_.Algo.«graph».Graph.Call before input expectedHeap expected) :
    after = expectedHeap ∧ output = expected := by
  have same := Reaches.eq_of_terminal (step := Step) deterministic
    run.2 call.2.root (terminal _ _) (terminal _ _)
  cases same
  exact ⟨rfl, rfl⟩

end Algo.«graph»
Source
https://github.com/lunjiah/algo2lean/blob/0f79d5e4242e6477531c9ee4410409f5b1c92f76/examples/graph.algo

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me