feat(Algorithms): comparison sort lower bound via decision-tree induction - #770
Open
SamuelSchlesinger wants to merge 2 commits into
Open
feat(Algorithms): comparison sort lower bound via decision-tree induction#770SamuelSchlesinger wants to merge 2 commits into
SamuelSchlesinger wants to merge 2 commits into
Conversation
…tion A comparison program is a free monad over a single comparison query, i.e. a binary decision tree. card_image_run_le_two_pow_of_cost_le bounds its distinct results over a finite comparator family by 2 ^ t whenever every comparator in the family costs at most t comparisons, by structural induction: the family splits at the root comparison. A program that sorts under every hidden permutation order on Fin n therefore takes at least log2(n!) comparisons in the worst case; (n/2) * log2(n/2) follows as a corollary.
SamuelSchlesinger
requested review from
chenson2018,
fmontesi and
sorrachai
as code owners
August 3, 2026 06:41
Address the suggestion that data should not be constructed by pattern matching on FreeM: run and cost are now the two projections of the canonical interpretation (FreeM.liftM) of a program into TimeM, so the monad-morphism lemmas run_bind and cost_bind follow from liftM_bind rather than bespoke inductions. The unfolding lemmas remain definitional, and the lower bound proofs are unchanged except that grind now closes their mechanical leaf goals through the grind-annotated unfolding lemmas. Drop two unused imports.
eric-wieser
reviewed
Aug 4, 2026
Comment on lines
+71
to
+72
| def run (P : FreeM (SortOps α) β) (le : α → α → Bool) : β := | ||
| (P.liftM (sortHandler le)).ret |
Collaborator
There was a problem hiding this comment.
I don't think Cslib.Algorithms.run should be about SortOps; this needs a better name.
Collaborator
There was a problem hiding this comment.
Alternatively, just drop the run altogether and inline (P.liftM (sortHandler le)).ret below (note that ret has notation)
Contributor
There was a problem hiding this comment.
Large parts of this code verbatim resemble the proof in #685
Contributor
|
Large parts of this resemble #685 verbatim. It would be nice to have a diff. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
An attempt to prove the comparison sort lower bound of #685 taking, in my view, a more natural proof approach for this model. The original proof analyzes traces of the computation, while this approach takes direct advantage of the inductive structure of the free monad and essentially does induction on the program. At a high level, #685 formalizes the encoding argument whereas we formalize the decision tree argument. The arithmetic estimate of log_2 n! is adapted from #685.
Claude Fable 5 authored this code under my direction and review.