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