diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md
index 9d3e6e7b8..ab305dcf7 100644
--- a/BREAKING-CHANGES.md
+++ b/BREAKING-CHANGES.md
@@ -555,6 +555,29 @@ already did. With downcasting on, nothing changes ([#2622](https://github.com/as
| `(-30.5)^(1/3)` | `1.562… + 2.705…i` | `-3.124…` |
| `(-30.5)^(1/2)` | `5.522…i` | unchanged |
+### A union or a product that opens with a set difference or a mod prints in brackets
+
+**Silent**, in what is printed. A union printed its right operand flat when that operand was a
+union, and a product its right operand when it was a product or a quotient, because both operators
+are associative. But `\` shares a precedence level with `\/`, and `mod` with `*` and `/`, and the
+parser folds each level to the left, so an operand that *opens* with one of them was read back
+with the outer operator inside it: the printed form named a different set or number. The entity
+was always right; `Stringize` and, for unions, `Latexize` now bracket such an operand.
+[#2758](https://github.com/asc-community/AngouriMath/issues/2758)
+
+| Expression | Before (`578680eed`) | Now |
+|---|---|---|
+| `A \/ ((B \ C) \/ D)` | `A \/ B \ C \/ D`, read back as `((A \/ B) \ C) \/ D` | `A \/ (B \ C \/ D)` |
+| `{ 1 } \/ (({ 2 } \ { 1 }) \/ { 3 })` | `{ 1 } \/ { 2 } \ { 1 } \/ { 3 }`, which is `{ 2, 3 }` | `{ 1 } \/ ({ 2 } \ { 1 } \/ { 3 })` |
+| Simplify of `({ -1, 1 } \ [-2; +oo)) \/ (QQ \/ { pi })` | `{ pi } \/ { -1, 1 } \ [-2; +oo) \/ QQ`, which has no `pi` | `{ pi } \/ ({ -1, 1 } \ [-2; +oo) \/ QQ)` |
+| `Latexize` of `A \/ ((B \ C) \/ D)` | `A \cup B \setminus C \cup D` | `A \cup \left(B \setminus C \cup D\right)` |
+| `2 * ((3 mod 2) * 4)` | `2 * 3 mod 2 * 4`, which is 0 | `2 * (3 mod 2 * 4)`, which is 8 |
+| `x * ((y mod z) / w)` | `x * y mod z / w` | `x * (y mod z / w)` |
+| `-((y mod z) * w)` | `-y mod z * w`, read as `((-y) mod z) * w` | `-(y mod z * w)` |
+
+A right operand made only of unions, or only of products and quotients, still prints flat. The LaTeX
+of a product already bracketed each `\bmod` factor and is unchanged.
+
### A singular matrix is not inverted because its determinant is not written as 0
**Not silent: a wrong answer is now no answer.** `Matrix.Inverse`, `Matrix.Pow` with a negative exponent, `M^(-1)` and
diff --git a/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs b/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs
index 7491bfaba..3109be9a6 100644
--- a/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs
+++ b/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs
@@ -76,9 +76,10 @@ partial record Unionf
// CSharpMath.Evaluation reads \cup and \setminus at one precedence level and folds
// them to the left, exactly as the grammar here does, so a \setminus on the right
// needs bracketing or it comes back as the outer operator. Union with union is
- // associative and stays flat.
+ // associative and stays flat -- unless it opens with a \setminus, which the flat
+ // form would hand the outer union to (#2758).
private protected override string LatexizeNode()
- => $@"{Left.Latexize(Left.LatexPriority < LatexPriority)} \cup {Right.Latexize(Right.LatexPriority < LatexPriority || Right is SetMinusf)}";
+ => $@"{Left.Latexize(Left.LatexPriority < LatexPriority)} \cup {Right.Latexize(Right.LatexPriority < LatexPriority || Right.LatexPriority == LatexPriority && Right.OpensWithANonAssociativeOperator(latex: true))}";
}
partial record Intersectionf
diff --git a/Sources/AngouriMath/Functions/Output/ToString.Definition.cs b/Sources/AngouriMath/Functions/Output/ToString.Definition.cs
index 17f2c03fc..dc0c384c8 100644
--- a/Sources/AngouriMath/Functions/Output/ToString.Definition.cs
+++ b/Sources/AngouriMath/Functions/Output/ToString.Definition.cs
@@ -91,5 +91,41 @@ private string StringizeAsWritten()
protected internal string Stringize(bool parenthesesRequired) =>
(parenthesesRequired && !PrintsItsCodomain) || MathS.Diagnostic.OutputExplicit
? $"({Stringize()})" : Stringize();
+
+ ///
+ /// Whether this, printed flat, would open with a \ or a mod at its own
+ /// precedence level -- the non-associative operators that share a level with the
+ /// associative \/ and *.
+ ///
+ ///
+ /// An associative operator prints an operand of its own level on the right without
+ /// brackets, and the grammar folds the flat form to the left. That re-associates only the
+ /// operators along the operand's left edge, the ones printed without brackets of their
+ /// own, so it is sound exactly when none of them is a \ or a mod.
+ /// A \/ ((B \ C) \/ D) printed as A \/ B \ C \/ D came back as
+ /// ((A \/ B) \ C) \/ D, and 2 * ((3 mod 2) * 4) printed as
+ /// 2 * 3 mod 2 * 4 came back as 0 where it is 8.
+ /// https://github.com/asc-community/AngouriMath/issues/2758
+ ///
+ internal bool OpensWithANonAssociativeOperator(bool latex = false)
+ {
+ var level = latex ? LatexPriority : Priority;
+ Entity? leftmost = this switch
+ {
+ Set.SetMinusf or Modf => null,
+ Set.Unionf union => union.Left,
+ // -x prints as a unary minus on its own operand, which brackets itself if it opens with a mod.
+ Mulf(Integer(-1), _) when !MathS.Diagnostic.OutputExplicit => this,
+ Mulf product => product.Multiplier,
+ Divf quotient => quotient.Dividend,
+ _ => this
+ };
+ if (leftmost is null)
+ return true;
+ if (ReferenceEquals(leftmost, this) || leftmost.PrintsItsCodomain)
+ return false;
+ return (latex ? leftmost.LatexPriority : leftmost.Priority) == level
+ && leftmost.OpensWithANonAssociativeOperator(latex);
+ }
}
}
diff --git a/Sources/AngouriMath/Functions/Output/ToString/ToString.Arithmetics.Classes.cs b/Sources/AngouriMath/Functions/Output/ToString/ToString.Arithmetics.Classes.cs
index bd60a72d8..39fb29186 100644
--- a/Sources/AngouriMath/Functions/Output/ToString/ToString.Arithmetics.Classes.cs
+++ b/Sources/AngouriMath/Functions/Output/ToString/ToString.Arithmetics.Classes.cs
@@ -33,10 +33,11 @@ public partial record Mulf
// Multiplication is associative, so a product or a quotient on the right may stay
// unbracketed -- but `mod` shares this precedence level and is not associative, so a
// `mod` on the right must be bracketed or it is re-read as the outer operator:
- // `2 * (3 mod 2)` is 2 and `(2 * 3) mod 2` is 0.
+ // `2 * (3 mod 2)` is 2 and `(2 * 3) mod 2` is 0. So must a product or a quotient that
+ // opens with one: `2 * ((3 mod 2) * 4)` is 8, and `2 * 3 mod 2 * 4` is 0. #2758
private protected override string StringizeNode()
{
- var multiplicand = Multiplicand.Stringize(Multiplicand.Priority < Priority || Multiplicand is Modf);
+ var multiplicand = Multiplicand.Stringize(Multiplicand.Priority < Priority || Multiplicand.Priority == Priority && Multiplicand.OpensWithANonAssociativeOperator());
if (Multiplier is not Integer(-1) || MathS.Diagnostic.OutputExplicit)
return Multiplier.Stringize(Multiplier.Priority < Priority) + " * " + multiplicand;
// A negation of something printed with a sign of its own is bracketed: -(-2/3), not --2/3, which is
diff --git a/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs b/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs
index b78829987..68c5a42e4 100644
--- a/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs
+++ b/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs
@@ -118,9 +118,11 @@ partial record Unionf
// and `\` share one precedence level and are folded by one loop in the grammar, so
// a *set difference* on the right must be bracketed or it is re-read as the outer
// operator: `{ 1, 2 } \/ ({ 3 } \ { 1, 2 })` is { 1, 2, 3 } and
- // `({ 1, 2 } \/ { 3 }) \ { 1, 2 }` is { 3 }.
+ // `({ 1, 2 } \/ { 3 }) \ { 1, 2 }` is { 3 }. So must a union that *opens* with a set
+ // difference: `A \/ ((B \ C) \/ D)` flat is `((A \/ B) \ C) \/ D`, and
+ // `{ 1 } \/ (({ 2 } \ { 1 }) \/ { 3 })` would lose its 1. #2758
private protected override string StringizeNode()
- => $@"{Left.Stringize(Left.Priority < Priority)} \/ {Right.Stringize(Right.Priority < Priority || Right is SetMinusf)}";
+ => $@"{Left.Stringize(Left.Priority < Priority)} \/ {Right.Stringize(Right.Priority < Priority || Right.Priority == Priority && Right.OpensWithANonAssociativeOperator())}";
///
public override string ToString() => Stringize();
}
diff --git a/Sources/Tests/UnitTests/Convenience/LatexTest.cs b/Sources/Tests/UnitTests/Convenience/LatexTest.cs
index bd7f0dbf9..40002144c 100644
--- a/Sources/Tests/UnitTests/Convenience/LatexTest.cs
+++ b/Sources/Tests/UnitTests/Convenience/LatexTest.cs
@@ -561,6 +561,12 @@ [Fact] public void SetMinusOfUnionOnTheRight()
=> Test($@"{As} \setminus \left({Bs} \cup {Cs}\right)", A.SetSubtract(B.Unite(C)));
[Fact] public void UnionOfSetMinusOnTheRight()
=> Test($@"{As} \cup \left({Bs} \setminus {Cs}\right)", A.Unite(B.SetSubtract(C)));
+ // https://github.com/asc-community/AngouriMath/issues/2758: a union on the right that
+ // opens with a \setminus re-associates the \setminus, not only the union.
+ [Fact] public void UnionOfAUnionOpeningWithSetMinusOnTheRight()
+ => Test($@"{As} \cup \left({Bs} \setminus {Cs} \cup {As}\right)", A.Unite(B.SetSubtract(C).Unite(A)));
+ [Fact] public void UnionOfAUnionOpeningWithUnionOnTheRightStaysFlat()
+ => Test($@"{As} \cup {Bs} \cup {Cs} \cup \left({As} \setminus {Bs}\right)", A.Unite(B.Unite(C).Unite(A.SetSubtract(B))));
[Fact] public void UnionOfUnionOnTheRightStaysFlat()
=> Test($@"{As} \cup {Bs} \cup {Cs}", A.Unite(B.Unite(C)));
[Fact] public void IntersectionOfIntersectionOnTheRightStaysFlat()
diff --git a/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs b/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs
index a3d5fcf94..5f10c5a3d 100644
--- a/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs
+++ b/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs
@@ -200,6 +200,16 @@ public void TheNodesWithoutAnOperatorSpellingPrintAsTheirFunction(string source)
[InlineData("x mod (y * z)")]
[InlineData("x / (y mod z)")]
[InlineData("x mod (y mod z)")]
+ // An associative operator's right operand of its own level is printed flat, which
+ // re-associates every operator along that operand's left edge -- including a `\` or a
+ // `mod` the operand merely opens with. https://github.com/asc-community/AngouriMath/issues/2758
+ [InlineData(@"A \/ ((B \ C) \/ D)")]
+ [InlineData(@"A \/ (((B \ C) \/ D) \/ E)")]
+ [InlineData(@"x in A \/ ((B \ C) \/ D)")]
+ [InlineData("x * ((y mod z) * w)")]
+ [InlineData("x * ((y mod z) / w)")]
+ [InlineData("x * (((y mod z) * w) / v)")]
+ [InlineData("-((y mod z) * w)")]
// `provided` is the mirror case: it folds to the *right*, so it is the left operand that
// mis-associates. Its value survives either reading -- `(x provided p) provided q` and
// `x provided (p provided q)` are both `x` exactly when `p` and `q` hold -- which is why
@@ -221,12 +231,30 @@ public void TheNodesWithoutAnOperatorSpellingPrintAsTheirFunction(string source)
[InlineData(@"{ 1, 2 } \/ ({ 3 } \ { 1, 2 })")]
[InlineData("2 * (3 mod 2)")]
[InlineData("(3 in { 3 }) in BB")]
+ [InlineData(@"{ 1 } \/ (({ 2 } \ { 1 }) \/ { 3 })")]
+ [InlineData("2 * ((3 mod 2) * 4)")]
+ [InlineData("-((3 mod 2) * 4)")]
public void AMisreadGroupingWouldChangeTheValue(string source)
{
var original = source.ToEntity();
Assert.Equal(original.Evaled, Print(original).ToEntity().Evaled);
}
+ ///
+ /// Simplify's own answers are what reach a reader, and it puts a union in front of a set
+ /// difference on its own: (B \ C) \/ (D \/ { pi }) became { pi } \/ B \ C \/ D,
+ /// from which C can take pi out again.
+ /// https://github.com/asc-community/AngouriMath/issues/2758
+ ///
+ [Theory]
+ [InlineData(@"(B \ C) \/ (D \/ { pi })")]
+ [InlineData(@"({ -1, 1 } \ [-2; +oo)) \/ (QQ \/ { pi })")]
+ public void ASimplifiedSetReadsBackAsItself(string source)
+ {
+ var simplified = source.ToEntity().Simplify();
+ Assert.Equal(simplified, Print(simplified).ToEntity());
+ }
+
///
/// The other half of the rule, pinned so that it is a decision rather than an oversight:
/// an operator that is associative prints flat, because the bracketing then
@@ -244,6 +272,7 @@ public void AMisreadGroupingWouldChangeTheValue(string source)
[InlineData("true xor (false xor true)", "True xor False xor True")]
[InlineData(@"{ 1 } \/ ({ 2 } \/ { 3 })", @"{ 1 } \/ { 2 } \/ { 3 }")]
[InlineData(@"{ 1, 2 } /\ ({ 2, 3 } /\ { 2 })", @"{ 1, 2 } /\ { 2, 3 } /\ { 2 }")]
+ [InlineData(@"{ 1 } \/ (({ 2 } \/ { 3 }) \/ ({ 1 } \ { 2 }))", @"{ 1 } \/ { 2 } \/ { 3 } \/ ({ 1 } \ { 2 })")]
public void AnAssociativeOperatorPrintsFlatAndKeepsItsValue(string source, string expectedFlat)
{
var original = source.ToEntity();