src/HOL/Tools/SMT2/smt2_normalize.ML
author blanchet
Thu Mar 13 14:48:20 2014 +0100 (2014-03-13)
changeset 56105 75dc126f5dcb
parent 56104 fd6e132ee4fb
child 56106 9cfea3ab002a
permissions -rw-r--r--
more robust indices
     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 _ => norm_def (thm RS @{thm meta_eq_to_obj_eq})
    53   | _ => thm)
    54 
    55 
    56 (** atomization **)
    57 
    58 fun atomize_conv ctxt ct =
    59   (case Thm.term_of ct of
    60     @{const "==>"} $ _ $ _ =>
    61       Conv.binop_conv (atomize_conv ctxt) then_conv Conv.rewr_conv @{thm atomize_imp}
    62   | Const (@{const_name "=="}, _) $ _ $ _ =>
    63       Conv.binop_conv (atomize_conv ctxt) then_conv Conv.rewr_conv @{thm atomize_eq}
    64   | Const (@{const_name all}, _) $ Abs _ =>
    65       Conv.binder_conv (atomize_conv o snd) ctxt then_conv Conv.rewr_conv @{thm atomize_all}
    66   | _ => Conv.all_conv) ct
    67 
    68 val setup_atomize =
    69   fold SMT2_Builtin.add_builtin_fun_ext'' [@{const_name "==>"}, @{const_name "=="},
    70     @{const_name all}, @{const_name Trueprop}]
    71 
    72 
    73 (** unfold special quantifiers **)
    74 
    75 local
    76   val special_quants = [
    77     (@{const_name Ex1}, @{thm Ex1_def_raw}),
    78     (@{const_name Ball}, @{thm Ball_def_raw}),
    79     (@{const_name Bex}, @{thm Bex_def_raw})]
    80   
    81   fun special_quant (Const (n, _)) = AList.lookup (op =) special_quants n
    82     | special_quant _ = NONE
    83 
    84   fun special_quant_conv _ ct =
    85     (case special_quant (Thm.term_of ct) of
    86       SOME thm => Conv.rewr_conv thm
    87     | NONE => Conv.all_conv) ct
    88 in
    89 
    90 fun unfold_special_quants_conv ctxt =
    91   SMT2_Util.if_exists_conv (is_some o special_quant) (Conv.top_conv special_quant_conv ctxt)
    92 
    93 val setup_unfolded_quants = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) special_quants
    94 
    95 end
    96 
    97 
    98 (** trigger inference **)
    99 
   100 local
   101   (*** check trigger syntax ***)
   102 
   103   fun dest_trigger (Const (@{const_name pat}, _) $ _) = SOME true
   104     | dest_trigger (Const (@{const_name nopat}, _) $ _) = SOME false
   105     | dest_trigger _ = NONE
   106 
   107   fun eq_list [] = false
   108     | eq_list (b :: bs) = forall (equal b) bs
   109 
   110   fun proper_trigger t =
   111     t
   112     |> these o try HOLogic.dest_list
   113     |> map (map_filter dest_trigger o these o try HOLogic.dest_list)
   114     |> (fn [] => false | bss => forall eq_list bss)
   115 
   116   fun proper_quant inside f t =
   117     (case t of
   118       Const (@{const_name All}, _) $ Abs (_, _, u) => proper_quant true f u
   119     | Const (@{const_name Ex}, _) $ Abs (_, _, u) => proper_quant true f u
   120     | @{const trigger} $ p $ u =>
   121         (if inside then f p else false) andalso proper_quant false f u
   122     | Abs (_, _, u) => proper_quant false f u
   123     | u1 $ u2 => proper_quant false f u1 andalso proper_quant false f u2
   124     | _ => true)
   125 
   126   fun check_trigger_error ctxt t =
   127     error ("SMT triggers must only occur under quantifier and multipatterns " ^
   128       "must have the same kind: " ^ Syntax.string_of_term ctxt t)
   129 
   130   fun check_trigger_conv ctxt ct =
   131     if proper_quant false proper_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
   132     else check_trigger_error ctxt (Thm.term_of ct)
   133 
   134 
   135   (*** infer simple triggers ***)
   136 
   137   fun dest_cond_eq ct =
   138     (case Thm.term_of ct of
   139       Const (@{const_name HOL.eq}, _) $ _ $ _ => Thm.dest_binop ct
   140     | @{const HOL.implies} $ _ $ _ => dest_cond_eq (Thm.dest_arg ct)
   141     | _ => raise CTERM ("no equation", [ct]))
   142 
   143   fun get_constrs thy (Type (n, _)) = these (Datatype.get_constrs thy n)
   144     | get_constrs _ _ = []
   145 
   146   fun is_constr thy (n, T) =
   147     let fun match (m, U) = m = n andalso Sign.typ_instance thy (T, U)
   148     in can (the o find_first match o get_constrs thy o Term.body_type) T end
   149 
   150   fun is_constr_pat thy t =
   151     (case Term.strip_comb t of
   152       (Free _, []) => true
   153     | (Const c, ts) => is_constr thy c andalso forall (is_constr_pat thy) ts
   154     | _ => false)
   155 
   156   fun is_simp_lhs ctxt t =
   157     (case Term.strip_comb t of
   158       (Const c, ts as _ :: _) =>
   159         not (SMT2_Builtin.is_builtin_fun_ext ctxt c ts) andalso
   160         forall (is_constr_pat (Proof_Context.theory_of ctxt)) ts
   161     | _ => false)
   162 
   163   fun has_all_vars vs t =
   164     subset (op aconv) (vs, map Free (Term.add_frees t []))
   165 
   166   fun minimal_pats vs ct =
   167     if has_all_vars vs (Thm.term_of ct) then
   168       (case Thm.term_of ct of
   169         _ $ _ =>
   170           (case pairself (minimal_pats vs) (Thm.dest_comb ct) of
   171             ([], []) => [[ct]]
   172           | (ctss, ctss') => union (eq_set (op aconvc)) ctss ctss')
   173       | _ => [])
   174     else []
   175 
   176   fun proper_mpat _ _ _ [] = false
   177     | proper_mpat thy gen u cts =
   178         let
   179           val tps = (op ~~) (`gen (map Thm.term_of cts))
   180           fun some_match u = tps |> exists (fn (t', t) =>
   181             Pattern.matches thy (t', u) andalso not (t aconv u))
   182         in not (Term.exists_subterm some_match u) end
   183 
   184   val pat = SMT2_Util.mk_const_pat @{theory} @{const_name SMT2.pat} SMT2_Util.destT1
   185   fun mk_pat ct = Thm.apply (SMT2_Util.instT' ct pat) ct
   186 
   187   fun mk_clist T = pairself (Thm.cterm_of @{theory}) (HOLogic.cons_const T, HOLogic.nil_const T)
   188   fun mk_list (ccons, cnil) f cts = fold_rev (Thm.mk_binop ccons o f) cts cnil
   189   val mk_pat_list = mk_list (mk_clist @{typ SMT2.pattern})
   190   val mk_mpat_list = mk_list (mk_clist @{typ "SMT2.pattern list"})  
   191   fun mk_trigger ctss = mk_mpat_list (mk_pat_list mk_pat) ctss
   192 
   193   val trigger_eq = mk_meta_eq @{lemma "p = SMT2.trigger t p" by (simp add: trigger_def)}
   194 
   195   fun insert_trigger_conv [] ct = Conv.all_conv ct
   196     | insert_trigger_conv ctss ct =
   197         let val (ctr, cp) = Thm.dest_binop (Thm.rhs_of trigger_eq) ||> rpair ct
   198         in Thm.instantiate ([], [cp, (ctr, mk_trigger ctss)]) trigger_eq end
   199 
   200   fun infer_trigger_eq_conv outer_ctxt (ctxt, cvs) ct =
   201     let
   202       val (lhs, rhs) = dest_cond_eq ct
   203 
   204       val vs = map Thm.term_of cvs
   205       val thy = Proof_Context.theory_of ctxt
   206 
   207       fun get_mpats ct =
   208         if is_simp_lhs ctxt (Thm.term_of ct) then minimal_pats vs ct
   209         else []
   210       val gen = Variable.export_terms ctxt outer_ctxt
   211       val filter_mpats = filter (proper_mpat thy gen (Thm.term_of rhs))
   212 
   213     in insert_trigger_conv (filter_mpats (get_mpats lhs)) ct end
   214 
   215   fun has_trigger (@{const SMT2.trigger} $ _ $ _) = true
   216     | has_trigger _ = false
   217 
   218   fun try_trigger_conv cv ct =
   219     if SMT2_Util.under_quant has_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
   220     else Conv.try_conv cv ct
   221 
   222   fun infer_trigger_conv ctxt =
   223     if Config.get ctxt SMT2_Config.infer_triggers then
   224       try_trigger_conv (SMT2_Util.under_quant_conv (infer_trigger_eq_conv ctxt) ctxt)
   225     else Conv.all_conv
   226 in
   227 
   228 fun trigger_conv ctxt =
   229   SMT2_Util.prop_conv (check_trigger_conv ctxt then_conv infer_trigger_conv ctxt)
   230 
   231 val setup_trigger =
   232   fold SMT2_Builtin.add_builtin_fun_ext''
   233     [@{const_name SMT2.pat}, @{const_name SMT2.nopat}, @{const_name SMT2.trigger}]
   234 
   235 end
   236 
   237 
   238 (** adding quantifier weights **)
   239 
   240 local
   241   (*** check weight syntax ***)
   242 
   243   val has_no_weight =
   244     not o Term.exists_subterm (fn @{const SMT2.weight} => true | _ => false)
   245 
   246   fun is_weight (@{const SMT2.weight} $ w $ t) =
   247         (case try HOLogic.dest_number w of
   248           SOME (_, i) => i >= 0 andalso has_no_weight t
   249         | _ => false)
   250     | is_weight t = has_no_weight t
   251 
   252   fun proper_trigger (@{const SMT2.trigger} $ _ $ t) = is_weight t
   253     | proper_trigger t = is_weight t 
   254 
   255   fun check_weight_error ctxt t =
   256     error ("SMT weight must be a non-negative number and must only occur " ^
   257       "under the top-most quantifier and an optional trigger: " ^
   258       Syntax.string_of_term ctxt t)
   259 
   260   fun check_weight_conv ctxt ct =
   261     if SMT2_Util.under_quant proper_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
   262     else check_weight_error ctxt (Thm.term_of ct)
   263 
   264 
   265   (*** insertion of weights ***)
   266 
   267   fun under_trigger_conv cv ct =
   268     (case Thm.term_of ct of
   269       @{const SMT2.trigger} $ _ $ _ => Conv.arg_conv cv
   270     | _ => cv) ct
   271 
   272   val weight_eq = mk_meta_eq @{lemma "p = SMT2.weight i p" by (simp add: weight_def)}
   273   fun mk_weight_eq w =
   274     let val cv = Thm.dest_arg1 (Thm.rhs_of weight_eq)
   275     in Thm.instantiate ([], [(cv, Numeral.mk_cnumber @{ctyp int} w)]) weight_eq end
   276 
   277   fun add_weight_conv NONE _ = Conv.all_conv
   278     | add_weight_conv (SOME weight) ctxt =
   279         let val cv = Conv.rewr_conv (mk_weight_eq weight)
   280         in SMT2_Util.under_quant_conv (K (under_trigger_conv cv)) ctxt end
   281 in
   282 
   283 fun weight_conv weight ctxt = 
   284   SMT2_Util.prop_conv (check_weight_conv ctxt then_conv add_weight_conv weight ctxt)
   285 
   286 val setup_weight = SMT2_Builtin.add_builtin_fun_ext'' @{const_name SMT2.weight}
   287 
   288 end
   289 
   290 
   291 (** combined general normalizations **)
   292 
   293 fun gen_normalize1_conv ctxt weight =
   294   atomize_conv ctxt then_conv
   295   unfold_special_quants_conv ctxt then_conv
   296   Thm.beta_conversion true then_conv
   297   trigger_conv ctxt then_conv
   298   weight_conv weight ctxt
   299 
   300 fun gen_normalize1 ctxt weight thm =
   301   thm
   302   |> instantiate_elim
   303   |> norm_def
   304   |> Conv.fconv_rule (Thm.beta_conversion true then_conv Thm.eta_conversion)
   305   |> Drule.forall_intr_vars
   306   |> Conv.fconv_rule (gen_normalize1_conv ctxt weight)
   307   (* Z3 4.3.1 silently normalizes "P --> Q --> R" to "P & Q --> R" *)
   308   |> Raw_Simplifier.rewrite_rule ctxt @{thms HOL.imp_conjL[symmetric, THEN eq_reflection]}
   309 
   310 fun gen_norm1_safe ctxt (i, (weight, thm)) =
   311   (case try (gen_normalize1 ctxt weight) thm of
   312     SOME thm' => SOME (i, thm')
   313   | NONE => (drop_fact_warning ctxt thm; NONE))
   314 
   315 fun gen_normalize ctxt iwthms = map_filter (gen_norm1_safe ctxt) iwthms
   316 
   317 
   318 
   319 (* unfolding of definitions and theory-specific rewritings *)
   320 
   321 fun expand_head_conv cv ct =
   322   (case Thm.term_of ct of
   323     _ $ _ =>
   324       Conv.fun_conv (expand_head_conv cv) then_conv
   325       Conv.try_conv (Thm.beta_conversion false)
   326   | _ => cv) ct
   327 
   328 
   329 (** rewrite bool case expressions as if expressions **)
   330 
   331 local
   332   fun is_case_bool (Const (@{const_name "bool.case_bool"}, _)) = true
   333     | is_case_bool _ = false
   334 
   335   fun unfold_conv _ =
   336     SMT2_Util.if_true_conv (is_case_bool o Term.head_of)
   337       (expand_head_conv (Conv.rewr_conv @{thm case_bool_if}))
   338 in
   339 
   340 fun rewrite_case_bool_conv ctxt =
   341   SMT2_Util.if_exists_conv is_case_bool (Conv.top_conv unfold_conv ctxt)
   342 
   343 val setup_case_bool = SMT2_Builtin.add_builtin_fun_ext'' @{const_name "bool.case_bool"}
   344 
   345 end
   346 
   347 
   348 (** unfold abs, min and max **)
   349 
   350 local
   351   val defs = [
   352     (@{const_name min}, @{thm min_def_raw}),
   353     (@{const_name max}, @{thm max_def_raw}),
   354     (@{const_name abs}, @{thm abs_if_raw})]
   355 
   356   fun abs_min_max ctxt (Const (n, Type (@{type_name fun}, [T, _]))) =
   357         (case AList.lookup (op =) defs n of
   358           NONE => NONE
   359         | SOME thm => if SMT2_Builtin.is_builtin_typ_ext ctxt T then SOME thm else NONE)
   360     | abs_min_max _ _ = NONE
   361 
   362   fun unfold_amm_conv ctxt ct =
   363     (case abs_min_max ctxt (Term.head_of (Thm.term_of ct)) of
   364       SOME thm => expand_head_conv (Conv.rewr_conv thm)
   365     | NONE => Conv.all_conv) ct
   366 in
   367 
   368 fun unfold_abs_min_max_conv ctxt =
   369   SMT2_Util.if_exists_conv (is_some o abs_min_max ctxt) (Conv.top_conv unfold_amm_conv ctxt)
   370   
   371 val setup_abs_min_max = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) defs
   372 
   373 end
   374 
   375 
   376 (** embedding of standard natural number operations into integer operations **)
   377 
   378 local
   379   val nat_embedding = @{thms nat_int int_nat_nneg int_nat_neg}
   380 
   381   val simple_nat_ops = [
   382     @{const less (nat)}, @{const less_eq (nat)},
   383     @{const Suc}, @{const plus (nat)}, @{const minus (nat)}]
   384 
   385   val mult_nat_ops =
   386     [@{const times (nat)}, @{const div (nat)}, @{const mod (nat)}]
   387 
   388   val nat_ops = simple_nat_ops @ mult_nat_ops
   389 
   390   val nat_consts = nat_ops @ [@{const numeral (nat)},
   391     @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
   392 
   393   val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
   394 
   395   val builtin_nat_ops = nat_int_coercions @ simple_nat_ops
   396 
   397   val is_nat_const = member (op aconv) nat_consts
   398 
   399   fun is_nat_const' @{const of_nat (int)} = true
   400     | is_nat_const' t = is_nat_const t
   401 
   402   val expands = map mk_meta_eq @{thms nat_zero_as_int nat_one_as_int nat_numeral_as_int
   403     nat_less_as_int nat_leq_as_int Suc_as_int nat_plus_as_int nat_minus_as_int nat_times_as_int
   404     nat_div_as_int nat_mod_as_int}
   405 
   406   val ints = map mk_meta_eq @{thms int_0 int_1 int_Suc int_plus int_minus int_mult zdiv_int
   407     zmod_int}
   408   val int_if = mk_meta_eq @{lemma "int (if P then n else m) = (if P then int n else int m)" by simp}
   409 
   410   fun mk_number_eq ctxt i lhs =
   411     let
   412       val eq = SMT2_Util.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
   413       val ctxt' = put_simpset HOL_ss ctxt addsimps @{thms Int.int_numeral}
   414       val tac = HEADGOAL (Simplifier.simp_tac ctxt')
   415     in Goal.norm_result ctxt (Goal.prove_internal ctxt [] eq (K tac)) end
   416 
   417   fun ite_conv cv1 cv2 =
   418     Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
   419 
   420   fun int_conv ctxt ct =
   421     (case Thm.term_of ct of
   422       @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
   423         Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
   424     | @{const of_nat (int)} $ _ =>
   425         (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
   426         (Conv.rewr_conv int_if then_conv
   427           ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
   428         Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
   429     | _ => Conv.no_conv) ct
   430 
   431   and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
   432 
   433   and expand_conv ctxt =
   434     SMT2_Util.if_conv (is_nat_const o Term.head_of)
   435       (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt) (int_conv ctxt)
   436 
   437   and nat_conv ctxt = SMT2_Util.if_exists_conv is_nat_const' (Conv.top_sweep_conv expand_conv ctxt)
   438 
   439   val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
   440 in
   441 
   442 val nat_as_int_conv = nat_conv
   443 
   444 fun add_nat_embedding thms =
   445   if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding) else (thms, [])
   446 
   447 val setup_nat_as_int =
   448   SMT2_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
   449   fold (SMT2_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
   450 
   451 end
   452 
   453 
   454 (** normalize numerals **)
   455 
   456 local
   457   (*
   458     rewrite Numeral1 into 1
   459     rewrite - 0 into 0
   460   *)
   461 
   462   fun is_irregular_number (Const (@{const_name numeral}, _) $ Const (@{const_name num.One}, _)) =
   463         true
   464     | is_irregular_number (Const (@{const_name uminus}, _) $ Const (@{const_name Groups.zero}, _)) =
   465         true
   466     | is_irregular_number _ =
   467         false;
   468 
   469   fun is_strange_number ctxt t = is_irregular_number t andalso SMT2_Builtin.is_builtin_num ctxt t;
   470 
   471   val proper_num_ss =
   472     simpset_of (put_simpset HOL_ss @{context}
   473       addsimps @{thms Num.numeral_One minus_zero})
   474 
   475   fun norm_num_conv ctxt =
   476     SMT2_Util.if_conv (is_strange_number ctxt) (Simplifier.rewrite (put_simpset proper_num_ss ctxt))
   477       Conv.no_conv
   478 in
   479 
   480 fun normalize_numerals_conv ctxt =
   481   SMT2_Util.if_exists_conv (is_strange_number ctxt) (Conv.top_sweep_conv norm_num_conv ctxt)
   482 
   483 end
   484 
   485 
   486 (** combined unfoldings and rewritings **)
   487 
   488 fun unfold_conv ctxt =
   489   rewrite_case_bool_conv ctxt then_conv
   490   unfold_abs_min_max_conv ctxt then_conv
   491   nat_as_int_conv ctxt then_conv
   492   Thm.beta_conversion true
   493 
   494 fun unfold1 ctxt = map (apsnd (Conv.fconv_rule (unfold_conv ctxt)))
   495 
   496 fun burrow_ids f ithms =
   497   let
   498     val (is, thms) = split_list ithms
   499     val (thms', extra_thms) = f thms
   500   in (is ~~ thms') @ tag_list (fold Integer.max is 0 + 1) extra_thms end
   501 
   502 fun unfold2 ctxt ithms =
   503   ithms
   504   |> map (apsnd (Conv.fconv_rule (normalize_numerals_conv ctxt)))
   505   |> burrow_ids add_nat_embedding
   506 
   507 
   508 
   509 (* overall normalization *)
   510 
   511 type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
   512 
   513 structure Extra_Norms = Generic_Data
   514 (
   515   type T = extra_norm SMT2_Util.dict
   516   val empty = []
   517   val extend = I
   518   fun merge data = SMT2_Util.dict_merge fst data
   519 )
   520 
   521 fun add_extra_norm (cs, norm) = Extra_Norms.map (SMT2_Util.dict_update (cs, norm))
   522 
   523 fun apply_extra_norms ctxt ithms =
   524   let
   525     val cs = SMT2_Config.solver_class_of ctxt
   526     val es = SMT2_Util.dict_lookup (Extra_Norms.get (Context.Proof ctxt)) cs
   527   in burrow_ids (fold (fn e => e ctxt) es o rpair []) ithms end
   528 
   529 local
   530   val ignored = member (op =) [@{const_name All}, @{const_name Ex},
   531     @{const_name Let}, @{const_name If}, @{const_name HOL.eq}]
   532 
   533   val schematic_consts_of =
   534     let
   535       fun collect (@{const SMT2.trigger} $ p $ t) =
   536             collect_trigger p #> collect t
   537         | collect (t $ u) = collect t #> collect u
   538         | collect (Abs (_, _, t)) = collect t
   539         | collect (t as Const (n, _)) = 
   540             if not (ignored n) then Monomorph.add_schematic_consts_of t else I
   541         | collect _ = I
   542       and collect_trigger t =
   543         let val dest = these o try HOLogic.dest_list 
   544         in fold (fold collect_pat o dest) (dest t) end
   545       and collect_pat (Const (@{const_name SMT2.pat}, _) $ t) = collect t
   546         | collect_pat (Const (@{const_name SMT2.nopat}, _) $ t) = collect t
   547         | collect_pat _ = I
   548     in (fn t => collect t Symtab.empty) end
   549 in
   550 
   551 fun monomorph ctxt xthms =
   552   let val (xs, thms) = split_list xthms
   553   in
   554     map (pair 1) thms
   555     |> Monomorph.monomorph schematic_consts_of ctxt
   556     |> maps (uncurry (map o pair)) o map2 pair xs o map (map snd)
   557   end
   558 
   559 end
   560 
   561 fun normalize iwthms ctxt =
   562   iwthms
   563   |> gen_normalize ctxt
   564   |> unfold1 ctxt
   565   |> monomorph ctxt
   566   |> unfold2 ctxt
   567   |> apply_extra_norms ctxt
   568   |> rpair ctxt
   569 
   570 val _ = Theory.setup (Context.theory_map (
   571   setup_atomize #>
   572   setup_unfolded_quants #>
   573   setup_trigger #>
   574   setup_weight #>
   575   setup_case_bool #>
   576   setup_abs_min_max #>
   577   setup_nat_as_int))
   578 
   579 end