Skip to content

Propagate errors from recursive calls in sort - #4767

Open
diracdeltafunk wants to merge 1 commit into
Macaulay2:developmentfrom
aimathconference:sort-propagate-errors
Open

diracdeltafunk wants to merge 1 commit into
Macaulay2:developmentfrom
aimathconference:sort-propagate-errors

Conversation

@diracdeltafunk

@diracdeltafunk diracdeltafunk commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

sort and rsort drop an error raised by a ? method whenever the failing comparison happens inside one of quicksort's recursive calls. subsort (in d/actors3.d) returns the error from each comparison it makes itself, but ignores the values returned by its recursive calls, so the caller gets a partially sorted list and no error. This change returns those errors too. basicsort already restores its saved state when subsort returns an error, so nothing else is needed.

T = new Type of List
T ? T := (a, b) -> if set {a#0, b#0} === set {1, 2} then error "incomparable" else a#0 ? b#0
sort apply({4,3,2,1,0}, i -> new T from {i})

Before: {T{2}, T{1}, T{0}, T{3}, T{4}}, with no error.
After: error: incomparable.

Testing

Added a case to tests/normal/sort.m2: a ? method that fails on one pair of adjacent elements. Quicksort compares adjacent elements at some level of the recursion whatever pivots it picks, so the test doesn't depend on pivot choice. Both new assertions fail on 1.26.06 and pass with this change.

Built on macOS (arm64, CMake) and ran tests/normal, with no new failures.

Release notes

sort and rsort no longer ignore errors raised by the comparison method.

AI disclosure

  • 0 — No AI tools were used or consulted.
  • 1 — AI was used only for lookups, explanations, or advice; it wrote no code or text in this pull request.
  • 2 — AI made small edits, which I reviewed.
  • 3 — AI drafted substantial parts, which I reviewed.
  • 4 — AI produced most or all of the content, which I reviewed thoroughly.
  • 5 — AI produced the content with minimal human review.

Tools and models used: Claude Code (Claude Opus 5.5). The bug was first flagged in an automated audit by Codex.
How AI was used, and on which parts: Claude reproduced the bug, wrote the fix, the test, and this description.
How I verified the result: I read the code and looked at test outputs.

License

  • My changes use the existing license of the affected files or package.
  • I release my new material under the GNU GPL, version 2 or later.
  • Other (public domain, another license, or not applicable); details below.

Checklist

  • This pull request targets the development branch.
  • I have reviewed my own changes.
  • Documentation and tests are updated as needed.

🤖 Generated with Claude Code

subsort returned errors from the comparisons it made itself but ignored
the values of its recursive calls, so an error from a ? method was
dropped whenever it happened below the top level, and sort and rsort
returned a partially sorted list instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@diracdeltafunk
diracdeltafunk marked this pull request as ready for review October 31, 2026 00:04
@d-torrance d-torrance added Core Issues involving the Core scripts. Interpreter AI-generated This PR contains AI-generated code or this issue was written by AI labels Oct 31, 2026
@mahrud

mahrud commented Oct 31, 2026

Copy link
Copy Markdown
Member

Just a thought: wouldn't it be better if sort didn't fail on incomparable elements, but just ordered the elements by the partial order? (Ideally with the order of incomparable elements matching their original order)

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

Labels

AI-generated This PR contains AI-generated code or this issue was written by AI Core Issues involving the Core scripts. Interpreter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants