Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
8be8d07
Big PR
Shreyas4991 Feb 26, 2026
e50c8b0
Fixed worst case statement
Shreyas4991 Feb 26, 2026
79d77de
Linarith
Shreyas4991 Feb 26, 2026
c1e3323
More review fixes
Shreyas4991 Feb 26, 2026
ddab6f0
More review fixes
Shreyas4991 Feb 26, 2026
08decfa
More review fixes
Shreyas4991 Feb 26, 2026
a2b4782
More review fixes
Shreyas4991 Feb 26, 2026
54bb351
More review fixes
Shreyas4991 Feb 26, 2026
7ee16a0
More review fixes
Shreyas4991 Feb 26, 2026
cc806f0
More review fixes
Shreyas4991 Feb 26, 2026
a732ed8
Fix test file imports
Shreyas4991 Feb 26, 2026
2cee489
More review fixes
Shreyas4991 Feb 26, 2026
3e7edf2
small golfs
chenson2018 Feb 27, 2026
4789639
Update CslibTests/QueryModel/ProgExamples.lean
Shreyas4991 Feb 27, 2026
afcfd57
Add docstrings for test files
Shreyas4991 Feb 27, 2026
30a7905
Merge branch 'query-final-squash' of github.com:Shreyas4991/cslib int…
Shreyas4991 Feb 27, 2026
4e3d80c
simps in a tutorial example
Shreyas4991 Feb 27, 2026
9f3df4d
Suggested name change. Additionally add co-author list:
Shreyas4991 Feb 27, 2026
6db1fde
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 Feb 28, 2026
a9485da
Fix lake shake issues
Shreyas4991 Feb 28, 2026
6fef51f
Done
Shreyas4991 Feb 28, 2026
f479c93
Switch to bool
Shreyas4991 Mar 2, 2026
5e2a2f6
Lower bound
Shreyas4991 Mar 2, 2026
6b78316
GPT generated lower bound
Shreyas4991 Mar 2, 2026
4fab097
Added module
Shreyas4991 Mar 2, 2026
8097c61
exe mk_all
Shreyas4991 Mar 2, 2026
87e7ded
Minimize imports
Shreyas4991 Mar 2, 2026
3f71048
Done
Shreyas4991 Mar 2, 2026
57856b7
GPT finished the proof for lists with nodup
Shreyas4991 Mar 2, 2026
53c2ef3
Got it for infinite types as well
Shreyas4991 Mar 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Cslib.lean
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
module -- shake: keep-all

public import Cslib.Algorithms.Lean.MergeSort.MergeSort
public import Cslib.Algorithms.Lean.TimeM
public import Cslib.AlgorithmsTheory.Algorithms.ListInsertionSort
public import Cslib.AlgorithmsTheory.Algorithms.ListLinearSearch
public import Cslib.AlgorithmsTheory.Algorithms.ListOrderedInsert
public import Cslib.AlgorithmsTheory.Algorithms.MergeSort
public import Cslib.AlgorithmsTheory.Lean.MergeSort.MergeSort
public import Cslib.AlgorithmsTheory.Lean.TimeM
public import Cslib.AlgorithmsTheory.LowerBounds.ComparisonSort
public import Cslib.AlgorithmsTheory.Models.ListComparisonSearch
public import Cslib.AlgorithmsTheory.Models.ListComparisonSort
public import Cslib.AlgorithmsTheory.QueryModel
public import Cslib.Computability.Automata.Acceptors.Acceptor
public import Cslib.Computability.Automata.Acceptors.OmegaAcceptor
public import Cslib.Computability.Automata.DA.Basic
Expand Down
93 changes: 93 additions & 0 deletions Cslib/AlgorithmsTheory/Algorithms/ListInsertionSort.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/-
Copyright (c) 2026 Shreyas Srinivas. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Shreyas Srinivas, Eric Wieser
-/
module

public import Cslib.AlgorithmsTheory.Algorithms.ListOrderedInsert
public import Mathlib.Tactic.NormNum

@[expose] public section

/-!
# Insertion sort in a list

In this file we state and prove the correctness and complexity of insertion sort in lists under
the `SortOpsInsertHead` model. This insertionSort evaluates identically to the upstream version of
`List.insertionSort`
--

## Main Definitions

- `insertionSort` : Insertion sort algorithm in the `SortOpsInsertHead` query model

## Main results

- `insertionSort_eval`: `insertionSort` evaluates identically to `List.insertionSort`.
- `insertionSort_permutation` : `insertionSort` outputs a permutation of the input list.
- `insertionSort_sorted` : `insertionSort` outputs a sorted list.
- `insertionSort_complexity` : `insertionSort` takes at most n * (n + 1) comparisons and
(n + 1) * (n + 2) list head-insertions.
-/

namespace Cslib

namespace Algorithms

open Prog

/-- The insertionSort algorithms on lists with the `SortOps` query. -/
def insertionSort (l : List α) : Prog (SortOpsInsertHead α) (List α) :=
match l with
| [] => return []
| x :: xs => do
let rest ← insertionSort xs
insertOrd x rest

@[simp]
theorem insertionSort_eval (l : List α) (le : α → α → Bool) :
(insertionSort l).eval (sortModel le) = l.insertionSort (fun x y => le x y = true) := by
induction l with simp_all [insertionSort]

theorem insertionSort_permutation (l : List α) (le : α → α → Bool) :
((insertionSort l).eval (sortModel le)).Perm l := by
simp [insertionSort_eval, List.perm_insertionSort]

theorem insertionSort_sorted
(l : List α) (le : α → α → Bool)
[Std.Total (fun x y => le x y = true)] [IsTrans α (fun x y => le x y = true)] :
((insertionSort l).eval (sortModel le)).Pairwise (fun x y => le x y = true) := by
simpa using List.pairwise_insertionSort _ _

lemma insertionSort_length (l : List α) (le : α → α → Bool) :
((insertionSort l).eval (sortModel le)).length = l.length := by
simp

lemma insertionSort_time_compares (head : α) (tail : List α) (le : α → α → Bool) :
((insertionSort (head :: tail)).time (sortModel le)).compares =
((insertionSort tail).time (sortModel le)).compares +
((insertOrd head (tail.insertionSort (fun x y => le x y = true))).time
(sortModel le)).compares := by
simp [insertionSort]

lemma insertionSort_time_inserts (head : α) (tail : List α) (le : α → α → Bool) :
((insertionSort (head :: tail)).time (sortModel le)).inserts =
((insertionSort tail).time (sortModel le)).inserts +
((insertOrd head (tail.insertionSort (fun x y => le x y = true))).time
(sortModel le)).inserts := by
simp [insertionSort]

theorem insertionSort_complexity (l : List α) (le : α → α → Bool) :
((insertionSort l).time (sortModel le))
≤ ⟨l.length * (l.length + 1), (l.length + 1) * (l.length + 2)⟩ := by
induction l with
| nil =>
simp [insertionSort]
| cons head tail ih =>
grind [insertOrd_complexity_upper_bound, List.length_insertionSort, SortOpsCost.le_def,
insertionSort_time_compares, insertionSort_time_inserts]

end Algorithms

end Cslib
88 changes: 88 additions & 0 deletions Cslib/AlgorithmsTheory/Algorithms/ListLinearSearch.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/-
Copyright (c) 2026 Shreyas Srinivas. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Shreyas Srinivas, Eric Wieser
-/

module

public import Cslib.AlgorithmsTheory.QueryModel
public import Cslib.AlgorithmsTheory.Models.ListComparisonSearch
public import Batteries.Data.List
public import Mathlib.Algebra.Order.Group.Nat
public import Mathlib.Tactic.Set

@[expose] public section

/-!
# Linear search in a list

In this file we state and prove the correctness and complexity of linear search in lists under
the `ListSearch` model.
--

## Main Definitions

- `listLinearSearch` : Linear search algorithm in the `ListSearch` query model

## Main results

- `listLinearSearch_eval`: `insertOrd` evaluates identically to `List.contains`.
- `listLinearSearchM_time_complexity_upper_bound` : `linearSearch` takes at most `n`
comparison operations
- `listLinearSearchM_time_complexity_lower_bound` : There exist lists on which `linearSearch` needs
`n` comparisons
-/
namespace Cslib

namespace Algorithms

open Prog

open ListSearch in
/-- Linear Search in Lists on top of the `ListSearch` query model. -/
def listLinearSearch (l : List α) (x : α) : Prog (ListSearch α) Bool := do
match l with
| [] => return false
| l :: ls =>
let cmp : Bool ← compare (l :: ls) x
if cmp then
return true
else
listLinearSearch ls x

@[simp, grind =]
lemma listLinearSearch_eval [BEq α] (l : List α) (x : α) :
(listLinearSearch l x).eval ListSearch.natCost = l.contains x := by
fun_induction l.elem x with simp_all [listLinearSearch]

lemma listLinearSearchM_correct_true [BEq α] [LawfulBEq α] (l : List α)
{x : α} (x_mem_l : x ∈ l) : (listLinearSearch l x).eval ListSearch.natCost = true := by
simp [x_mem_l]

lemma listLinearSearchM_correct_false [BEq α] [LawfulBEq α] (l : List α)
{x : α} (x_mem_l : x ∉ l) : (listLinearSearch l x).eval ListSearch.natCost = false := by
simp [x_mem_l]

lemma listLinearSearchM_time_complexity_upper_bound [BEq α] (l : List α) (x : α) :
(listLinearSearch l x).time ListSearch.natCost ≤ l.length := by
fun_induction l.elem x with
| case1 => simp [listLinearSearch]
| case2 => simp_all [listLinearSearch]
| case3 =>
simp [listLinearSearch]
lia

lemma listLinearSearchM_time_complexity_lower_bound [DecidableEq α] [Nontrivial α] (n : ℕ) :
∃ (l : List α) (x : α), l.length = n
∧ (listLinearSearch l x).time ListSearch.natCost = l.length := by
obtain ⟨x, y, hneq⟩ := exists_pair_ne α
use List.replicate n y, x
split_ands
· simp
· induction n <;> simp [listLinearSearch, List.replicate]
grind [ListSearch.natCost_cost, ListSearch.natCost_evalQuery]

end Algorithms

end Cslib
102 changes: 102 additions & 0 deletions Cslib/AlgorithmsTheory/Algorithms/ListOrderedInsert.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/-
Copyright (c) 2026 Shreyas Srinivas. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Shreyas Srinivas, Eric Wieser
-/

module

public import Cslib.AlgorithmsTheory.QueryModel
public import Cslib.AlgorithmsTheory.Models.ListComparisonSort
public import Mathlib.Algebra.Order.Group.Nat
public import Mathlib.Data.Int.ConditionallyCompleteOrder
public import Mathlib.Data.List.Sort
public import Mathlib.Order.ConditionallyCompleteLattice.Basic

@[expose] public section

/-!
# Ordered insertion in a list

In this file we state and prove the correctness and complexity of ordered insertions in lists under
the `SortOps` model. This ordered insert is later used in `insertionSort` mirroring the structure
in upstream libraries for the pure lean code versions of these declarations.

--

## Main Definitions

- `insertOrd` : ordered insert algorithm in the `SortOps` query model

## Main results

- `insertOrd_eval`: `insertOrd` evaluates identically to `List.orderedInsert`.
- `insertOrd_complexity_upper_bound` : Shows that `insertOrd` takes at most `n` comparisons,
and `n + 1` list head-insertion operations.
- `insertOrd_sorted` : Applying `insertOrd` to a sorted list yields a sorted list.
-/

namespace Cslib
namespace Algorithms

open Prog

open SortOpsInsertHead

/--
Performs ordered insertion of `x` into a list `l` in the `SortOps` query model.
If `l` is sorted, then `x` is inserted into `l` such that the resultant list is also sorted.
-/
def insertOrd (x : α) (l : List α) : Prog (SortOpsInsertHead α) (List α) := do
match l with
| [] => insertHead x l
| a :: as =>
if (← cmpLE x a : Bool) then
insertHead x (a :: as)
else
let res ← insertOrd x as
insertHead a res

@[simp]
lemma insertOrd_eval (x : α) (l : List α) (le : α → α → Bool) :
(insertOrd x l).eval (sortModel le) = l.orderedInsert (fun x y => le x y = true) x := by
induction l with
| nil =>
simp [insertOrd, sortModel]
| cons head tail ih =>
by_cases h_head : le x head
· simp [insertOrd, h_head]
· simp [insertOrd, h_head, ih]

-- TODO : to upstream
@[simp]
lemma _root_.List.length_orderedInsert (x : α) (l : List α) [DecidableRel r] :
(l.orderedInsert r x).length = l.length + 1 := by
induction l <;> grind

theorem insertOrd_complexity_upper_bound
(l : List α) (x : α) (le : α → α → Bool) :
(insertOrd x l).time (sortModel le) ≤ ⟨l.length, l.length + 1⟩ := by
induction l with
| nil =>
simp [insertOrd, sortModel]
| cons head tail ih =>
obtain ⟨ih_compares, ih_inserts⟩ := ih
rw [insertOrd]
by_cases h_head : le x head
· simp [h_head]
· simp [h_head]
grind

lemma insertOrd_sorted
(l : List α) (x : α) (le : α → α → Bool)
[Std.Total (fun x y => le x y)]
[IsTrans _ (fun x y => le x y)] :
l.Pairwise (fun x y => le x y)
→ ((insertOrd x l).eval (sortModel le)).Pairwise (fun x y => le x y = true) := by
rw [insertOrd_eval]
exact List.Pairwise.orderedInsert _ _

end Algorithms

end Cslib
Loading