src/HOL/Tools/SMT2/smt2_normalize.ML
author blanchet
Thu Mar 13 13:18:14 2014 +0100 (2014-03-13)
changeset 56090 34bd10a9a2ad
parent 56078 624faeda77b5
child 56100 0dc5f68a7802
permissions -rw-r--r--
adapted to renamed ML files
     1 (*  Title:      HOL/Tools/SMT2/smt2_normalize.ML
     2     Author:     Sascha Boehme, TU Muenchen
     3 
     4 Normalization steps on theorems required by SMT solvers.
     5 *)
     6 
     7 signature SMT2_NORMALIZE =
     8 sig
     9   val drop_fact_warning: Proof.context -> thm -> unit
    10   val atomize_conv: Proof.context -> conv
    11   type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
    12   val add_extra_norm: SMT2_Util.class * extra_norm -> Context.generic -> Context.generic
    13   val normalize: (int * (int option * thm)) list -> Proof.context ->
    14     (int * thm) list * Proof.context
    15 end
    16 
    17 structure SMT2_Normalize: SMT2_NORMALIZE =
    18 struct
    19 
    20 fun drop_fact_warning ctxt =
    21   SMT2_Config.verbose_msg ctxt (prefix "Warning: dropping assumption: " o
    22     Display.string_of_thm ctxt)
    23 
    24 
    25 (* general theorem normalizations *)
    26 
    27 (** instantiate elimination rules **)
    28  
    29 local
    30   val (cpfalse, cfalse) = `SMT2_Util.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 SMT2_Builtin.add_builtin_fun_ext'' [@{const_name "==>"},
    74     @{const_name "=="}, @{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   SMT2_Util.if_exists_conv (is_some o special_quant) (Conv.top_conv special_quant_conv ctxt)
   104 
   105 val setup_unfolded_quants =
   106   fold (SMT2_Builtin.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 (SMT2_Util.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 (SMT2_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 = SMT2_Util.mk_const_pat @{theory} @{const_name SMT2.pat} SMT2_Util.destT1
   198   fun mk_pat ct = Thm.apply (SMT2_Util.instT' ct pat) ct
   199 
   200   fun mk_clist T = pairself (Thm.cterm_of @{theory}) (HOLogic.cons_const T, HOLogic.nil_const T)
   201   fun mk_list (ccons, cnil) f cts = fold_rev (Thm.mk_binop ccons o f) cts cnil
   202   val mk_pat_list = mk_list (mk_clist @{typ SMT2.pattern})
   203   val mk_mpat_list = mk_list (mk_clist @{typ "SMT2.pattern list"})  
   204   fun mk_trigger ctss = mk_mpat_list (mk_pat_list mk_pat) ctss
   205 
   206   val trigger_eq =
   207     mk_meta_eq @{lemma "p = SMT2.trigger t p" by (simp add: trigger_def)}
   208 
   209   fun insert_trigger_conv [] ct = Conv.all_conv ct
   210     | insert_trigger_conv ctss ct =
   211         let val (ctr, cp) = Thm.dest_binop (Thm.rhs_of trigger_eq) ||> rpair ct
   212         in Thm.instantiate ([], [cp, (ctr, mk_trigger ctss)]) trigger_eq end
   213 
   214   fun infer_trigger_eq_conv outer_ctxt (ctxt, cvs) ct =
   215     let
   216       val (lhs, rhs) = dest_cond_eq ct
   217 
   218       val vs = map Thm.term_of cvs
   219       val thy = Proof_Context.theory_of ctxt
   220 
   221       fun get_mpats ct =
   222         if is_simp_lhs ctxt (Thm.term_of ct) then minimal_pats vs ct
   223         else []
   224       val gen = Variable.export_terms ctxt outer_ctxt
   225       val filter_mpats = filter (proper_mpat thy gen (Thm.term_of rhs))
   226 
   227     in insert_trigger_conv (filter_mpats (get_mpats lhs)) ct end
   228 
   229   fun has_trigger (@{const SMT2.trigger} $ _ $ _) = true
   230     | has_trigger _ = false
   231 
   232   fun try_trigger_conv cv ct =
   233     if SMT2_Util.under_quant has_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
   234     else Conv.try_conv cv ct
   235 
   236   fun infer_trigger_conv ctxt =
   237     if Config.get ctxt SMT2_Config.infer_triggers then
   238       try_trigger_conv (SMT2_Util.under_quant_conv (infer_trigger_eq_conv ctxt) ctxt)
   239     else Conv.all_conv
   240 in
   241 
   242 fun trigger_conv ctxt =
   243   SMT2_Util.prop_conv (check_trigger_conv ctxt then_conv infer_trigger_conv ctxt)
   244 
   245 val setup_trigger =
   246   fold SMT2_Builtin.add_builtin_fun_ext''
   247     [@{const_name SMT2.pat}, @{const_name SMT2.nopat}, @{const_name SMT2.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 SMT2.weight} => true | _ => false)
   259 
   260   fun is_weight (@{const SMT2.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 SMT2.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 SMT2_Util.under_quant proper_trigger (SMT2_Util.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 SMT2.trigger} $ _ $ _ => Conv.arg_conv cv
   284     | _ => cv) ct
   285 
   286   val weight_eq =
   287     mk_meta_eq @{lemma "p = SMT2.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 SMT2_Util.under_quant_conv (K (under_trigger_conv cv)) ctxt end
   298 in
   299 
   300 fun weight_conv weight ctxt = 
   301   SMT2_Util.prop_conv (check_weight_conv ctxt then_conv add_weight_conv weight ctxt)
   302 
   303 val setup_weight = SMT2_Builtin.add_builtin_fun_ext'' @{const_name SMT2.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   (* Z3 4.3.1 silently normalizes "P --> Q --> R" to "P & Q --> R" *)
   325   |> Raw_Simplifier.rewrite_rule ctxt @{thms HOL.imp_conjL[symmetric, THEN eq_reflection]}
   326 
   327 fun gen_norm1_safe ctxt (i, (weight, thm)) =
   328   (case try (gen_normalize1 ctxt weight) thm of
   329     SOME thm' => SOME (i, thm')
   330   | NONE => (drop_fact_warning ctxt thm; NONE))
   331 
   332 fun gen_normalize ctxt iwthms = map_filter (gen_norm1_safe ctxt) iwthms
   333 
   334 
   335 
   336 (* unfolding of definitions and theory-specific rewritings *)
   337 
   338 fun expand_head_conv cv ct =
   339   (case Thm.term_of ct of
   340     _ $ _ =>
   341       Conv.fun_conv (expand_head_conv cv) then_conv
   342       Conv.try_conv (Thm.beta_conversion false)
   343   | _ => cv) ct
   344 
   345 
   346 (** rewrite bool case expressions as if expressions **)
   347 
   348 local
   349   fun is_case_bool (Const (@{const_name "bool.case_bool"}, _)) = true
   350     | is_case_bool _ = false
   351 
   352   val thm = mk_meta_eq @{lemma
   353     "case_bool = (%x y P. if P then x else y)" by (rule ext)+ simp}
   354 
   355   fun unfold_conv _ =
   356     SMT2_Util.if_true_conv (is_case_bool o Term.head_of) (expand_head_conv (Conv.rewr_conv thm))
   357 in
   358 
   359 fun rewrite_case_bool_conv ctxt =
   360   SMT2_Util.if_exists_conv is_case_bool (Conv.top_conv unfold_conv ctxt)
   361 
   362 val setup_case_bool =
   363   SMT2_Builtin.add_builtin_fun_ext'' @{const_name "bool.case_bool"}
   364 
   365 end
   366 
   367 
   368 (** unfold abs, min and max **)
   369 
   370 local
   371   val abs_def = mk_meta_eq @{lemma
   372     "abs = (%a::'a::abs_if. if a < 0 then - a else a)"
   373     by (rule ext) (rule abs_if)}
   374 
   375   val min_def = mk_meta_eq @{lemma "min = (%a b. if a <= b then a else b)"
   376     by (rule ext)+ (rule min_def)}
   377 
   378   val max_def = mk_meta_eq  @{lemma "max = (%a b. if a <= b then b else a)"
   379     by (rule ext)+ (rule max_def)}
   380 
   381   val defs = [(@{const_name min}, min_def), (@{const_name max}, max_def),
   382     (@{const_name abs}, abs_def)]
   383 
   384   fun is_builtinT ctxt T =
   385     SMT2_Builtin.is_builtin_typ_ext ctxt (Term.domain_type T)
   386 
   387   fun abs_min_max ctxt (Const (n, T)) =
   388         (case AList.lookup (op =) defs n of
   389           NONE => NONE
   390         | SOME thm => if is_builtinT ctxt T then SOME thm else NONE)
   391     | abs_min_max _ _ = NONE
   392 
   393   fun unfold_amm_conv ctxt ct =
   394     (case abs_min_max ctxt (Term.head_of (Thm.term_of ct)) of
   395       SOME thm => expand_head_conv (Conv.rewr_conv thm)
   396     | NONE => Conv.all_conv) ct
   397 in
   398 
   399 fun unfold_abs_min_max_conv ctxt =
   400   SMT2_Util.if_exists_conv (is_some o abs_min_max ctxt) (Conv.top_conv unfold_amm_conv ctxt)
   401   
   402 val setup_abs_min_max = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) defs
   403 
   404 end
   405 
   406 
   407 (** embedding of standard natural number operations into integer operations **)
   408 
   409 local
   410   val nat_embedding = @{lemma
   411     "ALL n. nat (int n) = n"
   412     "ALL i. i >= 0 --> int (nat i) = i"
   413     "ALL i. i < 0 --> int (nat i) = 0"
   414     by simp_all}
   415 
   416   val simple_nat_ops = [
   417     @{const less (nat)}, @{const less_eq (nat)},
   418     @{const Suc}, @{const plus (nat)}, @{const minus (nat)}]
   419 
   420   val mult_nat_ops =
   421     [@{const times (nat)}, @{const div (nat)}, @{const mod (nat)}]
   422 
   423   val nat_ops = simple_nat_ops @ mult_nat_ops
   424 
   425   val nat_consts = nat_ops @ [@{const numeral (nat)},
   426     @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
   427 
   428   val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
   429 
   430   val builtin_nat_ops = nat_int_coercions @ simple_nat_ops
   431 
   432   val is_nat_const = member (op aconv) nat_consts
   433 
   434   fun is_nat_const' @{const of_nat (int)} = true
   435     | is_nat_const' t = is_nat_const t
   436 
   437   val expands = map mk_meta_eq @{lemma
   438     "0 = nat 0"
   439     "1 = nat 1"
   440     "(numeral :: num => nat) = (%i. nat (numeral i))"
   441     "op < = (%a b. int a < int b)"
   442     "op <= = (%a b. int a <= int b)"
   443     "Suc = (%a. nat (int a + 1))"
   444     "op + = (%a b. nat (int a + int b))"
   445     "op - = (%a b. nat (int a - int b))"
   446     "op * = (%a b. nat (int a * int b))"
   447     "op div = (%a b. nat (int a div int b))"
   448     "op mod = (%a b. nat (int a mod int b))"
   449     by (fastforce simp add: nat_mult_distrib nat_div_distrib nat_mod_distrib)+}
   450 
   451   val ints = map mk_meta_eq @{lemma
   452     "int 0 = 0"
   453     "int 1 = 1"
   454     "int (Suc n) = int n + 1"
   455     "int (n + m) = int n + int m"
   456     "int (n - m) = int (nat (int n - int m))"
   457     "int (n * m) = int n * int m"
   458     "int (n div m) = int n div int m"
   459     "int (n mod m) = int n mod int m"
   460     by (auto simp add: int_mult zdiv_int zmod_int)}
   461 
   462   val int_if = mk_meta_eq @{lemma
   463     "int (if P then n else m) = (if P then int n else int m)"
   464     by simp}
   465 
   466   fun mk_number_eq ctxt i lhs =
   467     let
   468       val eq = SMT2_Util.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
   469       val ctxt' = put_simpset HOL_ss ctxt addsimps @{thms Int.int_numeral}
   470       val tac = HEADGOAL (Simplifier.simp_tac ctxt')
   471     in Goal.norm_result ctxt (Goal.prove_internal ctxt [] eq (K tac)) end
   472 
   473   fun ite_conv cv1 cv2 =
   474     Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
   475 
   476   fun int_conv ctxt ct =
   477     (case Thm.term_of ct of
   478       @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
   479         Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
   480     | @{const of_nat (int)} $ _ =>
   481         (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
   482         (Conv.rewr_conv int_if then_conv
   483           ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
   484         Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
   485     | _ => Conv.no_conv) ct
   486 
   487   and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
   488 
   489   and expand_conv ctxt =
   490     SMT2_Util.if_conv (is_nat_const o Term.head_of)
   491       (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt) (int_conv ctxt)
   492 
   493   and nat_conv ctxt = SMT2_Util.if_exists_conv is_nat_const' (Conv.top_sweep_conv expand_conv ctxt)
   494 
   495   val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
   496 in
   497 
   498 val nat_as_int_conv = nat_conv
   499 
   500 fun add_nat_embedding thms =
   501   if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding)
   502   else (thms, [])
   503 
   504 val setup_nat_as_int =
   505   SMT2_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
   506   fold (SMT2_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
   507 
   508 end
   509 
   510 
   511 (** normalize numerals **)
   512 
   513 local
   514   (*
   515     rewrite Numeral1 into 1
   516     rewrite - 0 into 0
   517   *)
   518 
   519   fun is_irregular_number (Const (@{const_name numeral}, _) $ Const (@{const_name num.One}, _)) =
   520         true
   521     | is_irregular_number (Const (@{const_name uminus}, _) $ Const (@{const_name Groups.zero}, _)) =
   522         true
   523     | is_irregular_number _ =
   524         false;
   525 
   526   fun is_strange_number ctxt t = is_irregular_number t andalso SMT2_Builtin.is_builtin_num ctxt t;
   527 
   528   val proper_num_ss =
   529     simpset_of (put_simpset HOL_ss @{context}
   530       addsimps @{thms Num.numeral_One minus_zero})
   531 
   532   fun norm_num_conv ctxt =
   533     SMT2_Util.if_conv (is_strange_number ctxt) (Simplifier.rewrite (put_simpset proper_num_ss ctxt))
   534       Conv.no_conv
   535 in
   536 
   537 fun normalize_numerals_conv ctxt =
   538   SMT2_Util.if_exists_conv (is_strange_number ctxt) (Conv.top_sweep_conv norm_num_conv ctxt)
   539 
   540 end
   541 
   542 
   543 (** combined unfoldings and rewritings **)
   544 
   545 fun unfold_conv ctxt =
   546   rewrite_case_bool_conv ctxt then_conv
   547   unfold_abs_min_max_conv ctxt then_conv
   548   nat_as_int_conv ctxt then_conv
   549   Thm.beta_conversion true
   550 
   551 fun unfold1 ctxt = map (apsnd (Conv.fconv_rule (unfold_conv ctxt)))
   552 
   553 fun burrow_ids f ithms =
   554   let
   555     val (is, thms) = split_list ithms
   556     val (thms', extra_thms) = f thms
   557   in (is ~~ thms') @ map (pair ~1) extra_thms end
   558 
   559 fun unfold2 ctxt ithms =
   560   ithms
   561   |> map (apsnd (Conv.fconv_rule (normalize_numerals_conv ctxt)))
   562   |> burrow_ids add_nat_embedding
   563 
   564 
   565 
   566 (* overall normalization *)
   567 
   568 type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
   569 
   570 structure Extra_Norms = Generic_Data
   571 (
   572   type T = extra_norm SMT2_Util.dict
   573   val empty = []
   574   val extend = I
   575   fun merge data = SMT2_Util.dict_merge fst data
   576 )
   577 
   578 fun add_extra_norm (cs, norm) = Extra_Norms.map (SMT2_Util.dict_update (cs, norm))
   579 
   580 fun apply_extra_norms ctxt ithms =
   581   let
   582     val cs = SMT2_Config.solver_class_of ctxt
   583     val es = SMT2_Util.dict_lookup (Extra_Norms.get (Context.Proof ctxt)) cs
   584   in burrow_ids (fold (fn e => e ctxt) es o rpair []) ithms end
   585 
   586 local
   587   val ignored = member (op =) [@{const_name All}, @{const_name Ex},
   588     @{const_name Let}, @{const_name If}, @{const_name HOL.eq}]
   589 
   590   val schematic_consts_of =
   591     let
   592       fun collect (@{const SMT2.trigger} $ p $ t) =
   593             collect_trigger p #> collect t
   594         | collect (t $ u) = collect t #> collect u
   595         | collect (Abs (_, _, t)) = collect t
   596         | collect (t as Const (n, _)) = 
   597             if not (ignored n) then Monomorph.add_schematic_consts_of t else I
   598         | collect _ = I
   599       and collect_trigger t =
   600         let val dest = these o try HOLogic.dest_list 
   601         in fold (fold collect_pat o dest) (dest t) end
   602       and collect_pat (Const (@{const_name SMT2.pat}, _) $ t) = collect t
   603         | collect_pat (Const (@{const_name SMT2.nopat}, _) $ t) = collect t
   604         | collect_pat _ = I
   605     in (fn t => collect t Symtab.empty) end
   606 in
   607 
   608 fun monomorph ctxt xthms =
   609   let val (xs, thms) = split_list xthms
   610   in
   611     map (pair 1) thms
   612     |> Monomorph.monomorph schematic_consts_of ctxt
   613     |> maps (uncurry (map o pair)) o map2 pair xs o map (map snd)
   614   end
   615 
   616 end
   617 
   618 fun normalize iwthms ctxt =
   619   iwthms
   620   |> gen_normalize ctxt
   621   |> unfold1 ctxt
   622   |> monomorph ctxt
   623   |> unfold2 ctxt
   624   |> apply_extra_norms ctxt
   625   |> rpair ctxt
   626 
   627 val _ = Theory.setup (Context.theory_map (
   628   setup_atomize #>
   629   setup_unfolded_quants #>
   630   setup_trigger #>
   631   setup_weight #>
   632   setup_case_bool #>
   633   setup_abs_min_max #>
   634   setup_nat_as_int))
   635 
   636 end