src/HOL/Tools/SMT/smt_real.ML
author boehmes
Wed Dec 08 08:33:02 2010 +0100 (2010-12-08)
changeset 41072 9f9bc1bdacef
parent 41059 d2b1fc1b8e19
child 41280 a7de9d36f4f2
permissions -rw-r--r--
be more flexible: store SMT built-in symbols in generic contexts (not in theory contexts)
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@41059
    15
structure B = SMT_Builtin
boehmes@41059
    16
boehmes@36899
    17
boehmes@36899
    18
(* SMT-LIB logic *)
boehmes@36899
    19
boehmes@36899
    20
fun smtlib_logic ts =
boehmes@36899
    21
  if exists (Term.exists_type (Term.exists_subtype (equal @{typ real}))) ts
boehmes@36899
    22
  then SOME "AUFLIRA"
boehmes@36899
    23
  else NONE
boehmes@36899
    24
boehmes@36899
    25
boehmes@41059
    26
(* SMT-LIB and Z3 built-ins *)
boehmes@36899
    27
boehmes@36899
    28
local
boehmes@41059
    29
  val smtlibC = SMTLIB_Interface.smtlibC
boehmes@36899
    30
boehmes@41059
    31
  fun real_num _ i = SOME (string_of_int i ^ ".0")
boehmes@36899
    32
in
boehmes@36899
    33
boehmes@41059
    34
val setup_builtins =
boehmes@41059
    35
  B.add_builtin_typ smtlibC (@{typ real}, K (SOME "Real"), real_num) #>
boehmes@41059
    36
  fold (B.add_builtin_fun' smtlibC) [
boehmes@41059
    37
    (@{const uminus (real)}, "~"),
boehmes@41059
    38
    (@{const plus (real)}, "+"),
boehmes@41059
    39
    (@{const minus (real)}, "-"),
boehmes@41059
    40
    (@{const times (real)}, "*"),
boehmes@41059
    41
    (@{const less (real)}, "<"),
boehmes@41059
    42
    (@{const less_eq (real)}, "<=") ] #>
boehmes@41059
    43
  B.add_builtin_fun' Z3_Interface.smtlib_z3C (@{const divide (real)}, "/")
boehmes@36899
    44
boehmes@36899
    45
end
boehmes@36899
    46
boehmes@36899
    47
boehmes@36899
    48
(* Z3 constructors *)
boehmes@36899
    49
boehmes@36899
    50
local
boehmes@36899
    51
  structure I = Z3_Interface
boehmes@36899
    52
boehmes@40516
    53
  fun z3_mk_builtin_typ (I.Sym ("Real", _)) = SOME @{typ real}
boehmes@40516
    54
    | z3_mk_builtin_typ (I.Sym ("real", _)) = SOME @{typ real} (*FIXME: delete*)
boehmes@36899
    55
    | z3_mk_builtin_typ _ = NONE
boehmes@36899
    56
boehmes@36899
    57
  fun z3_mk_builtin_num _ i T =
boehmes@36899
    58
    if T = @{typ real} then SOME (Numeral.mk_cnumber @{ctyp real} i)
boehmes@36899
    59
    else NONE
boehmes@36899
    60
boehmes@40579
    61
  val mk_uminus = Thm.capply (Thm.cterm_of @{theory} @{const uminus (real)})
boehmes@40579
    62
  val mk_add = Thm.mk_binop (Thm.cterm_of @{theory} @{const plus (real)})
boehmes@40579
    63
  val mk_sub = Thm.mk_binop (Thm.cterm_of @{theory} @{const minus (real)})
boehmes@40579
    64
  val mk_mul = Thm.mk_binop (Thm.cterm_of @{theory} @{const times (real)})
boehmes@40579
    65
  val mk_div = Thm.mk_binop (Thm.cterm_of @{theory} @{const divide (real)})
boehmes@40579
    66
  val mk_lt = Thm.mk_binop (Thm.cterm_of @{theory} @{const less (real)})
boehmes@40579
    67
  val mk_le = Thm.mk_binop (Thm.cterm_of @{theory} @{const less_eq (real)})
boehmes@36899
    68
boehmes@36899
    69
  fun z3_mk_builtin_fun (I.Sym ("-", _)) [ct] = SOME (mk_uminus ct)
boehmes@36899
    70
    | z3_mk_builtin_fun (I.Sym ("+", _)) [ct, cu] = SOME (mk_add ct cu)
boehmes@36899
    71
    | z3_mk_builtin_fun (I.Sym ("-", _)) [ct, cu] = SOME (mk_sub ct cu)
boehmes@36899
    72
    | z3_mk_builtin_fun (I.Sym ("*", _)) [ct, cu] = SOME (mk_mul ct cu)
boehmes@36899
    73
    | z3_mk_builtin_fun (I.Sym ("/", _)) [ct, cu] = SOME (mk_div ct cu)
boehmes@36899
    74
    | z3_mk_builtin_fun (I.Sym ("<", _)) [ct, cu] = SOME (mk_lt ct cu)
boehmes@36899
    75
    | z3_mk_builtin_fun (I.Sym ("<=", _)) [ct, cu] = SOME (mk_le ct cu)
boehmes@36899
    76
    | z3_mk_builtin_fun (I.Sym (">", _)) [ct, cu] = SOME (mk_lt cu ct)
boehmes@36899
    77
    | z3_mk_builtin_fun (I.Sym (">=", _)) [ct, cu] = SOME (mk_le cu ct)
boehmes@36899
    78
    | z3_mk_builtin_fun _ _ = NONE
boehmes@36899
    79
in
boehmes@36899
    80
boehmes@36899
    81
val z3_mk_builtins = {
boehmes@36899
    82
  mk_builtin_typ = z3_mk_builtin_typ,
boehmes@36899
    83
  mk_builtin_num = z3_mk_builtin_num,
boehmes@36899
    84
  mk_builtin_fun = (fn _ => fn sym => fn cts =>
boehmes@36899
    85
    (case try (#T o Thm.rep_cterm o hd) cts of
boehmes@36899
    86
      SOME @{typ real} => z3_mk_builtin_fun sym cts
boehmes@36899
    87
    | _ => NONE)) }
boehmes@36899
    88
boehmes@36899
    89
end
boehmes@36899
    90
boehmes@36899
    91
boehmes@36899
    92
(* Z3 proof reconstruction *)
boehmes@36899
    93
boehmes@36899
    94
val real_rules = @{lemma
boehmes@36899
    95
  "0 + (x::real) = x"
boehmes@36899
    96
  "x + 0 = x"
boehmes@36899
    97
  "0 * x = 0"
boehmes@36899
    98
  "1 * x = x"
boehmes@36899
    99
  "x + y = y + x"
boehmes@36899
   100
  by auto}
boehmes@36899
   101
wenzelm@38715
   102
val real_linarith_proc = Simplifier.simproc_global @{theory} "fast_real_arith" [
boehmes@36899
   103
  "(m::real) < n", "(m::real) <= n", "(m::real) = n"] (K Lin_Arith.simproc)
boehmes@36899
   104
boehmes@36899
   105
boehmes@36899
   106
(* setup *)
boehmes@36899
   107
boehmes@36899
   108
val setup =
boehmes@36899
   109
  Context.theory_map (
boehmes@41059
   110
    SMTLIB_Interface.add_logic (10, smtlib_logic) #>
boehmes@41072
   111
    setup_builtins #>
boehmes@36899
   112
    Z3_Interface.add_mk_builtins z3_mk_builtins #>
boehmes@36899
   113
    fold Z3_Proof_Reconstruction.add_z3_rule real_rules #>
boehmes@36899
   114
    Z3_Proof_Tools.add_simproc real_linarith_proc)
boehmes@36899
   115
boehmes@36899
   116
end