src/HOL/Tools/SMT/smt_real.ML
author wenzelm
Wed Aug 25 18:36:22 2010 +0200 (2010-08-25)
changeset 38715 6513ea67d95d
parent 36899 bcd6fce5bf06
child 40516 516a367eb38c
permissions -rw-r--r--
renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
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@36899
    24
boehmes@36899
    25
(* SMT-LIB builtins *)
boehmes@36899
    26
boehmes@36899
    27
local
boehmes@36899
    28
  fun smtlib_builtin_typ @{typ real} = SOME "Real"
boehmes@36899
    29
    | smtlib_builtin_typ _ = NONE
boehmes@36899
    30
boehmes@36899
    31
  fun smtlib_builtin_num @{typ real} i = SOME (string_of_int i ^ ".0")
boehmes@36899
    32
    | smtlib_builtin_num _ _ = NONE
boehmes@36899
    33
boehmes@36899
    34
  fun smtlib_builtin_func @{const_name uminus} ts = SOME ("~", ts)
boehmes@36899
    35
    | smtlib_builtin_func @{const_name plus} ts = SOME ("+", ts)
boehmes@36899
    36
    | smtlib_builtin_func @{const_name minus} ts = SOME ("-", ts)
boehmes@36899
    37
    | smtlib_builtin_func @{const_name times} ts = SOME ("*", ts)
boehmes@36899
    38
    | smtlib_builtin_func _ _ = NONE
boehmes@36899
    39
boehmes@36899
    40
  fun smtlib_builtin_pred @{const_name less} = SOME "<"
boehmes@36899
    41
    | smtlib_builtin_pred @{const_name less_eq} = SOME "<="
boehmes@36899
    42
    | smtlib_builtin_pred _ = NONE
boehmes@36899
    43
boehmes@36899
    44
  fun real_fun T y f x = 
boehmes@36899
    45
    (case try Term.domain_type T of
boehmes@36899
    46
      SOME @{typ real} => f x
boehmes@36899
    47
    | _ => y)
boehmes@36899
    48
in
boehmes@36899
    49
boehmes@36899
    50
val smtlib_builtins = {
boehmes@36899
    51
  builtin_typ = smtlib_builtin_typ,
boehmes@36899
    52
  builtin_num = smtlib_builtin_num,
boehmes@36899
    53
  builtin_func = (fn (n, T) => real_fun T NONE (smtlib_builtin_func n)),
boehmes@36899
    54
  builtin_pred = (fn (n, T) => fn ts =>
boehmes@36899
    55
    real_fun T NONE smtlib_builtin_pred n |> Option.map (rpair ts)),
boehmes@36899
    56
  is_builtin_pred = (fn n => fn T =>
boehmes@36899
    57
    real_fun T false (is_some o smtlib_builtin_pred) n) }
boehmes@36899
    58
boehmes@36899
    59
end
boehmes@36899
    60
boehmes@36899
    61
boehmes@36899
    62
boehmes@36899
    63
(* Z3 builtins *)
boehmes@36899
    64
boehmes@36899
    65
local
boehmes@36899
    66
  fun z3_builtin_fun @{term "op / :: real => _"} ts = SOME ("/", ts)
boehmes@36899
    67
    | z3_builtin_fun _ _ = NONE
boehmes@36899
    68
in
boehmes@36899
    69
boehmes@36899
    70
val z3_builtins = (fn c => fn ts => z3_builtin_fun (Const c) ts)
boehmes@36899
    71
boehmes@36899
    72
end
boehmes@36899
    73
boehmes@36899
    74
boehmes@36899
    75
boehmes@36899
    76
(* Z3 constructors *)
boehmes@36899
    77
boehmes@36899
    78
local
boehmes@36899
    79
  structure I = Z3_Interface
boehmes@36899
    80
boehmes@36899
    81
  fun z3_mk_builtin_typ (I.Sym ("real", _)) = SOME @{typ real}
boehmes@36899
    82
    | z3_mk_builtin_typ _ = NONE
boehmes@36899
    83
boehmes@36899
    84
  fun z3_mk_builtin_num _ i T =
boehmes@36899
    85
    if T = @{typ real} then SOME (Numeral.mk_cnumber @{ctyp real} i)
boehmes@36899
    86
    else NONE
boehmes@36899
    87
boehmes@36899
    88
  val mk_uminus = Thm.capply @{cterm "uminus :: real => _"}
boehmes@36899
    89
  val mk_add = Thm.mk_binop @{cterm "op + :: real => _"}
boehmes@36899
    90
  val mk_sub = Thm.mk_binop @{cterm "op - :: real => _"}
boehmes@36899
    91
  val mk_mul = Thm.mk_binop @{cterm "op * :: real => _"}
boehmes@36899
    92
  val mk_div = Thm.mk_binop @{cterm "op / :: real => _"}
boehmes@36899
    93
  val mk_lt = Thm.mk_binop @{cterm "op < :: real => _"}
boehmes@36899
    94
  val mk_le = Thm.mk_binop @{cterm "op <= :: real => _"}
boehmes@36899
    95
boehmes@36899
    96
  fun z3_mk_builtin_fun (I.Sym ("-", _)) [ct] = SOME (mk_uminus ct)
boehmes@36899
    97
    | z3_mk_builtin_fun (I.Sym ("+", _)) [ct, cu] = SOME (mk_add ct cu)
boehmes@36899
    98
    | z3_mk_builtin_fun (I.Sym ("-", _)) [ct, cu] = SOME (mk_sub ct cu)
boehmes@36899
    99
    | z3_mk_builtin_fun (I.Sym ("*", _)) [ct, cu] = SOME (mk_mul ct cu)
boehmes@36899
   100
    | z3_mk_builtin_fun (I.Sym ("/", _)) [ct, cu] = SOME (mk_div ct cu)
boehmes@36899
   101
    | z3_mk_builtin_fun (I.Sym ("<", _)) [ct, cu] = SOME (mk_lt ct cu)
boehmes@36899
   102
    | z3_mk_builtin_fun (I.Sym ("<=", _)) [ct, cu] = SOME (mk_le ct cu)
boehmes@36899
   103
    | z3_mk_builtin_fun (I.Sym (">", _)) [ct, cu] = SOME (mk_lt cu ct)
boehmes@36899
   104
    | z3_mk_builtin_fun (I.Sym (">=", _)) [ct, cu] = SOME (mk_le cu ct)
boehmes@36899
   105
    | z3_mk_builtin_fun _ _ = NONE
boehmes@36899
   106
in
boehmes@36899
   107
boehmes@36899
   108
val z3_mk_builtins = {
boehmes@36899
   109
  mk_builtin_typ = z3_mk_builtin_typ,
boehmes@36899
   110
  mk_builtin_num = z3_mk_builtin_num,
boehmes@36899
   111
  mk_builtin_fun = (fn _ => fn sym => fn cts =>
boehmes@36899
   112
    (case try (#T o Thm.rep_cterm o hd) cts of
boehmes@36899
   113
      SOME @{typ real} => z3_mk_builtin_fun sym cts
boehmes@36899
   114
    | _ => NONE)) }
boehmes@36899
   115
boehmes@36899
   116
end
boehmes@36899
   117
boehmes@36899
   118
boehmes@36899
   119
boehmes@36899
   120
(* Z3 proof reconstruction *)
boehmes@36899
   121
boehmes@36899
   122
val real_rules = @{lemma
boehmes@36899
   123
  "0 + (x::real) = x"
boehmes@36899
   124
  "x + 0 = x"
boehmes@36899
   125
  "0 * x = 0"
boehmes@36899
   126
  "1 * x = x"
boehmes@36899
   127
  "x + y = y + x"
boehmes@36899
   128
  by auto}
boehmes@36899
   129
wenzelm@38715
   130
val real_linarith_proc = Simplifier.simproc_global @{theory} "fast_real_arith" [
boehmes@36899
   131
  "(m::real) < n", "(m::real) <= n", "(m::real) = n"] (K Lin_Arith.simproc)
boehmes@36899
   132
boehmes@36899
   133
boehmes@36899
   134
boehmes@36899
   135
(* setup *)
boehmes@36899
   136
boehmes@36899
   137
val setup =
boehmes@36899
   138
  Context.theory_map (
boehmes@36899
   139
    SMTLIB_Interface.add_logic smtlib_logic #>
boehmes@36899
   140
    SMTLIB_Interface.add_builtins smtlib_builtins #>
boehmes@36899
   141
    Z3_Interface.add_builtin_funs z3_builtins #>
boehmes@36899
   142
    Z3_Interface.add_mk_builtins z3_mk_builtins #>
boehmes@36899
   143
    fold Z3_Proof_Reconstruction.add_z3_rule real_rules #>
boehmes@36899
   144
    Z3_Proof_Tools.add_simproc real_linarith_proc)
boehmes@36899
   145
boehmes@36899
   146
end