Repository navigation
Expand file tree
/
Copy pathEGraphTest.cs
More file actions
468 lines (411 loc) · 18.5 KB
/
Copy pathEGraphTest.cs
File metadata and controls
468 lines (411 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//
using System.Linq;
using AngouriMath;
using AngouriMath.Core;
using AngouriMath.Core.Transformations;
using AngouriMath.Extensions;
using Xunit;
namespace AngouriMath.Tests.Core.Transformations
{
public sealed class EGraphTest
{
[Fact]
public void AddingTheSameLeafTwiceReturnsTheSameClass()
{
var graph = new EGraph();
var first = graph.Add("x");
var second = graph.Add("x");
Assert.Equal(first, second);
}
[Fact]
public void AddingTwoDifferentLeavesReturnsDifferentClasses()
{
var graph = new EGraph();
var x = graph.Add("x");
var y = graph.Add("y");
Assert.NotEqual(x, y);
}
[Fact]
public void UnionMakesFindAgreeOnBothClasses()
{
var graph = new EGraph();
var x = graph.Add("x");
var y = graph.Add("y");
graph.Union(x, y);
Assert.Equal(graph.Find(x), graph.Find(y));
}
[Fact]
public void UnionOfTheSameClassReportsNoChange()
{
var graph = new EGraph();
var x = graph.Add("x");
Assert.False(graph.Union(x, x));
}
[Fact]
public void UnionOfTwoDifferentClassesReportsAChange()
{
var graph = new EGraph();
var x = graph.Add("x");
var y = graph.Add("y");
Assert.True(graph.Union(x, y));
}
[Fact]
public void UnionReducesTheClassCountByOne()
{
var graph = new EGraph();
var x = graph.Add("x");
var y = graph.Add("y");
var before = graph.ClassCount;
graph.Union(x, y);
Assert.Equal(before - 1, graph.ClassCount);
}
[Fact]
public void RebuildMergesNodesThatBecomeCongruentAfterAUnion()
{
// f(x) and f(y) are different nodes until x and y are unioned; the congruence
// rebuild is what then notices the two applications of f agree too.
var graph = new EGraph();
var x = graph.Add("x");
var y = graph.Add("y");
var fx = graph.Add("f", x);
var fy = graph.Add("f", y);
Assert.NotEqual(graph.Find(fx), graph.Find(fy));
graph.Union(x, y);
graph.Rebuild();
Assert.Equal(graph.Find(fx), graph.Find(fy));
}
[Fact]
public void NodesOfReturnsTheNodeJustAdded()
{
var graph = new EGraph();
var x = graph.Add("x");
Assert.Contains(new ENode("x", System.Array.Empty<int>()), graph.NodesOf(x));
}
[Fact]
public void ClassesListsEveryClassAdded()
{
var graph = new EGraph();
var x = graph.Add("x");
var y = graph.Add("y");
Assert.Equal(new[] { x, y }, graph.Classes.OrderBy(id => id));
}
[Theory]
// x + 0 -> x, and commutatively 0 + x -> x.
[InlineData("Sumf", "x", "0", true)]
[InlineData("Sumf", "0", "x", true)]
// x - 0 -> x, but 0 - x is a different value (its negation), not a fold.
[InlineData("Minusf", "x", "0", true)]
[InlineData("Minusf", "0", "x", false)]
// x * 1 -> x, and commutatively 1 * x -> x.
[InlineData("Mulf", "x", "1", true)]
[InlineData("Mulf", "1", "x", true)]
// x / 1 -> x, but 1 / x is a different value (its reciprocal), not a fold.
[InlineData("Divf", "x", "1", true)]
[InlineData("Divf", "1", "x", false)]
// x ^ 1 -> x, but 1 ^ x is the constant 1, not this operand -- not this fold either.
[InlineData("Powf", "x", "1", true)]
[InlineData("Powf", "1", "x", false)]
public void NeutralElementFoldsIntoTheOtherOperandsClassOnlyWhenTheValuesAgree(
string op, string leftLeaf, string rightLeaf, bool shouldFold)
{
var graph = new EGraph();
var left = graph.Add(leftLeaf);
var right = graph.Add(rightLeaf);
var before = graph.NodeCount;
var result = graph.Add(op, left, right);
var nonLeafOperand = leftLeaf == "x" ? left : right;
if (shouldFold)
{
Assert.Equal(graph.Find(nonLeafOperand), graph.Find(result));
Assert.Equal(before, graph.NodeCount);
}
else
{
Assert.NotEqual(graph.Find(nonLeafOperand), graph.Find(result));
Assert.Equal(before + 1, graph.NodeCount);
}
}
[Theory]
[InlineData("x + 1")]
[InlineData("sin(x) * cos(y)")]
[InlineData("(x + 1) ^ 2")]
public void AnEntityAddedAndExtractedUnchangedRoundTrips(string source)
{
var expr = source.ToEntity();
var graph = new EGraph();
var root = graph.AddEntity(expr);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.NotNull(extracted);
Assert.True(expr.Equals(extracted));
}
[Fact]
public void FoldingOnInsertionMeansNoRuleIsNeededToDropAnAddedZero()
{
var expr = "x + 0".ToEntity();
var graph = new EGraph();
var root = graph.AddEntity(expr);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.True("x".ToEntity().Equals(extracted));
}
/// <summary>
/// <see cref="EGraph.Extract"/> rebuilds a non-leaf node through
/// <see cref="AngouriMath.Core.Transformations.Matching.MatchPattern.ConstructNode"/>,
/// which never restores <see cref="Entity.Codomain"/> -- an init-settable, per-node-type
/// property the rest of the codebase treats as load-bearing and explicitly preserves on
/// every <c>Replace</c>. Caught in code review before this PR was merged.
/// </summary>
[Fact]
public void ExtractPreservesANarrowedCodomain()
{
Entity narrowed = MathS.Sqrt(-1).WithCodomain(Domain.Real);
var graph = new EGraph();
var root = graph.AddEntity(narrowed);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.NotNull(extracted);
Assert.Equal(Domain.Real, extracted!.Codomain);
Assert.Equal(narrowed, extracted);
Assert.Equal(narrowed.Evaled, extracted.Evaled);
}
/// <summary>
/// <see cref="EGraph.Key"/> keys a leaf by <see cref="Entity.Stringize"/>, and
/// <see cref="Entity.Constant.EulerIntrinsic"/> prints identically to the ordinary named
/// constant <c>e</c> -- the two are <see cref="object.Equals(object)"/>-equal by design,
/// but only <see cref="EulerIntrinsic"/> is meant to stay outside what a binder over the
/// name <c>e</c> can capture. Re-parsing the printed form silently swaps it for the named
/// constant, which is invisible to every equality-based check and only shows up at a
/// binder. Caught in code review before this PR was merged.
/// </summary>
[Fact]
public void ExtractPreservesEulerIntrinsicIdentity()
{
var graph = new EGraph();
var id = graph.AddEntity(Entity.Constant.EulerIntrinsic);
var extracted = graph.Extract(id, CostModel.Default.Cost);
Assert.True(ReferenceEquals(Entity.Constant.EulerIntrinsic, extracted));
}
/// <summary>
/// A cost model that answers <see cref="double.NaN"/> has not ranked the candidate, and
/// every IEEE-754 comparison against <see cref="double.NaN"/> is false -- so the
/// cheapest-so-far test never declines it. A model that <i>throws</i> is already declined
/// by the surrounding <see langword="catch"/>; one that answers
/// <see cref="double.NaN"/> is saying the same thing and is declined the same way.
/// </summary>
[Fact]
public void ExtractDeclinesACandidateItsCostModelCannotRank()
{
var graph = new EGraph();
var root = graph.AddEntity("x".ToEntity());
var extracted = graph.Extract(root, static _ => double.NaN);
Assert.Null(extracted);
}
/// <summary>
/// The consequence of the above when a class holds more than one candidate: an unranked
/// candidate that is not declined becomes the incumbent cheapest, and because every later
/// comparison against it is false, every candidate after it wins unconditionally --
/// <see cref="EGraph.Extract"/> stops answering with the cheapest and answers with
/// whichever member the enumeration reached last.
/// </summary>
[Fact]
public void ExtractPicksTheCheapestPastACandidateItCannotRank()
{
var graph = new EGraph();
var cheap = graph.AddEntity("x".ToEntity());
graph.Union(cheap, graph.AddEntity("y".ToEntity()));
graph.Union(cheap, graph.AddEntity("z".ToEntity()));
var extracted = graph.Extract(cheap, static candidate => candidate.Stringize() switch
{
"x" => 1,
"y" => double.NaN,
_ => 100
});
Assert.True("x".ToEntity().Equals(extracted));
}
/// <summary>
/// <see cref="System.Collections.Generic.HashSet{T}"/> enumeration order is an
/// unspecified implementation detail, and a string's hash code is randomised per process
/// -- so without a defined order over a class's members, an exact cost tie is settled
/// differently from one run to the next. <see cref="CostModel"/>'s own remarks say ties
/// are common enough to design the models against, and a bounded computation is meant to
/// be reproducible given a defined algorithm order. A tie goes to the ordinally-first
/// e-node.
/// </summary>
[Fact]
public void ExtractSettlesACostTieOnADefinedOrder()
{
var graph = new EGraph();
var id = graph.AddEntity("b".ToEntity());
graph.Union(id, graph.AddEntity("c".ToEntity()));
graph.Union(id, graph.AddEntity("a".ToEntity()));
var extracted = graph.Extract(id, static _ => 1);
Assert.True("a".ToEntity().Equals(extracted));
}
/// <summary>
/// <see cref="EGraph.Extract"/> used to recurse through an e-class's children and decline
/// to build past 256 levels, because a <see cref="System.StackOverflowException"/> cannot
/// be caught. It is a fixed point from the leaves up now, with no stack to exhaust, so a
/// chain three hundred deep extracts as itself -- this test asserted <see langword="null"/>
/// for that input while the guard existed.
/// </summary>
[Fact]
public void ExtractBuildsAChainDeeperThanTheOldCap()
{
Entity deep = "x".ToEntity();
for (var i = 0; i < 300; i++) deep += 1;
var graph = new EGraph();
var root = graph.AddEntity(deep);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.Equal(deep, extracted);
}
/// <summary>
/// The cap is a crash guard, not a quality knob: an expression of ordinary depth must
/// still extract normally.
/// </summary>
[Fact]
public void ExtractStillBuildsAnExpressionOfOrdinaryDepth()
{
Entity ordinary = "x".ToEntity();
for (var i = 0; i < 30; i++) ordinary += 1;
var graph = new EGraph();
var root = graph.AddEntity(ordinary);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.NotNull(extracted);
}
/// <summary>
/// <see cref="EGraph.Extract"/> rebuilds through the node types
/// <c>MatchPattern.Construct</c> knows, and that used to be fourteen arithmetic and
/// trigonometric ones. Every other root -- a comparison, a condition attached to a value,
/// a set operation -- extracted as <see langword="null"/>, so
/// <c>Transformation.EqualitySaturation</c> handed back its input reporting
/// <c>Changed = false</c>, which is indistinguishable from "already at its cheapest" even
/// where the graph had proved an improvement inside it.
/// </summary>
[Theory]
[InlineData("x > 0")]
[InlineData("x = y")]
[InlineData("x >= y + 1")]
[InlineData("a and b")]
[InlineData("not a")]
[InlineData("x mod y")]
[InlineData("floor(x)")]
[InlineData("arcsin(x)")]
[InlineData("x!")]
// A special set is a leaf, so both operands rebuild and the union over them does too.
[InlineData("RR unite ZZ")]
public void ExtractRebuildsRootsThatUsedToFallOutsideTheBuildableTypes(string source)
{
var expr = source.ToEntity();
var graph = new EGraph();
var root = graph.AddEntity(expr);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.NotNull(extracted);
Assert.True(expr.Equals(extracted), $"{source} came back as {extracted!.Stringize()}");
}
/// <summary>
/// The case the review named specifically: a rule that does not hold unconditionally
/// wraps its result in a <see cref="Entity.Providedf"/>, which is the registry's own
/// documented convention -- and that wrapper used to be unbuildable, so the class it was
/// unioned onto was a dead end and the improvement never reached extraction.
/// </summary>
[Fact]
public void ExtractRebuildsAConditionAttachedToAValue()
{
Entity provided = new Entity.Providedf("x".ToEntity(), "x > 0".ToEntity());
var graph = new EGraph();
var root = graph.AddEntity(provided);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.NotNull(extracted);
Assert.IsType<Entity.Providedf>(extracted);
Assert.True(provided.Equals(extracted));
}
/// <summary>
/// A binder is still not rebuilt, and that is deliberate rather than pending: the e-graph
/// has no notion of a bound variable's scope, and <c>DirectChildren</c> hands out a
/// capture-avoidingly renamed body, so rebuilding one from an e-class would produce a term
/// meaning something else.
/// </summary>
/// <remarks>
/// The variable-arity nodes are declined for a different reason -- the table keys on a
/// type and an arity of one or two, so an n-child node has no entry -- and a union over
/// two finite sets shows that this reaches the root through its operands: the
/// <c>Unionf</c> itself is buildable and its operands are not.
/// </remarks>
[Theory]
[InlineData("{ x : x > 0 }")] // a binder: no notion of scope here
[InlineData("{ 1, 2 }")] // variable arity
[InlineData("{ 1, 2 } unite { 2, 3 }")] // buildable root, unbuildable operands
public void ExtractStillDeclinesWhatItCannotFaithfullyRebuild(string source)
{
var graph = new EGraph();
var root = graph.AddEntity(source.ToEntity());
Assert.Null(graph.Extract(root, CostModel.Default.Cost));
}
/// <summary>
/// The folds the e-graph performs on insertion are read off <see cref="Entity.InnerSimplified"/>
/// rather than written out again, so the two cannot disagree. This names what that
/// derivation currently finds — not to restate the table, but so that a change in
/// <c>InnerSimplify</c> shows up here as a changed list rather than as e-graph folding
/// quietly gaining or losing a case.
/// </summary>
/// <remarks>
/// The asymmetries are the point. <c>x + 0</c> and <c>0 + x</c> both fold, because
/// addition is commutative; <c>x - 0</c> folds and <c>0 - x</c> does not, because that is
/// a negation and not this operand; <c>x ^ 1</c> folds and <c>1 ^ x</c> does not, because
/// that is the constant 1.
/// </remarks>
[Fact]
public void TheFoldsAreExactlyWhatInnerSimplifyDoes()
=> Assert.Equal(
new[]
{
"Divf(x, 1)", "Minusf(x, 0)", "Mulf(1, x)", "Mulf(x, 1)",
"Powf(x, 1)", "Sumf(0, x)", "Sumf(x, 0)",
},
EGraph.NeutralFolds.OrderBy(fold => fold, System.StringComparer.Ordinal).ToArray());
[Theory]
// The e-graph folds exactly where InnerSimplify does, so these agree by construction.
[InlineData("x + 0")]
[InlineData("0 + x")]
[InlineData("x - 0")]
[InlineData("x * 1")]
[InlineData("1 * x")]
[InlineData("x / 1")]
[InlineData("x ^ 1")]
public void AFoldedInsertionAgreesWithInnerSimplify(string source)
{
var expr = source.ToEntity();
var graph = new EGraph();
var root = graph.AddEntity(expr);
var extracted = graph.Extract(root, CostModel.Default.Cost);
Assert.Equal(expr.InnerSimplified, extracted);
}
[Fact]
public void ContainsLeafFindsAMatchingLiteral()
{
var graph = new EGraph();
var id = graph.AddEntity("2".ToEntity());
Assert.True(graph.ContainsLeaf(id, "2".ToEntity()));
Assert.False(graph.ContainsLeaf(id, "3".ToEntity()));
}
[Fact]
public void RuntimeTypeOfALeafIsItsParsedType()
{
var graph = new EGraph();
var id = graph.AddEntity("x".ToEntity());
var node = graph.NodesOf(id).Single();
Assert.Equal(typeof(Entity.Variable), EGraph.RuntimeType(node));
}
[Fact]
public void RuntimeTypeOfANonLeafIsItsNodeType()
{
var graph = new EGraph();
var id = graph.AddEntity("x + y".ToEntity());
var node = graph.NodesOf(id).Single();
Assert.Equal(typeof(Entity.Sumf), EGraph.RuntimeType(node));
}
}
}