diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index beac197a1..6d4e3971a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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, diff --git a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs index 24dda2218..9fd5ae938 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Intersection.cs @@ -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) @@ -64,6 +64,22 @@ B.Left.Evaled is not Real bLeft || return new Interval(left, leftClosed, right, rightClosed); } + /// + /// A ray up from a and a ray down to b meet in the interval from a to + /// b, whatever the two are, which holds nothing where a is not below b: + /// (x; +oo) /\ (-oo; x) is nothing, and is what x < y and y < x is + /// solved to. where the two are not such rays. + /// https://github.com/asc-community/AngouriMath/issues/1409 + /// + 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); diff --git a/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs b/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs index 7977b36ed..784cfb0fc 100644 --- a/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs +++ b/Sources/AngouriMath/Functions/Boolean/QuantifierFacts.cs @@ -199,6 +199,10 @@ internal static Entity Renamed(Entity expression, Variable from, Variable to) /// 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 @@ -239,6 +243,27 @@ internal static bool PrimeDividesItsBinomial(Entity p, Entity k) return true; } + /// + /// s in S for a set of whole numbers, as the comparison it is where the facts in scope + /// make s a whole number: s >= 0 in ZZ*, s >= 1 in ZZ+, and + /// True in ZZ. otherwise. Sullivan and Mackey's Prob + /// 4.11.6 asks for a z in ZZ* with x - y = z or y - x = z, for whole + /// x and y: the witnesses are x - y and y - x, one of which is + /// not negative. + /// + 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, + }; + } + /// /// Whether p divides d by the binomial theorem, with the facts in scope: p is /// prime, d is a polynomial with whole coefficients in whole quantities, and diff --git a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs index 3cebc869f..288c456e9 100644 --- a/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs +++ b/Sources/AngouriMath/Functions/Boolean/Quantifiers.cs @@ -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"); @@ -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; } + /// Whether the expression is a polynomial in its names, and so has a value wherever they do. + 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, + }; + + /// + /// The verdict of 's route through a term, and the term: for + /// exists x : forall y : P, a term at which P fails for every x, which + /// makes it false; for forall x : exists y : P, one at which P holds for every + /// x, 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. + /// + 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; + } + + /// + /// Terms in that lie in whenever it lies in + /// : 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. + /// + private static IEnumerable 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(), + }; + } + /// /// The least member of a set of whole numbers bounded below -- ZZ+, ZZ*, /// either cut by an interval -- or where the set is something else. @@ -1382,7 +1466,7 @@ private static IEnumerable 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 @@ -1414,6 +1498,28 @@ private static IEnumerable Candidates(Set set) return null; } + /// + /// That one of the listed solutions is in the set, where the facts in scope read each + /// membership as a comparison, or . 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, exists z in ZZ* : x - y = z or y - x = z for + /// whole x and y, is x - y >= 0 or y - x >= 0, which the quantifiers + /// over x and y then decide. + /// + 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; + } + /// /// The negation of the body with not pushed down to the comparisons, which is what /// the statement solver has arms for: not (a implies b) is a and not b, and diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs index 542a8d0fd..0784d7377 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Discrete/Evaluation.Discrete.Classes.cs @@ -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); diff --git a/Sources/Tests/UnitTests/Core/QuantifierTest.cs b/Sources/Tests/UnitTests/Core/QuantifierTest.cs index 21a18dca7..cce76d28e 100644 --- a/Sources/Tests/UnitTests/Core/QuantifierTest.cs +++ b/Sources/Tests/UnitTests/Core/QuantifierTest.cs @@ -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")] @@ -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()); diff --git a/Sources/Tests/UnitTests/Core/Sets/IntervalAndInterval.cs b/Sources/Tests/UnitTests/Core/Sets/IntervalAndInterval.cs index 39735efed..40b36df10 100644 --- a/Sources/Tests/UnitTests/Core/Sets/IntervalAndInterval.cs +++ b/Sources/Tests/UnitTests/Core/Sets/IntervalAndInterval.cs @@ -65,6 +65,20 @@ private void CannotSimplify(Entity act) [Fact] public void Intersection13() => TestInt(A.Intersect(F1), Interval(3, false, 5, true)); [Fact] public void Intersection14() => TestInt(F1.Intersect(A), Interval(3, false, 5, true)); + /// + /// A ray up from a and a ray down to b meet in the interval from one to the + /// other whatever the two are, and where they are one name, in that name or in nothing: + /// what x < y and y < x is solved to. + /// + [Theory] + [InlineData("(a; +oo) /\\ (-oo; b)", "(a; b)")] + [InlineData("(-oo; b] /\\ [a; +oo)", "[a; b]")] + [InlineData("(x; +oo) /\\ (-oo; x)", "{}")] + [InlineData("[x; +oo) /\\ (-oo; x]", "{x}")] + [InlineData("[x; +oo) /\\ (-oo; x)", "{}")] + public void TwoRaysMeetWhateverTheirEnds(string intersection, string expected) + => TestArb(MathS.FromString(intersection), MathS.FromString(expected).InnerSimplified); + [Fact] public void Subtraction1() => TestInt(A.SetSubtract(B), Interval(3, false, 5, true)); [Fact] public void Subtraction2() => TestArb(B.SetSubtract(A), Set.Empty); [Fact] public void Subtraction3() => TestInt(E.SetSubtract(D), (Interval)E);