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