-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathUserPropagator.cs
464 lines (404 loc) · 15.2 KB
/
UserPropagator.cs
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
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
UserPropagator.cs
Abstract:
User Propagator plugin
Author:
Nikolaj Bjorner (nbjorner) 2022-05-07
Notes:
--*/
using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Microsoft.Z3
{
using Z3_solver_callback = System.IntPtr;
using Z3_context = System.IntPtr;
using Z3_solver = System.IntPtr;
using voidp = System.IntPtr;
using Z3_ast = System.IntPtr;
/// <summary>
/// Propagator context for .Net
/// </summary>
public class UserPropagator : IDisposable
{
/// <summary>
/// Delegate type for fixed callback
/// Note that the life-time of the term and value only applies within the scope of the callback.
/// That means the term and value cannot be stored in an array, dictionary or similar and accessed after the callback has returned.
/// Use the functionality Dup on expressions to create a duplicate copy that extends the lifetime.
/// </summary>
public delegate void FixedEh(Expr term, Expr value);
/// <summary>
/// Delegate type for equality or disequality callback
/// </summary>
public delegate void EqEh(Expr term, Expr value);
/// <summary>
/// Delegate type for when a new term using a registered function symbol is created internally
/// </summary>
public delegate void CreatedEh(Expr term);
/// <summary>
/// Delegate type for callback into solver's branching. The values can be overridden by calling <see cref="NextSplit" />.
/// </summary>
/// <param name="term">A bit-vector or Boolean used for branching</param>
/// <param name="idx">If the term is a bit-vector, then an index into the bit-vector being branched on</param>
/// <param name="phase">The tentative truth-value</param>
public delegate void DecideEh(Expr term, uint idx, bool phase);
// access managed objects through a static array.
// thread safety is ignored for now.
GCHandle gch;
Solver solver;
Context ctx;
Z3_solver_callback callback = IntPtr.Zero;
int callbackNesting = 0;
FixedEh fixed_eh;
Action final_eh;
EqEh eq_eh;
EqEh diseq_eh;
CreatedEh created_eh;
DecideEh decide_eh;
Native.Z3_push_eh push_eh;
Native.Z3_pop_eh pop_eh;
Native.Z3_fresh_eh fresh_eh;
Native.Z3_fixed_eh fixed_wrapper;
Native.Z3_final_eh final_wrapper;
Native.Z3_eq_eh eq_wrapper;
Native.Z3_eq_eh diseq_wrapper;
Native.Z3_decide_eh decide_wrapper;
Native.Z3_created_eh created_wrapper;
void Callback(Action fn, Z3_solver_callback cb)
{
this.callbackNesting++;
this.callback = cb;
try
{
fn();
}
catch
{
// TBD: add debug log or exception handler
}
finally
{
callbackNesting--;
if (callbackNesting == 0) // callbacks can be nested (e.g., internalizing new element in "created")
this.callback = IntPtr.Zero;
}
}
static void _push(voidp ctx, Z3_solver_callback cb)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
prop.Callback(() => prop.Push(), cb);
}
static void _pop(voidp ctx, Z3_solver_callback cb, uint num_scopes)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
prop.Callback(() => prop.Pop(num_scopes), cb);
}
static voidp _fresh(voidp _ctx, Z3_context new_context)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(_ctx).Target;
var ctx = new Context(new_context);
var prop1 = prop.Fresh(prop.ctx);
return GCHandle.ToIntPtr(prop1.gch);
}
static void _fixed(voidp ctx, Z3_solver_callback cb, Z3_ast _term, Z3_ast _value)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
using var term = Expr.Create(prop.ctx, _term);
using var value = Expr.Create(prop.ctx, _value);
prop.Callback(() => prop.fixed_eh(term, value), cb);
}
static void _final(voidp ctx, Z3_solver_callback cb)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
prop.Callback(() => prop.final_eh(), cb);
}
static void _eq(voidp ctx, Z3_solver_callback cb, Z3_ast a, Z3_ast b)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
using var s = Expr.Create(prop.ctx, a);
using var t = Expr.Create(prop.ctx, b);
prop.Callback(() => prop.eq_eh(s, t), cb);
}
static void _diseq(voidp ctx, Z3_solver_callback cb, Z3_ast a, Z3_ast b)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
using var s = Expr.Create(prop.ctx, a);
using var t = Expr.Create(prop.ctx, b);
prop.Callback(() => prop.diseq_eh(s, t), cb);
}
static void _created(voidp ctx, Z3_solver_callback cb, Z3_ast a)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
using var t = Expr.Create(prop.ctx, a);
prop.Callback(() => prop.created_eh(t), cb);
}
static void _decide(voidp ctx, Z3_solver_callback cb, Z3_ast a, uint idx, bool phase)
{
var prop = (UserPropagator)GCHandle.FromIntPtr(ctx).Target;
using var t = Expr.Create(prop.ctx, a);
prop.Callback(() => prop.decide_eh(t, idx, phase), cb);
}
/// <summary>
/// Propagator constructor from a solver class.
/// </summary>
public UserPropagator(Solver s)
{
gch = GCHandle.Alloc(this);
solver = s;
ctx = solver.Context;
push_eh = _push;
pop_eh = _pop;
fresh_eh = _fresh;
Native.Z3_solver_propagate_init(ctx.nCtx, solver.NativeObject, GCHandle.ToIntPtr(gch), push_eh, pop_eh, fresh_eh);
}
/// <summary>
/// Propagator constructor from a context. It is used from inside of Fresh.
/// </summary>
public UserPropagator(Context _ctx)
{
gch = GCHandle.Alloc(this);
solver = null;
ctx = _ctx;
}
/// <summary>
/// Release private memory.
/// </summary>
~UserPropagator()
{
Dispose();
}
/// <summary>
/// Must be called. The object will not be garbage collected automatically even if the context is disposed
/// </summary>
public virtual void Dispose()
{
if (!gch.IsAllocated)
return;
gch.Free();
if (solver == null)
ctx.Dispose();
}
/// <summary>
/// Virtual method for push. It must be overwritten by inherited class.
/// </summary>
public virtual void Push() { throw new Z3Exception("Push method should be overwritten"); }
/// <summary>
/// Virtual method for pop. It must be overwritten by inherited class.
/// </summary>
public virtual void Pop(uint n) { throw new Z3Exception("Pop method should be overwritten"); }
/// <summary>
/// Virtual method for fresh. It can be overwritten by inherited class.
/// </summary>
public virtual UserPropagator Fresh(Context ctx) { return new UserPropagator(ctx); }
/// <summary>
/// Declare combination of assigned expressions a conflict
/// </summary>
public void Conflict(params Expr[] terms)
{
Propagate(terms, ctx.MkFalse());
}
/// <summary>
/// Declare combination of assigned expressions a conflict
/// </summary>
public void Conflict(IEnumerable<Expr> terms)
{
Propagate(terms, ctx.MkFalse());
}
/// <summary>
/// Propagate consequence
/// <returns>
/// <see langword="true" /> if the propagated expression is new for the solver;
/// <see langword="false" /> if the propagation was ignored
/// </returns>
/// </summary>
public bool Propagate(IEnumerable<Expr> terms, Expr conseq)
{
return Propagate(terms, new EqualityPairs(), conseq);
}
/// <summary>
/// Propagate consequence
/// <returns>
/// <see langword="true" /> if the propagated expression is new for the solver;
/// <see langword="false" /> if the propagation was ignored
/// </returns>
/// </summary>
public bool Propagate(IEnumerable<Expr> terms, EqualityPairs equalities, Expr conseq)
{
var nTerms = Z3Object.ArrayToNative(terms.ToArray());
var nLHS = Z3Object.ArrayToNative(equalities.LHS.ToArray());
var nRHS = Z3Object.ArrayToNative(equalities.RHS.ToArray());
return Native.Z3_solver_propagate_consequence(ctx.nCtx, this.callback, (uint)nTerms.Length, nTerms, (uint)equalities.Count, nLHS, nRHS, conseq.NativeObject) != 0;
}
/// <summary>
/// Set fixed callback
/// </summary>
public FixedEh Fixed
{
set
{
this.fixed_wrapper = _fixed;
this.fixed_eh = value;
if (solver != null)
Native.Z3_solver_propagate_fixed(ctx.nCtx, solver.NativeObject, fixed_wrapper);
}
}
/// <summary>
/// Set final callback
/// </summary>
public Action Final
{
set
{
this.final_wrapper = _final;
this.final_eh = value;
if (solver != null)
Native.Z3_solver_propagate_final(ctx.nCtx, solver.NativeObject, final_wrapper);
}
}
/// <summary>
/// Set equality event callback
/// </summary>
public EqEh Eq
{
set
{
this.eq_wrapper = _eq;
this.eq_eh = value;
if (solver != null)
Native.Z3_solver_propagate_eq(ctx.nCtx, solver.NativeObject, eq_wrapper);
}
}
/// <summary>
/// Set disequality event callback
/// </summary>
public EqEh Diseq
{
set
{
this.diseq_wrapper = _diseq;
this.diseq_eh = value;
if (solver != null)
Native.Z3_solver_propagate_diseq(ctx.nCtx, solver.NativeObject, diseq_wrapper);
}
}
/// <summary>
/// Set created callback
/// </summary>
public CreatedEh Created
{
set
{
this.created_wrapper = _created;
this.created_eh = value;
if (solver != null)
Native.Z3_solver_propagate_created(ctx.nCtx, solver.NativeObject, created_wrapper);
}
}
/// <summary>
/// Set decision callback
/// </summary>
public DecideEh Decide
{
set
{
this.decide_wrapper = _decide;
this.decide_eh = value;
if (solver != null)
Native.Z3_solver_propagate_decide(ctx.nCtx, solver.NativeObject, decide_wrapper);
}
}
/// <summary>
/// Set the next decision
/// <param name="e">A bit-vector or Boolean used for branching. Use <see langword="null" /> to clear</param>
/// <param name="idx">If the term is a bit-vector, then an index into the bit-vector being branched on</param>
/// <param name="phase">The tentative truth-value (-1/false, 1/true, 0/let Z3 decide)</param>
/// </summary>
/// <returns>
/// <see langword="true" /> in case the value was successfully set;
/// <see langword="false" /> if the next split could not be set
/// </returns>
public bool NextSplit(Expr e, uint idx, int phase)
{
return Native.Z3_solver_next_split(ctx.nCtx, this.callback, e?.NativeObject ?? IntPtr.Zero, idx, phase) != 0;
}
/// <summary>
/// Track assignments to a term
/// </summary>
public void Register(Expr term)
{
if (this.callback != IntPtr.Zero)
{
Native.Z3_solver_propagate_register_cb(ctx.nCtx, callback, term.NativeObject);
}
else
{
Native.Z3_solver_propagate_register(ctx.nCtx, solver.NativeObject, term.NativeObject);
}
}
}
/// <summary>
/// A list of equalities used as justifications for propagation
/// </summary>
public class EqualityPairs {
readonly List<Expr> lhsList = new List<Expr>();
readonly List<Expr> rhsList = new List<Expr>();
/// <summary>
/// The left hand sides of the equalities
/// </summary>
public Expr[] LHS => lhsList.ToArray();
/// <summary>
/// The right hand sides of the equalities
/// </summary>
public Expr[] RHS => rhsList.ToArray();
/// <summary>
/// The number of equalities
/// </summary>
public int Count => lhsList.Count;
/// <summary>
/// Adds an equality to the list. The sorts of the arguments have to be the same.
/// <param name="lhs">The left hand side of the equality</param>
/// <param name="rhs">The right hand side of the equality</param>
/// </summary>
public void Add(Expr lhs, Expr rhs) {
lhsList.Add(lhs);
rhsList.Add(rhs);
}
/// <summary>
/// Checks if two equality lists are equal.
/// The function does not take symmetries, shuffling, or duplicates into account.
/// </summary>
public override bool Equals(object obj) {
if (ReferenceEquals(this, obj))
return true;
if (!(obj is EqualityPairs other))
return false;
if (lhsList.Count != other.lhsList.Count)
return false;
for (int i = 0; i < lhsList.Count; i++) {
if (!lhsList[i].Equals(other.lhsList[i]))
return false;
}
return true;
}
/// <summary>
/// Gets a hash code for the list of equalities
/// </summary>
public override int GetHashCode() {
int hash = lhsList.Count;
unchecked {
for (int i = 0; i < lhsList.Count; i++) {
hash ^= lhsList[i].GetHashCode();
hash *= 17;
hash ^= rhsList[i].GetHashCode();
hash *= 29;
}
return hash;
}
}
}
}