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