algoveri / lean.jsonl
zzzzzhy's picture
Upload Dafny / Verus / Lean data for AlgoVeri dataset
779e088 verified
Raw
History Blame Contribute Delete
227 kB
{"problem_id": "ac_automata", "nl_desc": "Your task is to implement the Aho-Corasick algorithm to find all occurrences of multiple patterns simultaneously within a haystack (text) and verify its functional correctness in Lean using Mathlib. Preconditions: You can assume the input text and the total length of all patterns are at most 1,000,000 to ensure index arithmetic does not overflow. Requirements: Implement the method ac_automata_search which returns a Array (Nat × Nat) (pattern ID, start index). (1) Trie Construction: Build a Trie (prefix tree) from the set of input patterns. (2) Failure Links: Implement a BFS to compute \"failure links\" for each node, pointing to the longest proper suffix of the current string that exists in the Trie. (3) Traversal: Iterate through the text once, following Trie transitions and failure links to report matches. Verification Challenges: (1) Automaton Invariants: Proving that the failure links correctly point to the longest suffix-prefix match. (2) State Validity: Proving the invariant that after processing text character t[i], the current node in the automaton represents the longest suffix of t[0...i] that is a prefix of some pattern. (3) Multi-Pattern Completeness: Ensuring that if a node represents a match, or if its failure-link ancestors represent matches (substring matches), all are correctly added to the results sequence.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef ac_automata_search_precond (haystack : Array UInt8) (patterns : Array (Array UInt8)) : Prop :=\n -- !benchmark @start precond\n haystack.size < 1000000 ∧\n patterns.size > 0 ∧\n patterns.size < 1000000 ∧\n (∀ i : Nat, i < patterns.size → \n let p := patterns.getD i #[]\n p.size < 1000000)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef ac_automata_search (haystack : Array UInt8) (patterns : Array (Array UInt8))\n (_ : ac_automata_search_precond haystack patterns) :\n Array (Nat × Nat) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition auxiliary definitions\ndef matches_at (haystack : Array UInt8) (needle : Array UInt8) (start : Nat) : Prop :=\n start + needle.size ≤ haystack.size ∧\n (∀ i : Nat, i < needle.size → haystack.getD (start + i) 0 = needle.getD i 0)\n\n-- Postcondition definitions\n@[reducible, simp]\ndef ac_automata_search_postcond (haystack : Array UInt8) (patterns : Array (Array UInt8))\n (result : Array (Nat × Nat)) (_ : ac_automata_search_precond haystack patterns) : Prop :=\n -- !benchmark @start postcond\n (∀ i : Nat, i < result.size →\n let pair := result.getD i (0, 0)\n let pid := pair.fst\n let idx := pair.snd\n pid < patterns.size ∧\n -- Removed duplicate `pid < patterns.size` check\n matches_at haystack (patterns.getD pid #[]) idx) ∧\n (∀ pid idx : Nat,\n pid < patterns.size →\n matches_at haystack (patterns.getD pid #[]) idx →\n ∃ k : Nat, k < result.size ∧ result.getD k (0, 0) = (pid, idx))\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem ac_automata_search_postcond_satisfied (haystack : Array UInt8) (patterns : Array (Array UInt8))\n (h_precond : ac_automata_search_precond haystack patterns) :\n ac_automata_search_postcond haystack patterns\n (ac_automata_search haystack patterns h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof"}
{"problem_id": "bellman_ford", "nl_desc": "Your task is to implement the Bellman-Ford Algorithm and verify its functional correctness in Lean using Mathlib. Unlike Dijkstra, this algorithm must handle graphs with negative edge weights. Preconditions: You can assume the graph contains at most 100,000 nodes and edge weights are between -100,000 and 100,000 (inclusive) to prevent integer overflow. Crucially, the graph is a Simple Graph (no parallel edges between the same two nodes). Requirements: Implement the function bellman_ford which returns Option (List (Option Int)). Case 1 (Success): If the graph contains no negative cycles reachable from the start node, return Some(dists). dists[v] = Some d implies d is the shortest path weight. dists[v] = None implies v is unreachable. Case 2 (Negative Cycle): If the graph contains a reachable negative cycle (a cycle with total weight $< 0$), return None. Verification Challenges: (1) Relaxation Loop: The algorithm runs for $|V|-1$ iterations. You must prove the invariant that after $k$ iterations, dist[v] contains the shortest path weight using at most $k$ edges. (2) Negative Cycle Detection: You must verify the final check (the $|V|$-th iteration). Proving that if any distance can still be improved after $|V|-1$ iterations, a negative cycle effectively exists.\n", "spec": "import Mathlib\n\nstructure WeightedGraph where\n adj : Array (Array (Nat × Int))\n\ndef WeightedGraph.size (g : WeightedGraph) : Nat :=\n g.adj.size\n\ndef WeightedGraph.has_edge (g : WeightedGraph) (u v : Nat) (w : Int) : Prop :=\n u < g.size ∧\n ∃ pair, pair ∈ g.adj.getD u #[] ∧ pair.1 = v ∧ pair.2 = w\n\ndef WeightedGraph.well_formed (g : WeightedGraph) : Prop :=\n ∀ u, u < g.size →\n -- All targets in range\n (∀ pair, pair ∈ g.adj.getD u #[] → pair.1 < g.size) ∧\n -- Unique targets (simple graph)\n (∀ p1 p2, p1 ∈ g.adj.getD u #[] → p2 ∈ g.adj.getD u #[] → p1.1 = p2.1 → p1 = p2)\n\ndef WeightedGraph.path_has_weight (g : WeightedGraph) (p : List Nat) (w_total : Int) : Prop :=\n match p with\n | [] => w_total = 0\n | [_] => w_total = 0\n | u :: v :: rest =>\n ∃ w_edge w_rest,\n g.has_edge u v w_edge ∧\n g.path_has_weight (v :: rest) w_rest ∧\n w_total = w_edge + w_rest\n\ndef WeightedGraph.is_path (g : WeightedGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n match p with\n | [] => True\n | [_] => True\n | u :: v :: rest =>\n (∃ w, g.has_edge u v w) ∧ g.is_path (v :: rest)\n\n-- Shortest path predicate\ndef WeightedGraph.is_shortest_dist (g : WeightedGraph) (start target : Nat) (d : Int) : Prop :=\n (∃ p, g.is_path p ∧ p.head? = some start ∧ p.getLast? = some target ∧ g.path_has_weight p d) ∧\n (∀ p w, g.is_path p → p.head? = some start → p.getLast? = some target → g.path_has_weight p w → w >= d)\n\n-- Negative cycle predicate\ndef WeightedGraph.has_negative_cycle (g : WeightedGraph) : Prop :=\n ∃ p w, g.is_path p ∧ p.length > 1 ∧ p.head? = p.getLast? ∧ g.path_has_weight p w ∧ w < 0\n\ndef WeightedGraph.weights_and_size_bounded (g : WeightedGraph) : Prop :=\n g.size ≤ 100000 ∧\n ∀ u v w, g.has_edge u v w → w ≥ -100000 ∧ w ≤ 100000\n\n-- Precondition definitions\n@[reducible, simp]\ndef bellman_ford_precond (graph : WeightedGraph) (start : Nat) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧\n start < graph.size ∧\n graph.weights_and_size_bounded\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef bellman_ford (graph : WeightedGraph) (start : Nat)\n (h_precond : bellman_ford_precond graph start) : Option (List (Option Int)) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef bellman_ford_postcond (graph : WeightedGraph) (start : Nat)\n (result : Option (List (Option Int)))\n (_ : bellman_ford_precond graph start) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some dists =>\n dists.length = graph.size ∧\n ∀ v, v < graph.size →\n match dists.getD v none with\n | some d => graph.is_shortest_dist start v d\n | none => ¬ ∃ p, graph.is_path p ∧ p.head? = some start ∧ p.getLast? = some v\n | none =>\n graph.has_negative_cycle\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem bellman_ford_postcond_satisfied (graph : WeightedGraph) (start : Nat)\n (h_precond : bellman_ford_precond graph start) :\n bellman_ford_postcond graph start\n (bellman_ford graph start h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bfs", "nl_desc": "Your task is to implement a Breadth First Search (BFS) algorithm to compute the shortest path distance between two nodes and verify its functional correctness in Lean using Mathlib. You need to implement bfs_shortest_path. The function should return Some(distance) if reachable, and None otherwise. There are several verification challenges: (1) Minimality: Unlike DFS, it is not enough to find any path. You must prove that the returned distance is minimal—i.e., no path exists with fewer edges. (2) Queue Invariants: Proving the specific structural properties of the BFS queue. You must verify that the queue always remains sorted by distance (containing only nodes at distance $k$ and $k+1$) and that nodes are processed level-by-level. (3) Reachability: Proving that if None is returned, the target is truly unreachable from the start node, effectively proving that the \"frontier\" of the search has been exhausted without finding the target.\n", "spec": "import Mathlib\n\nstructure BFSGraph where\n adj : Array (Array Nat)\n\ndef BFSGraph.well_formed (g : BFSGraph) : Prop :=\n ∀ u, u < g.adj.size →\n ∀ v, v ∈ g.adj.getD u #[] → v < g.adj.size\n\ndef BFSGraph.size (g : BFSGraph) : Nat :=\n g.adj.size\n\ndef BFSGraph.has_edge (g : BFSGraph) (u v : Nat) : Prop :=\n u < g.adj.size ∧\n v ∈ g.adj.getD u #[]\n\ndef BFSGraph.is_path (g : BFSGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n g.has_edge (p.getD i 0) (p.getD (i + 1) 0)\n\ndef BFSGraph.reachable (g : BFSGraph) (start target : Nat) : Prop :=\n ∃ p, g.is_path p ∧ p.head? = some start ∧ p.getLast? = some target\n\ndef BFSGraph.is_shortest_distance (g : BFSGraph) (start target : Nat) (d : Nat) : Prop :=\n (∃ p, g.is_path p ∧ p.head? = some start ∧ p.getLast? = some target ∧ p.length = d + 1) ∧\n (∀ p, g.is_path p → p.head? = some start → p.getLast? = some target → p.length ≥ d + 1)\n\n-- Precondition definitions\n@[reducible, simp]\ndef bfs_shortest_path_precond (graph : BFSGraph) (start target : Nat) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧ graph.size ≤ 10000 ∧ start < graph.size ∧ target < graph.size\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef bfs_shortest_path (graph : BFSGraph) (start target : Nat)\n (h_precond : bfs_shortest_path_precond graph start target) : Option Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef bfs_shortest_path_postcond (graph : BFSGraph) (start target : Nat) (result : Option Nat)\n (_ : bfs_shortest_path_precond graph start target) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some dist => graph.is_shortest_distance start target dist\n | none => ¬ graph.reachable start target\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem bfs_shortest_path_postcond_satisfied (graph : BFSGraph) (start target : Nat)\n (h_precond : bfs_shortest_path_precond graph start target) :\n bfs_shortest_path_postcond graph start target\n (bfs_shortest_path graph start target h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "binary_search", "nl_desc": "Your task is to implement the Lower Bound Binary Search algorithm and verify its correctness in Lean. Given a sorted array of integers and a target value, find the first index i such that arr[i] >= target. If no such element exists, return arr.size. Postconditions: Result index 'res' satisfies: (1) res <= arr.size, (2) all elements before 'res' are strictly less than target, and (3) all elements at or after 'res' are greater than or equal to target. Preconditions: Input array is sorted. Array size fits in signed 32-bit integer. Implementation should be iterative or recursive O(log N).\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef binary_search_lower_bound_precond (seq : Array Int) (target : Int) : Prop :=\n -- !benchmark @start precond\n seq.size ≤ 0x7FFFFFFF ∧\n (∀ i j : Nat, i < j ∧ j < seq.size → seq.getD i 0 ≤ seq.getD j 0)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef binary_search_lower_bound (seq : Array Int) (target : Int) (h_precond : binary_search_lower_bound_precond seq target) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef binary_search_lower_bound_postcond (seq : Array Int) (target : Int) (result : Nat) (h_precond : binary_search_lower_bound_precond seq target) : Prop :=\n -- !benchmark @start postcond\n result ≤ seq.size ∧\n (∀ i : Nat, i < result → seq.getD i 0 < target) ∧\n (∀ i : Nat, result ≤ i ∧ i < seq.size → seq.getD i 0 ≥ target)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem binary_search_lower_bound_postcond_satisfied (seq : Array Int) (target : Int) (h_precond : binary_search_lower_bound_precond seq target) :\n binary_search_lower_bound_postcond seq target (binary_search_lower_bound seq target h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bipartite_check", "nl_desc": "Your task is to implement an algorithm to check if a graph is Bipartite (2-colorable) and verify its functional correctness in Lean using Mathlib. You need to implement check_bipartite, which attempts to color the graph with two colors (represented as booleans). It should return Some(colors) if successful, and None if the graph is not bipartite. There are several verification challenges: (1) Coloring Validity: If returning Some(colors), you must prove that for every edge $(u, v)$ in the graph, colors[u] != colors[v]. (2) Non-Bipartiteness: If returning None, you must prove that no valid 2-coloring exists. This is the hardest part. It requires proving that the conflict found (e.g., an edge connecting two nodes forced to be the same color) implies a contradiction for any possible valid coloring of the component. (3) Connectivity: Since the graph may be disconnected, you must prove the algorithm correctly handles all connected components, ensuring the property holds globally.\n", "spec": "import Mathlib\n\nstructure BipartiteGraph where\n adj : Array (Array Nat)\n\ndef BipartiteGraph.well_formed (g : BipartiteGraph) : Prop :=\n ∀ u, u < g.adj.size →\n ∀ v, v ∈ g.adj.getD u #[] → v < g.adj.size\n\ndef BipartiteGraph.size (g : BipartiteGraph) : Nat :=\n g.adj.size\n\ndef BipartiteGraph.has_edge (g : BipartiteGraph) (u v : Nat) : Prop :=\n u < g.adj.size ∧\n v ∈ g.adj.getD u #[]\n\ndef BipartiteGraph.is_valid_coloring (g : BipartiteGraph) (colors : Array Bool) : Prop :=\n colors.size = g.size ∧\n ∀ u v, g.has_edge u v →\n u < colors.size → v < colors.size → colors.getD u false ≠ colors.getD v false\n\ndef BipartiteGraph.is_bipartite (g : BipartiteGraph) : Prop :=\n ∃ colors, g.is_valid_coloring colors\n\n-- Precondition definitions\n@[reducible, simp]\ndef check_bipartite_precond (graph : BipartiteGraph) : Prop :=\n -- !benchmark @start precond\n graph.well_formed\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef check_bipartite (graph : BipartiteGraph)\n (h_precond : check_bipartite_precond graph) : Option (Array Bool) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef check_bipartite_postcond (graph : BipartiteGraph) (result : Option (Array Bool))\n (_ : check_bipartite_precond graph) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some colors => graph.is_valid_coloring colors\n | none => ¬ graph.is_bipartite\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem check_bipartite_postcond_satisfied (graph : BipartiteGraph)\n (h_precond : check_bipartite_precond graph) :\n check_bipartite_postcond graph (check_bipartite graph h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bracket_matching", "nl_desc": "Your task is to implement a linear scan algorithm for Bracket Matching and verify its correctness in Lean using Mathlib. The algorithm determines whether a sequence of characters $s$ (represented as UInt8 arrays) has perfectly balanced parentheses by maintaining a running balance counter. The operation proceeds by iterating through the string index $i$: (1) Initialization: Start with a balance counter variable $c$ set to $0$. (2) Iteration: Traverse the string character by character. Increment $c$ by $1$ for an open bracket '(' (40) and decrement $c$ by $1$ for a closed bracket ')' (41). (3) Early Failure: If $c$ drops below $0$ at any point, immediately return false. This enforces the constraint that a closing bracket must never appear before its corresponding opening bracket. (4) Final Check: After processing the entire string, return true only if $c$ is exactly $0$. Specifically, you must prove the following: (1) Functional Correctness: The executable boolean result must strictly match the formal definition of a valid bracket sequence, which requires that the sum of weights for the entire string is $0$ and the sum of weights for every prefix is non-negative (where '(' is $1$ and ')' is $-1$). (2) Loop Invariants: You must demonstrate that at every step $i$, the current counter value $c$ equals the mathematical summation of weights of the substring processed so far ($s[0..i]$), and that the non-negative prefix property holds for all prefixes within that range.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef bracket_match_precond (s : Array UInt8) : Prop :=\n -- !benchmark @start precond\n s.size ≤ 1000000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef bracket_match (s : Array UInt8) (_ : bracket_match_precond s) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Helper definitions\ndef char_weight (c : UInt8) : Int :=\n if c == 40 then 1 -- '('\n else if c == 41 then -1 -- ')'\n else 0\n\n-- Calculates total weight of a list of characters\ndef total_weight (s : List UInt8) : Int :=\n (s.map char_weight).sum\n\n-- Prefix property: all prefixes have non-negative total weight\ndef valid_prefix_weights (s : List UInt8) : Prop :=\n ∀ i, i ≤ s.length → total_weight (s.take i) ≥ 0\n\n-- Correctness predicate\ndef is_matched (s : List UInt8) : Prop :=\n total_weight s = 0 ∧ valid_prefix_weights s\n\n-- Postcondition definitions\n@[reducible, simp]\ndef bracket_match_postcond (s : Array UInt8) (result : Bool) (_ : bracket_match_precond s) : Prop :=\n -- !benchmark @start postcond\n result = is_matched s.toList\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem bracket_match_postcond_satisfied\n (s : Array UInt8) (h_precond : bracket_match_precond s) :\n bracket_match_postcond s (bracket_match s h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bst_delete", "nl_desc": "Your task is to implement a Binary Search Tree (BST) and verify that it correctly implements a mathematical Set in Lean. The tree must maintain the BST Invariant: for any node with value $v$, all values in the left subtree are less than $v$ and all values in the right subtree are greater than $v$. Focus on the delete operation. Delete: Prove that (1) the resulting tree maintains the BST invariant, and (2) the new model set is the old set minus the deleted value. You can implement the naive BST delete.\n", "spec": "import Mathlib\n\ninductive BinarySearchTree : Type\n| Empty : BinarySearchTree\n| Node : Int → BinarySearchTree → BinarySearchTree → BinarySearchTree\n\ndef view (t : BinarySearchTree) : Set Int :=\n match t with\n | BinarySearchTree.Empty => ∅\n | BinarySearchTree.Node v l r => view l ∪ view r ∪ {v}\n\ndef is_bst (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Empty => True\n | BinarySearchTree.Node v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Precondition definitions\n@[reducible, simp]\ndef delete_precond (t : BinarySearchTree) (v : Int) : Prop :=\n -- !benchmark @start precond\n is_bst t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef delete (t : BinarySearchTree) (v : Int)\n (h_precond : delete_precond t v) : BinarySearchTree :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef delete_postcond (t : BinarySearchTree) (v : Int) (result : BinarySearchTree)\n (_ : delete_precond t v) : Prop :=\n -- !benchmark @start postcond\n is_bst result ∧\n view result = view t \\ {v}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem delete_postcond_satisfied (t : BinarySearchTree) (v : Int)\n (h_precond : delete_precond t v) :\n delete_postcond t v (delete t v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bst_insert", "nl_desc": "Your task is to implement a Binary Search Tree (BST) and verify that it correctly implements a mathematical Set in Lean. The tree must maintain the BST Invariant: for any node with value $v$, all values in the left subtree are less than $v$ and all values in the right subtree are greater than $v$. Focus on the insert operation. Insert: Prove that (1) the resulting tree maintains the BST invariant, and (2) the new model set is the union of the old set and the inserted value. You can implement the naive BST insert.\n", "spec": "import Mathlib\n\ninductive BinarySearchTree : Type\n| Empty : BinarySearchTree\n| Node : Int → BinarySearchTree → BinarySearchTree → BinarySearchTree\n\ndef view (t : BinarySearchTree) : Set Int :=\n match t with\n | BinarySearchTree.Empty => ∅\n | BinarySearchTree.Node v l r => view l ∪ view r ∪ {v}\n\ndef is_bst (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Empty => True\n | BinarySearchTree.Node v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Precondition definitions\n@[reducible, simp]\ndef insert_precond (t : BinarySearchTree) (v : Int) : Prop :=\n -- !benchmark @start precond\n is_bst t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef insert (t : BinarySearchTree) (v : Int)\n (h_precond : insert_precond t v) : BinarySearchTree :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef insert_postcond (t : BinarySearchTree) (v : Int) (result : BinarySearchTree)\n (_ : insert_precond t v) : Prop :=\n -- !benchmark @start postcond\n is_bst result ∧\n view result = view t ∪ {v}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem insert_postcond_satisfied (t : BinarySearchTree) (v : Int)\n (h_precond : insert_precond t v) :\n insert_postcond t v (insert t v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bst_search", "nl_desc": "Your task is to implement a Binary Search Tree (BST) and verify that it correctly implements a search operation in Lean. The tree must maintain the BST Invariant: for any node with value $v$, all values in the left subtree are less than $v$ and all values in the right subtree are greater than $v$. Search: Prove that the function returns true if and only if the target value is a member of the mathematical set represented by the tree's view. You can implement the naive BST search.\n", "spec": "import Mathlib\n\ninductive BinarySearchTree : Type\n| Empty : BinarySearchTree\n| Node : Int → BinarySearchTree → BinarySearchTree → BinarySearchTree\n\ndef view (t : BinarySearchTree) : Set Int :=\n match t with\n | BinarySearchTree.Empty => ∅\n | BinarySearchTree.Node v l r => view l ∪ view r ∪ {v}\n\ndef is_bst (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Empty => True\n | BinarySearchTree.Node v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Precondition definitions\n@[reducible, simp]\ndef search_precond (t : BinarySearchTree) (v : Int) : Prop :=\n -- !benchmark @start precond\n is_bst t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef search (t : BinarySearchTree) (v : Int)\n (h_precond : search_precond t v) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef search_postcond (t : BinarySearchTree) (v : Int) (result : Bool)\n (_ : search_precond t v) : Prop :=\n -- !benchmark @start postcond\n result = (v ∈ view t)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem search_postcond_satisfied (t : BinarySearchTree) (v : Int)\n (h_precond : search_precond t v) :\n search_postcond t v (search t v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bst_zig", "nl_desc": "Your task is to implement a Binary Search Tree (BST) and verify that it correctly implements a mathematical Set in Lean. The tree must maintain the BST Invariant. Focus on the Zig operation (single right rotation). Zig: Prove that (1) the mathematical set of values (view) remains exactly the same, (2) the BST invariant is preserved, and (3) structural correctness: the original left child becomes the new root, and the original root becomes the new right child.\n", "spec": "import Mathlib\n\ninductive BinarySearchTree : Type\n| Empty : BinarySearchTree\n| Node : Int → BinarySearchTree → BinarySearchTree → BinarySearchTree\nderiving Inhabited\n\ndef view (t : BinarySearchTree) : Set Int :=\n match t with\n | BinarySearchTree.Empty => ∅\n | BinarySearchTree.Node v l r => view l ∪ view r ∪ {v}\n\ndef is_bst (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Empty => True\n | BinarySearchTree.Node v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\ndef get_left (t : BinarySearchTree) : Option BinarySearchTree :=\n match t with\n | BinarySearchTree.Node _ l _ => some l\n | _ => none\n\ndef get_right (t : BinarySearchTree) : Option BinarySearchTree :=\n match t with\n | BinarySearchTree.Node _ _ r => some r\n | _ => none\n\ndef get_val (t : BinarySearchTree) : Option Int :=\n match t with\n | BinarySearchTree.Node v _ _ => some v\n | _ => none\n\ndef BinarySearchTree.is_node (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Node _ _ _ => True\n | _ => False\n\n-- Precondition definitions\n@[reducible, simp]\ndef zig_precond (t : BinarySearchTree) : Prop :=\n -- !benchmark @start precond\n is_bst t ∧ \n BinarySearchTree.is_node t ∧ \n (match get_left t with | some l => BinarySearchTree.is_node l | none => False)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef zig (t : BinarySearchTree) \n (h_precond : zig_precond t) : BinarySearchTree :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef zig_postcond (t : BinarySearchTree) (result : BinarySearchTree)\n (_ : zig_precond t) : Prop :=\n -- !benchmark @start postcond\n is_bst result ∧\n view result = view t ∧\n -- Structural properties\n get_val result = get_val (get_left t).get! ∧\n BinarySearchTree.is_node (get_right result).get! ∧\n get_val (get_right result).get! = get_val t\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem zig_postcond_satisfied (t : BinarySearchTree)\n (h_precond : zig_precond t) :\n zig_postcond t (zig t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bst_zigzag", "nl_desc": "Your task is to implement the Zig-Zag operation (Left-Right Rotation) in a binary search tree and verify its correctness in Lean. A Zig-Zag is performed when node X lies between parent P and grandparent G (X is right child of P, P is left child of G). Prove: (1) Set Preservation: The tree's view is unchanged. (2) BST Invariant Preservation. (3) Structural Correctness: X becomes the new root, P becomes the left child of X, and G becomes the right child of X.\n", "spec": "import Mathlib\n\ninductive BinarySearchTree : Type\n| Empty : BinarySearchTree\n| Node : Int → BinarySearchTree → BinarySearchTree → BinarySearchTree\nderiving Inhabited\n\ndef view (t : BinarySearchTree) : Set Int :=\n match t with\n | BinarySearchTree.Empty => ∅\n | BinarySearchTree.Node v l r => view l ∪ view r ∪ {v}\n\ndef is_bst (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Empty => True\n | BinarySearchTree.Node v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\ndef get_left (t : BinarySearchTree) : Option BinarySearchTree :=\n match t with\n | BinarySearchTree.Node _ l _ => some l\n | _ => none\n\ndef get_right (t : BinarySearchTree) : Option BinarySearchTree :=\n match t with\n | BinarySearchTree.Node _ _ r => some r\n | _ => none\n\ndef get_val (t : BinarySearchTree) : Option Int :=\n match t with\n | BinarySearchTree.Node v _ _ => some v\n | _ => none\n\ndef BinarySearchTree.is_node (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Node _ _ _ => True\n | _ => False\n\n-- Precondition definitions\n@[reducible, simp]\ndef zigzag_precond (t : BinarySearchTree) : Prop :=\n -- !benchmark @start precond\n is_bst t ∧ \n BinarySearchTree.is_node t ∧ \n (match get_left t with \n | some p => BinarySearchTree.is_node p ∧ \n (match get_right p with \n | some x => BinarySearchTree.is_node x \n | none => False)\n | none => False)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef zigzag (t : BinarySearchTree) \n (h_precond : zigzag_precond t) : BinarySearchTree :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef zigzag_postcond (t : BinarySearchTree) (result : BinarySearchTree)\n (_ : zigzag_precond t) : Prop :=\n -- !benchmark @start postcond\n is_bst result ∧\n view result = view t ∧\n -- Structural properties: X (grandchild, originally P's right child) is now root\n get_val result = get_val (get_right (get_left t).get!).get!\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem zigzag_postcond_satisfied (t : BinarySearchTree)\n (h_precond : zigzag_precond t) :\n zigzag_postcond t (zigzag t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bst_zigzig", "nl_desc": "Your task is to implement the Zig-Zig operation (Double Right Rotation) in a binary search tree and verify its correctness in Lean. A Zig-Zig is performed when a node X, its parent P, and its grandparent G form a straight line (X is left child of P, P is left child of G). Prove: (1) Set Preservation: The tree's view is unchanged. (2) BST Invariant Preservation. (3) Structural Correctness: X becomes the new root, P becomes the right child of X, and G becomes the right child of P.\n", "spec": "import Mathlib\n\ninductive BinarySearchTree : Type\n| Empty : BinarySearchTree\n| Node : Int → BinarySearchTree → BinarySearchTree → BinarySearchTree\nderiving Inhabited\n\ndef view (t : BinarySearchTree) : Set Int :=\n match t with\n | BinarySearchTree.Empty => ∅\n | BinarySearchTree.Node v l r => view l ∪ view r ∪ {v}\n\ndef is_bst (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Empty => True\n | BinarySearchTree.Node v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\ndef get_left (t : BinarySearchTree) : Option BinarySearchTree :=\n match t with\n | BinarySearchTree.Node _ l _ => some l\n | _ => none\n\ndef get_right (t : BinarySearchTree) : Option BinarySearchTree :=\n match t with\n | BinarySearchTree.Node _ _ r => some r\n | _ => none\n\ndef get_val (t : BinarySearchTree) : Option Int :=\n match t with\n | BinarySearchTree.Node v _ _ => some v\n | _ => none\n\ndef BinarySearchTree.is_node (t : BinarySearchTree) : Prop :=\n match t with\n | BinarySearchTree.Node _ _ _ => True\n | _ => False\n\n-- Precondition definitions\n@[reducible, simp]\ndef zigzig_precond (t : BinarySearchTree) : Prop :=\n -- !benchmark @start precond\n is_bst t ∧ \n BinarySearchTree.is_node t ∧ \n (match get_left t with \n | some p => BinarySearchTree.is_node p ∧ \n (match get_left p with \n | some x => BinarySearchTree.is_node x \n | none => False)\n | none => False)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef zigzig (t : BinarySearchTree) \n (h_precond : zigzig_precond t) : BinarySearchTree :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef zigzig_postcond (t : BinarySearchTree) (result : BinarySearchTree)\n (_ : zigzig_precond t) : Prop :=\n -- !benchmark @start postcond\n is_bst result ∧\n view result = view t ∧\n -- Structural properties: X (grandchild) is now root\n get_val result = get_val (get_left (get_left t).get!).get!\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem zigzig_postcond_satisfied (t : BinarySearchTree)\n (h_precond : zigzig_precond t) :\n zigzig_postcond t (zigzig t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "bubble_sort", "nl_desc": "Your task is to implement the Bubble Sort algorithm and verify its correctness in Lean. Given a list of integers, return a new list containing the same elements but in non-decreasing order. Postconditions: (1) The output list is sorted. (2) The output list is a permutation of the input list. Preconditions: None (valid for any list which fits in memory). Implementation should use the standard O(N^2) bubble sort logic: repeatedly iterate through the list, swapping adjacent elements if they are in the wrong order, until no swaps are needed.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef bubble_sort_precond (v : List Int) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef bubble_sort (v : List Int) (h_precond : bubble_sort_precond v) : List Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef bubble_sort_postcond (v : List Int) (result : List Int) (h_precond : bubble_sort_precond v) : Prop :=\n -- !benchmark @start postcond\n List.Sorted (· ≤ ·) result ∧ result.Perm v\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem bubble_sort_postcond_satisfied (v : List Int) (h_precond : bubble_sort_precond v) :\n bubble_sort_postcond v (bubble_sort v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "coin_change", "nl_desc": "Your task is to implement the Coin Change algorithm and verify its correctness in Lean. Given a set of coin denominations and a target amount, find the minimum number of coins needed to make up that amount, or return -1 if impossible. Postconditions: If solvable, result satisfies Existence (valid count of coins yielding amount) and Optimality (minimal count). If impossible (-1), no valid combination exists. Coins are positive.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef coin_change_precond (coins : List Nat) (amount : Nat) : Prop :=\n coins.length > 0 ∧\n coins.length ≤ 100 ∧\n amount ≤ 10000 ∧\n ∀ i, i < coins.length → (coins.getD i 0 > 0 ∧ coins.getD i 0 ≤ 10000)\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef coin_change (coins : List Nat) (amount : Nat)\n (h_precond : coin_change_precond coins amount) : Int :=\n -- !benchmark @start code\n -- A simple placeholder implementation; the detailed DP algorithm is omitted for\n -- brevity and to keep the code easily type‑checkable.\n sorry\n -- !benchmark @end code\n\n/-! ## Postcondition auxiliary definitions -/\ndef listNatToInt (s : List Nat) : List Int :=\n s.map (fun x => (x : Int))\n\ndef total_amount (counts : List Int) (coins : List Int) : Int :=\n (counts.zip coins).foldl (fun acc pair => acc + pair.1 * pair.2) 0\n\ndef total_coins (counts : List Int) : Int :=\n counts.foldl (fun acc c => acc + c) 0\n\ndef is_valid_change (counts : List Int) (coins : List Int) (amount : Int) : Prop :=\n counts.length = coins.length ∧\n (∀ i, i < counts.length → counts.getD i 0 ≥ 0) ∧\n total_amount counts coins = amount\n\n-- Postcondition definitions\n@[reducible, simp]\ndef coin_change_postcond (coins : List Nat) (amount : Nat) (result : Int)\n (h_precond : coin_change_precond coins amount) : Prop :=\n -- !benchmark @start postcond\n (result ≠ -1 →\n (∃ counts : List Int,\n is_valid_change counts (listNatToInt coins) (amount : Int) ∧\n total_coins counts = result) ∧\n (∀ counts : List Int,\n is_valid_change counts (listNatToInt coins) (amount : Int) →\n total_coins counts ≥ result))\n ∧\n (result = -1 →\n ∀ counts : List Int,\n is_valid_change counts (listNatToInt coins) (amount : Int) → False)\n -- !benchmark @end postcond\n\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem coin_change_postcond_satisfied (coins : List Nat) (amount : Nat)\n (h_precond : coin_change_precond coins amount) :\n coin_change_postcond coins amount (coin_change coins amount h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "cycle_detection", "nl_desc": "Your task is to implement an algorithm to detect if a directed graph (unweighted) contains a cycle and verify its functional correctness in Lean using Mathlib. You need to implement the function has_cycle, which returns true if the graph contains at least one cycle, and false if it is a Directed Acyclic Graph (DAG). The graph is represented as an Array of Array Nat (adjacency list). There are several verification challenges: (1) Soundness (Cycle Existence): Proving that if the function returns true, there indeed exists a path $p$ starting and ending at the same node with length $>1$. (2) Completeness (Acyclicity): Proving that if the function returns false, no such cycle exists in the entire graph. (3) Termination: Since the goal is to find a cycle, you must prove the algorithm terminates even when cycles are present, typically by tracking a set of visited nodes.\n", "spec": "import Mathlib\n\nstructure CycleGraph where\n adj : Array (Array Nat)\n\ndef CycleGraph.well_formed (g : CycleGraph) : Prop :=\n ∀ u, u < g.adj.size →\n ∀ v, v ∈ g.adj.getD u #[] → v < g.adj.size\n\ndef CycleGraph.size (g : CycleGraph) : Nat :=\n g.adj.size\n\ndef CycleGraph.has_edge (g : CycleGraph) (u v : Nat) : Prop :=\n u < g.adj.size ∧\n v ∈ g.adj.getD u #[]\n\ndef CycleGraph.is_path (g : CycleGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n g.has_edge (p.getD i 0) (p.getD (i + 1) 0)\n\ndef CycleGraph.is_cycle (g : CycleGraph) (p : List Nat) : Prop :=\n g.is_path p ∧ p.length > 1 ∧ p.head? = p.getLast?\n\ndef CycleGraph.graph_has_cycle (g : CycleGraph) : Prop :=\n ∃ p, g.is_cycle p\n\n-- Precondition definitions\n@[reducible, simp]\ndef has_cycle_precond (graph : CycleGraph) : Prop :=\n -- !benchmark @start precond\n graph.well_formed\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef has_cycle (graph : CycleGraph)\n (h_precond : has_cycle_precond graph) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef has_cycle_postcond (graph : CycleGraph) (result : Bool)\n (_ : has_cycle_precond graph) : Prop :=\n -- !benchmark @start postcond\n result = graph.graph_has_cycle\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem has_cycle_postcond_satisfied (graph : CycleGraph)\n (h_precond : has_cycle_precond graph) :\n has_cycle_postcond graph (has_cycle graph h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "dfs", "nl_desc": "Your task is to implement a Depth First Search (DFS) algorithm to determine reachability in a directed graph and verify its functional correctness in Lean using Mathlib. The graph is represented as an adjacency list (Array of Array Nat). You need to implement dfs_check_reachability, which returns true if and only if a path exists from the start node to the target node. There are several verification challenges: (1) Soundness (Path Validity): Proving that if the function returns true, a valid sequence of edges actually exists connecting the start to the target. (2) Completeness (Unreachability): Proving that if the function returns false, no such path exists. This is the hardest part; it requires a strong loop invariant relating the visited set to the stack. You typically need to prove that \"any path from the start to an unvisited node must pass through a node currently in the stack.\" (3) Termination: Proving that the algorithm finishes. Since cycles may exist in the graph, you must prove that the visited set grows (or the count of unvisited nodes decreases) to guarantee termination.\n", "spec": "import Mathlib\n\nstructure DFSGraph where\n adj : Array (Array Nat)\n\ndef DFSGraph.well_formed (g : DFSGraph) : Prop :=\n ∀ u, u < g.adj.size →\n ∀ v, v ∈ g.adj.getD u #[] → v < g.adj.size\n\ndef DFSGraph.size (g : DFSGraph) : Nat :=\n g.adj.size\n\ndef DFSGraph.has_edge (g : DFSGraph) (u v : Nat) : Prop :=\n u < g.adj.size ∧\n v ∈ g.adj.getD u #[]\n\ndef DFSGraph.is_path (g : DFSGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n g.has_edge (p.getD i 0) (p.getD (i + 1) 0)\n\ndef DFSGraph.reachable (g : DFSGraph) (start target : Nat) : Prop :=\n ∃ p, g.is_path p ∧ p.head? = some start ∧ p.getLast? = some target\n\n-- Precondition definitions\n@[reducible, simp]\ndef dfs_check_reachability_precond (graph : DFSGraph) (start target : Nat) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧ start < graph.size ∧ target < graph.size\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef dfs_check_reachability (graph : DFSGraph) (start target : Nat)\n (h_precond : dfs_check_reachability_precond graph start target) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef dfs_check_reachability_postcond (graph : DFSGraph) (start target : Nat) (result : Bool)\n (_ : dfs_check_reachability_precond graph start target) : Prop :=\n -- !benchmark @start postcond\n result = graph.reachable start target\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem dfs_check_reachability_postcond_satisfied (graph : DFSGraph) (start target : Nat)\n (h_precond : dfs_check_reachability_precond graph start target) :\n dfs_check_reachability_postcond graph start target\n (dfs_check_reachability graph start target h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "dijkstra", "nl_desc": "Your task is to implement Dijkstra's Algorithm for single-source shortest paths and verify its functional correctness in Lean using Mathlib. The input is a WeightedGraph where edge weights are Int. Preconditions: You can assume the graph contains at most 100,000 nodes and edge weights are between -100,000 and 100,000 (inclusive). Crucially, the graph is a Simple Graph (no parallel edges between the same two nodes) and all edge weights are non-negative ($w \\ge 0$). Requirements: Implement the function dijkstra_shortest_paths which returns a list of Option Int. If a node v is reachable, the sequence at index v must contain Some(d), where d is the unique minimal path weight from the start node to v. If a node v is unreachable, the sequence must contain None. Verification Challenges: (1) Relaxation Invariant: Proving that when a node u is extracted from the priority queue, its recorded distance is the true shortest path distance. (2) Queue Invariants: Proving that the priority queue correctly maintains the \"frontier\" of the search. (3) Path Minimality: Proving that no path exists with weight strictly less than the returned distance.\n", "spec": "import Mathlib\n\nstructure WeightedGraph where\n adj : Array (Array (Nat × Int))\n\ndef WeightedGraph.size (g : WeightedGraph) : Nat :=\n g.adj.size\n\ndef WeightedGraph.has_edge (g : WeightedGraph) (u v : Nat) (w : Int) : Prop :=\n u < g.size ∧\n ∃ pair, pair ∈ g.adj.getD u #[] ∧ pair.1 = v ∧ pair.2 = w\n\ndef WeightedGraph.well_formed (g : WeightedGraph) : Prop :=\n ∀ u, u < g.size →\n -- All targets in range\n (∀ pair, pair ∈ g.adj.getD u #[] → pair.1 < g.size) ∧\n -- Unique targets (simple graph)\n (∀ p1 p2, p1 ∈ g.adj.getD u #[] → p2 ∈ g.adj.getD u #[] → p1.1 = p2.1 → p1 = p2)\n\ndef WeightedGraph.get_edge_weight (g : WeightedGraph) (_ _ : Nat) : Int :=\n -- Since edges are unique, we can abstractly pick the weight\n -- In specification logic, we can use `Classical.choose` or similar if needed.\n -- But since we pass adjacency list, we can just say \"w s.t. has_edge\"\n -- For simple functional definition:\n 0 -- placeholder for helper, spec logic mainly uses has_edge\n\ndef WeightedGraph.is_path (g : WeightedGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n ∃ w, g.has_edge (p.getD i 0) (p.getD (i + 1) 0) w\n\ndef WeightedGraph.path_weight (_ : WeightedGraph) (p : List Nat) : Int :=\n match p with\n | [] => 0\n | [_] => 0\n | _ :: _ :: _ =>\n -- This assumes unique edges logic or we need an explicit path including weights\n -- For simplicity, we assume `has_edge` defines the unique weight\n -- and we sum it up. However, extracting it in logic is annoying.\n -- Let's define path_weight relationally instead.\n 0 \n\ndef WeightedGraph.path_has_weight (g : WeightedGraph) (p : List Nat) (w_total : Int) : Prop :=\n match p with\n | [] => w_total = 0\n | [_] => w_total = 0\n | u :: v :: rest =>\n ∃ w_edge w_rest,\n g.has_edge u v w_edge ∧\n g.path_has_weight (v :: rest) w_rest ∧\n w_total = w_edge + w_rest\n\ndef WeightedGraph.is_shortest_dist (g : WeightedGraph) (start target : Nat) (d : Int) : Prop :=\n (∃ p, g.is_path p ∧ p.head? = some start ∧ p.getLast? = some target ∧ g.path_has_weight p d) ∧\n (∀ p w, g.is_path p → p.head? = some start → p.getLast? = some target → g.path_has_weight p w → w >= d)\n\ndef WeightedGraph.weights_non_negative (g : WeightedGraph) : Prop :=\n ∀ u v w, g.has_edge u v w → w >= 0\n\ndef WeightedGraph.weights_and_size_bounded (g : WeightedGraph) : Prop :=\n g.size ≤ 100000 ∧\n ∀ u v w, g.has_edge u v w → w ≥ -100000 ∧ w ≤ 100000\n\n-- Precondition definitions\n@[reducible, simp]\ndef dijkstra_shortest_paths_precond (graph : WeightedGraph) (start : Nat) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧\n start < graph.size ∧\n graph.weights_non_negative ∧\n graph.weights_and_size_bounded\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef dijkstra_shortest_paths (graph : WeightedGraph) (start : Nat)\n (h_precond : dijkstra_shortest_paths_precond graph start) : List (Option Int) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef dijkstra_shortest_paths_postcond (graph : WeightedGraph) (start : Nat)\n (result : List (Option Int))\n (_ : dijkstra_shortest_paths_precond graph start) : Prop :=\n -- !benchmark @start postcond\n result.length = graph.size ∧\n ∀ v, v < graph.size →\n match result.getD v none with\n | some d => graph.is_shortest_dist start v d\n | none => ¬ ∃ p, graph.is_path p ∧ p.head? = some start ∧ p.getLast? = some v\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem dijkstra_shortest_paths_postcond_satisfied (graph : WeightedGraph) (start : Nat)\n (h_precond : dijkstra_shortest_paths_precond graph start) :\n dijkstra_shortest_paths_postcond graph start\n (dijkstra_shortest_paths graph start h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "discrete_logarithm", "nl_desc": "Your task is to implement a solver for the Discrete Logarithm Problem and verify its correctness in Lean. Given a base g, a target h, and a modulus p, find the smallest non-negative integer x < p such that g^x ≡ h (mod p), or return none if no such x exists. Algorithm: Use a naive linear search. Iterate `x` from 0 to `p-1`. Maintain a running product for `g^x % p` to avoid recomputing the power from scratch. Minimality: If multiple solutions exist, you must return the smallest `x`. Unsolvable Case: If no such `x` exists in the range `[0, p)`, return `none`. Goal: Find a non-negative integer `x` (where `0 ≤ x < p`) such that `(g.toNat ^ x) % p.toNat = h.toNat % p.toNat`. Preconditions: * Inputs: The generator `g`, target `h`, and modulus `p` are `UInt64`. * Constraint: `p.toNat > 1`. Postconditions: Implement `discrete_log_naive` which returns `Option UInt64`. Verification Challenges: (1) Incremental Modular Exponentiation: You are not calling a power function inside the loop; you are updating `current_val = (current_val * g) % p`. You must prove the invariant that at iteration `i`, `current_val == (Nat.pow g i) % p`. (2) Negative Proof (None): The hardest part is the `none` case. If the loop finishes without returning, you must prove the universal quantifier: `for all k < p, (Nat.pow g k) % p \\ne h % p`. (3) Minimality (Some): When you return `some x`, you must prove that no earlier index `k < x` was a solution. Since your loop searches strictly from 0 upwards, this follows from the control flow, but you must capture it in the loop invariant (\"for all checked k, g^k % p\\ne h % p\").", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef discrete_log_naive_precond (g h p : UInt64) : Prop :=\n -- !benchmark @start precond\n p.toNat > 1\n -- !benchmark @end precond\n\ndef is_discrete_log (g h p : Nat) (x : Nat) : Prop :=\n (Nat.pow g x) % p = h % p\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef discrete_log_naive (g h p : UInt64) (h_precond : discrete_log_naive_precond g h p) : Option UInt64 :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef discrete_log_naive_postcond (g h p : UInt64) (result : Option UInt64) (h_precond : discrete_log_naive_precond g h p) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some x => is_discrete_log g.toNat h.toNat p.toNat x.toNat ∧ x.toNat < p.toNat ∧\n ∀ k : Nat, k < x.toNat → ¬ is_discrete_log g.toNat h.toNat p.toNat k\n | none => ∀ k : Nat, k < p.toNat → ¬ is_discrete_log g.toNat h.toNat p.toNat k\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem discrete_log_naive_postcond_satisfied (g h p : UInt64) (h_precond : discrete_log_naive_precond g h p) :\n discrete_log_naive_postcond g h p (discrete_log_naive g h p h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "edmond_karp", "nl_desc": "Your task is to implement the Edmonds-Karp Algorithm to find the maximum flow in a directed graph with edge capacities and verify its functional correctness in Lean using Mathlib. Preconditions: You can assume the graph contains at most 1,000 nodes. You can assume edge capacities are between 0 and 100,000. The Source s and Sink t are valid, distinct nodes. Crucially, the graph is a Simple Graph. Requirements: Implement the function max_flow_value which returns the integer value of the max flow. Algorithm: Edmonds-Karp uses BFS to find augmenting paths. Output: The returned value must be equal to the net flow out of the source in a valid maximum flow assignment. Verification Challenges: (1) Residual Graph Correctness. (2) Termination. (3) Optimality (Min-Cut).\n", "spec": "import Mathlib\n\nstructure CapacityGraph where\n adj : Array (Array (Nat × Int))\n\ndef CapacityGraph.size (g : CapacityGraph) : Nat :=\n g.adj.size\n\ndef CapacityGraph.has_capacity (g : CapacityGraph) (u v : Nat) (c : Int) : Prop :=\n u < g.size ∧\n ∃ pair, pair ∈ g.adj.getD u #[] ∧ pair.1 = v ∧ pair.2 = c\n\ndef CapacityGraph.well_formed (g : CapacityGraph) : Prop :=\n ∀ u, u < g.size →\n (∀ pair, pair ∈ g.adj.getD u #[] → pair.1 < g.size) ∧\n -- Unique targets\n (∀ p1 p2, p1 ∈ g.adj.getD u #[] → p2 ∈ g.adj.getD u #[] → p1.1 = p2.1 → p1 = p2)\n\n-- A flow map assigns a flow value to each edge (u, v)\n-- We can model this as a function or list of entries.\n-- Since the result is just the max flow value, we can keep FlowMap abstract in the spec\n-- or define it as `Nat -> Nat -> Int`.\n-- Dijkstra and others used abstract/implicit paths. here we need explicit flow values.\ndef FlowMap := Nat → Nat → Int\n\ndef FlowMap.get (f : FlowMap) (u v : Nat) : Int := f u v\n\ndef CapacityGraph.respects_capacity (g : CapacityGraph) (f : FlowMap) : Prop :=\n ∀ u v,\n (f.get u v > 0 →\n ∃ c, g.has_capacity u v c ∧ f.get u v ≤ c) ∧\n (f.get u v >= 0) -- Non-negative flow\n\ndef flow_out (g : CapacityGraph) (f : FlowMap) (u : Nat) : Int :=\n -- Sum of f(u, v) for all v. Hard to define as sum over infinite/large domain without Finset.\n -- But we can sum over the adjacency list of u.\n -- Since well_formed ensures unique neighbors, we can map adj to flow and sum.\n let neighbors := g.adj.getD u #[]\n neighbors.foldl (λ sum pair => sum + f.get u pair.1) 0\n\ndef flow_in (g : CapacityGraph) (f : FlowMap) (u : Nat) : Int :=\n -- Sum of f(v, u). This requires iterating over all nodes v that have edge to u.\n -- We can iterate over all nodes v from 0 to size-1.\n -- This is a bit computationally heavy for definition but fine for spec.\n (List.range g.size).foldl (λ sum v => sum + f.get v u) 0\n\ndef is_conserved (g : CapacityGraph) (f : FlowMap) (s t : Nat) : Prop :=\n ∀ u, u < g.size → u ≠ s → u ≠ t →\n flow_in g f u = flow_out g f u\n\ndef is_valid_flow (g : CapacityGraph) (f : FlowMap) (s t : Nat) : Prop :=\n g.respects_capacity f ∧ is_conserved g f s t\n\ndef flow_val (g : CapacityGraph) (f : FlowMap) (s : Nat) : Int :=\n flow_out g f s - flow_in g f s\n\ndef is_max_flow (g : CapacityGraph) (f : FlowMap) (s t : Nat) : Prop :=\n is_valid_flow g f s t ∧\n ∀ other, is_valid_flow g other s t → flow_val g f s ≥ flow_val g other s\n\ndef CapacityGraph.capacities_bounded (g : CapacityGraph) : Prop :=\n g.size ≤ 1000 ∧\n ∀ u v c, g.has_capacity u v c → c ≥ 0 ∧ c ≤ 100000\n\n-- Precondition definitions\n@[reducible, simp]\ndef max_flow_value_precond (graph : CapacityGraph) (s t : Nat) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧\n graph.capacities_bounded ∧\n s < graph.size ∧\n t < graph.size ∧\n s ≠ t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef max_flow_value (graph : CapacityGraph) (s t : Nat)\n (h_precond : max_flow_value_precond graph s t) : Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef max_flow_value_postcond (graph : CapacityGraph) (s t : Nat) (result : Int)\n (_ : max_flow_value_precond graph s t) : Prop :=\n -- !benchmark @start postcond\n ∃ f, is_max_flow graph f s t ∧ flow_val graph f s = result\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem max_flow_value_postcond_satisfied (graph : CapacityGraph) (s t : Nat)\n (h_precond : max_flow_value_precond graph s t) :\n max_flow_value_postcond graph s t (max_flow_value graph s t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "fast_exponential", "nl_desc": "Your task is to implement the Fast Exponentiation Algorithm (Exponentiation by Squaring) and verify its correctness in Lean. Given a base b and a non-negative exponent e, compute b to the power of e in logarithmic time complexity. Preconditions: Result must fit within 64-bit unsigned integer limits (UInt64). Postconditions: Implement the function `exponentiation`. (1) Structure: Implementation should involve `exponentiation_by_squaring_aux`. (2) Functional Correctness: You must verify that your implementation returns exactly `b ^ e` (as defined by `Nat.pow`). (3) Safety: You must prove that neither the accumulator updates nor the squaring of the base ever cause a `UInt64` overflow. Verification Challenges: (1) Algebraic Lemmas: The core logic relies on $(b^2)^k = b^{2k}$. You must state and verify helper lemmas to handle this non-linear arithmetic. (2) Conservation Invariant: You must prove that at every step of the recursion/loop, $current\\_res \\times current\\_base^{current\\_exp} = original\\_base^{original\\_exp}$. (3) Intermediate Overflow Safety: You must prove that the base `b` is never squared if it would exceed `UInt64` limits, or that such squaring is avoided when unnecessary.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef exponentiation_precond (b e : UInt64) : Prop :=\n -- !benchmark @start precond\n b.toNat ^ e.toNat ≤ (2 ^ 64 - 1)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef exponentiation (b e : UInt64) (h_precond : exponentiation_precond b e) : UInt64 :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef exponentiation_postcond (b e : UInt64) (result : UInt64) (h_precond : exponentiation_precond b e) : Prop :=\n -- !benchmark @start postcond\n result.toNat = b.toNat ^ e.toNat\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem exponentiation_postcond_satisfied (b e : UInt64)\n (h_precond : exponentiation_precond b e) :\n exponentiation_postcond b e (exponentiation b e h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "gcd", "nl_desc": "Your task is to implement the Euclidean Algorithm for Greatest Common Divisor (GCD) and verify its functional correctness in Lean. Given two non-negative integers a and b, compute the largest integer that divides both a and b. Postconditions: Result is a common divisor of a and b, and any other common divisor divides the result. Preconditions: a and b are non-negative. Implementation should use standard Euclidean algorithm (recursive or iterative).", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef compute_gcd_precond (a : UInt64) (b : UInt64) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef compute_gcd (a : UInt64) (b : UInt64) (h_precond : compute_gcd_precond a b) : UInt64 :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef compute_gcd_postcond (a : UInt64) (b : UInt64) (result : UInt64) (h_precond : compute_gcd_precond a b) : Prop :=\n -- !benchmark @start postcond\n result.toNat = Nat.gcd (UInt64.toNat a) (UInt64.toNat b)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem compute_gcd_postcond_satisfied (a : UInt64) (b : UInt64) (h_precond : compute_gcd_precond a b) :\n compute_gcd_postcond a b (compute_gcd a b h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "house_robber", "nl_desc": "Your task is to implement the House Robber algorithm and verify its correctness in Lean. Given a list of house values, maximize the total loot without robbing adjacent houses. Postconditions: Result is the value of some valid non-adjacent robbery plan, and no valid plan yields a higher value. Values are non-negative.\n", "spec": "import Mathlib\n\ndef seq_u64_to_int (xs : List Nat) : List Nat := xs\n\ndef is_valid_robbery (houses : List Nat) (len : Nat) : Prop :=\n (∀ i, i < houses.length → houses.getD i 0 < len) ∧\n (∀ i, i < houses.length - 1 → houses.getD (i+1) 0 ≥ houses.getD i 0 + 2)\n\ndef total_loot (houses : List Nat) (values : List Nat) : Nat :=\n houses.foldl (fun acc i => acc + values.getD i 0) 0\n\n-- Precondition definition\n@[simp, reducible]\ndef rob_precond (nums : List Nat) : Prop :=\n -- !benchmark @start precond\n nums.length ≤ 100 ∧\n ∀ i, i < nums.length → nums.getD i 0 ≤ 10000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function (dynamic programming solution)\ndef rob (nums : List Nat) (h_precond : rob_precond nums) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definition\n@[simp, reducible]\ndef rob_postcond (nums : List Nat) (result : Nat) (h_precond : rob_precond nums) : Prop :=\n -- !benchmark @start postcond\n (∀ houses : List Nat,\n is_valid_robbery houses nums.length →\n total_loot houses (seq_u64_to_int nums) ≤ result) ∧\n (∃ houses : List Nat,\n is_valid_robbery houses nums.length ∧\n total_loot houses (seq_u64_to_int nums) = result)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof that the implementation satisfies the postcondition\ntheorem rob_postcond_satisfied (nums : List Nat) (h_precond : rob_precond nums) :\n rob_postcond nums (rob nums h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "insertion_sort", "nl_desc": "Your task is to implement the Insertion Sort algorithm and verify its correctness in Lean. Given a list of integers, return a sorted permutation of the list. Postconditions: (1) Output is sorted. (2) Output is a permutation of input. Preconditions: None. Implementation: Recursive or iterative insertion sort. Insert each element into the sorted prefix.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef insertionSort_precond (v : List Int) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef insertionSort (v : List Int) (h_precond : insertionSort_precond v) : List Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef insertionSort_postcond (v : List Int) (result : List Int) (h_precond : insertionSort_precond v) : Prop :=\n -- !benchmark @start postcond\n List.Sorted (· ≤ ·) result ∧ result.Perm v\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem insertionSort_postcond_satisfied (v : List Int) (h_precond : insertionSort_precond v) :\n insertionSort_postcond v (insertionSort v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "integer_exponential", "nl_desc": "Your task is to implement Integer Exponentiation and verify its correctness in Lean. Given a base b and a non-negative exponent e, compute b to the power of e. Preconditions: The input base b and exponent e are natural numbers (Nat). Postconditions: Implement the function to compute the exponential. (1) Functional Correctness: You must verify that your implementation returns exactly b ^ e (as strictly defined by Lean's Nat.pow). (2) Termination: You must prove that your recursive implementation terminates. Verification Challenges: (1) Accumulation Invariant: You must prove the invariant (or recursion hypothesis) that ensures the computation correctly builds up to the final power b^e.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef exponentiation_precond (b e : Nat) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef exponentiation (b e : Nat) (h_precond : exponentiation_precond b e) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef exponentiation_postcond (b e result : Nat) (h_precond : exponentiation_precond b e) : Prop :=\n -- !benchmark @start postcond\n result = b ^ e\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem exponentiation_postcond_satisfied (b e : Nat)\n (h_precond : exponentiation_precond b e) :\n exponentiation_postcond b e (exponentiation b e h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "jump_game", "nl_desc": "Your task is to implement the Jump Game algorithm and verify its correctness in Lean. Given an array of non-negative integers where each represents the max jump length from that position, determine if you can reach the last index starting from index 0. Postconditions: Result is true iff there exists a valid sequence of jumps from 0 to last index. A valid jump at i lands on j where i < j <= i + nums[i].\n", "spec": "import Mathlib\n\n-- Precondition definition\n@[reducible, simp]\ndef can_jump_precond (nums : List Nat) : Prop :=\n -- !benchmark @start precond\n 0 < nums.length ∧\n nums.length ≤ 10000 ∧\n (∀ i, i < nums.length → nums.getD i 0 ≤ 10000)\n -- !benchmark @end precond\n\ndef is_valid_jump_path (path : List Nat) (nums : List Nat) : Prop :=\n path.length > 0 ∧\n path.head? = some 0 ∧\n path.getLast? = some (nums.length - 1) ∧\n (∀ i, i < path.length → path.getD i 0 < nums.length) ∧\n (∀ i, i < path.length - 1 →\n let u := path.getD i 0\n let v := path.getD (i+1) 0\n u < v ∧ v ≤ u + nums.getD u 0)\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef can_jump (nums : List Nat) (h_precond : can_jump_precond nums) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definition\n@[reducible, simp]\ndef can_jump_postcond (nums : List Nat) (result : Bool) (h_precond : can_jump_precond nums) : Prop :=\n -- !benchmark @start postcond\n (result = true) ↔ ∃ (path : List Nat), is_valid_jump_path path nums\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem can_jump_postcond_satisfied (nums : List Nat) (h_precond : can_jump_precond nums) :\n can_jump_postcond nums (can_jump nums h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "k_smallest", "nl_desc": "Your task is to implement the Quick Select algorithm to find the k-th smallest element and verify its correctness in Lean. Given a list of integers and an index k, return the k-th smallest element (0-indexed). Postconditions: The result is the element that would be at index k if the array were sorted. Preconditions: k < array.size. Implementation: Recursive partition-based selection (like Quick Sort partition). Return the element if the pivot index is k, otherwise recurse into the appropriate sub-array. Time complexity should be O(N) on average.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef quick_select_precond (v : Array Int) (k : Nat) : Prop :=\n -- !benchmark @start precond\n k < v.size\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef quick_select (v : Array Int) (k : Nat) (h_precond : quick_select_precond v k) : Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef quick_select_postcond (v : Array Int) (k : Nat) (result : Int) (h_precond : quick_select_precond v k) : Prop :=\n -- !benchmark @start postcond\n ∃ sorted : List Int,\n sorted.length = v.size ∧\n List.Sorted (· ≤ ·) sorted ∧\n sorted.getD k 0 = result ∧\n sorted.Perm v.toList\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem quick_select_postcond_satisfied (v : Array Int) (k : Nat) (h_precond : quick_select_precond v k) :\n quick_select_postcond v k (quick_select v k h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "kmp", "nl_desc": "Your task is to implement the Knuth-Morris-Pratt (KMP) algorithm to efficiently find all occurrences of a needle (pattern) within a haystack (text) and verify its functional correctness in Lean using Mathlib. Preconditions: You can assume the input text and pattern are arrays of UInt8 (representing bytes) and their lengths are at most 1,000,000. Requirements: Implement the method kmpSearch which returns an Array Nat. (1) Preprocessing: You must implement a helper to compute the Longest Prefix Suffix (LPS) array (or \"next\" array), where lps[i] stores the length of the longest proper prefix of the sub-pattern that is also a suffix of that sub-pattern. (2) Search: The main loop must use the LPS array to skip unnecessary comparisons in the text when a mismatch occurs, achieving linear time complexity. (3) Functional Correctness: Prove that despite the optimization, the result is identical to a naive search (Soundness and Completeness). Verification Challenges: (1) LPS Property: Proving that your preprocessing logic actually satisfies the mathematical definition of the LPS table. (2) Safe Skipping: Proving that the \"jump\" logic (moving the pattern index based on LPS) never skips a valid match. (3) Complex Invariants: The main loop invariant must relate the current state of the pattern index to the prefix of the pattern matched against the text so far.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef kmpSearch_precond (haystack : Array UInt8) (needle : Array UInt8) : Prop :=\n -- !benchmark @start precond\n haystack.size < 1000000 ∧ \n needle.size < 1000000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef kmpSearch (haystack : Array UInt8) (needle : Array UInt8)\n (_ : kmpSearch_precond haystack needle) : Array Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition auxiliary definitions\ndef matchesAt (haystack needle : Array UInt8) (i : Nat) : Prop :=\n i + needle.size ≤ haystack.size ∧\n (∀ j, j < needle.size → haystack.getD (i + j) 0 = needle.getD j 0)\n\n-- Postcondition definitions\n@[reducible, simp]\ndef kmpSearch_postcond (haystack : Array UInt8) (needle : Array UInt8)\n (result : Array Nat) (_ : kmpSearch_precond haystack needle) : Prop :=\n -- !benchmark @start postcond\n (∀ i, i < result.size → matchesAt haystack needle (result.getD i 0)) ∧ -- Soundness\n (∀ i, matchesAt haystack needle i → ∃ k, k < result.size ∧ result.getD k 0 = i) -- Completeness\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem kmpSearch_postcond_satisfied (haystack : Array UInt8) (needle : Array UInt8)\n (h_precond : kmpSearch_precond haystack needle) :\n kmpSearch_postcond haystack needle (kmpSearch haystack needle h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof"}
{"problem_id": "knapsack_01", "nl_desc": "Your task is to implement the 0/1 Knapsack algorithm and verify its correctness in Lean. Given weights and values for n items and a capacity W, determine the maximum total value of items that can be selected without exceeding W. Postconditions: Upper Bound (result ≥ value of any valid selection) and Existence (result = value of some valid selection). Valid selection means total weight ≤ W.\n", "spec": "import Mathlib\n\ndef total_weight (selected : List Bool) (weights : List Nat) : Nat :=\n match selected, weights with\n | [], _ => 0\n | _, [] => 0\n | b :: bs, w :: ws => (if b then w else 0) + total_weight bs ws\n\ndef total_value (selected : List Bool) (values : List Nat) : Nat :=\n match selected, values with\n | [], _ => 0\n | _, [] => 0\n | b :: bs, v :: vs => (if b then v else 0) + total_value bs vs\n\ndef is_valid_strategy (selected : List Bool) (weights : List Nat) (capacity : Nat) : Prop :=\n selected.length = weights.length ∧\n total_weight selected weights ≤ capacity\n\n-- Precondition definitions\n@[reducible, simp]\ndef knapsack_01_precond (weights values : List Nat) (capacity : Nat) : Prop :=\n -- !benchmark @start precond\n weights.length = values.length\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef knapsack_01 (weights values : List Nat) (capacity : Nat)\n (h_precond : knapsack_01_precond weights values capacity) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef knapsack_01_postcond (weights values : List Nat) (capacity : Nat) (result : Nat)\n (_ : knapsack_01_precond weights values capacity) : Prop :=\n -- !benchmark @start postcond\n (∀ selected, is_valid_strategy selected weights capacity → total_value selected values ≤ result) ∧\n (∃ selected, is_valid_strategy selected weights capacity ∧ total_value selected values = result)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem knapsack_01_postcond_satisfied (weights values : List Nat) (capacity : Nat)\n (h_precond : knapsack_01_precond weights values capacity) :\n knapsack_01_postcond weights values capacity (knapsack_01 weights values capacity h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "knapsack_unbounded", "nl_desc": "Your task is to implement the Unbounded Knapsack algorithm and verify its correctness in Lean. Given weights and values for n items and a capacity W, determine the maximum total value of items that can be selected (with replacement) without exceeding W. Postconditions: Upper Bound and Existence of a valid strategy (counts of items). Weights are strictly positive.\n", "spec": "import Mathlib\n\n/-! ### Precondition definitions -/\n\ndef solve_knapsack_unbounded_precond (weights values : List Nat) (capacity : Nat) : Prop :=\n (weights.length = values.length) ∧\n (weights.length > 0) ∧\n (capacity ≤ 1000) ∧\n (∀ i, i < weights.length → weights.getD i 0 > 0) ∧\n (∀ i, i < weights.length → weights.getD i 0 ≤ 1000) ∧\n (∀ i, i < values.length → values.getD i 0 ≤ 1000)\n\n/-! ### Auxiliary definitions for the post‑condition -/\n\ndef list_nat_to_int (l : List Nat) : List Int :=\n l.map (fun x => Int.ofNat x)\n\ndef total_weight (counts weights : List Int) : Int :=\n (counts.zip weights).foldl (fun acc p => acc + p.1 * p.2) 0\n\ndef total_value (counts values : List Int) : Int :=\n (counts.zip values).foldl (fun acc p => acc + p.1 * p.2) 0\n\ndef is_valid_strategy (counts weights : List Int) (capacity : Int) : Prop :=\n counts.length = weights.length ∧\n (∀ i, i < counts.length → counts.getD i 0 ≥ 0) ∧\n total_weight counts weights ≤ capacity\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef solve_knapsack_unbounded\n (weights values : List Nat) (capacity : Nat)\n (h_precond : solve_knapsack_unbounded_precond weights values capacity) : Nat :=\n -- !benchmark @start code\n -- A placeholder implementation; the correctness proof is discharged by `sorry`.\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\ndef solve_knapsack_unbounded_postcond\n (weights values : List Nat) (capacity : Nat) (result : Nat)\n (h_precond : solve_knapsack_unbounded_precond weights values capacity) : Prop :=\n -- !benchmark @start postcond\n (∀ counts : List Int,\n is_valid_strategy counts (list_nat_to_int weights) (Int.ofNat capacity) →\n total_value counts (list_nat_to_int values) ≤ Int.ofNat result) ∧\n (∃ counts : List Int,\n is_valid_strategy counts (list_nat_to_int weights) (Int.ofNat capacity) ∧\n total_value counts (list_nat_to_int values) = Int.ofNat result)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem solve_knapsack_unbounded_postcond_satisfied\n (weights values : List Nat) (capacity : Nat)\n (h_precond : solve_knapsack_unbounded_precond weights values capacity) :\n solve_knapsack_unbounded_postcond\n weights values capacity\n (solve_knapsack_unbounded weights values capacity h_precond)\n h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "kruskal", "nl_desc": "Your task is to implement Kruskal's Algorithm to find the Minimum Spanning Tree (MST) of a connected, undirected, weighted graph and verify its functional correctness in Lean using Mathlib. The graph is represented as an adjacency list with Int weights. Preconditions: You can assume the graph is connected (a path exists between any two nodes). You can assume the graph contains at most 100,000 nodes and edge weights are between -100,000 and 100,000 to prevent integer overflow. Crucially, the graph is a Simple Graph (no parallel edges between the same two nodes). Requirements: Implement the function kruskal_mst which returns a list of edges (source, target, weight). (1) The function must sort all edges by weight and iterate through them. (2) It should add an edge to the result only if it connects two previously disconnected components (i.e., does not form a cycle). (3) Global Optimality: You must prove that the sum of weights of your returned edges is less than or equal to the sum of weights of any other possible spanning tree of the graph. Verification Challenges: (1) Union-Find (Disjoint Set): To efficiently implement Kruskal's, you typically use a Union-Find data structure; you must verify that your find and union operations correctly track component connectivity. (2) Cycle Prevention: Proving that your check (typically find(u) != find(v)) correctly predicts whether adding edge $(u, v)$ would create a cycle. (3) Forest Invariant: Unlike Prim's (which grows one tree), Kruskal's grows a forest of trees; you must prove the invariant that this forest eventually merges into a single spanning tree and that every edge added is a \"safe edge\" for some MST.", "spec": "import Mathlib\n\nstructure WeightedGraph where\n adj : Array (Array (Nat × Int))\n\ndef WeightedGraph.size (g : WeightedGraph) : Nat :=\n g.adj.size\n\ndef WeightedGraph.has_edge (g : WeightedGraph) (u v : Nat) (w : Int) : Prop :=\n u < g.size ∧\n ∃ pair, pair ∈ g.adj.getD u #[] ∧ pair.1 = v ∧ pair.2 = w\n\ndef WeightedGraph.well_formed (g : WeightedGraph) : Prop :=\n ∀ u, u < g.size →\n -- Bounds check\n (∀ pair, pair ∈ g.adj.getD u #[] → pair.1 < g.size) ∧\n -- Simple graph check (matches Dafny)\n (∀ p1 p2, p1 ∈ g.adj.getD u #[] → p2 ∈ g.adj.getD u #[] → p1.1 = p2.1 → p1 = p2)\n\nstructure MSTEdge where\n u : Nat\n v : Nat\n w : Int\n\n-- 1. Validity\ndef MSTEdge.is_valid (e : MSTEdge) (g : WeightedGraph) : Prop :=\n g.has_edge e.u e.v e.w\n\n-- 2. Connectivity for Solution Edges (Undirected View)\n-- Dafny: `edge_in_set` checks (u, v) || (v, u)\ndef edges_contain (edges : List MSTEdge) (u v : Nat) : Prop :=\n ∃ e, e ∈ edges ∧ ((e.u = u ∧ e.v = v) ∨ (e.u = v ∧ e.v = u))\n\ndef path_follows_edges (edges : List MSTEdge) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n edges_contain edges (p.getD i 0) (p.getD (i + 1) 0)\n\ndef is_connected_with_edges (g : WeightedGraph) (edges : List MSTEdge) : Prop :=\n ∀ u v, u < g.size → v < g.size →\n ∃ p, p.head? = some u ∧ p.getLast? = some v ∧ path_follows_edges edges p\n\n-- 3. Spanning Tree Definition\ndef is_spanning_tree (g : WeightedGraph) (edges : List MSTEdge) : Prop :=\n edges.length = g.size - 1 ∧\n (∀ e, e ∈ edges → e.is_valid g) ∧\n is_connected_with_edges g edges\n -- Note: |V|-1 edges + Connected implies Acyclic (Tree)\n\ndef total_weight (edges : List MSTEdge) : Int :=\n edges.foldl (λ sum e => sum + e.w) 0\n\n-- 4. Optimality (MST)\ndef is_mst (g : WeightedGraph) (edges : List MSTEdge) : Prop :=\n is_spanning_tree g edges ∧\n ∀ other, is_spanning_tree g other → total_weight edges ≤ total_weight other\n\n-- 5. Graph Connectivity (Precondition)\n-- Dafny uses directed edges for the graph path check: `graph_has_any_edge`\ndef path_follows_graph (g : WeightedGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n ∃ w, g.has_edge (p.getD i 0) (p.getD (i + 1) 0) w\n\ndef WeightedGraph.is_connected (g : WeightedGraph) : Prop :=\n ∀ u v, u < g.size → v < g.size →\n ∃ p, p.head? = some u ∧ p.getLast? = some v ∧ path_follows_graph g p\n\ndef WeightedGraph.weights_bounded (g : WeightedGraph) : Prop :=\n g.size ≤ 100000 ∧\n ∀ u v w, g.has_edge u v w → w ≥ -100000 ∧ w ≤ 100000\n\n-- Precondition definitions\n@[reducible, simp]\ndef kruskal_mst_precond (graph : WeightedGraph) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧\n graph.size > 0 ∧\n graph.is_connected ∧\n graph.weights_bounded\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef kruskal_mst (graph : WeightedGraph)\n (h_precond : kruskal_mst_precond graph) : List MSTEdge :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef kruskal_mst_postcond (graph : WeightedGraph) (result : List MSTEdge)\n (_ : kruskal_mst_precond graph) : Prop :=\n -- !benchmark @start postcond\n is_mst graph result\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem kruskal_mst_postcond_satisfied (graph : WeightedGraph)\n (h_precond : kruskal_mst_precond graph) :\n kruskal_mst_postcond graph (kruskal_mst graph h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof"}
{"problem_id": "lca", "nl_desc": "Your task is to implement the Lowest Common Ancestor (LCA) algorithm and verify its correctness in Lean. Given a binary tree with distinct values and two target values p and q that exist in the tree, find the lowest common ancestor node. Postconditions: Result is an ancestor of both p and q, and is the deepest such ancestor. Distinct values are assumed.\n", "spec": "import Mathlib\n\ninductive Node where\n | nil\n | node (val : Nat) (left : Node) (right : Node)\nderiving Inhabited, Repr\n\nopen Node\n\ndef view (t : Node) : Set Nat :=\n match t with\n | nil => ∅\n | node v l r => {v} ∪ view l ∪ view r\n\ndef tree_contains (t : Node) (target : Nat) : Bool :=\n match t with\n | nil => false\n | node v l r => v == target || tree_contains l target || tree_contains r target\n\ndef tree_distinct (t : Node) : Prop :=\n match t with\n | nil => True\n | node v l r =>\n tree_distinct l ∧ tree_distinct r ∧\n v ∉ view l ∧ v ∉ view r ∧\n (view l ∩ view r = ∅)\n\n-- Precondition definitions\n@[reducible, simp]\ndef lowest_common_ancestor_precond (root : Node) (p q : Nat) : Prop :=\n -- !benchmark @start precond\n tree_contains root p ∧ tree_contains root q ∧ tree_distinct root\n -- !benchmark @end precond\n\ndef aux (root : Node) (p q : Nat) : Option Nat × Bool × Bool :=\n match root with\n | nil => (none, false, false)\n | node val l r =>\n let (lcaL, hasPL, hasQL) := aux l p q\n let (lcaR, hasPR, hasQR) := aux r p q\n let hasP := (val == p) || hasPL || hasPR\n let hasQ := (val == q) || hasQL || hasQR\n let lca :=\n if lcaL.isSome then lcaL\n else if lcaR.isSome then lcaR\n else if hasP && hasQ then some val else none\n (lca, hasP, hasQ)\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef lowest_common_ancestor (root : Node) (p q : Nat)\n (h_precond : lowest_common_ancestor_precond root p q) : Option Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\ndef is_ancestor (root : Node) (anc target : Nat) : Prop :=\n match root with\n | nil => False\n | node v l r =>\n if v = anc then tree_contains (node v l r) target = true\n else is_ancestor l anc target ∨ is_ancestor r anc target\n\n-- ADDED: Helper to determine depth for the \"Lowest\" check\ndef get_depth (root : Node) (target : Nat) : Option Nat :=\n match root with\n | nil => none\n | node v l r =>\n if v == target then some 0\n else\n match get_depth l target, get_depth r target with\n | some d, _ => some (d + 1)\n | none, some d => some (d + 1)\n | none, none => none\n\n-- Postcondition definitions\n@[reducible, simp]\ndef lowest_common_ancestor_postcond (root : Node) (p q : Nat) (result : Option Nat)\n (_ : lowest_common_ancestor_precond root p q) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some lca =>\n -- 1. Validity: It is a common ancestor\n is_ancestor root lca p ∧\n is_ancestor root lca q ∧\n -- 2. Optimality: It is the LOWEST (Deepest) common ancestor\n (∀ x, is_ancestor root x p → is_ancestor root x q →\n match get_depth root x, get_depth root lca with\n | some dx, some dres => dx ≤ dres\n | _, _ => True) -- Should imply dx and dres exist if is_ancestor is true\n | none => False\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem lowest_common_ancestor_postcond_satisfied (root : Node) (p q : Nat)\n (h_precond : lowest_common_ancestor_precond root p q) :\n lowest_common_ancestor_postcond root p q (lowest_common_ancestor root p q h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof"}
{"problem_id": "linear_search", "nl_desc": "Your task is to implement the Linear Search algorithm to find the lower bound index and verify its correctness in Lean. Given a sequence of integers (not necessarily sorted, though typically used with sorting in mind for lower_bound semantics, the spec here just asks for the partition point if it were sorted or just the first element >= target), find the first index k such that seq[k] >= target. Returns seq.size if no such element is found. Postconditions: (1) result <= seq.size. (2) All elements before 'result' are < target. (3) All elements from 'result' onwards (if sorted) or just the element at 'result' matches the condition? Wait, the Dafny spec says \"forall i < result ==> s[i] < target\" and \"forall i >= result ==> s[i] >= target\". This implies the array MUST be sorted for this spec to hold for ALL i >= result. However, the Dafny spec provided does NOT explicitly require `is_sorted` in the precondition for `linear_search` in the file view (wait, let me check the file content again... checked: explicitly requires `is_sorted(s)`). So it is Lower Bound on a sorted array using Linear Search. Implementation should be O(N).\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef linear_search_lower_bound_precond (seq : Array Int) (target : Int) : Prop :=\n -- !benchmark @start precond\n seq.size ≤ 0x7FFFFFFF ∧\n (∀ i j : Nat, i < j ∧ j < seq.size → seq.getD i 0 ≤ seq.getD j 0)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef linear_search_lower_bound (seq : Array Int) (target : Int) (h_precond : linear_search_lower_bound_precond seq target) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef linear_search_lower_bound_postcond (seq : Array Int) (target : Int) (result : Nat) (h_precond : linear_search_lower_bound_precond seq target) : Prop :=\n -- !benchmark @start postcond\n result ≤ seq.size ∧\n (∀ i : Nat, i < result → seq.getD i 0 < target) ∧\n (∀ i : Nat, result ≤ i ∧ i < seq.size → seq.getD i 0 ≥ target)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem linear_search_lower_bound_postcond_satisfied (seq : Array Int) (target : Int) (h_precond : linear_search_lower_bound_precond seq target) :\n linear_search_lower_bound_postcond seq target (linear_search_lower_bound seq target h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "linearsys_gf2", "nl_desc": "Your task is to implement a Gaussian Elimination solver for linear systems over GF(2) and verify its correctness in Lean. Given a binary matrix A (m x n) and a binary vector b (length m), find a binary vector x (length n) such that Ax = b (modulo 2), or return None if no solution exists. Postconditions: If result is Some(x), then Ax = b (mod 2). If result is None, then no such x exists. Preconditions: Matrix and vector dimensions match. Elements are 0 or 1. Dimensions are small (<= 100) to ensure tractable verification. Implementation should use standard Gaussian elimination with row operations modulo 2.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef solve_linear_system_gf2_precond (matrix : List (List Nat)) (b : List Nat) : Prop :=\n -- !benchmark @start precond\n matrix.length > 0 ∧\n (matrix.getD 0 []).length > 0 ∧\n matrix.length = b.length ∧\n matrix.length ≤ 100 ∧\n (matrix.getD 0 []).length ≤ 100 ∧\n (∀ row, row ∈ matrix → (∀ x, x ∈ row → x = 0 ∨ x = 1)) ∧\n (∀ x, x ∈ b → x = 0 ∨ x = 1)\n -- !benchmark @end precond\n\ndef is_solution (matrix : List (List Nat)) (b : List Nat) (x : List Nat) : Prop :=\n x.length = (matrix.getD 0 []).length ∧\n (∀ i, i < matrix.length →\n ( (List.range x.length).foldl (fun acc j => acc + (matrix.getD i []).getD j 0 * x.getD j 0) 0 ) % 2 = b.getD i 0)\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef solve_linear_system_gf2 (matrix : List (List Nat)) (b : List Nat) (h_precond : solve_linear_system_gf2_precond matrix b) : Option (List Nat) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef solve_linear_system_gf2_postcond (matrix : List (List Nat)) (b : List Nat) (result : Option (List Nat)) (h_precond : solve_linear_system_gf2_precond matrix b) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some x => is_solution matrix b x\n | none => ∀ x, x.length = (matrix.getD 0 []).length → (∀ v, v ∈ x → v = 0 ∨ v = 1) → ¬ is_solution matrix b x\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem solve_linear_system_gf2_postcond_satisfied (matrix : List (List Nat)) (b : List Nat) (h_precond : solve_linear_system_gf2_precond matrix b) :\n solve_linear_system_gf2_postcond matrix b (solve_linear_system_gf2 matrix b h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "llrbt_delete", "nl_desc": "Your task is to implement and verify the deletion algorithm for a Left-Leaning Red-Black Tree (LLRBT) in Lean. Verification Specification: (1) Preconditions: The input tree must be a valid LLRBT. (2) Postconditions: Functional Correctness (the resulting view is the original set minus the deleted value v) and Invariant Maintenance (the resulting tree satisfies is_llrbt, maintaining BST order, Black Height, and Color Properties). The deletion involves recursive descent with `move_red_left` or `move_red_right` operations and fixup on the way back up. Note: The root of the resulting subtree may be Red.\n", "spec": "import Mathlib\n\ninductive Color\n| Red\n| Black\nderiving Inhabited, BEq\n\ninductive Node\n| Empty : Node\n| Tree (color : Color) (val : Int) (left : Node) (right : Node) : Node\nderiving Inhabited\n\ndef view (t : Node) : Set Int :=\n match t with\n | Node.Empty => ∅\n | Node.Tree _ v l r => view l ∪ view r ∪ {v}\n\ndef is_red (t : Node) : Bool :=\n match t with\n | Node.Tree Color.Red _ _ _ => true\n | _ => false\n\ndef is_bst (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree _ v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Black Height: Returns -1 if invalid\ndef black_height (t : Node) : Int :=\n match t with\n | Node.Empty => 1\n | Node.Tree c _ l r =>\n let lh := black_height l\n let rh := black_height r\n if lh ≠ -1 ∧ rh ≠ -1 ∧ lh = rh then\n if c == Color.Black then lh + 1 else lh\n else\n -1\n\ndef satisfies_red_props (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree _ _ l r =>\n satisfies_red_props l ∧\n satisfies_red_props r ∧\n -- Rule: No Right-Leaning Red\n (¬ is_red r) ∧\n -- Rule: No Double Red on Left (left child red AND left.left child red)\n (match l with\n | Node.Tree Color.Red _ ll _ => ¬ is_red ll\n | _ => True)\n\n-- Main Invariant\ndef is_llrbt (t : Node) : Prop :=\n is_bst t ∧\n black_height t ≠ -1 ∧\n satisfies_red_props t\n\n-- Precondition definitions\n@[reducible, simp]\ndef delete_precond (t : Node) : Prop :=\n -- !benchmark @start precond\n is_llrbt t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef delete (t : Node) (v : Int)\n (h_precond : delete_precond t) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef delete_postcond (t : Node) (v : Int) (result : Node)\n (_ : delete_precond t) : Prop :=\n -- !benchmark @start postcond\n is_llrbt result ∧\n view result = view t \\ {v}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem delete_postcond_satisfied (t : Node) (v : Int)\n (h_precond : delete_precond t) :\n delete_postcond t v (delete t v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "llrbt_flipcolor", "nl_desc": "Your task is to implement and verify the flip_colors algorithm for a Left-Leaning Red-Black Tree (LLRBT) in Lean. Verification Specification: (1) Preconditions: The inputs (node, left child, right child) must form a valid local cluster where the node has a different color from its children, and both children have the same color. (2) Postconditions: Functional Correctness (view/set is preserved) and Invariant Maintenance (BST order is preserved using is_llrbt predicate, Black Height is preserved meaning the flip effectively pushes redness up or down without changing the black count on any path, and the colors of the node and its children are inverted: if node was Black and children Red, node becomes Red and children Black, and vice-versa).\n", "spec": "import Mathlib\n\ninductive Color\n| Red\n| Black\nderiving Inhabited, BEq\n\ninductive Node\n| Empty : Node\n| Tree (color : Color) (val : Int) (left : Node) (right : Node) : Node\nderiving Inhabited\n\ndef view (t : Node) : Set Int :=\n match t with\n | Node.Empty => ∅\n | Node.Tree _ v l r => view l ∪ view r ∪ {v}\n\ndef is_red (t : Node) : Bool :=\n match t with\n | Node.Tree Color.Red _ _ _ => true\n | _ => false\n\ndef get_color (t : Node) : Color :=\n match t with\n | Node.Tree c _ _ _ => c\n | Node.Empty => Color.Black -- Empty nodes are black\n\ndef is_bst (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree _ v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Black Height: Returns -1 if invalid\ndef black_height (t : Node) : Int :=\n match t with\n | Node.Empty => 1\n | Node.Tree c _ l r =>\n let lh := black_height l\n let rh := black_height r\n if lh ≠ -1 ∧ rh ≠ -1 ∧ lh = rh then\n if c == Color.Black then lh + 1 else lh\n else\n -1\n\ndef satisfies_red_props (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree _ _ l r =>\n satisfies_red_props l ∧\n satisfies_red_props r ∧\n -- Rule: No Right-Leaning Red\n (¬ is_red r) ∧\n -- Rule: No Double Red on Left\n (match l with\n | Node.Tree Color.Red _ ll _ => ¬ is_red ll\n | _ => True)\n\n-- Main Invariant\ndef is_llrbt (t : Node) : Prop :=\n is_bst t ∧\n black_height t ≠ -1 ∧\n satisfies_red_props t\n\ndef get_left (t : Node) : Option Node :=\n match t with | Node.Tree _ _ l _ => some l | _ => none\n\ndef get_right (t : Node) : Option Node :=\n match t with | Node.Tree _ _ _ r => some r | _ => none\n\ndef get_val (t : Node) : Option Int :=\n match t with | Node.Tree _ v _ _ => some v | _ => none\n\ndef is_not_empty (t : Node) : Prop :=\n match t with | Node.Empty => False | _ => True\n\n-- Precondition definitions\n@[reducible, simp]\ndef flip_colors_precond (t : Node) : Prop :=\n -- !benchmark @start precond\n -- t is not empty\n is_not_empty t ∧\n is_bst t ∧\n match t with\n | Node.Tree c _ l r =>\n is_not_empty l ∧ is_not_empty r ∧\n (get_color l = get_color r) ∧\n (c ≠ get_color l)\n | _ => False\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef flip_colors (t : Node) \n (h_precond : flip_colors_precond t) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef flip_colors_postcond (t : Node) (result : Node)\n (_ : flip_colors_precond t) : Prop :=\n -- !benchmark @start postcond\n view result = view t ∧\n is_bst result ∧ -- Requirement 1: BST structure preserved\n black_height result = black_height t ∧ -- Requirement 2: Balance preserved (Critical)\n match result, t with\n | Node.Tree c' _ l' r', Node.Tree c _ l r =>\n c' ≠ c ∧\n get_color l' ≠ get_color l ∧\n get_color r' ≠ get_color r\n | _, _ => False\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem flip_colors_postcond_satisfied (t : Node)\n (h_precond : flip_colors_precond t) :\n flip_colors_postcond t (flip_colors t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "llrbt_insert", "nl_desc": "Your task is to implement and verify the insertion algorithm for a Left-Leaning Red-Black Tree (LLRBT) in Lean. Verification Specification: (1) Preconditions: The input tree must be a valid LLRBT. (2) Postconditions: Functional Correctness (the resulting view is the union of the original set and the inserted value v) and Invariant Maintenance (the resulting tree satisfies is_llrbt, implying BST Order, Black Height preservation, and Color Properties: no Red node has a Red child and no Red node is a right child). Note: The root of the resulting subtree may be Red.\n", "spec": "import Mathlib\n\ninductive Color\n| Red\n| Black\nderiving Inhabited, BEq\n\ninductive Node\n| Empty : Node\n| Tree (color : Color) (val : Int) (left : Node) (right : Node) : Node\nderiving Inhabited\n\ndef view (t : Node) : Set Int :=\n match t with\n | Node.Empty => ∅\n | Node.Tree _ v l r => view l ∪ view r ∪ {v}\n\ndef is_red (t : Node) : Bool :=\n match t with\n | Node.Tree Color.Red _ _ _ => true\n | _ => false\n\ndef is_bst (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree _ v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Black Height: Returns -1 if invalid\ndef black_height (t : Node) : Int :=\n match t with\n | Node.Empty => 1\n | Node.Tree c _ l r =>\n let lh := black_height l\n let rh := black_height r\n if lh ≠ -1 ∧ rh ≠ -1 ∧ lh = rh then\n if c == Color.Black then lh + 1 else lh\n else\n -1\n\ndef satisfies_red_props (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree c _ l r =>\n satisfies_red_props l ∧\n satisfies_red_props r ∧\n -- Rule 1: No Right-Leaning Red (Matches Dafny)\n (if is_red r then False else True) ∧\n -- Rule 2: No Double Red on Left (Matches Dafny)\n -- Check: If Left is Red, then Left.Left must NOT be Red.\n -- This applies regardless of 'c' (current color).\n (match l with\n | Node.Tree Color.Red _ ll _ => \n (match ll with | Node.Tree Color.Red _ _ _ => False | _ => True)\n | _ => True)\n\n-- This definition is tricky because LLRBT insert allows the root to be red temporarily or permanently if it's the root of the whole tree (though usually root is black).\n-- However, the recursive step needs a weaker invariant.\n-- Dafny spec says: \"This allows the root to be Red or Black\"\ndef is_llrbt (t : Node) : Prop :=\n is_bst t ∧\n black_height t ≠ -1 ∧\n -- We need to check red properties. Dafny's satisfies_red_props handles recursive checks.\n -- But wait, Dafny's `satisfies_red_props` seems to strictly forbid right-leaning reds everywhere?\n -- Let's re-read Dafny carefully.\n -- satisfy_red_props:\n -- left_ok && right_ok && right_is_black && no_double_red\n -- Right is black: if right is red -> false.\n -- No double red on left: if left is red, left.left cannot be red? No, `l.is_red` then `l.left` check?\n -- Dafny: `match this.left { case Some(l) => if l.is_red then match l.left { case Some(ll) => !ll.is_red ...`\n -- This means: If I am any node, my right child cannot be red. And if my left child is red, IT'S left child cannot be red (no consecutive reds on left edge).\n -- What about Me being Red and Left being Red? Dafny spec doesn't explicitly ban `this.is_red && this.left.is_red` in `satisfies_red_props` itself?\n -- Ah, `no_double_red` checks `this.left` (l) and `l.left` (ll).\n -- So it prevents `Red -> Red` on the left edge.\n satisfies_red_props t\n\n\n\n-- Precondition definitions\n@[reducible, simp]\ndef insert_precond (t : Node) : Prop :=\n -- !benchmark @start precond\n is_llrbt t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef insert (t : Node) (v : Int)\n (h_precond : insert_precond t) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef insert_postcond (t : Node) (v : Int) (result : Node)\n (_ : insert_precond t) : Prop :=\n -- !benchmark @start postcond\n is_llrbt result ∧\n view result = view t ∪ {v}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem insert_postcond_satisfied (t : Node) (v : Int)\n (h_precond : insert_precond t) :\n insert_postcond t v (insert t v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "llrbt_rotateleft", "nl_desc": "Your task is to implement and verify the fixup_rotate_left algorithm for a Left-Leaning Red-Black Tree (LLRBT) in Lean. Verification Specification: (1) Preconditions: The node must have a right child that is Red (and the node itself typically Black or Red, but the rotation is triggered by a right-leaning Red). (2) Postconditions: Functional Correctness (view/set is preserved) and Invariant Maintenance (BST order is preserved because x < val < right.val order is maintained, Black Height is preserved as the path length logic is symbolic and unchanged, and colors are rotated: the new root takes the old root's color, and the old root becomes Red). Note: This is a standard left rotation to fix a right-leaning Red link.\n", "spec": "import Mathlib\n\ninductive Color\n| Red\n| Black\nderiving Inhabited, BEq\n\ninductive Node\n| Empty : Node\n| Tree (color : Color) (val : Int) (left : Node) (right : Node) : Node\nderiving Inhabited\n\ndef view (t : Node) : Set Int :=\n match t with\n | Node.Empty => ∅\n | Node.Tree _ v l r => view l ∪ view r ∪ {v}\n\ndef is_red (t : Node) : Bool :=\n match t with\n | Node.Tree Color.Red _ _ _ => true\n | _ => false\n\ndef get_color (t : Node) : Color :=\n match t with\n | Node.Tree c _ _ _ => c\n | Node.Empty => Color.Black -- Empty nodes are black\n\ndef is_bst (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree _ v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Black Height: Returns -1 if invalid\ndef black_height (t : Node) : Int :=\n match t with\n | Node.Empty => 1\n | Node.Tree c _ l r =>\n let lh := black_height l\n let rh := black_height r\n if lh ≠ -1 ∧ rh ≠ -1 ∧ lh = rh then\n if c == Color.Black then lh + 1 else lh\n else\n -1\n\ndef get_left (t : Node) : Option Node :=\n match t with | Node.Tree _ _ l _ => some l | _ => none\n\ndef get_right (t : Node) : Option Node :=\n match t with | Node.Tree _ _ _ r => some r | _ => none\n\ndef is_not_empty (t : Node) : Prop :=\n match t with | Node.Empty => False | _ => True\n\n-- Precondition definitions\n@[reducible, simp]\ndef rotate_left_precond (t : Node) : Prop :=\n -- !benchmark @start precond\n match t with\n | Node.Tree _ _ _ r =>\n -- Right child exists and is Red.\n -- (Dafny spec says Right is Red. Node itself can be any color).\n is_red r\n | _ => False\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef rotate_left (t : Node) \n (h_precond : rotate_left_precond t) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef rotate_left_postcond (t : Node) (result : Node)\n (_ : rotate_left_precond t) : Prop :=\n -- !benchmark @start postcond\n view result = view t ∧\n is_bst t → is_bst result ∧\n match result, t with\n | Node.Tree c' _ l' _, Node.Tree c _ _ r =>\n -- New root color matches old root color\n c' = c ∧\n -- New left child matches old root and is RED\n (match l' with\n | Node.Tree Color.Red _ _ _ => True\n | _ => False) ∧\n -- Black height preservation\n (black_height t ≠ -1 → black_height result = black_height t)\n | _, _ => False\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem rotate_left_postcond_satisfied (t : Node)\n (h_precond : rotate_left_precond t) :\n rotate_left_postcond t (rotate_left t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "llrbt_rotateright", "nl_desc": "Your task is to implement and verify a right rotation (fixup_rotate_right) for a Left-Leaning Red-Black Tree (LLRBT) in Lean. Verification Specification: (1) Preconditions: The node must have a left child that is Red, and that left child must also have a left child that is Red (Double Red on Left). (2) Postconditions: Functional Correctness (view/set is preserved) and Invariant Maintenance (BST order is preserved, Black Height is preserved, and colors are rotated: the new root takes the old root's color, the old root becomes Red and takes the right position). This fixes the \"Double Red\" violation by lifting the middle node.\n", "spec": "import Mathlib\n\ninductive Color\n| Red\n| Black\nderiving Inhabited, BEq\n\ninductive Node\n| Empty : Node\n| Tree (color : Color) (val : Int) (left : Node) (right : Node) : Node\nderiving Inhabited\n\ndef view (t : Node) : Set Int :=\n match t with\n | Node.Empty => ∅\n | Node.Tree _ v l r => view l ∪ view r ∪ {v}\n\ndef is_red (t : Node) : Bool :=\n match t with\n | Node.Tree Color.Red _ _ _ => true\n | _ => false\n\ndef get_color (t : Node) : Color :=\n match t with\n | Node.Tree c _ _ _ => c\n | Node.Empty => Color.Black -- Empty nodes are black\n\ndef is_bst (t : Node) : Prop :=\n match t with\n | Node.Empty => True\n | Node.Tree _ v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\n\n-- Black Height: Returns -1 if invalid\ndef black_height (t : Node) : Int :=\n match t with\n | Node.Empty => 1\n | Node.Tree c _ l r =>\n let lh := black_height l\n let rh := black_height r\n if lh ≠ -1 ∧ rh ≠ -1 ∧ lh = rh then\n if c == Color.Black then lh + 1 else lh\n else\n -1\n\ndef get_left (t : Node) : Option Node :=\n match t with | Node.Tree _ _ l _ => some l | _ => none\n\ndef get_right (t : Node) : Option Node :=\n match t with | Node.Tree _ _ _ r => some r | _ => none\n\ndef is_not_empty (t : Node) : Prop :=\n match t with | Node.Empty => False | _ => True\n\n-- Precondition definitions\n@[reducible, simp]\ndef rotate_right_precond (t : Node) : Prop :=\n -- !benchmark @start precond\n match t with\n | Node.Tree _ _ l _ =>\n -- Strictly match Dafny/Verus: Only require Left child to be Red.\n -- The Double-Red context is checked by the caller (insert), not this helper.\n is_red l\n | _ => False\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef rotate_right (t : Node) \n (h_precond : rotate_right_precond t) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef rotate_right_postcond (t : Node) (result : Node)\n (_ : rotate_right_precond t) : Prop :=\n -- !benchmark @start postcond\n view result = view t ∧\n is_bst t → is_bst result ∧\n match result, t with\n | Node.Tree c' _ _ r', Node.Tree c _ _ _ =>\n -- New root color matches old root color\n c' = c ∧\n -- New right child is Red\n (match r' with\n | Node.Tree Color.Red _ _ _ => True\n | _ => False) ∧\n -- Black height preservation\n (black_height t ≠ -1 → black_height result = black_height t)\n | _, _ => False\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem rotate_right_postcond_satisfied (t : Node)\n (h_precond : rotate_right_precond t) :\n rotate_right_postcond t (rotate_right t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "longest_common_subsequence", "nl_desc": "Your task is to implement the Longest Common Subsequence (LCS) algorithm and verify its correctness in Lean. Given two strings (or sequences of characters), find the length of the longest subsequence present in both. Postconditions: Result equals the value defined by the standard recursive LCS specification.\n", "spec": "import Mathlib\n\ndef lcs_spec (s t : List Char) : Nat :=\n match s, t with\n | [], _ => 0\n | _, [] => 0\n | x :: xs, y :: ys =>\n if x = y then 1 + lcs_spec xs ys\n else max (lcs_spec xs (y :: ys)) (lcs_spec (x :: xs) ys)\n\n-- Precondition definitions\n@[reducible, simp]\ndef solve_longest_common_subsequence_precond (s t : List Char) : Prop :=\n -- !benchmark @start precond\n s.length ≤ 3000 ∧ t.length ≤ 3000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef solve_longest_common_subsequence (s t : List Char)\n (h_precond : solve_longest_common_subsequence_precond s t) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef solve_longest_common_subsequence_postcond (s t : List Char) (result : Nat)\n (_ : solve_longest_common_subsequence_precond s t) : Prop :=\n -- !benchmark @start postcond\n result = lcs_spec s t\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem solve_longest_common_subsequence_postcond_satisfied (s t : List Char)\n (h_precond : solve_longest_common_subsequence_precond s t) :\n solve_longest_common_subsequence_postcond s t (solve_longest_common_subsequence s t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "longest_increasing_subsequence", "nl_desc": "Your task is to implement the Longest Increasing Subsequence (LIS) algorithm and verify its correctness in Lean. Given a sequence of integers, find the length of the longest subsequence where the elements are strictly increasing. Postconditions: Upper Bound (result ≥ length of any valid strictly increasing subsequence) and Existence (result = length of some valid strictly increasing subsequence). A valid subsequence is defined by a strictly increasing sequence of indices pointing to strictly increasing values.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef longest_increasing_subsequence_precond (seq : List Int) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef longest_increasing_subsequence (seq : List Int)\n (h_precond : longest_increasing_subsequence_precond seq) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n/-\n Post‑condition auxiliary predicates.\n-/\n\n/-- `inc_nat l` states that the list `l` of natural numbers is strictly increasing. -/\ndef inc_nat (l : List Nat) : Prop :=\n ∀ i j, i < j → i < l.length → j < l.length → l.getD i 0 < l.getD j 0\n\n/-- `inc_vals seq indices` states that the values of `seq` at the given (strictly increasing)\n indices form a strictly increasing sequence. -/\ndef inc_vals (seq : List Int) (indices : List Nat) : Prop :=\n ∀ i j, i < j → i < indices.length → j < indices.length →\n seq.getD (indices.getD i 0) 0 < seq.getD (indices.getD j 0) 0\n\n/-- `is_valid_is seq indices` bundles the three necessary conditions for a list of indices\n to represent a valid increasing subsequence of `seq`. -/\ndef is_valid_is (seq : List Int) (indices : List Nat) : Prop :=\n inc_nat indices ∧\n (∀ i, i ∈ indices → i < seq.length) ∧\n inc_vals seq indices\n\n-- Postcondition definitions\n@[reducible, simp]\ndef longest_increasing_subsequence_postcond\n (seq : List Int) (result : Nat)\n (h_precond : longest_increasing_subsequence_precond seq) : Prop :=\n -- !benchmark @start postcond\n (∀ sub : List Nat, is_valid_is seq sub → sub.length ≤ result) ∧\n (∃ sub : List Nat, is_valid_is seq sub ∧ sub.length = result)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem longest_increasing_subsequence_postcond_satisfied\n (seq : List Int) (h_precond : longest_increasing_subsequence_precond seq) :\n longest_increasing_subsequence_postcond\n seq (longest_increasing_subsequence seq h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "longest_palindrome_substring", "nl_desc": "Your task is to implement Manacher's algorithm for the Longest Palindromic Substring and verify its correctness in Lean using Mathlib. The algorithm identifies the substring of maximal length in input $s$ that reads the same forwards and backwards. The operation proceeds as follows: (1) Transformation: Construct a modified string $T$ by inserting a unique separator (e.g., '#') between every character of $s$ (and at the outer boundaries) to uniformly handle both odd and even length palindromes. (2) Initialization: Maintain an array $P$ where $P[i]$ represents the radius of the palindrome centered at $T[i]$. Initialize a center variable $C$ and a right boundary variable $R$ to $0$. (3) Iteration: Iterate through each index $i$ of $T$. If $i < R$, initialize $P[i]$ using the properties of symmetry around $C$ via the mirror index $i' = 2C - i$, specifically setting $P[i] = \\min(R - i, P[i'])$. Otherwise, initialize $P[i]$ to $0$. (4) Expansion: Attempt to expand the palindrome centered at $i$ by checking if characters at $T[i - 1 - P[i]]$ and $T[i + 1 + P[i]]$ are equal. Increment $P[i]$ repeatedly while they match. (5) Boundary Update: If the newly expanded palindrome at $i$ extends beyond $R$ (i.e., $i + P[i] > R$), update the center $C$ to $i$ and the boundary $R$ to $i + P[i]$. (6) Result Extraction: Find the maximum value in $P$ and map its center and radius back to the corresponding start index and length in original string $s$. Use Array UInt8 for input. Specifically, you must prove the following: (1) Functional Correctness: The returned tuple $(start, len)$ defines a valid subrange of $s$, the substring defined by this range satisfies the formal is_palindrome definition (symmetric characters), and for any other valid palindromic substring in $s$, its length is $\\le len$. (2) Loop Invariants: You must demonstrate that for every processed center $k < i$, $P[k]$ stores the correct maximal palindrome radius for that center in $T$, and that the current variables $C$ and $R$ consistently mark the rightmost ending palindrome found so far.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef longest_palindromic_substring_precond (s : Array UInt8) : Prop :=\n -- !benchmark @start precond\n s.size ≤ 1000000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef longest_palindromic_substring (s : Array UInt8)\n (_ : longest_palindromic_substring_precond s) :\n Nat × Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Helper definitions\ndef is_valid_subrange (s : Array UInt8) (start len : Nat) : Prop :=\n start + len ≤ s.size\n\n-- A sequence slice s[start, start+len) is a palindrome if for all i < len,\n-- s[start + i] == s[start + len - 1 - i]\ndef is_palindrome_slice (s : Array UInt8) (start len : Nat) : Prop :=\n ∀ i, i < len → s.getD (start + i) 0 = s.getD (start + len - 1 - i) 0\n\n-- Postcondition definitions\n@[reducible, simp]\ndef longest_palindromic_substring_postcond\n (s : Array UInt8) (result : Nat × Nat)\n (_ : longest_palindromic_substring_precond s) : Prop :=\n -- !benchmark @start postcond\n let (start, len) := result\n is_valid_subrange s start len ∧\n is_palindrome_slice s start len ∧\n (∀ i l, is_valid_subrange s i l → is_palindrome_slice s i l → l ≤ len)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem longest_palindromic_substring_postcond_satisfied\n (s : Array UInt8) (h_precond : longest_palindromic_substring_precond s) :\n longest_palindromic_substring_postcond\n s (longest_palindromic_substring s h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "matrix_multiplication", "nl_desc": "Your task is to implement Matrix Multiplication and verify its correctness in Lean. Given two valid matrices A (m x n) and B (n x k), compute their product C (m x k). Postconditions: The element at C[i][j] is the dot product of row i of A and column j of B. Preconditions: Matrices must be well-formed (non-empty, rectangular) and dimensions must match (cols(A) == rows(B)). Elements are natural numbers.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef matrix_multiply_precond (A B : List (List Nat)) : Prop :=\n -- !benchmark @start precond\n A.length > 0 ∧\n A.length ≤ 10 ∧ -- Aligned with Dafny/Verus (10)\n (∀ row, row ∈ A → row.length > 0) ∧\n (∀ row, row ∈ A → row.length ≤ 10) ∧\n (∀ i j, i < A.length → j < A.length → (A.getD i []).length = (A.getD j []).length) ∧\n \n -- Value Bounds (ADDED to match references)\n (∀ row, row ∈ A → ∀ val, val ∈ row → val ≤ 100) ∧\n (∀ row, row ∈ B → ∀ val, val ∈ row → val ≤ 100) ∧\n\n B.length > 0 ∧\n B.length ≤ 10 ∧\n (∀ row, row ∈ B → row.length > 0) ∧\n (∀ row, row ∈ B → row.length ≤ 10) ∧\n (∀ i j, i < B.length → j < B.length → (B.getD i []).length = (B.getD j []).length) ∧\n (A.getD 0 []).length = B.length\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef matrix_multiply (A B : List (List Nat)) (h_precond : matrix_multiply_precond A B) : List (List Nat) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\ndef inner_product (row : List Nat) (col_idx : Nat) (B : List (List Nat)) : Nat :=\n (List.range row.length).foldl (fun acc k => acc + (row.getD k 0) * ((B.getD k []).getD col_idx 0)) 0\n\n-- Postcondition definitions\n@[reducible, simp]\ndef matrix_multiply_postcond (A B : List (List Nat)) (result : List (List Nat)) (h_precond : matrix_multiply_precond A B) : Prop :=\n -- !benchmark @start postcond\n result.length = A.length ∧\n (∀ row, row ∈ result → row.length = (B.getD 0 []).length) ∧\n (∀ i j, i < result.length → j < (result.getD 0 []).length →\n (result.getD i []).getD j 0 = inner_product (A.getD i []) j B)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem matrix_multiply_postcond_satisfied (A B : List (List Nat))\n (h_precond : matrix_multiply_precond A B) :\n matrix_multiply_postcond A B (matrix_multiply A B h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof"}
{"problem_id": "max_matching", "nl_desc": "Your task is to implement an algorithm to find the Maximum Bipartite Matching and verify its functional correctness in Lean using Mathlib. Preconditions The input is a Bipartite Graph consisting of two disjoint sets of vertices: Left Set (U) with left_size nodes. Right Set (V) with right_size nodes. The graph is represented as an adjacency list adj (Array of Array Nat) where adj[u] contains a list of neighbors in the Right Set. Requirements: Implement the function max_bipartite_matching which returns the size (number of edges) of the maximum matching. Verification Challenges: (1) Valid Pairing: Proving that the match array maintained by your algorithm correctly maps nodes in the Right Set to their matched partners in the Left Set, and that every recorded match corresponds to a real edge exists in adj. (2) Disjointness: Proving that the algorithm never assigns two different Left-nodes to the same Right-node (and vice-versa). (3) Optimality: Proving that when the algorithm terminates, the matching size is indeed maximal. This usually requires proving Berge's Lemma: a matching is maximum if and only if there is no augmenting path.\n", "spec": "import Mathlib\n\nstructure MaxMatchingGraph where\n left_size : Nat\n right_size : Nat\n adj : Array (Array Nat)\n\ndef MaxMatchingGraph.well_formed (g : MaxMatchingGraph) : Prop :=\n g.adj.size = g.left_size ∧\n ∀ u, u < g.left_size →\n ∀ v, v ∈ g.adj.getD u #[] → v < g.right_size\n\ndef MaxMatchingGraph.has_edge (g : MaxMatchingGraph) (u v : Nat) : Prop :=\n u < g.left_size ∧\n v ∈ g.adj.getD u #[]\n\n-- Matching definition\nstructure Matching where\n edges : List (Nat × Nat)\n\ndef Matching.is_valid (m : Matching) (g : MaxMatchingGraph) : Prop :=\n ∀ e, e ∈ m.edges → g.has_edge e.1 e.2\n\ndef Matching.is_disjoint (m : Matching) : Prop :=\n ∀ e1 e2, e1 ∈ m.edges → e2 ∈ m.edges → e1 ≠ e2 →\n e1.1 ≠ e2.1 ∧ e1.2 ≠ e2.2\n\ndef Matching.size (m : Matching) : Nat :=\n m.edges.length\n\ndef is_matching (g : MaxMatchingGraph) (m : Matching) : Prop :=\n m.is_valid g ∧ \n m.is_disjoint ∧\n m.edges.Nodup\n\ndef is_max_matching_size (g : MaxMatchingGraph) (k : Nat) : Prop :=\n (∃ m, is_matching g m ∧ m.size = k) ∧\n (∀ m, is_matching g m → m.size ≤ k)\n\n-- Precondition definitions\n@[reducible, simp]\ndef max_bipartite_matching_precond (graph : MaxMatchingGraph) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧ graph.left_size ≤ 1000 ∧ graph.right_size ≤ 1000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef max_bipartite_matching (graph : MaxMatchingGraph)\n (h_precond : max_bipartite_matching_precond graph) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef max_bipartite_matching_postcond (graph : MaxMatchingGraph) (result : Nat)\n (_ : max_bipartite_matching_precond graph) : Prop :=\n -- !benchmark @start postcond\n is_max_matching_size graph result\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem max_bipartite_matching_postcond_satisfied (graph : MaxMatchingGraph)\n (h_precond : max_bipartite_matching_precond graph) :\n max_bipartite_matching_postcond graph\n (max_bipartite_matching graph h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "maxheap_popmax", "nl_desc": "Your task is to implement the Pop Max operation for a binary max-heap and verify its correctness in Lean. The operation removes the root (max element), moves the last element to the root, and bubbles it down. Postconditions: Returns max element, heap size decreases by 1, multiset view is view - {max}, and heap invariant is preserved.\n", "spec": "import Mathlib\n\nstructure BinaryMaxHeap where\n storage : List Int\nderiving Inhabited\n\nnamespace BinaryMaxHeap\n\ndef len (h : BinaryMaxHeap) : Nat :=\n h.storage.length\n\ndef get (h : BinaryMaxHeap) (i : Nat) : Int :=\n h.storage.getD i 0\n\ndef parent (i : Nat) : Nat := (i - 1) / 2\n\ndef is_heap (h : BinaryMaxHeap) : Prop :=\n ∀ i, 0 < i ∧ i < h.len → h.get i ≤ h.get (parent i)\n\ndef view (h : BinaryMaxHeap) : Multiset Int :=\n Multiset.ofList h.storage\n\n-- Precondition definitions\n@[reducible, simp]\ndef pop_max_precond (h : BinaryMaxHeap) : Prop :=\n -- !benchmark @start precond\n is_heap h ∧ h.len > 0\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\n-- Returns (new_heap, popped_max)\ndef pop_max (h : BinaryMaxHeap)\n (h_precond : pop_max_precond h) : BinaryMaxHeap × Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef pop_max_postcond (h : BinaryMaxHeap) (result : BinaryMaxHeap × Int)\n (_ : pop_max_precond h) : Prop :=\n -- !benchmark @start postcond\n let (new_h, max_val) := result\n is_heap new_h ∧\n new_h.len = h.len - 1 ∧\n (∀ x ∈ h.view, max_val ≥ x) ∧\n new_h.view = h.view - {max_val}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem pop_max_postcond_satisfied (h : BinaryMaxHeap)\n (h_precond : pop_max_precond h) :\n pop_max_postcond h (pop_max h h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n\nend BinaryMaxHeap\n"}
{"problem_id": "maxheap_push", "nl_desc": "Your task is to implement the Push operation for a binary max-heap and verify its correctness in Lean. The operation inserts a new value at the end and bubbles it up to restore the heap property. Postconditions: Multiset view includes the new element (view = old_view + {v}), heap size increases by 1, and the heap invariant is preserved (parent ≥ child).\n", "spec": "import Mathlib\n\nstructure BinaryMaxHeap where\n storage : List Int\nderiving Inhabited\n\nnamespace BinaryMaxHeap\n\ndef len (h : BinaryMaxHeap) : Nat :=\n h.storage.length\n\ndef get (h : BinaryMaxHeap) (i : Nat) : Int :=\n h.storage.getD i 0 -- Safe access with default\n\ndef parent (i : Nat) : Nat := (i - 1) / 2\n\ndef is_heap (h : BinaryMaxHeap) : Prop :=\n ∀ i, 0 < i ∧ i < h.len → h.get i ≤ h.get (parent i)\n\ndef view (h : BinaryMaxHeap) : Multiset Int :=\n Multiset.ofList h.storage\n\n-- Precondition definitions\n@[reducible, simp]\ndef push_precond (h : BinaryMaxHeap) (v : Int) : Prop :=\n -- !benchmark @start precond\n is_heap h ∧ h.len < 1023 -- Limited capacity as per spec example\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef push (h : BinaryMaxHeap) (v : Int)\n (h_precond : push_precond h v) : BinaryMaxHeap :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef push_postcond (h : BinaryMaxHeap) (v : Int) (result : BinaryMaxHeap)\n (_ : push_precond h v) : Prop :=\n -- !benchmark @start postcond\n is_heap result ∧\n result.len = h.len + 1 ∧\n result.view = h.view + {v}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem push_postcond_satisfied (h : BinaryMaxHeap) (v : Int)\n (h_precond : push_precond h v) :\n push_postcond h v (push h v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n\nend BinaryMaxHeap\n"}
{"problem_id": "maximum_subarray_sum", "nl_desc": "Your task is to implement the Maximum Subarray Sum algorithm (Kadane's Algorithm) and verify its correctness in Lean. Given a sequence of integers, find the maximum sum of any contiguous subarray. Postconditions: Result is greater than or equal to the sum of any contiguous subarray, and there exists a contiguous subarray with that sum. Basic case handles empty sequence as sum 0 or handling non-empty requirement. Spec assumes non-empty list.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef maxSubarraySum_precond (seq : List Int) : Prop :=\n -- !benchmark @start precond\n 0 < seq.length ∧ seq.length ≤ 100000\n -- !benchmark @end precond\n\ndef spec_sum (seq : List Int) (i j : Nat) : Int :=\n ((seq.drop i).take (j - i)).foldl (fun acc x => acc + x) 0\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef maxSubarraySum (seq : List Int) (h_precond : maxSubarraySum_precond (seq)) : Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef maxSubarraySum_postcond (seq : List Int) (result: Int) (h_precond : maxSubarraySum_precond (seq)) : Prop :=\n -- !benchmark @start postcond\n (∀ i j : Nat, i ≤ j → j ≤ seq.length → spec_sum seq i j ≤ result) ∧\n (∃ i j : Nat, i ≤ j ∧ j ≤ seq.length ∧ spec_sum seq i j = result)\n -- !benchmark @end postcond\n\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem maxSubarraySum_postcond_satisfied (seq: List Int) (h_precond : maxSubarraySum_precond (seq)) :\n maxSubarraySum_postcond (seq) (maxSubarraySum (seq) h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "merge_sort", "nl_desc": "Your task is to implement the Merge Sort algorithm and verify its correctness in Lean. Given a list of integers, return a new list containing the same elements sorted in non-decreasing order. Postconditions: (1) Output is sorted. (2) Output is a permutation of input. Preconditions: None. Implementation: Recursive divide-and-conquer. Split list into two halves, recursively sort them, then merge the sorted halves. Note: You should use 'partial' or a termination proof for recursion.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef mergeSort_precond (v : List Int) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef mergeSort (v : List Int) (h_precond : mergeSort_precond v) : List Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef mergeSort_postcond (v : List Int) (result : List Int) (h_precond : mergeSort_precond v) : Prop :=\n -- !benchmark @start postcond\n List.Sorted (· ≤ ·) result ∧ result.Perm v\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem mergeSort_postcond_satisfied (v : List Int) (h_precond : mergeSort_precond v) :\n mergeSort_postcond v (mergeSort v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "polymul_karatsuba", "nl_desc": "Your task is to implement the Karatsuba Multiplication Algorithm for polynomials and verify its functional correctness in Lean. Given two polynomials A and B represented as coefficient lists, compute their product. Preconditions: 1. Lengths: A and B have equal lengths, which are powers of two (e.g., 2, 4, 8...). 2. Constraints: The sum of lengths is at most 1000. Coefficients are integers between -1,000,000 and 1,000,000. Postconditions: Implement `karatsuba_mul` using the divide-and-conquer approach. (1) Algorithm: - Base Case: Small N (e.g., N=1), use simple multiplication. - Recursive Step: Split A and B into halves (A0, A1, B0, B1). Compute z0 = A0*B0, z2 = A1*B1, and z1 = (A0+A1)*(B0+B1). - Combine: result = z0 + (z1 - z0 - z2)x^m + z2 x^2m. (2) Output: Returns a list of size |A| + |B| - 1. (3) Functional Correctness: Verify that the result matches the standard mathematical convolution of A and B. Verification Challenges: (1) Algebraic Equivalence: You must prove that the Karatsuba formula mathematically equals the standard polynomial multiplication formula. This typically requires a helper lemma showing (A0+A1)(B0+B1) - A0B0 - A1B1 = A0B1 + A1B0. (2) Termination: Prove that the recursion on list length terminates.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef karatsuba_mul_precond (a b : List Int) : Prop :=\n -- !benchmark @start precond\n a.length > 0 ∧\n b.length > 0 ∧\n a.length + b.length ≤ 1000 ∧\n (∃ k : Nat, 2 ^ k = a.length) ∧\n (∃ k : Nat, 2 ^ k = b.length) ∧\n a.length = b.length ∧\n (∀ c, c ∈ a → -1000000 ≤ c ∧ c ≤ 1000000) ∧\n (∀ c, c ∈ b → -1000000 ≤ c ∧ c ≤ 1000000)\n -- !benchmark @end precond\n\ndef spec_poly_mul_coeff (a b : List Int) (k : Nat) : Int :=\n -- Omitted definition for brevity, same as naive\n 0 -- placeholder\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef karatsuba_mul (a b : List Int) (h_precond : karatsuba_mul_precond a b) : List Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef karatsuba_mul_postcond (a b : List Int) (result : List Int) (h_precond : karatsuba_mul_precond a b) : Prop :=\n -- !benchmark @start postcond\n result.length = a.length + b.length - 1 ∧\n ∀ k : Nat, k < result.length →\n result.getD k 0 = (List.range (k + 1)).foldl (fun acc i =>\n if i < a.length ∧ k - i < b.length then\n acc + a.getD i 0 * b.getD (k - i) 0\n else acc) 0\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem karatsuba_mul_postcond_satisfied (a b : List Int) (h_precond : karatsuba_mul_precond a b) :\n karatsuba_mul_postcond a b (karatsuba_mul a b h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "polymul_naive", "nl_desc": "Your task is to implement Naive Polynomial Multiplication and verify its correctness in Lean. Given two polynomials A and B represented as coefficient lists, compute their product C using the O(N^2) algorithm. Preconditions: 1. Input polynomials are non-empty. 2. Constraints: To simplify verification, the sum of lengths is at most 1000, and every coefficient is between -1,000,000 and 1,000,000. Postconditions: Implement `poly_multiply` to return the product polynomial. (1) Output: Returns a list of size |A| + |B| - 1. (2) Functional Correctness: The coefficients of C must match the mathematical convolution of A and B. Verification Challenges: (1) Accumulation Logic: You must define the convolution sum (sum_{p+q=k} A[p]*B[q]) and prove your loop computes it.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef poly_multiply_precond (a b : List Int) : Prop :=\n -- !benchmark @start precond\n a.length > 0 ∧\n b.length > 0 ∧\n a.length + b.length ≤ 1000 ∧\n (∀ c, c ∈ a → -1000000 ≤ c ∧ c ≤ 1000000) ∧\n (∀ c, c ∈ b → -1000000 ≤ c ∧ c ≤ 1000000)\n -- !benchmark @end precond\n\ndef spec_poly_mul_coeff (a b : List Int) (k : Nat) : Int :=\n -- Omitted definition for brevity in this context, but would simulate convolution\n 0 -- placeholder\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef poly_multiply (a b : List Int) (h_precond : poly_multiply_precond a b) : List Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef poly_multiply_postcond (a b : List Int) (result : List Int) (h_precond : poly_multiply_precond a b) : Prop :=\n -- !benchmark @start postcond\n result.length = a.length + b.length - 1 ∧\n ∀ k : Nat, k < result.length →\n result.getD k 0 = (List.range (k + 1)).foldl (fun acc i =>\n if i < a.length ∧ k - i < b.length then\n acc + a.getD i 0 * b.getD (k - i) 0\n else acc) 0\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem poly_multiply_postcond_satisfied (a b : List Int) (h_precond : poly_multiply_precond a b) :\n poly_multiply_postcond a b (poly_multiply a b h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "prim", "nl_desc": "Your task is to implement Prim's Algorithm to find the Minimum Spanning Tree (MST) of a connected, undirected, weighted graph and verify its functional correctness in Lean using Mathlib. The graph is represented as an adjacency list with Int weights. Preconditions: You can assume the graph is connected (a path exists between any two nodes). You can assume the graph contains at most 100,000 nodes and edge weights are between -100,000 and 100,000 to prevent integer overflow. Crucially, the graph is a Simple Graph (no parallel edges between the same two nodes). Requirements: Implement the function prim_mst which returns a list of edges (source, target, weight). (1) The returned sequence must contain exactly N-1 edges (where N is the number of nodes). (2) The edges must form a valid Spanning Tree (connecting all nodes with no cycles). (3) Global Optimality: You must prove that the sum of weights of your returned edges is less than or equal to the sum of weights of any other possible spanning tree of the graph. Verification Challenges: (1) The Cut Property: The core of Prim’s correctness. (2) Tree Growth: Proving the invariant that at every step of the algorithm, the set of selected edges forms a single connected tree that is a subset of a valid MST. (3) Termination & Connectivity.\n", "spec": "import Mathlib\n\nstructure WeightedGraph where\n adj : Array (Array (Nat × Int))\n\ndef WeightedGraph.size (g : WeightedGraph) : Nat :=\n g.adj.size\n\ndef WeightedGraph.has_edge (g : WeightedGraph) (u v : Nat) (w : Int) : Prop :=\n u < g.size ∧\n ∃ pair, pair ∈ g.adj.getD u #[] ∧ pair.1 = v ∧ pair.2 = w\n\ndef WeightedGraph.well_formed (g : WeightedGraph) : Prop :=\n ∀ u, u < g.size →\n -- Bounds check\n (∀ pair, pair ∈ g.adj.getD u #[] → pair.1 < g.size) ∧\n -- Simple graph check (matches Dafny)\n (∀ p1 p2, p1 ∈ g.adj.getD u #[] → p2 ∈ g.adj.getD u #[] → p1.1 = p2.1 → p1 = p2)\n\nstructure MSTEdge where\n u : Nat\n v : Nat\n w : Int\n\n-- 1. Validity\ndef MSTEdge.is_valid (e : MSTEdge) (g : WeightedGraph) : Prop :=\n g.has_edge e.u e.v e.w\n\n-- 2. Connectivity for Solution Edges (Undirected View)\n-- Dafny: `edge_in_set` checks (u, v) || (v, u)\ndef edges_contain (edges : List MSTEdge) (u v : Nat) : Prop :=\n ∃ e, e ∈ edges ∧ ((e.u = u ∧ e.v = v) ∨ (e.u = v ∧ e.v = u))\n\ndef path_follows_edges (edges : List MSTEdge) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n edges_contain edges (p.getD i 0) (p.getD (i + 1) 0)\n\ndef is_connected_with_edges (g : WeightedGraph) (edges : List MSTEdge) : Prop :=\n ∀ u v, u < g.size → v < g.size →\n ∃ p, p.head? = some u ∧ p.getLast? = some v ∧ path_follows_edges edges p\n\n-- 3. Spanning Tree Definition\ndef is_spanning_tree (g : WeightedGraph) (edges : List MSTEdge) : Prop :=\n edges.length = g.size - 1 ∧\n (∀ e, e ∈ edges → e.is_valid g) ∧\n is_connected_with_edges g edges\n -- Note: |V|-1 edges + Connected implies Acyclic (Tree)\n\ndef total_weight (edges : List MSTEdge) : Int :=\n edges.foldl (λ sum e => sum + e.w) 0\n\n-- 4. Optimality (MST)\ndef is_mst (g : WeightedGraph) (edges : List MSTEdge) : Prop :=\n is_spanning_tree g edges ∧\n ∀ other, is_spanning_tree g other → total_weight edges ≤ total_weight other\n\n-- 5. Graph Connectivity (Precondition)\n-- Dafny uses directed edges for the graph path check: `graph_has_any_edge`\ndef path_follows_graph (g : WeightedGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n ∃ w, g.has_edge (p.getD i 0) (p.getD (i + 1) 0) w\n\ndef WeightedGraph.is_connected (g : WeightedGraph) : Prop :=\n ∀ u v, u < g.size → v < g.size →\n ∃ p, p.head? = some u ∧ p.getLast? = some v ∧ path_follows_graph g p\n\ndef WeightedGraph.weights_bounded (g : WeightedGraph) : Prop :=\n g.size ≤ 100000 ∧\n ∀ u v w, g.has_edge u v w → w ≥ -100000 ∧ w ≤ 100000\n\n-- Precondition definitions\n@[reducible, simp]\ndef prim_mst_precond (graph : WeightedGraph) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧\n graph.size > 0 ∧\n graph.is_connected ∧\n graph.weights_bounded ∧\n -- Undirected: if u→v with weight w exists, then so does v→u with the same weight\n (∀ u v w, graph.has_edge u v w → graph.has_edge v u w)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef prim_mst (graph : WeightedGraph)\n (h_precond : prim_mst_precond graph) : List MSTEdge :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef prim_mst_postcond (graph : WeightedGraph) (result : List MSTEdge)\n (_ : prim_mst_precond graph) : Prop :=\n -- !benchmark @start postcond\n is_mst graph result\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem prim_mst_postcond_satisfied (graph : WeightedGraph)\n (h_precond : prim_mst_precond graph) :\n prim_mst_postcond graph (prim_mst graph h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof"}
{"problem_id": "push_relabel", "nl_desc": "Your task is to implement the Push-Relabel Algorithm to find the maximum flow in a directed graph and verify its functional correctness in Lean using Mathlib. Preconditions: You can assume the graph contains at most 1,000 nodes. You can assume edge capacities are between 0 and 100,000. The Source s and Sink t are valid, distinct nodes. Crucially, the graph is a Simple Graph (no parallel edges between the same two nodes), ensuring that capacities between any pair of nodes are unique. Requirements: Implement the function max_flow_value which returns the integer value of the max flow. Algorithm: You must implement the Push-Relabel strategy. This involves maintaining a \"Preflow\" (where Flow In $\\ge$ Flow Out) and a \"Height\" function for every node. Operations: You should repeatedly perform Push (moving excess flow to a lower neighbor) and Relabel (increasing the height of a node) operations until no nodes have excess flow. Output: The returned value must be equal to the net flow out of the source in a valid maximum flow assignment. Verification Challenges: (1) Height Invariant: Proving that for every edge $(u, v)$ with residual capacity, $height(u) \\le height(v) + 1$. This guarantees that the flow never goes \"uphill\" too steeply, preventing cycles. (2) Preflow to Valid Flow: Unlike augmenting path algorithms, the intermediate state here is invalid (nodes have excess). You must prove that when the algorithm terminates (no active nodes), the Preflow becomes a valid Flow. (3) Termination (Well-Foundedness): Proving the algorithm doesn't run forever is critical in Lean; you likely need a defined measure (e.g., based on heights of active nodes) to prove the operations strictly decrease a well-founded metric.", "spec": "import Mathlib\n\nstructure CapacityGraph where\n adj : Array (Array (Nat × Int))\n\ndef CapacityGraph.size (g : CapacityGraph) : Nat :=\n g.adj.size\n\ndef CapacityGraph.has_capacity (g : CapacityGraph) (u v : Nat) (c : Int) : Prop :=\n u < g.size ∧\n ∃ pair, pair ∈ g.adj.getD u #[] ∧ pair.1 = v ∧ pair.2 = c\n\ndef CapacityGraph.well_formed (g : CapacityGraph) : Prop :=\n ∀ u, u < g.size →\n (∀ pair, pair ∈ g.adj.getD u #[] → pair.1 < g.size) ∧\n -- Unique targets\n (∀ p1 p2, p1 ∈ g.adj.getD u #[] → p2 ∈ g.adj.getD u #[] → p1.1 = p2.1 → p1 = p2)\n\n-- A flow map assigns a flow value to each edge (u, v)\n-- We can model this as a function or list of entries.\n-- Since the result is just the max flow value, we can keep FlowMap abstract in the spec\n-- or define it as `Nat -> Nat -> Int`.\n-- Dijkstra and others used abstract/implicit paths. here we need explicit flow values.\ndef FlowMap := Nat → Nat → Int\n\ndef FlowMap.get (f : FlowMap) (u v : Nat) : Int := f u v\n\ndef CapacityGraph.respects_capacity (g : CapacityGraph) (f : FlowMap) : Prop :=\n ∀ u v,\n (f.get u v > 0 →\n ∃ c, g.has_capacity u v c ∧ f.get u v ≤ c) ∧\n (f.get u v >= 0) -- Non-negative flow\n\ndef flow_out (g : CapacityGraph) (f : FlowMap) (u : Nat) : Int :=\n -- Sum of f(u, v) for all v. Hard to define as sum over infinite/large domain without Finset.\n -- But we can sum over the adjacency list of u.\n -- Since well_formed ensures unique neighbors, we can map adj to flow and sum.\n let neighbors := g.adj.getD u #[]\n neighbors.foldl (λ sum pair => sum + f.get u pair.1) 0\n\ndef flow_in (g : CapacityGraph) (f : FlowMap) (u : Nat) : Int :=\n -- Sum of f(v, u). This requires iterating over all nodes v that have edge to u.\n -- We can iterate over all nodes v from 0 to size-1.\n -- This is a bit computationally heavy for definition but fine for spec.\n (List.range g.size).foldl (λ sum v => sum + f.get v u) 0\n\ndef is_conserved (g : CapacityGraph) (f : FlowMap) (s t : Nat) : Prop :=\n ∀ u, u < g.size → u ≠ s → u ≠ t →\n flow_in g f u = flow_out g f u\n\ndef is_valid_flow (g : CapacityGraph) (f : FlowMap) (s t : Nat) : Prop :=\n g.respects_capacity f ∧ is_conserved g f s t\n\ndef flow_val (g : CapacityGraph) (f : FlowMap) (s : Nat) : Int :=\n flow_out g f s - flow_in g f s\n\ndef is_max_flow (g : CapacityGraph) (f : FlowMap) (s t : Nat) : Prop :=\n is_valid_flow g f s t ∧\n ∀ other, is_valid_flow g other s t → flow_val g f s ≥ flow_val g other s\n\ndef CapacityGraph.capacities_bounded (g : CapacityGraph) : Prop :=\n g.size ≤ 1000 ∧\n ∀ u v c, g.has_capacity u v c → c ≥ 0 ∧ c ≤ 100000\n\n-- Precondition definitions\n@[reducible, simp]\ndef max_flow_value_precond (graph : CapacityGraph) (s t : Nat) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧\n graph.capacities_bounded ∧\n s < graph.size ∧\n t < graph.size ∧\n s ≠ t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef max_flow_value (graph : CapacityGraph) (s t : Nat)\n (h_precond : max_flow_value_precond graph s t) : Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef max_flow_value_postcond (graph : CapacityGraph) (s t : Nat) (result : Int)\n (_ : max_flow_value_precond graph s t) : Prop :=\n -- !benchmark @start postcond\n ∃ f, is_max_flow graph f s t ∧ flow_val graph f s = result\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem max_flow_value_postcond_satisfied (graph : CapacityGraph) (s t : Nat)\n (h_precond : max_flow_value_precond graph s t) :\n max_flow_value_postcond graph s t (max_flow_value graph s t h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "queue_dequeue", "nl_desc": "Your task is to implement a Queue and verify its First-In, First-Out correctness in Lean. The queue is modeled as a sequence. You should implement and verify the dequeue operation. Dequeue: Verify that the operation returns the first element (index 0) of the sequence and that the new model sequence contains all remaining elements in their original relative order (the tail of the list). In a functional setting, the function should accept a proof of non-emptiness and return both the dequeued value and the new queue state.\n", "spec": "import Mathlib\n\nstructure VerifiableQueue (T : Type) where\n data : List T\n\ndef VerifiableQueue.view {T} (q : VerifiableQueue T) : List T :=\n q.data\n\ndef VerifiableQueue.is_valid {T} (q : VerifiableQueue T) : Prop :=\n True\n\n-- Precondition definitions\n@[reducible, simp]\ndef dequeue_precond {T} (q : VerifiableQueue T) : Prop :=\n -- !benchmark @start precond\n q.is_valid ∧ q.view.length > 0\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\n-- Returns the projected pair (val, new_queue)\ndef dequeue {T} (q : VerifiableQueue T)\n (h_precond : dequeue_precond q) : T × VerifiableQueue T :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef dequeue_postcond {T} (q : VerifiableQueue T) (result : T × VerifiableQueue T)\n (_ : dequeue_precond q) : Prop :=\n -- !benchmark @start postcond\n let (val, new_queue) := result\n new_queue.is_valid ∧\n -- Concise & Proof-Free: The old queue consists of the return value followed by the new queue.\n q.view = val :: new_queue.view\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem dequeue_postcond_satisfied {T} (q : VerifiableQueue T)\n (h_precond : dequeue_precond q) :\n dequeue_postcond q (dequeue q h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "queue_enqueue", "nl_desc": "Your task is to implement a Queue and verify its First-In, First-Out correctness in Lean. The queue is modeled as a sequence where elements enter at the \"back\" and exit from the \"front.\" You should implement and verify the enqueue (push) operation. Enqueue: Verify that the item is appended to the end of the model sequence. In a functional setting, this means returning a new queue with the item appended to the data list.\n", "spec": "import Mathlib\n\nstructure VerifiableQueue (T : Type) where\n data : List T\n\ndef VerifiableQueue.view {T} (q : VerifiableQueue T) : List T :=\n q.data\n\ndef VerifiableQueue.is_valid {T} (q : VerifiableQueue T) : Prop :=\n True\n\n-- Precondition definitions\n@[reducible, simp]\ndef enqueue_precond {T} (q : VerifiableQueue T) (v : T) : Prop :=\n -- !benchmark @start precond\n q.is_valid\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef enqueue {T} (q : VerifiableQueue T) (v : T)\n (h_precond : enqueue_precond q v) : VerifiableQueue T :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef enqueue_postcond {T} (q : VerifiableQueue T) (v : T) (result : VerifiableQueue T)\n (_ : enqueue_precond q v) : Prop :=\n -- !benchmark @start postcond\n result.is_valid ∧\n result.view = q.view ++ [v]\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem enqueue_postcond_satisfied {T} (q : VerifiableQueue T) (v : T)\n (h_precond : enqueue_precond q v) :\n enqueue_postcond q v (enqueue q v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "quick_sort", "nl_desc": "Your task is to implement the Quick Sort algorithm and verify its correctness in Lean. Given a list of integers, return a new list containing the same elements sorted in non-decreasing order. Postconditions: (1) Output is sorted. (2) Output is a permutation of input. Preconditions: None. Implementation: Recursive divide-and-conquer. Pick a pivot (e.g., head), partition the remaining elements into those less-than-or-equal and those greater-than, then recursively sort and append. Use 'partial' or prove termination (decreasing size).\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef quickSort_precond (v : List Int) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef quickSort (v : List Int) (h_precond : quickSort_precond v) : List Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef quickSort_postcond (v : List Int) (result : List Int) (h_precond : quickSort_precond v) : Prop :=\n -- !benchmark @start postcond\n List.Sorted (· ≤ ·) result ∧ result.Perm v\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem quickSort_postcond_satisfied (v : List Int) (h_precond : quickSort_precond v) :\n quickSort_postcond v (quickSort v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "ringbuffer_dequeue", "nl_desc": "Your task is to implement and verify the dequeue operation for a Ring Buffer data structure in Lean. Because the ring buffer uses a fixed-size underlying array, the \"front\" of the queue is tracked by a head pointer. Verification: Verify that the operation returns the element currently at the \"front\" of the logical sequence (index 0). Structural Update: You must prove that after the element is returned, the new model view is shortened by one and contains the remaining elements in their original relative order (tail of the view). Capacity remains unchanged.\n", "spec": "import Mathlib\n\nstructure RingBuffer (T : Type) where\n capacity : Nat\n view : List T\n\ndef RingBuffer.is_valid {T} (rb : RingBuffer T) : Prop :=\n rb.capacity > 0 ∧ rb.view.length ≤ rb.capacity\n\n-- Precondition definitions\n@[reducible, simp]\ndef dequeue_precond {T} (rb : RingBuffer T) : Prop :=\n -- !benchmark @start precond\n rb.is_valid ∧ rb.view.length > 0\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\n-- Returns (value, new_ringbuffer)\ndef dequeue {T} (rb : RingBuffer T)\n (h_precond : dequeue_precond rb) : T × RingBuffer T :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef dequeue_postcond {T} (rb : RingBuffer T) (result : T × RingBuffer T)\n (_ : dequeue_precond rb) : Prop :=\n -- !benchmark @start postcond\n let (val, new_rb) := result\n new_rb.is_valid ∧\n new_rb.capacity = rb.capacity ∧\n -- Concise & Proof-Free: Old view is (val :: new_view)\n rb.view = val :: new_rb.view\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem dequeue_postcond_satisfied {T} (rb : RingBuffer T)\n (h_precond : dequeue_precond rb) :\n dequeue_postcond rb (dequeue rb h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "ringbuffer_enqueue", "nl_desc": "Your task is to implement and verify the circular enqueue operation for a Ring Buffer in Lean. The ring buffer is modeled as a sequence with a fixed capacity. Enqueue: Verify that if the buffer is not full, the element is appended. If the buffer is full, the oldest element (head) is discarded, and the new element is appended, maintaining the fixed capacity (First-In, First-Out with Overwrite). In a functional setting, return a new Ring Buffer state.\n", "spec": "import Mathlib\n\nstructure RingBuffer (T : Type) where\n capacity : Nat\n view : List T\n\ndef RingBuffer.is_valid {T} (rb : RingBuffer T) : Prop :=\n rb.capacity > 0 ∧ rb.view.length ≤ rb.capacity\n\n-- Precondition definitions\n@[reducible, simp]\ndef enqueue_precond {T} (rb : RingBuffer T) (v : T) : Prop :=\n -- !benchmark @start precond\n rb.is_valid\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef enqueue {T} (rb : RingBuffer T) (v : T)\n (h_precond : enqueue_precond rb v) : RingBuffer T :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef enqueue_postcond {T} (rb : RingBuffer T) (v : T) (result : RingBuffer T)\n (_ : enqueue_precond rb v) : Prop :=\n -- !benchmark @start postcond\n result.is_valid ∧\n result.capacity = rb.capacity ∧\n (rb.view.length < rb.capacity → result.view = rb.view ++ [v]) ∧\n (rb.view.length = rb.capacity → result.view = rb.view.drop 1 ++ [v])\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem enqueue_postcond_satisfied {T} (rb : RingBuffer T) (v : T)\n (h_precond : enqueue_precond rb v) :\n enqueue_postcond rb v (enqueue rb v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "rod_cutting", "nl_desc": "Your task is to implement the Rod Cutting algorithm and verify its correctness in Lean. Given a rod of length n and a table of prices (where prices[i] is the price of a piece of length i+1), determine the maximum revenue obtainable by cutting the rod given the prices. Postconditions: Upper Bound (result ≥ revenue of any valid cut strategy) and Existence (result = revenue of some valid cut strategy). Valid strategy means cut lengths sum to n.\n", "spec": "import Mathlib\n\ndef sum_lengths (cuts : List Nat) : Nat :=\n cuts.foldl (· + ·) 0\n\ndef get_price (prices : List Nat) (len : Nat) : Nat :=\n if len > 0 then prices.getD (len - 1) 0 else 0\n\ndef calculate_revenue (cuts : List Nat) (prices : List Nat) : Nat :=\n cuts.foldl (fun acc len => acc + get_price prices len) 0\n\ndef is_valid_strategy (cuts : List Nat) (n : Nat) (prices : List Nat) : Prop :=\n (∀ len ∈ cuts, len > 0 ∧ len - 1 < prices.length) ∧\n sum_lengths cuts = n\n\n-- Precondition definitions\n@[reducible, simp]\ndef rod_cutting_precond (n : Nat) (prices : List Nat) : Prop :=\n -- !benchmark @start precond\n prices.length ≥ n ∧\n n ≤ 1000 ∧\n (∀ i, i < prices.length → prices.getD i 0 ≤ 10000)\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef rod_cutting (n : Nat) (prices : List Nat)\n (h_precond : rod_cutting_precond n prices) : Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef rod_cutting_postcond (n : Nat) (prices : List Nat) (result : Nat)\n (_ : rod_cutting_precond n prices) : Prop :=\n -- !benchmark @start postcond\n (∀ cuts, is_valid_strategy cuts n prices → calculate_revenue cuts prices ≤ result) ∧\n (∃ cuts, is_valid_strategy cuts n prices ∧ calculate_revenue cuts prices = result)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem rod_cutting_postcond_satisfied (n : Nat) (prices : List Nat)\n (h_precond : rod_cutting_precond n prices) :\n rod_cutting_postcond n prices (rod_cutting n prices h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "scc_tarjan", "nl_desc": "Your task is to implement Tarjan's Algorithm to find the Strongly Connected Components (SCCs) of a directed graph and verify its functional correctness in Lean using Mathlib. Preconditions: You can assume the graph contains at most 1,000 nodes. The graph is directed and represented as an adjacency list. Requirements: Implement the function find_sccs which returns a List (List Nat). Output Format: The result must be a list of lists, where each inner list represents one Strongly Connected Component. Partition: The set of returned components must form a partition of the graph vertices. Strong Connectivity: For every component returned, and for every pair of nodes $(u, v)$ within that component, there must exist a path $u \\rightsquigarrow v$ AND a path $v \\rightsquigarrow u$. Maximality: The components must be maximal. This means no two components can be merged to form a larger strongly connected set. Formally, if there is a path from Component A to Component B, there must be no path from Component B back to Component A (The \"Condensation Graph\" must be a DAG). Verification Challenges: (1) DFS Invariants: Tarjan's algorithm uses discovery_time and low_link values. You must prove that low_link[u] correctly identifies the highest ancestor reachable from u in the DFS tree. (2) Stack Property: You must prove that the stack used during DFS correctly tracks the current path and potential \"open\" SCCs. (3) Maximality Proof: Proving that the algorithm does not \"break\" a large component into smaller valid pieces.\n", "spec": "import Mathlib\n\nstructure SCCGraph where\n adj : Array (Array Nat)\n\ndef SCCGraph.well_formed (g : SCCGraph) : Prop :=\n ∀ u, u < g.adj.size →\n ∀ v, v ∈ g.adj.getD u #[] → v < g.adj.size\n\ndef SCCGraph.size (g : SCCGraph) : Nat :=\n g.adj.size\n\ndef SCCGraph.has_edge (g : SCCGraph) (u v : Nat) : Prop :=\n u < g.adj.size ∧\n v ∈ g.adj.getD u #[]\n\ndef SCCGraph.is_path (g : SCCGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n g.has_edge (p.getD i 0) (p.getD (i + 1) 0)\n\ndef SCCGraph.has_path (g : SCCGraph) (u v : Nat) : Prop :=\n ∃ p, g.is_path p ∧ p.head? = some u ∧ p.getLast? = some v\n\ndef SCCGraph.is_strongly_connected (g : SCCGraph) (comp : List Nat) : Prop :=\n comp.length > 0 ∧\n (∀ u, u ∈ comp → u < g.size) ∧\n ∀ u v, u ∈ comp → v ∈ comp →\n (g.has_path u v ∧ g.has_path v u)\n\ndef SCCGraph.is_partition (g : SCCGraph) (sccs : List (List Nat)) : Prop :=\n -- Enforce no duplicate components to match index-based disjointness\n sccs.Nodup ∧ \n -- All nodes covered\n (∀ u, u < g.size → ∃ comp, comp ∈ sccs ∧ u ∈ comp) ∧\n -- Disjoint\n (∀ c1 c2, c1 ∈ sccs → c2 ∈ sccs → c1 ≠ c2 →\n ∀ u, u ∈ c1 → u ∉ c2)\n\ndef SCCGraph.scc_has_path (g : SCCGraph) (c1 c2 : List Nat) : Prop :=\n ∃ u v, u ∈ c1 ∧ v ∈ c2 ∧ g.has_path u v\n\ndef SCCGraph.is_maximal_scc_structure (g : SCCGraph) (sccs : List (List Nat)) : Prop :=\n ∀ c1 c2, c1 ∈ sccs → c2 ∈ sccs → c1 ≠ c2 →\n (g.scc_has_path c1 c2 → ¬ g.scc_has_path c2 c1)\n\ndef SCCGraph.is_valid_scc_result (g : SCCGraph) (sccs : List (List Nat)) : Prop :=\n g.is_partition sccs ∧\n (∀ c, c ∈ sccs → g.is_strongly_connected c) ∧\n g.is_maximal_scc_structure sccs\n\n-- Precondition definitions\n@[reducible, simp]\ndef find_sccs_precond (graph : SCCGraph) : Prop :=\n -- !benchmark @start precond\n graph.well_formed ∧ graph.size ≤ 1000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef find_sccs (graph : SCCGraph)\n (h_precond : find_sccs_precond graph) : List (List Nat) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef find_sccs_postcond (graph : SCCGraph) (result : List (List Nat))\n (_ : find_sccs_precond graph) : Prop :=\n -- !benchmark @start postcond\n graph.is_valid_scc_result result\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem find_sccs_postcond_satisfied (graph : SCCGraph)\n (h_precond : find_sccs_precond graph) :\n find_sccs_postcond graph (find_sccs graph h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "segmenttree_build", "nl_desc": "Your task is to implement the build operation for a Range Maximum Segment Tree and verify its correctness in Lean. The operation builds a tree for range [l, r) initialized to 0. Postconditions: Result is a valid Segment Tree, covers range [l, r), and all values in view are 0. Invariants: Node value is max of children, ranges match children ranges.\n", "spec": "import Mathlib\n\ninductive Node\n| mk (val : Int) (low high : Int) (left right : Option Node)\nderiving Inhabited\n\nnamespace Node\n\ndef val (t : Node) : Int :=\n match t with | Node.mk v _ _ _ _ => v\n\ndef low (t : Node) : Int :=\n match t with | Node.mk _ l _ _ _ => l\n\ndef high (t : Node) : Int :=\n match t with | Node.mk _ _ h _ _ => h\n\ndef left (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ l _ => l\n\ndef right (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ _ r => r\n\nend Node\n\ndef view (t : Node) : Int → Option Int :=\n fun k =>\n if k < t.low ∨ k ≥ t.high then none\n else\n match t.left, t.right with\n | some l, some r =>\n if k < l.high then view l k else view r k\n | _, _ => some t.val\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_segment_tree (t : Node) : Prop :=\n 0 ≤ t.low ∧ t.low < t.high ∧ 0 ≤ t.val ∧\n match t.left, t.right with\n | some l, some r =>\n let mid := (t.low + t.high) / 2\n l.low = t.low ∧ l.high = mid ∧\n r.low = mid ∧ r.high = t.high ∧\n is_segment_tree l ∧ is_segment_tree r ∧\n t.val = max l.val r.val\n | none, none =>\n t.high = t.low + 1\n | _, _ => False\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\n-- Precondition definitions\n@[reducible, simp]\ndef build_precond (l r : Int) : Prop :=\n -- !benchmark @start precond\n 0 ≤ l ∧ l < r\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef build (l r : Int)\n (h_precond : build_precond l r) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Abstract View Helper used in Postcond\ndef tree_view_at (t : Node) (k : Int) : Int :=\n match view t k with\n | some v => v\n | none => 0 -- Should not happen for in-range queries if consistent\n\n-- Postcondition definitions\n@[reducible, simp]\ndef build_postcond (l r : Int) (result : Node)\n (_ : build_precond l r) : Prop :=\n -- !benchmark @start postcond\n is_segment_tree result ∧\n result.low = l ∧\n result.high = r ∧\n (∀ k, l ≤ k ∧ k < r → tree_view_at result k = 0)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem build_postcond_satisfied (l r : Int)\n (h_precond : build_precond l r) :\n build_postcond l r (build l r h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "segmenttree_modify", "nl_desc": "Your task is to implement the update (modify) operation for a Range Maximum Segment Tree and verify its correctness in Lean. The operation updates the value at a specific index using path copying (functional update). Postconditions: Functional Correctness (view(result) = view(node)[idx := v]) and Invariant Preservation (result satisfies is_segment_tree). Frame Preservation (other indices unchanged).\n", "spec": "import Mathlib\n\ninductive Node\n| mk (val : Int) (low high : Int) (left right : Option Node)\nderiving Inhabited\n\nnamespace Node\n\ndef val (t : Node) : Int :=\n match t with | Node.mk v _ _ _ _ => v\n\ndef low (t : Node) : Int :=\n match t with | Node.mk _ l _ _ _ => l\n\ndef high (t : Node) : Int :=\n match t with | Node.mk _ _ h _ _ => h\n\ndef left (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ l _ => l\n\ndef right (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ _ r => r\n\nend Node\n\ndef view (t : Node) : Int → Option Int :=\n fun k =>\n if k < t.low ∨ k ≥ t.high then none\n else\n match t.left, t.right with\n | some l, some r =>\n if k < l.high then view l k else view r k\n | _, _ => some t.val\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_segment_tree (t : Node) : Prop :=\n 0 ≤ t.low ∧ t.low < t.high ∧ 0 ≤ t.val ∧\n match t.left, t.right with\n | some l, some r =>\n let mid := (t.low + t.high) / 2\n l.low = t.low ∧ l.high = mid ∧\n r.low = mid ∧ r.high = t.high ∧\n is_segment_tree l ∧ is_segment_tree r ∧\n t.val = max l.val r.val\n | none, none =>\n t.high = t.low + 1\n | _, _ => False\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\n-- Precondition definitions\n@[reducible, simp]\ndef modify_precond (node : Node) (idx : Int) (v : Int) : Prop :=\n -- !benchmark @start precond\n 0 ≤ v ∧\n is_segment_tree node ∧\n node.low ≤ idx ∧ idx < node.high\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef updateTree (node : Node) (idx : Int) (v : Int)\n (h_precond : modify_precond node idx v) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Abstract View Helper used in Postcond\ndef tree_view_at (t : Node) (k : Int) : Int :=\n match view t k with\n | some v => v\n | none => 0\n\n-- Postcondition definitions\n@[reducible, simp]\ndef modify_postcond (node : Node) (idx : Int) (v : Int) (result : Node)\n (_ : modify_precond node idx v) : Prop :=\n -- !benchmark @start postcond\n is_segment_tree result ∧\n result.low = node.low ∧\n result.high = node.high ∧\n (∀ k, node.low ≤ k ∧ k < node.high →\n if k = idx then tree_view_at result k = v\n else tree_view_at result k = tree_view_at node k)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem modify_postcond_satisfied (node : Node) (idx : Int) (v : Int)\n (h_precond : modify_precond node idx v) :\n modify_postcond node idx v (updateTree node idx v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "segmenttree_query", "nl_desc": "Your task is to implement the query operation for a Range Maximum Segment Tree and verify its correctness in Lean. The operation queries the maximum value in range [ql, qr). Postconditions: Correctness (result ≥ all values in range), Tightness (result exists in range). Invariant: Node is a valid segment tree.\n", "spec": "import Mathlib\n\ninductive Node\n| mk (val : Int) (low high : Int) (left right : Option Node)\nderiving Inhabited\n\nnamespace Node\n\ndef val (t : Node) : Int :=\n match t with | Node.mk v _ _ _ _ => v\n\ndef low (t : Node) : Int :=\n match t with | Node.mk _ l _ _ _ => l\n\ndef high (t : Node) : Int :=\n match t with | Node.mk _ _ h _ _ => h\n\ndef left (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ l _ => l\n\ndef right (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ _ r => r\n\nend Node\n\ndef view (t : Node) : Int → Option Int :=\n fun k =>\n if k < t.low ∨ k ≥ t.high then none\n else\n match t.left, t.right with\n | some l, some r =>\n if k < l.high then view l k else view r k\n | _, _ => some t.val\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_segment_tree (t : Node) : Prop :=\n 0 ≤ t.low ∧ t.low < t.high ∧ 0 ≤ t.val ∧\n match t.left, t.right with\n | some l, some r =>\n let mid := (t.low + t.high) / 2\n l.low = t.low ∧ l.high = mid ∧\n r.low = mid ∧ r.high = t.high ∧\n is_segment_tree l ∧ is_segment_tree r ∧\n t.val = max l.val r.val\n | none, none =>\n t.high = t.low + 1\n | _, _ => False\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\n-- Precondition definitions\n@[reducible, simp]\ndef query_precond (node : Node) (ql qr : Int) : Prop :=\n -- !benchmark @start precond\n is_segment_tree node ∧\n ql < qr ∧\n node.low ≤ ql ∧\n qr ≤ node.high\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef query (node : Node) (ql qr : Int)\n (h_precond : query_precond node ql qr) : Int :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Abstract View Helper used in Postcond\ndef tree_view_at (t : Node) (k : Int) : Int :=\n match view t k with\n | some v => v\n | none => 0\n\n-- Postcondition definitions\n@[reducible, simp]\ndef query_postcond (node : Node) (ql qr : Int) (result : Int)\n (_ : query_precond node ql qr) : Prop :=\n -- !benchmark @start postcond\n result ≥ 0 ∧\n (∀ k, ql ≤ k ∧ k < qr → tree_view_at node k ≤ result) ∧\n (∃ k, ql ≤ k ∧ k < qr ∧ tree_view_at node k = result)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem query_postcond_satisfied (node : Node) (ql qr : Int)\n (h_precond : query_precond node ql qr) :\n query_postcond node ql qr (query node ql qr h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "sieve_method", "nl_desc": "Your task is to implement the classic Sieve of Eratosthenes algorithm to find all prime numbers up to a limit n and verify its correctness in Lean. Given a non-negative integer n, return a boolean array of size n where the i-th element is true iff i is prime. Preconditions: The input n is a natural number (Nat). You may assume n <= 100,000. Requirements: Implement the function `sieve_of_eratosthenes` which returns an `Array Bool`. (1) Output Format: The returned array must be of size n. For any index i, result[i] should be true if i is prime, and false otherwise. (2) Algorithm: Initialize a boolean array to true. Iterate i from 2 up to sqrt(n). If i is prime, iterate j starting from i*i up to n with a step size of i (j += i), marking these indices as false. Verification Challenges: (1) Array Invariants: You must maintain an invariant over the entire array. For example: \"For all k < n, if arr[k] is true, then k has no prime factors smaller than the current loop index i\". (2) Inner Loop Stride: The inner loop increments by i. You must prove that every index j visited by this loop is indeed a multiple of i. (3) Optimization Logic (i*i): The standard Sieve starts marking multiples from i*i. To verify this is safe, you must prove the lemma that any composite number x < i*i must have a prime factor strictly smaller than i.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef sieve_of_eratosthenes_precond (n : Nat) : Prop :=\n -- !benchmark @start precond\n n ≤ 100_000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef sieve_of_eratosthenes (n : Nat) (h_precond : sieve_of_eratosthenes_precond n) : Array Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef sieve_of_eratosthenes_postcond (n : Nat) (result : Array Bool) (h_precond : sieve_of_eratosthenes_precond n) : Prop :=\n -- !benchmark @start postcond\n result.size = n ∧\n ∀ i : Nat, i < n →\n ((result.getD i false = true) ↔\n (i > 1 ∧ ∀ d : Nat, (1 < d ∧ d < i) → i % d ≠ 0))\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem sieve_of_eratosthenes_postcond_satisfied (n : Nat) (h_precond : sieve_of_eratosthenes_precond n) :\n sieve_of_eratosthenes_postcond n (sieve_of_eratosthenes n h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "splaytree_splay", "nl_desc": "Your task is to implement the Splay operation in a splay tree and verify its correctness in Lean. Splaying moves a target node (or the last accessed node) to the root via rotations (Zig, Zig-Zig, Zig-Zag). Postconditions: Result is BST, set of values is unchanged, and if v is in the tree, result.val = v.\n", "spec": "import Mathlib\n\ninductive SplayTree\n| empty\n| node (val : Int) (left right : SplayTree)\nderiving Inhabited\n\nnamespace SplayTree\n\ndef view (t : SplayTree) : Set Int :=\n match t with\n | empty => ∅\n | node v l r => view l ∪ view r ∪ {v}\ntermination_by sizeOf t\n\ndef is_bst (t : SplayTree) : Prop :=\n match t with\n | empty => True\n | node v l r =>\n (∀ x ∈ view l, x < v) ∧\n is_bst l ∧\n (∀ x ∈ view r, x > v) ∧\n is_bst r\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef val (t : SplayTree) : Option Int :=\n match t with | node v _ _ => some v | empty => none\n\ndef is_node (t : SplayTree) : Bool :=\n match t with | node _ _ _ => true | empty => false\n\n-- Precondition definitions\n@[reducible, simp]\ndef splay_precond (t : SplayTree) (v : Int) : Prop :=\n -- !benchmark @start precond\n t.is_node ∧ is_bst t\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef splay (t : SplayTree) (v : Int)\n (h_precond : splay_precond t v) : SplayTree :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef splay_postcond (t : SplayTree) (v : Int) (result : SplayTree)\n (_ : splay_precond t v) : Prop :=\n -- !benchmark @start postcond\n result.is_node ∧\n is_bst result ∧\n view result = view t ∧\n (v ∈ view t → result.val = some v)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem splay_postcond_satisfied (t : SplayTree) (v : Int)\n (h_precond : splay_precond t v) :\n splay_postcond t v (splay t v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n\nend SplayTree\n"}
{"problem_id": "stack_pop", "nl_desc": "Your task is to implement a Stack data structure and verify its Last-In, First-Out behavior in Lean. The stack is modeled as a mathematical sequence. Specifically, you are required to implement and verify the pop operation. Pop: You must prove that the operation returns the last element of the sequence and that the resulting model sequence is the original sequence minus its last element. You must also verify the safety precondition that the stack is non-empty before a pop. In a functional setting, the function should accept a proof of non-emptiness and return both the popped value and the new stack state.\n", "spec": "import Mathlib\n\nstructure VerifiableStack (T : Type) where\n data : List T\n\ndef VerifiableStack.view {T} (s : VerifiableStack T) : List T :=\n s.data\n\ndef VerifiableStack.is_valid {T} (s : VerifiableStack T) : Prop :=\n True\n\n-- Precondition definitions\n@[reducible, simp]\ndef pop_precond {T} (s : VerifiableStack T) : Prop :=\n -- !benchmark @start precond\n s.is_valid ∧ s.view.length > 0\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\n-- Returns the projected pair (val, new_stack)\ndef pop {T} (s : VerifiableStack T)\n (h_precond : pop_precond s) : T × VerifiableStack T :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef pop_postcond {T} (s : VerifiableStack T) (result : T × VerifiableStack T)\n (_ : pop_precond s) : Prop := -- We don't even need the proof term 'h' here!\n -- !benchmark @start postcond\n let (val, new_stack) := result\n new_stack.is_valid ∧\n -- Concise & Idiomatic: The old stack was the new stack followed by the popped value.\n s.view = new_stack.view ++ [val]\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem pop_postcond_satisfied {T} (s : VerifiableStack T)\n (h_precond : pop_precond s) :\n pop_postcond s (pop s h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "stack_push", "nl_desc": "Your task is to implement a Stack data structure and verify its Last-In, First-Out behavior in Lean. The stack is modeled as a mathematical sequence where elements are added and removed from the \"top\" (the end of the sequence). Specifically, you are required to implement and verify the push operation. Push: You must prove that after pushing an element, the new model sequence is equal to the old sequence with the new element appended to the end. In a functional setting, this means the function returns a new stack whose view is the old view with the element appended.\n", "spec": "import Mathlib\n\nstructure VerifiableStack (T : Type) where\n data : List T\n\ndef VerifiableStack.view {T} (s : VerifiableStack T) : List T :=\n s.data\n\ndef VerifiableStack.is_valid {T} (s : VerifiableStack T) : Prop :=\n True\n\n-- Precondition definitions\n@[reducible, simp]\ndef push_precond {T} (s : VerifiableStack T) (v : T) : Prop :=\n -- !benchmark @start precond\n s.is_valid\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef push {T} (s : VerifiableStack T) (v : T)\n (h_precond : push_precond s v) : VerifiableStack T :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef push_postcond {T} (s : VerifiableStack T) (v : T) (result : VerifiableStack T)\n (_ : push_precond s v) : Prop :=\n -- !benchmark @start postcond\n result.is_valid ∧\n result.view = s.view ++ [v]\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem push_postcond_satisfied {T} (s : VerifiableStack T) (v : T)\n (h_precond : push_precond s v) :\n push_postcond s v (push s v h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "string_search_naive", "nl_desc": "Your task is to implement a Naive String Search algorithm to find all occurrences of a needle (pattern) within a haystack (text) and verify its functional correctness in Lean using Mathlib. Preconditions: You can assume the input text and pattern are arrays of UInt8 (representing bytes) and their lengths are at most 1,000,000. Requirements: Implement the method naive_search which returns an Array Nat containing all starting indices. (1) The method must iterate through every possible starting position in the haystack. (2) For each position, it must perform a character-by-character comparison to check for a match. (3) Soundness & Completeness: You must prove that every index returned is a valid match and that no valid match is omitted. Verification Challenges: (1) Nested Loop Invariants: You must correctly specify the invariant for the inner comparison loop to prove it correctly identifies a match or mismatch. (2) Quantifier Instantiation: Proving completeness involves complex quantifiers (forall indices, if a match exists, it is in the result), which may require manual triggers or ghost code to show the iteration covers the entire search space.\n", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef naive_search_precond (haystack : Array UInt8) (needle : Array UInt8) : Prop :=\n -- !benchmark @start precond\n haystack.size < 1000000 ∧ \n needle.size < 1000000\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef naive_search (haystack : Array UInt8) (needle : Array UInt8)\n (_ : naive_search_precond haystack needle) : Array Nat :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition auxiliary definitions\ndef matches_at (haystack needle : Array UInt8) (i : Nat) : Prop :=\n i + needle.size ≤ haystack.size ∧\n (∀ j, j < needle.size → haystack.getD (i + j) 0 = needle.getD j 0)\n\n-- Postcondition definitions\n@[reducible, simp]\ndef naive_search_postcond (haystack : Array UInt8) (needle : Array UInt8)\n (result : Array Nat) (_ : naive_search_precond haystack needle) : Prop :=\n -- !benchmark @start postcond\n (∀ i, i < result.size → matches_at haystack needle (result.getD i 0)) ∧\n (∀ i, matches_at haystack needle i → ∃ k, k < result.size ∧ result.getD k 0 = i)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem naive_search_postcond_satisfied (haystack : Array UInt8) (needle : Array UInt8)\n (h_precond : naive_search_precond haystack needle) :\n naive_search_postcond haystack needle (naive_search haystack needle h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof"}
{"problem_id": "ternarysearchtree_delete", "nl_desc": "Your task is to implement the Delete operation for a Ternary Search Tree (TST) and verify its correctness in Lean. The operation removes a key and performs \"Eager Pruning\" to remove useless nodes (nodes that are not endpoints and have no children). Postconditions: Functional Correctness (view(result) = view(tree) - {key}) and Invariant Preservation (result satisfies well-formedness).\n", "spec": "import Mathlib\n\ninductive Node\n| mk (val : Int) (is_end : Bool) (left mid right : Option Node)\nderiving Inhabited\n\nnamespace Node\n\ndef val (t : Node) : Int :=\n match t with | Node.mk v _ _ _ _ => v\n\ndef is_end (t : Node) : Bool :=\n match t with | Node.mk _ b _ _ _ => b\n\ndef left (t : Node) : Option Node :=\n match t with | Node.mk _ _ l _ _ => l\n\ndef mid (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ m _ => m\n\ndef right (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ _ r => r\n\nend Node\n\ndef view (t : Node) : Set (List Int) :=\n let l := match t.left with | some child => view child | none => ∅\n let r := match t.right with | some child => view child | none => ∅\n let m := match t.mid with | some child => (view child).image (fun s => t.val :: s) | none => ∅\n let current := if t.is_end then {[t.val]} else ∅\n l ∪ r ∪ m ∪ current\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_empty_node (t : Node) : Bool :=\n !t.is_end ∧ t.left.isNone ∧ t.mid.isNone ∧ t.right.isNone\n\ndef well_formed (t : Node) : Prop :=\n (0 ≤ t.val ∧ t.val < 256) ∧\n -- Check Left\n (match t.left with \n | some l => \n well_formed l ∧ \n !is_empty_node l ∧ -- CRITICAL: Match Dafny pruning\n (∀ s ∈ view l, match s with | [] => False | c::_ => c < t.val) \n | none => True) ∧\n -- Check Right\n (match t.right with \n | some r => \n well_formed r ∧ \n !is_empty_node r ∧ -- CRITICAL\n (∀ s ∈ view r, match s with | [] => False | c::_ => c > t.val) \n | none => True) ∧\n -- Check Mid\n (match t.mid with \n | some m => \n well_formed m ∧ \n !is_empty_node m -- CRITICAL\n | none => True)\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_valid_key (key : List Int) : Prop :=\n ∀ k ∈ key, 0 ≤ k ∧ k < 256\n\n-- Precondition definitions\n@[reducible, simp]\ndef delete_precond (t : Option Node) (key : List Int) : Prop :=\n -- !benchmark @start precond\n (match t with\n | some node => well_formed node ∧ !is_empty_node node\n | none => True) ∧\n is_valid_key key -- Match Dafny input constraint\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef delete (t : Option Node) (key : List Int)\n (h_precond : delete_precond t key) : Option Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- View wrapper for Option Node\ndef view_opt (t : Option Node) : Set (List Int) :=\n match t with\n | some node => view node\n | none => ∅\n\n-- Postcondition definitions\n@[reducible, simp]\ndef delete_postcond (t : Option Node) (key : List Int) (result : Option Node)\n (_ : delete_precond t key) : Prop :=\n -- !benchmark @start postcond\n (match result with\n | some res => well_formed res ∧ !is_empty_node res\n | none => True) ∧\n view_opt result = view_opt t \\ {key}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem delete_postcond_satisfied (t : Option Node) (key : List Int)\n (h_precond : delete_precond t key) :\n delete_postcond t key (delete t key h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "ternarysearchtree_insert", "nl_desc": "Your task is to implement the Insert operation for a Ternary Search Tree (TST) and verify its correctness in Lean. Postconditions: Functional Correctness (view(result) = view(tree) + {key}) and Invariant Preservation (result satisfies well-formedness). Insertion combines BST insertion (left/right) with Trie extension (mid). If path doesn't exist, create new chain.\n", "spec": "import Mathlib\n\ninductive Node\n| mk (val : Int) (is_end : Bool) (left mid right : Option Node)\nderiving Inhabited\n\nnamespace Node\n\ndef val (t : Node) : Int :=\n match t with | Node.mk v _ _ _ _ => v\n\ndef is_end (t : Node) : Bool :=\n match t with | Node.mk _ b _ _ _ => b\n\ndef left (t : Node) : Option Node :=\n match t with | Node.mk _ _ l _ _ => l\n\ndef mid (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ m _ => m\n\ndef right (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ _ r => r\n\nend Node\n\ndef view (t : Node) : Set (List Int) :=\n let l := match t.left with | some child => view child | none => ∅\n let r := match t.right with | some child => view child | none => ∅\n let m := match t.mid with | some child => (view child).image (fun s => t.val :: s) | none => ∅\n let current := if t.is_end then {[t.val]} else ∅\n l ∪ r ∪ m ∪ current\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_empty_node (t : Node) : Bool :=\n !t.is_end ∧ t.left.isNone ∧ t.mid.isNone ∧ t.right.isNone\n\ndef well_formed (t : Node) : Prop :=\n (0 ≤ t.val ∧ t.val < 256) ∧\n -- Check Left\n (match t.left with \n | some l => \n well_formed l ∧ \n !is_empty_node l ∧ -- CRITICAL: Match Dafny pruning\n (∀ s ∈ view l, match s with | [] => False | c::_ => c < t.val) \n | none => True) ∧\n -- Check Right\n (match t.right with \n | some r => \n well_formed r ∧ \n !is_empty_node r ∧ -- CRITICAL\n (∀ s ∈ view r, match s with | [] => False | c::_ => c > t.val) \n | none => True) ∧\n -- Check Mid\n (match t.mid with \n | some m => \n well_formed m ∧ \n !is_empty_node m -- CRITICAL\n | none => True)\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_valid_key (key : List Int) : Prop :=\n ∀ k ∈ key, 0 ≤ k ∧ k < 256\n\n-- Precondition definitions\n@[reducible, simp]\ndef insert_precond (t : Option Node) (key : List Int) : Prop :=\n -- !benchmark @start precond\n (match t with\n | some node => well_formed node ∧ !is_empty_node node\n | none => True) ∧\n is_valid_key key ∧ -- Match Dafny byte constraint\n key.length > 0 -- Match Dafny non-empty constraint\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef insert (t : Option Node) (key : List Int)\n (h_precond : insert_precond t key) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- View wrapper for Option Node\ndef view_opt (t : Option Node) : Set (List Int) :=\n match t with\n | some node => view node\n | none => ∅\n\n-- Postcondition definitions\n@[reducible, simp]\ndef insert_postcond (t : Option Node) (key : List Int) (result : Node)\n (_ : insert_precond t key) : Prop :=\n -- !benchmark @start postcond\n well_formed result ∧\n !is_empty_node result ∧\n view result = view_opt t ∪ {key}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem insert_postcond_satisfied (t : Option Node) (key : List Int)\n (h_precond : insert_precond t key) :\n insert_postcond t key (insert t key h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "ternarysearchtree_search", "nl_desc": "Your task is to implement the Search operation for a Ternary Search Tree (TST) and verify its correctness in Lean. A TST Node has a value (val), an end marker (is_end), and three children: left (val > char), mid (val == char), right (val < char). Postconditions: Functional Correctness (view(result) = view(tree) contains key). Preconditions: Input assumed well-formed. Search logic respects 3-way branching.\n", "spec": "import Mathlib\n\ninductive Node\n| mk (val : Int) (is_end : Bool) (left mid right : Option Node)\nderiving Inhabited\n\nnamespace Node\n\ndef val (t : Node) : Int :=\n match t with | Node.mk v _ _ _ _ => v\n\ndef is_end (t : Node) : Bool :=\n match t with | Node.mk _ b _ _ _ => b\n\ndef left (t : Node) : Option Node :=\n match t with | Node.mk _ _ l _ _ => l\n\ndef mid (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ m _ => m\n\ndef right (t : Node) : Option Node :=\n match t with | Node.mk _ _ _ _ r => r\n\nend Node\n\ndef view (t : Node) : Set (List Int) :=\n let l := match t.left with | some child => view child | none => ∅\n let r := match t.right with | some child => view child | none => ∅\n let m := match t.mid with | some child => (view child).image (fun s => t.val :: s) | none => ∅\n let current := if t.is_end then {[t.val]} else ∅\n l ∪ r ∪ m ∪ current\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_empty_node (t : Node) : Bool :=\n !t.is_end ∧ t.left.isNone ∧ t.mid.isNone ∧ t.right.isNone\n\ndef well_formed (t : Node) : Prop :=\n (0 ≤ t.val ∧ t.val < 256) ∧\n -- Check Left\n (match t.left with \n | some l => \n well_formed l ∧ \n !is_empty_node l ∧ -- CRITICAL: Match Dafny pruning\n (∀ s ∈ view l, match s with | [] => False | c::_ => c < t.val) \n | none => True) ∧\n -- Check Right\n (match t.right with \n | some r => \n well_formed r ∧ \n !is_empty_node r ∧ -- CRITICAL\n (∀ s ∈ view r, match s with | [] => False | c::_ => c > t.val) \n | none => True) ∧\n -- Check Mid\n (match t.mid with \n | some m => \n well_formed m ∧ \n !is_empty_node m -- CRITICAL\n | none => True)\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef contains (t : Node) (key : List Int) : Bool :=\n match key with\n | [] => False -- TST nodes always represent at least one char\n | c :: cs =>\n if c < t.val then\n match t.left with | some l => contains l key | none => False\n else if c > t.val then\n match t.right with | some r => contains r key | none => False\n else -- c == t.val\n match cs with\n | [] => t.is_end\n | _ => match t.mid with | some m => contains m cs | none => False\ntermination_by sizeOf t\ndecreasing_by all_goals sorry\n\ndef is_valid_key (key : List Int) : Prop :=\n ∀ k ∈ key, 0 ≤ k ∧ k < 256\n\n-- Precondition definitions\n@[reducible, simp]\ndef search_precond (t : Option Node) (key : List Int) : Prop :=\n -- !benchmark @start precond\n (match t with\n | some node => well_formed node ∧ !is_empty_node node\n | none => True) ∧\n is_valid_key key -- Match Dafny input constraint\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef search (t : Option Node) (key : List Int)\n (h_precond : search_precond t key) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- View wrapper for Option Node\ndef view_opt (t : Option Node) : Set (List Int) :=\n match t with\n | some node => view node\n | none => ∅\n\n-- Postcondition definitions\n@[reducible, simp]\ndef search_postcond (t : Option Node) (key : List Int) (result : Bool)\n (_ : search_precond t key) : Prop :=\n -- !benchmark @start postcond\n result = (key ∈ view_opt t)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem search_postcond_satisfied (t : Option Node) (key : List Int)\n (h_precond : search_precond t key) :\n search_postcond t key (search t key h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "topological_sort", "nl_desc": "Your task is to implement a Topological Sort algorithm and verify its functional correctness in Lean using Mathlib. You need to implement topological_sort, which takes a directed graph and returns Some(order) containing a valid topological ordering if the graph is a DAG, or None if the graph contains a cycle. There are several verification challenges: (1) Ordering Validity: If returning Some(order), you must prove that order is a permutation of all graph nodes and that for every edge $u \\to v$ in the graph, $u$ appears before $v$ in the sequence. (2) Cycle Detection: If returning None, you must prove that the graph actually contains a cycle. This ensures the algorithm doesn't fail spuriously on valid DAGs.\n", "spec": "import Mathlib\n\nstructure TopoGraph where\n adj : Array (Array Nat)\n\ndef TopoGraph.well_formed (g : TopoGraph) : Prop :=\n ∀ u, u < g.adj.size →\n ∀ v, v ∈ g.adj.getD u #[] → v < g.adj.size\n\ndef TopoGraph.size (g : TopoGraph) : Nat :=\n g.adj.size\n\ndef TopoGraph.has_edge (g : TopoGraph) (u v : Nat) : Prop :=\n u < g.adj.size ∧\n v ∈ g.adj.getD u #[]\n\ndef TopoGraph.is_path (g : TopoGraph) (p : List Nat) : Prop :=\n p.length > 0 ∧\n ∀ i, i + 1 < p.length →\n g.has_edge (p.getD i 0) (p.getD (i + 1) 0)\n\ndef TopoGraph.is_cycle (g : TopoGraph) (p : List Nat) : Prop :=\n g.is_path p ∧ p.length > 1 ∧ p.head? = p.getLast?\n\ndef TopoGraph.graph_has_cycle (g : TopoGraph) : Prop :=\n ∃ p, g.is_cycle p\n\ndef TopoGraph.is_topological_ordering (g : TopoGraph) (order : Array Nat) : Prop :=\n -- Contains all nodes (size check + surjective)\n order.size = g.size ∧\n (∀ n, n < g.size → ∃ k, k < order.size ∧ order.getD k 0 = n) ∧\n -- No duplicates is implied by size check + surjective on finite set, but for completeness:\n (∀ i j, i < order.size → j < order.size → i ≠ j → order.getD i 0 ≠ order.getD j 0) ∧\n -- No back edges (if j < i, no edge order[i] -> order[j])\n (∀ i j, j < i → i < order.size →\n ¬ g.has_edge (order.getD i 0) (order.getD j 0))\n\n-- Precondition definitions\n@[reducible, simp]\ndef topological_sort_precond (graph : TopoGraph) : Prop :=\n -- !benchmark @start precond\n graph.well_formed\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef topological_sort (graph : TopoGraph)\n (h_precond : topological_sort_precond graph) : Option (Array Nat) :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef topological_sort_postcond (graph : TopoGraph) (result : Option (Array Nat))\n (_ : topological_sort_precond graph) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some order =>\n ¬ graph.graph_has_cycle ∧\n graph.is_topological_ordering order\n | none =>\n graph.graph_has_cycle\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem topological_sort_postcond_satisfied (graph : TopoGraph)\n (h_precond : topological_sort_precond graph) :\n topological_sort_postcond graph (topological_sort graph h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "trial_division_naive", "nl_desc": "Your task is to implement the \"Trial Division (Naive)\" Primality Test and verify its correctness in Lean. Preconditions: The input n is a natural number (Nat). Postconditions: Implement the function `check_prime` which returns true if n is prime and false otherwise. (1) Algorithm: You should use a simple trial division loop (or recursion) that checks every integer from 2 up to n - 1. (2) Functional Correctness: You must verify that your result matches the mathematical definition of primality (a number n > 1 divisible only by 1 and itself). Verification Challenges: (1) Loop Invariant: You must maintain an invariant stating that \"for all k checked so far (2 <= k < i), k does not divide n\". (2) Quantifier Instantiation: When the loop finishes without finding a divisor, you must prove that this implies the universal quantifier forall d. 1 < d < n -> n % d != 0.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef check_prime_precond (n : Nat) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef check_prime (n : Nat) (h_precond : check_prime_precond n) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef check_prime_postcond (n : Nat) (result : Bool) (h_precond : check_prime_precond n) : Prop :=\n -- !benchmark @start postcond\n result = true ↔ (n > 1 ∧ ∀ d : Nat, 1 < d ∧ d < n → n % d ≠ 0)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem check_prime_postcond_satisfied (n : Nat) (h_precond : check_prime_precond n) :\n check_prime_postcond n (check_prime n h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "trial_division_optimized", "nl_desc": "Your task is to implement the \"Optimized Trial Division\" primality test algorithm and verify its functional correctness in Lean. Given a non-negative integer n, determine whether n is prime by dividing it by integers from 2 up to sqrt(n). Postconditions: Returns true iff n > 1 and has no divisors between 2 and n-1. Preconditions: n is a natural number (Nat). Verification Challenges: (1) Algorithm: Stop checking when i * i > n. (2) Proof Requirement: You must prove that if no divisor is found up to sqrt(n), then no divisor exists up to n-1. This requires stating and proving the number-theoretic lemma: if n is composite, it has a factor less than or equal to its square root.", "spec": "import Mathlib\n\n-- Precondition definitions\n@[reducible, simp]\ndef check_prime_precond (n : Nat) : Prop :=\n -- !benchmark @start precond\n True\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definitions\ndef check_prime (n : Nat) (h_precond : check_prime_precond n) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef check_prime_postcond (n : Nat) (result : Bool) (h_precond : check_prime_precond n) : Prop :=\n -- !benchmark @start postcond\n result = true ↔ (n > 1 ∧ ∀ d : Nat, 1 < d ∧ d < n → n % d ≠ 0)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem check_prime_postcond_satisfied (n : Nat) (h_precond : check_prime_precond n) :\n check_prime_postcond n (check_prime n h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "trie_delete", "nl_desc": "Your task is to implement the Delete operation for a Byte-based Trie data structure (Prefix Tree) and verify its correctness in Lean. Postconditions: Functional Correctness (view(result) = view(tree) - {key}) and Invariant Preservation (result satisfies well-formedness and is not \"empty\"/useless). This requires \"Eager Pruning\": if deletion leaves a node with no children and not an endpoint, it must be removed.\n", "spec": "import Mathlib\n\ninductive Node\n| mk (is_end : Bool) (children : List (Option Node))\nderiving Inhabited\n\nnamespace Node\n\ndef is_end (t : Node) : Bool :=\n match t with\n | Node.mk b _ => b\n\ndef children (t : Node) : List (Option Node) :=\n match t with\n | Node.mk _ c => c\n\nend Node\n\n-- Helper for enum behavior\ndef enumerate {α} (l : List α) : List (Nat × α) :=\n (List.range l.length).zip l\n\ndef join {α} (l : List (List α)) : List α :=\n l.foldr List.append []\n\ndef flatMap {α β} (l : List α) (f : α → List β) : List β :=\n join (l.map f)\n\n-- Using Set theory directly\ndef view (t : Node) : Set (List Int) :=\n let current : Set (List Int) := if t.is_end then {[]} else ∅\n let child_views : Set (List Int) :=\n let indexed_children := enumerate t.children\n indexed_children.foldr (fun (idx_child : Nat × Option Node) acc =>\n match idx_child.2 with\n | some child =>\n let suffix_set := view child\n let prefixed_set := suffix_set.image (fun suffix => (idx_child.1 : Int) :: suffix)\n acc ∪ prefixed_set\n | none => acc\n ) ∅\n current ∪ child_views\ntermination_by sizeOf t\ndecreasing_by sorry\n\ndef is_empty_node (t : Node) : Bool :=\n !t.is_end ∧ t.children.all Option.isNone\n\n-- Dafny uses an array of size 256. Here we model it as a list of 256 Option Node.\ndef well_formed (t : Node) : Prop :=\n match t with\n | Node.mk _ children =>\n children.length = 256 ∧\n ∀ c ∈ children, match c with\n | some child => \n well_formed child ∧ \n !is_empty_node child -- CRITICAL: Match Dafny/Verus Pruning requirement\n | none => True\ntermination_by sizeOf t\ndecreasing_by sorry\n\ndef is_valid_key (key : List Int) : Prop :=\n ∀ k ∈ key, 0 ≤ k ∧ k < 256\n\n-- Precondition definitions\n@[reducible, simp]\ndef delete_precond (t : Option Node) (key : List Int) : Prop :=\n -- !benchmark @start precond\n (match t with\n | some node => well_formed node ∧ !is_empty_node node\n | none => True) ∧\n is_valid_key key -- Match Dafny/Verus input constraint\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef delete (t : Option Node) (key : List Int)\n (h_precond : delete_precond t key) : Option Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- View wrapper for Option Node\ndef view_opt (t : Option Node) : Set (List Int) :=\n match t with\n | some node => view node\n | none => ∅\n\n-- Postcondition definitions\n@[reducible, simp]\ndef delete_postcond (t : Option Node) (key : List Int) (result : Option Node)\n (_ : delete_precond t key) : Prop :=\n -- !benchmark @start postcond\n match result with\n | some res => well_formed res ∧ !is_empty_node res\n | none => True\n ∧\n view_opt result = view_opt t \\ {key}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem delete_postcond_satisfied (t : Option Node) (key : List Int)\n (h_precond : delete_precond t key) :\n delete_postcond t key (delete t key h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "trie_insert", "nl_desc": "Your task is to implement the Insert operation for a Byte-based Trie data structure (Prefix Tree) and verify its correctness in Lean. The operation takes a tree (Option Node) and a key (List Int) and returns a new Node. Postconditions: Functional Correctness (view(result) = view(tree) + {key}), Well-Formedness (result is well-formed), and Non-Empty (result is not useless/empty). If the path does not exist, new nodes are allocated with 256 None children.\n", "spec": "import Mathlib\n\ninductive Node\n| mk (is_end : Bool) (children : List (Option Node))\nderiving Inhabited\n\nnamespace Node\n\ndef is_end (t : Node) : Bool :=\n match t with\n | Node.mk b _ => b\n\ndef children (t : Node) : List (Option Node) :=\n match t with\n | Node.mk _ c => c\n\nend Node\n\n-- Helper for enum behavior\ndef enumerate {α} (l : List α) : List (Nat × α) :=\n (List.range l.length).zip l\n\ndef join {α} (l : List (List α)) : List α :=\n l.foldr List.append []\n\ndef flatMap {α β} (l : List α) (f : α → List β) : List β :=\n join (l.map f)\n\n-- Using Set theory directly\ndef view (t : Node) : Set (List Int) :=\n let current : Set (List Int) := if t.is_end then {[]} else ∅\n let child_views : Set (List Int) :=\n let indexed_children := enumerate t.children\n indexed_children.foldr (fun (idx_child : Nat × Option Node) acc =>\n match idx_child.2 with\n | some child =>\n let suffix_set := view child\n let prefixed_set := suffix_set.image (fun suffix => (idx_child.1 : Int) :: suffix)\n acc ∪ prefixed_set\n | none => acc\n ) ∅\n current ∪ child_views\ntermination_by sizeOf t\ndecreasing_by sorry\n\ndef is_empty_node (t : Node) : Bool :=\n !t.is_end ∧ t.children.all Option.isNone\n\ndef is_valid_key (key : List Int) : Prop :=\n ∀ k ∈ key, 0 ≤ k ∧ k < 256\n\n-- Dafny uses an array of size 256. Here we model it as a list of 256 Option Node.\ndef well_formed (t : Node) : Prop :=\n match t with\n | Node.mk _ children =>\n children.length = 256 ∧\n ∀ c ∈ children, match c with\n | some child => \n well_formed child ∧ \n !is_empty_node child -- CRITICAL: Match Dafny's pruning invariant\n | none => True\ntermination_by sizeOf t\ndecreasing_by sorry\n\n-- Precondition definitions\n@[reducible, simp]\ndef insert_precond (t : Option Node) (key : List Int) : Prop :=\n -- !benchmark @start precond\n (match t with\n | some node => well_formed node ∧ !is_empty_node node\n | none => True) ∧\n is_valid_key key -- Added to match Dafny requirement\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef insert (t : Option Node) (key : List Int)\n (h_precond : insert_precond t key) : Node :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- View wrapper for Option Node\ndef view_opt (t : Option Node) : Set (List Int) :=\n match t with\n | some node => view node\n | none => ∅\n\n-- Postcondition definitions\n@[reducible, simp]\ndef insert_postcond (t : Option Node) (key : List Int) (result : Node)\n (_ : insert_precond t key) : Prop :=\n -- !benchmark @start postcond\n well_formed result ∧\n !is_empty_node result ∧\n view result = view_opt t ∪ {key}\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem insert_postcond_satisfied (t : Option Node) (key : List Int)\n (h_precond : insert_precond t key) :\n insert_postcond t key (insert t key h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "trie_search", "nl_desc": "Your task is to implement the Search operation for a Byte-based Trie data structure (Prefix Tree) and verify its correctness in Lean. The Trie represents keys as lists of bytes (integers 0-255). Postconditions: Functional Correctness (returns true iff key exists in the tree's view). Preconditions: Operation assumes the input tree satisfies well-formedness invariants (children array size 256, no useless nodes).\n", "spec": "import Mathlib\n\ninductive Node\n| mk (is_end : Bool) (children : List (Option Node))\nderiving Inhabited\n\nnamespace Node\n\ndef is_end (t : Node) : Bool :=\n match t with\n | Node.mk b _ => b\n\ndef children (t : Node) : List (Option Node) :=\n match t with\n | Node.mk _ c => c\n\nend Node\n\n-- Helper for enum behavior\ndef enumerate {α} (l : List α) : List (Nat × α) :=\n (List.range l.length).zip l\n\n-- Using Set theory directly\ndef view (t : Node) : Set (List Int) :=\n let current : Set (List Int) := if t.is_end then {[]} else ∅\n let child_views : Set (List Int) :=\n let indexed_children := enumerate t.children\n indexed_children.foldr (fun (idx_child : Nat × Option Node) acc =>\n match idx_child.2 with\n | some child =>\n let suffix_set := view child\n let prefixed_set := suffix_set.image (fun suffix => (idx_child.1 : Int) :: suffix)\n acc ∪ prefixed_set\n | none => acc\n ) ∅\n current ∪ child_views\ntermination_by sizeOf t\ndecreasing_by sorry\n\n-- Safe list access helper\ndef list_get_opt {α} (l : List α) (n : Nat) : Option α :=\n if h : n < l.length then some (l.get ⟨n, h⟩) else none\n\ndef contains (t : Node) (key : List Int) : Bool :=\n match key with\n | [] => t.is_end\n | c :: cs =>\n let children := t.children\n if c < children.length ∧ c ≥ 0 then\n match list_get_opt children c.toNat with\n | some (some child) => contains child cs\n | _ => false\n else\n false\n\ndef is_empty_node (t : Node) : Bool :=\n !t.is_end ∧ t.children.all Option.isNone\n\ndef is_valid_key (key : List Int) : Prop :=\n ∀ k ∈ key, 0 ≤ k ∧ k < 256\n\ndef is_valid_key (key : List Int) : Prop :=\n ∀ k ∈ key, 0 ≤ k ∧ k < 256\n\n@[reducible, simp]\ndef insert_precond (t : Option Node) (key : List Int) : Prop :=\n -- !benchmark @start precond\n (match t with\n | some node => well_formed node ∧ !is_empty_node node\n | none => True) ∧\n is_valid_key key -- Added to match Dafny requirement\n -- !benchmark @end precond\n\n-- Precondition definitions\n@[reducible, simp]\ndef search_precond (t : Option Node) (key : List Int) : Prop :=\n -- !benchmark @start precond\n (match t with\n | some node => well_formed node ∧ !is_empty_node node\n | none => True) ∧\n is_valid_key key -- Added to match Dafny\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef search (t : Option Node) (key : List Int)\n (h_precond : search_precond t key) : Bool :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- View wrapper for Option Node\ndef view_opt (t : Option Node) : Set (List Int) :=\n match t with\n | some node => view node\n | none => ∅\n\n-- Postcondition definitions\n@[reducible, simp]\ndef search_postcond (t : Option Node) (key : List Int) (result : Bool)\n (_ : search_precond t key) : Prop :=\n -- !benchmark @start postcond\n result = (key ∈ view_opt t)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem search_postcond_satisfied (t : Option Node) (key : List Int)\n (h_precond : search_precond t key) :\n search_postcond t key (search t key h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n"}
{"problem_id": "unionfind_find", "nl_desc": "Your task is to implement the Find operation for a Disjoint Set Union (Union-Find) data structure and verify its correctness in Lean. The find operation returns the representative root of the set containing element x and performs path compression. Postconditions: Returns the root of x, valid index, root is a root (parent[root] = root), and path compression (future lookups strictly faster/closer to root).\n", "spec": "import Mathlib\n\nstructure UnionFind where\n parent : List Nat\n rank : List Nat -- Added to match Dafny/Verus\nderiving Inhabited\n\nnamespace UnionFind\n\ndef len (uf : UnionFind) : Nat := uf.parent.length\n\ndef is_valid (uf : UnionFind) : Prop :=\n uf.parent.length = uf.rank.length ∧\n ∀ i, i < uf.len →\n let p := uf.parent.getD i 0\n p < uf.len ∧\n uf.rank.getD i 0 < uf.len ∧\n -- CRITICAL: Rank monotonicity enforces acyclicity (DAG structure)\n (p ≠ i → uf.rank.getD i 0 < uf.rank.getD p 0)\n\n-- Measure for termination proof, matching Dafny\ndef measure (uf : UnionFind) (i : Nat) : Nat :=\n if h : i < uf.len then\n uf.len - uf.rank.getD i 0\n else 0\n\n-- The Abstract Model (\"Ghost Spec\")\ndef spec_find (uf : UnionFind) (i : Nat) : Nat :=\n if h : i < uf.len ∧ uf.rank.getD i 0 < uf.len then\n let p := uf.parent.getD i 0\n -- If not root and parent has higher rank (valid edge up)\n if p ≠ i ∧ p < uf.len ∧ uf.rank.getD i 0 < uf.rank.getD p 0 then\n spec_find uf p\n else\n i\n else\n i\ntermination_by measure uf i\ndecreasing_by sorry -- Dafny/Verus solve this automatically; Lean needs 'sorry' or manual proof\n\n-- Precondition definitions\n@[reducible, simp]\ndef find_precond (uf : UnionFind) (i : Nat) : Prop :=\n -- !benchmark @start precond\n uf.is_valid ∧ i < uf.len\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef find (uf : UnionFind) (i : Nat)\n (h_precond : find_precond uf i) : Nat × UnionFind :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef find_postcond (uf : UnionFind) (i : Nat) (result : Nat × UnionFind)\n (pre : find_precond uf i) : Prop :=\n -- !benchmark @start postcond\n let (root, new_uf) := result\n -- 1. Validity preservation\n new_uf.is_valid ∧\n -- 2. Functional Correctness: Returns the specific representative\n root = spec_find uf i ∧\n -- 3. Logical Stability: Path compression doesn't change sets\n (∀ j, j < uf.len → spec_find new_uf j = spec_find uf j) ∧\n -- 4. Structure: Length and Ranks preserved\n new_uf.len = uf.len ∧\n new_uf.rank = uf.rank\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem find_postcond_satisfied (uf : UnionFind) (i : Nat)\n (h_precond : find_precond uf i) :\n find_postcond uf i (find uf i h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n\nend UnionFind"}
{"problem_id": "unionfind_linkroots", "nl_desc": "Your task is to implement the link_roots operation for a Union-Find data structure and verify its correctness in Lean. This operation merges two sets given their roots, using union-by-rank. Postconditions: The two roots now have the same representative, rank invariant is preserved, and other disjoint sets are unaffected.\n", "spec": "import Mathlib\n\nstructure UnionFind where\n parent : List Nat\n rank : List Nat\n len : Nat\nderiving Inhabited\n\nnamespace UnionFind\n\n-- 1. Correct Invariant (Matches Dafny/Verus)\n-- Must enforce rank monotonicity to prevent cycles.\ndef is_valid (uf : UnionFind) : Prop :=\n uf.parent.length = uf.len ∧\n uf.rank.length = uf.len ∧\n ∀ i, i < uf.len →\n let p := uf.parent.getD i 0\n p < uf.len ∧\n uf.rank.getD i 0 < uf.len ∧\n -- CRITICAL: Child rank must be strictly less than Parent rank (unless root)\n (p ≠ i → uf.rank.getD i 0 < uf.rank.getD p 0)\n\n-- Measure for termination (Matches Dafny 'measure')\ndef measure (uf : UnionFind) (i : Nat) : Nat :=\n if h : i < uf.len then\n uf.len - uf.rank.getD i 0\n else 0\n\n-- 2. Concrete Ghost Function (Matches Dafny 'spec_find')\n-- We need this to define functional correctness.\ndef spec_find (uf : UnionFind) (i : Nat) : Nat :=\n if h : i < uf.len ∧ uf.rank.getD i 0 < uf.len then\n let p := uf.parent.getD i 0\n -- If valid parent exists with higher rank...\n if p ≠ i ∧ p < uf.len ∧ uf.rank.getD i 0 < uf.rank.getD p 0 then\n spec_find uf p\n else\n i\n else\n i\ntermination_by measure uf i\ndecreasing_by sorry\n\n-- Precondition definitions\n@[reducible, simp]\ndef link_roots_precond (uf : UnionFind) (root_i root_j : Nat) : Prop :=\n -- !benchmark @start precond\n uf.is_valid ∧\n root_i < uf.len ∧\n root_j < uf.len ∧\n uf.parent.getD root_i 0 = root_i ∧ -- Input i is a root\n uf.parent.getD root_j 0 = root_j -- Input j is a root\n -- !benchmark @end precond\n\n-- !benchmark @start auxcode\n-- !benchmark @end auxcode\n\n-- Main function definition\ndef link_roots (uf : UnionFind) (root_i root_j : Nat)\n (h_precond : link_roots_precond uf root_i root_j) : UnionFind :=\n -- !benchmark @start code\n sorry\n -- !benchmark @end code\n\n-- Postcondition definitions\n@[reducible, simp]\ndef link_roots_postcond (uf : UnionFind) (root_i root_j : Nat) (result : UnionFind)\n (_ : link_roots_precond uf root_i root_j) : Prop :=\n -- !benchmark @start postcond\n result.is_valid ∧\n result.len = uf.len ∧\n -- Functional Correctness: The two trees are merged\n spec_find result root_i = spec_find result root_j ∧\n -- Frame Condition: Other roots must NOT change.\n -- (Matches Dafny: forall k ... old(parent)[k] == k ==> parent[k] == k)\n (∀ k, k < uf.len ∧ k ≠ root_i ∧ k ≠ root_j →\n uf.parent.getD k 0 = k → result.parent.getD k 0 = k)\n -- !benchmark @end postcond\n\n-- !benchmark @start lemma\n-- !benchmark @end lemma\n\n-- Proof content\ntheorem link_roots_postcond_satisfied (uf : UnionFind) (root_i root_j : Nat)\n (h_precond : link_roots_precond uf root_i root_j) :\n link_roots_postcond uf root_i root_j (link_roots uf root_i root_j h_precond) h_precond := by\n -- !benchmark @start proof\n sorry\n -- !benchmark @end proof\n\nend UnionFind"}