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