src/HOL/Tools/SMT2/smt2_normalize.ML
author blanchet
Tue Jun 03 11:43:07 2014 +0200 (2014-06-03)
changeset 57165 7b1bf424ec5f
parent 56981 3ef45ce002b5
child 57229 489083abce44
permissions -rw-r--r--
removed SMT weights -- their impact was very inconclusive anyway
blanchet@56078
     1
(*  Title:      HOL/Tools/SMT2/smt2_normalize.ML
blanchet@56078
     2
    Author:     Sascha Boehme, TU Muenchen
blanchet@56078
     3
blanchet@56078
     4
Normalization steps on theorems required by SMT solvers.
blanchet@56078
     5
*)
blanchet@56078
     6
blanchet@56078
     7
signature SMT2_NORMALIZE =
blanchet@56078
     8
sig
blanchet@56078
     9
  val drop_fact_warning: Proof.context -> thm -> unit
blanchet@56078
    10
  val atomize_conv: Proof.context -> conv
blanchet@56981
    11
blanchet@56981
    12
  val special_quant_table: (string * thm) list
blanchet@56981
    13
  val case_bool_entry: string * thm
blanchet@56981
    14
  val abs_min_max_table: (string * thm) list
blanchet@56981
    15
blanchet@56078
    16
  type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
blanchet@56090
    17
  val add_extra_norm: SMT2_Util.class * extra_norm -> Context.generic -> Context.generic
blanchet@57165
    18
  val normalize: Proof.context -> thm list -> (int * thm) list
blanchet@56078
    19
end
blanchet@56078
    20
blanchet@56078
    21
structure SMT2_Normalize: SMT2_NORMALIZE =
blanchet@56078
    22
struct
blanchet@56078
    23
blanchet@56078
    24
fun drop_fact_warning ctxt =
blanchet@56078
    25
  SMT2_Config.verbose_msg ctxt (prefix "Warning: dropping assumption: " o
blanchet@56078
    26
    Display.string_of_thm ctxt)
blanchet@56078
    27
blanchet@56078
    28
blanchet@56078
    29
(* general theorem normalizations *)
blanchet@56078
    30
blanchet@56078
    31
(** instantiate elimination rules **)
blanchet@56078
    32
 
blanchet@56078
    33
local
blanchet@56090
    34
  val (cpfalse, cfalse) = `SMT2_Util.mk_cprop (Thm.cterm_of @{theory} @{const False})
blanchet@56078
    35
blanchet@56078
    36
  fun inst f ct thm =
blanchet@56078
    37
    let val cv = f (Drule.strip_imp_concl (Thm.cprop_of thm))
blanchet@56078
    38
    in Thm.instantiate ([], [(cv, ct)]) thm end
blanchet@56078
    39
in
blanchet@56078
    40
blanchet@56078
    41
fun instantiate_elim thm =
blanchet@56078
    42
  (case Thm.concl_of thm of
blanchet@56078
    43
    @{const Trueprop} $ Var (_, @{typ bool}) => inst Thm.dest_arg cfalse thm
blanchet@56078
    44
  | Var _ => inst I cpfalse thm
blanchet@56078
    45
  | _ => thm)
blanchet@56078
    46
blanchet@56078
    47
end
blanchet@56078
    48
blanchet@56078
    49
blanchet@56078
    50
(** normalize definitions **)
blanchet@56078
    51
blanchet@56078
    52
fun norm_def thm =
blanchet@56078
    53
  (case Thm.prop_of thm of
blanchet@56078
    54
    @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) =>
blanchet@56078
    55
      norm_def (thm RS @{thm fun_cong})
wenzelm@56245
    56
  | Const (@{const_name Pure.eq}, _) $ _ $ Abs _ => norm_def (thm RS @{thm meta_eq_to_obj_eq})
blanchet@56078
    57
  | _ => thm)
blanchet@56078
    58
blanchet@56078
    59
blanchet@56078
    60
(** atomization **)
blanchet@56078
    61
blanchet@56078
    62
fun atomize_conv ctxt ct =
blanchet@56078
    63
  (case Thm.term_of ct of
wenzelm@56245
    64
    @{const Pure.imp} $ _ $ _ =>
blanchet@56103
    65
      Conv.binop_conv (atomize_conv ctxt) then_conv Conv.rewr_conv @{thm atomize_imp}
wenzelm@56245
    66
  | Const (@{const_name Pure.eq}, _) $ _ $ _ =>
blanchet@56103
    67
      Conv.binop_conv (atomize_conv ctxt) then_conv Conv.rewr_conv @{thm atomize_eq}
wenzelm@56245
    68
  | Const (@{const_name Pure.all}, _) $ Abs _ =>
blanchet@56103
    69
      Conv.binder_conv (atomize_conv o snd) ctxt then_conv Conv.rewr_conv @{thm atomize_all}
blanchet@56078
    70
  | _ => Conv.all_conv) ct
blanchet@56078
    71
blanchet@56078
    72
val setup_atomize =
wenzelm@56245
    73
  fold SMT2_Builtin.add_builtin_fun_ext'' [@{const_name Pure.imp}, @{const_name Pure.eq},
wenzelm@56245
    74
    @{const_name Pure.all}, @{const_name Trueprop}]
blanchet@56078
    75
blanchet@56078
    76
blanchet@56078
    77
(** unfold special quantifiers **)
blanchet@56078
    78
blanchet@56981
    79
val special_quant_table = [
blanchet@56981
    80
  (@{const_name Ex1}, @{thm Ex1_def_raw}),
blanchet@56981
    81
  (@{const_name Ball}, @{thm Ball_def_raw}),
blanchet@56981
    82
  (@{const_name Bex}, @{thm Bex_def_raw})]
blanchet@56981
    83
blanchet@56078
    84
local
blanchet@56981
    85
  fun special_quant (Const (n, _)) = AList.lookup (op =) special_quant_table n
blanchet@56078
    86
    | special_quant _ = NONE
blanchet@56078
    87
blanchet@56078
    88
  fun special_quant_conv _ ct =
blanchet@56078
    89
    (case special_quant (Thm.term_of ct) of
blanchet@56078
    90
      SOME thm => Conv.rewr_conv thm
blanchet@56078
    91
    | NONE => Conv.all_conv) ct
blanchet@56078
    92
in
blanchet@56078
    93
blanchet@56078
    94
fun unfold_special_quants_conv ctxt =
blanchet@56090
    95
  SMT2_Util.if_exists_conv (is_some o special_quant) (Conv.top_conv special_quant_conv ctxt)
blanchet@56078
    96
blanchet@56981
    97
val setup_unfolded_quants = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) special_quant_table
blanchet@56078
    98
blanchet@56078
    99
end
blanchet@56078
   100
blanchet@56078
   101
blanchet@56078
   102
(** trigger inference **)
blanchet@56078
   103
blanchet@56078
   104
local
blanchet@56078
   105
  (*** check trigger syntax ***)
blanchet@56078
   106
blanchet@56078
   107
  fun dest_trigger (Const (@{const_name pat}, _) $ _) = SOME true
blanchet@56078
   108
    | dest_trigger (Const (@{const_name nopat}, _) $ _) = SOME false
blanchet@56078
   109
    | dest_trigger _ = NONE
blanchet@56078
   110
blanchet@56078
   111
  fun eq_list [] = false
blanchet@56078
   112
    | eq_list (b :: bs) = forall (equal b) bs
blanchet@56078
   113
blanchet@56078
   114
  fun proper_trigger t =
blanchet@56078
   115
    t
blanchet@56078
   116
    |> these o try HOLogic.dest_list
blanchet@56078
   117
    |> map (map_filter dest_trigger o these o try HOLogic.dest_list)
blanchet@56078
   118
    |> (fn [] => false | bss => forall eq_list bss)
blanchet@56078
   119
blanchet@56078
   120
  fun proper_quant inside f t =
blanchet@56078
   121
    (case t of
blanchet@56078
   122
      Const (@{const_name All}, _) $ Abs (_, _, u) => proper_quant true f u
blanchet@56078
   123
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => proper_quant true f u
blanchet@56078
   124
    | @{const trigger} $ p $ u =>
blanchet@56078
   125
        (if inside then f p else false) andalso proper_quant false f u
blanchet@56078
   126
    | Abs (_, _, u) => proper_quant false f u
blanchet@56078
   127
    | u1 $ u2 => proper_quant false f u1 andalso proper_quant false f u2
blanchet@56078
   128
    | _ => true)
blanchet@56078
   129
blanchet@56078
   130
  fun check_trigger_error ctxt t =
blanchet@56078
   131
    error ("SMT triggers must only occur under quantifier and multipatterns " ^
blanchet@56078
   132
      "must have the same kind: " ^ Syntax.string_of_term ctxt t)
blanchet@56078
   133
blanchet@56078
   134
  fun check_trigger_conv ctxt ct =
blanchet@56090
   135
    if proper_quant false proper_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
blanchet@56078
   136
    else check_trigger_error ctxt (Thm.term_of ct)
blanchet@56078
   137
blanchet@56078
   138
blanchet@56078
   139
  (*** infer simple triggers ***)
blanchet@56078
   140
blanchet@56078
   141
  fun dest_cond_eq ct =
blanchet@56078
   142
    (case Thm.term_of ct of
blanchet@56078
   143
      Const (@{const_name HOL.eq}, _) $ _ $ _ => Thm.dest_binop ct
blanchet@56078
   144
    | @{const HOL.implies} $ _ $ _ => dest_cond_eq (Thm.dest_arg ct)
blanchet@56078
   145
    | _ => raise CTERM ("no equation", [ct]))
blanchet@56078
   146
blanchet@56078
   147
  fun get_constrs thy (Type (n, _)) = these (Datatype.get_constrs thy n)
blanchet@56078
   148
    | get_constrs _ _ = []
blanchet@56078
   149
blanchet@56078
   150
  fun is_constr thy (n, T) =
blanchet@56078
   151
    let fun match (m, U) = m = n andalso Sign.typ_instance thy (T, U)
blanchet@56078
   152
    in can (the o find_first match o get_constrs thy o Term.body_type) T end
blanchet@56078
   153
blanchet@56078
   154
  fun is_constr_pat thy t =
blanchet@56078
   155
    (case Term.strip_comb t of
blanchet@56078
   156
      (Free _, []) => true
blanchet@56078
   157
    | (Const c, ts) => is_constr thy c andalso forall (is_constr_pat thy) ts
blanchet@56078
   158
    | _ => false)
blanchet@56078
   159
blanchet@56078
   160
  fun is_simp_lhs ctxt t =
blanchet@56078
   161
    (case Term.strip_comb t of
blanchet@56078
   162
      (Const c, ts as _ :: _) =>
blanchet@56078
   163
        not (SMT2_Builtin.is_builtin_fun_ext ctxt c ts) andalso
blanchet@56078
   164
        forall (is_constr_pat (Proof_Context.theory_of ctxt)) ts
blanchet@56078
   165
    | _ => false)
blanchet@56078
   166
blanchet@56078
   167
  fun has_all_vars vs t =
blanchet@56078
   168
    subset (op aconv) (vs, map Free (Term.add_frees t []))
blanchet@56078
   169
blanchet@56078
   170
  fun minimal_pats vs ct =
blanchet@56078
   171
    if has_all_vars vs (Thm.term_of ct) then
blanchet@56078
   172
      (case Thm.term_of ct of
blanchet@56078
   173
        _ $ _ =>
blanchet@56078
   174
          (case pairself (minimal_pats vs) (Thm.dest_comb ct) of
blanchet@56078
   175
            ([], []) => [[ct]]
blanchet@56078
   176
          | (ctss, ctss') => union (eq_set (op aconvc)) ctss ctss')
blanchet@56078
   177
      | _ => [])
blanchet@56078
   178
    else []
blanchet@56078
   179
blanchet@56078
   180
  fun proper_mpat _ _ _ [] = false
blanchet@56078
   181
    | proper_mpat thy gen u cts =
blanchet@56078
   182
        let
blanchet@56078
   183
          val tps = (op ~~) (`gen (map Thm.term_of cts))
blanchet@56078
   184
          fun some_match u = tps |> exists (fn (t', t) =>
blanchet@56078
   185
            Pattern.matches thy (t', u) andalso not (t aconv u))
blanchet@56078
   186
        in not (Term.exists_subterm some_match u) end
blanchet@56078
   187
blanchet@56090
   188
  val pat = SMT2_Util.mk_const_pat @{theory} @{const_name SMT2.pat} SMT2_Util.destT1
blanchet@56090
   189
  fun mk_pat ct = Thm.apply (SMT2_Util.instT' ct pat) ct
blanchet@56078
   190
blanchet@56090
   191
  fun mk_clist T = pairself (Thm.cterm_of @{theory}) (HOLogic.cons_const T, HOLogic.nil_const T)
blanchet@56078
   192
  fun mk_list (ccons, cnil) f cts = fold_rev (Thm.mk_binop ccons o f) cts cnil
blanchet@56078
   193
  val mk_pat_list = mk_list (mk_clist @{typ SMT2.pattern})
blanchet@56078
   194
  val mk_mpat_list = mk_list (mk_clist @{typ "SMT2.pattern list"})  
blanchet@56078
   195
  fun mk_trigger ctss = mk_mpat_list (mk_pat_list mk_pat) ctss
blanchet@56078
   196
blanchet@56103
   197
  val trigger_eq = mk_meta_eq @{lemma "p = SMT2.trigger t p" by (simp add: trigger_def)}
blanchet@56078
   198
blanchet@56078
   199
  fun insert_trigger_conv [] ct = Conv.all_conv ct
blanchet@56078
   200
    | insert_trigger_conv ctss ct =
blanchet@56078
   201
        let val (ctr, cp) = Thm.dest_binop (Thm.rhs_of trigger_eq) ||> rpair ct
blanchet@56078
   202
        in Thm.instantiate ([], [cp, (ctr, mk_trigger ctss)]) trigger_eq end
blanchet@56078
   203
blanchet@56078
   204
  fun infer_trigger_eq_conv outer_ctxt (ctxt, cvs) ct =
blanchet@56078
   205
    let
blanchet@56078
   206
      val (lhs, rhs) = dest_cond_eq ct
blanchet@56078
   207
blanchet@56078
   208
      val vs = map Thm.term_of cvs
blanchet@56078
   209
      val thy = Proof_Context.theory_of ctxt
blanchet@56078
   210
blanchet@56078
   211
      fun get_mpats ct =
blanchet@56078
   212
        if is_simp_lhs ctxt (Thm.term_of ct) then minimal_pats vs ct
blanchet@56078
   213
        else []
blanchet@56078
   214
      val gen = Variable.export_terms ctxt outer_ctxt
blanchet@56078
   215
      val filter_mpats = filter (proper_mpat thy gen (Thm.term_of rhs))
blanchet@56078
   216
blanchet@56078
   217
    in insert_trigger_conv (filter_mpats (get_mpats lhs)) ct end
blanchet@56078
   218
blanchet@56078
   219
  fun has_trigger (@{const SMT2.trigger} $ _ $ _) = true
blanchet@56078
   220
    | has_trigger _ = false
blanchet@56078
   221
blanchet@56078
   222
  fun try_trigger_conv cv ct =
blanchet@56090
   223
    if SMT2_Util.under_quant has_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
blanchet@56078
   224
    else Conv.try_conv cv ct
blanchet@56078
   225
blanchet@56078
   226
  fun infer_trigger_conv ctxt =
blanchet@56078
   227
    if Config.get ctxt SMT2_Config.infer_triggers then
blanchet@56090
   228
      try_trigger_conv (SMT2_Util.under_quant_conv (infer_trigger_eq_conv ctxt) ctxt)
blanchet@56078
   229
    else Conv.all_conv
blanchet@56078
   230
in
blanchet@56078
   231
blanchet@56078
   232
fun trigger_conv ctxt =
blanchet@56090
   233
  SMT2_Util.prop_conv (check_trigger_conv ctxt then_conv infer_trigger_conv ctxt)
blanchet@56078
   234
blanchet@56078
   235
val setup_trigger =
blanchet@56078
   236
  fold SMT2_Builtin.add_builtin_fun_ext''
blanchet@56078
   237
    [@{const_name SMT2.pat}, @{const_name SMT2.nopat}, @{const_name SMT2.trigger}]
blanchet@56078
   238
blanchet@56078
   239
end
blanchet@56078
   240
blanchet@56078
   241
blanchet@56078
   242
(** combined general normalizations **)
blanchet@56078
   243
blanchet@57165
   244
fun gen_normalize1_conv ctxt =
blanchet@56078
   245
  atomize_conv ctxt then_conv
blanchet@56078
   246
  unfold_special_quants_conv ctxt then_conv
blanchet@56078
   247
  Thm.beta_conversion true then_conv
blanchet@57165
   248
  trigger_conv ctxt
blanchet@56078
   249
blanchet@57165
   250
fun gen_normalize1 ctxt =
blanchet@56107
   251
  instantiate_elim #>
blanchet@56107
   252
  norm_def #>
blanchet@56107
   253
  Conv.fconv_rule (Thm.beta_conversion true then_conv Thm.eta_conversion) #>
blanchet@56107
   254
  Drule.forall_intr_vars #>
blanchet@57165
   255
  Conv.fconv_rule (gen_normalize1_conv ctxt) #>
blanchet@56078
   256
  (* Z3 4.3.1 silently normalizes "P --> Q --> R" to "P & Q --> R" *)
blanchet@56107
   257
  Raw_Simplifier.rewrite_rule ctxt @{thms HOL.imp_conjL[symmetric, THEN eq_reflection]}
blanchet@56078
   258
blanchet@57165
   259
fun gen_norm1_safe ctxt (i, thm) =
blanchet@57165
   260
  (case try (gen_normalize1 ctxt) thm of
blanchet@56078
   261
    SOME thm' => SOME (i, thm')
blanchet@56078
   262
  | NONE => (drop_fact_warning ctxt thm; NONE))
blanchet@56078
   263
blanchet@56078
   264
fun gen_normalize ctxt iwthms = map_filter (gen_norm1_safe ctxt) iwthms
blanchet@56078
   265
blanchet@56078
   266
blanchet@56078
   267
blanchet@56078
   268
(* unfolding of definitions and theory-specific rewritings *)
blanchet@56078
   269
blanchet@56078
   270
fun expand_head_conv cv ct =
blanchet@56078
   271
  (case Thm.term_of ct of
blanchet@56078
   272
    _ $ _ =>
blanchet@56078
   273
      Conv.fun_conv (expand_head_conv cv) then_conv
blanchet@56078
   274
      Conv.try_conv (Thm.beta_conversion false)
blanchet@56078
   275
  | _ => cv) ct
blanchet@56078
   276
blanchet@56078
   277
blanchet@56078
   278
(** rewrite bool case expressions as if expressions **)
blanchet@56078
   279
blanchet@56981
   280
val case_bool_entry = (@{const_name "bool.case_bool"}, @{thm case_bool_if})
blanchet@56981
   281
blanchet@56078
   282
local
blanchet@56078
   283
  fun is_case_bool (Const (@{const_name "bool.case_bool"}, _)) = true
blanchet@56078
   284
    | is_case_bool _ = false
blanchet@56078
   285
blanchet@56078
   286
  fun unfold_conv _ =
blanchet@56103
   287
    SMT2_Util.if_true_conv (is_case_bool o Term.head_of)
blanchet@56103
   288
      (expand_head_conv (Conv.rewr_conv @{thm case_bool_if}))
blanchet@56078
   289
in
blanchet@56078
   290
blanchet@56078
   291
fun rewrite_case_bool_conv ctxt =
blanchet@56090
   292
  SMT2_Util.if_exists_conv is_case_bool (Conv.top_conv unfold_conv ctxt)
blanchet@56078
   293
blanchet@56103
   294
val setup_case_bool = SMT2_Builtin.add_builtin_fun_ext'' @{const_name "bool.case_bool"}
blanchet@56078
   295
blanchet@56078
   296
end
blanchet@56078
   297
blanchet@56078
   298
blanchet@56078
   299
(** unfold abs, min and max **)
blanchet@56078
   300
blanchet@56981
   301
val abs_min_max_table = [
blanchet@56981
   302
  (@{const_name min}, @{thm min_def_raw}),
blanchet@56981
   303
  (@{const_name max}, @{thm max_def_raw}),
blanchet@56981
   304
  (@{const_name abs}, @{thm abs_if_raw})]
blanchet@56981
   305
blanchet@56078
   306
local
blanchet@56100
   307
  fun abs_min_max ctxt (Const (n, Type (@{type_name fun}, [T, _]))) =
blanchet@56981
   308
        (case AList.lookup (op =) abs_min_max_table n of
blanchet@56078
   309
          NONE => NONE
blanchet@56100
   310
        | SOME thm => if SMT2_Builtin.is_builtin_typ_ext ctxt T then SOME thm else NONE)
blanchet@56078
   311
    | abs_min_max _ _ = NONE
blanchet@56078
   312
blanchet@56078
   313
  fun unfold_amm_conv ctxt ct =
blanchet@56078
   314
    (case abs_min_max ctxt (Term.head_of (Thm.term_of ct)) of
blanchet@56078
   315
      SOME thm => expand_head_conv (Conv.rewr_conv thm)
blanchet@56078
   316
    | NONE => Conv.all_conv) ct
blanchet@56078
   317
in
blanchet@56078
   318
blanchet@56078
   319
fun unfold_abs_min_max_conv ctxt =
blanchet@56090
   320
  SMT2_Util.if_exists_conv (is_some o abs_min_max ctxt) (Conv.top_conv unfold_amm_conv ctxt)
blanchet@56078
   321
  
blanchet@56981
   322
val setup_abs_min_max = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) abs_min_max_table
blanchet@56078
   323
blanchet@56078
   324
end
blanchet@56078
   325
blanchet@56078
   326
blanchet@56078
   327
(** embedding of standard natural number operations into integer operations **)
blanchet@56078
   328
blanchet@56078
   329
local
blanchet@56107
   330
  val nat_embedding = @{thms nat_int' int_nat_nneg int_nat_neg}
blanchet@56078
   331
blanchet@56078
   332
  val simple_nat_ops = [
blanchet@56078
   333
    @{const less (nat)}, @{const less_eq (nat)},
blanchet@56078
   334
    @{const Suc}, @{const plus (nat)}, @{const minus (nat)}]
blanchet@56078
   335
blanchet@56078
   336
  val mult_nat_ops =
blanchet@56078
   337
    [@{const times (nat)}, @{const div (nat)}, @{const mod (nat)}]
blanchet@56078
   338
blanchet@56078
   339
  val nat_ops = simple_nat_ops @ mult_nat_ops
blanchet@56078
   340
blanchet@56078
   341
  val nat_consts = nat_ops @ [@{const numeral (nat)},
blanchet@56078
   342
    @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
blanchet@56078
   343
blanchet@56078
   344
  val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
blanchet@56078
   345
blanchet@56078
   346
  val builtin_nat_ops = nat_int_coercions @ simple_nat_ops
blanchet@56078
   347
blanchet@56078
   348
  val is_nat_const = member (op aconv) nat_consts
blanchet@56078
   349
blanchet@56078
   350
  fun is_nat_const' @{const of_nat (int)} = true
blanchet@56078
   351
    | is_nat_const' t = is_nat_const t
blanchet@56078
   352
blanchet@56103
   353
  val expands = map mk_meta_eq @{thms nat_zero_as_int nat_one_as_int nat_numeral_as_int
blanchet@56103
   354
    nat_less_as_int nat_leq_as_int Suc_as_int nat_plus_as_int nat_minus_as_int nat_times_as_int
blanchet@56103
   355
    nat_div_as_int nat_mod_as_int}
blanchet@56078
   356
blanchet@56103
   357
  val ints = map mk_meta_eq @{thms int_0 int_1 int_Suc int_plus int_minus int_mult zdiv_int
blanchet@56103
   358
    zmod_int}
blanchet@56103
   359
  val int_if = mk_meta_eq @{lemma "int (if P then n else m) = (if P then int n else int m)" by simp}
blanchet@56078
   360
blanchet@56078
   361
  fun mk_number_eq ctxt i lhs =
blanchet@56078
   362
    let
blanchet@56090
   363
      val eq = SMT2_Util.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
blanchet@56078
   364
      val ctxt' = put_simpset HOL_ss ctxt addsimps @{thms Int.int_numeral}
blanchet@56078
   365
      val tac = HEADGOAL (Simplifier.simp_tac ctxt')
blanchet@56078
   366
    in Goal.norm_result ctxt (Goal.prove_internal ctxt [] eq (K tac)) end
blanchet@56078
   367
blanchet@56078
   368
  fun ite_conv cv1 cv2 =
blanchet@56078
   369
    Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
blanchet@56078
   370
blanchet@56078
   371
  fun int_conv ctxt ct =
blanchet@56078
   372
    (case Thm.term_of ct of
blanchet@56078
   373
      @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
blanchet@56078
   374
        Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
blanchet@56078
   375
    | @{const of_nat (int)} $ _ =>
blanchet@56078
   376
        (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
blanchet@56078
   377
        (Conv.rewr_conv int_if then_conv
blanchet@56078
   378
          ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
blanchet@56078
   379
        Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
blanchet@56078
   380
    | _ => Conv.no_conv) ct
blanchet@56078
   381
blanchet@56078
   382
  and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
blanchet@56078
   383
blanchet@56078
   384
  and expand_conv ctxt =
blanchet@56090
   385
    SMT2_Util.if_conv (is_nat_const o Term.head_of)
blanchet@56090
   386
      (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt) (int_conv ctxt)
blanchet@56078
   387
blanchet@56090
   388
  and nat_conv ctxt = SMT2_Util.if_exists_conv is_nat_const' (Conv.top_sweep_conv expand_conv ctxt)
blanchet@56078
   389
blanchet@56078
   390
  val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
blanchet@56078
   391
in
blanchet@56078
   392
blanchet@56078
   393
val nat_as_int_conv = nat_conv
blanchet@56078
   394
blanchet@56078
   395
fun add_nat_embedding thms =
blanchet@56100
   396
  if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding) else (thms, [])
blanchet@56078
   397
blanchet@56078
   398
val setup_nat_as_int =
blanchet@56078
   399
  SMT2_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
blanchet@56078
   400
  fold (SMT2_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
blanchet@56078
   401
blanchet@56078
   402
end
blanchet@56078
   403
blanchet@56078
   404
blanchet@56078
   405
(** normalize numerals **)
blanchet@56078
   406
blanchet@56078
   407
local
blanchet@56078
   408
  (*
blanchet@56078
   409
    rewrite Numeral1 into 1
blanchet@56078
   410
    rewrite - 0 into 0
blanchet@56078
   411
  *)
blanchet@56078
   412
blanchet@56078
   413
  fun is_irregular_number (Const (@{const_name numeral}, _) $ Const (@{const_name num.One}, _)) =
blanchet@56078
   414
        true
blanchet@56078
   415
    | is_irregular_number (Const (@{const_name uminus}, _) $ Const (@{const_name Groups.zero}, _)) =
blanchet@56078
   416
        true
blanchet@56078
   417
    | is_irregular_number _ =
blanchet@56078
   418
        false;
blanchet@56078
   419
blanchet@56078
   420
  fun is_strange_number ctxt t = is_irregular_number t andalso SMT2_Builtin.is_builtin_num ctxt t;
blanchet@56078
   421
blanchet@56078
   422
  val proper_num_ss =
blanchet@56078
   423
    simpset_of (put_simpset HOL_ss @{context}
blanchet@56078
   424
      addsimps @{thms Num.numeral_One minus_zero})
blanchet@56078
   425
blanchet@56078
   426
  fun norm_num_conv ctxt =
blanchet@56090
   427
    SMT2_Util.if_conv (is_strange_number ctxt) (Simplifier.rewrite (put_simpset proper_num_ss ctxt))
blanchet@56090
   428
      Conv.no_conv
blanchet@56078
   429
in
blanchet@56078
   430
blanchet@56078
   431
fun normalize_numerals_conv ctxt =
blanchet@56090
   432
  SMT2_Util.if_exists_conv (is_strange_number ctxt) (Conv.top_sweep_conv norm_num_conv ctxt)
blanchet@56078
   433
blanchet@56078
   434
end
blanchet@56078
   435
blanchet@56078
   436
blanchet@56078
   437
(** combined unfoldings and rewritings **)
blanchet@56078
   438
blanchet@56078
   439
fun unfold_conv ctxt =
blanchet@56078
   440
  rewrite_case_bool_conv ctxt then_conv
blanchet@56078
   441
  unfold_abs_min_max_conv ctxt then_conv
blanchet@56078
   442
  nat_as_int_conv ctxt then_conv
blanchet@56078
   443
  Thm.beta_conversion true
blanchet@56078
   444
blanchet@56078
   445
fun unfold1 ctxt = map (apsnd (Conv.fconv_rule (unfold_conv ctxt)))
blanchet@56078
   446
blanchet@56078
   447
fun burrow_ids f ithms =
blanchet@56078
   448
  let
blanchet@56078
   449
    val (is, thms) = split_list ithms
blanchet@56078
   450
    val (thms', extra_thms) = f thms
blanchet@56106
   451
  in (is ~~ thms') @ map (pair ~1) extra_thms end
blanchet@56078
   452
blanchet@56078
   453
fun unfold2 ctxt ithms =
blanchet@56078
   454
  ithms
blanchet@56078
   455
  |> map (apsnd (Conv.fconv_rule (normalize_numerals_conv ctxt)))
blanchet@56078
   456
  |> burrow_ids add_nat_embedding
blanchet@56078
   457
blanchet@56078
   458
blanchet@56078
   459
blanchet@56078
   460
(* overall normalization *)
blanchet@56078
   461
blanchet@56078
   462
type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
blanchet@56078
   463
blanchet@56078
   464
structure Extra_Norms = Generic_Data
blanchet@56078
   465
(
blanchet@56090
   466
  type T = extra_norm SMT2_Util.dict
blanchet@56078
   467
  val empty = []
blanchet@56078
   468
  val extend = I
blanchet@56090
   469
  fun merge data = SMT2_Util.dict_merge fst data
blanchet@56078
   470
)
blanchet@56078
   471
blanchet@56090
   472
fun add_extra_norm (cs, norm) = Extra_Norms.map (SMT2_Util.dict_update (cs, norm))
blanchet@56078
   473
blanchet@56078
   474
fun apply_extra_norms ctxt ithms =
blanchet@56078
   475
  let
blanchet@56078
   476
    val cs = SMT2_Config.solver_class_of ctxt
blanchet@56090
   477
    val es = SMT2_Util.dict_lookup (Extra_Norms.get (Context.Proof ctxt)) cs
blanchet@56078
   478
  in burrow_ids (fold (fn e => e ctxt) es o rpair []) ithms end
blanchet@56078
   479
blanchet@56078
   480
local
blanchet@56078
   481
  val ignored = member (op =) [@{const_name All}, @{const_name Ex},
blanchet@56078
   482
    @{const_name Let}, @{const_name If}, @{const_name HOL.eq}]
blanchet@56078
   483
blanchet@56078
   484
  val schematic_consts_of =
blanchet@56078
   485
    let
blanchet@56078
   486
      fun collect (@{const SMT2.trigger} $ p $ t) =
blanchet@56078
   487
            collect_trigger p #> collect t
blanchet@56078
   488
        | collect (t $ u) = collect t #> collect u
blanchet@56078
   489
        | collect (Abs (_, _, t)) = collect t
blanchet@56078
   490
        | collect (t as Const (n, _)) = 
blanchet@56078
   491
            if not (ignored n) then Monomorph.add_schematic_consts_of t else I
blanchet@56078
   492
        | collect _ = I
blanchet@56078
   493
      and collect_trigger t =
blanchet@56078
   494
        let val dest = these o try HOLogic.dest_list 
blanchet@56078
   495
        in fold (fold collect_pat o dest) (dest t) end
blanchet@56078
   496
      and collect_pat (Const (@{const_name SMT2.pat}, _) $ t) = collect t
blanchet@56078
   497
        | collect_pat (Const (@{const_name SMT2.nopat}, _) $ t) = collect t
blanchet@56078
   498
        | collect_pat _ = I
blanchet@56078
   499
    in (fn t => collect t Symtab.empty) end
blanchet@56078
   500
in
blanchet@56078
   501
blanchet@56078
   502
fun monomorph ctxt xthms =
blanchet@56078
   503
  let val (xs, thms) = split_list xthms
blanchet@56078
   504
  in
blanchet@56078
   505
    map (pair 1) thms
blanchet@56078
   506
    |> Monomorph.monomorph schematic_consts_of ctxt
blanchet@56078
   507
    |> maps (uncurry (map o pair)) o map2 pair xs o map (map snd)
blanchet@56078
   508
  end
blanchet@56078
   509
blanchet@56078
   510
end
blanchet@56078
   511
blanchet@56106
   512
fun normalize ctxt wthms =
blanchet@56106
   513
  wthms
blanchet@56106
   514
  |> map_index I
blanchet@56078
   515
  |> gen_normalize ctxt
blanchet@56078
   516
  |> unfold1 ctxt
blanchet@56078
   517
  |> monomorph ctxt
blanchet@56078
   518
  |> unfold2 ctxt
blanchet@56078
   519
  |> apply_extra_norms ctxt
blanchet@56078
   520
blanchet@56078
   521
val _ = Theory.setup (Context.theory_map (
blanchet@56078
   522
  setup_atomize #>
blanchet@56078
   523
  setup_unfolded_quants #>
blanchet@56078
   524
  setup_trigger #>
blanchet@56078
   525
  setup_case_bool #>
blanchet@56078
   526
  setup_abs_min_max #>
blanchet@56078
   527
  setup_nat_as_int))
blanchet@56078
   528
blanchet@56078
   529
end