diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 2db9400c6..1fa147e33 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -233,6 +233,32 @@ returned as all of them. `MathS.Equations("y^2 - 2", "x^2 + (P - Q y) * x - 3"). --- +### A set `MathS.Settings.Budget` caps a budget passed as an argument instead of replacing it + +**Silent: the same call stops sooner.** A set `MathS.Settings.Budget` replaced every budget in the library, including one +a caller passed by hand, so `EqualitySaturation(budget, ...)` and `CanonicalizationOverGraph(budget)` inside a scope set +to `WorkBudget.Unlimited` ran unlimited. Now what a set budget does depends on what the budget it meets is for +([#2454](https://github.com/asc-community/AngouriMath/issues/2454)): a budget passed as an argument runs under the smaller +limit on each axis of it and the setting, so `Unlimited` leaves it as passed; the perfect-square proof that `Factorize` +runs and `Simplify` offers keeps its own lid of 2,000 steps and 50 ms under the same rule, so a set budget only tightens +it; and the system solver and its Gröbner path are replaced by the setting as before, so raising it still allows more. +With `budget = new WorkBudget { Steps = 5, Time = TimeSpan.FromSeconds(30) }`, inside +`MathS.Settings.Budget.Set(WorkBudget.Unlimited)`: + +| Input | Was (2.7.0) | Now | +|---|---|---| +| `Transformation.EqualitySaturation(budget, CostModel.Default).ApplyOrKeep("x * 2 * 3 + x")` | `x * 7` | `x * 2 * 3 + x` | +| `Transformation.EqualitySaturation(budget, CostModel.Default).ApplyOrKeep("2 * sqrt(2) * sqrt(3)")` | `2 * sqrt(6)` | `2 * sqrt(2) * sqrt(3)` | +| `Transformation.CanonicalizationOverGraph(budget).ApplyOrKeep("a * b + b * a")` | `2 * a * b` | `a * b + a * b` | +| `Transformation.CanonicalizationOverGraph(budget).ApplyOrKeep("x + 0 + 2 * x")` | `3 * x` | `2 * x + x` | + +Unset, nothing moves. `Factorize` and `Simplify` give the same answers on 2.7.0 and now under an unset, +an `Unlimited`, a `{ Steps = 1 }` and a `{ Steps = 1_000_000 }` budget, measured on `2 + 2 * sqrt(2) * sqrt(3) + 3`, +`a + 2 * sqrt(a) * sqrt(b) + b` and `2 + 2 * sqrt(6) + 3`: a proof the lid stops is "not proved", and the symbolic test +behind it still answers. + +--- + ### An indexed union or intersection binds its index **Silent: a bound name is no longer reported free.** `FreeVariables` of `union(A_k, k in I)` and `intersection(A_k, k in I)` diff --git a/Sources/AngouriMath/Convenience/MathS.Equality.cs b/Sources/AngouriMath/Convenience/MathS.Equality.cs index 0d3ad6880..261f16b1b 100644 --- a/Sources/AngouriMath/Convenience/MathS.Equality.cs +++ b/Sources/AngouriMath/Convenience/MathS.Equality.cs @@ -25,7 +25,8 @@ partial class MathS /// tried, a rule's match attempt, a node the graph grows by -- so the same question under /// the same steps gets the same answer on any machine; a /// makes it depend on the machine, and says when it - /// did. , where set, overrides this. + /// did. , where set, caps this: the smaller limit on each axis + /// applies, so an ambient leaves it as passed. /// /// /// The weakest soundness a proof may rest on. , the default, diff --git a/Sources/AngouriMath/Convenience/MathS.cs b/Sources/AngouriMath/Convenience/MathS.cs index 142a9d47a..a2cde479c 100644 --- a/Sources/AngouriMath/Convenience/MathS.cs +++ b/Sources/AngouriMath/Convenience/MathS.cs @@ -6694,11 +6694,40 @@ internal static EDecimal DowncastingTolerance /// /// Leaving this alone is not the same as setting it to /// . Unset, each algorithm keeps the budget - /// it chose for itself -- the Gröbner path bounds itself by five seconds and - /// by five structural ceilings, and did so before this setting existed. Set, this - /// replaces those defaults wherever a budget is honoured, so + /// it chose for itself. Set, what it does depends on what that budget is for + /// (#2454): + /// + /// + /// A working allowance is replaced. The system solver + /// (: 10,000 steps and 60 seconds + /// over the whole call) and its Gröbner path (5 seconds) run under this budget + /// instead of their own, so this is how a caller raises them -- and /// here means "do not stop", which for a - /// doubly-exponential algorithm means what it says. + /// doubly-exponential algorithm means what it says. + /// A lid can only be tightened. The proof that a written-out perfect + /// square is one, which runs and + /// offers as a candidate, bounds itself by 2,000 + /// steps and 50 milliseconds against a stall. It runs under the smaller limit on + /// each axis of that and this, so setting this never makes Simplify or + /// Factorize run longer. A proof stopped early is "not proved" and falls back + /// to the symbolic test, so tightening it costs time, not answers. + /// A budget passed as an argument is capped. + /// , + /// and + /// + /// spend the budget they were given, under the smaller limit on each axis of it and + /// this: the outer scope bounds the inner one, and + /// here leaves the argument as passed. + /// + /// + /// A budget names both axes. cannot say "no opinion" + /// about one of them, so a left + /// is "no clock": new WorkBudget { Steps = 1_000_000 } raises the solver's + /// steps and also removes its 60 seconds. Against a lid or an argument the missing + /// clock is simply the looser limit, and theirs applies -- the lid's 50 milliseconds + /// included, which decide how soon the proof gives up and not the answer. Saying + /// "no opinion" per axis is a change to itself, left to v3 + /// (#746). /// /// /// A budget in is reproducible and one in @@ -6709,17 +6738,10 @@ internal static EDecimal DowncastingTolerance /// (#373) /// /// - /// It is honoured only where an algorithm asks. Today that is the system solver -- - /// its Gröbner path, and the ledger - /// keeps over the whole call --, the rewrite graph wherever it runs: - /// , - /// and - /// , and the proof that a written-out perfect - /// square is one, which runs and - /// offers as a candidate. Simplify itself - /// is not bounded by it, and nor is anything else in the library, each of which - /// bounds itself by ceilings of its own that this does not reach; so setting it does - /// not make an arbitrary call bounded. + /// It is honoured only at the sites listed above. Simplify itself is not + /// bounded by it, and nor is anything else in the library, each of which bounds + /// itself by ceilings of its own that this does not reach; so setting it does not + /// make an arbitrary call bounded. /// /// /// diff --git a/Sources/AngouriMath/Core/Budgets/BudgetLedger.cs b/Sources/AngouriMath/Core/Budgets/BudgetLedger.cs index 07bac7201..209fdf105 100644 --- a/Sources/AngouriMath/Core/Budgets/BudgetLedger.cs +++ b/Sources/AngouriMath/Core/Budgets/BudgetLedger.cs @@ -69,11 +69,48 @@ private BudgetLedger(string where, WorkBudget limits) /// /// A ledger for one computation. is what the algorithm - /// bounds itself by when nobody has said otherwise; a caller who sets - /// replaces it. + /// bounds itself by when nobody has set ; + /// says what a set one does to it. + /// https://github.com/asc-community/AngouriMath/issues/2454 /// - internal static BudgetLedger For(string where, WorkBudget ownDefault) - => new(where, MathS.Settings.Budget.IsOverriden ? MathS.Settings.Budget.Value : ownDefault); + internal static BudgetLedger For(string where, WorkBudget ownDefault, BudgetRole role) + => new(where, Limits(ownDefault, role)); + + /// The limits a computation of runs under. + internal static WorkBudget Limits(WorkBudget ownDefault, BudgetRole role) + { + var setting = MathS.Settings.Budget; + if (!setting.IsOverriden) + return ownDefault; + var set = setting.Value; + // A lid is tightened on both axes, not on the clock alone, although a step means + // something different to each algorithm. The incomparability is a reason not to + // *raise* one algorithm's ceiling by a number chosen for another, and a lid is never + // raised. Taking only the clock would ignore a caller who set steps -- the + // reproducible axis -- at exactly the sites where they asked for less. And it is + // cheap: the one lid, the perfect-square proof, completes in 15 to 1,203 steps over + // the unit suite (median 212), and a proof stopped short is "not proved", which falls + // back to the symbolic test -- so a budget below that costs time, and no answer of + // Factorize or Simplify was measured to move under one. + return role switch + { + BudgetRole.Allowance => set, + BudgetRole.Lid or BudgetRole.Argument => Tighter(ownDefault, set), + _ => throw new ArgumentOutOfRangeException(nameof(role), role, null), + }; + } + + /// + /// The smaller limit on each axis: what both and + /// allow. A missing is no ceiling, + /// so it yields to any clock. + /// + private static WorkBudget Tighter(WorkBudget a, WorkBudget b) + => new() + { + Steps = Math.Min(a.Steps, b.Steps), + Time = a.Time is { } x && b.Time is { } y ? (x < y ? x : y) : a.Time ?? b.Time, + }; /// /// Charges of work and answers whether the computation may diff --git a/Sources/AngouriMath/Core/Budgets/BudgetRole.cs b/Sources/AngouriMath/Core/Budgets/BudgetRole.cs new file mode 100644 index 000000000..8ced7a44b --- /dev/null +++ b/Sources/AngouriMath/Core/Budgets/BudgetRole.cs @@ -0,0 +1,39 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +namespace AngouriMath.Core.Budgets +{ + /// + /// What a computation's own budget is for, which decides what a set + /// does to it. Declared where the ledger is made, + /// , so that each site says which it is. + /// https://github.com/asc-community/AngouriMath/issues/2454 + /// + internal enum BudgetRole + { + /// + /// A guard against a stall inside an operation that is not about this computation + /// at all -- the perfect-square proof Factorize runs. A set budget can only + /// tighten it, so setting one never makes Simplify or Factorize run + /// longer than they would unset. + /// + Lid, + + /// + /// The working allowance of an algorithm a caller asks for by name -- the system + /// solver, the Gröbner path. A set budget replaces it, which is what lets a caller + /// raise it; capping it instead would make raising it do nothing. + /// + Allowance, + + /// + /// A budget the caller passed as an argument. It is the allowance, and a set budget, + /// being the outer scope, caps it: the smaller limit on each axis. + /// + Argument, + } +} diff --git a/Sources/AngouriMath/Core/Budgets/WorkBudget.cs b/Sources/AngouriMath/Core/Budgets/WorkBudget.cs index 271962f67..b3450a3c7 100644 --- a/Sources/AngouriMath/Core/Budgets/WorkBudget.cs +++ b/Sources/AngouriMath/Core/Budgets/WorkBudget.cs @@ -62,8 +62,8 @@ public sealed record WorkBudget /// /// No ceiling on either axis. This is what /// reads as its default — but see that property: leaving it alone means each - /// algorithm keeps its own default, and it is only a caller who sets it who gets - /// this. + /// algorithm keeps its own default, and setting it to this lifts only the working + /// allowances, never a lid. /// public static WorkBudget Unlimited { get; } = new(); } diff --git a/Sources/AngouriMath/Core/Transformations/EqualityOracle.cs b/Sources/AngouriMath/Core/Transformations/EqualityOracle.cs index 656cf9a83..4bd9636e9 100644 --- a/Sources/AngouriMath/Core/Transformations/EqualityOracle.cs +++ b/Sources/AngouriMath/Core/Transformations/EqualityOracle.cs @@ -203,7 +203,8 @@ internal static EqualityAnswer Ask(Entity left, Entity right, WorkBudget budget, throw new ArgumentOutOfRangeException(nameof(domain), domain, "Equality is asked over the complex or the real numbers."); - var ledger = BudgetLedger.For(Where, budget); + // The caller's argument, capped by a set MathS.Settings.Budget. + var ledger = BudgetLedger.For(Where, budget, BudgetRole.Argument); try { if (left.Equals(right)) diff --git a/Sources/AngouriMath/Core/Transformations/Saturation.cs b/Sources/AngouriMath/Core/Transformations/Saturation.cs index 71c36041b..9c6bcc4ef 100644 --- a/Sources/AngouriMath/Core/Transformations/Saturation.cs +++ b/Sources/AngouriMath/Core/Transformations/Saturation.cs @@ -270,7 +270,9 @@ internal static bool ProvesEqual(Entity left, Entity right, var b = graph.AddEntity(right); graph.Rebuild(); - var ledger = BudgetLedger.For(nameof(ProvesEqual), budget); + // A lid: the one caller passes a guard against stalls, not an allowance a user + // chose, so a set MathS.Settings.Budget can only tighten it. + var ledger = BudgetLedger.For(nameof(ProvesEqual), budget, BudgetRole.Lid); Run(graph, rules, ledger, CostModel.Default.Cost); ledger.Report(); return graph.Find(a) == graph.Find(b); diff --git a/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs b/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs index 4d6395984..3232384d6 100644 --- a/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs +++ b/Sources/AngouriMath/Core/Transformations/Transformation.Catalogue.cs @@ -441,9 +441,9 @@ private static class PolynomialFactorizationHolder /// /// What this call may spend before it settles for the best it has found so far. /// is charged once per e-node the graph actually creates; - /// is the wall-clock backstop. A caller who sets - /// overrides this, the same as every other bounded - /// computation in the library. + /// is the wall-clock backstop. A set + /// caps this, the smaller limit on each axis + /// applying, and does not replace it. /// /// Which candidate counts as cheapest once exploration stops. /// @@ -770,7 +770,9 @@ internal EqualitySaturationTransformation(WorkBudget budget, CostModel costModel var root = graph.AddEntity(input); graph.Rebuild(); - var ledger = BudgetLedger.For(Name, budget); + // The caller passed this budget, so it is theirs to spend and a set + // MathS.Settings.Budget, the outer scope, caps it. + var ledger = BudgetLedger.For(Name, budget, BudgetRole.Argument); // Stop once the answer has: after two passes in which the cheapest member of the // root's class did not change, further merging can only add members this // extraction would not choose. That is the fixed point a caller who extracts @@ -829,7 +831,8 @@ internal GraphCanonicalizationTransformation(WorkBudget budget, RewriteRuleGrowt var root = graph.AddEntity(input); graph.Rebuild(); - var ledger = BudgetLedger.For(Name, budget); + // An argument, as in EqualitySaturationTransformation. + var ledger = BudgetLedger.For(Name, budget, BudgetRole.Argument); // The same stop as EqualitySaturationTransformation's, on the least member // rather than the cheapest, since that is what this extracts. Entity? last = null; diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/Buchberger.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/Buchberger.cs index 6ed042a09..6956f5304 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/Buchberger.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/Buchberger.cs @@ -51,7 +51,8 @@ internal sealed class GroebnerBudget [ConstantField] internal static readonly WorkBudget Default = new() { Time = TimeSpan.FromSeconds(5) }; - private readonly BudgetLedger ledger = BudgetLedger.For("Gröbner", Default); + // An allowance: a set MathS.Settings.Budget replaces Default, so a caller can raise it. + private readonly BudgetLedger ledger = BudgetLedger.For("Gröbner", Default, BudgetRole.Allowance); /// /// Charges one unit of work and answers whether to carry on. Called where the diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs index e0b1bb205..6dc06fce4 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs @@ -120,7 +120,9 @@ internal static Set Solve(Entity equation, Variable x) // budget of its own is what bounded the fast path and left the fall-through // unbounded, so the same `Solve` finished or did not depending on which internal // path accepted it. https://github.com/asc-community/AngouriMath/issues/896 - var ledger = BudgetLedger.For("SolveSystem", SystemSolveBudget); + // An allowance: a set MathS.Settings.Budget replaces SystemSolveBudget, which is + // what the exceptions below mean by "raise MathS.Settings.Budget to allow more". + var ledger = BudgetLedger.For("SolveSystem", SystemSolveBudget, BudgetRole.Allowance); try { var variables = new Variable[vars.Length]; diff --git a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs index 0659be336..746763348 100644 --- a/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs +++ b/Sources/AngouriMath/Functions/Simplification/Patterns/Patterns.ExpandFactorize.cs @@ -201,8 +201,10 @@ internal static Entity PerfectSquareRules(Entity x) /// What the rewrite graph may spend proving a cross term equal to its candidate: two /// thousand steps and fifty milliseconds. On the corpus the safe ceiling saturates a /// term of this size in under a millisecond and a few dozen e-nodes, so this is a lid - /// against the stall family rather than a working allowance. + /// against the stall family rather than a working allowance, and is declared as one: a set + /// can only tighten it. /// https://github.com/asc-community/AngouriMath/issues/1200 + /// https://github.com/asc-community/AngouriMath/issues/2454 /// [ConstantField] private static readonly WorkBudget PerfectSquareProofBudget = new() { Steps = 2_000, Time = TimeSpan.FromMilliseconds(50) }; diff --git a/Sources/Tests/UnitTests/Core/Budgets/BudgetRoleTest.cs b/Sources/Tests/UnitTests/Core/Budgets/BudgetRoleTest.cs new file mode 100644 index 000000000..6f8fb8c7a --- /dev/null +++ b/Sources/Tests/UnitTests/Core/Budgets/BudgetRoleTest.cs @@ -0,0 +1,146 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System; +using System.Linq; +using AngouriMath.Core.Budgets; +using AngouriMath.Core.Transformations; +using AngouriMath.Extensions; +using AngouriMath.Functions; +using AngouriMath.Functions.Algebra; +using Xunit; + +namespace AngouriMath.Tests.Core.Budgets +{ + /// + /// What a set does to each kind of budget: it replaces + /// an allowance, and it can only tighten a lid or a budget passed as an argument. + /// https://github.com/asc-community/AngouriMath/issues/2454 + /// + [Trait("Area", "Core")] + public sealed class BudgetRoleTest + { + private static readonly WorkBudget Lid + = new() { Steps = 2_000, Time = TimeSpan.FromMilliseconds(50) }; + + [Fact] + public void UnsetLeavesEveryRoleOnItsOwn() + { + Assert.False(MathS.Settings.Budget.IsOverriden); + foreach (var role in new[] { BudgetRole.Lid, BudgetRole.Allowance, BudgetRole.Argument }) + Assert.Equal(Lid, BudgetLedger.Limits(Lid, role)); + } + + [Fact] + public void UnlimitedDoesNotLiftALid() + { + using var _ = MathS.Settings.Budget.Set(WorkBudget.Unlimited); + Assert.Equal(Lid, BudgetLedger.Limits(Lid, BudgetRole.Lid)); + } + + [Fact] + public void ALidTakesTheSmallerLimitOnEachAxis() + { + using (MathS.Settings.Budget.Set(new WorkBudget { Steps = 1 })) + Assert.Equal(Lid with { Steps = 1 }, BudgetLedger.Limits(Lid, BudgetRole.Lid)); + using (MathS.Settings.Budget.Set(new WorkBudget { Steps = 1_000_000 })) + Assert.Equal(Lid, BudgetLedger.Limits(Lid, BudgetRole.Lid)); + using (MathS.Settings.Budget.Set(new WorkBudget { Time = TimeSpan.FromMilliseconds(1) })) + Assert.Equal(Lid with { Time = TimeSpan.FromMilliseconds(1) }, BudgetLedger.Limits(Lid, BudgetRole.Lid)); + } + + /// + /// What the solver's refusal tells a caller to do -- "raise MathS.Settings.Budget to allow + /// more" -- has to work, so an allowance is replaced, not capped. + /// + [Fact] + public void ARaisedBudgetRaisesAnAllowance() + { + var raised = new WorkBudget { Steps = 100_000 }; + using var _ = MathS.Settings.Budget.Set(raised); + Assert.Equal(raised, BudgetLedger.Limits(EquationSolver.SystemSolveBudget, BudgetRole.Allowance)); + } + + [Fact] + public void AnArgumentIsCappedByTheSetBudget() + { + var argument = new WorkBudget { Steps = 1_000, Time = TimeSpan.FromSeconds(1) }; + using (MathS.Settings.Budget.Set(WorkBudget.Unlimited)) + Assert.Equal(argument, BudgetLedger.Limits(argument, BudgetRole.Argument)); + using (MathS.Settings.Budget.Set(new WorkBudget { Steps = 5 })) + Assert.Equal(argument with { Steps = 5 }, BudgetLedger.Limits(argument, BudgetRole.Argument)); + } + + /// + /// The perfect-square proof under an unlimited setting keeps the lid its caller gave it. + /// It used to run to saturation; ten steps do not reach the proof it would find. + /// + [Fact] + public void TheProofKeepsItsLidUnderUnlimited() + { + using var _ = MathS.Settings.Budget.Set(WorkBudget.Unlimited); + using var recording = BudgetRecording.Start(); + var proved = Saturation.ProvesEqual("2 * sqrt(2) * sqrt(3)".ToEntity(), "2 * sqrt(6)".ToEntity(), + Saturation.SafeRules, new WorkBudget { Steps = 10, Time = TimeSpan.FromSeconds(30) }); + Assert.False(proved); + Assert.Equal("steps", Assert.Single(recording.Outcomes).Reason); + } + + /// + /// A tight setting tightens the proof, and the collapse still answers: an unproved cross + /// term falls back to the symbolic test, so the proof stopping early costs time, not the + /// answer. + /// + [Fact] + public void ATightSettingTightensTheProofAndTheCollapseStillAnswers() + { + using var _ = MathS.Settings.Budget.Set(new WorkBudget { Steps = 1 }); + using var recording = BudgetRecording.Start(); + Assert.Equal("(sqrt(3) + sqrt(2)) ^ 2".ToEntity(), + Patterns.CollapseToPerfectSquare("2 + 2 * sqrt(2) * sqrt(3) + 3".ToEntity())); + var proofs = recording.Outcomes.Where(outcome => outcome.Where == nameof(Saturation.ProvesEqual)).ToList(); + Assert.NotEmpty(proofs); + Assert.All(proofs, proof => Assert.True(proof.Steps <= 2)); + } + + /// + /// AreEqual with a budget of its own inside an unlimited scope spends that budget. + /// It used to run unlimited and prove the equality. + /// + [Fact] + public void AnExplicitBudgetHoldsInsideAnUnlimitedScope() + { + using var _ = MathS.Settings.Budget.Set(WorkBudget.Unlimited); + var answer = MathS.AreEqual("2 * sqrt(2) * sqrt(3)", "2 * sqrt(6)", new WorkBudget { Steps = 1 }); + Assert.Equal(EqualityVerdict.Unknown, answer.Verdict); + Assert.Equal("steps", answer.Budget.Reason); + } + + /// The rows of BREAKING-CHANGES.md, which were run unlimited on 2.7.0. + [Theory] + [InlineData("x * 2 * 3 + x", "x * 2 * 3 + x", "6 * x + x")] + [InlineData("a * b + b * a", "a * b + b * a", "a * b + a * b")] + public void TheGraphSpendsItsArgumentInsideAnUnlimitedScope(string input, string saturated, string canonical) + { + var budget = new WorkBudget { Steps = 5, Time = TimeSpan.FromSeconds(30) }; + using var _ = MathS.Settings.Budget.Set(WorkBudget.Unlimited); + Assert.Equal(saturated.ToEntity(), + Transformation.EqualitySaturation(budget, AngouriMath.Core.CostModel.Default).ApplyOrKeep(input.ToEntity())); + Assert.Equal(canonical.ToEntity(), + Transformation.CanonicalizationOverGraph(budget).ApplyOrKeep(input.ToEntity())); + } + + [Fact] + public void ATighterScopeCapsAnExplicitBudget() + { + using var _ = MathS.Settings.Budget.Set(new WorkBudget { Steps = 1 }); + var answer = MathS.AreEqual("2 * sqrt(2) * sqrt(3)", "2 * sqrt(6)", new WorkBudget { Steps = 1_000_000 }); + Assert.Equal(EqualityVerdict.Unknown, answer.Verdict); + Assert.Equal("steps", answer.Budget.Reason); + } + } +} diff --git a/Sources/Tests/UnitTests/Core/Transformations/ConstantFoldTest.cs b/Sources/Tests/UnitTests/Core/Transformations/ConstantFoldTest.cs index 1cd4fa83d..b9a0dc797 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/ConstantFoldTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/ConstantFoldTest.cs @@ -68,7 +68,7 @@ private static (bool Saturated, int Nodes, Entity? Extracted) Saturate(string so { var graph = new EGraph(); var root = graph.AddEntity(source.ToEntity()); - var ledger = BudgetLedger.For(nameof(ConstantFoldTest), budget); + var ledger = BudgetLedger.For(nameof(ConstantFoldTest), budget, BudgetRole.Argument); var saturated = Saturation.Run(graph, Saturation.RulesUpTo(RewriteRuleGrowth.Rearranges), ledger, n => n.Complexity); return (saturated, graph.NodeCount, graph.Extract(root, n => n.Complexity)); } diff --git a/Sources/Tests/UnitTests/Core/Transformations/ProvidedInAnEClassTest.cs b/Sources/Tests/UnitTests/Core/Transformations/ProvidedInAnEClassTest.cs index 4b35f391e..6f9db6f3b 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/ProvidedInAnEClassTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/ProvidedInAnEClassTest.cs @@ -30,7 +30,7 @@ public sealed class ProvidedInAnEClassTest var graph = new EGraph(); var root = graph.AddEntity(source.ToEntity()); var ledger = BudgetLedger.For(nameof(ProvidedInAnEClassTest), - new WorkBudget { Steps = 200_000, Time = TimeSpan.FromSeconds(20) }); + new WorkBudget { Steps = 200_000, Time = TimeSpan.FromSeconds(20) }, BudgetRole.Argument); Saturation.Run(graph, Saturation.RulesUpTo(ceiling), ledger, node => node.Complexity); return graph.Extract(root, node => node.Complexity); diff --git a/Sources/Tests/UnitTests/Core/Transformations/RunawayBreadthTest.cs b/Sources/Tests/UnitTests/Core/Transformations/RunawayBreadthTest.cs index 51ea0512f..bb9080431 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/RunawayBreadthTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/RunawayBreadthTest.cs @@ -77,7 +77,7 @@ private static (bool Saturated, int Nodes, string Extracted) Saturate(string sou { var graph = new EGraph(); var root = graph.AddEntity(source.ToEntity()); - var ledger = BudgetLedger.For(nameof(RunawayBreadthTest), Quick); + var ledger = BudgetLedger.For(nameof(RunawayBreadthTest), Quick, BudgetRole.Argument); var saturated = Saturation.Run(graph, rules, ledger, node => node.Complexity); return (saturated, graph.NodeCount, graph.Extract(root, node => node.Complexity)?.Stringize() ?? "(nothing)"); } diff --git a/Sources/Tests/UnitTests/Core/Transformations/SaturationAblationTest.cs b/Sources/Tests/UnitTests/Core/Transformations/SaturationAblationTest.cs index 118088bc7..10d7017e7 100644 --- a/Sources/Tests/UnitTests/Core/Transformations/SaturationAblationTest.cs +++ b/Sources/Tests/UnitTests/Core/Transformations/SaturationAblationTest.cs @@ -91,7 +91,7 @@ private static (bool Saturated, int Nodes) Saturate(string source, IReadOnlyList { var graph = new EGraph(); graph.AddEntity(source.ToEntity()); - var ledger = BudgetLedger.For(nameof(SaturationAblationTest), Quick); + var ledger = BudgetLedger.For(nameof(SaturationAblationTest), Quick, BudgetRole.Argument); var saturated = Saturation.Run(graph, rules, ledger, node => node.Complexity); return (saturated, graph.NodeCount); }