Skip to content

Commit 5ba95bc

Browse files
An indexed union or intersection binds its index (#2464)
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 <noreply@anthropic.com>
1 parent a622719 commit 5ba95bc

6 files changed

Lines changed: 117 additions & 5 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,30 @@ returned as all of them. `MathS.Equations("y^2 - 2", "x^2 + (P - Q y) * x - 3").
167167

168168
---
169169

170+
### An indexed union or intersection binds its index
171+
172+
**Silent: a bound name is no longer reported free.** `FreeVariables` of `union(A_k, k in I)` and `intersection(A_k, k in I)`
173+
listed `k`, though the family binds it as a quantifier does and `Substitute` already left it alone. `DomainCondition` left
174+
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
175+
condition of a set builder mentioned the name its predicate is renamed to for traversal, `%1`, and of a lambda its
176+
parameter; both are where the set's members or the function's arguments lie, not where the expression is defined, and are
177+
dropped ([#2458](https://github.com/asc-community/AngouriMath/issues/2458)).
178+
179+
| Input | Was (2.7.0) | Now |
180+
|---|---|---|
181+
| `"union({2*pi*k + y}, k in ZZ)".ToEntity().FreeVariables` | `k, pi, y` | `pi, y` |
182+
| `"intersection({k, y}, k in ZZ)".ToEntity().FreeVariables` | `k, y` | `y` |
183+
| `"union({k*y}, k in {k, n})".ToEntity().FreeVariables` | `k, n, y` | `y, n` |
184+
| `"union({1/k}, k in ZZ)".ToEntity().DomainCondition` | `not k = 0` | `False` |
185+
| `"union({1/k}, k in ZZ+)".ToEntity().DomainCondition` | `not k = 0` | `True` |
186+
| `"{ k : 1/k > y }".ToEntity().DomainCondition` | `1 / %1 in RR and y in RR and not %1 = 0` | `y in RR` |
187+
| `"x in { k : k > y }".ToEntity().DomainCondition` | `%1 in RR and y in RR` | `y in RR` |
188+
| `"lambda(k, 1/(k - y))".ToEntity().DomainCondition` | `not k - y = 0` | `True` |
189+
190+
`Vars` and `VarsAndConsts` still list the index, which occurs.
191+
192+
---
193+
170194
### A system row that is not a solution declines the system
171195

172196
**Silent: a wrong row is now no answer.** With `P + Q sqrt(2) = (1 + sqrt(2))^70`, the coefficient `P - Q y` is

‎Sources/AngouriMath/Core/Entity/Entity.Definition.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,12 @@ Limitf limit
724724
// https://github.com/asc-community/AngouriMath/issues/1409
725725
Quantifier quantifier
726726
=> BoundBy(quantifier.Var, quantifier.Body, quantifier.Over),
727+
// An indexed union or intersection binds its index as a quantifier does, and
728+
// for the reason its membership is one: x in union(A_k, k in I) is
729+
// exists k in I : x in A_k. union({ 2 pi k + y }, k in ZZ) is a function of y.
730+
// https://github.com/asc-community/AngouriMath/issues/2458
731+
Set.IndexedSetOperation indexed
732+
=> BoundBy(indexed.Var, indexed.Body, indexed.Over),
727733
_ => new HashSet<Variable>(@this.DirectChildren.SelectMany(c => c.FreeVariables))
728734
}
729735
,

‎Sources/AngouriMath/Core/Entity/Omni/Entity.Omni.Classes.cs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,14 @@ internal Entity New(Entity var, Entity predicate)
517517
// https://github.com/asc-community/AngouriMath/issues/891
518518
/// <inheritdoc/>
519519
protected override Entity[] InitDirectChildren() =>
520-
new[] { Predicate.Substitute(Var, Variable.CreateTemp(Predicate.Vars)) };
520+
new[] { Predicate.Substitute(Var, Placeholder) };
521+
522+
/// <summary>
523+
/// The name <see cref="Var"/> is renamed to in the predicate this set builder
524+
/// publishes as its child, which is what a question asked of that child mentions
525+
/// in place of <see cref="Var"/>.
526+
/// </summary>
527+
internal Variable Placeholder => Variable.CreateTemp(Predicate.Vars);
521528

522529
/// <summary>
523530
/// The written parts, which is what a pattern has to be able to say. The rename

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Definition.cs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ private static Entity WhereTheCaseItTakesIsDefined(Piecewise piecewise, System.F
109109
/// coefficient is a number that is not zero. A finite range of numbers is the conjunction
110110
/// over it. Anything else is kept as <c>forall</c> over the range: the set, the whole
111111
/// numbers from the lower bound to the upper, the interval between a definite integral's
112-
/// limits. A set builder admits the members its predicate is defined at, so its own
113-
/// condition on its name is part of whom it admits rather than of where the set is defined.
114-
/// A limit does not need its body defined throughout anything, and is left as it was.
112+
/// limits, the index set of an indexed union or intersection. A set builder admits the
113+
/// members its predicate is defined at, so its own condition on its name is part of whom it
114+
/// admits rather than of where the set is defined; a lambda's condition on its parameter is
115+
/// where the function it denotes is defined, and both are dropped. A limit does not need its
116+
/// body defined throughout anything, and is left as it was.
115117
/// https://github.com/asc-community/AngouriMath/issues/1632
118+
/// https://github.com/asc-community/AngouriMath/issues/2458
116119
/// </para>
117120
/// <para>
118121
/// 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)
135138
Argmaxf(_, var name, var over) => (name, over),
136139
Argminf(_, var name, var over) => (name, over),
137140
Quantifier quantifier => (quantifier.Var, quantifier.Over),
141+
Set.IndexedSetOperation indexed => (indexed.Var, indexed.Over),
138142
Summationf(_, var index, var from, var to) => (index, WholeNumbersBetween(index, from, to)),
139143
Productf(_, var index, var from, var to) => (index, WholeNumbersBetween(index, from, to)),
140144
Integralf { Range: { } limits } integral => (integral.Var, Between(integral.Var, limits.from, limits.to)),
141-
Set.ConditionalSet(var name, _) => (name, null),
145+
// The condition is of the child the set builder publishes, in which its name is
146+
// renamed: that placeholder is the name the conjuncts mention.
147+
Set.ConditionalSet set => (set.Placeholder, null),
148+
// A lambda, like a set builder, binds its parameter over no range: a condition on
149+
// the parameter is where the function is defined, not where the lambda is.
150+
Lambda(var parameter, _) => (parameter, null),
142151
_ => null
143152
};
144153
if (scope is not var (bound, range))

‎Sources/Tests/UnitTests/Common/FreeVariablesTest.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,30 @@ public void AnIndexOutsideItsBinderIsStillFree()
195195
=> Assert.Equal(
196196
SeqVar("k", "n").OrderBy(v => v.Name),
197197
"sum(k, k, 1, n) + k".ToEntity().FreeVariables.OrderBy(v => v.Name));
198+
199+
/// <summary>
200+
/// An indexed union or intersection binds its index as a quantifier does, throughout the
201+
/// family and the index set: <c>x in union(A_k, k in I)</c> is <c>exists k in I : x in A_k</c>.
202+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2458">#2458</a>
203+
/// </summary>
204+
[Theory]
205+
[InlineData("union({ 2*pi*k + y }, k in ZZ)", "pi y")]
206+
[InlineData("intersection({ k, y }, k in ZZ)", "y")]
207+
[InlineData("union([k; y], k in [0; n])", "n y")]
208+
[InlineData("union({ k*y }, k in { k, n })", "n y")]
209+
[InlineData("union({ 1/k }, k in ZZ)", "")]
210+
public void Issue2458_AnIndexedFamilyBindsItsIndex(string exprRaw, string free)
211+
=> Assert.Equal(
212+
free.Split(' ', System.StringSplitOptions.RemoveEmptyEntries),
213+
exprRaw.ToEntity().FreeVariables.Select(v => v.Name).OrderBy(n => n, System.StringComparer.Ordinal));
214+
215+
/// <summary>Only inside the family that declares it, and it still occurs.</summary>
216+
[Fact]
217+
public void Issue2458_AnIndexOutsideItsFamilyIsStillFree()
218+
{
219+
var expr = "union({ 2*pi*k + y }, k in ZZ) \\/ { k }".ToEntity();
220+
Assert.Equal(new[] { "k", "pi", "y" }, expr.FreeVariables.Select(v => v.Name).OrderBy(n => n, System.StringComparer.Ordinal));
221+
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));
222+
}
198223
}
199224
}

‎Sources/Tests/UnitTests/Core/BinderConditionTest.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,46 @@ public void AFiniteRangeIsTheConjunctionOverIt(string sum, string at, bool defin
129129
[Fact]
130130
public void ASetBuildersOwnConditionIsMembership()
131131
=> Assert.DoesNotContain("{ w : 1/w > a }".ToEntity().DomainCondition.Vars, v => v.Name == "w");
132+
133+
/// <summary>
134+
/// The set builder's condition is of the predicate it publishes, in which its name is
135+
/// renamed, so the test above held on a condition that mentioned <c>%1</c> instead:
136+
/// <c>1 / %1 in RR and a in RR and not %1 = 0</c>. No name of the builder's, written or
137+
/// invented, is left.
138+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2458">#2458</a>
139+
/// </summary>
140+
[Theory]
141+
[InlineData("{ w : 1/w > a }")]
142+
[InlineData("{ w in ZZ : 1/w > a }")]
143+
[InlineData("x in { w : w > a }")]
144+
public void Issue2458_ASetBuilderLeavesNoPlaceholderInItsCondition(string exprRaw)
145+
=> Assert.Equal(new[] { "a" }, exprRaw.ToEntity().DomainCondition.Vars.Select(v => v.Name).ToArray());
146+
147+
/// <summary>
148+
/// A lambda's condition on its parameter is where the function it denotes is defined,
149+
/// and outside it that name is another one.
150+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2458">#2458</a>
151+
/// </summary>
152+
[Fact]
153+
public void Issue2458_ALambdaLeavesNoParameterInItsCondition()
154+
=> Assert.DoesNotContain("lambda(k, 1/(k - y))".ToEntity().DomainCondition.Vars, v => v.Name == "k");
155+
156+
/// <summary>
157+
/// An indexed union or intersection is defined where every set of the family is, as a
158+
/// sum is where every term is: over the index set, not at a free name of the same spelling.
159+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2458">#2458</a>
160+
/// </summary>
161+
[Theory]
162+
[InlineData("union({ 1/k }, k in ZZ)", "0", false)]
163+
[InlineData("union({ 1/k }, k in ZZ+)", "0", true)]
164+
[InlineData("intersection([-1/k; 1/k], k in ZZ+)", "0", true)]
165+
[InlineData("union({ 1/(k - x) }, k in { 1, 2 })", "1", false)]
166+
[InlineData("union({ 1/(k - x) }, k in { 1, 2 })", "3", true)]
167+
public void Issue2458_AnIndexedFamilysConditionHoldsOverItsIndexSet(string family, string at, bool defined)
168+
{
169+
var condition = family.ToEntity().DomainCondition;
170+
Assert.DoesNotContain(condition.FreeVariables, v => v.Name == "k");
171+
Assert.Equal(defined ? MathS.Boolean.True : MathS.Boolean.False, condition.Substitute("x", at.ToEntity()).Simplify());
172+
}
132173
}
133174
}

0 commit comments

Comments
 (0)