Expand a cancelled quotient of factorials instead of throwing (#817) - #820
Merged
Conversation
Expansion cancels a quotient of factorials before anything else, and the cancellation can leave a sum: (x + 1)! / x! is x + 1. The result went straight back into SmartExpandOver, which is documented not to take a sum and asserts that it never gets one -- so the assertion fired and an AngouriBugException came out of Entity.Expand, on an expression Simplify answered as 1 + x throughout. Sending the cancelled expression through GatherLinearChildrenOverSumAndExpand instead is one identifier and a strict superset: for anything that is not a sum it calls SmartExpandOver, which is what the line did before, and for a sum it splits and expands the terms rather than throwing. Measured on this build: (x + 1)! / x! was AngouriBugException, is x + 1 (x + 2)! / x! was AngouriBugException, is x ^ 2 + 3 * x + 2 (x + 3)! / x! was AngouriBugException, is x ^ 3 + 6 * x ^ 2 + 11 * x + 6 x! / (x + 1)! was AngouriBugException, is 1 / (x + 1) (x + y + 1)! / (x + y)! was AngouriBugException, is x + y + 1 Each checked as a value rather than a string, at a point where the factorials are defined: 5!/4! is 5, 6!/4! is 30, 7!/4! is 210. The regression tests assert those values and that no factorial survives where the two cancel completely; they fail on five cases without the fix, including the nested and multiplied ones. Tests: 5808 passing, 0 failed, 14 skipped -- the skip this issue held is gone. F# 130. BREAKING-CHANGES.md records the throw becoming a value. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Closes #817.
What was wrong
Entity.Expandcancels a quotient of factorials before it does anything else, and the cancellation can leave a sum —(x + 1)! / x!isx + 1. The result went straight back intoSmartExpandOver, which is documented<summary>expr is NEITHER Sumf NOR Minusf</summary>and asserts that it never receives one. So the assertion fired and anAngouriBugException— the library reporting that the library is broken — came out of a public method, on an expressionSimplifyanswered as1 + xthroughout.Thrown rather than returned, so a caller mapping
Expandover a corpus loses the whole run instead of one entry.Why this fix is right
GatherLinearChildrenOverSumAndExpandis the sum-aware entry point, and it already delegates toSmartExpandOverfor anything that is not a sum. Calling it here is one identifier and a strict superset of the old behaviour: identical where the cancelled expression is not a sum, and correct instead of fatal where it is.The other two recursive calls are unaffected — one is handed a product, and the public
MathS.SmartExpandOversplits bySumf.LinearChildrenbefore descending, which is exactly the guard that was missing here.Measured
On this build, each of these threw
AngouriBugExceptionbefore:(x + 1)! / x!x + 1(x + 2)! / x!x ^ 2 + 3 * x + 2(x + 3)! / x!x ^ 3 + 6 * x ^ 2 + 11 * x + 6x! / (x + 1)!1 / (x + 1)(x + 1)! / x! + yx + 1 + y((x + 1)! / x!) * (a + b)a * x + b * x + a + b(x + y + 1)! / (x + y)!x + y + 1Checked as values, not strings, at a point where the factorials are defined: 5!/4! = 5, 6!/4! = 30, 7!/4! = 210 — and the expanded form agrees with the original at that point in every case.
sin(x)! / x!is unchanged and still comes back as written, which is correct: nothing cancels.Tests
5808 passing, 0 failed, 14 skipped — the skip this issue was holding is gone. F# 130 passing.
Two regression tests: one asserting the value is preserved, one asserting no factorial survives where the two cancel completely. Both were checked against the unfixed code — five cases fail without the change, including the nested (
+ y) and multiplied (* (a + b)) ones, so the tests are not passing vacuously.Compatibility
BREAKING-CHANGES.mdrecords it: the same call returns a value where it used to throw. Nothing could have depended on the old behaviour —AngouriBugExceptionis not a contract — but the entry belongs there anyway since the answer changed.🤖 Generated with Claude Code