Z3
NativeContext.cs
Go to the documentation of this file.
1/*++
2Copyright (c) 2012 Microsoft Corporation
3
4Module Name:
5
6 NativeContext.cs
7
8Abstract:
9
10 Z3 Managed API: Native Context
11
12Author:
13
14 Christoph Wintersteiger (cwinter) 2012-03-22
15 John Fleisher, Nikolaj Bjorner (nbjorner) 2022-03-01
16
17--*/
18
19using System;
20using System.Collections.Generic;
21using System.Diagnostics;
22using System.Linq;
23using System.Runtime.InteropServices;
24
25namespace Microsoft.Z3
26{
27 using Z3_app = System.IntPtr;
28 using Z3_ast = System.IntPtr;
29 using Z3_ast_vector = System.IntPtr;
30 using Z3_func_decl = System.IntPtr;
31 using Z3_pattern = System.IntPtr;
32 using Z3_solver = System.IntPtr;
33 using Z3_sort = System.IntPtr;
34 using Z3_stats = System.IntPtr;
35 using Z3_symbol = System.IntPtr;
36
43 {
62 public NativeContext(Dictionary<string, string> settings)
63 : base()
64 {
65 Debug.Assert(settings != null);
66
67 lock (creation_lock)
68 {
69 IntPtr cfg = Native.Z3_mk_config();
70 foreach (KeyValuePair<string, string> kv in settings)
71 Native.Z3_set_param_value(cfg, kv.Key, kv.Value);
72 m_ctx = Native.Z3_mk_context(cfg);
73 Native.Z3_del_config(cfg);
74 InitContext();
75 }
76 }
77
78 #region Arithmetic
82
83 public Z3_ast MkAdd(params Z3_ast[] t)
84 {
85 Debug.Assert(t != null);
86 Debug.Assert(t.All(a => a != IntPtr.Zero));
87
88 return Native.Z3_mk_add(nCtx, (uint)(t?.Length ?? 0), t);
89 }
90
94 public Z3_ast MkMul(params Z3_ast[] t)
95 {
96 Debug.Assert(t != null);
97 Debug.Assert(t.All(a => a != IntPtr.Zero));
98
99 var ts = t.ToArray();
100 return Native.Z3_mk_mul(nCtx, (uint)(ts?.Length ?? 0), ts);
101 }
102
106 public Z3_ast MkSub(params Z3_ast[] t)
107 {
108 Debug.Assert(t != null);
109 Debug.Assert(t.All(a => a != IntPtr.Zero));
110 var ts = t.ToArray();
111 return Native.Z3_mk_sub(nCtx, (uint)(ts?.Length ?? 0), ts);
112 }
113
117 public Z3_ast MkDiv(Z3_ast t1, Z3_ast t2)
118 {
119 Debug.Assert(t1 != IntPtr.Zero);
120 Debug.Assert(t2 != IntPtr.Zero);
121
122 return Native.Z3_mk_div(nCtx, t1, t2);
123 }
124
128 public Z3_ast MkLe(Z3_ast t1, Z3_ast t2)
129 {
130 Debug.Assert(t1 != IntPtr.Zero);
131 Debug.Assert(t2 != IntPtr.Zero);
132
133 return Native.Z3_mk_le(nCtx, t1, t2);
134 }
135
139 public Z3_ast MkLt(Z3_ast t1, Z3_ast t2)
140 {
141 Debug.Assert(t1 != IntPtr.Zero);
142 Debug.Assert(t2 != IntPtr.Zero);
143
144 return Native.Z3_mk_lt(nCtx, t1, t2);
145 }
146
150 public Z3_ast MkGe(Z3_ast t1, Z3_ast t2)
151 {
152 Debug.Assert(t1 != IntPtr.Zero);
153 Debug.Assert(t2 != IntPtr.Zero);
154
155 return Native.Z3_mk_ge(nCtx, t1, t2);
156 }
157
161 public Z3_ast MkGt(Z3_ast t1, Z3_ast t2)
162 {
163 Debug.Assert(t1 != IntPtr.Zero);
164 Debug.Assert(t2 != IntPtr.Zero);
165
166 return Native.Z3_mk_gt(nCtx, t1, t2);
167 }
168
176 {
177 Debug.Assert(t1 != IntPtr.Zero);
178 Debug.Assert(t2 != IntPtr.Zero);
179
180 return Native.Z3_mk_bvult(nCtx, t1, t2);
181 }
182
190 {
191 Debug.Assert(t1 != IntPtr.Zero);
192 Debug.Assert(t2 != IntPtr.Zero);
193
194 return Native.Z3_mk_bvule(nCtx, t1, t2);
195 }
196
201 {
202 Debug.Assert(x != IntPtr.Zero);
203 Debug.Assert(y != IntPtr.Zero);
204
205 return Native.Z3_mk_eq(nCtx, x, y);
206 }
207
212 {
213 Debug.Assert(a != IntPtr.Zero);
214
215 return Native.Z3_mk_not(nCtx, a);
216 }
217
221 public Z3_ast MkAnd(params Z3_ast[] t)
222 {
223 Debug.Assert(t != null);
224 Debug.Assert(t.All(a => a != IntPtr.Zero));
225
226 return Native.Z3_mk_and(nCtx, (uint)(t?.Length ?? 0), t);
227 }
228
232 public Z3_ast MkOr(params Z3_ast[] t)
233 {
234 Debug.Assert(t != null);
235 Debug.Assert(t.All(a => a != IntPtr.Zero));
236
237 return Native.Z3_mk_or(nCtx, (uint)(t?.Length ?? 0), t);
238 }
239
240
246 public Z3_ast MkReal(string v)
247 {
248 Debug.Assert(!string.IsNullOrEmpty(v));
249 return Native.Z3_mk_numeral(nCtx, v, RealSort);
250 }
251
257 public Z3_ast MkNumeral(int v, Z3_sort sort)
258 {
259 Debug.Assert(sort != IntPtr.Zero);
260
261 return Native.Z3_mk_int(nCtx, v, sort);
262 }
263
269 public Z3_ast MkNumeral(uint v, Z3_sort sort)
270 {
271 Debug.Assert(sort != null);
272
273 return Native.Z3_mk_unsigned_int(nCtx, v, sort);
274 }
275
281 public Z3_ast MkNumeral(long v, Z3_sort sort)
282 {
283 Debug.Assert(sort != null);
284
285 return Native.Z3_mk_int64(nCtx, v, sort);
286 }
287
294 public Z3_ast MkIte(Z3_ast t1, Z3_ast t2, Z3_ast t3)
295 {
296 Debug.Assert(t1 != IntPtr.Zero);
297 Debug.Assert(t2 != IntPtr.Zero);
298 Debug.Assert(t3 != IntPtr.Zero);
299
300 return Native.Z3_mk_ite(nCtx, t1, t2, t3);
301 }
302
307 {
308 Debug.Assert(t1 != IntPtr.Zero);
309 Debug.Assert(t2 != IntPtr.Zero);
310
311 return Native.Z3_mk_implies(nCtx, t1, t2);
312 }
313
314 #endregion
315
316 #region Sort
317
321 public Z3_sort IntSort => Native.Z3_mk_int_sort(nCtx);
322
326 public Z3_sort BoolSort => Native.Z3_mk_bool_sort(nCtx);
327
331 public Z3_sort RealSort => Native.Z3_mk_real_sort(nCtx);
332
333
337 public Z3_sort MkBvSort(uint size) => Native.Z3_mk_bv_sort(nCtx, size);
338
351
352 public Z3_sort MkListSort(string name, Z3_sort elemSort,
353 out Z3_func_decl inil, out Z3_func_decl iisnil,
354 out Z3_func_decl icons, out Z3_func_decl iiscons,
355 out Z3_func_decl ihead, out Z3_func_decl itail)
356 {
357 Debug.Assert(!string.IsNullOrEmpty(name));
358 Debug.Assert(elemSort != IntPtr.Zero);
359
360 IntPtr nil = IntPtr.Zero, isnil = IntPtr.Zero,
361 cons = IntPtr.Zero, iscons = IntPtr.Zero,
362 head = IntPtr.Zero, tail = IntPtr.Zero;
363
364 var symbol = Native.Z3_mk_string_symbol(nCtx, name);
365 var sort = Native.Z3_mk_list_sort(nCtx, symbol, elemSort,
366 ref nil, ref isnil, ref cons, ref iscons, ref head, ref tail);
367
368 inil = nil;
369 iisnil = isnil;
370 icons = cons;
371 iiscons = iscons;
372 ihead = head;
373 itail = tail;
374
375 return sort;
376 }
377
382 {
383 Debug.Assert(domain != IntPtr.Zero);
384 Debug.Assert(range != IntPtr.Zero);
385
386 return Native.Z3_mk_array_sort(nCtx, domain, range);
387 }
388
392 public Z3_sort MkTupleSort(Z3_symbol name, Z3_symbol[] fieldNames, Z3_sort[] fieldSorts, out Z3_func_decl constructor, Z3_func_decl[] projections)
393 {
394 Debug.Assert(name != IntPtr.Zero);
395 Debug.Assert(fieldNames != null);
396 Debug.Assert(fieldNames.All(fn => fn != IntPtr.Zero));
397 Debug.Assert(fieldSorts == null || fieldSorts.All(fs => fs != IntPtr.Zero));
398
399 var numFields = (uint)(fieldNames?.Length ?? 0);
400 constructor = IntPtr.Zero;
401 return Native.Z3_mk_tuple_sort(nCtx, name, numFields, fieldNames, fieldSorts, ref constructor, projections);
402 }
403
404 #endregion
405
406 #region Propositional
410 public Z3_ast MkTrue() => Native.Z3_mk_true(nCtx);
411
415 public Z3_ast MkFalse() => Native.Z3_mk_false(nCtx);
416
420 public Z3_ast MkBool(bool value) => value ? MkTrue() : MkFalse();
421
425 public Z3_ast MkIff(Z3_ast t1, Z3_ast t2)
426 {
427 Debug.Assert(t1 != IntPtr.Zero);
428 Debug.Assert(t2 != IntPtr.Zero);
429
430 return Native.Z3_mk_iff(nCtx, t1, t2);
431 }
432 #endregion
433
434 #region Constants
438 public Z3_ast MkConst(string name, Z3_sort range)
439 {
440 Debug.Assert(!string.IsNullOrEmpty(name));
441 Debug.Assert(range != IntPtr.Zero);
442
443 return Native.Z3_mk_const(nCtx, MkStringSymbol(name), range);
444 }
445
446 #endregion
447
448 #region Symbol
454 public Z3_symbol MkStringSymbol(string name)
455 {
456 Debug.Assert(!string.IsNullOrEmpty(name));
457
458 return Native.Z3_mk_string_symbol(nCtx, name);
459 }
460 #endregion
461
462 #region Terms
466 public Z3_ast MkApp(Z3_func_decl f, params Z3_ast[] args)
467 {
468 Debug.Assert(f != IntPtr.Zero);
469 Debug.Assert(args == null || args.All(a => a != IntPtr.Zero));
470
471 return Native.Z3_mk_app(nCtx, f, (uint)(args?.Length ?? 0), args);
472 }
473
474 #endregion
475
476 #region Bound Variables
482 public Z3_ast MkBound(uint index, Z3_sort sort)
483 {
484 Debug.Assert(sort != IntPtr.Zero);
485
486 return Native.Z3_mk_bound(nCtx, index, sort);
487 }
488 #endregion
489
490 #region Bit-vectors
496 {
497 Debug.Assert(t1 != IntPtr.Zero);
498 Debug.Assert(t2 != IntPtr.Zero);
499
500 return Native.Z3_mk_bvand(nCtx, t1, t2);
501 }
502
508 {
509 Debug.Assert(t != IntPtr.Zero);
510
511 return Native.Z3_mk_bvnot(nCtx, t);
512 }
513
519 {
520 Debug.Assert(t != IntPtr.Zero);
521
522 return Native.Z3_mk_bvneg(nCtx, t);
523 }
524
530 {
531 Debug.Assert(t != IntPtr.Zero);
532
533 return Native.Z3_mk_bvneg(nCtx, t);
534 }
535
541 {
542 Debug.Assert(t1 != IntPtr.Zero);
543 Debug.Assert(t2 != IntPtr.Zero);
544
545 return Native.Z3_mk_bvadd(nCtx, t1, t2);
546 }
547
557 public Z3_ast_vector MkBvExtract(uint high, uint low, Z3_ast_vector t)
558 {
559 Debug.Assert(t != IntPtr.Zero);
560
561 return Native.Z3_mk_extract(nCtx, high, low, t);
562 }
563
573 {
574 Debug.Assert(t != IntPtr.Zero);
575
576 return Native.Z3_mk_sign_ext(nCtx, i, t);
577 }
578
589 {
590 Debug.Assert(t != IntPtr.Zero);
591
592 return Native.Z3_mk_zero_ext(nCtx, i, t);
593 }
594
608 {
609 Debug.Assert(t1 != IntPtr.Zero);
610 Debug.Assert(t2 != IntPtr.Zero);
611
612 return Native.Z3_mk_bvshl(nCtx, t1, t2);
613 }
614
628 {
629 Debug.Assert(t1 != IntPtr.Zero);
630 Debug.Assert(t2 != IntPtr.Zero);
631
632 return Native.Z3_mk_bvlshr(nCtx, t1, t2);
633 }
634
650 {
651 Debug.Assert(t1 != IntPtr.Zero);
652 Debug.Assert(t2 != IntPtr.Zero);
653
654 return Native.Z3_mk_bvashr(nCtx, t1, t2);
655 }
656
664 {
665 Debug.Assert(t1 != IntPtr.Zero);
666 Debug.Assert(t2 != IntPtr.Zero);
667
668 return Native.Z3_mk_bvslt(nCtx, t1, t2);
669 }
670
676 {
677 Debug.Assert(t1 != IntPtr.Zero);
678 Debug.Assert(t2 != IntPtr.Zero);
679
680 return Native.Z3_mk_bvmul(nCtx, t1, t2);
681 }
682
693 {
694 Debug.Assert(t1 != IntPtr.Zero);
695 Debug.Assert(t2 != IntPtr.Zero);
696
697 return Native.Z3_mk_bvudiv(nCtx, t1, t2);
698 }
699
714 {
715 Debug.Assert(t1 != IntPtr.Zero);
716 Debug.Assert(t2 != IntPtr.Zero);
717
718 return Native.Z3_mk_bvsdiv(nCtx, t1, t2);
719 }
720
730 {
731 Debug.Assert(t1 != IntPtr.Zero);
732 Debug.Assert(t2 != IntPtr.Zero);
733
734 return Native.Z3_mk_bvurem(nCtx, t1, t2);
735 }
736
748 {
749 Debug.Assert(t1 != IntPtr.Zero);
750 Debug.Assert(t2 != IntPtr.Zero);
751
752 return Native.Z3_mk_bvsrem(nCtx, t1, t2);
753 }
754
760 {
761 Debug.Assert(t1 != IntPtr.Zero);
762 Debug.Assert(t2 != IntPtr.Zero);
763
764 return Native.Z3_mk_bvsub(nCtx, t1, t2);
765 }
766
772 {
773 Debug.Assert(t1 != IntPtr.Zero);
774 Debug.Assert(t2 != IntPtr.Zero);
775
776 return Native.Z3_mk_bvor(nCtx, t1, t2);
777 }
778
784 {
785 Debug.Assert(t1 != null);
786 Debug.Assert(t2 != IntPtr.Zero);
787
788 return Native.Z3_mk_bvxor(nCtx, t1, t2);
789 }
790 #endregion
791
792 #region Quantifiers
817 public Z3_ast MkForall(Z3_sort[] sorts, Z3_symbol[] names, Z3_ast body, uint weight = 1, Z3_ast[] patterns = null, Z3_ast[] noPatterns = null, Symbol quantifierID = null, Symbol skolemID = null)
818 {
819 return MkQuantifier(true, sorts, names, body, weight, patterns, noPatterns, quantifierID, skolemID);
820 }
821
835 public Z3_ast MkExists(Z3_sort[] sorts, Z3_symbol[] names, Z3_ast body, uint weight = 1, Z3_ast[] patterns = null, Z3_ast[] noPatterns = null, Symbol quantifierID = null, Symbol skolemID = null)
836 {
837 return MkQuantifier(false, sorts, names, body, weight, patterns, noPatterns, quantifierID, skolemID);
838 }
839
853 private Z3_ast MkQuantifier(bool is_forall, Z3_sort[] sorts, Z3_symbol[] names, Z3_ast body, uint weight, Z3_ast[] patterns, Z3_ast[] noPatterns, Symbol quantifierID, Symbol skolemID)
854 {
855 Debug.Assert(sorts != null);
856 Debug.Assert(names != null);
857 Debug.Assert(body != null);
858 Debug.Assert(sorts.Length == names.Length);
859 Debug.Assert(sorts.All(s => s != IntPtr.Zero));
860 Debug.Assert(names.All(n => n != IntPtr.Zero));
861 Debug.Assert(patterns == null || patterns.All(p => p != IntPtr.Zero));
862 Debug.Assert(noPatterns == null || noPatterns.All(np => np != IntPtr.Zero));
863
864 if (noPatterns == null && quantifierID == null && skolemID == null)
865 {
866 return Native.Z3_mk_quantifier(nCtx, (byte)(is_forall ? 1 : 0), weight,
867 (uint)(patterns?.Length ?? 0), patterns,
868 (uint)(sorts?.Length ?? 0), sorts,
869 names,
870 body);
871 }
872 else
873 {
874 return Native.Z3_mk_quantifier_ex(nCtx, (byte)(is_forall ? 1 : 0), weight,
875 AST.GetNativeObject(quantifierID), AST.GetNativeObject(skolemID),
876 (uint)(patterns?.Length ?? 0), patterns,
877 (uint)(noPatterns?.Length ?? 0), noPatterns,
878 (uint)(sorts?.Length ?? 0), sorts,
879 names,
880 body);
881 }
882 }
883
884 #endregion
885
886 #region Options
904 {
905 set { Native.Z3_set_ast_print_mode(nCtx, (uint)value); }
906 }
907
908 #endregion
909
910 #region Arrays
911
912
921 {
922 Debug.Assert(domain != IntPtr.Zero);
923 Debug.Assert(v != IntPtr.Zero);
924
925 return Native.Z3_mk_const_array(nCtx, domain, v);
926 }
927
944 {
945 Debug.Assert(a != IntPtr.Zero);
946 Debug.Assert(i != IntPtr.Zero);
947 Debug.Assert(v != IntPtr.Zero);
948
949 return Native.Z3_mk_store(nCtx, a, i, v);
950 }
951
963 public Z3_ast MkSelect(Z3_ast array, Z3_ast index)
964 {
965 Debug.Assert(array != IntPtr.Zero);
966 Debug.Assert(index != IntPtr.Zero);
967
968 return Native.Z3_mk_select(nCtx, array, index);
969 }
970
979 {
980 Debug.Assert(a != null);
981
982 return Native.Z3_mk_array_default(nCtx, a);
983 }
984
985 #endregion
986
987 #region Function Declarations
988
992 public Z3_func_decl MkFuncDecl(string name, Z3_sort[] domain, Z3_sort range)
993 {
994 Debug.Assert(!string.IsNullOrEmpty(name));
995 Debug.Assert(range != IntPtr.Zero);
996 Debug.Assert(domain != null);
997 Debug.Assert(domain.All(d => d != IntPtr.Zero));
998
999 var symbol = Native.Z3_mk_string_symbol(nCtx, name);
1000 return Native.Z3_mk_func_decl(nCtx, symbol, (uint)(domain?.Length ?? 0), domain, range);
1001 }
1002
1006 public Z3_func_decl MkFuncDecl(string name, Z3_sort domain, Z3_sort range)
1007 {
1008 Debug.Assert(!string.IsNullOrEmpty(name));
1009 Debug.Assert(range != IntPtr.Zero);
1010 Debug.Assert(domain != IntPtr.Zero);
1011
1012 var symbol = Native.Z3_mk_string_symbol(nCtx, name);
1013 var q = new Z3_sort[] { domain };
1014 return Native.Z3_mk_func_decl(nCtx, symbol, (uint)q.Length, q, range);
1015 }
1016
1020 public Z3_func_decl MkFreshFuncDecl(string prefix, Z3_sort[] domain, Z3_sort range)
1021 {
1022 Debug.Assert(domain != null);
1023 Debug.Assert(range != IntPtr.Zero);
1024 Debug.Assert(domain.All(d => d != IntPtr.Zero));
1025
1026 return Native.Z3_mk_fresh_func_decl(nCtx, prefix, (uint)(domain?.Length ?? 0), domain, range);
1027 }
1028
1033 {
1034 Debug.Assert(range != IntPtr.Zero);
1035
1036 var symbol = Native.Z3_mk_string_symbol(nCtx, name);
1037 return Native.Z3_mk_func_decl(nCtx, symbol, 0, new IntPtr[0], range);
1038 }
1039
1046 {
1047 Debug.Assert(fdecl != IntPtr.Zero);
1048
1049 var sz = Native.Z3_get_domain_size(nCtx, fdecl);
1050 var domain = new Z3_sort[sz];
1051 for (uint i = 0; i < sz; i++)
1052 {
1053 domain[i] = Native.Z3_get_domain(nCtx, fdecl, i);
1054 }
1055 return domain;
1056 }
1057
1064 {
1065 Debug.Assert(fdecl != IntPtr.Zero);
1066
1067 return Native.Z3_get_range(nCtx, fdecl);
1068 }
1069
1070 #endregion
1071
1072 #region Quantifier Patterns
1076 public Z3_pattern MkPattern(params Z3_ast[] terms)
1077 {
1078 Debug.Assert(terms != null);
1079 if (terms == null || terms.Length == 0)
1080 throw new Z3Exception("Cannot create a pattern from zero terms");
1081
1082 return Native.Z3_mk_pattern(nCtx, (uint)terms.Length, terms);
1083 }
1084 #endregion
1085
1086 #region Solver
1087
1092 {
1093 Z3_solver nSolver = Native.Z3_mk_simple_solver(nCtx);
1094 return new NativeSolver(this, nSolver);
1095 }
1096
1097 #endregion
1098
1099 #region Utilities
1104 {
1105 Debug.Assert(sort != IntPtr.Zero);
1106
1107 return (Z3_sort_kind)Native.Z3_get_sort_kind(nCtx, sort);
1108 }
1109
1114 {
1115 Debug.Assert(ast != IntPtr.Zero);
1116
1117 return (Z3_ast_kind)Native.Z3_get_ast_kind(nCtx, ast);
1118 }
1119
1124 {
1125 Debug.Assert(decl != IntPtr.Zero);
1126
1127 return (Z3_decl_kind)Native.Z3_get_decl_kind(nCtx, decl);
1128 }
1129
1134 {
1135 Debug.Assert(ast != IntPtr.Zero);
1136
1137 return Native.Z3_get_sort(nCtx, ast);
1138 }
1139
1146 {
1147 var numArgs = GetNumArgs(app);
1148 var args = new Z3_ast[numArgs];
1149 for (uint i = 0; i < numArgs; i++)
1150 {
1151 args[i] = GetAppArg(app, i);
1152 }
1153 return args;
1154 }
1155
1161 public uint GetNumArgs(Z3_app app)
1162 {
1163 Debug.Assert(app != IntPtr.Zero);
1164
1165 return Native.Z3_get_app_num_args(nCtx, app);
1166 }
1167
1168 internal Z3_ast GetAppArg(Z3_app app, uint i) => Native.Z3_get_app_arg(nCtx, app, i);
1169
1174 {
1175 Debug.Assert(ast != IntPtr.Zero);
1176
1177 return Native.Z3_get_app_decl(nCtx, ast);
1178 }
1179
1185 public string GetDeclName(Z3_func_decl decl)
1186 {
1187 Debug.Assert(decl != IntPtr.Zero);
1188
1189 var namePtr = Native.Z3_get_decl_name(nCtx, decl);
1190 return Marshal.PtrToStringAnsi(namePtr);
1191 }
1192
1196 public uint GetBvSortSize(Z3_sort bvSort)
1197 {
1198 Debug.Assert(bvSort != IntPtr.Zero);
1199
1200 return Native.Z3_get_bv_sort_size(nCtx, bvSort);
1201 }
1202
1207 {
1208 Debug.Assert(array != IntPtr.Zero);
1209
1210 return Native.Z3_get_array_sort_domain(nCtx, array);
1211 }
1212
1217 {
1218 Debug.Assert(array != IntPtr.Zero);
1219
1220 return Native.Z3_get_array_sort_range(nCtx, array);
1221 }
1222
1229 public bool TryGetNumeralInt(Z3_ast v, out int i)
1230 {
1231 Debug.Assert(v != IntPtr.Zero);
1232
1233 int result = i = 0;
1234 if (Native.Z3_get_numeral_int(nCtx, v, ref result) == 0)
1235 {
1236 return false;
1237 }
1238 i = result;
1239 return true;
1240 }
1241
1248 public bool TryGetNumeralUInt(Z3_ast v, out uint u)
1249 {
1250 Debug.Assert(v != IntPtr.Zero);
1251
1252 uint result = u = 0;
1253 if (Native.Z3_get_numeral_uint(nCtx, v, ref result) == 0)
1254 {
1255 return false;
1256 }
1257 u = result;
1258 return true;
1259 }
1260
1267 public bool TryGetNumeralInt64(Z3_ast v, out long i)
1268 {
1269 Debug.Assert(v != IntPtr.Zero);
1270
1271 long result = i = 0;
1272 if (Native.Z3_get_numeral_int64(nCtx, v, ref result) == 0)
1273 {
1274 return false;
1275 }
1276 i = result;
1277 return true;
1278 }
1279
1286 public bool TryGetNumeralUInt64(Z3_ast v, out ulong u)
1287 {
1288 Debug.Assert(v != IntPtr.Zero);
1289
1290 ulong result = u = 0;
1291 if (Native.Z3_get_numeral_uint64(nCtx, v, ref result) == 0)
1292 {
1293 return false;
1294 }
1295 u = result;
1296 return true;
1297 }
1298
1304 public string GetNumeralString(Z3_ast v)
1305 {
1306 Debug.Assert(v != IntPtr.Zero);
1307 return Native.Z3_get_numeral_string(nCtx, v);
1308 }
1309
1315 public string ToString(Z3_ast ast)
1316 {
1317 Debug.Assert(ast != IntPtr.Zero);
1318
1319 return Native.Z3_ast_to_string(nCtx, ast);
1320 }
1321
1326 public void ToggleWarningMessages(bool turnOn)
1327 => Native.Z3_toggle_warning_messages(turnOn ? (byte)1 : (byte)0);
1328
1329 #endregion
1330
1331 #region Internal
1332 internal static Object creation_lock = new Object();
1333 internal IntPtr m_ctx = IntPtr.Zero;
1334 internal Native.Z3_error_handler m_n_err_handler = null;
1335 internal IntPtr nCtx { get { return m_ctx; } }
1336
1337 internal void NativeErrorHandler(IntPtr ctx, Z3_error_code errorCode)
1338 {
1339 // Do-nothing error handler. The wrappers in Z3.Native will throw exceptions upon errors.
1340 }
1341
1342 internal void InitContext()
1343 {
1344 PrintMode = Z3_ast_print_mode.Z3_PRINT_SMTLIB2_COMPLIANT;
1345 m_n_err_handler = new Native.Z3_error_handler(NativeErrorHandler); // keep reference so it doesn't get collected.
1346 Native.Z3_set_error_handler(m_ctx, m_n_err_handler);
1347
1348 GC.SuppressFinalize(this);
1349 }
1350
1351 #endregion
1352
1353 #region Tracing
1358 public static void EnableTrace(string tag)
1359 {
1360 Debug.Assert(!string.IsNullOrEmpty(tag));
1361 Native.Z3_enable_trace(tag);
1362 }
1363
1364 #endregion
1365
1366 #region Dispose
1367
1371 public void Dispose()
1372 {
1373 if (m_ctx != IntPtr.Zero)
1374 {
1375 m_n_err_handler = null;
1376 IntPtr ctx = m_ctx;
1377 m_ctx = IntPtr.Zero;
1378 Native.Z3_del_context(ctx);
1379 }
1380 else
1381 GC.ReRegisterForFinalize(this);
1382 }
1383 #endregion
1384
1385
1392 {
1393 Native.Z3_ast_vector_inc_ref(nCtx, vec);
1394 var sz = Native.Z3_ast_vector_size(nCtx, vec);
1395 var result = new Z3_ast[sz];
1396 for (uint i = 0; i < sz; ++i)
1397 result[i] = Native.Z3_ast_vector_get(nCtx, vec, i);
1398 Native.Z3_ast_vector_dec_ref(nCtx, vec);
1399 return result;
1400 }
1401
1408 {
1409 Native.Z3_stats_inc_ref(nCtx, stats);
1410 var result = Statistics.NativeEntries(nCtx, stats);
1411 Native.Z3_stats_dec_ref(nCtx, stats);
1412 return result;
1413 }
1414
1415 }
1416}
A Boolean sort.
Definition: BoolSort.cs:29
An Integer sort
Definition: IntSort.cs:29
The main interaction with Z3 happens via the Context. NativeContext allows for efficient wrapper-redu...
Z3_sort MkListSort(string name, Z3_sort elemSort, out Z3_func_decl inil, out Z3_func_decl iisnil, out Z3_func_decl icons, out Z3_func_decl iiscons, out Z3_func_decl ihead, out Z3_func_decl itail)
returns ListSort
Z3_ast MkGe(Z3_ast t1, Z3_ast t2)
Create an expression representing t1 >= t2
Z3_ast MkApp(Z3_func_decl f, params Z3_ast[] args)
Create a new function application.
Z3_ast_vector MkBvSrem(Z3_ast_vector t1, Z3_ast_vector t2)
Signed remainder.
Z3_sort MkTupleSort(Z3_symbol name, Z3_symbol[] fieldNames, Z3_sort[] fieldSorts, out Z3_func_decl constructor, Z3_func_decl[] projections)
Create a new tuple sort.
Z3_ast_vector MkBvAshr(Z3_ast_vector t1, Z3_ast_vector t2)
Arithmetic shift right
Z3_ast_kind GetAstKind(Z3_ast ast)
Get the AST kind from IntPtr
Z3_ast_vector MkBvSub(Z3_ast_vector t1, Z3_ast_vector t2)
Two's complement subtraction.
Z3_ast MkIff(Z3_ast t1, Z3_ast t2)
Create an expression representing t1 iff t2.
uint GetNumArgs(Z3_app app)
Return number of arguments for app
Z3_ast MkAdd(params Z3_ast[] t)
Create an expression representing t[0] + t[1] + ....
Z3_sort GetArraySortRange(Z3_ast array)
Get the range IntPtr for Sort
Z3_ast_vector MkBvAnd(Z3_ast_vector t1, Z3_ast_vector t2)
Bitwise conjunction.
Z3_ast_vector MkBvSdiv(Z3_ast_vector t1, Z3_ast_vector t2)
Signed division.
Z3_ast_vector MkBVNeg(Z3_ast_vector t)
Standard two's complement unary minus.
bool TryGetNumeralInt(Z3_ast v, out int i)
Try to get integer from AST
Z3_ast MkBvUle(Z3_ast t1, Z3_ast t2)
Unsigned less-than-equal
Z3_func_decl MkFuncDecl(string name, Z3_sort[] domain, Z3_sort range)
Creates a new function declaration.
bool TryGetNumeralUInt(Z3_ast v, out uint u)
Try to get uint from AST
Z3_sort MkBvSort(uint size)
Returns the BvSort for size in this NativeContext
Z3_pattern MkPattern(params Z3_ast[] terms)
Create a quantifier pattern.
Z3_ast MkEq(Z3_ast x, Z3_ast y)
Creates the equality x = y .
Z3_symbol MkStringSymbol(string name)
Return a ptr to symbol for string
Z3_func_decl MkFuncDecl(string name, Z3_sort domain, Z3_sort range)
Creates a new function declaration.
Z3_ast[] GetAppArgs(Z3_app app)
Get the arguments for app
Z3_ast MkDiv(Z3_ast t1, Z3_ast t2)
Create an expression representing t1 / t2.
Z3_ast MkBool(bool value)
Creates a Boolean value.
Z3_ast MkExists(Z3_sort[] sorts, Z3_symbol[] names, Z3_ast body, uint weight=1, Z3_ast[] patterns=null, Z3_ast[] noPatterns=null, Symbol quantifierID=null, Symbol skolemID=null)
Same as MkForAll but defaults to "forall" = false Create an existential Quantifier.
Z3_ast_vector MkBvNot(Z3_ast_vector t)
Bitwise negation.
Z3_ast MkStore(Z3_ast a, Z3_ast i, Z3_ast v)
Array update.
bool TryGetNumeralInt64(Z3_ast v, out long i)
Try to get long from AST
Z3_ast_vector MkBvShl(Z3_ast_vector t1, Z3_ast_vector t2)
Shift left.
Z3_ast MkNot(Z3_ast a)
Mk an expression representing not(a).
Z3_ast_print_mode PrintMode
Selects the format used for pretty-printing expressions.
void Dispose()
Disposes of the context.
Z3_ast MkNumeral(uint v, Z3_sort sort)
Create a Term of a given sort. This function can be used to create numerals that fit in a machine int...
Z3_ast MkAnd(params Z3_ast[] t)
Create an expression representing t[0] and t[1] and ....
Z3_sort[] GetDomain(Z3_func_decl fdecl)
Get domain for a funcdecl
Z3_decl_kind GetDeclKind(Z3_func_decl decl)
Get the Decl kind from IntPtr
Z3_ast MkOr(params Z3_ast[] t)
Create an expression representing t[0] or t[1] or ....
Z3_ast MkConstArray(Z3_sort domain, Z3_ast v)
Create a constant array.
Z3_ast MkIte(Z3_ast t1, Z3_ast t2, Z3_ast t3)
Create an expression representing an if-then-else: ite(t1, t2, t3).
Z3_ast MkLe(Z3_ast t1, Z3_ast t2)
Create an expression representing t1 <= t2
Z3_ast_vector MkBvOr(Z3_ast_vector t1, Z3_ast_vector t2)
Bitwise disjunction.
Z3_ast MkSelect(Z3_ast array, Z3_ast index)
Array read.
Z3_sort GetRange(Z3_func_decl fdecl)
Get range for a funcdecl
Z3_ast_vector MkBvUrem(Z3_ast_vector t1, Z3_ast_vector t2)
Unsigned remainder.
Z3_ast_vector MkBvAdd(Z3_ast_vector t1, Z3_ast_vector t2)
Two's complement addition.
Z3_ast MkBvUlt(Z3_ast t1, Z3_ast t2)
Unsigned less-than
Z3_ast MkNumeral(long v, Z3_sort sort)
Create a Term of a given sort. This function can be used to create numerals that fit in a machine int...
Z3_ast MkNumeral(int v, Z3_sort sort)
Create a Term of a given sort. This function can be used to create numerals that fit in a machine int...
Z3_func_decl MkConstDecl(string name, Z3_sort range)
Creates a new constant function declaration.
Z3_ast MkReal(string v)
Create a real numeral.
Statistics.Entry[] GetStatistics(Z3_stats stats)
Retrieve statistics as an array of entries
Z3_sort GetSort(Z3_ast ast)
Get Sort for AST
Z3_ast MkFalse()
The false Term.
void ToggleWarningMessages(bool turnOn)
Enable or disable warning messages
Z3_ast_vector MkBvXor(Z3_ast_vector t1, Z3_ast_vector t2)
Bitwise XOR.
Z3_ast MkMul(params Z3_ast[] t)
Create an expression representing t[0] * t[1] * ....
Z3_ast MkLt(Z3_ast t1, Z3_ast t2)
Create an expression representing t1 < t2
Z3_sort GetArraySortDomain(Z3_ast array)
Get the domain IntPtr for Sort
Z3_ast_vector MkBvZeroExt(uint i, Z3_ast_vector t)
Bit-vector zero extension.
Z3_sort_kind GetSortKind(Z3_sort sort)
Get the sort kind from IntPtr
Z3_sort MkArraySort(Z3_sort domain, Z3_sort range)
Create a new array sort.
Z3_ast_vector MkBvMul(Z3_ast_vector t1, Z3_ast_vector t2)
Two's complement multiplication.
Z3_ast MkBound(uint index, Z3_sort sort)
Creates a new bound variable.
Z3_ast_vector MkBvNeg(Z3_ast_vector t)
Standard two's complement unary minus.
Z3_ast MkSub(params Z3_ast[] t)
Create an expression representing t[0] - t[1] - ....
uint GetBvSortSize(Z3_sort bvSort)
Get size of BitVector Sort
string GetDeclName(Z3_func_decl decl)
Get string name for Decl
Z3_ast_vector MkBvLshr(Z3_ast_vector t1, Z3_ast_vector t2)
Logical shift right
Z3_ast MkBvSlt(Z3_ast_vector t1, Z3_ast_vector t2)
Two's complement signed less-than
Z3_ast MkDefault(Z3_ast a)
Access the array default value.
Z3_ast MkTrue()
The true Term.
string GetNumeralString(Z3_ast v)
Get string for numeral ast
NativeContext(Dictionary< string, string > settings)
Constructor.
Z3_func_decl GetAppDecl(Z3_ast ast)
Get App Decl from IntPtr
Z3_ast_vector MkBvSignExt(uint i, Z3_ast_vector t)
Bit-vector sign extension.
Z3_ast MkConst(string name, Z3_sort range)
Creates a new Constant of sort range and named name .
Z3_ast_vector MkBvUdiv(Z3_ast_vector t1, Z3_ast_vector t2)
Unsigned division.
static void EnableTrace(string tag)
Enable trace to file
bool TryGetNumeralUInt64(Z3_ast v, out ulong u)
Try get ulong from AST
NativeSolver MkSimpleSolver()
Creates a new (incremental) solver.
Z3_ast MkGt(Z3_ast t1, Z3_ast t2)
Create an expression representing t1 > t2
Z3_ast[] ToArray(Z3_ast_vector vec)
Utility to convert a vector object of ast to a .Net array
string ToString(Z3_ast ast)
Get printable string representing Z3_ast
Z3_ast MkForall(Z3_sort[] sorts, Z3_symbol[] names, Z3_ast body, uint weight=1, Z3_ast[] patterns=null, Z3_ast[] noPatterns=null, Symbol quantifierID=null, Symbol skolemID=null)
Create a universal Quantifier.
Z3_func_decl MkFreshFuncDecl(string prefix, Z3_sort[] domain, Z3_sort range)
Creates a fresh function declaration with a name prefixed with prefix .
Z3_ast_vector MkBvExtract(uint high, uint low, Z3_ast_vector t)
Bit-vector extraction.
Z3_ast MkImplies(Z3_ast t1, Z3_ast t2)
Create an expression representing t1 -> t2.
Statistical data is organized into pairs of [Key, Entry], where every Entry is either a DoubleEntry o...
Definition: Statistics.cs:39
Objects of this class track statistical information about solvers.
Definition: Statistics.cs:33
Symbols are used to name several term and type constructors.
Definition: Symbol.cs:30
The exception base class for error reporting from Z3
Definition: Z3Exception.cs:32
Z3_ast_print_mode
Z3 pretty printing modes (See Z3_set_ast_print_mode).
Definition: z3_api.h:1317
Z3_ast_kind
The different kinds of Z3 AST (abstract syntax trees). That is, terms, formulas and types.
Definition: z3_api.h:139
Z3_decl_kind
The different kinds of interpreted function kinds.
Definition: z3_api.h:961
Z3_sort_kind
The different kinds of Z3 types (See Z3_get_sort_kind).
Definition: z3_api.h:108
Z3_error_code
Z3 error codes (See Z3_get_error_code).
Definition: z3_api.h:1342
System.IntPtr Z3_pattern
System.IntPtr Z3_app
System.IntPtr Z3_func_decl
System.IntPtr Z3_ast_vector
System.IntPtr Z3_stats
System.IntPtr Z3_ast
System.IntPtr Z3_solver
System.IntPtr Z3_sort
System.IntPtr Z3_symbol
expr range(expr const &lo, expr const &hi)
Definition: z3++.h:3946
def Length(s)
Definition: z3py.py:11063