Skip to content

Comments

feat: Adding proofs of correctness and time complexity of insertion sort#280

Open
bhargavkulk wants to merge 9 commits intoleanprover:mainfrom
bhargavkulk:main
Open

feat: Adding proofs of correctness and time complexity of insertion sort#280
bhargavkulk wants to merge 9 commits intoleanprover:mainfrom
bhargavkulk:main

Conversation

@bhargavkulk
Copy link

@bhargavkulk bhargavkulk commented Jan 23, 2026

I have added the file InsertionSort.lean in this PR. I am aware that List.insertionSort exists, but it is not instrumented with tick, which does not make it possible to write the complexity proof. I have chosen the worst case time bound as n^2 but the same proof discharges for n(n+1)/2. I know I have not added the required documentation in this PR, but I first want to get an OK for the actual proofs implemented.

I think insertion sort, while a very simple algorithm, merits its inclusion because more complicated sorts implemented in the standard libraries of programming languages (like "TimSort" in Python) use insertion sort to quickly sort smaller sub-arrays as insertion sort is faster than divide-and-conquer based comparison sorts like merge and quick sort for very small arrays. I believe that the proofs in this file would aid future proofs about these hybrid in-actual-use sorts.

@Shreyas4991
Copy link
Contributor

Shreyas4991 commented Jan 24, 2026

I think this PR should be put on hold until we reach a decision on #275, specifically this question

@chenson2018
Copy link
Collaborator

chenson2018 commented Jan 24, 2026

Hi Bhargav, thanks for interest in contributing! Just to add context to the above comment, I think this sort of PR will be welcome, but there is some infrastructure flux at the moment.

In the meantime could you add the standard copyright header and address the CI failure? Thanks!

@bhargavkulk
Copy link
Author

I have added the copyright header and fixed the CI error.

@fmontesi
Copy link
Collaborator

@sorrachai Can you review this code?

Copy link
Collaborator

@sorrachai sorrachai left a comment

Choose a reason for hiding this comment

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

Please see https://github.com/leanprover/cslib?tab=contributing-ov-file#style-and-documentation for a link to the style documentation. You can follow the style from MergeSort in CSLib, for example.

@bhargavkulk
Copy link
Author

@sorrachai I have added doc comments and edited the code to meet the style guide. Let me know id anything else is missing!

Copy link
Collaborator

@chenson2018 chenson2018 left a comment

Choose a reason for hiding this comment

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

Here are some reviews that should help you in merging in changes from main.

grind

/-- A list is sorted if it satisfies the `Pairwise (· ≤ ·)` predicate. -/
abbrev IsSorted (l : List α) : Prop := List.Pairwise (· ≤ ·) l
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should use List.SortedLE for this.

At minimum you can't have this duplicated because it will cause a failure in importing this alongside the merge sort module. This declaration Cslib.Algorithms.Lean.TimeM.IsSorted should probably never have been namespaced in this way.

Copy link
Contributor

@Shreyas4991 Shreyas4991 Feb 23, 2026

Choose a reason for hiding this comment

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

I think SortedLE is deprecated in favour of Pairwise, so this usage is correct. The abbreviation is not necessary however.

Copy link
Author

Choose a reason for hiding this comment

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

MergeSort.lean also defines the IsSorted abbrev. So, I am not sure if I should inline it.

Copy link
Collaborator

@chenson2018 chenson2018 Feb 23, 2026

Choose a reason for hiding this comment

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

@Shreyas4991 I'm not sure that is correct. The deprecation on List.Sorted does refer to List.Pairwise, but the comment specifically says to consider using SortedLE, which is not deprecated and has sortedLE_iff_pairwise as a grind = lemma. I think it is correct to use SortedLE here. (And in the merge sort file, but let's make a separate PR)

@Shreyas4991
Copy link
Contributor

I suggest merging main. The simp set for TimeM has changed. In fact depending on when main was merged last time, there might be considerable breakage.

@Shreyas4991
Copy link
Contributor

Shreyas4991 commented Feb 23, 2026

Fwiw this PR duplicates List.orderedInsert. See my Prog version of insertionSort in #275.

@bhargavkulk
Copy link
Author

@chenson2018 I have made the requested changes. See my comment on the IsSorted abbrev, I'm not sure if i should inline it; I'll defer to your decision.

@Shreyas4991
Copy link
Contributor

Shreyas4991 commented Feb 24, 2026

Just as a heads up, #275 contains insertionSort already. Also the PR does not seem to pass CI at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants