src/HOL/Tools/SMT/smt_normalize.ML
author boehmes
Tue, 07 Dec 2010 14:53:12 +0100
changeset 41059 d2b1fc1b8e19
parent 40686 4725ed462387
child 41072 9f9bc1bdacef
permissions -rw-r--r--
centralized handling of built-in types and constants; also store types and constants which are rewritten during preprocessing; interfaces are identified by classes (supporting inheritance, at least on the level of built-in symbols); removed term_eq in favor of type replacements: term-level occurrences of type bool are replaced by type term_bool (only for the translation)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     1
(*  Title:      HOL/Tools/SMT/smt_normalize.ML
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     3
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     4
Normalization steps on theorems required by SMT solvers:
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     5
  * simplify trivial distincts (those with less than three elements),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     6
  * rewrite bool case expressions as if expressions,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     7
  * normalize numerals (e.g. replace negative numerals by negated positive
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     8
    numerals),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     9
  * embed natural numbers into integers,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    10
  * add extra rules specifying types and constants which occur frequently,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    11
  * fully translate into object logic, add universal closure,
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
    12
  * monomorphize (create instances of schematic rules),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    13
  * lift lambda terms,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    14
  * make applications explicit for functions with varying number of arguments.
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
    15
  * add (hypothetical definitions for) missing datatype selectors,
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    16
*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    17
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    18
signature SMT_NORMALIZE =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    19
sig
40162
7f58a9a843c2 joined setup of SMT solvers in one place; turned Z3-specific options into SMT options (renamed configuration options from z3_* to smt_*); more detailed SMT exception; improved SMT filter interface
boehmes
parents: 40161
diff changeset
    20
  type extra_norm = bool -> (int * thm) list -> Proof.context ->
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
    21
    (int * thm) list * Proof.context
40424
7550b2cba1cb better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
boehmes
parents: 40279
diff changeset
    22
  val normalize: extra_norm -> bool -> (int * thm) list -> Proof.context ->
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
    23
    (int * thm) list * Proof.context
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    24
  val atomize_conv: Proof.context -> conv
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    25
  val eta_expand_conv: (Proof.context -> conv) -> Proof.context -> conv
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
    26
  val setup: theory -> theory
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    27
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    28
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    29
structure SMT_Normalize: SMT_NORMALIZE =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    30
struct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    31
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40579
diff changeset
    32
structure U = SMT_Utils
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
    33
structure B = SMT_Builtin
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40579
diff changeset
    34
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    35
infix 2 ??
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    36
fun (test ?? f) x = if test x then f x else x
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    37
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    38
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    39
40685
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    40
(* instantiate elimination rules *)
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    41
 
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    42
local
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    43
  val (cpfalse, cfalse) = `U.mk_cprop (Thm.cterm_of @{theory} @{const False})
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    44
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    45
  fun inst f ct thm =
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    46
    let val cv = f (Drule.strip_imp_concl (Thm.cprop_of thm))
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    47
    in Thm.instantiate ([], [(cv, ct)]) thm end
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    48
in
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    49
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    50
fun instantiate_elim thm =
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    51
  (case Thm.concl_of thm of
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    52
    @{const Trueprop} $ Var (_, @{typ bool}) => inst Thm.dest_arg cfalse thm
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    53
  | Var _ => inst I cpfalse thm
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    54
  | _ => thm)
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    55
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    56
end
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    57
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    58
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
    59
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    60
(* simplification of trivial distincts (distinct should have at least
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    61
   three elements in the argument list) *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    62
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    63
local
40681
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    64
  fun is_trivial_distinct (Const (@{const_name distinct}, _) $ t) =
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    65
        (case try HOLogic.dest_list t of
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    66
          SOME [] => true
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    67
        | SOME [_] => true
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    68
        | _ => false)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    69
    | is_trivial_distinct _ = false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    70
37786
4eb98849c5c0 fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents: 37153
diff changeset
    71
  val thms = map mk_meta_eq @{lemma
40681
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    72
    "distinct [] = True"
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    73
    "distinct [x] = True"
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    74
    "distinct [x, y] = (x ~= y)"
872b08416fb4 be more precise: only treat constant 'distinct' applied to an explicit list as built-in
boehmes
parents: 40663
diff changeset
    75
    by simp_all}
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    76
  fun distinct_conv _ =
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40579
diff changeset
    77
    U.if_true_conv is_trivial_distinct (Conv.rewrs_conv thms)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    78
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    79
fun trivial_distinct ctxt =
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
    80
  map (apsnd ((Term.exists_subterm is_trivial_distinct o Thm.prop_of) ??
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
    81
    Conv.fconv_rule (Conv.top_conv distinct_conv ctxt)))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    82
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    83
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    84
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    85
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    86
(* rewrite bool case expressions as if expressions *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    87
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    88
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    89
  val is_bool_case = (fn
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    90
      Const (@{const_name "bool.bool_case"}, _) $ _ $ _ $ _ => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    91
    | _ => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    92
40275
boehmes
parents: 40274
diff changeset
    93
  val thm = mk_meta_eq @{lemma
boehmes
parents: 40274
diff changeset
    94
    "(case P of True => x | False => y) = (if P then x else y)" by simp}
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40579
diff changeset
    95
  val unfold_conv = U.if_true_conv is_bool_case (Conv.rewr_conv thm)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    96
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    97
fun rewrite_bool_cases ctxt =
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
    98
  map (apsnd ((Term.exists_subterm is_bool_case o Thm.prop_of) ??
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
    99
    Conv.fconv_rule (Conv.top_conv (K unfold_conv) ctxt)))
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   100
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   101
val setup_bool_case = B.add_builtin_fun_ext'' @{const_name "bool.bool_case"}
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   102
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   103
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   104
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   105
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   106
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   107
(* normalization of numerals: rewriting of negative integer numerals into
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   108
   positive numerals, Numeral0 into 0, Numeral1 into 1 *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   109
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   110
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   111
  fun is_number_sort ctxt T =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   112
    Sign.of_sort (ProofContext.theory_of ctxt) (T, @{sort number_ring})
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   113
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   114
  fun is_strange_number ctxt (t as Const (@{const_name number_of}, _) $ _) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   115
        (case try HOLogic.dest_number t of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   116
          SOME (T, i) => is_number_sort ctxt T andalso i < 2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   117
        | NONE => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   118
    | is_strange_number _ _ = false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   119
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   120
  val pos_numeral_ss = HOL_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   121
    addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   122
    addsimps [@{thm Int.number_of_Pls}, @{thm Int.numeral_1_eq_1}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   123
    addsimps @{thms Int.pred_bin_simps}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   124
    addsimps @{thms Int.normalize_bin_simps}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   125
    addsimps @{lemma
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   126
      "Int.Min = - Int.Bit1 Int.Pls"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   127
      "Int.Bit0 (- Int.Pls) = - Int.Pls"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   128
      "Int.Bit0 (- k) = - Int.Bit0 k"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   129
      "Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   130
      by simp_all (simp add: pred_def)}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   131
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40579
diff changeset
   132
  fun pos_conv ctxt = U.if_conv (is_strange_number ctxt)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   133
    (Simplifier.rewrite (Simplifier.context ctxt pos_numeral_ss))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   134
    Conv.no_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   135
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   136
fun normalize_numerals ctxt =
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   137
  map (apsnd ((Term.exists_subterm (is_strange_number ctxt) o Thm.prop_of) ??
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   138
    Conv.fconv_rule (Conv.top_sweep_conv pos_conv ctxt)))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   139
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   140
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   141
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   142
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   143
(* embedding of standard natural number operations into integer operations *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   144
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   145
local
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   146
  val nat_embedding = map (pair ~1) @{lemma
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   147
    "nat (int n) = n"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   148
    "i >= 0 --> int (nat i) = i"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   149
    "i < 0 --> int (nat i) = 0"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   150
    by simp_all}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   151
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   152
  val nat_rewriting = @{lemma
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   153
    "0 = nat 0"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   154
    "1 = nat 1"
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   155
    "(number_of :: int => nat) = (%i. nat (number_of i))"
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   156
    "int (nat 0) = 0"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   157
    "int (nat 1) = 1"
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   158
    "op < = (%a b. int a < int b)"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   159
    "op <= = (%a b. int a <= int b)"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   160
    "Suc = (%a. nat (int a + 1))"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   161
    "op + = (%a b. nat (int a + int b))"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   162
    "op - = (%a b. nat (int a - int b))"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   163
    "op * = (%a b. nat (int a * int b))"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   164
    "op div = (%a b. nat (int a div int b))"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   165
    "op mod = (%a b. nat (int a mod int b))"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   166
    "min = (%a b. nat (min (int a) (int b)))"
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   167
    "max = (%a b. nat (max (int a) (int b)))"
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   168
    "int (nat (int a + int b)) = int a + int b"
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   169
    "int (nat (int a + 1)) = int a + 1"  (* special rule due to Suc above *)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   170
    "int (nat (int a * int b)) = int a * int b"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   171
    "int (nat (int a div int b)) = int a div int b"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   172
    "int (nat (int a mod int b)) = int a mod int b"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   173
    "int (nat (min (int a) (int b))) = min (int a) (int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   174
    "int (nat (max (int a) (int b))) = max (int a) (int b)"
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   175
    by (auto intro!: ext simp add: nat_mult_distrib nat_div_distrib
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   176
      nat_mod_distrib int_mult[symmetric] zdiv_int[symmetric]
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   177
      zmod_int[symmetric])}
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   178
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   179
  fun on_positive num f x = 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   180
    (case try HOLogic.dest_number (Thm.term_of num) of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   181
      SOME (_, i) => if i >= 0 then SOME (f x) else NONE
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   182
    | NONE => NONE)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   183
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   184
  val cancel_int_nat_ss = HOL_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   185
    addsimps [@{thm Nat_Numeral.nat_number_of}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   186
    addsimps [@{thm Nat_Numeral.int_nat_number_of}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   187
    addsimps @{thms neg_simps}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   188
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40424
diff changeset
   189
  val int_eq = Thm.cterm_of @{theory} @{const "==" (int)}
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40424
diff changeset
   190
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   191
  fun cancel_int_nat_simproc _ ss ct = 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   192
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   193
      val num = Thm.dest_arg (Thm.dest_arg ct)
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40424
diff changeset
   194
      val goal = Thm.mk_binop int_eq ct num
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   195
      val simpset = Simplifier.inherit_context ss cancel_int_nat_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   196
      fun tac _ = Simplifier.simp_tac simpset 1
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   197
    in on_positive num (Goal.prove_internal [] goal) tac end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   198
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   199
  val nat_ss = HOL_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   200
    addsimps nat_rewriting
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   201
    addsimprocs [
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   202
      Simplifier.make_simproc {
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   203
        name = "cancel_int_nat_num", lhss = [@{cpat "int (nat _)"}],
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   204
        proc = cancel_int_nat_simproc, identifier = [] }]
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   205
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   206
  fun conv ctxt = Simplifier.rewrite (Simplifier.context ctxt nat_ss)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   207
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   208
  val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat}))
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40424
diff changeset
   209
  val uses_nat_int = Term.exists_subterm (member (op aconv)
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40424
diff changeset
   210
    [@{const of_nat (int)}, @{const nat}])
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   211
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   212
  val nat_ops = [
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   213
    @{const less (nat)}, @{const less_eq (nat)},
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   214
    @{const Suc}, @{const plus (nat)}, @{const minus (nat)},
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   215
    @{const times (nat)}, @{const div (nat)}, @{const mod (nat)}]
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   216
  val nat_ops' = @{const of_nat (int)} :: @{const nat} :: nat_ops
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   217
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   218
fun nat_as_int ctxt =
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   219
  map (apsnd ((uses_nat_type o Thm.prop_of) ?? Conv.fconv_rule (conv ctxt))) #>
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   220
  exists (uses_nat_int o Thm.prop_of o snd) ?? append nat_embedding
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   221
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   222
val setup_nat_as_int =
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   223
  B.add_builtin_typ_ext (@{typ nat}, K true) #>
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   224
  fold (B.add_builtin_fun_ext' o Term.dest_Const) nat_ops'
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   225
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   226
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   227
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   228
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   229
(* further normalizations: beta/eta, universal closure, atomize *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   230
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   231
val eta_expand_eq = @{lemma "f == (%x. f x)" by (rule reflexive)}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   232
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   233
fun eta_expand_conv cv ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   234
  Conv.rewr_conv eta_expand_eq then_conv Conv.abs_conv (cv o snd) ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   235
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   236
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   237
  val eta_conv = eta_expand_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   238
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   239
  fun args_conv cv ct =
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   240
    (case Thm.term_of ct of
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   241
      _ $ _ => Conv.combination_conv (args_conv cv) cv
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   242
    | _ => Conv.all_conv) ct
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   243
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   244
  fun eta_args_conv cv 0 = args_conv o cv
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   245
    | eta_args_conv cv i = eta_conv (eta_args_conv cv (i-1))
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   246
36936
c52d1c130898 incorporated further conversions and conversionals, after some minor tuning;
wenzelm
parents: 36899
diff changeset
   247
  fun keep_conv ctxt = Conv.binder_conv (norm_conv o snd) ctxt
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   248
  and eta_binder_conv ctxt = Conv.arg_conv (eta_conv norm_conv ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   249
  and keep_let_conv ctxt = Conv.combination_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   250
    (Conv.arg_conv (norm_conv ctxt)) (Conv.abs_conv (norm_conv o snd) ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   251
  and unfold_let_conv ctxt = Conv.combination_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   252
    (Conv.arg_conv (norm_conv ctxt)) (eta_conv norm_conv ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   253
  and unfold_conv thm ctxt = Conv.rewr_conv thm then_conv keep_conv ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   254
  and unfold_ex1_conv ctxt = unfold_conv @{thm Ex1_def} ctxt
37786
4eb98849c5c0 fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents: 37153
diff changeset
   255
  and unfold_ball_conv ctxt = unfold_conv (mk_meta_eq @{thm Ball_def}) ctxt
4eb98849c5c0 fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents: 37153
diff changeset
   256
  and unfold_bex_conv ctxt = unfold_conv (mk_meta_eq @{thm Bex_def}) ctxt
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   257
  and norm_conv ctxt ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   258
    (case Thm.term_of ct of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   259
      Const (@{const_name All}, _) $ Abs _ => keep_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   260
    | Const (@{const_name All}, _) $ _ => eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   261
    | Const (@{const_name All}, _) => eta_conv eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   262
    | Const (@{const_name Ex}, _) $ Abs _ => keep_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   263
    | Const (@{const_name Ex}, _) $ _ => eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   264
    | Const (@{const_name Ex}, _) => eta_conv eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   265
    | Const (@{const_name Let}, _) $ _ $ Abs _ => keep_let_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   266
    | Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   267
    | Const (@{const_name Let}, _) $ _ => eta_conv unfold_let_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   268
    | Const (@{const_name Let}, _) => eta_conv (eta_conv unfold_let_conv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   269
    | Const (@{const_name Ex1}, _) $ _ => unfold_ex1_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   270
    | Const (@{const_name Ex1}, _) => eta_conv unfold_ex1_conv 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   271
    | Const (@{const_name Ball}, _) $ _ $ _ => unfold_ball_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   272
    | Const (@{const_name Ball}, _) $ _ => eta_conv unfold_ball_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   273
    | Const (@{const_name Ball}, _) => eta_conv (eta_conv unfold_ball_conv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   274
    | Const (@{const_name Bex}, _) $ _ $ _ => unfold_bex_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   275
    | Const (@{const_name Bex}, _) $ _ => eta_conv unfold_bex_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   276
    | Const (@{const_name Bex}, _) => eta_conv (eta_conv unfold_bex_conv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   277
    | Abs _ => Conv.abs_conv (norm_conv o snd)
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   278
    | _ =>
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   279
        (case Term.strip_comb (Thm.term_of ct) of
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   280
          (Const (c as (_, T)), ts) =>
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   281
            if SMT_Builtin.is_builtin_fun ctxt c ts
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   282
            then eta_args_conv norm_conv
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   283
              (length (Term.binder_types T) - length ts)
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   284
            else args_conv o norm_conv
40424
7550b2cba1cb better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
boehmes
parents: 40279
diff changeset
   285
        | _ => args_conv o norm_conv)) ctxt ct
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   286
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   287
  fun is_normed ctxt t =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   288
    (case t of
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   289
      Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed ctxt u
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   290
    | Const (@{const_name All}, _) $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   291
    | Const (@{const_name All}, _) => false
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   292
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed ctxt u
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   293
    | Const (@{const_name Ex}, _) $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   294
    | Const (@{const_name Ex}, _) => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   295
    | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   296
        is_normed ctxt u1 andalso is_normed ctxt u2
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   297
    | Const (@{const_name Let}, _) $ _ $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   298
    | Const (@{const_name Let}, _) $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   299
    | Const (@{const_name Let}, _) => false
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   300
    | Const (@{const_name Ex1}, _) $ _ => false
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   301
    | Const (@{const_name Ex1}, _) => false
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   302
    | Const (@{const_name Ball}, _) $ _ $ _ => false
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   303
    | Const (@{const_name Ball}, _) $ _ => false
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   304
    | Const (@{const_name Ball}, _) => false
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   305
    | Const (@{const_name Bex}, _) $ _ $ _ => false
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   306
    | Const (@{const_name Bex}, _) $ _ => false
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   307
    | Const (@{const_name Bex}, _) => false
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   308
    | Abs (_, _, u) => is_normed ctxt u
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   309
    | _ =>
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   310
        (case Term.strip_comb t of
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   311
          (Const (c as (_, T)), ts) =>
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   312
            if SMT_Builtin.is_builtin_fun ctxt c ts
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   313
            then length (Term.binder_types T) = length ts andalso
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   314
              forall (is_normed ctxt) ts
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   315
            else forall (is_normed ctxt) ts
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   316
        | (_, ts) => forall (is_normed ctxt) ts))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   317
in
40279
96365b4ae7b6 eta-expand built-in constants; also rewrite partially applied natural number terms
boehmes
parents: 40278
diff changeset
   318
fun norm_binder_conv ctxt =
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40579
diff changeset
   319
  U.if_conv (is_normed ctxt) Conv.all_conv (norm_conv ctxt)
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   320
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   321
val setup_unfolded_quants =
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   322
  fold B.add_builtin_fun_ext'' [@{const_name Ball}, @{const_name Bex},
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   323
    @{const_name Ex1}]
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   324
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   325
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   326
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   327
fun norm_def ctxt thm =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   328
  (case Thm.prop_of thm of
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40424
diff changeset
   329
    @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) =>
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   330
      norm_def ctxt (thm RS @{thm fun_cong})
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   331
  | Const (@{const_name "=="}, _) $ _ $ Abs _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   332
      norm_def ctxt (thm RS @{thm meta_eq_to_obj_eq})
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   333
  | _ => thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   334
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   335
fun atomize_conv ctxt ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   336
  (case Thm.term_of ct of
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40424
diff changeset
   337
    @{const "==>"} $ _ $ _ =>
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   338
      Conv.binop_conv (atomize_conv ctxt) then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   339
      Conv.rewr_conv @{thm atomize_imp}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   340
  | Const (@{const_name "=="}, _) $ _ $ _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   341
      Conv.binop_conv (atomize_conv ctxt) then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   342
      Conv.rewr_conv @{thm atomize_eq}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   343
  | Const (@{const_name all}, _) $ Abs _ =>
36936
c52d1c130898 incorporated further conversions and conversionals, after some minor tuning;
wenzelm
parents: 36899
diff changeset
   344
      Conv.binder_conv (atomize_conv o snd) ctxt then_conv
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   345
      Conv.rewr_conv @{thm atomize_all}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   346
  | _ => Conv.all_conv) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   347
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   348
val setup_atomize =
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   349
  fold B.add_builtin_fun_ext'' [@{const_name "==>"}, @{const_name "=="},
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   350
    @{const_name all}, @{const_name Trueprop}]
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   351
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   352
fun normalize_rule ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   353
  Conv.fconv_rule (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   354
    (* reduce lambda abstractions, except at known binders: *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   355
    Thm.beta_conversion true then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   356
    Thm.eta_conversion then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   357
    norm_binder_conv ctxt) #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   358
  norm_def ctxt #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   359
  Drule.forall_intr_vars #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   360
  Conv.fconv_rule (atomize_conv ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   361
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   362
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   363
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   364
(* lift lambda terms into additional rules *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   365
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   366
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   367
  fun used_vars cvs ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   368
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   369
      val lookup = AList.lookup (op aconv) (map (` Thm.term_of) cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   370
      val add = (fn SOME ct => insert (op aconvc) ct | _ => I)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   371
    in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   372
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   373
  fun apply cv thm = 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   374
    let val thm' = Thm.combination thm (Thm.reflexive cv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   375
    in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   376
  fun apply_def cvs eq = Thm.symmetric (fold apply cvs eq)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   377
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   378
  fun replace_lambda cvs ct (cx as (ctxt, defs)) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   379
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   380
      val cvs' = used_vars cvs ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   381
      val ct' = fold_rev Thm.cabs cvs' ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   382
    in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   383
      (case Termtab.lookup defs (Thm.term_of ct') of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   384
        SOME eq => (apply_def cvs' eq, cx)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   385
      | NONE =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   386
          let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   387
            val {T, ...} = Thm.rep_cterm ct' and n = Name.uu
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   388
            val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40579
diff changeset
   389
            val cu = U.mk_cequals (U.certify ctxt (Free (n', T))) ct'
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   390
            val (eq, ctxt'') = yield_singleton Assumption.add_assumes cu ctxt'
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   391
            val defs' = Termtab.update (Thm.term_of ct', eq) defs
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   392
          in (apply_def cvs' eq, (ctxt'', defs')) end)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   393
    end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   394
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   395
  fun none ct cx = (Thm.reflexive ct, cx)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   396
  fun in_comb f g ct cx =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   397
    let val (cu1, cu2) = Thm.dest_comb ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   398
    in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   399
  fun in_arg f = in_comb none f
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   400
  fun in_abs f cvs ct (ctxt, defs) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   401
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   402
      val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   403
      val (cv, cu) = Thm.dest_abs (SOME n) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   404
    in  (ctxt', defs) |> f (cv :: cvs) cu |>> Thm.abstract_rule n cv end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   405
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   406
  fun traverse cvs ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   407
    (case Thm.term_of ct of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   408
      Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   409
    | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   410
    | Const (@{const_name Let}, _) $ _ $ Abs _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   411
        in_comb (in_arg (traverse cvs)) (in_abs traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   412
    | Abs _ => at_lambda cvs
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   413
    | _ $ _ => in_comb (traverse cvs) (traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   414
    | _ => none) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   415
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   416
  and at_lambda cvs ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   417
    in_abs traverse cvs ct #-> (fn thm =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   418
    replace_lambda cvs (Thm.rhs_of thm) #>> Thm.transitive thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   419
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   420
  fun has_free_lambdas t =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   421
    (case t of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   422
      Const (@{const_name All}, _) $ Abs (_, _, u) => has_free_lambdas u
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   423
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => has_free_lambdas u
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   424
    | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   425
        has_free_lambdas u1 orelse has_free_lambdas u2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   426
    | Abs _ => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   427
    | u1 $ u2 => has_free_lambdas u1 orelse has_free_lambdas u2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   428
    | _ => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   429
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   430
  fun lift_lm f thm cx =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   431
    if not (has_free_lambdas (Thm.prop_of thm)) then (thm, cx)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   432
    else cx |> f (Thm.cprop_of thm) |>> (fn thm' => Thm.equal_elim thm' thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   433
in
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   434
fun lift_lambdas irules ctxt =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   435
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   436
    val cx = (ctxt, Termtab.empty)
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   437
    val (idxs, thms) = split_list irules
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   438
    val (thms', (ctxt', defs)) = fold_map (lift_lm (traverse [])) thms cx
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   439
    val eqs = Termtab.fold (cons o normalize_rule ctxt' o snd) defs []
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   440
  in (map (pair ~1) eqs @ (idxs ~~ thms'), ctxt') end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   441
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   442
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   443
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   444
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   445
(* make application explicit for functions with varying number of arguments *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   446
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   447
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   448
  val const = prefix "c" and free = prefix "f"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   449
  fun min i (e as (_, j)) = if i <> j then (true, Int.min (i, j)) else e
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   450
  fun add t i = Symtab.map_default (t, (false, i)) (min i)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   451
  fun traverse t =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   452
    (case Term.strip_comb t of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   453
      (Const (n, _), ts) => add (const n) (length ts) #> fold traverse ts 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   454
    | (Free (n, _), ts) => add (free n) (length ts) #> fold traverse ts
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   455
    | (Abs (_, _, u), ts) => fold traverse (u :: ts)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   456
    | (_, ts) => fold traverse ts)
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   457
  fun prune tab = Symtab.fold (fn (n, (true, i)) =>
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   458
    Symtab.update (n, i) | _ => I) tab Symtab.empty
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   459
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   460
  fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   461
  fun nary_conv conv1 conv2 ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   462
    (Conv.combination_conv (nary_conv conv1 conv2) conv2 else_conv conv1) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   463
  fun abs_conv conv tb = Conv.abs_conv (fn (cv, cx) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   464
    let val n = fst (Term.dest_Free (Thm.term_of cv))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   465
    in conv (Symtab.update (free n, 0) tb) cx end)
37153
8feed34275ce renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents: 36936
diff changeset
   466
  val fun_app_rule = @{lemma "f x == fun_app f x" by (simp add: fun_app_def)}
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   467
in
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   468
fun explicit_application ctxt irules =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   469
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   470
    fun sub_conv tb ctxt ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   471
      (case Term.strip_comb (Thm.term_of ct) of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   472
        (Const (n, _), ts) => app_conv tb (const n) (length ts) ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   473
      | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   474
      | (Abs _, _) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   475
      | (_, _) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   476
    and app_conv tb n i ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   477
      (case Symtab.lookup tb n of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   478
        NONE => nary_conv Conv.all_conv (sub_conv tb ctxt)
37153
8feed34275ce renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents: 36936
diff changeset
   479
      | SOME j => fun_app_conv tb ctxt (i - j))
8feed34275ce renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents: 36936
diff changeset
   480
    and fun_app_conv tb ctxt i ct = (
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   481
      if i = 0 then nary_conv Conv.all_conv (sub_conv tb ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   482
      else
37153
8feed34275ce renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents: 36936
diff changeset
   483
        Conv.rewr_conv fun_app_rule then_conv
8feed34275ce renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents: 36936
diff changeset
   484
        binop_conv (fun_app_conv tb ctxt (i-1)) (sub_conv tb ctxt)) ct
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   485
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   486
    fun needs_exp_app tab = Term.exists_subterm (fn
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   487
        Bound _ $ _ => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   488
      | Const (n, _) => Symtab.defined tab (const n)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   489
      | Free (n, _) => Symtab.defined tab (free n)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   490
      | _ => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   491
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   492
    fun rewrite tab ctxt thm =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   493
      if not (needs_exp_app tab (Thm.prop_of thm)) then thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   494
      else Conv.fconv_rule (sub_conv tab ctxt) thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   495
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   496
    val tab = prune (fold (traverse o Thm.prop_of o snd) irules Symtab.empty)
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   497
  in map (apsnd (rewrite tab ctxt)) irules end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   498
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   499
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   500
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   501
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   502
(* add missing datatype selectors via hypothetical definitions *)
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   503
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   504
local
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   505
  val add = (fn Type (n, _) => Symtab.update (n, ()) | _ => I)
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   506
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   507
  fun collect t =
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   508
    (case Term.strip_comb t of
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   509
      (Abs (_, T, t), _) => add T #> collect t
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   510
    | (Const (_, T), ts) => collects T ts
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   511
    | (Free (_, T), ts) => collects T ts
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   512
    | _ => I)
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   513
  and collects T ts =
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   514
    let val ((Ts, Us), U) = Term.strip_type T |> apfst (chop (length ts))
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   515
    in fold add Ts #> add (Us ---> U) #> fold collect ts end
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   516
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   517
  fun add_constructors thy n =
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   518
    (case Datatype.get_info thy n of
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   519
      NONE => I
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   520
    | SOME {descr, ...} => fold (fn (_, (_, _, cs)) => fold (fn (n, ds) =>
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   521
        fold (insert (op =) o pair n) (1 upto length ds)) cs) descr)
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   522
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   523
  fun add_selector (c as (n, i)) ctxt =
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   524
    (case Datatype_Selectors.lookup_selector ctxt c of
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   525
      SOME _ => ctxt
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   526
    | NONE =>
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   527
        let
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   528
          val T = Sign.the_const_type (ProofContext.theory_of ctxt) n
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   529
          val U = Term.body_type T --> nth (Term.binder_types T) (i-1)
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   530
        in
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   531
          ctxt
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   532
          |> yield_singleton Variable.variant_fixes Name.uu
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   533
          |>> pair ((n, T), i) o rpair U
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   534
          |-> Context.proof_map o Datatype_Selectors.add_selector
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   535
        end)
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   536
in
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   537
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   538
fun datatype_selectors irules ctxt =
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   539
  let
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   540
    val ns = Symtab.keys (fold (collect o Thm.prop_of o snd) irules Symtab.empty)
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   541
    val cs = fold (add_constructors (ProofContext.theory_of ctxt)) ns []
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   542
  in (irules, fold add_selector cs ctxt) end
39483
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   543
    (* FIXME: also generate hypothetical definitions for the selectors *)
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   544
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   545
end
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   546
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   547
9f0e5684f04b add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents: 38864
diff changeset
   548
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   549
(* combined normalization *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   550
40162
7f58a9a843c2 joined setup of SMT solvers in one place; turned Z3-specific options into SMT options (renamed configuration options from z3_* to smt_*); more detailed SMT exception; improved SMT filter interface
boehmes
parents: 40161
diff changeset
   551
type extra_norm = bool -> (int * thm) list -> Proof.context ->
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   552
  (int * thm) list * Proof.context
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   553
40161
539d07b00e5f keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents: 39483
diff changeset
   554
fun with_context f irules ctxt = (f ctxt irules, ctxt)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   555
40424
7550b2cba1cb better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
boehmes
parents: 40279
diff changeset
   556
fun normalize extra_norm with_datatypes irules ctxt =
40278
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   557
  let
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   558
    fun norm f ctxt' (i, thm) =
40424
7550b2cba1cb better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
boehmes
parents: 40279
diff changeset
   559
      if Config.get ctxt' SMT_Config.drop_bad_facts then
40278
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   560
        (case try (f ctxt') thm of
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   561
          SOME thm' => SOME (i, thm')
40424
7550b2cba1cb better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
boehmes
parents: 40279
diff changeset
   562
        | NONE => (SMT_Config.verbose_msg ctxt' (prefix ("Warning: " ^
40278
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   563
            "dropping assumption: ") o Display.string_of_thm ctxt') thm; NONE))
40424
7550b2cba1cb better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
boehmes
parents: 40279
diff changeset
   564
      else SOME (i, f ctxt' thm)
40278
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   565
  in
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   566
    irules
40685
dcb27631cb45 instantiate elimination rules (reduces number of quantified variables, and makes such theorems better amenable for SMT solvers)
boehmes
parents: 40681
diff changeset
   567
    |> map (apsnd instantiate_elim)
40278
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   568
    |> trivial_distinct ctxt
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   569
    |> rewrite_bool_cases ctxt
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   570
    |> normalize_numerals ctxt
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   571
    |> nat_as_int ctxt
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   572
    |> rpair ctxt
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   573
    |-> extra_norm with_datatypes
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   574
    |-> with_context (map_filter o norm normalize_rule)
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   575
    |-> SMT_Monomorph.monomorph
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   576
    |-> lift_lambdas
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   577
    |-> with_context explicit_application
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   578
    |-> (if with_datatypes then datatype_selectors else pair)
0fc78bb54f18 optionally drop assumptions which cannot be preprocessed
boehmes
parents: 40275
diff changeset
   579
  end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   580
41059
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   581
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   582
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   583
(* setup *)
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   584
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   585
val setup =
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   586
  setup_bool_case #>
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   587
  setup_nat_as_int #>
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   588
  setup_unfolded_quants #>
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   589
  setup_atomize
d2b1fc1b8e19 centralized handling of built-in types and constants;
boehmes
parents: 40686
diff changeset
   590
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   591
end