src/HOL/Tools/SMT/smt_real.ML
author blanchet
Tue Aug 29 18:30:23 2017 +0200 (21 months ago)
changeset 66551 4df6b0ae900d
parent 62913 13252110a6fe
child 69205 8050734eee3e
permissions -rw-r--r--
towards support for HO SMT-LIB
blanchet@58061
     1
(*  Title:      HOL/Tools/SMT/smt_real.ML
blanchet@56078
     2
    Author:     Sascha Boehme, TU Muenchen
blanchet@56078
     3
blanchet@56078
     4
SMT setup for reals.
blanchet@56078
     5
*)
blanchet@56078
     6
blanchet@58061
     7
structure SMT_Real: sig end =
blanchet@56078
     8
struct
blanchet@56078
     9
blanchet@56078
    10
blanchet@56078
    11
(* SMT-LIB logic *)
blanchet@56078
    12
blanchet@56078
    13
fun smtlib_logic ts =
blanchet@56078
    14
  if exists (Term.exists_type (Term.exists_subtype (equal @{typ real}))) ts
blanchet@56078
    15
  then SOME "AUFLIRA"
blanchet@56078
    16
  else NONE
blanchet@56078
    17
blanchet@56078
    18
blanchet@56078
    19
(* SMT-LIB and Z3 built-ins *)
blanchet@56078
    20
blanchet@56078
    21
local
blanchet@56078
    22
  fun real_num _ i = SOME (string_of_int i ^ ".0")
blanchet@56078
    23
blanchet@58061
    24
  fun is_linear [t] = SMT_Util.is_number t
blanchet@58061
    25
    | is_linear [t, u] = SMT_Util.is_number t orelse SMT_Util.is_number u
blanchet@56078
    26
    | is_linear _ = false
blanchet@56078
    27
blanchet@56078
    28
  fun mk_times ts = Term.list_comb (@{const times (real)}, ts)
blanchet@56078
    29
blanchet@56078
    30
  fun times _ _ ts = if is_linear ts then SOME ("*", 2, ts, mk_times) else NONE
blanchet@56078
    31
in
blanchet@56078
    32
blanchet@56078
    33
val setup_builtins =
blanchet@58061
    34
  SMT_Builtin.add_builtin_typ SMTLIB_Interface.smtlibC
blanchet@66551
    35
    (@{typ real}, K (SOME ("Real", [])), real_num) #>
blanchet@58061
    36
  fold (SMT_Builtin.add_builtin_fun' SMTLIB_Interface.smtlibC) [
blanchet@56078
    37
    (@{const less (real)}, "<"),
blanchet@56078
    38
    (@{const less_eq (real)}, "<="),
fleury@57711
    39
    (@{const uminus (real)}, "-"),
blanchet@56078
    40
    (@{const plus (real)}, "+"),
blanchet@56078
    41
    (@{const minus (real)}, "-") ] #>
blanchet@58061
    42
  SMT_Builtin.add_builtin_fun SMTLIB_Interface.smtlibC
blanchet@56078
    43
    (Term.dest_Const @{const times (real)}, times) #>
blanchet@58061
    44
  SMT_Builtin.add_builtin_fun' Z3_Interface.smtlib_z3C
blanchet@56078
    45
    (@{const times (real)}, "*") #>
blanchet@58061
    46
  SMT_Builtin.add_builtin_fun' Z3_Interface.smtlib_z3C
blanchet@56078
    47
    (@{const divide (real)}, "/")
blanchet@56078
    48
blanchet@56078
    49
end
blanchet@56078
    50
blanchet@56078
    51
blanchet@56078
    52
(* Z3 constructors *)
blanchet@56078
    53
blanchet@56078
    54
local
blanchet@58061
    55
  fun z3_mk_builtin_typ (Z3_Interface.Sym ("Real", _)) = SOME @{typ real}
blanchet@58061
    56
    | z3_mk_builtin_typ (Z3_Interface.Sym ("real", _)) = SOME @{typ real}
blanchet@56078
    57
        (*FIXME: delete*)
blanchet@56078
    58
    | z3_mk_builtin_typ _ = NONE
blanchet@56078
    59
blanchet@56078
    60
  fun z3_mk_builtin_num _ i T =
blanchet@56078
    61
    if T = @{typ real} then SOME (Numeral.mk_cnumber @{ctyp real} i)
blanchet@56078
    62
    else NONE
blanchet@56078
    63
blanchet@56078
    64
  fun mk_nary _ cu [] = cu
blanchet@56078
    65
    | mk_nary ct _ cts = uncurry (fold_rev (Thm.mk_binop ct)) (split_last cts)
blanchet@56078
    66
wenzelm@59632
    67
  val mk_uminus = Thm.apply (Thm.cterm_of @{context} @{const uminus (real)})
wenzelm@59632
    68
  val add = Thm.cterm_of @{context} @{const plus (real)}
blanchet@56078
    69
  val real0 = Numeral.mk_cnumber @{ctyp real} 0
wenzelm@59632
    70
  val mk_sub = Thm.mk_binop (Thm.cterm_of @{context} @{const minus (real)})
wenzelm@59632
    71
  val mk_mul = Thm.mk_binop (Thm.cterm_of @{context} @{const times (real)})
wenzelm@59632
    72
  val mk_div = Thm.mk_binop (Thm.cterm_of @{context} @{const divide (real)})
wenzelm@59632
    73
  val mk_lt = Thm.mk_binop (Thm.cterm_of @{context} @{const less (real)})
wenzelm@59632
    74
  val mk_le = Thm.mk_binop (Thm.cterm_of @{context} @{const less_eq (real)})
blanchet@56078
    75
blanchet@58061
    76
  fun z3_mk_builtin_fun (Z3_Interface.Sym ("-", _)) [ct] = SOME (mk_uminus ct)
blanchet@58061
    77
    | z3_mk_builtin_fun (Z3_Interface.Sym ("+", _)) cts = SOME (mk_nary add real0 cts)
blanchet@58061
    78
    | z3_mk_builtin_fun (Z3_Interface.Sym ("-", _)) [ct, cu] = SOME (mk_sub ct cu)
blanchet@58061
    79
    | z3_mk_builtin_fun (Z3_Interface.Sym ("*", _)) [ct, cu] = SOME (mk_mul ct cu)
blanchet@58061
    80
    | z3_mk_builtin_fun (Z3_Interface.Sym ("/", _)) [ct, cu] = SOME (mk_div ct cu)
blanchet@58061
    81
    | z3_mk_builtin_fun (Z3_Interface.Sym ("<", _)) [ct, cu] = SOME (mk_lt ct cu)
blanchet@58061
    82
    | z3_mk_builtin_fun (Z3_Interface.Sym ("<=", _)) [ct, cu] = SOME (mk_le ct cu)
blanchet@58061
    83
    | z3_mk_builtin_fun (Z3_Interface.Sym (">", _)) [ct, cu] = SOME (mk_lt cu ct)
blanchet@58061
    84
    | z3_mk_builtin_fun (Z3_Interface.Sym (">=", _)) [ct, cu] = SOME (mk_le cu ct)
blanchet@56078
    85
    | z3_mk_builtin_fun _ _ = NONE
blanchet@56078
    86
in
blanchet@56078
    87
blanchet@56078
    88
val z3_mk_builtins = {
blanchet@56078
    89
  mk_builtin_typ = z3_mk_builtin_typ,
blanchet@56078
    90
  mk_builtin_num = z3_mk_builtin_num,
blanchet@56078
    91
  mk_builtin_fun = (fn _ => fn sym => fn cts =>
wenzelm@59586
    92
    (case try (Thm.typ_of_cterm o hd) cts of
blanchet@56078
    93
      SOME @{typ real} => z3_mk_builtin_fun sym cts
blanchet@56078
    94
    | _ => NONE)) }
blanchet@56078
    95
blanchet@56078
    96
end
blanchet@56078
    97
blanchet@56078
    98
blanchet@56078
    99
(* Z3 proof replay *)
blanchet@56078
   100
wenzelm@61144
   101
val real_linarith_proc =
wenzelm@61144
   102
  Simplifier.make_simproc @{context} "fast_real_arith"
wenzelm@61144
   103
   {lhss = [@{term "(m::real) < n"}, @{term "(m::real) \<le> n"}, @{term "(m::real) = n"}],
wenzelm@62913
   104
    proc = K Lin_Arith.simproc}
blanchet@56078
   105
blanchet@56078
   106
blanchet@56078
   107
(* setup *)
blanchet@56078
   108
blanchet@56078
   109
val _ = Theory.setup (Context.theory_map (
blanchet@58061
   110
  SMTLIB_Interface.add_logic (10, smtlib_logic) #>
blanchet@56078
   111
  setup_builtins #>
blanchet@58061
   112
  Z3_Interface.add_mk_builtins z3_mk_builtins #>
blanchet@58061
   113
  Z3_Replay_Util.add_simproc real_linarith_proc))
blanchet@56078
   114
blanchet@57229
   115
end;