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