Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,30 @@ as written.
| `forall n in ZZ* : not (7 divides 2^n + 1)` | `UnhandledParseException` | `True` |
| `forall n in ZZ+ : 5 divides 7^n - 1` | `UnhandledParseException` | `False` |

### Nested quantifiers are decided through listed witnesses and witness terms

`exists z in S : P`, where the solver lists the `z` at which `P` holds and the quantifiers around
it make each of them a whole number, is the statement that one of them is in `S`: in `ZZ*` a whole
`s` is where `s >= 0`, in `ZZ+` where `s >= 1`. Sullivan and Mackey's Prob 4.11.6,
`forall x in ZZ : forall y in ZZ : exists z in ZZ* : x - y = z or y - x = z`, is `True` so: the
witnesses are `x - y` and `y - x`, and `x - y < 0 and y - x < 0` has no solution
([#1409](https://github.com/asc-community/AngouriMath/issues/1409)). That last step needed two rays
with symbolic ends to meet: `(a; +oo) /\ (-oo; b)` is `(a; b)`, and `(x; +oo) /\ (-oo; x)` is
empty, where both were left as written. And `exists x : forall y : P` is false where a term in
`x`, in the inner set for every `x`, fails `P` at every `x`, and `forall x : exists y : P` true where
one holds `P` at every `x`: Prob 4.11.5, `exists x in RR : forall y in RR : x^2 - y^2 >= 0`, fails at
`y = x^2 + 1`. The terms are a fixed few polynomials, so a statement none of them decides is left
as it was. A comparison of two polynomials that differ by a number says the same of every member:
`x^2 + 1 > x^2` is `1 > 0` at each, which is what the term `x^2 + 1` asks.

| Input | Was (2.5.0) | Now |
|---|---|---|
| `forall x in ZZ : forall y in ZZ : exists z in ZZ* : x - y = z or y - x = z` | `UnhandledParseException` (quantifiers are new since; left as written when they arrived) | `True` |
| `exists x in RR : forall y in RR : x^2 - y^2 >= 0` | `UnhandledParseException` (left as written when quantifiers arrived) | `False` |
| `"(x; +oo) /\ (-oo; x)".ToEntity().InnerSimplified` | as written | `{ }` |
| `exists y in RR : forall x in RR : y = x^3` | `UnhandledParseException` (left as written when quantifiers arrived) | `False` |
| `forall x in RR : x^2 + 1 > x^2` | `UnhandledParseException` (left as written when quantifiers arrived) | `True` |

### `PP` is the set of primes, and `min(S)` the least member of a set

`PP` is a special set beside `ZZ+` — the primes `{2, 3, 5, 7, ...}`, `\mathbb{P}` in LaTeX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal static Set IntersectIntervalAndInterval(Interval A, Interval B)
A.Right.Evaled is not Real aRight ||
B.Left.Evaled is not Real bLeft ||
B.Right.Evaled is not Real bRight)
return A.Intersect(B);
return Rays(A, B) ?? Rays(B, A) ?? A.Intersect(B);
if (aLeft == bRight)
return A.LeftClosed && B.RightClosed ? new FiniteSet(A.Left) : Empty;
if (bLeft == aRight)
Expand All @@ -64,6 +64,22 @@ B.Left.Evaled is not Real bLeft ||
return new Interval(left, leftClosed, right, rightClosed);
}

/// <summary>
/// A ray up from <c>a</c> and a ray down to <c>b</c> meet in the interval from <c>a</c> to
/// <c>b</c>, whatever the two are, which holds nothing where <c>a</c> is not below <c>b</c>:
/// <c>(x; +oo) /\ (-oo; x)</c> is nothing, and is what <c>x &lt; y and y &lt; x</c> is
/// solved to. <see langword="null"/> where the two are not such rays.
/// https://github.com/asc-community/AngouriMath/issues/1409
/// </summary>
private static Set? Rays(Interval up, Interval down)
{
if (up.Right != Real.PositiveInfinity || down.Left != Real.NegativeInfinity)
return null;
if (up.Left == down.Right)
return up.LeftClosed && down.RightClosed ? new FiniteSet(up.Left) : Empty;
return new Interval(up.Left, up.LeftClosed, down.Right, down.RightClosed);
}

internal static Set IntersectCSetAndCSet(ConditionalSet intLeft, ConditionalSet intRight)
{
(intLeft, intRight) = MergeToOneVariable(intLeft, intRight);
Expand Down
25 changes: 25 additions & 0 deletions Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ internal static Entity Renamed(Entity expression, Variable from, Variable to)
/// </summary>
private static bool Reads(Entity node) => node switch
{
// A membership in a set of whole numbers, which MembershipAsComparison reads, and a
// quantifier over one, whose listed witnesses are asked about such memberships.
Inf(not Number, SpecialSet whole) => Quantifiers.IsIntegerSet(whole),
Quantifier(_, SpecialSet over, _) => Quantifiers.IsIntegerSet(over),
Dividesf(not Number, _) or Congruentf(_, _, not Number) => true,
Equalsf(Modf(_, var modulus), Modf(_, var other)) => modulus is not Number && modulus == other,
_ => false
Expand Down Expand Up @@ -239,6 +243,27 @@ internal static bool PrimeDividesItsBinomial(Entity p, Entity k)
return true;
}

/// <summary>
/// <c>s in S</c> for a set of whole numbers, as the comparison it is where the facts in scope
/// make <c>s</c> a whole number: <c>s &gt;= 0</c> in <c>ZZ*</c>, <c>s &gt;= 1</c> in <c>ZZ+</c>, and
/// <c>True</c> in <c>ZZ</c>. <see langword="null"/> otherwise. Sullivan and Mackey's Prob
/// 4.11.6 asks for a <c>z</c> in <c>ZZ*</c> with <c>x - y = z</c> or <c>y - x = z</c>, for whole
/// <c>x</c> and <c>y</c>: the witnesses are <c>x - y</c> and <c>y - x</c>, one of which is
/// not negative.
/// </summary>
internal static Entity? MembershipAsComparison(Entity element, Set set)
{
if (top is null || element is Number || !MentionsARenamedName(element) || !IsWholePolynomial(element))
return null;
return set switch
{
SpecialSet.Integers => Entity.Boolean.True,
SpecialSet.NonNegativeIntegers => element >= Integer.Zero,
SpecialSet.PositiveIntegers => element >= Integer.One,
_ => null,
};
}

/// <summary>
/// Whether <c>p divides d</c> by the binomial theorem, with the facts in scope: <c>p</c> is
/// prime, <c>d</c> is a polynomial with whole coefficients in whole quantities, and
Expand Down
126 changes: 116 additions & 10 deletions Sources/AngouriMath/Functions/Boolean/Quantifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body
}
ProofRecording.Rollback(tried);
}
// A comparison of two polynomials that differ by a number says the same of every
// member: x^2 + 1 > x^2 is 1 > 0 at each, which is what the witness term x^2 + 1 asks.
// Polynomials only, which are defined at every member: 1/x + 1 > 1/x is not 1 > 0 at 0.
if (body is Greaterf or Lessf or GreaterOrEqualf or LessOrEqualf
&& body is IBinaryNode { NodeFirstChild: var greater, NodeSecondChild: var lesser }
&& DefinedEverywhere(greater) && DefinedEverywhere(lesser)
&& (greater - lesser).Expand().InnerSimplified(isExact) is Real { IsFinite: true } gap)
{
var holds = body switch
{
Greaterf => gap > Integer.Zero,
Lessf => gap < Integer.Zero,
GreaterOrEqualf => gap >= Integer.Zero,
_ => gap <= Integer.Zero,
};
by = ($"the two sides differ by {gap}, so the comparison says the same of every member", "linarith");
return Closed(kind, set, holds ? Entity.Boolean.True : Entity.Boolean.False);
}
if (set is FiniteSet finite)
{
by = ($"evaluated at each of the {finite.Count} members", "decide");
Expand Down Expand Up @@ -329,20 +347,86 @@ private static Entity Quantified(Kind kind, Entity var, Entity over, Entity body
}
if (BySolving(kind, x, set, body) is { } bySolving)
{
by = ((kind, bySolving == Entity.Boolean.True) switch
{
(Kind.All, true) => "the negation of the body has no solution in the set",
(Kind.All, false) => "the negation of the body has a solution in the set",
(Kind.Some, true) => "the body's solutions meet the set",
(Kind.Some, false) => "the body has no solution in the set",
(_, true) => "the body has exactly one solution in the set",
_ => "the body has no solution, or more than one, in the set",
}, "nlinarith");
by = bySolving is not Entity.Boolean
? ("the body holds exactly at its listed solutions, so there is a member where it holds exactly when one of them is a member", "exists_eq_left")
: ((kind, bySolving == Entity.Boolean.True) switch
{
(Kind.All, true) => "the negation of the body has no solution in the set",
(Kind.All, false) => "the negation of the body has a solution in the set",
(Kind.Some, true) => "the body's solutions meet the set",
(Kind.Some, false) => "the body has no solution in the set",
(_, true) => "the body has exactly one solution in the set",
_ => "the body has no solution, or more than one, in the set",
}, "nlinarith");
return bySolving;
}
// exists x in X : forall y in Y : P is false where one term in x, in Y for every x,
// fails P at every x, and forall x in X : exists y in Y : P is true where one holds P at
// every x: Sullivan and Mackey's Prob 4.11.5, exists x in RR : forall y in RR :
// x^2 - y^2 >= 0, fails at y = x^2 + 1 whatever x is.
// https://github.com/asc-community/AngouriMath/issues/1409
if (body is Quantifier(Variable nested, Set nestedOver, var nestedBody) && nested != x && !nestedOver.ContainsNode(x)
&& (kind, body) is (Kind.Some, Forallf) or (Kind.All, Existsf)
&& ByATerm(kind, x, set, nested, nestedOver, nestedBody, isExact) is var (byTerm, witnessTerm))
{
by = ($"{nested} = {witnessTerm} is in {nestedOver} for every {x} and {(kind == Kind.Some ? "fails" : "holds")} there", "use");
return byTerm;
}
return null;
}

/// <summary>Whether the expression is a polynomial in its names, and so has a value wherever they do.</summary>
private static bool DefinedEverywhere(Entity expression)
=> expression switch
{
Number or Variable => true,
Sumf(var a, var b) => DefinedEverywhere(a) && DefinedEverywhere(b),
Minusf(var a, var b) => DefinedEverywhere(a) && DefinedEverywhere(b),
Mulf(var a, var b) => DefinedEverywhere(a) && DefinedEverywhere(b),
Powf(var @base, Integer { IsNegative: false }) => DefinedEverywhere(@base),
_ => false,
};

/// <summary>
/// The verdict of <see cref="DecideBy"/>'s route through a term, and the term: for
/// <c>exists x : forall y : P</c>, a term at which <c>P</c> fails for every <c>x</c>, which
/// makes it false; for <c>forall x : exists y : P</c>, one at which <c>P</c> holds for every
/// <c>x</c>, which makes it true. The terms are few and fixed, each certainly in the inner
/// set for every member of the outer one, so a term that works is a proof, and none
/// working says nothing.
/// </summary>
private static (Entity Verdict, Entity Term)? ByATerm(Kind kind, Variable x, Set set, Variable y, Set inner, Entity claim, bool isExact)
{
foreach (var term in TermsIn(x, set, inner))
{
var at = claim.Substitute(y, term);
var everywhere = Decide(Kind.All, x, set, (kind == Kind.Some ? !at : at).InnerSimplified(isExact), isExact);
if (everywhere == Entity.Boolean.True)
return (kind == Kind.Some ? Entity.Boolean.False : Entity.Boolean.True, term);
}
return null;
}

/// <summary>
/// Terms in <paramref name="x"/> that lie in <paramref name="inner"/> whenever it lies in
/// <paramref name="set"/>: polynomials with whole coefficients, which map the reals to the
/// reals and the whole numbers to the whole numbers, and of them the ones that are not
/// negative, or positive, for the whole numbers from 0 or from 1.
/// </summary>
private static IEnumerable<Entity> TermsIn(Variable x, Set set, Set inner)
{
var whole = set is SpecialSet outer && IsIntegerSet(outer);
var any = new Entity[] { x + 1, x - 1, -x, 2 * x, MathS.Pow(x, 2) + 1, -MathS.Pow(x, 2) - 1, Integer.Zero, Integer.One };
return inner switch
{
SpecialSet.Reals when WithinReals(set) => any,
SpecialSet.Integers when whole => any,
SpecialSet.NonNegativeIntegers when whole => new Entity[] { MathS.Pow(x, 2) + 1, MathS.Pow(x, 2), Integer.Zero, Integer.One },
SpecialSet.PositiveIntegers when whole => new Entity[] { MathS.Pow(x, 2) + 1, Integer.One },
_ => System.Array.Empty<Entity>(),
};
}

/// <summary>
/// The least member of a set of whole numbers bounded below -- <c>ZZ+</c>, <c>ZZ*</c>,
/// either cut by an interval -- or <see langword="null"/> where the set is something else.
Expand Down Expand Up @@ -1382,7 +1466,7 @@ private static IEnumerable<Entity> Candidates(Set set)
{
true => Entity.Boolean.True,
false => Entity.Boolean.False,
null => null,
null => OneOfTheListedIsIn(solved, set),
} : null;
case Kind.All:
return Algebra.AnalyticalSolving.StatementSolver.Solved(Negated(body), x) is { } solvedNot ? Meets(solvedNot, set) switch
Expand Down Expand Up @@ -1414,6 +1498,28 @@ private static IEnumerable<Entity> Candidates(Set set)
return null;
}

/// <summary>
/// That one of the listed solutions is in the set, where the facts in scope read each
/// membership as a comparison, or <see langword="null"/>. The body holds exactly at its
/// solutions, so there is a member where it holds exactly when one of them is a member:
/// Sullivan and Mackey's Prob 4.11.6, <c>exists z in ZZ* : x - y = z or y - x = z</c> for
/// whole <c>x</c> and <c>y</c>, is <c>x - y &gt;= 0 or y - x &gt;= 0</c>, which the quantifiers
/// over <c>x</c> and <c>y</c> then decide.
/// </summary>
private static Entity? OneOfTheListedIsIn(Set solutions, Set set)
{
if (solutions is not FiniteSet { Count: > 0 } listed)
return null;
Entity? any = null;
foreach (var solution in listed)
{
if (QuantifierFacts.MembershipAsComparison(solution, set) is not { } comparison)
return null;
any = any is null ? comparison : any | comparison;
}
return any;
}

/// <summary>
/// The negation of the body with <c>not</c> pushed down to the comparisons, which is what
/// the statement solver has arms for: <c>not (a implies b)</c> is <c>a and not b</c>, and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ protected override Entity InnerSimplify(bool isExact)
(a, b) => (a, b) switch
{
(var el, Set set) when set.TryContains(el, out var contains) => contains,
// A whole number, by what the quantifiers around it establish, is in
// ZZ* where it is not negative. https://github.com/asc-community/AngouriMath/issues/1409
(var el, Set set) when Functions.Boolean.QuantifierFacts.MembershipAsComparison(el, set) is { } comparison
=> comparison.InnerSimplified(isExact),
_ => null
},
(@this, a, b) => ((Inf)@this).New(a, b), isExact, propagateSet: false);
Expand Down
19 changes: 18 additions & 1 deletion Sources/Tests/UnitTests/Core/QuantifierTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ public void DecidedOverAFiniteSet(string statement, string expected)
[InlineData("forall p in PP : forall k in ZZ : k < p implies p divides binomial(p, k)", "False")]
[InlineData("forall p in PP : forall k in ZZ : (0 < k and k <= p) implies p divides binomial(p, k)", "False")]
[InlineData("forall p in PP : forall p in ZZ+ : forall k in ZZ : (0 < k and k < p) implies p divides binomial(p, k)", "False")]
// Sullivan and Mackey's Prob 4.11.6: for whole x and y the witnesses are x - y and y - x,
// and one of them is not negative. Not so in ZZ+, where x = y leaves 0, nor for reals,
// where 1/2 and 0 leave ±1/2.
[InlineData("forall x in ZZ : forall y in ZZ : exists z in ZZ* : x - y = z or y - x = z", "True")]
[InlineData("forall x in ZZ : forall y in ZZ : exists z in ZZ+ : x - y = z or y - x = z", "False")]
[InlineData("forall x in RR : forall y in RR : exists z in ZZ* : x - y = z or y - x = z", "False")]
// Prob 4.11.5: a square is not the greatest square, for y = x^2 + 1 is past every x, and
// a term in x is a witness for every x at once. Where none of the terms tried works, a
// single member may still decide it: x = 0 is below every square.
[InlineData("exists x in RR : forall y in RR : x^2 - y^2 >= 0", "False")]
[InlineData("forall x in RR : exists y in RR : y > x^2", "True")]
[InlineData("exists x in ZZ : forall y in ZZ : x <= y", "False")]
[InlineData("forall x in ZZ+ : exists y in ZZ+ : y > x", "True")]
[InlineData("exists x in RR : forall y in RR : x <= y^2", "True")]
// No number is every cube: whatever y is, the cube of x = y^2 + 1 is not y.
[InlineData("exists y in RR : forall x in RR : y = x^3", "False")]
[InlineData("forall x in RR : x^2 + 1 > x^2", "True")]
[InlineData("forall x in RR : (x + 1)^2 < x^2 + 2 x", "False")]
// The set may be an interval, a set builder, or a special set with no member of its own.
[InlineData("forall x in (0; 1) : x^2 < 1", "True")]
[InlineData("forall x in [0; 1] : x^2 < 1", "False")]
Expand Down Expand Up @@ -141,7 +159,6 @@ public void DecidedOverAnInfiniteSet(string statement, string expected)
[InlineData("forall x in RR : sin(x) <= 1")]
[InlineData("exists x in RR : sin(x) = 2")]
[InlineData("forall x in RR : exists y in RR : y = x^3")]
[InlineData("exists y in RR : forall x in RR : y = x^3")]
public void LeftAsWrittenWhereNothingDecidesIt(string statement)
=> Assert.Equal(statement.ToEntity(), statement.ToEntity().Simplify());

Expand Down
Loading
Loading