src/HOL/Tools/SMT/smt_real.ML
author boehmes
Sun Dec 19 17:55:56 2010 +0100 (2010-12-19)
changeset 41280 a7de9d36f4f2
parent 41072 9f9bc1bdacef
child 41281 679118e35378
permissions -rw-r--r--
only linear occurrences of multiplication are treated as built-in (SMT solvers only support linear arithmetic in general);
hide internal constants z3div and z3mod;
rewrite div/mod to z3div/z3mod instead of adding extra rules characterizing div/mod in terms of z3div/z3mod
     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 times _ T ts = if is_linear ts then SOME ((("*", 2), T), ts, T) else NONE
    39     | times _ _ _  = NONE
    40 
    41   fun divide _ T (ts as [_, t]) =
    42         if U.is_number t then SOME ((("/", 2), T), ts, T) else NONE
    43     | divide _ _ _ = NONE
    44 in
    45 
    46 val setup_builtins =
    47   B.add_builtin_typ smtlibC (@{typ real}, K (SOME "Real"), real_num) #>
    48   fold (B.add_builtin_fun' smtlibC) [
    49     (@{const less (real)}, "<"),
    50     (@{const less_eq (real)}, "<="),
    51     (@{const uminus (real)}, "~"),
    52     (@{const plus (real)}, "+"),
    53     (@{const minus (real)}, "-") ] #>
    54   B.add_builtin_fun SMTLIB_Interface.smtlibC
    55     (Term.dest_Const @{const times (real)}, times) #>
    56   B.add_builtin_fun Z3_Interface.smtlib_z3C
    57     (Term.dest_Const @{const divide (real)}, divide)
    58 
    59 end
    60 
    61 
    62 (* Z3 constructors *)
    63 
    64 local
    65   structure I = Z3_Interface
    66 
    67   fun z3_mk_builtin_typ (I.Sym ("Real", _)) = SOME @{typ real}
    68     | z3_mk_builtin_typ (I.Sym ("real", _)) = SOME @{typ real} (*FIXME: delete*)
    69     | z3_mk_builtin_typ _ = NONE
    70 
    71   fun z3_mk_builtin_num _ i T =
    72     if T = @{typ real} then SOME (Numeral.mk_cnumber @{ctyp real} i)
    73     else NONE
    74 
    75   val mk_uminus = Thm.capply (Thm.cterm_of @{theory} @{const uminus (real)})
    76   val mk_add = Thm.mk_binop (Thm.cterm_of @{theory} @{const plus (real)})
    77   val mk_sub = Thm.mk_binop (Thm.cterm_of @{theory} @{const minus (real)})
    78   val mk_mul = Thm.mk_binop (Thm.cterm_of @{theory} @{const times (real)})
    79   val mk_div = Thm.mk_binop (Thm.cterm_of @{theory} @{const divide (real)})
    80   val mk_lt = Thm.mk_binop (Thm.cterm_of @{theory} @{const less (real)})
    81   val mk_le = Thm.mk_binop (Thm.cterm_of @{theory} @{const less_eq (real)})
    82 
    83   fun z3_mk_builtin_fun (I.Sym ("-", _)) [ct] = SOME (mk_uminus ct)
    84     | z3_mk_builtin_fun (I.Sym ("+", _)) [ct, cu] = SOME (mk_add ct cu)
    85     | z3_mk_builtin_fun (I.Sym ("-", _)) [ct, cu] = SOME (mk_sub ct cu)
    86     | z3_mk_builtin_fun (I.Sym ("*", _)) [ct, cu] = SOME (mk_mul ct cu)
    87     | z3_mk_builtin_fun (I.Sym ("/", _)) [ct, cu] = SOME (mk_div ct cu)
    88     | z3_mk_builtin_fun (I.Sym ("<", _)) [ct, cu] = SOME (mk_lt ct cu)
    89     | z3_mk_builtin_fun (I.Sym ("<=", _)) [ct, cu] = SOME (mk_le ct cu)
    90     | z3_mk_builtin_fun (I.Sym (">", _)) [ct, cu] = SOME (mk_lt cu ct)
    91     | z3_mk_builtin_fun (I.Sym (">=", _)) [ct, cu] = SOME (mk_le cu ct)
    92     | z3_mk_builtin_fun _ _ = NONE
    93 in
    94 
    95 val z3_mk_builtins = {
    96   mk_builtin_typ = z3_mk_builtin_typ,
    97   mk_builtin_num = z3_mk_builtin_num,
    98   mk_builtin_fun = (fn _ => fn sym => fn cts =>
    99     (case try (#T o Thm.rep_cterm o hd) cts of
   100       SOME @{typ real} => z3_mk_builtin_fun sym cts
   101     | _ => NONE)) }
   102 
   103 end
   104 
   105 
   106 (* Z3 proof reconstruction *)
   107 
   108 val real_rules = @{lemma
   109   "0 + (x::real) = x"
   110   "x + 0 = x"
   111   "0 * x = 0"
   112   "1 * x = x"
   113   "x + y = y + x"
   114   by auto}
   115 
   116 val real_linarith_proc = Simplifier.simproc_global @{theory} "fast_real_arith" [
   117   "(m::real) < n", "(m::real) <= n", "(m::real) = n"] (K Lin_Arith.simproc)
   118 
   119 
   120 (* setup *)
   121 
   122 val setup =
   123   Context.theory_map (
   124     SMTLIB_Interface.add_logic (10, smtlib_logic) #>
   125     setup_builtins #>
   126     Z3_Interface.add_mk_builtins z3_mk_builtins #>
   127     fold Z3_Proof_Reconstruction.add_z3_rule real_rules #>
   128     Z3_Proof_Tools.add_simproc real_linarith_proc)
   129 
   130 end