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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
#define THROW_UNSUPPORTED_COMPARISONS
using Microsoft.Dafny;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
namespace Microsoft.Dafny.Triggers {
internal static class DeduplicateExtension {
public static List<T> Deduplicate<T>(this IEnumerable<T> seq, Func<T, T, bool> eq) {
List<T> deduplicated = new List<T>();
foreach (var elem in seq) {
if (!deduplicated.Any(other => eq(elem, other))) {
deduplicated.Add(elem);
}
}
return deduplicated;
}
}
internal struct TriggerMatch {
internal Expression Expr;
internal Expression OriginalExpr;
internal Dictionary<IVariable, Expression> Bindings;
internal static bool Eq(TriggerMatch t1, TriggerMatch t2) {
return ExprExtensions.ExpressionEq(t1.Expr, t2.Expr);
}
internal bool CouldCauseLoops(List<TriggerTerm> terms) {
var expr = Expr;
return !terms.Any(term => term.Expr.ExpressionEqModuloVariableNames(expr));
}
}
internal static class ExprExtensions {
internal static IEnumerable<Expression> AllSubExpressions(this Expression expr, bool wrapOld, bool strict) {
bool isOld = expr is OldExpr;
foreach (var subexpr in expr.SubExpressions) {
foreach (var r_subexpr in AllSubExpressions(subexpr, wrapOld, false)) {
yield return TriggerUtils.MaybeWrapInOld(r_subexpr, isOld);
}
}
if (expr is StmtExpr) {
foreach (var r_subexpr in AllSubExpressions(((StmtExpr)expr).S, wrapOld, false)) {
yield return TriggerUtils.MaybeWrapInOld(r_subexpr, isOld);
}
}
if (!strict) {
yield return expr;
}
}
internal static IEnumerable<Expression> AllSubExpressions(this Statement stmt, bool wrapOld, bool strict) {
foreach (var subexpr in stmt.SubExpressions) {
foreach (var r_subexpr in AllSubExpressions(subexpr, wrapOld, false)) {
yield return r_subexpr;
}
}
foreach (var substmt in stmt.SubStatements) {
foreach (var r_subexpr in AllSubExpressions(substmt, wrapOld, false)) {
yield return r_subexpr;
}
}
}
internal static bool ExpressionEq(this Expression expr1, Expression expr2) {
expr1 = expr1.Resolved;
expr2 = expr2.Resolved;
return ShallowEq_Top(expr1, expr2) && TriggerUtils.SameLists(expr1.SubExpressions, expr2.SubExpressions, (e1, e2) => ExpressionEq(e1, e2));
}
internal static bool ExpressionEqModuloVariableNames(this Expression expr1, Expression expr2) {
expr1 = expr1.Resolved;
expr2 = expr2.Resolved;
if (expr1 is IdentifierExpr) {
return expr2 is IdentifierExpr;
}
return ShallowEq_Top(expr1, expr2) && TriggerUtils.SameLists(expr1.SubExpressions, expr2.SubExpressions, (e1, e2) => ExpressionEqModuloVariableNames(e1, e2));
}
internal static bool MatchesTrigger(this Expression expr, Expression trigger, ISet<BoundVar> holes, Dictionary<IVariable, Expression> bindings) {
expr = expr.Resolved;
trigger = trigger.Resolved;
if (trigger is IdentifierExpr) {
var var = ((IdentifierExpr)trigger).Var;
if (holes.Contains(var)) {
Expression existing_binding = null;
if (bindings.TryGetValue(var, out existing_binding)) {
return ExpressionEq(expr, existing_binding);
} else {
bindings[var] = expr;
return true;
}
}
}
return ShallowEq_Top(expr, trigger) && TriggerUtils.SameLists(expr.SubExpressions, trigger.SubExpressions, (e1, e2) => MatchesTrigger(e1, e2, holes, bindings));
}
private static TriggerMatch? MatchAgainst(this Expression expr, Expression trigger, IEnumerable<BoundVar> holes, Expression originalExpr) {
var bindings = new Dictionary<IVariable, Expression>();
if (expr.MatchesTrigger(trigger, new HashSet<BoundVar>(holes), bindings)) {
return new TriggerMatch { Expr = expr, OriginalExpr = originalExpr ?? expr, Bindings = bindings };
} else {
return null;
}
}
internal static IEnumerable<TriggerMatch> SubexpressionsMatchingTrigger(this QuantifierExpr quantifier, Expression trigger) {
return quantifier.AllSubExpressions(true, true)
.Select(e => TriggerUtils.PrepareExprForInclusionInTrigger(e).MatchAgainst(trigger, quantifier.BoundVars, e))
.Where(e => e.HasValue).Select(e => e.Value);
}
private static bool ShallowSameAttributes(Attributes attributes1, Attributes attributes2) {
return TriggerUtils.SameLists(attributes1.AsEnumerable(), attributes2.AsEnumerable(), ShallowSameSingleAttribute);
}
private static bool ShallowSameSingleAttribute(Attributes arg1, Attributes arg2) {
return arg1.Name == arg2.Name;
}
private static bool SameBoundVar(IVariable arg1, IVariable arg2) {
return arg1 == arg2 ||
(arg1.Name == arg2.Name &&
arg1.CompileName == arg2.CompileName &&
arg1.DisplayName == arg2.DisplayName &&
arg1.UniqueName == arg2.UniqueName &&
arg1.IsGhost == arg2.IsGhost &&
arg1.IsMutable == arg2.IsMutable &&
((arg1.Type == null && arg2.Type == null) || arg1.Type.Equals(arg2.Type)));
}
/// <summary>
/// Compares two abstract syntax expressions, looking only at their direct members. Subexpressions and substatements are not compared.
/// </summary>
/// <returns></returns>
internal static bool ShallowEq_Top(Expression expr1, Expression expr2) {
Contract.Requires(expr1 != null);
Contract.Requires(expr2 != null);
// We never compare concrete expressions
Contract.Requires(!(expr1 is ConcreteSyntaxExpression));
Contract.Requires(!(expr2 is ConcreteSyntaxExpression));
// CPC: Hey future editor: the following block of code is auto-generated. Just add your own cases at the end.
// This could be a visitor pattern, except I need to visit a pair of nodes.
// It could also be implemented in each individual class. I'd have a slight preference for that.
// This really just wants to use double dispatch.
if (expr1 is UnboxingCastExpr && expr2 is UnboxingCastExpr) {
return ShallowEq((UnboxingCastExpr)expr1, (UnboxingCastExpr)expr2);
} else if (expr1 is BoxingCastExpr && expr2 is BoxingCastExpr) {
return ShallowEq((BoxingCastExpr)expr1, (BoxingCastExpr)expr2);
} else if (expr1 is MatchExpr && expr2 is MatchExpr) {
return ShallowEq((MatchExpr)expr1, (MatchExpr)expr2);
} else if (expr1 is ITEExpr && expr2 is ITEExpr) {
return ShallowEq((ITEExpr)expr1, (ITEExpr)expr2);
} else if (expr1 is StmtExpr && expr2 is StmtExpr) {
return ShallowEq((StmtExpr)expr1, (StmtExpr)expr2);
} else if (expr1 is WildcardExpr && expr2 is WildcardExpr) {
return ShallowEq((WildcardExpr)expr1, (WildcardExpr)expr2);
} else if (expr1 is ComprehensionExpr && expr2 is ComprehensionExpr) {
return ShallowEq((ComprehensionExpr)expr1, (ComprehensionExpr)expr2);
} else if (expr1 is NamedExpr && expr2 is NamedExpr) {
return ShallowEq((NamedExpr)expr1, (NamedExpr)expr2);
} else if (expr1 is LetExpr && expr2 is LetExpr) {
return ShallowEq((LetExpr)expr1, (LetExpr)expr2);
} else if (expr1 is TernaryExpr && expr2 is TernaryExpr) {
return ShallowEq((TernaryExpr)expr1, (TernaryExpr)expr2);
} else if (expr1 is BinaryExpr && expr2 is BinaryExpr) {
return ShallowEq((BinaryExpr)expr1, (BinaryExpr)expr2);
} else if (expr1 is UnaryExpr && expr2 is UnaryExpr) {
return ShallowEq((UnaryExpr)expr1, (UnaryExpr)expr2);
} else if (expr1 is MultiSetFormingExpr && expr2 is MultiSetFormingExpr) {
return ShallowEq((MultiSetFormingExpr)expr1, (MultiSetFormingExpr)expr2);
} else if (expr1 is OldExpr && expr2 is OldExpr) {
return ShallowEq((OldExpr)expr1, (OldExpr)expr2);
} else if (expr1 is FunctionCallExpr && expr2 is FunctionCallExpr) {
return ShallowEq((FunctionCallExpr)expr1, (FunctionCallExpr)expr2);
} else if (expr1 is ApplyExpr && expr2 is ApplyExpr) {
return ShallowEq((ApplyExpr)expr1, (ApplyExpr)expr2);
} else if (expr1 is SeqUpdateExpr && expr2 is SeqUpdateExpr) {
return ShallowEq((SeqUpdateExpr)expr1, (SeqUpdateExpr)expr2);
} else if (expr1 is MultiSelectExpr && expr2 is MultiSelectExpr) {
return ShallowEq((MultiSelectExpr)expr1, (MultiSelectExpr)expr2);
} else if (expr1 is SeqSelectExpr && expr2 is SeqSelectExpr) {
return ShallowEq((SeqSelectExpr)expr1, (SeqSelectExpr)expr2);
} else if (expr1 is MemberSelectExpr && expr2 is MemberSelectExpr) {
return ShallowEq((MemberSelectExpr)expr1, (MemberSelectExpr)expr2);
} else if (expr1 is MapDisplayExpr && expr2 is MapDisplayExpr) { //Note: MapDisplayExpr is not a DisplayExpression
return ShallowEq((MapDisplayExpr)expr1, (MapDisplayExpr)expr2);
} else if (expr1 is DisplayExpression && expr2 is DisplayExpression) {
return ShallowEq((DisplayExpression)expr1, (DisplayExpression)expr2);
} else if (expr1 is IdentifierExpr && expr2 is IdentifierExpr) {
return ShallowEq((IdentifierExpr)expr1, (IdentifierExpr)expr2);
} else if (expr1 is ThisExpr && expr2 is ThisExpr) {
return ShallowEq((ThisExpr)expr1, (ThisExpr)expr2);
} else if (expr1 is DatatypeValue && expr2 is DatatypeValue) {
return ShallowEq((DatatypeValue)expr1, (DatatypeValue)expr2);
} else if (expr1 is LiteralExpr && expr2 is LiteralExpr) {
return ShallowEq((LiteralExpr)expr1, (LiteralExpr)expr2);
} else {
// If this assertion fail, then a new abstract AST node was probably introduced but not registered here.
Contract.Assert(expr1.GetType() != expr2.GetType());
return false;
}
}
private static bool ShallowEq(UnboxingCastExpr expr1, UnboxingCastExpr expr2) {
Contract.Requires(false);
throw new InvalidOperationException();
}
private static bool ShallowEq(BoxingCastExpr expr1, BoxingCastExpr expr2) {
return expr1.FromType == expr2.FromType &&
expr1.ToType == expr2.ToType;
}
private static bool ShallowEq(MatchExpr expr1, MatchExpr expr2) {
return true;
}
private static bool ShallowEq(ITEExpr expr1, ITEExpr expr2) {
return true;
}
private static bool ShallowEq(StmtExpr expr1, StmtExpr expr2) {
#if THROW_UNSUPPORTED_COMPARISONS
Contract.Assume(false); // This kind of expression never appears in a trigger
throw new NotImplementedException();
#else
return expr1.S == expr2.S;
#endif
}
private static bool ShallowEq(WildcardExpr expr1, WildcardExpr expr2) {
return true;
}
private static bool ShallowEq(LambdaExpr expr1, LambdaExpr expr2) {
#if THROW_UNSUPPORTED_COMPARISONS
Contract.Assume(false); // This kind of expression never appears in a trigger
throw new NotImplementedException();
#else
return false;
#endif
}
private static bool ShallowEq(MapComprehension expr1, MapComprehension expr2) {
return expr1.Finite == expr2.Finite;
}
private static bool ShallowEq(SetComprehension expr1, SetComprehension expr2) {
return expr1.TermIsImplicit == expr2.TermIsImplicit && //TODO
expr1.Finite == expr2.Finite;
}
private static bool ShallowEq(ExistsExpr expr1, ExistsExpr expr2) {
return true;
}
private static bool ShallowEq(ForallExpr expr1, ForallExpr expr2) {
return true;
}
private static bool ShallowEq(QuantifierExpr expr1, QuantifierExpr expr2) {
if (!TriggerUtils.SameNullity(expr1.SplitQuantifier, expr2.SplitQuantifier)) {
return false;
}
if (expr1.SplitQuantifier != null && expr2.SplitQuantifier != null) {
return ShallowEq_Top(expr1.SplitQuantifierExpression, expr2.SplitQuantifierExpression);
}
if (expr1.TypeArgs.Count != expr2.TypeArgs.Count ||
!TriggerUtils.SameNullity(expr1.Range, expr2.Range)) {
return false;
}
if (expr1 is ExistsExpr && expr2 is ExistsExpr) {
return ShallowEq((ExistsExpr)expr1, (ExistsExpr)expr2);
} else if (expr1 is ForallExpr && expr2 is ForallExpr) {
return ShallowEq((ForallExpr)expr1, (ForallExpr)expr2);
} else {
return false;
}
}
private static bool ShallowEq(ComprehensionExpr expr1, ComprehensionExpr expr2) {
if (!TriggerUtils.SameLists(expr1.BoundVars, expr2.BoundVars, SameBoundVar) ||
!ShallowSameAttributes(expr1.Attributes, expr2.Attributes) ||
// Filled in during resolution: !SameLists(expr1.Bounds, expr2.Bounds, ReferenceCompare) ||
// !SameLists(expr1.MissingBounds, expr2.MissingBounds, SameBoundVar) ||
!TriggerUtils.SameNullity(expr1.Range, expr2.Range)) { //TODO Check
return false;
}
if (expr1 is LambdaExpr && expr2 is LambdaExpr) {
return ShallowEq((LambdaExpr)expr1, (LambdaExpr)expr2);
} else if (expr1 is MapComprehension && expr2 is MapComprehension) {
return ShallowEq((MapComprehension)expr1, (MapComprehension)expr2);
} else if (expr1 is SetComprehension && expr2 is SetComprehension) {
return ShallowEq((SetComprehension)expr1, (SetComprehension)expr2);
} else if (expr1 is QuantifierExpr && expr2 is QuantifierExpr) {
return ShallowEq((QuantifierExpr)expr1, (QuantifierExpr)expr2);
} else {
return false; // ComprehensionExpr is abstract
}
}
private static bool ShallowEq(NamedExpr expr1, NamedExpr expr2) {
return expr1.Name == expr2.Name &&
TriggerUtils.SameNullity(expr1.Contract, expr2.Contract);
}
private static bool ShallowEq(LetExpr expr1, LetExpr expr2) {
return expr1.Exact == expr2.Exact &&
ShallowSameAttributes(expr1.Attributes, expr2.Attributes);
}
private static bool ShallowEq(TernaryExpr expr1, TernaryExpr expr2) {
return expr1.Op == expr2.Op;
}
private static bool ShallowEq(BinaryExpr expr1, BinaryExpr expr2) {
Contract.Requires(expr1.ResolvedOp != BinaryExpr.ResolvedOpcode.YetUndetermined);
Contract.Requires(expr2.ResolvedOp != BinaryExpr.ResolvedOpcode.YetUndetermined);
return expr1.ResolvedOp == expr2.ResolvedOp;
}
private static bool ShallowEq(ConversionExpr expr1, ConversionExpr expr2) {
return expr1.Type == expr2.Type; //TODO equality on types?
}
private static bool ShallowEq(UnaryOpExpr expr1, UnaryOpExpr expr2) {
return expr1.Op == expr2.Op;
}
private static bool ShallowEq(UnaryExpr expr1, UnaryExpr expr2) {
if (expr1 is ConversionExpr && expr2 is ConversionExpr) {
return ShallowEq((ConversionExpr)expr1, (ConversionExpr)expr2);
} else if (expr1 is UnaryOpExpr && expr2 is UnaryOpExpr) {
return ShallowEq((UnaryOpExpr)expr1, (UnaryOpExpr)expr2);
} else {
return false; // UnaryExpr is abstract
}
}
private static bool ShallowEq(MultiSetFormingExpr expr1, MultiSetFormingExpr expr2) {
return true;
}
private static bool ShallowEq(OldExpr expr1, OldExpr expr2) {
return true;
}
private static bool ShallowEq(FunctionCallExpr expr1, FunctionCallExpr expr2) {
return expr1.Name == expr2.Name &&
expr1.CoCall == expr2.CoCall && //TODO
expr1.Function == expr2.Function; // TODO TypeArgumentSubstitutions?
}
private static bool ShallowEq(ApplyExpr expr1, ApplyExpr expr2) {
return true;
}
private static bool ShallowEq(SeqUpdateExpr expr1, SeqUpdateExpr expr2) {
Contract.Requires(expr1.ResolvedUpdateExpr != null && expr2.ResolvedUpdateExpr != null);
return true;
}
private static bool ShallowEq(MultiSelectExpr expr1, MultiSelectExpr expr2) {
return true;
}
private static bool ShallowEq(SeqSelectExpr expr1, SeqSelectExpr expr2) {
return expr1.SelectOne == expr2.SelectOne &&
TriggerUtils.SameNullity(expr1.Seq, expr2.Seq) &&
TriggerUtils.SameNullity(expr1.E0, expr2.E0) &&
TriggerUtils.SameNullity(expr1.E1, expr2.E1);
}
private static bool ShallowEq(MemberSelectExpr expr1, MemberSelectExpr expr2) {
return expr1.MemberName == expr2.MemberName &&
expr1.Member == expr2.Member &&
TriggerUtils.SameLists(expr1.TypeApplication, expr2.TypeApplication, Microsoft.Dafny.Type.Equals);
}
private static bool ShallowEq(SeqDisplayExpr expr1, SeqDisplayExpr expr2) {
return true;
}
private static bool ShallowEq(MapDisplayExpr expr1, MapDisplayExpr expr2) {
return expr1.Finite == expr2.Finite;
}
private static bool ShallowEq(MultiSetDisplayExpr expr1, MultiSetDisplayExpr expr2) {
return true;
}
private static bool ShallowEq(SetDisplayExpr expr1, SetDisplayExpr expr2) {
return expr1.Finite == expr2.Finite;
}
private static bool ShallowEq(DisplayExpression expr1, DisplayExpression expr2) {
if (expr1 is SeqDisplayExpr && expr2 is SeqDisplayExpr) {
return ShallowEq((SeqDisplayExpr)expr1, (SeqDisplayExpr)expr2);
} else if (expr1 is MultiSetDisplayExpr && expr2 is MultiSetDisplayExpr) {
return ShallowEq((MultiSetDisplayExpr)expr1, (MultiSetDisplayExpr)expr2);
} else if (expr1 is SetDisplayExpr && expr2 is SetDisplayExpr) {
return ShallowEq((SetDisplayExpr)expr1, (SetDisplayExpr)expr2);
} else {
return false;
}
}
private static bool ShallowEq(AutoGhostIdentifierExpr expr1, AutoGhostIdentifierExpr expr2) {
return true;
}
private static bool ShallowEq(IdentifierExpr expr1, IdentifierExpr expr2) {
if (expr1.Name != expr2.Name ||
expr1.Var != expr2.Var) {
return false;
}
if (expr1 is AutoGhostIdentifierExpr && expr2 is AutoGhostIdentifierExpr) {
return ShallowEq((AutoGhostIdentifierExpr)expr1, (AutoGhostIdentifierExpr)expr2);
} else {
return true;
}
}
private static bool ShallowEq(ImplicitThisExpr expr1, ImplicitThisExpr expr2) {
return true;
}
private static bool ShallowEq(ThisExpr expr1, ThisExpr expr2) {
if (expr1 is ImplicitThisExpr && expr2 is ImplicitThisExpr) {
return ShallowEq((ImplicitThisExpr)expr1, (ImplicitThisExpr)expr2);
} else {
return (expr1.GetType() == expr2.GetType()); // LiteralExpr is not abstract
}
}
private static bool ShallowEq(DatatypeValue expr1, DatatypeValue expr2) {
return // Implied by Ctor equality: expr1.DatatypeName == expr2.DatatypeName &&
// Implied by Ctor equality: expr1.MemberName == expr2.MemberName &&
expr1.Ctor == expr2.Ctor &&
// Contextual information: expr1.IsCoCall == expr2.IsCoCall &&
TriggerUtils.SameLists(expr1.InferredTypeArgs, expr2.InferredTypeArgs, Microsoft.Dafny.Type.Equals);
}
private static bool ShallowEq(StringLiteralExpr expr1, StringLiteralExpr expr2) {
return true;
}
private static bool ShallowEq(CharLiteralExpr expr1, CharLiteralExpr expr2) {
return true;
}
private static bool ShallowEq(StaticReceiverExpr expr1, StaticReceiverExpr expr2) {
return true;
}
private static bool ShallowEq(LiteralExpr expr1, LiteralExpr expr2) {
if (!TriggerUtils.SameNullity(expr1.Value, expr2.Value) || (expr1.Value != null && !expr1.Value.Equals(expr2.Value))) {
return false;
}
if (expr1 is StringLiteralExpr && expr2 is StringLiteralExpr) {
return ShallowEq((StringLiteralExpr)expr1, (StringLiteralExpr)expr2);
} else if (expr1 is CharLiteralExpr && expr2 is CharLiteralExpr) {
return ShallowEq((CharLiteralExpr)expr1, (CharLiteralExpr)expr2);
} else if (expr1 is StaticReceiverExpr && expr2 is StaticReceiverExpr) {
return ShallowEq((StaticReceiverExpr)expr1, (StaticReceiverExpr)expr2);
} else {
return (expr1.GetType() == expr2.GetType()); // LiteralExpr is not abstract
}
}
}
}
|