Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 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
b712d16
remove deicsion tree proofs
Shreyas4991 Mar 3, 2026
1eeb2d8
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 Mar 8, 2026
7370fc6
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 Mar 11, 2026
df70978
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 Mar 16, 2026
275d827
Merge upstream main
Shreyas4991 Mar 16, 2026
e9cb648
Remove accidental file
Shreyas4991 Mar 17, 2026
7f4010e
Initialize clean up of lower bound proof
Shreyas4991 Mar 17, 2026
d04ca73
Include the model parametric style lower bound in the lower bound file
Shreyas4991 Mar 17, 2026
e7c8bde
Clean up
Shreyas4991 Mar 17, 2026
a8e9f3d
Clean up a bit
Shreyas4991 Mar 17, 2026
4ca216d
Merge branch 'main' into query-final-squash
Shreyas4991 Mar 17, 2026
30cf281
Remove Prop from structure
Shreyas4991 Mar 17, 2026
a145ded
docstring
Shreyas4991 Mar 17, 2026
6761169
Merge branch 'query-final-squash' of github.com:Shreyas4991/cslib int…
Shreyas4991 Mar 17, 2026
c6e1add
Where did prop typed structures sneak in. Purge them
Shreyas4991 Mar 17, 2026
e07de01
Where did prop typed structures sneak in. Purge them
Shreyas4991 Mar 17, 2026
cf9f1f5
Fix lint
Shreyas4991 Mar 18, 2026
56c98a5
Fix documentation for listLinearSearch evaluation
Shreyas4991 Mar 19, 2026
1fb24e1
Update ListComparisonSort.lean
Shreyas4991 Apr 16, 2026
3fc5afd
Merge #372 (query complexity, attempt 1) for attribution
kim-em Apr 22, 2026
7ece1dd
feat(Query): query complexity framework with sorting lower bound
kim-em Mar 5, 2026
883edfd
feat(Query): add Prog.cost and complex multiplication example
kim-em Mar 5, 2026
7327006
docs(Query/Arith): clarify these are toy examples of parametrized costs
kim-em Mar 5, 2026
de06cdf
refactor(Query): replace Prog with FreeM directly
kim-em Apr 28, 2026
b46a156
refactor(Query): replace QueryTree with generalised FreeM lower bound
kim-em Apr 28, 2026
3bf2acb
docs(Query/Arith): fix off-by-strictness in Gauss/naive crossover com…
kim-em Apr 28, 2026
3b93bad
refactor(Query/FreeM): route eval, cost, and queriesOn through FreeM.…
kim-em Apr 28, 2026
2dc7d9f
docs(Query/FreeM): add field docstrings to Tally to satisfy docBlame …
kim-em Apr 28, 2026
391cab0
drop Tally
eric-wieser Apr 29, 2026
6fdf909
refactor(Query): address review feedback
kim-em Aug 27, 2026
c3067db
Merge remote-tracking branch 'origin/main' into combined-query-comple…
kim-em Aug 27, 2026
40ea780
fix(Query): adapt to Lean 4.34
kim-em Aug 27, 2026
79c29f8
docs(Query): update FreeM simp description
kim-em Aug 27, 2026
f257485
refactor(Query): polish docs and merge sort
kim-em Aug 27, 2026
1cc3324
refactor(Query): apply second-opinion review
kim-em Aug 27, 2026
36e098c
refactor(Query): apply review suggestions
kim-em Sep 1, 2026
2a83ed9
refactor(Query): make FreeM query interpreters universe polymorphic
kim-em Sep 1, 2026
a2e9050
chore: golf proofs
eric-wieser Sep 1, 2026
8727846
refactor: simplify InfinitePermOrder
eric-wieser Sep 1, 2026
57b1c35
refactor(Query): make mergeSort agree with List.mergeSort
kim-em Sep 2, 2026
46d7d92
refactor: `Infinite` is a distraction in most of the proof
eric-wieser Sep 2, 2026
693c2c5
Merge branch 'combined-query-complexity' of github.com:kim-em/cslib i…
eric-wieser Sep 2, 2026
cd51b93
feat(Query): UpperBound.of_pointwise and LowerBound.le_upperBound
kim-em Sep 2, 2026
901c0ce
docs(Query): FreeM setup recipe and countQueries design note
kim-em Sep 2, 2026
877cf1a
feat(Query): tighten insertion sort bound to n * (n - 1) / 2
kim-em Sep 2, 2026
239b7ad
refactor(Query): merge sort count lemma in simp normal form
kim-em Sep 2, 2026
398c22b
feat(Query): IsSort.eval_eq uniqueness lemma
kim-em Sep 2, 2026
8547036
chore(Query): mark declarations proposed upstream to Mathlib private
kim-em Sep 2, 2026
09c565a
feat(Query): combined bounds for mergeSort
kim-em Sep 2, 2026
6a904b2
test(Query): executable and API checks for the query framework
kim-em Sep 2, 2026
aee6621
refactor(Query): mirror upstream List.mergeSort_append
kim-em Sep 2, 2026
dad82a1
chore(Query): drop unused imports
kim-em Sep 2, 2026
0bd0e2a
refactor(Query): single cardinal bound in the lower-bound theorem
kim-em Sep 2, 2026
b5e94ca
Add monad-generic versions first
eric-wieser Sep 2, 2026
f6d8269
test(Query): naturality of the monad-generic sorts
kim-em Sep 5, 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: 12 additions & 0 deletions Cslib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ module -- shake: keep-all --deprecated_module: ignore

public import Cslib.Algorithms.CCS.VendingMachine
public import Cslib.Algorithms.Lean.MergeSort.MergeSort
public import Cslib.Algorithms.Lean.Query.Arith.Defs
public import Cslib.Algorithms.Lean.Query.Arith.Lemmas
public import Cslib.Algorithms.Lean.Query.Bounds
public import Cslib.Algorithms.Lean.Query.FreeM
public import Cslib.Algorithms.Lean.Query.Sort.Insertion.Defs
public import Cslib.Algorithms.Lean.Query.Sort.Insertion.Lemmas
public import Cslib.Algorithms.Lean.Query.Sort.IsSort
public import Cslib.Algorithms.Lean.Query.Sort.LEQuery
public import Cslib.Algorithms.Lean.Query.Sort.LowerBound
public import Cslib.Algorithms.Lean.Query.Sort.Merge.Bounds
public import Cslib.Algorithms.Lean.Query.Sort.Merge.Defs
public import Cslib.Algorithms.Lean.Query.Sort.Merge.Lemmas
public import Cslib.Algorithms.Lean.TimeM
public import Cslib.Computability.Automata.Acceptors.Acceptor
public import Cslib.Computability.Automata.Acceptors.OmegaAcceptor
Expand Down
78 changes: 78 additions & 0 deletions Cslib/Algorithms/Lean/Query/Arith/Defs.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/-
Copyright (c) 2026 Lean FRO, LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kim Morrison
-/
module

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the Arith Prog from my CslibTests files?

public import Cslib.Algorithms.Lean.Query.FreeM

/-! # Arithmetic Queries and Complex Multiplication

A simple example showing how to use `FreeM.cost` with variable/parametrized query costs.

`ArithQuery α` supports addition, subtraction, and multiplication, each with
independently parametrized costs. Complex number multiplication provides a toy example
where two algorithms (naive and Gauss's trick) trade multiplications for additions,
and the optimal choice depends on the cost ratio.
-/

public section

namespace Cslib.Query

/-- Arithmetic queries: addition, subtraction, and multiplication. -/
inductive ArithQuery (α : Type) : Type → Type where

@Shreyas4991 Shreyas4991 Mar 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want a zero and one for completeness. Also if it is just an illustrative example, it belongs in the tests folder (see my PR).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a 0 and 1 are useful here, it would be strange to allocate them a cost.

| add (a b : α) : ArithQuery α α
| sub (a b : α) : ArithQuery α α
| mul (a b : α) : ArithQuery α α

namespace ArithQuery

/-- Lift `ArithQuery.add a b` into a `FreeM` that returns the sum. -/
abbrev doAdd (a b : α) : FreeM (ArithQuery α) α := FreeM.lift (.add a b)
/-- Lift `ArithQuery.sub a b` into a `FreeM` that returns the difference. -/
abbrev doSub (a b : α) : FreeM (ArithQuery α) α := FreeM.lift (.sub a b)
/-- Lift `ArithQuery.mul a b` into a `FreeM` that returns the product. -/
abbrev doMul (a b : α) : FreeM (ArithQuery α) α := FreeM.lift (.mul a b)

/-- An honest oracle interprets arithmetic queries using the actual ring operations. -/
@[expose] def honest [Add α] [Sub α] [Mul α] {ι : Type} : ArithQuery α ι → ι
| .add a b => a + b
| .sub a b => a - b
| .mul a b => a * b

/-- Weighted cost model for arithmetic queries. Subtraction costs the same as addition
(both are linear-time on bignums). -/
@[expose] def weight (c_add c_mul : Nat) {ι : Type} : ArithQuery α ι → Nat
| .add _ _ => c_add
| .sub _ _ => c_add
| .mul _ _ => c_mul

end ArithQuery

/-- Naive complex multiplication: `(a + bi)(c + di) = (ac - bd) + (ad + bc)i`.
Uses 4 multiplications, 1 subtraction, 1 addition. -/
@[expose] def complexMulNaive (a b c d : α) : FreeM (ArithQuery α) (α × α) := do
let ac ← ArithQuery.doMul a c
let bd ← ArithQuery.doMul b d
let ad ← ArithQuery.doMul a d
let bc ← ArithQuery.doMul b c
let real ← ArithQuery.doSub ac bd
let imag ← ArithQuery.doAdd ad bc
return (real, imag)

/-- Gauss's trick for complex multiplication: computes `(a+b)(c+d)` to save one
multiplication, at the cost of extra additions and subtractions.
Uses 3 multiplications, 2 subtractions, 3 additions. -/
@[expose] def complexMulGauss (a b c d : α) : FreeM (ArithQuery α) (α × α) := do
let ac ← ArithQuery.doMul a c
let bd ← ArithQuery.doMul b d
let apb ← ArithQuery.doAdd a b
let cpd ← ArithQuery.doAdd c d
let abcd ← ArithQuery.doMul apb cpd
let real ← ArithQuery.doSub ac bd
let imag ← ArithQuery.doSub abcd (← ArithQuery.doAdd ac bd)
return (real, imag)

end Cslib.Query
75 changes: 75 additions & 0 deletions Cslib/Algorithms/Lean/Query/Arith/Lemmas.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/-
Copyright (c) 2026 Lean FRO, LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kim Morrison
-/
module

public import Cslib.Algorithms.Lean.Query.Arith.Defs
import Mathlib.Tactic.Ring
public import Mathlib.Algebra.Ring.Defs

/-! # Complex Multiplication: Correctness and Cost Analysis

A simple example showing how to use `FreeM.cost` with variable/parametrized query costs.

We prove that both `complexMulNaive` and `complexMulGauss` correctly compute
complex multiplication when given an honest oracle, and compute their exact
costs under a parametric weight function. The cost theorems hold for *any* oracle
(not just honest ones), because both algorithms are straight-line (no branching
on query results).
-/

open Cslib.Query

public section

namespace Cslib.Query

variable {α : Type}

/-! ## Correctness -/

theorem complexMulNaive_eval_honest [Add α] [Sub α] [Mul α] (a b c d : α) :
(complexMulNaive a b c d).eval ArithQuery.honest = (a * c - b * d, a * d + b * c) := by
simp [complexMulNaive, ArithQuery.doMul, ArithQuery.doSub, ArithQuery.doAdd, ArithQuery.honest]

theorem complexMulGauss_eval_honest [CommRing α] (a b c d : α) :
(complexMulGauss a b c d).eval ArithQuery.honest = (a * c - b * d, a * d + b * c) := by
simp [complexMulGauss, ArithQuery.doMul, ArithQuery.doSub, ArithQuery.doAdd, ArithQuery.honest]
ring

/-! ## Exact cost counts -/

theorem complexMulNaive_cost (oracle : {ι : Type} → ArithQuery α ι → ι)
(c_add c_mul : Nat) (a b c d : α) :
(complexMulNaive a b c d).cost oracle (ArithQuery.weight c_add c_mul) =
4 * c_mul + 2 * c_add := by
simp [complexMulNaive, ArithQuery.doMul, ArithQuery.doSub, ArithQuery.doAdd, ArithQuery.weight]
omega

theorem complexMulGauss_cost (oracle : {ι : Type} → ArithQuery α ι → ι)
(c_add c_mul : Nat) (a b c d : α) :
(complexMulGauss a b c d).cost oracle (ArithQuery.weight c_add c_mul) =
3 * c_mul + 5 * c_add := by
simp [complexMulGauss, ArithQuery.doMul, ArithQuery.doSub, ArithQuery.doAdd, ArithQuery.weight]
omega

/-! ## Crossover: Gauss beats naive when multiplication costs at least 3× addition -/

theorem gauss_le_naive (oracle : {ι : Type} → ArithQuery α ι → ι)
(c_add c_mul : Nat) (a b c d : α) (h : 3 * c_add ≤ c_mul) :
(complexMulGauss a b c d).cost oracle (ArithQuery.weight c_add c_mul) ≤
(complexMulNaive a b c d).cost oracle (ArithQuery.weight c_add c_mul) := by
rw [complexMulGauss_cost, complexMulNaive_cost]
omega

theorem gauss_le_naive_iff (oracle : {ι : Type} → ArithQuery α ι → ι)
(c_add c_mul : Nat) (a b c d : α) :
(complexMulGauss a b c d).cost oracle (ArithQuery.weight c_add c_mul) ≤
(complexMulNaive a b c d).cost oracle (ArithQuery.weight c_add c_mul) ↔
3 * c_add ≤ c_mul := by
rw [complexMulGauss_cost, complexMulNaive_cost]
omega

end Cslib.Query
54 changes: 54 additions & 0 deletions Cslib/Algorithms/Lean/Query/Bounds.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/-
Copyright (c) 2026 Lean FRO, LLC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Graf, Kim Morrison, Shreyas Srinivas
-/
module

public import Cslib.Algorithms.Lean.Query.FreeM
public import Mathlib.Order.Monotone.Defs

/-! # Upper and Lower Bounds for Query Complexity

Definitions of upper and lower bounds on the number of queries a program makes,
quantified over oracles.
-/

public section

namespace Cslib.Query

universe u v w

variable {α : Type w} {Q : Type u → Type v} {β : Type u}

/-- Upper bound: for all oracles, inputs of size ≤ n make at most `bound n` queries. -/
@[expose] def UpperBound (prog : α → FreeM Q β)
(size : α → Nat) (bound : Nat → Nat) : Prop :=
∀ (oracle : {ι : Type u} → Q ι → ι) (n : Nat) (x : α),
size x ≤ n → (prog x).countQueries oracle ≤ bound n

/-- Lower bound: for every size n, there exists an input of size at most n and an oracle
making the program perform ≥ `bound n` queries. -/
@[expose] def LowerBound (prog : α → FreeM Q β)
(size : α → Nat) (bound : Nat → Nat) : Prop :=
∀ (n : Nat), ∃ (x : α), size x ≤ n ∧
∃ (oracle : {ι : Type u} → Q ι → ι), bound n ≤ (prog x).countQueries oracle

/-- To prove an `UpperBound` with a monotone bound function, it suffices to bound the
query count of each input by `bound` at its own size. -/
theorem UpperBound.of_pointwise {prog : α → FreeM Q β} {size : α → Nat} {bound : Nat → Nat}
(hmono : Monotone bound)
(h : ∀ (oracle : {ι : Type u} → Q ι → ι) (x : α),
(prog x).countQueries oracle ≤ bound (size x)) :
UpperBound prog size bound :=
fun oracle _n x hx => (h oracle x).trans (hmono hx)

/-- A lower bound for a program never exceeds an upper bound for the same program and
size function. -/
theorem LowerBound.le_upperBound {prog : α → FreeM Q β} {size : α → Nat} {l u : Nat → Nat}
(hl : LowerBound prog size l) (hu : UpperBound prog size u) (n : Nat) : l n ≤ u n := by
obtain ⟨x, hx, oracle, hbound⟩ := hl n
exact hbound.trans (hu oracle n x hx)

end Cslib.Query
Loading