Skip to content

Commit db33ae1

Browse files
A substitution under a binder neither captures the value nor passes over a body without the target (#1811)
Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 138f714 commit db33ae1

3 files changed

Lines changed: 146 additions & 77 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ after it
176176
|---|---|---|
177177
| `"(a*c + b*c*x)^(-3-2*p)*(f + g*x)*(a^2 + 2*a*b*x + b^2*x^2)^p".ToEntity().Integrate("x")` | `integral(...)`; an answer with no value on the unreleased master | the antiderivative |
178178

179+
### A value substituted under a binder is not captured by the name it binds
180+
181+
**Wrong answers fixed.** An integral, a sum, a product, a derivative, a limit, a set builder and the
182+
other binders renamed their bound name to a temporary, substituted, and renamed it back, so a value
183+
holding the bound name was captured by it: `integral(t + a, t, 0, 1)` with `a := t` became
184+
`integral(t + t, t, 0, 1)`, which is 1 where the integral is `t + 1/2`. Where the value holds the
185+
bound name, the binder now takes a fresh one, `t_1`, and keeps it, as a lambda already did. A
186+
substitution whose target is absent from the body no longer passes over it, which made a chain of
187+
nested integrals cost twice as much per level
188+
([#1808](https://github.com/asc-community/AngouriMath/issues/1808)).
189+
190+
| Input | Was (2.5.0) | Now |
191+
|---|---|---|
192+
| `new Integralf("t + a".ToEntity(), "t", (0, 1)).Substitute("a", "t")` | `integral(t + t, t, 0, 1)` | `integral(t_1 + t, t_1, 0, 1)` |
193+
| `new Summationf("k * a".ToEntity(), "k", 1, 3).Substitute("a", "k")` | `sum(k * k, k, 1, 3)`, 14 | `sum(k_1 * k, k_1, 1, 3)`, `6 k` |
194+
179195
### A power of a constant below `1e-50` is no longer simplified to zero
180196

181197
**Answers that were wrong.** Evaluation rounds a value within `1e-50` of an integer onto it, and

‎Sources/AngouriMath/Functions/Substitute.cs‎

Lines changed: 57 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -570,50 +570,63 @@ partial record Set
570570
{
571571
partial record ConditionalSet
572572
{
573-
// TODO: it might be optimized
574573
/// <inheritdoc/>
575574
public override Entity Substitute(Entity x, Entity value)
576575
{
577576
if (this == x)
578577
return value;
579-
var replacement = Variable.CreateTemp((x + value + Predicate + Var).Vars);
578+
var (postSubs, boundName) = SubstituteUnderABinder(Predicate, Var, x, value);
580579

581-
// { x | x > a } -> { temp_1 | temp_1 > a }
582-
var tempSubstituted = Predicate.Substitute(Var, replacement);
583-
584-
// a = 0 -> { temp_1 | temp_1 > a } -> { temp_1 | temp_1 > 0 }
585-
// x = 0 -> { temp_1 | temp_1 > a } -> { temp_1 | temp_1 > a }
586-
var subs = tempSubstituted.Substitute(x, value);
587-
588-
// { temp_1 | temp_1 > a } -> { x | x > a }
589-
var postSubs = subs.Substitute(replacement, Var);
590-
591-
return New(Var, postSubs);
580+
return New(boundName, postSubs);
592581
}
593582
}
594583
}
595584

585+
/// <summary>
586+
/// <paramref name="body"/>, bound over <paramref name="bound"/>, with <paramref name="x"/> replaced
587+
/// by <paramref name="value"/> where it occurs free, and the name the binder binds afterwards.
588+
/// </summary>
589+
/// <remarks>
590+
/// <para>
591+
/// Where <paramref name="value"/> holds the bound name, the binder takes a fresh one and keeps
592+
/// it, as <see cref="Lambda"/> does: renamed back after the substitution, the value's
593+
/// occurrence of the name was captured, and <c>integral(t + a, t, 0, 1)</c> with <c>a := t</c>
594+
/// became <c>integral(t + t, t, 0, 1)</c>, which is 1 where the integral is <c>t + 1/2</c>.
595+
/// </para>
596+
/// <para>
597+
/// The body is passed over only where it can change. A body without <paramref name="x"/>, or
598+
/// under a binder over <paramref name="x"/> itself, is returned as it is; where neither
599+
/// <paramref name="x"/> nor <paramref name="value"/> holds the bound name, one pass gives what a
600+
/// renaming would. Renaming it three times at every level, as every binder did, made a chain of
601+
/// nested integrals cost a multiple of itself per level
602+
/// (https://github.com/asc-community/AngouriMath/issues/1808).
603+
/// </para>
604+
/// </remarks>
605+
private static (Entity Body, Entity Bound) SubstituteUnderABinder(Entity body, Entity bound, Entity x, Entity value)
606+
{
607+
if (x == bound || !body.ContainsNode(x))
608+
return (body, bound);
609+
if (value.ContainsNode(bound))
610+
{
611+
var fresh = Variable.CreateUnique(body + bound + x + value, bound is Variable named ? named.Name : "t");
612+
return (body.Substitute(bound, fresh).Substitute(x, value), fresh);
613+
}
614+
if (!x.ContainsNode(bound))
615+
return (body.Substitute(x, value), bound);
616+
var replacement = Variable.CreateTemp((x + value + body + bound).Vars);
617+
return (body.Substitute(bound, replacement).Substitute(x, value).Substitute(replacement, bound), bound);
618+
}
619+
596620
partial record Integralf
597621
{
598-
// TODO: it might be optimized
599622
/// <inheritdoc/>
600623
public override Entity Substitute(Entity x, Entity value)
601624
{
602625
if (this == x)
603626
return value;
604-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
605-
606-
// integrate(x ^ 2 + a, x) -> integrate(temp_1 ^ 2 + a, temp_1)
607-
var tempSubstituted = Expression.Substitute(Var, replacement);
627+
var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
608628

609-
// a = 0 -> integrate(temp_1 ^ 2 + a, temp_1) -> integrate(temp_1 ^ 2 + 0, temp_1)
610-
// x = 0 -> integrate(temp_1 ^ 2 + a, temp_1) -> integrate(temp_1 ^ 2 + a, temp_1)
611-
var subs = tempSubstituted.Substitute(x, value);
612-
613-
// integrate(temp_1 ^ 2 + a, temp_1) -> integrate(x ^ 2 + a, temp_1)
614-
var postSubs = subs.Substitute(replacement, Var);
615-
616-
return New(postSubs, Var, Range is var (from, to) ? (from.Substitute(x, value), to.Substitute(x, value)) : null);
629+
return New(postSubs, boundName, Range is var (from, to) ? (from.Substitute(x, value), to.Substitute(x, value)) : null);
617630
}
618631
}
619632

@@ -629,11 +642,8 @@ public override Entity Substitute(Entity x, Entity value)
629642
{
630643
if (this == x)
631644
return value;
632-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
633-
var tempSubstituted = Expression.Substitute(Var, replacement);
634-
var subs = tempSubstituted.Substitute(x, value);
635-
var postSubs = subs.Substitute(replacement, Var);
636-
return New(postSubs, Var, From.Substitute(x, value), To.Substitute(x, value));
645+
var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
646+
return New(postSubs, boundName, From.Substitute(x, value), To.Substitute(x, value));
637647
}
638648
}
639649

@@ -645,9 +655,8 @@ public override Entity Substitute(Entity x, Entity value)
645655
{
646656
if (this == x)
647657
return value;
648-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
649-
var renamed = Expression.Substitute(Var, replacement).Substitute(x, value).Substitute(replacement, Var);
650-
return New(renamed, Var, Over.Substitute(x, value));
658+
var (renamed, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
659+
return New(renamed, boundName, Over.Substitute(x, value));
651660
}
652661
}
653662

@@ -659,9 +668,8 @@ public override Entity Substitute(Entity x, Entity value)
659668
{
660669
if (this == x)
661670
return value;
662-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
663-
var renamed = Expression.Substitute(Var, replacement).Substitute(x, value).Substitute(replacement, Var);
664-
return New(renamed, Var, Over.Substitute(x, value));
671+
var (renamed, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
672+
return New(renamed, boundName, Over.Substitute(x, value));
665673
}
666674
}
667675

@@ -672,9 +680,8 @@ public override Entity Substitute(Entity x, Entity value)
672680
{
673681
if (this == x)
674682
return value;
675-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
676-
var renamed = Expression.Substitute(Var, replacement).Substitute(x, value).Substitute(replacement, Var);
677-
return New(renamed, Var, Over.Substitute(x, value));
683+
var (renamed, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
684+
return New(renamed, boundName, Over.Substitute(x, value));
678685
}
679686
}
680687

@@ -685,9 +692,8 @@ public override Entity Substitute(Entity x, Entity value)
685692
{
686693
if (this == x)
687694
return value;
688-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
689-
var renamed = Expression.Substitute(Var, replacement).Substitute(x, value).Substitute(replacement, Var);
690-
return New(renamed, Var, Over.Substitute(x, value));
695+
var (renamed, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
696+
return New(renamed, boundName, Over.Substitute(x, value));
691697
}
692698
}
693699

@@ -698,9 +704,8 @@ public override Entity Substitute(Entity x, Entity value)
698704
{
699705
if (this == x)
700706
return value;
701-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
702-
var renamed = Expression.Substitute(Var, replacement).Substitute(x, value).Substitute(replacement, Var);
703-
return New(renamed, Var, Over.Substitute(x, value));
707+
var (renamed, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
708+
return New(renamed, boundName, Over.Substitute(x, value));
704709
}
705710
}
706711

@@ -711,61 +716,36 @@ public override Entity Substitute(Entity x, Entity value)
711716
{
712717
if (this == x)
713718
return value;
714-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
715-
var tempSubstituted = Expression.Substitute(Var, replacement);
716-
var subs = tempSubstituted.Substitute(x, value);
717-
var postSubs = subs.Substitute(replacement, Var);
718-
return New(postSubs, Var, From.Substitute(x, value), To.Substitute(x, value));
719+
var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
720+
return New(postSubs, boundName, From.Substitute(x, value), To.Substitute(x, value));
719721
}
720722
}
721723

722724
partial record Derivativef
723725
{
724-
// TODO: it might be optimized
725726
/// <inheritdoc/>
726727
public override Entity Substitute(Entity x, Entity value)
727728
{
728729
if (this == x)
729730
return value;
730-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
731-
732-
// derive(x ^ 2 + a, x) -> derive(temp_1 ^ 2 + a, temp_1)
733-
var tempSubstituted = Expression.Substitute(Var, replacement);
734-
735-
// a = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + 0, temp_1)
736-
// x = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + a, temp_1)
737-
var subs = tempSubstituted.Substitute(x, value);
731+
var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
738732

739-
// derive(temp_1 ^ 2 + a, temp_1) -> derive(x ^ 2 + a, temp_1)
740-
var postSubs = subs.Substitute(replacement, Var);
741-
742-
return New(postSubs, Var);
733+
return New(postSubs, boundName);
743734
}
744735
}
745736

746737
partial record Limitf
747738
{
748-
// TODO: it might be optimized
749739
/// <inheritdoc/>
750740
public override Entity Substitute(Entity x, Entity value)
751741
{
752742
if (this == x)
753743
return value;
754-
var replacement = Variable.CreateTemp((x + value + Expression + Var).Vars);
755-
756-
// derive(x ^ 2 + a, x) -> derive(temp_1 ^ 2 + a, temp_1)
757-
var tempSubstituted = Expression.Substitute(Var, replacement);
758-
759-
// a = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + 0, temp_1)
760-
// x = 0 -> derive(temp_1 ^ 2 + a, temp_1) -> derive(temp_1 ^ 2 + a, temp_1)
761-
var subs = tempSubstituted.Substitute(x, value);
762-
763-
// derive(temp_1 ^ 2 + a, temp_1) -> derive(x ^ 2 + a, temp_1)
764-
var postSubs = subs.Substitute(replacement, Var);
744+
var (postSubs, boundName) = SubstituteUnderABinder(Expression, Var, x, value);
765745

766746
var dst = Destination.Substitute(x, value);
767747

768-
return New(postSubs, Var, dst, ApproachFrom);
748+
return New(postSubs, boundName, dst, ApproachFrom);
769749
}
770750
}
771751

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.Extensions;
9+
using Xunit;
10+
using static AngouriMath.Entity;
11+
12+
namespace AngouriMath.Tests.Core
13+
{
14+
/// <summary>
15+
/// Substituting under a binder: what is bound is not reached, a value holding the bound name is
16+
/// not captured, and a body the target does not occur in is not traversed again at every level.
17+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1808">#1808</a>
18+
/// </summary>
19+
[Trait("Area", "Core")]
20+
public sealed class SubstitutionUnderABinderTest
21+
{
22+
private static Entity Nested(int depth, Entity upper)
23+
{
24+
if (depth == 0)
25+
return 1;
26+
var dummy = Variable.CreateVariableUnchecked($"u_{depth}");
27+
return new Integralf(Nested(depth - 1, dummy), dummy, (0, upper));
28+
}
29+
30+
/// <summary>
31+
/// One integral per level, the target in the outermost bound only. Renaming every body three
32+
/// times at every level made this take about twice as long per level, past a few minutes by
33+
/// twenty; it is linear in the depth now. The answer is the chain built with the replacement.
34+
/// </summary>
35+
[Fact]
36+
public void AChainOfIntegralsIsSubstitutedInItsOuterBoundOnly()
37+
{
38+
var target = Variable.CreateVariableUnchecked("target");
39+
var replacement = Variable.CreateVariableUnchecked("w");
40+
Assert.Equal(Nested(20, replacement), Nested(20, target).Substitute(target, replacement));
41+
}
42+
43+
[Fact]
44+
public void TheBoundNameIsNotReached()
45+
{
46+
var integral = new Integralf("t^2 + a".ToEntity(), "t", (0, "b".ToEntity()));
47+
Assert.Equal(integral, integral.Substitute("t", 5));
48+
Assert.Equal(integral, integral.Substitute("t^2".ToEntity(), 5));
49+
Assert.Equal(new Integralf("t^2 + 3".ToEntity(), "t", (0, "b".ToEntity())), integral.Substitute("a", 3));
50+
}
51+
52+
[Fact]
53+
public void AValueHoldingTheBoundNameIsNotCaptured()
54+
{
55+
var integral = new Integralf("t + a".ToEntity(), "t", (0, 1));
56+
var substituted = (Integralf)integral.Substitute("a", "t");
57+
Assert.NotEqual(Variable.CreateVariableUnchecked("t"), substituted.Var);
58+
Assert.Equal(1.5.ToNumber(), substituted.Substitute("t", 1).EvalNumerical());
59+
var sum = (Summationf)new Summationf("k * a".ToEntity(), "k", 1, 3).Substitute("a", "k");
60+
Assert.NotEqual(Variable.CreateVariableUnchecked("k"), sum.Var);
61+
Assert.Equal(12.ToNumber(), sum.Substitute("k", 2).EvalNumerical());
62+
}
63+
64+
[Fact]
65+
public void ASumIsSubstitutedOutsideItsIndex()
66+
{
67+
var sum = new Summationf("k * a".ToEntity(), "k", 1, "n".ToEntity());
68+
Assert.Equal(new Summationf("k * 2".ToEntity(), "k", 1, "n".ToEntity()), sum.Substitute("a", 2));
69+
Assert.Equal(new Summationf("k * a".ToEntity(), "k", 1, 4), sum.Substitute("n", 4));
70+
Assert.Equal(sum, sum.Substitute("k", 7));
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)