Skip to content

Commit 70ca79e

Browse files
Over the primes, a comparison settles past its root (#1590)
A comparison linear in p keeps one truth value past its root, so the primes below the last root are asked one by one; from there each comparison is the truth value it has, and what is left is decided by the modulus's prime factors and units as before. A comparison may stand anywhere in the statement, not only as a lower bound in the hypothesis: every prime is 2, or 1 or 3 modulo 4. Where a comparison with rational coefficients settles is found exactly. It was a decimal quotient, so it depended on the decimal precision: at 16 or 30 digits 2/3 rounds up, the root of 2/3 n - 2 came out below 3, and forall n in ZZ* : 2/3 n - 2 <= 0 was True, though it fails at 4. At the default 100 digits it was right. Part of #1409. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 2c160f0 commit 70ca79e

3 files changed

Lines changed: 128 additions & 58 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,10 +1310,14 @@ and every unit is the residue of infinitely many primes, by Dirichlet's theorem.
13101310
`forall p in PP : p > 3 implies 24 divides p^2 - 1` is `True` while the same from `p > 2` is `False`
13111311
at `3`, Sullivan and Mackey's Prob 6.7.5 and its erratum, and `exists p in PP : p > 10 and
13121312
p = 3 (mod 4)` is `True` ([#1409](https://github.com/asc-community/AngouriMath/issues/1409)).
1313+
A comparison keeps one truth value past its root, so the primes below the last root are asked
1314+
one by one, and a comparison may stand anywhere in the statement: every prime is `2`, or `1` or `3`
1315+
modulo `4`.
13131316

13141317
| Input | Was (2.5.0) | Now |
13151318
|---|---|---|
13161319
| `forall p in PP : p > 3 implies 24 divides p^2 - 1` | `UnhandledParseException` (left as written when `PP` arrived) | `True` |
1320+
| `forall p in PP : p = 1 (mod 4) or p = 3 (mod 4) or p = 2` | `UnhandledParseException` (left as written when `PP` arrived) | `True` |
13171321

13181322
### `atanh(tanh(a + b x))` under an integral is linear in `x`, and was an atom no rule read
13191323

‎Sources/AngouriMath/Functions/Boolean/Quantifiers.cs‎

Lines changed: 94 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,14 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body
310310
// the residue of infinitely many primes (Dirichlet), so a statement that repeats modulo
311311
// M is decided over the primes by M's own prime factors, one by one, and by the units
312312
// modulo M: p^2 - 1 is a multiple of 24 for every prime past 3, though not for 3 --
313-
// Sullivan and Mackey's Prob 6.7.5. https://github.com/asc-community/AngouriMath/issues/1409
314-
if (set is SpecialSet.Primes && kind != Kind.Unique && OverThePrimes(kind, x, body, isExact) is { } overThePrimes)
315-
{
316-
by = ("the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes", "Nat.setOf_prime_and_eq_mod_infinite");
313+
// Sullivan and Mackey's Prob 6.7.5. A comparison keeps one truth value past its root,
314+
// so the primes below the last root are asked one by one: every prime is 2, or 1 or 3
315+
// modulo 4. https://github.com/asc-community/AngouriMath/issues/1409
316+
if (set is SpecialSet.Primes && kind != Kind.Unique && OverThePrimes(kind, x, body, isExact) is var (overThePrimes, threshold))
317+
{
318+
by = (threshold.CompareTo(EInteger.FromInt32(2)) > 0
319+
? $"the primes below {threshold} one by one; from {threshold} the comparisons have settled, the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes"
320+
: "the primes past the modulus's prime factors are its units, and each unit is the residue of infinitely many primes", "Nat.setOf_prime_and_eq_mod_infinite");
317321
return overThePrimes;
318322
}
319323
if (set is FiniteSet finite)
@@ -969,22 +973,44 @@ private static (EInteger Period, EInteger Threshold)? Settled(Entity body, Varia
969973
any = true;
970974
continue;
971975
}
972-
if (LinearComparison(atom, x) is not (var slope, var rest) || rest.Evaled is not Real { IsNaN: false } intercept
973-
|| !slope.IsFinite)
976+
if (SettlesAt(atom, x) is not { } settlesAt)
974977
return null;
975978
any = true;
976-
// Flat, or against an infinity -- x < +oo -- it has one truth value throughout.
977-
if (slope.IsZero || !intercept.IsFinite)
978-
continue;
979-
// Past floor(-b / a) + 1 the sign of a x + b no longer changes.
980-
var root = intercept.EDecimal.Negate().Divide(slope.EDecimal, PeterO.Numbers.EContext.ForPrecision(64));
981-
var settlesAt = root.RoundToExponent(0, PeterO.Numbers.ERounding.Floor).ToEInteger().Add(EInteger.One);
982979
if (settlesAt.CompareTo(threshold) > 0)
983980
threshold = settlesAt;
984981
}
985982
return any ? (period, threshold) : null;
986983
}
987984

985+
/// <summary>
986+
/// The whole number from which a comparison linear in <paramref name="x"/>, with numbers
987+
/// for coefficients, keeps one truth value, or <see langword="null"/> for an atom that is
988+
/// not one: past its root the sign of <c>a x + b</c> no longer changes, so from
989+
/// <c>floor(-b / a) + 1</c> on. Zero for a comparison that never changes.
990+
/// </summary>
991+
private static EInteger? SettlesAt(Entity atom, Variable x)
992+
{
993+
if (LinearComparison(atom, x) is not (var slope, var rest) || rest.Evaled is not Real { IsNaN: false } intercept
994+
|| !slope.IsFinite)
995+
return null;
996+
// Flat, or against an infinity -- x < +oo -- it has one truth value throughout.
997+
if (slope.IsZero || !intercept.IsFinite)
998+
return EInteger.Zero;
999+
// Exactly where both are rational, so that where it settles does not depend on the
1000+
// decimal precision: at 16 or 30 digits 2/3 rounds up, which put the root of 2/3 n - 2
1001+
// below 3, and 3 was taken for a member past it.
1002+
if (slope is Rational { ERational: var a } && intercept is Rational { ERational: var b })
1003+
{
1004+
var root = b.Negate().Divide(a);
1005+
var floor = root.ToEInteger();
1006+
if (root.CompareTo(ERational.FromEInteger(floor)) < 0)
1007+
floor -= EInteger.One;
1008+
return floor.Add(EInteger.One);
1009+
}
1010+
var approximated = intercept.EDecimal.Negate().Divide(slope.EDecimal, EContext.ForPrecision(64));
1011+
return approximated.RoundToExponent(0, ERounding.Floor).ToEInteger().Add(EInteger.One);
1012+
}
1013+
9881014
/// <summary>
9891015
/// The statement decided at every member from <paramref name="first"/> up to, and not
9901016
/// including, <paramref name="past"/>.
@@ -1069,36 +1095,49 @@ private static Entity OneOfTheFirst(Variable x, EInteger from, int count, Entity
10691095
return any ?? Entity.Boolean.False;
10701096
}
10711097

1072-
10731098
/// <summary>
1074-
/// The period of the body in <paramref name="x"/> over the whole numbers, where it has
1075-
/// one: a divisibility by a whole number, or a congruence modulo one, of polynomials
1076-
/// with whole coefficients, joined by the connectives; the least common multiple of the
1077-
/// parts' periods.
1099+
/// The statement over the primes, where past a threshold it repeats modulo some <c>M</c>,
1100+
/// and the threshold, or <see langword="null"/> where what is left does not repeat or a
1101+
/// member does not decide it. A comparison linear in <paramref name="x"/> keeps one truth
1102+
/// value past its root, so the primes below the last root are asked one by one. Past it
1103+
/// the statement is its value at each prime factor of <c>M</c> there, and at each unit
1104+
/// modulo <c>M</c>, which stands for the infinitely many primes of its class there.
10781105
/// </summary>
1079-
/// <summary>
1080-
/// The statement over the primes, where past a lower bound on them it repeats modulo some
1081-
/// <c>M</c>: at each prime factor of <c>M</c> past the bound, and at each unit modulo
1082-
/// <c>M</c>, which stands for the infinitely many primes of its class. <see langword="null"/>
1083-
/// where the statement does not repeat, or a residue does not decide it.
1084-
/// </summary>
1085-
private static Entity? OverThePrimes(Kind kind, Variable x, Entity body, bool isExact)
1106+
private static (Entity Verdict, EInteger Threshold)? OverThePrimes(Kind kind, Variable x, Entity body, bool isExact)
10861107
{
1087-
// The primes asked about: those past a bound the hypothesis sets, or all of them.
1088-
var above = EInteger.One;
1089-
var claim = body;
1090-
if (kind == Kind.All && body is Impliesf(var assumption, var conclusion) && LowerBound(assumption, x) is { } bound)
1091-
(above, claim) = (bound, conclusion);
1092-
else if (kind == Kind.Some && body is Andf(var first, var second))
1093-
{
1094-
if (LowerBound(first, x) is { } firstBound)
1095-
(above, claim) = (firstBound, second);
1096-
else if (LowerBound(second, x) is { } secondBound)
1097-
(above, claim) = (secondBound, first);
1098-
}
1099-
if (!claim.ContainsNode(x) || Period(claim, x, (SpecialSet)MathS.Sets.Z) is not { } modulus
1100-
|| modulus.CompareTo(EInteger.FromInt32(LargestPeriod)) > 0)
1108+
// The comparisons, and the least number from which every one of them has settled.
1109+
var atoms = new List<Entity>();
1110+
CollectAtoms(body, atoms);
1111+
var two = EInteger.FromInt32(2);
1112+
var threshold = two;
1113+
var comparisons = new List<Entity>();
1114+
foreach (var atom in atoms)
1115+
if (atom.ContainsNode(x) && SettlesAt(atom, x) is { } settlesAt)
1116+
{
1117+
comparisons.Add(atom);
1118+
if (settlesAt.CompareTo(threshold) > 0)
1119+
threshold = settlesAt;
1120+
}
1121+
if (threshold.CompareTo(EInteger.FromInt32(LargestPrefix)) > 0)
11011122
return null;
1123+
// From the threshold on, each comparison is the truth value it has there.
1124+
var settled = new Dictionary<Entity, Entity>();
1125+
foreach (var comparison in comparisons)
1126+
{
1127+
if (comparison.Substitute(x, Integer.Create(threshold)).Evaled is not Entity.Boolean value)
1128+
return null;
1129+
settled[comparison] = value;
1130+
}
1131+
var past = settled.Count == 0 ? body
1132+
: body.Replace(node => settled.TryGetValue(node, out var value) ? value : node).InnerSimplified(isExact);
1133+
// What is left repeats, or is one truth value, which repeats modulo 1.
1134+
var modulus = EInteger.One;
1135+
if (past.ContainsNode(x))
1136+
{
1137+
if (Period(past, x, (SpecialSet)MathS.Sets.Z) is not { } period || period.CompareTo(EInteger.FromInt32(LargestPeriod)) > 0)
1138+
return null;
1139+
modulus = period;
1140+
}
11021141
int holds = 0, fails = 0, undecided = 0;
11031142
void Tell(Entity verdict)
11041143
{
@@ -1109,9 +1148,13 @@ void Tell(Entity verdict)
11091148
default: undecided++; break;
11101149
}
11111150
}
1112-
// The prime factors of M past the bound, which are not units modulo it.
1151+
// The primes below the threshold, one by one.
1152+
for (var prime = two; prime.CompareTo(threshold) < 0; prime += 1)
1153+
if (Functions.Primes.IsPrime(prime) == true)
1154+
Tell(body.Substitute(x, Integer.Create(prime)).InnerSimplified(isExact));
1155+
// The prime factors of M from the threshold on, which are not units modulo it.
11131156
var rest = modulus;
1114-
for (var factor = EInteger.FromInt32(2); rest.CompareTo(EInteger.One) > 0; factor += 1)
1157+
for (var factor = two; rest.CompareTo(EInteger.One) > 0; factor += 1)
11151158
{
11161159
// Past the square root of what is left, what is left is prime.
11171160
if (factor.Multiply(factor).CompareTo(rest) > 0)
@@ -1120,31 +1163,24 @@ void Tell(Entity verdict)
11201163
continue;
11211164
while (rest.Remainder(factor).IsZero)
11221165
rest = rest.Divide(factor);
1123-
if (factor.CompareTo(above) > 0)
1124-
Tell(claim.Substitute(x, Integer.Create(factor)).InnerSimplified(isExact));
1166+
if (factor.CompareTo(threshold) >= 0)
1167+
Tell(past.Substitute(x, Integer.Create(factor)).InnerSimplified(isExact));
11251168
}
1126-
// The units modulo M, each the residue of primes past any bound.
1169+
// The units modulo M, each the residue of infinitely many primes past the threshold.
11271170
for (var residue = EInteger.One; residue.CompareTo(modulus) <= 0; residue += 1)
11281171
if (residue.Gcd(modulus).Equals(EInteger.One))
1129-
Tell(claim.Substitute(x, Integer.Create(residue)).InnerSimplified(isExact));
1130-
return Tally(kind, holds, fails, undecided, distinct: true);
1172+
Tell(past.Substitute(x, Integer.Create(residue)).InnerSimplified(isExact));
1173+
if (Tally(kind, holds, fails, undecided, distinct: true) is not { } verdict)
1174+
return null;
1175+
return (verdict, threshold);
11311176
}
11321177

11331178
/// <summary>
1134-
/// The whole number every <paramref name="x"/> satisfying <paramref name="condition"/> is
1135-
/// past, where the condition is a lower bound on it: <c>x &gt; 3</c> is <c>3</c>,
1136-
/// <c>x &gt;= 5</c> is <c>4</c>. <see langword="null"/> otherwise.
1179+
/// The period of the body in <paramref name="x"/> over the whole numbers, where it has
1180+
/// one: a divisibility by a whole number, or a congruence modulo one, of polynomials
1181+
/// with whole coefficients, joined by the connectives; the least common multiple of the
1182+
/// parts' periods.
11371183
/// </summary>
1138-
private static EInteger? LowerBound(Entity condition, Variable x)
1139-
=> condition switch
1140-
{
1141-
Greaterf(var name, Integer { EInteger: var past }) when name == x => past,
1142-
GreaterOrEqualf(var name, Integer { EInteger: var from }) when name == x => from.Subtract(EInteger.One),
1143-
Lessf(Integer { EInteger: var past }, var name) when name == x => past,
1144-
LessOrEqualf(Integer { EInteger: var from }, var name) when name == x => from.Subtract(EInteger.One),
1145-
_ => null,
1146-
};
1147-
11481184
private static EInteger? Period(Entity body, Variable x, SpecialSet set)
11491185
{
11501186
switch (body)

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ public void DecidedOverAFiniteSet(string statement, string expected)
104104
[InlineData("forall n in ZZ : n >= 14 implies (exists a in ZZ* : exists b in ZZ* : n = 3 a + 8 b)", "True")]
105105
[InlineData("forall n in ZZ : n >= 24 implies (exists a in ZZ* : exists b in ZZ* : n = 4 a + 9 b)", "True")]
106106
[InlineData("forall n in ZZ : n >= 23 implies (exists a in ZZ* : exists b in ZZ* : n = 4 a + 9 b)", "False")]
107+
// A root that is a whole number is found exactly: 2/3 n - 2 is 0 at 3 and positive from 4.
108+
[InlineData("forall n in ZZ* : 2/3 n - 2 <= 0", "False")]
109+
[InlineData("exists n in ZZ* : 2/3 n - 2 > 0 and 2 divides n + 1", "True")]
107110
// Floors and ceilings of n over a whole number beside n itself repeat where the drifts
108111
// cancel, and the residues decide them: floor(n/2) + ceil(n/2) = n, and Hermite's
109112
// identity for thirds.
@@ -138,6 +141,17 @@ public void DecidedOverAFiniteSet(string statement, string expected)
138141
[InlineData("forall p in PP : p > 2 implies 2 divides p + 1", "True")]
139142
[InlineData("exists p in PP : p > 3 and 3 divides p", "False")]
140143
[InlineData("exists p in PP : p > 10 and p = 3 (mod 4)", "True")]
144+
// A comparison keeps one truth value past its root, so the primes below the last root are
145+
// asked one by one and the rest by their residues: every prime is 2, or 1 or 3 modulo 4,
146+
// and every prime but 2 and 3 is 1 or 5 modulo 6.
147+
[InlineData("forall p in PP : p = 1 (mod 4) or p = 3 (mod 4) or p = 2", "True")]
148+
[InlineData("forall p in PP : p = 1 (mod 4) or p = 3 (mod 4)", "False")]
149+
[InlineData("forall p in PP : p = 2 or 2 divides p + 1", "True")]
150+
[InlineData("forall p in PP : p = 2 or p = 3 or 6 divides p^2 - 1", "True")]
151+
[InlineData("forall p in PP : p >= 5 implies p = 1 (mod 6) or p = 5 (mod 6)", "True")]
152+
[InlineData("forall p in PP : p < 10 implies p = 2 or 2 divides p + 1", "True")]
153+
[InlineData("exists p in PP : p > 2 and 2 divides p", "False")]
154+
[InlineData("exists p in PP : p < 3 and p = 2 (mod 3)", "True")]
141155
// Sullivan and Mackey's Prob 4.11.6: for whole x and y the witnesses are x - y and y - x,
142156
// and one of them is not negative. Not so in ZZ+, where x = y leaves 0, nor for reals,
143157
// where 1/2 and 0 leave ±1/2.
@@ -206,6 +220,22 @@ public void DecidedOverAnInfiniteSet(string statement, string expected)
206220
public void LeftAsWrittenWhereNothingDecidesIt(string statement)
207221
=> Assert.Equal(statement.ToEntity(), statement.ToEntity().Simplify());
208222

223+
/// <summary>
224+
/// Where a comparison with rational coefficients settles is found exactly, so the verdict
225+
/// does not depend on the decimal precision: at 16 or 30 digits 2/3 rounds up, which put
226+
/// the root of 2/3 n - 2 below 3, and the statement was decided as though 3 were past it.
227+
/// </summary>
228+
[Theory]
229+
[InlineData(16)]
230+
[InlineData(30)]
231+
[InlineData(100)]
232+
public void WhereAComparisonSettlesDoesNotDependOnThePrecision(int digits)
233+
{
234+
using var _ = MathS.Settings.DecimalPrecisionContext.Set(new PeterO.Numbers.EContext(digits, PeterO.Numbers.ERounding.HalfUp, -digits, 1000, false));
235+
Assert.Equal(Entity.Boolean.False, MathS.FromString("forall n in ZZ* : 2/3 n - 2 <= 0", useCache: false).Simplify());
236+
Assert.Equal(Entity.Boolean.False, MathS.FromString("forall n in ZZ* : 5/9 n - 5 <= 0", useCache: false).Simplify());
237+
}
238+
209239
[Fact]
210240
public void AWholeNumberIsNotAssumedOfAReal()
211241
// binomial(p, k) of a real k is not a whole number, so the rule for divisibility has

0 commit comments

Comments
 (0)