Skip to content

Commit fbfb4db

Browse files
authored
Stringize provided as associative (#660)
1 parent d2ebbbb commit fbfb4db

File tree

6 files changed

+8
-6
lines changed

6 files changed

+8
-6
lines changed

Sources/AngouriMath/Core/Antlr/AngouriMath.g

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22

33
Remember to run the "antlr_rerun.bat" file located at "Sources/Utils/antlr_rerun.bat" (relative to the repository root)
44
every time you modify this file so that the generated source files under the Antlr folder are updated and changes are
@@ -219,6 +219,8 @@ Keyword nodes
219219
provided_expression returns[Entity value]
220220
: expr = implies_expression { $value = $expr.value; }
221221
('provided' pred = provided_expression { $value = $value.Provided($pred.value); })?
222+
// note: even though Provided is associative, we parse it right-to-left matching natural language
223+
// "I'll go, provided you go, provided it's sunny" - Natural reading: I'll go ← (you go ← sunny)
222224
;
223225

224226
/*

Sources/AngouriMath/Functions/Output/Latex/Latex.Omni.Classes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ public override string Latexise()
7979
partial record Providedf
8080
{
8181
/// <inheritdoc/>
82-
public override string Latexise() => $@"{Expression.Latexise(Expression.LatexPriority < LatexPriority)} \quad \text{{for}} \quad {Predicate.Latexise(Predicate.LatexPriority <= LatexPriority)}";
82+
public override string Latexise() => $@"{Expression.Latexise(Expression.LatexPriority < LatexPriority)} \quad \text{{for}} \quad {Predicate.Latexise(Predicate.LatexPriority < LatexPriority)}";
8383
}
8484

8585
partial record Piecewise
8686
{
8787
/// <inheritdoc/>
88-
public override string Latexise() => $@"\begin{{cases}}" +
88+
public override string Latexise() => @"\begin{cases}" +
8989
string.Join(@"\\",
9090
Cases.Select(c =>
9191
{

Sources/AngouriMath/Functions/Output/ToString/ToString.Omni.Classes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public override string Stringize()
127127
partial record Providedf
128128
{
129129
/// <inheritdoc/>
130-
public override string Stringize() => $@"{Expression.Stringize(Expression.Priority <= Priority.Provided)} provided {Predicate.Stringize(Predicate.Priority < Priority.Provided)}";
130+
public override string Stringize() => $@"{Expression.Stringize(Expression.Priority < Priority.Provided)} provided {Predicate.Stringize(Predicate.Priority < Priority.Provided)}";
131131
/// <inheritdoc/>
132132
public override string ToString() => Stringize();
133133
}

Sources/Tests/UnitTests/Convenience/FromStringTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public void TestFormula8()
156156
[Fact] public void TestProvided1() => Assert.Equal(MathS.Provided("a", "b"), FromString("a provided b"));
157157
[Fact] public void TestProvided2() => Assert.Equal(MathS.Provided("a", MathS.Provided("b", "c")), FromString("a provided b provided c"));
158158
[Fact] public void TestProvided3() => Assert.Equal(MathS.Provided(MathS.Provided("a", "b"), "c"), FromString("(a provided b) provided c"));
159+
[Fact] public void TestProvided4() => Assert.Equal(FromString("a provided (b provided c)").Stringize(), FromString("(a provided b) provided c").Stringize());
159160

160161
[Theory]
161162
[InlineData("sh", "Sinh")]

Sources/Tests/UnitTests/Convenience/LatexTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ [Fact] public void Provided2()
314314
[Fact] public void Provided3()
315315
=> Test(@"a \quad \text{for} \quad b \quad \text{for} \quad c", MathS.Provided(MathS.Provided("a", "b"), "c"));
316316
[Fact] public void Provided4()
317-
=> Test(@"\left(a \quad \text{for} \quad b \quad \text{for} \quad \left(c \quad \text{for} \quad d\right)\right) \to \top ", MathS.Provided(MathS.Provided("a", "b"), MathS.Provided("c", "d")).Implies(Entity.Boolean.True));
317+
=> Test(@"\left(a \quad \text{for} \quad b \quad \text{for} \quad c \quad \text{for} \quad d\right) \to \top ", MathS.Provided(MathS.Provided("a", "b"), MathS.Provided("c", "d")).Implies(Entity.Boolean.True));
318318
// Juxtaposition tests
319319
[Fact] public void M1InTheMiddle() => Test(@"x \left(-1\right) \cdot x", (x * (-1)) * x);
320320
[Fact] public void MultiplyNumberWithPower() => Test(@"2 \cdot {3}^{4}", 2 * ((Entity)3).Pow(4));

Sources/Tests/UnitTests/Convenience/PriorityTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class PriorityTest
4040
[InlineData("not a or not b implies not c or not d", "((not a) or (not b)) implies ((not c) or (not d))")]
4141
[InlineData("a = b and b > c and d", "a = b > c and d")]
4242
[InlineData("a provided b", "a provided b")]
43-
[InlineData("(a provided b) provided c", "(a provided b) provided c")]
4443
[InlineData("a provided b provided c", "a provided (b provided c)")]
4544
[InlineData("a provided b and c", "a provided (b and c)")]
4645
[InlineData("a provided b + c > 0", "a provided (b + c > 0)")]

0 commit comments

Comments
 (0)