Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Index sorting through the imported heap: generated algorithm definition

Definition
Algo_sort_indices_Generated

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

algo2leanalgorithmsclrsdata-structuresrunning-timetextbook

The compiled semantics of sort_indices.algo in the examples/kruskal import root. This bundle contains source-function frames, transition rules and the stable public call/run/output interfaces. Imported modules are linked through the shared execution semantics, including mutable aliases and named tick increments.

These are the algorithm definitions proved about by the corresponding analysis nodes. Named-line observations are in a separate Points bundle, so editing labels does not change the main algorithm statement. The reproduction target is Algo2Lean v0.1.4; raw generated files and platform-adapted packaging are tracked separately, and release/source links must be finalized before submission.

<!-- 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_Algo2LeanLib_Modules
import Definitions.Def_Algo_indexed_heap_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

namespace Algo.«sort_indices»

open _root_.Algo2LeanGenerated

-- Independently compiled module; imported algorithms keep their own transitions.
namespace Algo2LeanGenerated

abbrev Ref__a2l_import_0_MinHeap := Algo.«indexed_heap».Algo2LeanGenerated.Ref_MinHeap

abbrev is__a2l_import_0_MinHeap := Algo.«indexed_heap».Algo2LeanGenerated.is_MinHeap
abbrev create__a2l_import_0_MinHeap := Algo.«indexed_heap».Algo2LeanGenerated.create_MinHeap
abbrev field__a2l_import_0_MinHeap_vertices := Algo.«indexed_heap».Algo2LeanGenerated.field_MinHeap_vertices
abbrev set__a2l_import_0_MinHeap_vertices := Algo.«indexed_heap».Algo2LeanGenerated.set_MinHeap_vertices
abbrev field__a2l_import_0_MinHeap_positions := Algo.«indexed_heap».Algo2LeanGenerated.field_MinHeap_positions
abbrev set__a2l_import_0_MinHeap_positions := Algo.«indexed_heap».Algo2LeanGenerated.set_MinHeap_positions
abbrev field__a2l_import_0_MinHeap_keys := Algo.«indexed_heap».Algo2LeanGenerated.field_MinHeap_keys
abbrev set__a2l_import_0_MinHeap_keys := Algo.«indexed_heap».Algo2LeanGenerated.set_MinHeap_keys
abbrev field__a2l_import_0_MinHeap_size := Algo.«indexed_heap».Algo2LeanGenerated.field_MinHeap_size
abbrev set__a2l_import_0_MinHeap_size := Algo.«indexed_heap».Algo2LeanGenerated.set_MinHeap_size

structure State_sort_indices where
  v0 : SeqRef := 0 -- keys
  v1 : Int := 0 -- count
  v2 : Ref__a2l_import_0_MinHeap := 0 -- queue
  v3 : SeqRef := 0 -- result
  v4 : Int := 0 -- index
  t5 : SeqRef := 0 -- temporary
  t6 : Bool := false -- temporary
  t7 : Int := 0 -- temporary
  t8 : Int := 0 -- temporary
  t9 : SeqRef := 0 -- temporary
  t10 : Ref__a2l_import_0_MinHeap := 0 -- temporary
  t11 : Bool := false -- temporary
  t12 : Unit := () -- temporary
  t13 : Ref__a2l_import_0_MinHeap := 0 -- temporary
  t14 : Int := 0 -- temporary
  t15 : Int := 0 -- temporary
  t16 : SeqRef := 0 -- temporary
  t17 : Int := 0 -- temporary
  t18 : Ref__a2l_import_0_MinHeap := 0 -- temporary

inductive Frame where
  | invalid
  | fn_sort_indices (pc : Nat) (state : State_sort_indices)
  | import_0 (frame : Algo.«indexed_heap».Algo2LeanGenerated.Frame) (inner : Algo.«indexed_heap».Algo2LeanGenerated.Stack) (resume : Algo.«indexed_heap».Algo2LeanGenerated.Result → Frame)

inductive Result where
  | fn_sort_indices (value : SeqRef)

abbrev Configuration := Algo2LeanCore.Config Heap (Frame) Result
abbrev Stack := Algo2LeanCore.Stack (Frame) Result
abbrev Transcript := List (Configuration)

inductive Step : Configuration → Configuration → Prop where
  | sort_indices_pc1 (h : Heap) (s : State_sort_indices) (stack : Stack) (defined : s.t5 < h.nextRef) :
      Step (.active h (Frame.fn_sort_indices 1 s) stack) (Algo2LeanCore.resumeConfig h (Result.fn_sort_indices (s.t5)) stack)
  | sort_indices_pc2 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 2 s) stack) (.active h (Frame.fn_sort_indices 1 { s with t5 := s.v3 }) stack)
  | sort_indices_pc3_true (h : Heap) (s : State_sort_indices) (stack : Stack) (condition : s.t6 = true) :
      Step (.active h (Frame.fn_sort_indices 3 s) stack) (.active h (Frame.fn_sort_indices 12 s) stack)
  | sort_indices_pc3_false (h : Heap) (s : State_sort_indices) (stack : Stack) (condition : s.t6 = false) :
      Step (.active h (Frame.fn_sort_indices 3 s) stack) (.active h (Frame.fn_sort_indices 2 s) stack)
  | sort_indices_pc4 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 4 s) stack) (.active h (Frame.fn_sort_indices 3 { s with t6 := (decide ((s.v4 < s.v1))) }) stack)
  | sort_indices_pc5 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 5 s) stack) (.active h (Frame.fn_sort_indices 4 { s with v4 := (s.v4 + (1 : Int)) }) stack)
  | sort_indices_pc6 (h : Heap) (s : State_sort_indices) (stack : Stack) (defined : s.t9 < h.nextRef) :
      Step (.active h (Frame.fn_sort_indices 6 s) stack) (.active (h.write s.t9 s.t8 s.t7) (Frame.fn_sort_indices 5 s) stack)
  | sort_indices_pc7 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 7 s) stack) (.active h (Frame.fn_sort_indices 6 { s with t8 := s.v4 }) stack)
  | sort_indices_pc8 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 8 s) stack) (.active h (Frame.fn_sort_indices 7 { s with t9 := s.v3 }) stack)
  | sort_indices_pc9 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 9 s) stack) (.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)
  | sort_indices_pc10 (h : Heap) (s : State_sort_indices) (stack : Stack) (defined : (is__a2l_import_0_MinHeap h (s.t10))) :
      Step (.active h (Frame.fn_sort_indices 10 s) stack) (.active h (Frame.fn_sort_indices 9 { s with t10 := s.t10 }) stack)
  | sort_indices_pc11 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 11 s) stack) (.active h (Frame.fn_sort_indices 10 { s with t10 := s.v2 }) stack)
  | sort_indices_pc12 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 12 s) stack) (.active (h.tick "sort_steps" 1) (Frame.fn_sort_indices 11 s) stack)
  | sort_indices_pc13 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 13 s) stack) (.active h (Frame.fn_sort_indices 4 { s with v4 := (0 : Int) }) stack)
  | sort_indices_pc14_true (h : Heap) (s : State_sort_indices) (stack : Stack) (condition : s.t11 = true) :
      Step (.active h (Frame.fn_sort_indices 14 s) stack) (.active h (Frame.fn_sort_indices 22 s) stack)
  | sort_indices_pc14_false (h : Heap) (s : State_sort_indices) (stack : Stack) (condition : s.t11 = false) :
      Step (.active h (Frame.fn_sort_indices 14 s) stack) (.active h (Frame.fn_sort_indices 13 s) stack)
  | sort_indices_pc15 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 15 s) stack) (.active h (Frame.fn_sort_indices 14 { s with t11 := (decide ((s.v4 < s.v1))) }) stack)
  | sort_indices_pc16 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 16 s) stack) (.active h (Frame.fn_sort_indices 15 { s with v4 := (s.v4 + (1 : Int)) }) stack)
  | sort_indices_pc17 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 17 s) stack) (.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)
  | sort_indices_pc18 (h : Heap) (s : State_sort_indices) (stack : Stack) (defined : (s.v0 < h.nextRef)) :
      Step (.active h (Frame.fn_sort_indices 18 s) stack) (.active h (Frame.fn_sort_indices 17 { s with t15 := (h.read s.v0 s.v4) }) stack)
  | sort_indices_pc19 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 19 s) stack) (.active h (Frame.fn_sort_indices 18 { s with t14 := s.v4 }) stack)
  | sort_indices_pc20 (h : Heap) (s : State_sort_indices) (stack : Stack) (defined : (is__a2l_import_0_MinHeap h (s.t13))) :
      Step (.active h (Frame.fn_sort_indices 20 s) stack) (.active h (Frame.fn_sort_indices 19 { s with t13 := s.t13 }) stack)
  | sort_indices_pc21 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 21 s) stack) (.active h (Frame.fn_sort_indices 20 { s with t13 := s.v2 }) stack)
  | sort_indices_pc22 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 22 s) stack) (.active (h.tick "sort_steps" 1) (Frame.fn_sort_indices 21 s) stack)
  | sort_indices_pc23 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 23 s) stack) (.active h (Frame.fn_sort_indices 15 { s with v4 := (0 : Int) }) stack)
  | sort_indices_pc24 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 24 s) stack) (.active h (Frame.fn_sort_indices 23 { s with v3 := s.t16 }) stack)
  | sort_indices_pc25 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 25 s) stack) (.active (h.allocate (fun _ => s.t17)) (Frame.fn_sort_indices 24 { s with t16 := h.nextRef }) stack)
  | sort_indices_pc26 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 26 s) stack) (.active h (Frame.fn_sort_indices 25 { s with t17 := (0 : Int) }) stack)
  | sort_indices_pc27 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 27 s) stack) (.active h (Frame.fn_sort_indices 26 { s with v2 := s.t18 }) stack)
  | sort_indices_pc28 (h : Heap) (s : State_sort_indices) (stack : Stack) :
      Step (.active h (Frame.fn_sort_indices 28 s) stack) (.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)
  | import_0 (h : Heap) (frame : Algo.«indexed_heap».Algo2LeanGenerated.Frame) (inner : Algo.«indexed_heap».Algo2LeanGenerated.Stack)
      (resume : Algo.«indexed_heap».Algo2LeanGenerated.Result → Frame) (caller : Stack) (after : Algo.«indexed_heap».Algo2LeanGenerated.Configuration)
      (transition : Algo.«indexed_heap».Algo2LeanGenerated.Step (.active h frame inner) after) :
      Step (.active h (Frame.import_0 frame inner resume) caller)
        (Algo2LeanCore.ModuleCall.config Frame.import_0 resume caller after)

theorem terminal (h : Heap) (result : Result) :
    Algo2LeanCore.Terminal Step (.done h result) := by
  intro next transition
  cases transition

theorem import_0_simulates :
    Algo2LeanCore.ModuleCall.Simulates Frame.import_0 Algo.«indexed_heap».Algo2LeanGenerated.Step Step := by
  apply Algo2LeanCore.ModuleCall.Delegates.simulates
  · exact Step.import_0
  · exact Algo.«indexed_heap».Algo2LeanGenerated.terminal

end Algo2LeanGenerated

open Algo2LeanGenerated

def entry_sort_indices (input : SeqRef × Int) : Frame :=
  (Frame.fn_sort_indices 28 ({ v0 := input.1, v1 := input.2 } : State_sort_indices))

def sort_indices.ValidInput (h : Heap) (input : SeqRef × Int) : Prop := input.1 < h.nextRef
def sort_indices.initial (h : Heap) (input : SeqRef × Int) : Configuration :=
  Algo2LeanCore.start h (Frame.fn_sort_indices 28 ({ v0 := input.1, v1 := input.2 } : State_sort_indices))
def sort_indices.Run (h : Heap) (input : SeqRef × Int) (h' : Heap) (output : SeqRef) : Prop :=
  _root_.Algo.«sort_indices».sort_indices.ValidInput h input ∧ Algo2LeanCore.Reaches Step (_root_.Algo.«sort_indices».sort_indices.initial h input) (.done h' (Result.fn_sort_indices output))
def sort_indices.ValidHeapTranscript (h : Heap) (input : SeqRef × Int) (trace : Transcript) : Prop :=
  _root_.Algo.«sort_indices».sort_indices.ValidInput h input ∧ ∃ final, Algo2LeanCore.Valid Step (_root_.Algo.«sort_indices».sort_indices.initial h input) trace final
def sort_indices.Call (h : Heap) (input : SeqRef × Int) (h' : Heap) (output : SeqRef) : Prop :=
  _root_.Algo.«sort_indices».sort_indices.ValidInput h input ∧ Algo2LeanCore.Summary Step h (Frame.fn_sort_indices 28 ({ v0 := input.1, v1 := input.2 } : State_sort_indices)) h' (Result.fn_sort_indices output)
-- Complete this function from the supplied local state, under any caller stack.
def sort_indices.CallFrom (state : Configuration) (finalHeap : Heap) (output : SeqRef) : Prop :=
  match state with
  | .active heap (Frame.fn_sort_indices pc locals) _ =>
      Algo2LeanCore.Summary Step heap (Frame.fn_sort_indices pc locals) finalHeap (Result.fn_sort_indices output)
  | _ => False
-- The value view gives each sequence input its own object.
def sort_indices.load (input : Seq × Int) : Heap × (SeqRef × Int) :=
  let h0 : Heap := {}
  let h1 := h0.allocate input.1
  (h1, ((0 : SeqRef), input.2))
def sort_indices.ValidTranscript (my_input : Seq × Int) (trace : Transcript) : Prop :=
  let loaded := _root_.Algo.«sort_indices».sort_indices.load my_input
  _root_.Algo.«sort_indices».sort_indices.ValidHeapTranscript loaded.1 loaded.2 trace
def sort_indices.Returns (my_input : Seq × Int) (my_output : Seq) : Prop :=
  let loaded := _root_.Algo.«sort_indices».sort_indices.load my_input
  ∃ finalHeap result, _root_.Algo.«sort_indices».sort_indices.Run loaded.1 loaded.2 finalHeap result ∧ finalHeap.store result = my_output

-- Output and clock increments from one completed execution.
def sort_indices.ReturnsWithCost (my_input : Seq × Int) (my_output : Seq) (costs : Clocks) : Prop :=
  let loaded := _root_.Algo.«sort_indices».sort_indices.load my_input
  ∃ finalHeap result, _root_.Algo.«sort_indices».sort_indices.Run loaded.1 loaded.2 finalHeap result ∧ finalHeap.store result = my_output ∧ Heap.cost loaded.1 finalHeap = costs
theorem sort_indices.ReturnsWithCost.returns {input output costs}
    (run : _root_.Algo.«sort_indices».sort_indices.ReturnsWithCost input output costs) : _root_.Algo.«sort_indices».sort_indices.Returns input output := by
  obtain ⟨heap, result, execution, value, _⟩ := run
  exact ⟨heap, result, execution, value⟩
theorem sort_indices.Returns.withCost {input output}
    (run : _root_.Algo.«sort_indices».sort_indices.Returns input output) : ∃ costs, _root_.Algo.«sort_indices».sort_indices.ReturnsWithCost input output costs := by
  obtain ⟨heap, result, execution, value⟩ := run
  exact ⟨Heap.cost (_root_.Algo.«sort_indices».sort_indices.load input).1 heap, heap, result, execution, value, rfl⟩

theorem sort_indices.Call.run {before input after output}
    (call : _root_.Algo.«sort_indices».sort_indices.Call before input after output) :
    _root_.Algo.«sort_indices».sort_indices.Run before input after output :=
  ⟨call.1, call.2.root⟩

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