|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2026 Angouri. |
| 3 | +// AngouriMath is licensed under MIT. |
| 4 | +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. |
| 5 | +// Website: https://am.angouri.org. |
| 6 | +// |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +using AngouriMath; |
| 12 | +using AngouriMath.Core.Transformations; |
| 13 | +using AngouriMath.Core.Transformations.Matching; |
| 14 | +using AngouriMath.Extensions; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace AngouriMath.Tests.Core.Transformations |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// A rule's <see cref="RewriteRuleGrowth"/> is a claim about every expression it fires on: |
| 21 | + /// <c>Collects</c> says the replacement is smaller, <c>Rearranges</c> that it is the same |
| 22 | + /// size, <c>Expands</c> that it is larger. Here that claim is run against a generated corpus |
| 23 | + /// instead of being taken on trust. |
| 24 | + /// </summary> |
| 25 | + /// <remarks> |
| 26 | + /// <para> |
| 27 | + /// It is worth checking because growth is <b>load-bearing</b> and not documentation: |
| 28 | + /// <c>Saturation.RulesUpTo</c> selects by it, so a rule declaring <c>Collects</c> or |
| 29 | + /// <c>Rearranges</c> is one equality saturation fires and a rule left at <c>Unknown</c> is |
| 30 | + /// one it does not. A declaration that is wrong in the shrinking direction tells the |
| 31 | + /// saturation a rewrite is safe to run when it is not. |
| 32 | + /// </para> |
| 33 | + /// <para> |
| 34 | + /// A corpus can only refute, never confirm: firing on fourteen hundred expressions without |
| 35 | + /// contradicting a claim is evidence for it and not proof of it, since the claim is about |
| 36 | + /// every expression there is. So this fails on a contradiction and says nothing about a rule |
| 37 | + /// it could not make fire. |
| 38 | + /// </para> |
| 39 | + /// </remarks> |
| 40 | + [Trait("Area", "Core")] |
| 41 | + public sealed class RuleGrowthAgreesWithTheCorpusTest |
| 42 | + { |
| 43 | + private static readonly string[] Leaves = |
| 44 | + { "x", "y", "2", "-1", "-2", "1/2", "-1/2", "1", "0" }; |
| 45 | + |
| 46 | + private static readonly string[] Unary = |
| 47 | + { "-({0})", "1 / ({0})", "({0}) ^ 2", "({0}) ^ (-2)", "sqrt({0})", "sin({0})", "abs({0})" }; |
| 48 | + |
| 49 | + private static readonly string[] Binary = |
| 50 | + { "({0}) + ({1})", "({0}) - ({1})", "({0}) * ({1})", "({0}) / ({1})", "({0}) ^ ({1})" }; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// The same shape as the corpus <c>MatchedRulesAgreeWithTheSwitchTest</c> builds, and for |
| 54 | + /// the same reason: generated inputs reach arrangements nobody would think to write down. |
| 55 | + /// </summary> |
| 56 | + private static List<Entity> Corpus() |
| 57 | + { |
| 58 | + var level1 = new List<string>(Leaves); |
| 59 | + var level2 = new List<string>(); |
| 60 | + foreach (var shape in Unary) |
| 61 | + foreach (var inner in level1) |
| 62 | + level2.Add(string.Format(shape, inner)); |
| 63 | + foreach (var shape in Binary) |
| 64 | + foreach (var left in level1) |
| 65 | + foreach (var right in level1) |
| 66 | + level2.Add(string.Format(shape, left, right)); |
| 67 | + var level3 = new List<string>(); |
| 68 | + foreach (var shape in Binary) |
| 69 | + foreach (var left in level2.Where((_, i) => i % 17 == 0)) |
| 70 | + foreach (var right in level2.Where((_, i) => i % 23 == 0)) |
| 71 | + level3.Add(string.Format(shape, left, right)); |
| 72 | + |
| 73 | + var parsed = new List<Entity>(); |
| 74 | + foreach (var source in level1.Concat(level2).Concat(level3)) |
| 75 | + { |
| 76 | + try { parsed.Add(source.ToEntity()); } |
| 77 | + catch { /* the generator makes some strings the parser declines */ } |
| 78 | + } |
| 79 | + return parsed; |
| 80 | + } |
| 81 | + |
| 82 | + private static int Size(Entity entity) => entity.Nodes.Count(); |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Every firing of every rule over the corpus, as (rule, how the size moved). A rule that |
| 86 | + /// hands back what it was given has not fired and is not counted — the factorial sets |
| 87 | + /// have arms that match a shape and then decline. |
| 88 | + /// </summary> |
| 89 | + private static Dictionary<MatchedRule, List<(Entity Before, Entity After)>> Firings() |
| 90 | + { |
| 91 | + var corpus = Corpus(); |
| 92 | + var firings = new Dictionary<MatchedRule, List<(Entity, Entity)>>(); |
| 93 | + foreach (var set in MatchedRules.All) |
| 94 | + foreach (var rule in set.Rules) |
| 95 | + { |
| 96 | + var seen = new List<(Entity, Entity)>(); |
| 97 | + foreach (var expr in corpus) |
| 98 | + { |
| 99 | + Entity? after; |
| 100 | + try { after = rule.TryApply(expr); } |
| 101 | + catch { continue; /* a rule declining loudly is not this test's subject */ } |
| 102 | + if (after is not null && after != expr) |
| 103 | + seen.Add((expr, after)); |
| 104 | + } |
| 105 | + firings[rule] = seen; |
| 106 | + } |
| 107 | + return firings; |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void NoDeclaredGrowthIsContradictedByTheCorpus() |
| 112 | + { |
| 113 | + var wrong = new List<string>(); |
| 114 | + foreach (var (rule, firings) in Firings()) |
| 115 | + { |
| 116 | + if (rule.Growth is RewriteRuleGrowth.Unknown || firings.Count == 0) |
| 117 | + continue; |
| 118 | + foreach (var (before, after) in firings) |
| 119 | + { |
| 120 | + var delta = Size(after) - Size(before); |
| 121 | + var contradicts = rule.Growth switch |
| 122 | + { |
| 123 | + RewriteRuleGrowth.Collects => delta >= 0, |
| 124 | + RewriteRuleGrowth.Rearranges => delta != 0, |
| 125 | + RewriteRuleGrowth.Expands => delta <= 0, |
| 126 | + _ => false |
| 127 | + }; |
| 128 | + if (contradicts) |
| 129 | + { |
| 130 | + wrong.Add($"{rule.Name} declares {rule.Growth} but {before} -> {after} " |
| 131 | + + $"moves {Size(before)} nodes to {Size(after)}"); |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + Assert.True(wrong.Count == 0, |
| 138 | + $"{wrong.Count} rules contradicted by the corpus:\n" + string.Join("\n", wrong)); |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// The corpus has to actually exercise the declarations, or the test above passes by |
| 143 | + /// never running. Named as a count that moves rather than as "most of them". |
| 144 | + /// </summary> |
| 145 | + [Fact] |
| 146 | + public void TheCorpusExercisesTheDeclarationsItChecks() |
| 147 | + { |
| 148 | + var fired = Firings().Where(pair => pair.Value.Count > 0).ToList(); |
| 149 | + var declared = fired.Count(pair => pair.Key.Growth is not RewriteRuleGrowth.Unknown); |
| 150 | + |
| 151 | + // Named as counts that move rather than as "most of them". 84 rules reach the corpus, |
| 152 | + // 18 of them carrying a declaration -- so the check above is exercised, and the other |
| 153 | + // 66 are the measured evidence for declaring more of them. |
| 154 | + Assert.True(fired.Count >= 80, $"only {fired.Count} rules fired at all"); |
| 155 | + Assert.True(declared >= 15, $"only {declared} rules with a declared growth fired"); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments