src/HOL/Library/Old_SMT/old_smt_real.ML
author blanchet
Wed Sep 24 15:45:55 2014 +0200 (2014-09-24)
changeset 58425 246985c6b20b
parent 58058 1a0b18176548
child 58825 2065f49da190
permissions -rw-r--r--
simpler proof
blanchet@58058
     1
(*  Title:      HOL/Library/Old_SMT/old_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
blanchet@58058
     7
signature OLD_SMT_REAL =
boehmes@36899
     8
sig
boehmes@36899
     9
  val setup: theory -> theory
boehmes@36899
    10
end
boehmes@36899
    11
blanchet@58058
    12
structure Old_SMT_Real: OLD_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
blanchet@58058
    29
  fun is_linear [t] = Old_SMT_Utils.is_number t
blanchet@58058
    30
    | is_linear [t, u] = Old_SMT_Utils.is_number t orelse Old_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
blanchet@58057
    35
  fun times _ _ ts = if is_linear ts then SOME ("*", 2, ts, mk_times) else NONE
boehmes@36899
    36
in
boehmes@36899
    37
boehmes@41059
    38
val setup_builtins =
blanchet@58058
    39
  Old_SMT_Builtin.add_builtin_typ Old_SMTLIB_Interface.smtlibC
boehmes@41439
    40
    (@{typ real}, K (SOME "Real"), real_num) #>
blanchet@58058
    41
  fold (Old_SMT_Builtin.add_builtin_fun' Old_SMTLIB_Interface.smtlibC) [
boehmes@41280
    42
    (@{const less (real)}, "<"),
boehmes@41280
    43
    (@{const less_eq (real)}, "<="),
boehmes@41059
    44
    (@{const uminus (real)}, "~"),
boehmes@41059
    45
    (@{const plus (real)}, "+"),
boehmes@41280
    46
    (@{const minus (real)}, "-") ] #>
blanchet@58058
    47
  Old_SMT_Builtin.add_builtin_fun Old_SMTLIB_Interface.smtlibC
boehmes@41280
    48
    (Term.dest_Const @{const times (real)}, times) #>
blanchet@58058
    49
  Old_SMT_Builtin.add_builtin_fun' Old_Z3_Interface.smtlib_z3C
boehmes@41439
    50
    (@{const times (real)}, "*") #>
blanchet@58058
    51
  Old_SMT_Builtin.add_builtin_fun' Old_Z3_Interface.smtlib_z3C
boehmes@41439
    52
    (@{const divide (real)}, "/")
boehmes@36899
    53
boehmes@36899
    54
end
boehmes@36899
    55
boehmes@36899
    56
boehmes@36899
    57
(* Z3 constructors *)
boehmes@36899
    58
boehmes@36899
    59
local
blanchet@58058
    60
  fun z3_mk_builtin_typ (Old_Z3_Interface.Sym ("Real", _)) = SOME @{typ real}
blanchet@58058
    61
    | z3_mk_builtin_typ (Old_Z3_Interface.Sym ("real", _)) = SOME @{typ real}
boehmes@41691
    62
        (*FIXME: delete*)
boehmes@36899
    63
    | z3_mk_builtin_typ _ = NONE
boehmes@36899
    64
boehmes@36899
    65
  fun z3_mk_builtin_num _ i T =
boehmes@36899
    66
    if T = @{typ real} then SOME (Numeral.mk_cnumber @{ctyp real} i)
boehmes@36899
    67
    else NONE
boehmes@36899
    68
boehmes@47965
    69
  fun mk_nary _ cu [] = cu
boehmes@47965
    70
    | mk_nary ct _ cts = uncurry (fold_rev (Thm.mk_binop ct)) (split_last cts)
boehmes@47965
    71
wenzelm@46497
    72
  val mk_uminus = Thm.apply (Thm.cterm_of @{theory} @{const uminus (real)})
boehmes@47965
    73
  val add = Thm.cterm_of @{theory} @{const plus (real)}
boehmes@47965
    74
  val real0 = Numeral.mk_cnumber @{ctyp real} 0
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
blanchet@58058
    81
  fun z3_mk_builtin_fun (Old_Z3_Interface.Sym ("-", _)) [ct] = SOME (mk_uminus ct)
blanchet@58058
    82
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("+", _)) cts =
boehmes@47965
    83
        SOME (mk_nary add real0 cts)
blanchet@58058
    84
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("-", _)) [ct, cu] =
boehmes@41691
    85
        SOME (mk_sub ct cu)
blanchet@58058
    86
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("*", _)) [ct, cu] =
boehmes@41691
    87
        SOME (mk_mul ct cu)
blanchet@58058
    88
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("/", _)) [ct, cu] =
boehmes@41691
    89
        SOME (mk_div ct cu)
blanchet@58058
    90
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("<", _)) [ct, cu] =
boehmes@41691
    91
        SOME (mk_lt ct cu)
blanchet@58058
    92
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym ("<=", _)) [ct, cu] =
boehmes@41691
    93
        SOME (mk_le ct cu)
blanchet@58058
    94
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym (">", _)) [ct, cu] =
boehmes@41691
    95
        SOME (mk_lt cu ct)
blanchet@58058
    96
    | z3_mk_builtin_fun (Old_Z3_Interface.Sym (">=", _)) [ct, cu] =
boehmes@41691
    97
        SOME (mk_le cu ct)
boehmes@36899
    98
    | z3_mk_builtin_fun _ _ = NONE
boehmes@36899
    99
in
boehmes@36899
   100
boehmes@36899
   101
val z3_mk_builtins = {
boehmes@36899
   102
  mk_builtin_typ = z3_mk_builtin_typ,
boehmes@36899
   103
  mk_builtin_num = z3_mk_builtin_num,
boehmes@36899
   104
  mk_builtin_fun = (fn _ => fn sym => fn cts =>
boehmes@36899
   105
    (case try (#T o Thm.rep_cterm o hd) cts of
boehmes@36899
   106
      SOME @{typ real} => z3_mk_builtin_fun sym cts
boehmes@36899
   107
    | _ => NONE)) }
boehmes@36899
   108
boehmes@36899
   109
end
boehmes@36899
   110
boehmes@36899
   111
boehmes@36899
   112
(* Z3 proof reconstruction *)
boehmes@36899
   113
boehmes@36899
   114
val real_rules = @{lemma
boehmes@36899
   115
  "0 + (x::real) = x"
boehmes@36899
   116
  "x + 0 = x"
boehmes@36899
   117
  "0 * x = 0"
boehmes@36899
   118
  "1 * x = x"
boehmes@36899
   119
  "x + y = y + x"
boehmes@36899
   120
  by auto}
boehmes@36899
   121
wenzelm@38715
   122
val real_linarith_proc = Simplifier.simproc_global @{theory} "fast_real_arith" [
wenzelm@51717
   123
  "(m::real) < n", "(m::real) <= n", "(m::real) = n"] Lin_Arith.simproc
boehmes@36899
   124
boehmes@36899
   125
boehmes@36899
   126
(* setup *)
boehmes@36899
   127
boehmes@36899
   128
val setup =
boehmes@36899
   129
  Context.theory_map (
blanchet@58058
   130
    Old_SMTLIB_Interface.add_logic (10, smtlib_logic) #>
boehmes@41072
   131
    setup_builtins #>
blanchet@58058
   132
    Old_Z3_Interface.add_mk_builtins z3_mk_builtins #>
blanchet@58058
   133
    fold Old_Z3_Proof_Reconstruction.add_z3_rule real_rules #>
blanchet@58058
   134
    Old_Z3_Proof_Tools.add_simproc real_linarith_proc)
boehmes@36899
   135
boehmes@36899
   136
end