Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Index sorting through the imported heap: determinism and call-to-run facts

Definition
Algo_sort_indices_Facts

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

algo2leanalgorithmsclrsdata-structuresrunning-timetextbook

Generated execution facts for sort_indices.algo, including the relation between a reusable Call witness and any completed Run of the same inputs. The determinism bridge lets ordinary-output and cost corollaries reuse the constructive call theorem without repeating its algorithm analysis.

This bundle depends on the stable algorithm definition and the corresponding facts for imported components. It does not introduce a separate specification of what the program computes.

<!-- 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_sort_indices_Generated
import Definitions.Def_Algo_indexed_heap_Facts

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».«sort_indices»

open Algo2LeanCore _root_.Algo2LeanGenerated

namespace Algo2LeanGenerated

private def next_sort_indices (h : Heap) (s : State_sort_indices) (stack : Stack) : Nat → Configuration
  | 1 => (Algo2LeanCore.resumeConfig h (Result.fn_sort_indices (s.t5)) stack)
  | 2 => (.active h (Frame.fn_sort_indices 1 { s with t5 := s.v3 }) stack)
  | 3 => if s.t6 then (.active h (Frame.fn_sort_indices 12 s) stack) else (.active h (Frame.fn_sort_indices 2 s) stack)
  | 4 => (.active h (Frame.fn_sort_indices 3 { s with t6 := (decide ((s.v4 < s.v1))) }) stack)
  | 5 => (.active h (Frame.fn_sort_indices 4 { s with v4 := (s.v4 + (1 : Int)) }) stack)
  | 6 => (.active (h.write s.t9 s.t8 s.t7) (Frame.fn_sort_indices 5 s) stack)
  | 7 => (.active h (Frame.fn_sort_indices 6 { s with t8 := s.v4 }) stack)
  | 8 => (.active h (Frame.fn_sort_indices 7 { s with t9 := s.v3 }) stack)
  | 9 => (.active h (Frame.import_0 (Algo.«indexed_heap».entry_MinHeap_pop_min (s.t10)) [] (fun value => match value with | .fn_MinHeap_pop_min result => (Frame.fn_sort_indices 8 { s with t7 := result }) | _ => .invalid)) stack)
  | 10 => (.active h (Frame.fn_sort_indices 9 { s with t10 := s.t10 }) stack)
  | 11 => (.active h (Frame.fn_sort_indices 10 { s with t10 := s.v2 }) stack)
  | 12 => (.active (h.tick "sort_steps" 1) (Frame.fn_sort_indices 11 s) stack)
  | 13 => (.active h (Frame.fn_sort_indices 4 { s with v4 := (0 : Int) }) stack)
  | 14 => if s.t11 then (.active h (Frame.fn_sort_indices 22 s) stack) else (.active h (Frame.fn_sort_indices 13 s) stack)
  | 15 => (.active h (Frame.fn_sort_indices 14 { s with t11 := (decide ((s.v4 < s.v1))) }) stack)
  | 16 => (.active h (Frame.fn_sort_indices 15 { s with v4 := (s.v4 + (1 : Int)) }) stack)
  | 17 => (.active h (Frame.import_0 (Algo.«indexed_heap».entry_MinHeap_push_or_decrease ((s.t13), s.t14, s.t15)) [] (fun value => match value with | .fn_MinHeap_push_or_decrease result => (Frame.fn_sort_indices 16 { s with t12 := result }) | _ => .invalid)) stack)
  | 18 => (.active h (Frame.fn_sort_indices 17 { s with t15 := (h.read s.v0 s.v4) }) stack)
  | 19 => (.active h (Frame.fn_sort_indices 18 { s with t14 := s.v4 }) stack)
  | 20 => (.active h (Frame.fn_sort_indices 19 { s with t13 := s.t13 }) stack)
  | 21 => (.active h (Frame.fn_sort_indices 20 { s with t13 := s.v2 }) stack)
  | 22 => (.active (h.tick "sort_steps" 1) (Frame.fn_sort_indices 21 s) stack)
  | 23 => (.active h (Frame.fn_sort_indices 15 { s with v4 := (0 : Int) }) stack)
  | 24 => (.active h (Frame.fn_sort_indices 23 { s with v3 := s.t16 }) stack)
  | 25 => (.active (h.allocate (fun _ => s.t17)) (Frame.fn_sort_indices 24 { s with t16 := h.nextRef }) stack)
  | 26 => (.active h (Frame.fn_sort_indices 25 { s with t17 := (0 : Int) }) stack)
  | 27 => (.active h (Frame.fn_sort_indices 26 { s with v2 := s.t18 }) stack)
  | 28 => (.active h (Frame.import_0 (Algo.«indexed_heap».entry_MinHeap ()) [] (fun value => match value with | .fn_MinHeap result => (Frame.fn_sort_indices 27 { s with t18 := result }) | _ => .invalid)) stack)
  | pc => .active h (.fn_sort_indices 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_sort_indices pc s => next_sort_indices h s stack pc
    | .import_0 frame inner resume =>
      ModuleCall.config Frame.import_0 resume stack (Algo.«indexed_heap».Algo2LeanGenerated.next (.active h frame inner))

/-- 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
  | sort_indices_pc1 h s stack defined => rfl
  | sort_indices_pc2 h s stack => rfl
  | sort_indices_pc3_true h s stack condition => simp [next, next_sort_indices, condition]
  | sort_indices_pc3_false h s stack condition => simp [next, next_sort_indices, condition]
  | sort_indices_pc4 h s stack => rfl
  | sort_indices_pc5 h s stack => rfl
  | sort_indices_pc6 h s stack defined => rfl
  | sort_indices_pc7 h s stack => rfl
  | sort_indices_pc8 h s stack => rfl
  | sort_indices_pc9 h s stack => rfl
  | sort_indices_pc10 h s stack defined => rfl
  | sort_indices_pc11 h s stack => rfl
  | sort_indices_pc12 h s stack => rfl
  | sort_indices_pc13 h s stack => rfl
  | sort_indices_pc14_true h s stack condition => simp [next, next_sort_indices, condition]
  | sort_indices_pc14_false h s stack condition => simp [next, next_sort_indices, condition]
  | sort_indices_pc15 h s stack => rfl
  | sort_indices_pc16 h s stack => rfl
  | sort_indices_pc17 h s stack => rfl
  | sort_indices_pc18 h s stack defined => rfl
  | sort_indices_pc19 h s stack => rfl
  | sort_indices_pc20 h s stack defined => rfl
  | sort_indices_pc21 h s stack => rfl
  | sort_indices_pc22 h s stack => rfl
  | sort_indices_pc23 h s stack => rfl
  | sort_indices_pc24 h s stack => rfl
  | sort_indices_pc25 h s stack => rfl
  | sort_indices_pc26 h s stack => rfl
  | sort_indices_pc27 h s stack => rfl
  | sort_indices_pc28 h s stack => rfl
  | import_0 h frame inner resume caller after transition =>
    exact congrArg (ModuleCall.config Frame.import_0 resume caller) (Algo.«indexed_heap».Algo2LeanGenerated.step_next transition)

/-- 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».«sort_indices»

namespace Algo.«sort_indices»

open Algo2LeanCore _root_.Algo2LeanGenerated Algo2LeanGenerated

theorem sort_indices.Run.eq_of_call {before after expectedHeap input output expected}
    (run : _root_.Algo.«sort_indices».sort_indices.Run before input after output)
    (call : _root_.Algo.«sort_indices».sort_indices.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.«sort_indices»
Source
https://github.com/lunjiah/algo2lean/blob/0f79d5e4242e6477531c9ee4410409f5b1c92f76/examples/kruskal/sort_indices.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