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