Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ partial record Unionf
{
/// <inheritdoc/>
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ partial record Complexes
partial record Unionf
{
/// <inheritdoc/>
// 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))}";
/// <inheritdoc/>
public override string ToString() => Stringize();
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Tests/UnitTests/Convenience/StringizeRoundTripTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)")]
Expand Down Expand Up @@ -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)
Expand Down