-
Notifications
You must be signed in to change notification settings - Fork 130
feat: query complexity model for algorithms theory #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shreyas4991
wants to merge
49
commits into
leanprover:main
Choose a base branch
from
Shreyas4991:query-final-squash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
8be8d07
Big PR
Shreyas4991 e50c8b0
Fixed worst case statement
Shreyas4991 79d77de
Linarith
Shreyas4991 c1e3323
More review fixes
Shreyas4991 ddab6f0
More review fixes
Shreyas4991 08decfa
More review fixes
Shreyas4991 a2b4782
More review fixes
Shreyas4991 54bb351
More review fixes
Shreyas4991 7ee16a0
More review fixes
Shreyas4991 cc806f0
More review fixes
Shreyas4991 a732ed8
Fix test file imports
Shreyas4991 2cee489
More review fixes
Shreyas4991 3e7edf2
small golfs
chenson2018 4789639
Update CslibTests/QueryModel/ProgExamples.lean
Shreyas4991 afcfd57
Add docstrings for test files
Shreyas4991 30a7905
Merge branch 'query-final-squash' of github.com:Shreyas4991/cslib int…
Shreyas4991 4e3d80c
simps in a tutorial example
Shreyas4991 9f3df4d
Suggested name change. Additionally add co-author list:
Shreyas4991 6db1fde
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 a9485da
Fix lake shake issues
Shreyas4991 6fef51f
Done
Shreyas4991 f479c93
Switch to bool
Shreyas4991 5e2a2f6
Lower bound
Shreyas4991 6b78316
GPT generated lower bound
Shreyas4991 4fab097
Added module
Shreyas4991 8097c61
exe mk_all
Shreyas4991 87e7ded
Minimize imports
Shreyas4991 3f71048
Done
Shreyas4991 57856b7
GPT finished the proof for lists with nodup
Shreyas4991 53c2ef3
Got it for infinite types as well
Shreyas4991 b712d16
remove deicsion tree proofs
Shreyas4991 1eeb2d8
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 7370fc6
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 df70978
Merge branch 'main' of github.com:leanprover/cslib into query-final-s…
Shreyas4991 275d827
Merge upstream main
Shreyas4991 e9cb648
Remove accidental file
Shreyas4991 7f4010e
Initialize clean up of lower bound proof
Shreyas4991 d04ca73
Include the model parametric style lower bound in the lower bound file
Shreyas4991 e7c8bde
Clean up
Shreyas4991 a8e9f3d
Clean up a bit
Shreyas4991 4ca216d
Merge branch 'main' into query-final-squash
Shreyas4991 30cf281
Remove Prop from structure
Shreyas4991 a145ded
docstring
Shreyas4991 6761169
Merge branch 'query-final-squash' of github.com:Shreyas4991/cslib int…
Shreyas4991 c6e1add
Where did prop typed structures sneak in. Purge them
Shreyas4991 e07de01
Where did prop typed structures sneak in. Purge them
Shreyas4991 cf9f1f5
Fix lint
Shreyas4991 56c98a5
Fix documentation for listLinearSearch evaluation
Shreyas4991 1fb24e1
Update ListComparisonSort.lean
Shreyas4991 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
Cslib/AlgorithmsTheory/Algorithms/ListInsertionSort.lean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /- | ||
| 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.Algorithms.ListOrderedInsert | ||
| public import Mathlib | ||
|
|
||
| @[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 `SortOps` model. This insertionSort evaluates identically to the upstream version of | ||
| `List.insertionSort` | ||
| -- | ||
|
|
||
| ## Main Definitions | ||
|
|
||
| - `insertionSort` : Insertion sort algorithm in the `SortOps` 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 (SortOps α) (List α) := | ||
| match l with | ||
| | [] => return [] | ||
| | x :: xs => do | ||
| let rest ← insertionSort xs | ||
| insertOrd x rest | ||
|
|
||
| @[simp] | ||
| theorem insertionSort_eval (l : List α) (le : α → α → Prop) [DecidableRel le] : | ||
| (insertionSort l).eval (sortModel le) = l.insertionSort le := by | ||
| induction l with simp_all [insertionSort] | ||
|
|
||
| theorem insertionSort_permutation (l : List α) (le : α → α → Prop) [DecidableRel le] : | ||
| ((insertionSort l).eval (sortModel le)).Perm l := by | ||
| simp [insertionSort_eval, List.perm_insertionSort] | ||
|
|
||
| theorem insertionSort_sorted | ||
| (l : List α) (le : α → α → Prop) [DecidableRel le] [Std.Total le] [IsTrans α le] : | ||
| ((insertionSort l).eval (sortModel le)).Pairwise le := by | ||
| simpa using List.pairwise_insertionSort _ _ | ||
|
|
||
| lemma insertionSort_length (l : List α) (le : α → α → Prop) [DecidableRel le] : | ||
| ((insertionSort l).eval (sortModel le)).length = l.length := by | ||
| simp | ||
|
|
||
| lemma insertionSort_time_compares (head : α) (tail : List α) (le : α → α → Prop) [DecidableRel le] : | ||
| ((insertionSort (head :: tail)).time (sortModel le)).compares = | ||
| ((insertionSort tail).time (sortModel le)).compares + | ||
| ((insertOrd head (tail.insertionSort le)).time (sortModel le)).compares := by | ||
| simp [insertionSort] | ||
|
|
||
| lemma insertionSort_time_inserts (head : α) (tail : List α) (le : α → α → Prop) [DecidableRel le] : | ||
| ((insertionSort (head :: tail)).time (sortModel le)).inserts = | ||
| ((insertionSort tail).time (sortModel le)).inserts + | ||
| ((insertOrd head (tail.insertionSort le)).time (sortModel le)).inserts := by | ||
| simp [insertionSort] | ||
|
|
||
| theorem insertionSort_complexity (l : List α) (le : α → α → Prop) [DecidableRel le] : | ||
| ((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 => | ||
| have h := insertOrd_complexity_upper_bound (tail.insertionSort le) head le | ||
| simp_all only [List.length_cons, List.length_insertionSort] | ||
| obtain ⟨ih₁,ih₂⟩ := ih | ||
| obtain ⟨h₁,h₂⟩ := h | ||
| refine ⟨?_, ?_⟩ | ||
| · clear h₂ | ||
| rw [insertionSort_time_compares] | ||
| nlinarith [ih₁, h₁] | ||
| · clear h₁ | ||
| rw [insertionSort_time_inserts] | ||
| nlinarith [ih₂, h₂] | ||
|
chenson2018 marked this conversation as resolved.
Outdated
|
||
|
|
||
| end Algorithms | ||
|
|
||
| end Cslib | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /- | ||
| 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 Mathlib | ||
|
|
||
| @[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_all [listLinearSearch] | ||
| grind | ||
|
chenson2018 marked this conversation as resolved.
Outdated
|
||
|
|
||
| -- This statement is wrong | ||
|
eric-wieser marked this conversation as resolved.
Outdated
|
||
| lemma listLinearSearchM_time_complexity_lower_bound [DecidableEq α] [Nonempty α] : | ||
| ∃ l : List α, ∃ x : α, (listLinearSearch l x).time ListSearch.natCost = l.length := by | ||
| inhabit α | ||
| refine ⟨[], default, ?_⟩ | ||
| simp_all [ListSearch.natCost, listLinearSearch] | ||
|
|
||
| end Algorithms | ||
|
|
||
| end Cslib | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /- | ||
| 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 | ||
|
|
||
| @[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 SortOps | ||
|
|
||
| /-- | ||
| 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 (SortOps α) (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 : α → α → Prop) [DecidableRel le] : | ||
| (insertOrd x l).eval (sortModel le) = l.orderedInsert le 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] | ||
|
|
||
| -- to upstream | ||
|
chenson2018 marked this conversation as resolved.
Outdated
|
||
| @[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 : α → α → Prop) [DecidableRel le] : | ||
| (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 : α → α → Prop) [DecidableRel le] [Std.Total le] [IsTrans _ le] : | ||
|
chenson2018 marked this conversation as resolved.
Outdated
|
||
| l.Pairwise le → ((insertOrd x l).eval (sortModel le)).Pairwise le := by | ||
| rw [insertOrd_eval] | ||
| exact List.Pairwise.orderedInsert _ _ | ||
|
|
||
| end Algorithms | ||
|
|
||
| end Cslib | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.