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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.Basetypes;
using Microsoft.Boogie.VCExprAST;
namespace Microsoft.Boogie.Z3
{
public class Z3apiExprLineariser : IVCExprVisitor<Z3TermAst, LineariserOptions>
{
private Z3apiOpLineariser OpLinObject = null;
private IVCExprOpVisitor<Z3TermAst, LineariserOptions> OpLineariser
{
get
{
Contract.Ensures(Contract.Result<IVCExprOpVisitor<bool, LineariserOptions>>() != null);
if (OpLinObject == null)
OpLinObject = new Z3apiOpLineariser(this);
return OpLinObject;
}
}
internal readonly UniqueNamer namer;
internal readonly Dictionary<VCExprVar, Z3TermAst> letBindings;
protected Z3Context cm;
public Z3apiExprLineariser(Z3Context cm, UniqueNamer namer)
{
this.cm = cm;
this.namer = namer;
this.letBindings = new Dictionary<VCExprVar, Z3TermAst>();
}
public Z3TermAst Linearise(VCExpr expr, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(expr != null);
return expr.Accept<Z3TermAst, LineariserOptions>(this, options);
}
/////////////////////////////////////////////////////////////////////////////////////
public Z3TermAst Visit(VCExprLiteral node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
if (node == VCExpressionGenerator.True)
return cm.MakeTrue();
else if (node == VCExpressionGenerator.False)
return cm.MakeFalse();
else if (node is VCExprIntLit)
return cm.MakeIntLiteral(((VCExprIntLit)node).Val.ToString());
else
{
Contract.Assert(false);
throw new cce.UnreachableException();
}
}
public Z3TermAst Visit(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
VCExprOp op = node.Op;
Contract.Assert(op != null);
if (op.Equals(VCExpressionGenerator.AndOp) || op.Equals(VCExpressionGenerator.OrOp))
{
// handle these operators without recursion
List<Z3TermAst> asts = new List<Z3TermAst>();
string opString = op.Equals(VCExpressionGenerator.AndOp) ? "AND" : "OR";
IEnumerator enumerator = new VCExprNAryUniformOpEnumerator(node);
Contract.Assert(enumerator != null);
while (enumerator.MoveNext())
{
VCExprNAry naryExpr = enumerator.Current as VCExprNAry;
if (naryExpr == null || !naryExpr.Op.Equals(op))
{
asts.Add(Linearise(cce.NonNull((VCExpr)enumerator.Current), options));
}
}
return cm.Make(opString, asts);
}
return node.Accept<Z3TermAst, LineariserOptions>(OpLineariser, options);
}
public Z3TermAst Visit(VCExprVar node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
if (letBindings.ContainsKey(node))
{
return letBindings[node];
}
else
{
string varName = namer.GetName(node, node.Name);
return cm.GetConstant(varName, node.Type);
}
}
public Z3TermAst Visit(VCExprQuantifier node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
Contract.Assert(node.TypeParameters.Count == 0);
namer.PushScope();
try
{
List<string> varNames;
List<Type> varTypes;
VisitBounds(node.BoundVars, out varNames, out varTypes);
List<Z3PatternAst> patterns;
List<Z3TermAst> no_patterns;
VisitTriggers(node.Triggers, options, out patterns, out no_patterns);
Z3TermAst body = Linearise(node.Body, options);
Z3TermAst result;
uint weight = 1;
/*
if (options.QuantifierIds)
{
// only needed for Z3
VCQuantifierInfos infos = node.Infos;
Contract.Assert(infos != null);
if (infos.qid != null)
{
wr.Write("(QID ");
wr.Write(infos.qid);
wr.Write(") ");
}
if (0 <= infos.uniqueId)
{
wr.Write("(SKOLEMID ");
wr.Write(infos.uniqueId);
wr.Write(") ");
}
}
*/
if (options.UseWeights)
{
weight = (uint) QKeyValue.FindIntAttribute(node.Infos.attributes, "weight", 1);
}
switch (node.Quan)
{
case Quantifier.ALL:
result = cm.MakeForall(weight, varNames, varTypes, patterns, no_patterns, body); break;
case Quantifier.EX:
result = cm.MakeExists(weight, varNames, varTypes, patterns, no_patterns, body); break;
default:
throw new Exception("unknown quantifier kind " + node.Quan);
}
return result;
}
finally
{
namer.PopScope();
}
}
private void VisitBounds(List<VCExprVar> boundVars, out List<string> varNames, out List<Type> varTypes)
{
varNames = new List<string>();
varTypes = new List<Type>();
foreach (VCExprVar var in boundVars)
{
string varName = namer.GetLocalName(var, var.Name);
varNames.Add(varName);
varTypes.Add(var.Type);
}
}
private void VisitTriggers(List<VCTrigger> triggers, LineariserOptions options, out List<Z3PatternAst> patterns, out List<Z3TermAst> no_patterns)
{
patterns = new List<Z3PatternAst>();
no_patterns = new List<Z3TermAst>();
foreach (VCTrigger trigger in triggers)
{
List<Z3TermAst> exprs = new List<Z3TermAst>();
foreach (VCExpr expr in trigger.Exprs)
{
System.Diagnostics.Debug.Assert(expr != null);
Z3TermAst termAst = Linearise(expr, options);
exprs.Add(termAst);
}
if (exprs.Count > 0)
{
if (trigger.Pos)
{
Z3PatternAst pattern = cm.MakePattern(exprs);
patterns.Add(pattern);
}
else
{
System.Diagnostics.Debug.Assert(false, "Z3api currently does not handle nopats");
foreach (Z3TermAst expr in exprs)
no_patterns.Add(expr);
}
}
}
}
public Z3TermAst Visit(VCExprLet node, LineariserOptions options)
{
foreach (VCExprLetBinding b in node)
{
Z3TermAst defAst = Linearise(b.E, options);
letBindings.Add(b.V, defAst);
}
Z3TermAst letAst = Linearise(node.Body, options);
foreach (VCExprLetBinding b in node)
{
letBindings.Remove(b.V);
}
return letAst;
}
/////////////////////////////////////////////////////////////////////////////////////
internal class Z3apiOpLineariser : IVCExprOpVisitor<Z3TermAst, LineariserOptions>
{
[ContractInvariantMethod]
void ObjectInvariant()
{
Contract.Invariant(ExprLineariser != null);
}
private readonly Z3apiExprLineariser ExprLineariser;
public Z3apiOpLineariser(Z3apiExprLineariser ExprLineariser)
{
Contract.Requires(ExprLineariser != null);
this.ExprLineariser = ExprLineariser;
}
///////////////////////////////////////////////////////////////////////////////////
private Z3TermAst WriteApplication(string op, IEnumerable<VCExpr> terms, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(op != null);
Contract.Requires(cce.NonNullElements(terms));
List<Z3TermAst> args = new List<Z3TermAst>();
foreach (VCExpr e in terms)
{
Contract.Assert(e != null);
args.Add(ExprLineariser.Linearise(e, options));
}
return ExprLineariser.cm.Make(op, args);
}
///////////////////////////////////////////////////////////////////////////////////
public Z3TermAst VisitNotOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("NOT", node, options);
}
public Z3TermAst VisitEqOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("EQ", node, options);
}
public Z3TermAst VisitNeqOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("NEQ", node, options);
}
public Z3TermAst VisitAndOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("AND", node, options);
}
public Z3TermAst VisitOrOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("OR", node, options);
}
public Z3TermAst VisitImpliesOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
if (options.InverseImplies)
{
List<Z3TermAst> args = new List<Z3TermAst>();
args.Add(ExprLineariser.Linearise(node[1], options));
List<Z3TermAst> t = new List<Z3TermAst>();
t.Add(ExprLineariser.Linearise(node[0], options));
args.Add(ExprLineariser.cm.Make("NOT", t));
return ExprLineariser.cm.Make("OR", args);
}
else
{
return WriteApplication("IMPLIES", node, options);
}
}
public Z3TermAst VisitDistinctOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
if (node.Length < 2)
{
return ExprLineariser.Linearise(VCExpressionGenerator.True, options);
}
else
{
List<Z3TermAst> args = new List<Z3TermAst>();
foreach (VCExpr e in node)
{
Contract.Assert(e != null);
args.Add(ExprLineariser.Linearise(e, options));
}
return ExprLineariser.cm.Make("DISTINCT", args);
}
}
public Z3TermAst VisitLabelOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
VCExprLabelOp op = (VCExprLabelOp)node.Op;
Contract.Assert(op != null);
return ExprLineariser.cm.MakeLabel(op.label, op.pos, ExprLineariser.Linearise(node[0], options));
}
public Z3TermAst VisitSelectOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
List<Z3TermAst> args = new List<Z3TermAst>();
foreach (VCExpr e in node)
{
Contract.Assert(e != null);
args.Add(ExprLineariser.Linearise(e, options));
}
return ExprLineariser.cm.MakeArraySelect(args);
// return ExprLineariser.cm.Make(SimplifyLikeExprLineariser.SelectOpName(node), args);
}
public Z3TermAst VisitStoreOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
List<Z3TermAst> args = new List<Z3TermAst>();
foreach (VCExpr e in node)
{
Contract.Assert(e != null);
args.Add(ExprLineariser.Linearise(e, options));
}
return ExprLineariser.cm.MakeArrayStore(args);
// return ExprLineariser.cm.Make(SimplifyLikeExprLineariser.StoreOpName(node), args);
}
public Z3TermAst VisitBvOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("$make_bv" + node.Type.BvBits, node, options);
}
public Z3TermAst VisitBvExtractOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication(SimplifyLikeExprLineariser.BvExtractOpName(node), node, options);
}
public Z3TermAst VisitBvConcatOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication(SimplifyLikeExprLineariser.BvConcatOpName(node), node, options);
}
public Z3TermAst VisitIfThenElseOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
List<Z3TermAst> args = new List<Z3TermAst>();
args.Add(ExprLineariser.Linearise(node[0], options));
args.Add(ExprLineariser.Linearise(node[1], options));
args.Add(ExprLineariser.Linearise(node[2], options));
return ExprLineariser.cm.Make("ITE", args);
}
public Z3TermAst VisitCustomOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(node != null);
Contract.Requires(options != null);
VCExprCustomOp op = (VCExprCustomOp)node.Op;
List<Z3TermAst> args = new List<Z3TermAst>();
foreach (VCExpr arg in node)
{
args.Add(ExprLineariser.Linearise(arg, options));
}
return ExprLineariser.cm.Make(op.Name, args);
}
public Z3TermAst VisitAddOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
if (CommandLineOptions.Clo.ReflectAdd)
{
return WriteApplication("Reflect$Add", node, options);
}
else
{
return WriteApplication("+", node, options);
}
}
public Z3TermAst VisitSubOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("-", node, options);
}
public Z3TermAst VisitMulOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("*", node, options);
}
public Z3TermAst VisitDivOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("/", node, options);
}
public Z3TermAst VisitModOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("%", node, options);
}
public Z3TermAst VisitLtOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("<", node, options);
}
public Z3TermAst VisitLeOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("<=", node, options);
}
public Z3TermAst VisitGtOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication(">", node, options);
}
public Z3TermAst VisitGeOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication(">=", node, options);
}
public Z3TermAst VisitSubtypeOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("<:", node, options);
}
public Z3TermAst VisitSubtype3Op(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
return WriteApplication("<::", node, options);
}
public Z3TermAst VisitBoogieFunctionOp(VCExprNAry node, LineariserOptions options)
{
Contract.Requires(options != null);
Contract.Requires(node != null);
VCExprBoogieFunctionOp op = (VCExprBoogieFunctionOp)node.Op;
Contract.Assert(op != null);
string funcName = op.Func.Name;
Contract.Assert(funcName != null);
string bvzName = op.Func.FindStringAttribute("external");
string printedName = ExprLineariser.namer.GetName(op.Func, funcName);
Contract.Assert(printedName != null);
if (bvzName != null) printedName = bvzName;
List<Z3TermAst> args = new List<Z3TermAst>();
foreach (VCExpr e in node)
{
Contract.Assert(e != null);
args.Add(ExprLineariser.Linearise(e, options));
}
return ExprLineariser.cm.Make(printedName, args);
}
}
}
}
|