src/HOL/Tools/SMT/smt_real.ML
author boehmes
Mon Dec 20 09:06:37 2010 +0100 (2010-12-20)
changeset 41302 0485186839a7
parent 41281 679118e35378
child 41439 a31c451183e6
permissions -rw-r--r--
re-introduced support for nonlinear multiplication in Z3 (overriding the built-in linear multiplication of the SMT-LIB class of solvers)
     1 (*  Title:      HOL/Tools/SMT/smt_real.ML
     2     Author:     Sascha Boehme, TU Muenchen
     3 
     4 SMT setup for reals.
     5 *)
     6 
     7 signature SMT_REAL =
     8 sig
     9   val setup: theory -> theory
    10 end
    11 
    12 structure SMT_Real: SMT_REAL =
    13 struct
    14 
    15 structure U = SMT_Utils
    16 structure B = SMT_Builtin
    17 
    18 
    19 (* SMT-LIB logic *)
    20 
    21 fun smtlib_logic ts =
    22   if exists (Term.exists_type (Term.exists_subtype (equal @{typ real}))) ts
    23   then SOME "AUFLIRA"
    24   else NONE
    25 
    26 
    27 (* SMT-LIB and Z3 built-ins *)
    28 
    29 local
    30   val smtlibC = SMTLIB_Interface.smtlibC
    31 
    32   fun real_num _ i = SOME (string_of_int i ^ ".0")
    33 
    34   fun is_linear [t] = U.is_number t
    35     | is_linear [t, u] = U.is_number t orelse U.is_number u
    36     | is_linear _ = false
    37 
    38   fun mk_times ts = Term.list_comb (@{const times (real)}, ts)
    39 
    40   fun times _ T ts = if is_linear ts then SOME ("*", 2, ts, mk_times) else NONE
    41     | times _ _ _  = NONE
    42 in
    43 
    44 val setup_builtins =
    45   B.add_builtin_typ smtlibC (@{typ real}, K (SOME "Real"), real_num) #>
    46   fold (B.add_builtin_fun' smtlibC) [
    47     (@{const less (real)}, "<"),
    48     (@{const less_eq (real)}, "<="),
    49     (@{const uminus (real)}, "~"),
    50     (@{const plus (real)}, "+"),
    51     (@{const minus (real)}, "-") ] #>
    52   B.add_builtin_fun SMTLIB_Interface.smtlibC
    53     (Term.dest_Const @{const times (real)}, times) #>
    54   B.add_builtin_fun' Z3_Interface.smtlib_z3C (@{const times (real)}, "*") #>
    55   B.add_builtin_fun' Z3_Interface.smtlib_z3C (@{const divide (real)}, "/")
    56 
    57 end
    58 
    59 
    60 (* Z3 constructors *)
    61 
    62 local
    63   structure I = Z3_Interface
    64 
    65   fun z3_mk_builtin_typ (I.Sym ("Real", _)) = SOME @{typ real}
    66     | z3_mk_builtin_typ (I.Sym ("real", _)) = SOME @{typ real} (*FIXME: delete*)
    67     | z3_mk_builtin_typ _ = NONE
    68 
    69   fun z3_mk_builtin_num _ i T =
    70     if T = @{typ real} then SOME (Numeral.mk_cnumber @{ctyp real} i)
    71     else NONE
    72 
    73   val mk_uminus = Thm.capply (Thm.cterm_of @{theory} @{const uminus (real)})
    74   val mk_add = Thm.mk_binop (Thm.cterm_of @{theory} @{const plus (real)})
    75   val mk_sub = Thm.mk_binop (Thm.cterm_of @{theory} @{const minus (real)})
    76   val mk_mul = Thm.mk_binop (Thm.cterm_of @{theory} @{const times (real)})
    77   val mk_div = Thm.mk_binop (Thm.cterm_of @{theory} @{const divide (real)})
    78   val mk_lt = Thm.mk_binop (Thm.cterm_of @{theory} @{const less (real)})
    79   val mk_le = Thm.mk_binop (Thm.cterm_of @{theory} @{const less_eq (real)})
    80 
    81   fun z3_mk_builtin_fun (I.Sym ("-", _)) [ct] = SOME (mk_uminus ct)
    82     | z3_mk_builtin_fun (I.Sym ("+", _)) [ct, cu] = SOME (mk_add ct cu)
    83     | z3_mk_builtin_fun (I.Sym ("-", _)) [ct, cu] = SOME (mk_sub ct cu)
    84     | z3_mk_builtin_fun (I.Sym ("*", _)) [ct, cu] = SOME (mk_mul ct cu)
    85     | z3_mk_builtin_fun (I.Sym ("/", _)) [ct, cu] = SOME (mk_div ct cu)
    86     | z3_mk_builtin_fun (I.Sym ("<", _)) [ct, cu] = SOME (mk_lt ct cu)
    87     | z3_mk_builtin_fun (I.Sym ("<=", _)) [ct, cu] = SOME (mk_le ct cu)
    88     | z3_mk_builtin_fun (I.Sym (">", _)) [ct, cu] = SOME (mk_lt cu ct)
    89     | z3_mk_builtin_fun (I.Sym (">=", _)) [ct, cu] = SOME (mk_le cu ct)
    90     | z3_mk_builtin_fun _ _ = NONE
    91 in
    92 
    93 val z3_mk_builtins = {
    94   mk_builtin_typ = z3_mk_builtin_typ,
    95   mk_builtin_num = z3_mk_builtin_num,
    96   mk_builtin_fun = (fn _ => fn sym => fn cts =>
    97     (case try (#T o Thm.rep_cterm o hd) cts of
    98       SOME @{typ real} => z3_mk_builtin_fun sym cts
    99     | _ => NONE)) }
   100 
   101 end
   102 
   103 
   104 (* Z3 proof reconstruction *)
   105 
   106 val real_rules = @{lemma
   107   "0 + (x::real) = x"
   108   "x + 0 = x"
   109   "0 * x = 0"
   110   "1 * x = x"
   111   "x + y = y + x"
   112   by auto}
   113 
   114 val real_linarith_proc = Simplifier.simproc_global @{theory} "fast_real_arith" [
   115   "(m::real) < n", "(m::real) <= n", "(m::real) = n"] (K Lin_Arith.simproc)
   116 
   117 
   118 (* setup *)
   119 
   120 val setup =
   121   Context.theory_map (
   122     SMTLIB_Interface.add_logic (10, smtlib_logic) #>
   123     setup_builtins #>
   124     Z3_Interface.add_mk_builtins z3_mk_builtins #>
   125     fold Z3_Proof_Reconstruction.add_z3_rule real_rules #>
   126     Z3_Proof_Tools.add_simproc real_linarith_proc)
   127 
   128 end