Skip to content

Commit f439c0e

Browse files
Ask the e-graph whether two expressions are equal: MathS.AreEqual (#2447)
* Ask the e-graph whether two expressions are equal MathS.AreEqual(left, right, budget, weakest = Sound, domain = Complex) answers Equal, NotEqual or Unknown. - NotEqual only with a witness: a point where the library's evaluation and an enclosure of every subexpression say both sides are defined, and enclosures of the two values are disjoint (doubles, then fifty digits). The point is looked for first, so no proof is reported over a point that refutes it. - Equal only when both sides land in one e-class. Under weakest = Sound only rules declaring Sound fire; under SoundUnderAssumptions the conditional rules and the canonical-form pass join, the answer names those that merged a class, and a proof the sound rules also make is reported as Sound. - Bounded in steps: a point tried, a match attempt, a node grown. Saturation.Run reports each rule whose application merged two classes. Part of #1251. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Reorder a sum or a product as a sound step when asking for equality The e-graph neither sorts a commutative pair nor flattens a nested chain, and the pass that does, CanonicalOrderExact, also groups like terms and so declares SoundUnderAssumptions. x + y = y + x was therefore Unknown to a caller asking for an unconditional proof. Two oracle-only steps, declared Sound, flatten a Sumf chain or a Mulf chain (differences and quotients stay operands) and rebuild it in EntityOrder.Canonical order, grouping nothing. Commutativity and associativity hold for numbers with no side condition, and a name is a number of the domain asked. A chain is left alone where an operand holds a matrix, a transpose, a set, a statement, a lambda or an application, or where a link is read over a narrower codomain; a question with any matrix or matrix operation in it gets neither step, since a name beside a transpose may be a matrix (#2147). Simplify and every rule set are untouched. Records the public surface #1251 adds in PublicApi.txt, and points CanonicalForm.md's "not a decision procedure for equality" at MathS.AreEqual. Part of #1251. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Offer MathS.AreEqual as a catalogue entry and an MCP tool are_equal joins Core/Operations/Catalogue.cs: an operation offered by name is an entry there. The MCP server offers it as am_are_equal: left and right as strings, a step budget (default 20000), domain (complex or real) and the weakest soundness (sound or sound_under_assumptions). The answer states the verdict, the justification, the assumptions, the witness and what the budget did; unknown is "inconclusive" with equal null, never false. The description sets it apart from am_verify_equal, which may answer from sampling and believes a simplification. The terminal lists it in OneShot.notOffered: its one-shot commands are AngouriMathCLI's, and adding commands is #1019 item 16's question. Part of #1251. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5ec47ae commit f439c0e

15 files changed

Lines changed: 1178 additions & 8 deletions

File tree

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,3 +1093,6 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
10931093

10941094
[AngouriMath/Functions/Simplification/ProductsOfMatrices.cs]
10951095
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
1096+
1097+
[AngouriMath/Convenience/MathS.Equality.cs]
1098+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 AngouriMath.Core;
9+
using AngouriMath.Core.Budgets;
10+
using AngouriMath.Core.Transformations;
11+
12+
namespace AngouriMath
13+
{
14+
partial class MathS
15+
{
16+
/// <summary>
17+
/// Whether <paramref name="left"/> and <paramref name="right"/> are equal: proved by the
18+
/// rewrite rules over one e-graph, refuted at a point, or neither within
19+
/// <paramref name="budget"/>.
20+
/// </summary>
21+
/// <param name="left">One side.</param>
22+
/// <param name="right">The other.</param>
23+
/// <param name="budget">
24+
/// What the question may spend. <see cref="WorkBudget.Steps"/> counts work -- a point
25+
/// tried, a rule's match attempt, a node the graph grows by -- so the same question under
26+
/// the same steps gets the same answer on any machine; a <see cref="WorkBudget.Time"/>
27+
/// makes it depend on the machine, and <see cref="EqualityAnswer.Budget"/> says when it
28+
/// did. <see cref="Settings.Budget"/>, where set, overrides this.
29+
/// </param>
30+
/// <param name="weakest">
31+
/// The weakest soundness a proof may rest on. <see cref="Soundness.Sound"/>, the default,
32+
/// admits only rules that hold with no side condition -- reordering the terms of a sum or
33+
/// the factors of a product among them -- and a proof that needs more is
34+
/// <see cref="EqualityVerdict.Unknown"/>. <see cref="Soundness.SoundUnderAssumptions"/>
35+
/// admits the conditional rules and the canonical-form pass as well, and names those it
36+
/// used in <see cref="EqualityAnswer.Assumptions"/>.
37+
/// </param>
38+
/// <param name="domain">
39+
/// <see cref="Domain.Complex"/>, the default, or <see cref="Domain.Real"/>: where the
40+
/// variables range, and so where a witness may be looked for. A name is a number here, so
41+
/// <c>a * b</c> and <c>b * a</c> are equal; a question with a matrix or a matrix operation
42+
/// anywhere in it has no product reordered, since a name in it may stand for a matrix.
43+
/// </param>
44+
/// <returns>
45+
/// <see cref="EqualityVerdict.Equal"/> only with a proof;
46+
/// <see cref="EqualityVerdict.NotEqual"/> only with a point at which both sides are
47+
/// defined and enclosures of their values are disjoint;
48+
/// <see cref="EqualityVerdict.Unknown"/> otherwise -- which is not "unequal".
49+
/// </returns>
50+
/// <remarks>
51+
/// <para>
52+
/// Not <c>(left - right).Simplify() == 0</c>. A simplification picks one representative
53+
/// per input by cost, and two equal inputs can settle on different ones; a graph holding
54+
/// both merges their classes whether or not an extraction would agree. And a
55+
/// simplification has no third answer: a difference that does not simplify to zero is
56+
/// read as "unequal" by whoever compares it.
57+
/// </para>
58+
/// <para>
59+
/// The rules are the safe ceiling's, which do not enlarge; zero-equivalence is
60+
/// undecidable over this language, so <see cref="EqualityVerdict.Unknown"/> is an
61+
/// answer this gives often and honestly.
62+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1251">#1251</a>
63+
/// </para>
64+
/// </remarks>
65+
public static EqualityAnswer AreEqual(Entity left, Entity right, WorkBudget budget,
66+
Soundness weakest = Soundness.Sound, Domain domain = Domain.Complex)
67+
=> EqualityOracle.Ask(left, right, budget, weakest, domain);
68+
}
69+
}

‎Sources/AngouriMath/Core/Operations/Catalogue.cs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.Linq;
11+
using AngouriMath.Core.Budgets;
1112
using AngouriMath.Core.Transformations;
1213
using static AngouriMath.Entity;
1314
using static AngouriMath.Entity.Number;
@@ -157,6 +158,15 @@ internal static class Catalogue
157158
: MathS.Series.Taylor(e, degree + 1, (variable, around)),
158159
TheVariable, new Parameter("degree", "The highest power kept."), new Parameter("around", "The point, or none for zero."));
159160

161+
/// <summary><see cref="MathS.AreEqual(Entity, Entity, WorkBudget, Soundness, Domain)"/>.</summary>
162+
public static Query<Func<Entity, Entity, WorkBudget, Soundness, Domain, EqualityAnswer>> AreEqual { get; } = new(
163+
"are_equal", "Decides whether two expressions are equal: proved by rewrite rules, refuted at a point where both are defined, or neither within the budget.",
164+
static (left, right, budget, weakest, domain) => MathS.AreEqual(left, right, budget, weakest, domain),
165+
new Parameter("other", "The expression on the right."),
166+
new Parameter("budget", "What the question may spend, in steps for an answer that does not depend on the machine."),
167+
new Parameter("weakest", "The weakest soundness a proof may rest on: sound, or sound under assumptions."),
168+
new Parameter("domain", "Where the variables range: the complex or the real numbers."));
169+
160170
// ---------------------------------------------------------------- queries on a number
161171

162172
/// <summary><see cref="MathS.ToBaseN(Real, int)"/>.</summary>
@@ -244,7 +254,7 @@ internal static class Catalogue
244254
public static IReadOnlyList<Operation> All { get; } =
245255
[
246256
Simplify, Normalize, Expand, Factorize, Differentiate, Integrate, Limit, Substitute,
247-
Solve, SolveSystem, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, TruthTable, SatisfyingAssignments, Series,
257+
Solve, SolveSystem, Evaluate, EvaluateNumerically, Variables, Alternatives, Latex, Sympy, TruthTable, SatisfyingAssignments, Series, AreEqual,
248258
ToBase, FromBase,
249259
Determinant, Inverse, Transpose, Rank, ReducedRowEchelonForm, Trace, TensorProduct,
250260
PrimeFactorization, Totient, GreatestCommonDivisor, DivisorCount, IsPrime,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.Core.Budgets;
12+
13+
namespace AngouriMath.Core.Transformations
14+
{
15+
/// <summary>What <see cref="MathS.AreEqual"/> could establish about two expressions.</summary>
16+
public enum EqualityVerdict
17+
{
18+
/// <summary>
19+
/// Neither shown: no proof within the budget and no point at which the two differ. Says
20+
/// nothing about whether they are equal.
21+
/// </summary>
22+
Unknown,
23+
24+
/// <summary>Proved equal by rewrite rules; see <see cref="EqualityAnswer.Justification"/>.</summary>
25+
Equal,
26+
27+
/// <summary>
28+
/// Shown to differ at <see cref="EqualityAnswer.Witness"/>, where both are defined and
29+
/// enclosures of their values are disjoint. Never a failure to prove equality.
30+
/// </summary>
31+
NotEqual,
32+
}
33+
34+
/// <summary>
35+
/// The answer of <see cref="MathS.AreEqual"/>: a verdict, what it rests on, and what it cost.
36+
/// </summary>
37+
/// <remarks>
38+
/// <b>What <see cref="EqualityVerdict.Equal"/> claims.</b> At every assignment of the free
39+
/// variables in the domain asked at which both sides have a value, the values agree, provided
40+
/// that each rewrite the proof used holds as its <see cref="Soundness"/> declares. A
41+
/// <see cref="Soundness.Sound"/> justification rests on nothing else. A
42+
/// <see cref="Soundness.SoundUnderAssumptions"/> one rests in addition on the side
43+
/// conditions of the steps named in <see cref="Assumptions"/>, which are not data the library
44+
/// can check: a caller who needs an unconditional answer asks for
45+
/// <see cref="Soundness.Sound"/> and gets <see cref="EqualityVerdict.Unknown"/> instead.
46+
/// </remarks>
47+
public sealed record EqualityAnswer
48+
{
49+
internal EqualityAnswer(EqualityVerdict verdict, Soundness? justification,
50+
IReadOnlyList<string> assumptions, IReadOnlyDictionary<Entity.Variable, Entity>? witness,
51+
BudgetOutcome budget)
52+
{
53+
Verdict = verdict;
54+
Justification = justification;
55+
Assumptions = assumptions;
56+
Witness = witness;
57+
Budget = budget;
58+
}
59+
60+
/// <summary>Equal, not equal, or not shown either way.</summary>
61+
public EqualityVerdict Verdict { get; }
62+
63+
/// <summary>
64+
/// For <see cref="EqualityVerdict.Equal"/>, the weakest soundness among the steps the
65+
/// proof may rest on. <see langword="null"/> for the other verdicts.
66+
/// </summary>
67+
public Soundness? Justification { get; }
68+
69+
/// <summary>
70+
/// The conditional steps — rules declaring <see cref="Soundness.SoundUnderAssumptions"/>,
71+
/// and the canonical-form pass — that joined two classes while the proof was sought: a
72+
/// superset of those it needs. Empty for a <see cref="Soundness.Sound"/> proof.
73+
/// </summary>
74+
public IReadOnlyList<string> Assumptions { get; }
75+
76+
/// <summary>
77+
/// For <see cref="EqualityVerdict.NotEqual"/>, the point at which the two sides differ:
78+
/// a value for each free variable. Empty where neither side has one.
79+
/// </summary>
80+
public IReadOnlyDictionary<Entity.Variable, Entity>? Witness { get; }
81+
82+
/// <summary>What the question spent, and which ceiling, if any, stopped it.</summary>
83+
public BudgetOutcome Budget { get; }
84+
85+
/// <inheritdoc/>
86+
public override string ToString()
87+
=> Verdict switch
88+
{
89+
EqualityVerdict.Equal when Assumptions.Count == 0 => $"Equal ({Justification})",
90+
EqualityVerdict.Equal => $"Equal ({Justification}: {string.Join(", ", Assumptions)})",
91+
EqualityVerdict.NotEqual => "NotEqual at "
92+
+ (Witness!.Count == 0 ? "the constants" : string.Join(", ", Witness.Select(p => $"{p.Key} = {p.Value}"))),
93+
_ => $"Unknown ({Budget})",
94+
};
95+
}
96+
}

0 commit comments

Comments
 (0)