/// <summary>
        /// Create a new function application.
        /// </summary>
        public Z3_ast MkApp(Z3_func_decl f, params Z3_ast[] args)
        {
            Debug.Assert(f != IntPtr.Zero);
            Debug.Assert(args == null || args.All(a => a != IntPtr.Zero));

            return(Native.Z3_mk_app(nCtx, f, (uint)(args?.Length ?? 0), args));
        }
Exemple #2
0
        /// <summary>
        /// Add constraints to ensure the function f can only be injective.
        /// Example:
        /// for function f : D1 x D2 -> R
        /// assert axioms
        ///   forall (x1 : D1, x2 : D2) x1 = inv1(f(x1,x2))
        ///   forall (x1 : D1, x2 : D2) x2 = inv2(f(x1,x2))
        /// </summary>
        /// <param name="f"></param>
        public void AssertInjective(Z3_func_decl f)
        {
            uint    arity = Native.Z3_get_arity(nCtx, f);
            Z3_sort range = Native.Z3_get_range(nCtx, f);

            Z3_ast[]    vars  = new Z3_ast[arity];
            Z3_sort[]   sorts = new Z3_sort[arity];
            Z3_symbol[] names = new Z3_symbol[arity];
            for (uint i = 0; i < arity; ++i)
            {
                Z3_sort domain = Native.Z3_get_domain(nCtx, f, i);
                vars[i]  = ntvContext.MkBound(arity - i - 1, domain);
                sorts[i] = domain;
                names[i] = Native.Z3_mk_int_symbol(nCtx, (int)i);
            }
            Z3_ast app_f = IntPtr.Zero; // Context.MkApp(f, vars);

            for (uint i = 0; i < arity; ++i)
            {
                Z3_sort      domain = Native.Z3_get_domain(nCtx, f, i);
                Z3_func_decl proj   = ntvContext.MkFreshFuncDecl("inv", new Z3_sort[] { range }, domain);
                Z3_ast       body   = ntvContext.MkEq(vars[i], ntvContext.MkApp(proj, app_f));
                Z3_ast       q      = ntvContext.MkForall(names, sorts, body);
                Assert(q);
            }
        }
Exemple #3
0
        internal NativeFuncInterp(NativeContext ctx, NativeModel mdl, Z3_func_decl decl, Z3_func_interp fi)
        {
            Debug.Assert(ctx != null);
            Z3_context nCtx = ctx.nCtx;

            Native.Z3_func_interp_inc_ref(nCtx, fi);

            Declaration = decl;
            Else        = Native.Z3_func_interp_get_else(nCtx, fi);
            uint numEntries = Native.Z3_func_interp_get_num_entries(nCtx, fi);
            uint numArgs    = Native.Z3_func_interp_get_arity(nCtx, fi);

            Entries = new Entry[numEntries];

            for (uint j = 0; j < numEntries; ++j)
            {
                var entry = Native.Z3_func_interp_get_entry(nCtx, fi, j);
                Native.Z3_func_entry_inc_ref(nCtx, entry);
                Entries[j].Arguments = new Z3_ast[numArgs];
                for (uint i = 0; i < numArgs; ++i)
                {
                    Entries[j].Arguments[i] = Native.Z3_func_entry_get_arg(nCtx, entry, i);
                }
                Entries[j].Result = Native.Z3_func_entry_get_value(nCtx, entry);
                Native.Z3_func_entry_dec_ref(nCtx, entry);
            }

            Native.Z3_func_interp_dec_ref(nCtx, fi);
        }
        /// <summary>
        /// returns ListSort
        /// </summary>
        /// <param name="name"></param>
        /// <param name="elemSort"></param>
        /// <param name="inil"></param>
        /// <param name="iisnil"></param>
        /// <param name="icons"></param>
        /// <param name="iiscons"></param>
        /// <param name="ihead"></param>
        /// <param name="itail"></param>
        /// <returns>The list algebraic datatype</returns>

        public 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)
        {
            Debug.Assert(!string.IsNullOrEmpty(name));
            Debug.Assert(elemSort != IntPtr.Zero);

            IntPtr nil = IntPtr.Zero, isnil = IntPtr.Zero,
                   cons = IntPtr.Zero, iscons = IntPtr.Zero,
                   head = IntPtr.Zero, tail = IntPtr.Zero;

            var symbol = Native.Z3_mk_string_symbol(nCtx, name);
            var sort   = Native.Z3_mk_list_sort(nCtx, symbol, elemSort,
                                                ref nil, ref isnil, ref cons, ref iscons, ref head, ref tail);

            inil    = nil;
            iisnil  = isnil;
            icons   = cons;
            iiscons = iscons;
            ihead   = head;
            itail   = tail;

            return(sort);
        }
        /// <summary>
        /// Get string name for Decl
        /// </summary>
        /// <param name="decl"></param>
        /// <returns></returns>
        public string GetDeclName(Z3_func_decl decl)
        {
            Debug.Assert(decl != IntPtr.Zero);

            var namePtr = Native.Z3_get_decl_name(nCtx, decl);

            return(Marshal.PtrToStringAnsi(namePtr));
        }
Exemple #6
0
        /// <summary>
        /// Retrieves the interpretation (the assignment) of <paramref name="f"/> in the model.
        /// </summary>
        /// <param name="f">A function declaration of zero arity</param>
        /// <returns>An expression if the function has an interpretation in the model, null otherwise.</returns>
        public Z3_ast ConstFuncInterp(Z3_func_decl f)
        {
            if (Native.Z3_get_arity(ntvContext.nCtx, f) != 0)
            {
                throw new Z3Exception("Non-zero arity functions have FunctionInterpretations as a model. Use FuncInterp.");
            }

            return(Native.Z3_model_get_const_interp(ntvContext.nCtx, NativeObject, f));
        }
        /// <summary>
        /// Get domain for a funcdecl
        /// </summary>
        /// <param name="fdecl"></param>
        /// <returns></returns>
        public Z3_sort[] GetDomain(Z3_func_decl fdecl)
        {
            Debug.Assert(fdecl != IntPtr.Zero);

            var sz     = Native.Z3_get_domain_size(nCtx, fdecl);
            var domain = new Z3_sort[sz];

            for (uint i = 0; i < sz; i++)
            {
                domain[i] = Native.Z3_get_domain(nCtx, fdecl, i);
            }
            return(domain);
        }
Exemple #8
0
        /// <summary>
        /// Retrieves the interpretation (the assignment) of a non-constant <paramref name="f"/> in the model.
        /// </summary>
        /// <param name="f">A function declaration of non-zero arity</param>
        /// <returns>A FunctionInterpretation if the function has an interpretation in the model, null otherwise.</returns>
        public NativeFuncInterp FuncInterp(Z3_func_decl f)
        {
            Z3_sort_kind sk = (Z3_sort_kind)Native.Z3_get_sort_kind(ntvContext.nCtx, Native.Z3_get_range(ntvContext.nCtx, f));

            if (Native.Z3_get_arity(ntvContext.nCtx, f) == 0)
            {
                IntPtr n = Native.Z3_model_get_const_interp(ntvContext.nCtx, NativeObject, f);

                if (sk == Z3_sort_kind.Z3_ARRAY_SORT)
                {
                    if (n == IntPtr.Zero)
                    {
                        return(null);
                    }
                    else
                    {
                        if (Native.Z3_is_as_array(ntvContext.nCtx, n) == 0)
                        {
                            throw new Z3Exception("Argument was not an array constant");
                        }
                        var fd = Native.Z3_get_as_array_func_decl(ntvContext.nCtx, n);
                        return(new NativeFuncInterp(ntvContext, this, f, fd));
                    }
                }
                else
                {
                    throw new Z3Exception("Constant functions do not have a function interpretation; use ConstInterp");
                }
            }
            else
            {
                IntPtr n = Native.Z3_model_get_func_interp(ntvContext.nCtx, NativeObject, f);
                if (n == IntPtr.Zero)
                {
                    return(null);
                }
                else
                {
                    return(new NativeFuncInterp(ntvContext, this, f, n));
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// Convert the interpretation of t into a sequence of array updates
        /// </summary>
        /// <param name="t"></param>
        /// <param name="result"></param>
        /// <returns>null if the argument does evaluate to a sequence of stores to an array</returns>
        public bool TryGetArrayValue(Z3_ast t, out ArrayValue result)
        {
            var r = Eval(t, true);
            // check that r is a sequence of store over a constant default array.
            var updates = new Dictionary <Z3_ast, Z3_ast>();

            result = null;
            while (true)
            {
                if (ntvContext.GetAstKind(r) != Z3_ast_kind.Z3_APP_AST)
                {
                    return(false);
                }
                Z3_func_decl f    = ntvContext.GetAppDecl(r);
                var          kind = ntvContext.GetDeclKind(f);
                if (kind == Z3_decl_kind.Z3_OP_CONST_ARRAY)
                {
                    result         = new ArrayValue();
                    result.Else    = ntvContext.GetAppArg(r, 0);
                    result.Updates = updates.ToArray();
                    result.Domain  = updates.Keys.ToArray();
                    result.Range   = updates.Values.ToArray();
                    return(true);
                }
                else if (kind == Z3_decl_kind.Z3_OP_STORE)
                {
                    Debug.Assert(ntvContext.GetNumArgs(r) == 3);
                    updates[ntvContext.GetAppArg(r, 1)] = ntvContext.GetAppArg(r, 2);
                    r = ntvContext.GetAppArg(r, 0);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemple #10
0
 public static Z3_ast Z3_fixedpoint_get_cover_delta(Z3_context a0, Z3_fixedpoint a1, int a2, Z3_func_decl a3) {
     Z3_ast r = LIB.Z3_fixedpoint_get_cover_delta(a0, a1, a2, a3);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return r;
 }
Exemple #11
0
 public extern static void Z3_fixedpoint_add_fact(Z3_context a0, Z3_fixedpoint a1, Z3_func_decl a2, uint a3, [In] uint[] a4);
Exemple #12
0
 public extern static IntPtr Z3_get_decl_rational_parameter(Z3_context a0, Z3_func_decl a1, uint a2);
Exemple #13
0
 public extern static Z3_func_interp Z3_model_get_func_interp(Z3_context a0, Z3_model a1, Z3_func_decl a2);
Exemple #14
0
 public extern static Z3_ast Z3_func_decl_to_ast(Z3_context a0, Z3_func_decl a1);
Exemple #15
0
 public extern static double Z3_get_decl_double_parameter(Z3_context a0, Z3_func_decl a1, uint a2);
Exemple #16
0
 public static int Z3_eval_decl(Z3_context a0, Z3_model a1, Z3_func_decl a2, uint a3, [In] Z3_ast[] a4, [In, Out] ref Z3_ast a5) {
     int r = LIB.Z3_eval_decl(a0, a1, a2, a3, a4, ref a5);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return r;
 }
Exemple #17
0
 public extern static Z3_ast Z3_datatype_update_field(Z3_context a0, Z3_func_decl a1, Z3_ast a2, Z3_ast a3);
Exemple #18
0
 public static void Z3_fixedpoint_set_predicate_representation(Z3_context a0, Z3_fixedpoint a1, Z3_func_decl a2, uint a3, [In] IntPtr[] a4) {
     LIB.Z3_fixedpoint_set_predicate_representation(a0, a1, a2, a3, a4);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
 }
Exemple #19
0
 public static void Z3_fixedpoint_register_relation(Z3_context a0, Z3_fixedpoint a1, Z3_func_decl a2) {
     LIB.Z3_fixedpoint_register_relation(a0, a1, a2);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
 }
Exemple #20
0
 public static void Z3_fixedpoint_add_cover(Z3_context a0, Z3_fixedpoint a1, int a2, Z3_func_decl a3, Z3_ast a4) {
     LIB.Z3_fixedpoint_add_cover(a0, a1, a2, a3, a4);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
 }
Exemple #21
0
 public static Z3_ast Z3_get_decl_ast_parameter(Z3_context a0, Z3_func_decl a1, uint a2) {
     Z3_ast r = LIB.Z3_get_decl_ast_parameter(a0, a1, a2);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return r;
 }
Exemple #22
0
 public extern static int Z3_is_eq_func_decl(Z3_context a0, Z3_func_decl a1, Z3_func_decl a2);
Exemple #23
0
 public extern static uint Z3_get_decl_parameter_kind(Z3_context a0, Z3_func_decl a1, uint a2);
Exemple #24
0
 public extern static IntPtr Z3_get_decl_name(Z3_context a0, Z3_func_decl a1);
Exemple #25
0
 public extern static Z3_func_decl Z3_get_decl_func_decl_parameter(Z3_context a0, Z3_func_decl a1, uint a2);
Exemple #26
0
 public extern static uint Z3_get_decl_kind(Z3_context a0, Z3_func_decl a1);
Exemple #27
0
 public extern static int Z3_model_has_interp(Z3_context a0, Z3_model a1, Z3_func_decl a2);
Exemple #28
0
 public extern static uint Z3_get_domain_size(Z3_context a0, Z3_func_decl a1);
Exemple #29
0
 public extern static IntPtr Z3_func_decl_to_string(Z3_context a0, Z3_func_decl a1);
        /// <summary>
        /// Get range for a funcdecl
        /// </summary>
        /// <param name="fdecl"></param>
        /// <returns></returns>
        public Z3_sort GetRange(Z3_func_decl fdecl)
        {
            Debug.Assert(fdecl != IntPtr.Zero);

            return(Native.Z3_get_range(nCtx, fdecl));
        }
Exemple #31
0
 public static string Z3_func_decl_to_string(Z3_context a0, Z3_func_decl a1) {
     IntPtr r = LIB.Z3_func_decl_to_string(a0, a1);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return Marshal.PtrToStringAnsi(r);
 }
Exemple #32
0
 public extern static Z3_ast Z3_mk_map(Z3_context a0, Z3_func_decl a1, uint a2, [In] Z3_ast[] a3);
Exemple #33
0
 public static Z3_func_interp Z3_model_get_func_interp(Z3_context a0, Z3_model a1, Z3_func_decl a2) {
     Z3_func_interp r = LIB.Z3_model_get_func_interp(a0, a1, a2);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return r;
 }
        /// <summary>
        /// Create a new tuple sort.
        /// </summary>
        public Z3_sort MkTupleSort(Z3_symbol name, Z3_symbol[] fieldNames, Z3_sort[] fieldSorts, out Z3_func_decl constructor, Z3_func_decl[] projections)
        {
            Debug.Assert(name != IntPtr.Zero);
            Debug.Assert(fieldNames != null);
            Debug.Assert(fieldNames.All(fn => fn != IntPtr.Zero));
            Debug.Assert(fieldSorts == null || fieldSorts.All(fs => fs != IntPtr.Zero));

            var numFields = (uint)(fieldNames?.Length ?? 0);

            constructor = IntPtr.Zero;
            return(Native.Z3_mk_tuple_sort(nCtx, name, numFields, fieldNames, fieldSorts, ref constructor, projections));
        }
Exemple #35
0
 public extern static uint Z3_get_arity(Z3_context a0, Z3_func_decl a1);
Exemple #36
0
 public static uint Z3_fixedpoint_get_num_levels(Z3_context a0, Z3_fixedpoint a1, Z3_func_decl a2) {
     uint r = LIB.Z3_fixedpoint_get_num_levels(a0, a1, a2);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return r;
 }
        /// <summary>
        /// Get the Decl kind from IntPtr
        /// </summary>
        public Z3_decl_kind GetDeclKind(Z3_func_decl decl)
        {
            Debug.Assert(decl != IntPtr.Zero);

            return((Z3_decl_kind)Native.Z3_get_decl_kind(nCtx, decl));
        }
Exemple #38
0
 public extern static Z3_sort Z3_get_domain(Z3_context a0, Z3_func_decl a1, uint a2);
Exemple #39
0
 public static Z3_ast Z3_datatype_update_field(Z3_context a0, Z3_func_decl a1, Z3_ast a2, Z3_ast a3) {
     Z3_ast r = LIB.Z3_datatype_update_field(a0, a1, a2, a3);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return r;
 }
Exemple #40
0
 public extern static uint Z3_get_decl_num_parameters(Z3_context a0, Z3_func_decl a1);
Exemple #41
0
 public extern static Z3_sort Z3_get_range(Z3_context a0, Z3_func_decl a1);
Exemple #42
0
 public static Z3_sort Z3_get_range(Z3_context a0, Z3_func_decl a1) {
     Z3_sort r = LIB.Z3_get_range(a0, a1);
     Z3_error_code err = (Z3_error_code)LIB.Z3_get_error_code(a0);
     if (err != Z3_error_code.Z3_OK)
         throw new Z3Exception(Marshal.PtrToStringAnsi(LIB.Z3_get_error_msg_ex(a0, (uint)err)));
     return r;
 }