From 93000f36f8ef67aa17ad596755ac2dd464024de1 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 15 Nov 2026 21:18:21 +0000 Subject: [PATCH] An indexed union or intersection binds its index FreeVariables of union(A_k, k in I) and intersection(A_k, k in I) listed k, though the family binds it as a quantifier does and Substitute already left it alone. It is now bound throughout the family and the index set. DomainCondition is the other binder-aware question, and the same sweep found three leaks of a bound name into it: the indexed family's condition on k is now required over the index set, as a sum's is over its range; a set builder's condition mentioned the placeholder %1 its published predicate is renamed to (so ASetBuildersOwnConditionIsMembership held vacuously), and a lambda's its parameter. Both are dropped, as the set builder's own name already was meant to be. A limit's condition is left as it was, as documented. Fixes #2458 Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 24 +++++++++++ .../Core/Entity/Entity.Definition.cs | 6 +++ .../Core/Entity/Omni/Entity.Omni.Classes.cs | 9 +++- .../Evaluation/Evaluation.Definition.cs | 17 ++++++-- .../UnitTests/Common/FreeVariablesTest.cs | 25 +++++++++++ .../UnitTests/Core/BinderConditionTest.cs | 41 +++++++++++++++++++ 6 files changed, 117 insertions(+), 5 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 0f882f264..3c9993532 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -167,6 +167,30 @@ returned as all of them. `MathS.Equations("y^2 - 2", "x^2 + (P - Q y) * x - 3"). --- +### 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)` +listed `k`, though the family binds it as a quantifier does and `Substitute` already left it alone. `DomainCondition` left +the family's condition on `k` free beside it; it now holds over the index set, as a sum's does over its range. The same +condition of a set builder mentioned the name its predicate is renamed to for traversal, `%1`, and of a lambda its +parameter; both are where the set's members or the function's arguments lie, not where the expression is defined, and are +dropped ([#2458](https://github.com/asc-community/AngouriMath/issues/2458)). + +| Input | Was (2.7.0) | Now | +|---|---|---| +| `"union({2*pi*k + y}, k in ZZ)".ToEntity().FreeVariables` | `k, pi, y` | `pi, y` | +| `"intersection({k, y}, k in ZZ)".ToEntity().FreeVariables` | `k, y` | `y` | +| `"union({k*y}, k in {k, n})".ToEntity().FreeVariables` | `k, n, y` | `y, n` | +| `"union({1/k}, k in ZZ)".ToEntity().DomainCondition` | `not k = 0` | `False` | +| `"union({1/k}, k in ZZ+)".ToEntity().DomainCondition` | `not k = 0` | `True` | +| `"{ k : 1/k > y }".ToEntity().DomainCondition` | `1 / %1 in RR and y in RR and not %1 = 0` | `y in RR` | +| `"x in { k : k > y }".ToEntity().DomainCondition` | `%1 in RR and y in RR` | `y in RR` | +| `"lambda(k, 1/(k - y))".ToEntity().DomainCondition` | `not k - y = 0` | `True` | + +`Vars` and `VarsAndConsts` still list the index, which occurs. + +--- + ### A system row that is not a solution declines the system **Silent: a wrong row is now no answer.** With `P + Q sqrt(2) = (1 + sqrt(2))^70`, the coefficient `P - Q y` is diff --git a/Sources/AngouriMath/Core/Entity/Entity.Definition.cs b/Sources/AngouriMath/Core/Entity/Entity.Definition.cs index ff904fe04..7a0edf6f3 100644 --- a/Sources/AngouriMath/Core/Entity/Entity.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Entity.Definition.cs @@ -724,6 +724,12 @@ Limitf limit // https://github.com/asc-community/AngouriMath/issues/1409 Quantifier quantifier => BoundBy(quantifier.Var, quantifier.Body, quantifier.Over), + // An indexed union or intersection binds its index as a quantifier does, and + // for the reason its membership is one: x in union(A_k, k in I) is + // exists k in I : x in A_k. union({ 2 pi k + y }, k in ZZ) is a function of y. + // https://github.com/asc-community/AngouriMath/issues/2458 + Set.IndexedSetOperation indexed + => BoundBy(indexed.Var, indexed.Body, indexed.Over), _ => new HashSet(@this.DirectChildren.SelectMany(c => c.FreeVariables)) } , diff --git a/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs b/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs index c9a41d0e2..f28f20a2e 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs @@ -517,7 +517,14 @@ internal Entity New(Entity var, Entity predicate) // https://github.com/asc-community/AngouriMath/issues/891 /// protected override Entity[] InitDirectChildren() => - new[] { Predicate.Substitute(Var, Variable.CreateTemp(Predicate.Vars)) }; + new[] { Predicate.Substitute(Var, Placeholder) }; + + /// + /// The name is renamed to in the predicate this set builder + /// publishes as its child, which is what a question asked of that child mentions + /// in place of . + /// + internal Variable Placeholder => Variable.CreateTemp(Predicate.Vars); /// /// The written parts, which is what a pattern has to be able to say. The rename diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs index 3b86ffdb2..cf8c6f31e 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs @@ -109,10 +109,13 @@ private static Entity WhereTheCaseItTakesIsDefined(Piecewise piecewise, System.F /// coefficient is a number that is not zero. A finite range of numbers is the conjunction /// over it. Anything else is kept as forall over the range: the set, the whole /// numbers from the lower bound to the upper, the interval between a definite integral's - /// limits. A set builder admits the members its predicate is defined at, so its own - /// condition on its name is part of whom it admits rather than of where the set is defined. - /// A limit does not need its body defined throughout anything, and is left as it was. + /// limits, the index set of an indexed union or intersection. A set builder admits the + /// members its predicate is defined at, so its own condition on its name is part of whom it + /// admits rather than of where the set is defined; a lambda's condition on its parameter is + /// where the function it denotes is defined, and both are dropped. A limit does not need its + /// body defined throughout anything, and is left as it was. /// https://github.com/asc-community/AngouriMath/issues/1632 + /// https://github.com/asc-community/AngouriMath/issues/2458 /// /// /// For a definite integral this is sufficient for a value and not necessary: an integral @@ -135,10 +138,16 @@ private static Entity ScopedToItsBinder(Entity binder, Entity condition) Argmaxf(_, var name, var over) => (name, over), Argminf(_, var name, var over) => (name, over), Quantifier quantifier => (quantifier.Var, quantifier.Over), + Set.IndexedSetOperation indexed => (indexed.Var, indexed.Over), Summationf(_, var index, var from, var to) => (index, WholeNumbersBetween(index, from, to)), Productf(_, var index, var from, var to) => (index, WholeNumbersBetween(index, from, to)), Integralf { Range: { } limits } integral => (integral.Var, Between(integral.Var, limits.from, limits.to)), - Set.ConditionalSet(var name, _) => (name, null), + // The condition is of the child the set builder publishes, in which its name is + // renamed: that placeholder is the name the conjuncts mention. + Set.ConditionalSet set => (set.Placeholder, null), + // A lambda, like a set builder, binds its parameter over no range: a condition on + // the parameter is where the function is defined, not where the lambda is. + Lambda(var parameter, _) => (parameter, null), _ => null }; if (scope is not var (bound, range)) diff --git a/Sources/Tests/UnitTests/Common/FreeVariablesTest.cs b/Sources/Tests/UnitTests/Common/FreeVariablesTest.cs index 0377dc2ca..b46713ee6 100644 --- a/Sources/Tests/UnitTests/Common/FreeVariablesTest.cs +++ b/Sources/Tests/UnitTests/Common/FreeVariablesTest.cs @@ -195,5 +195,30 @@ public void AnIndexOutsideItsBinderIsStillFree() => Assert.Equal( SeqVar("k", "n").OrderBy(v => v.Name), "sum(k, k, 1, n) + k".ToEntity().FreeVariables.OrderBy(v => v.Name)); + + /// + /// An indexed union or intersection binds its index as a quantifier does, throughout the + /// family and the index set: x in union(A_k, k in I) is exists k in I : x in A_k. + /// #2458 + /// + [Theory] + [InlineData("union({ 2*pi*k + y }, k in ZZ)", "pi y")] + [InlineData("intersection({ k, y }, k in ZZ)", "y")] + [InlineData("union([k; y], k in [0; n])", "n y")] + [InlineData("union({ k*y }, k in { k, n })", "n y")] + [InlineData("union({ 1/k }, k in ZZ)", "")] + public void Issue2458_AnIndexedFamilyBindsItsIndex(string exprRaw, string free) + => Assert.Equal( + free.Split(' ', System.StringSplitOptions.RemoveEmptyEntries), + exprRaw.ToEntity().FreeVariables.Select(v => v.Name).OrderBy(n => n, System.StringComparer.Ordinal)); + + /// Only inside the family that declares it, and it still occurs. + [Fact] + public void Issue2458_AnIndexOutsideItsFamilyIsStillFree() + { + var expr = "union({ 2*pi*k + y }, k in ZZ) \\/ { k }".ToEntity(); + Assert.Equal(new[] { "k", "pi", "y" }, expr.FreeVariables.Select(v => v.Name).OrderBy(n => n, System.StringComparer.Ordinal)); + Assert.Equal(new[] { "k", "y" }, "union({ 2*pi*k + y }, k in ZZ)".ToEntity().Vars.Select(v => v.Name).OrderBy(n => n, System.StringComparer.Ordinal)); + } } } \ No newline at end of file diff --git a/Sources/Tests/UnitTests/Core/BinderConditionTest.cs b/Sources/Tests/UnitTests/Core/BinderConditionTest.cs index 90b42daa7..107df29cc 100644 --- a/Sources/Tests/UnitTests/Core/BinderConditionTest.cs +++ b/Sources/Tests/UnitTests/Core/BinderConditionTest.cs @@ -129,5 +129,46 @@ public void AFiniteRangeIsTheConjunctionOverIt(string sum, string at, bool defin [Fact] public void ASetBuildersOwnConditionIsMembership() => Assert.DoesNotContain("{ w : 1/w > a }".ToEntity().DomainCondition.Vars, v => v.Name == "w"); + + /// + /// The set builder's condition is of the predicate it publishes, in which its name is + /// renamed, so the test above held on a condition that mentioned %1 instead: + /// 1 / %1 in RR and a in RR and not %1 = 0. No name of the builder's, written or + /// invented, is left. + /// #2458 + /// + [Theory] + [InlineData("{ w : 1/w > a }")] + [InlineData("{ w in ZZ : 1/w > a }")] + [InlineData("x in { w : w > a }")] + public void Issue2458_ASetBuilderLeavesNoPlaceholderInItsCondition(string exprRaw) + => Assert.Equal(new[] { "a" }, exprRaw.ToEntity().DomainCondition.Vars.Select(v => v.Name).ToArray()); + + /// + /// A lambda's condition on its parameter is where the function it denotes is defined, + /// and outside it that name is another one. + /// #2458 + /// + [Fact] + public void Issue2458_ALambdaLeavesNoParameterInItsCondition() + => Assert.DoesNotContain("lambda(k, 1/(k - y))".ToEntity().DomainCondition.Vars, v => v.Name == "k"); + + /// + /// An indexed union or intersection is defined where every set of the family is, as a + /// sum is where every term is: over the index set, not at a free name of the same spelling. + /// #2458 + /// + [Theory] + [InlineData("union({ 1/k }, k in ZZ)", "0", false)] + [InlineData("union({ 1/k }, k in ZZ+)", "0", true)] + [InlineData("intersection([-1/k; 1/k], k in ZZ+)", "0", true)] + [InlineData("union({ 1/(k - x) }, k in { 1, 2 })", "1", false)] + [InlineData("union({ 1/(k - x) }, k in { 1, 2 })", "3", true)] + public void Issue2458_AnIndexedFamilysConditionHoldsOverItsIndexSet(string family, string at, bool defined) + { + var condition = family.ToEntity().DomainCondition; + Assert.DoesNotContain(condition.FreeVariables, v => v.Name == "k"); + Assert.Equal(defined ? MathS.Boolean.True : MathS.Boolean.False, condition.Substitute("x", at.ToEntity()).Simplify()); + } } }