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