diff --git a/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs b/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs index 7491bfaba..619f62304 100644 --- a/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs +++ b/Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs @@ -74,11 +74,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. + // them left, exactly as the grammar here does. Keep a set difference at the start + // of the right operand grouped with that operand; plain right unions stay flat. 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 || StartsWithSetDifference(Right))}"; } partial record Intersectionf diff --git a/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs b/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs index b78829987..08c05d7fc 100644 --- a/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs +++ b/Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs @@ -114,13 +114,15 @@ partial record Complexes partial record Unionf { /// - // Union is associative, so a union on the right may stay unbracketed -- but `\/` - // 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 }. + // Union is associative, so a plain union on the right may stay unbracketed. But + // `\/` and `\` share one precedence level and are folded left; a set difference + // at the start of the right operand must remain grouped with that operand. + private static bool StartsWithSetDifference(Entity entity) + => entity is SetMinusf + || entity is Unionf union && StartsWithSetDifference(union.Left); + 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 || StartsWithSetDifference(Right))}"; /// public override string ToString() => Stringize(); } diff --git a/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs b/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs index a3d5fcf94..1c6f9614b 100644 --- a/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs +++ b/Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs @@ -189,6 +189,8 @@ public void TheNodesWithoutAnOperatorSpellingPrintAsTheirFunction(string source) [InlineData(@"A \ (B \ C)")] [InlineData(@"(A \ B) \ C")] [InlineData(@"A \/ (B \ C)")] + [InlineData(@"A \/ ((B \ C) \/ D)")] + [InlineData(@"A \/ (((B \ C) \/ E) \/ D)")] [InlineData(@"A \ (B \/ C)")] [InlineData(@"(A \/ B) \ C")] [InlineData(@"A \ (B /\ C)")] @@ -219,6 +221,7 @@ public void TheNodesWithoutAnOperatorSpellingPrintAsTheirFunction(string source) [InlineData(@"{ 1, 2, 3 } \ ({ 2, 3 } \ { 3 })")] [InlineData(@"{ 1, 2 } \ ({ 2 } \/ { 3 })")] [InlineData(@"{ 1, 2 } \/ ({ 3 } \ { 1, 2 })")] + [InlineData(@"{ 1 } \/ (({ 2 } \ { 1 }) \/ { 3 })")] [InlineData("2 * (3 mod 2)")] [InlineData("(3 in { 3 }) in BB")] public void AMisreadGroupingWouldChangeTheValue(string source)