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