From d3f2f281ec59b08ea49c63c3a320d3122dbea460 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 4 Sep 2026 12:52:05 +0000 Subject: [PATCH 1/3] The growth corpus reaches 139 rules instead of 84 A declaration is only checked if the corpus makes the rule fire, and the arithmetic grammar reached 84 of the 324. Six whole sets -- trigonometry, booleans, sets, comparisons and both factorial ones -- were never exercised at all, so a declaration on any of their rules was checked by a test that never ran it. Two changes. Shapes the generated grammar does not build are supplied on top of it, the way MatchedRulesAgreeWithTheSwitchTest already supplies its own: the trigonometric identities and their arguments, logarithms, factorials both ways round, the boolean laws, comparisons, set operations, and the polynomial shapes the division, gcd and factorisation sets want. And a rule is now tried at every node of every corpus expression rather than only at the root, since a rule matches where its shape sits and asking only about whole expressions leaves most of them never firing. 139 rules now fire, 51 of them carrying a declaration, against 84 and 18 before. The counts are asserted so that the check cannot quietly stop being exercised. Every one of the 51 existing declarations was re-run against the wider corpus. None is contradicted, which is worth knowing rather than assuming: the coverage this adds is exactly where an untested claim would have been hiding, and the one false declaration found when this test was written was found the same way. No rule's growth changes here. The 88 undeclared rules that now fire are the measured evidence for declaring them, and that is a separate change because Collects and Rearranges alter which rules equality saturation fires. Full suite 9552 passed, 0 failed. Part of #825. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../RuleGrowthAgreesWithTheCorpusTest.cs | 69 +++++++++++++++++-- 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs b/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs index 7846b7ebf..21e3a4028 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs @@ -53,6 +53,57 @@ public sealed class RuleGrowthAgreesWithTheCorpusTest /// The same shape as the corpus MatchedRulesAgreeWithTheSwitchTest builds, and for /// the same reason: generated inputs reach arrangements nobody would think to write down. /// + /// + /// Shapes the arithmetic grammar above does not build. Without them the corpus reaches + /// the sets about quotients and powers and none of the six about trigonometry, booleans, + /// sets, comparisons or factorials — so those rules' declarations would be checked by a + /// test that never ran them. + /// + private static readonly string[] Reached = + { + // trigonometric, and the arguments the multiple-angle and collapse rules want + "sin(x)", "cos(x)", "tan(x)", "cotan(x)", "sec(x)", "cosec(x)", + "sin(2 * x)", "cos(2 * x)", "sin(x + y)", "cos(x + y)", "sin(3 * x)", "cos(3 * x)", + "sin(x) * cos(x)", "cos(x) * sin(x)", "sin(x) ^ 2 + cos(x) ^ 2", "cos(x) ^ 2 + sin(x) ^ 2", + "sin(x) / cos(x)", "cos(x) / sin(x)", "1 / sin(x)", "1 / cos(x)", "1 / tan(x)", + "tan(x) * cotan(x)", "sin(x) ^ 2", "cos(x) ^ 2", "sin(x) ^ 2 - cos(x) ^ 2", + "arcsin(x)", "arccos(x)", "arctan(x)", "arccotan(x)", + "arcsin(x) + arccos(x)", "arctan(x) + arccotan(x)", "sin(arcsin(x))", "cos(arccos(x))", + + // logarithms and exponentials + "ln(x)", "log(2, x)", "ln(x * y)", "ln(x ^ 2)", "e ^ ln(x)", "ln(e ^ x)", + "log(x, x)", "2 ^ x * 2 ^ y", "e ^ x * e ^ y", + + // factorials, both sets + "x!", "(x + 1)!", "(x + 1)! / x!", "x! / (x + 1)!", "x! * (x + 1)", "(x + 1) * x!", + "(x + 2)! / x!", + + // boolean + "a and b", "a or b", "not a", "not (not a)", "a implies b", "a xor b", + "a and not a", "a or not a", "a and a", "a or a", "a and (a or b)", "a or (a and b)", + "not (a and b)", "not (a or b)", "a and (b or c)", "(a and b) or (a and c)", + "true and a", "false or a", "true or a", "false and a", + + // comparisons and the equality set + "x > y", "x < y", "x >= y", "x <= y", "x = y", "(x < y) or (x = y)", "(x > y) or (x = y)", + "not (x > y)", "not (x = y)", + + // sets + "x in [1; 2]", "[1; 2] \\/ [3; 4]", "[1; 2] /\\ [3; 4]", "[1; 2] \\ [3; 4]", + "A \\/ A", "A /\\ A", "A \\/ {}", "A /\\ {}", "{ t : t > 0 }", + + // powers and roots the arithmetic grammar reaches only shallowly + "sqrt(x) * sqrt(x)", "sqrt(x ^ 2)", "(x ^ 2) ^ 3", "x ^ 2 * x ^ 3", "x ^ 3 / x ^ 2", + "x ^ 2 * y ^ 2", "(x / y) ^ 2", "x ^ (1/2) * x ^ (1/2)", "(-x) ^ 2", + "1 / x ^ (-2)", "x ^ 0", "0 ^ x", + + // polynomial shapes for the division, gcd and factorisation sets + "(x ^ 2 - 1) / (x - 1)", "(x ^ 2 + 2 * x + 1) / (x + 1)", "x ^ 2 - 1", + "x ^ 2 + 2 * x + 1", "x ^ 3 - 1", "x ^ 2 - y ^ 2", "x ^ 4 - y ^ 4", + "x ^ 2 + 2 * x * y + y ^ 2", "(x + y) ^ 2", "(x + y) * (x - y)", + "1 / (1 + sqrt(2))", "1 / (sqrt(3) - 1)", "2 / (1 + sqrt(x))", + }; + private static List Corpus() { var level1 = new List(Leaves); @@ -71,7 +122,7 @@ private static List Corpus() level3.Add(string.Format(shape, left, right)); var parsed = new List(); - foreach (var source in level1.Concat(level2).Concat(level3)) + foreach (var source in level1.Concat(level2).Concat(level3).Concat(Reached)) { try { parsed.Add(source.ToEntity()); } catch { /* the generator makes some strings the parser declines */ } @@ -88,7 +139,9 @@ private static List Corpus() /// private static Dictionary> Firings() { - var corpus = Corpus(); + // Every node, not only the root: a rule matches where its shape sits, and asking + // only about whole corpus expressions leaves most rules never firing at all. + var corpus = Corpus().SelectMany(expr => expr.Nodes).Distinct().ToList(); var firings = new Dictionary>(); foreach (var set in MatchedRules.All) foreach (var rule in set.Rules) @@ -148,11 +201,13 @@ public void TheCorpusExercisesTheDeclarationsItChecks() var fired = Firings().Where(pair => pair.Value.Count > 0).ToList(); var declared = fired.Count(pair => pair.Key.Growth is not RewriteRuleGrowth.Unknown); - // Named as counts that move rather than as "most of them". 84 rules reach the corpus, - // 18 of them carrying a declaration -- so the check above is exercised, and the other - // 66 are the measured evidence for declaring more of them. - Assert.True(fired.Count >= 80, $"only {fired.Count} rules fired at all"); - Assert.True(declared >= 15, $"only {declared} rules with a declared growth fired"); + // Named as counts that move rather than as "most of them". 139 of the 324 rules reach + // the corpus and 51 of those carry a declaration, so the check above is exercised + // rather than passing by never running; the other 88 are the measured evidence for + // declaring more of them. It was 84 and 18 before the shapes in `Reached` were added + // and before rules were tried at every node rather than only at the root. + Assert.True(fired.Count >= 135, $"only {fired.Count} rules fired at all"); + Assert.True(declared >= 48, $"only {declared} rules with a declared growth fired"); } } } From 673708902d1decde8e8eddabb5ef1736cee977df Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 4 Sep 2026 14:41:28 +0000 Subject: [PATCH 2/3] The corpus reaches the comparison, boolean and reciprocal rules too 194 of the 324 rules now fire, against 139 after the first extension and 84 before it. 59 of those carry a declaration, so that many are checked rather than merely asserted. The largest block that was out of reach is the comparison set. Its rules key on zero or a number being on the left of the operator, on both sides being the same thing, and on a negative factor or divisor standing beside a zero -- none of which an arithmetic grammar produces, since it builds comparisons only in the form the writer would. So `0 > x`, `x >= x`, `x / (-2) > 0` and `(-2) * x <= 0` are given directly, over all five comparison operators. The rest are De Morgan in both directions, which the earlier shapes gave only one way round; the trigonometric reciprocal pairs, where a cosecant times a sine and a cotangent of an arccotangent are the shapes rules exist for and nothing built; logarithms in and of reciprocal bases; and the differences and products that contain their own operand a second time. No declaration is contradicted by any of it. Full suite 9552 passed, 0 failed. Part of #825. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../RuleGrowthAgreesWithTheCorpusTest.cs | 53 ++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs b/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs index 21e3a4028..feaeb3576 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs @@ -50,14 +50,10 @@ public sealed class RuleGrowthAgreesWithTheCorpusTest { "({0}) + ({1})", "({0}) - ({1})", "({0}) * ({1})", "({0}) / ({1})", "({0}) ^ ({1})" }; /// - /// The same shape as the corpus MatchedRulesAgreeWithTheSwitchTest builds, and for - /// the same reason: generated inputs reach arrangements nobody would think to write down. - /// - /// - /// Shapes the arithmetic grammar above does not build. Without them the corpus reaches - /// the sets about quotients and powers and none of the six about trigonometry, booleans, - /// sets, comparisons or factorials — so those rules' declarations would be checked by a - /// test that never ran them. + /// Shapes the arithmetic grammar does not build. Without them the corpus reaches the sets + /// about quotients and powers and none of the six about trigonometry, booleans, sets, + /// comparisons or factorials — so those rules' declarations would be checked by a test + /// that never ran them. /// private static readonly string[] Reached = { @@ -102,6 +98,37 @@ public sealed class RuleGrowthAgreesWithTheCorpusTest "x ^ 2 + 2 * x + 1", "x ^ 3 - 1", "x ^ 2 - y ^ 2", "x ^ 4 - y ^ 4", "x ^ 2 + 2 * x * y + y ^ 2", "(x + y) ^ 2", "(x + y) * (x - y)", "1 / (1 + sqrt(2))", "1 / (sqrt(3) - 1)", "2 / (1 + sqrt(x))", + + // Comparisons. The largest block of rules the arithmetic grammar cannot reach at all: + // they key on zero or a number being on the left, on both sides being the same thing, + // and on a negative factor or divisor standing beside a zero. + "0 > x", "0 < x", "0 >= x", "0 <= x", "0 = x", + "2 > x", "2 < x", "2 >= x", "2 <= x", "2 = x", + "x > x", "x < x", "x >= x", "x <= x", + "x / (-2) > 0", "x / (-2) < 0", "x / (-2) >= 0", "x / (-2) <= 0", "x / (-2) = 0", + "(-2) * x > 0", "(-2) * x < 0", "(-2) * x >= 0", "(-2) * x <= 0", "(-2) * x = 0", + "x * (-2) > 0", "x * (-2) < 0", "x * (-2) >= 0", "x * (-2) <= 0", "x * (-2) = 0", + "(x > y) and (y > z)", "(x < y) and (y < z)", + "not (x >= y)", "not (x <= y)", "not (x < y)", "y > x", "y >= x", + + // Boolean, both directions of De Morgan and the absorptions with a negation. + "not a and not b", "not a or not b", "a and not b", "a or not b", + "not a or b", "not a and b", "a and (b and c)", "a or (b or c)", + "(a and b) and c", "(a or b) or c", "(a or b) and (a or c)", "(a and b) or c", + + // The trigonometric reciprocal pairs and the inverses the shapes above miss. + "cosec(x) * sin(x)", "sin(x) * cosec(x)", "sec(x) * cos(x)", "cos(x) * sec(x)", + "cotan(arccotan(x))", "tan(arctan(x))", "arcsin(sin(x))", "arctan(tan(x))", + "sin(2 * x) * cosec(x)", "cos(2 * x) * sec(x)", "cotan(x) * tan(x)", + + // Logarithms in and of reciprocals. + "log(1/2, x)", "log(2, 1/x)", "log(1/2, 1/x)", "log(1/x, y)", "log(x, 1/y)", + + // A factorial beside a zero, and the differences and products that contain their own + // operand a second time. + "x! = 0", "(x - y) / (y - x)", "x - (x - y)", "(x - y) - x", + "x * y - x", "x - x * y", "x * x", "x * x * y", "x * (x * y)", + "sin(x) * 2", "2 * sin(x)", }; private static List Corpus() @@ -201,13 +228,13 @@ public void TheCorpusExercisesTheDeclarationsItChecks() var fired = Firings().Where(pair => pair.Value.Count > 0).ToList(); var declared = fired.Count(pair => pair.Key.Growth is not RewriteRuleGrowth.Unknown); - // Named as counts that move rather than as "most of them". 139 of the 324 rules reach - // the corpus and 51 of those carry a declaration, so the check above is exercised - // rather than passing by never running; the other 88 are the measured evidence for + // Named as counts that move rather than as "most of them". 194 of the 324 rules reach + // the corpus and 59 of those carry a declaration, so the check above is exercised + // rather than passing by never running; the rest are the measured evidence for // declaring more of them. It was 84 and 18 before the shapes in `Reached` were added // and before rules were tried at every node rather than only at the root. - Assert.True(fired.Count >= 135, $"only {fired.Count} rules fired at all"); - Assert.True(declared >= 48, $"only {declared} rules with a declared growth fired"); + Assert.True(fired.Count >= 190, $"only {fired.Count} rules fired at all"); + Assert.True(declared >= 56, $"only {declared} rules with a declared growth fired"); } } } From 78f2152f1dbffae265cc5c0f63311940d3486ec5 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Fri, 4 Sep 2026 15:09:34 +0000 Subject: [PATCH 3/3] The corpus puts a comparison inside a comparison The rules about comparisons bind their operands with `Any`, so one of those operands can be a comparison itself. Building the replacement with `<` or `>` then chains, where the same rule written with `EqualTo` would not: `(x > y) < 0` is `x > y and y < 0`, seven nodes rather than three. That is not hypothetical and it is the difference between a true declaration and a false one. `(a / k > 0) = (a < 0)` for a negative k measures -2 at every point the corpus reached, and counting its pattern against its replacement gives -(|k| + |zero|), which is at most -2 as well. Both say Collects. On `((x > y) / (-2)) > 0` the pattern is seven nodes and so is the replacement, the delta is 0, and Collects is false. The same holds for the fifteen rules of that family and for the comparison rules that turn an operand round. The comment on `a-equals-with-zero-on-the-left-turns-round` says exactly this -- that `EqualTo` does not chain "the way `Equalizes` and the comparison operators do" -- and its comparison twins were left undeclared for that reason. A corpus of a hundred and ninety-four rules could not refute the wrong declaration, because it never built a comparison inside a comparison. Now it does, so a rule whose replacement chains no longer looks like a clean shrink at every point that is looked at. No declaration is contradicted, so the fifty-nine that exist are unaffected. Full suite 9552 passed, 0 failed. Part of #825. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- .../RuleGrowthAgreesWithTheCorpusTest.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs b/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs index feaeb3576..ed989b123 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/RuleGrowthAgreesWithTheCorpusTest.cs @@ -124,6 +124,17 @@ public sealed class RuleGrowthAgreesWithTheCorpusTest // Logarithms in and of reciprocals. "log(1/2, x)", "log(2, 1/x)", "log(1/2, 1/x)", "log(1/x, y)", "log(x, 1/y)", + // A comparison standing where a number would. The rules about comparisons bind their + // operands with `Any`, so one of them can be a comparison itself -- and building the + // replacement with `<` or `>` then *chains*, where the same rule written with + // `EqualTo` would not: `(x > y) < 0` is `x > y and y < 0`, seven nodes rather than + // three. Without these a rule whose replacement chains looks like a clean shrink at + // every point the corpus reaches and is not one. + "(x > y) < 0", "(x > y) > 0", "(x > y) <= 0", "(x > y) >= 0", + "(x > y) / (-2) > 0", "(x > y) / (-2) < 0", "(x < y) / (-2) >= 0", + "(-2) * (x > y) > 0", "(x > y) * (-2) < 0", "0 > (x > y)", "0 < (x < y)", + "2 > (x > y)", "(x > y) = 0", + // A factorial beside a zero, and the differences and products that contain their own // operand a second time. "x! = 0", "(x - y) / (y - x)", "x - (x - y)", "(x - y) - x",