src/HOL/Tools/SMT/smt_normalize.ML
author boehmes
Thu Mar 28 23:44:41 2013 +0100 (2013-03-28)
changeset 51575 907efc894051
parent 50601 74da81de127f
child 51717 9e7d1c139569
permissions -rw-r--r--
new, simpler implementation of monomorphization;
old monomorphization code is still available as Legacy_Monomorphization;
modified SMT integration to use the new monomorphization code
     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 fun expand_head_conv cv ct =
   350   (case Thm.term_of ct of
   351     _ $ _ =>
   352       Conv.fun_conv (expand_head_conv cv) then_conv
   353       Conv.try_conv (Thm.beta_conversion false)
   354   | _ => cv) ct
   355 
   356 
   357 (** rewrite bool case expressions as if expressions **)
   358 
   359 local
   360   fun is_bool_case (Const (@{const_name "bool.bool_case"}, _)) = true
   361     | is_bool_case _ = false
   362 
   363   val thm = mk_meta_eq @{lemma
   364     "bool_case = (%x y P. if P then x else y)" by (rule ext)+ simp}
   365 
   366   fun unfold_conv _ =
   367     SMT_Utils.if_true_conv (is_bool_case o Term.head_of)
   368       (expand_head_conv (Conv.rewr_conv thm))
   369 in
   370 
   371 fun rewrite_bool_case_conv ctxt =
   372   SMT_Utils.if_exists_conv is_bool_case (Conv.top_conv unfold_conv ctxt)
   373 
   374 val setup_bool_case =
   375   SMT_Builtin.add_builtin_fun_ext'' @{const_name "bool.bool_case"}
   376 
   377 end
   378 
   379 
   380 (** unfold abs, min and max **)
   381 
   382 local
   383   val abs_def = mk_meta_eq @{lemma
   384     "abs = (%a::'a::abs_if. if a < 0 then - a else a)"
   385     by (rule ext) (rule abs_if)}
   386 
   387   val min_def = mk_meta_eq @{lemma "min = (%a b. if a <= b then a else b)"
   388     by (rule ext)+ (rule min_def)}
   389 
   390   val max_def = mk_meta_eq  @{lemma "max = (%a b. if a <= b then b else a)"
   391     by (rule ext)+ (rule max_def)}
   392 
   393   val defs = [(@{const_name min}, min_def), (@{const_name max}, max_def),
   394     (@{const_name abs}, abs_def)]
   395 
   396   fun is_builtinT ctxt T =
   397     SMT_Builtin.is_builtin_typ_ext ctxt (Term.domain_type T)
   398 
   399   fun abs_min_max ctxt (Const (n, T)) =
   400         (case AList.lookup (op =) defs n of
   401           NONE => NONE
   402         | SOME thm => if is_builtinT ctxt T then SOME thm else NONE)
   403     | abs_min_max _ _ = NONE
   404 
   405   fun unfold_amm_conv ctxt ct =
   406     (case abs_min_max ctxt (Term.head_of (Thm.term_of ct)) of
   407       SOME thm => expand_head_conv (Conv.rewr_conv thm)
   408     | NONE => Conv.all_conv) ct
   409 in
   410 
   411 fun unfold_abs_min_max_conv ctxt =
   412   SMT_Utils.if_exists_conv (is_some o abs_min_max ctxt)
   413     (Conv.top_conv unfold_amm_conv ctxt)
   414   
   415 val setup_abs_min_max = fold (SMT_Builtin.add_builtin_fun_ext'' o fst) defs
   416 
   417 end
   418 
   419 
   420 (** embedding of standard natural number operations into integer operations **)
   421 
   422 local
   423   val nat_embedding = @{lemma
   424     "ALL n. nat (int n) = n"
   425     "ALL i. i >= 0 --> int (nat i) = i"
   426     "ALL i. i < 0 --> int (nat i) = 0"
   427     by simp_all}
   428 
   429   val simple_nat_ops = [
   430     @{const less (nat)}, @{const less_eq (nat)},
   431     @{const Suc}, @{const plus (nat)}, @{const minus (nat)}]
   432 
   433   val mult_nat_ops =
   434     [@{const times (nat)}, @{const div (nat)}, @{const mod (nat)}]
   435 
   436   val nat_ops = simple_nat_ops @ mult_nat_ops
   437 
   438   val nat_consts = nat_ops @ [@{const numeral (nat)},
   439     @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
   440 
   441   val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
   442 
   443   val builtin_nat_ops = nat_int_coercions @ simple_nat_ops
   444 
   445   val is_nat_const = member (op aconv) nat_consts
   446 
   447   fun is_nat_const' @{const of_nat (int)} = true
   448     | is_nat_const' t = is_nat_const t
   449 
   450   val expands = map mk_meta_eq @{lemma
   451     "0 = nat 0"
   452     "1 = nat 1"
   453     "(numeral :: num => nat) = (%i. nat (numeral i))"
   454     "op < = (%a b. int a < int b)"
   455     "op <= = (%a b. int a <= int b)"
   456     "Suc = (%a. nat (int a + 1))"
   457     "op + = (%a b. nat (int a + int b))"
   458     "op - = (%a b. nat (int a - int b))"
   459     "op * = (%a b. nat (int a * int b))"
   460     "op div = (%a b. nat (int a div int b))"
   461     "op mod = (%a b. nat (int a mod int b))"
   462     by (fastforce simp add: nat_mult_distrib nat_div_distrib nat_mod_distrib)+}
   463 
   464   val ints = map mk_meta_eq @{lemma
   465     "int 0 = 0"
   466     "int 1 = 1"
   467     "int (Suc n) = int n + 1"
   468     "int (n + m) = int n + int m"
   469     "int (n - m) = int (nat (int n - int m))"
   470     "int (n * m) = int n * int m"
   471     "int (n div m) = int n div int m"
   472     "int (n mod m) = int n mod int m"
   473     by (auto simp add: int_mult zdiv_int zmod_int)}
   474 
   475   val int_if = mk_meta_eq @{lemma
   476     "int (if P then n else m) = (if P then int n else int m)"
   477     by simp}
   478 
   479   fun mk_number_eq ctxt i lhs =
   480     let
   481       val eq = SMT_Utils.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
   482       val ss = HOL_ss
   483         addsimps [@{thm Int.int_numeral}]
   484       fun tac _ = Simplifier.simp_tac (Simplifier.context ctxt ss) 1       
   485     in Goal.norm_result (Goal.prove_internal [] eq tac) end
   486 
   487   fun ite_conv cv1 cv2 =
   488     Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
   489 
   490   fun int_conv ctxt ct =
   491     (case Thm.term_of ct of
   492       @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
   493         Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
   494     | @{const of_nat (int)} $ _ =>
   495         (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
   496         (Conv.rewr_conv int_if then_conv
   497           ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
   498         Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
   499     | _ => Conv.no_conv) ct
   500 
   501   and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
   502 
   503   and expand_conv ctxt =
   504     SMT_Utils.if_conv (is_nat_const o Term.head_of)
   505       (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt)
   506       (int_conv ctxt)
   507 
   508   and nat_conv ctxt = SMT_Utils.if_exists_conv is_nat_const'
   509     (Conv.top_sweep_conv expand_conv ctxt)
   510 
   511   val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
   512 in
   513 
   514 val nat_as_int_conv = nat_conv
   515 
   516 fun add_nat_embedding thms =
   517   if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding)
   518   else (thms, [])
   519 
   520 val setup_nat_as_int =
   521   SMT_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
   522   fold (SMT_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
   523 
   524 end
   525 
   526 
   527 (** normalize numerals **)
   528 
   529 local
   530   (*
   531     rewrite negative numerals into positive numerals,
   532     rewrite Numeral0 into 0
   533     rewrite Numeral1 into 1
   534   *)
   535 
   536   fun is_strange_number ctxt (t as Const (@{const_name neg_numeral}, _) $ _) =
   537         (case try HOLogic.dest_number t of
   538           SOME (_, i) => SMT_Builtin.is_builtin_num ctxt t andalso i < 2
   539         | NONE => false)
   540     | is_strange_number _ _ = false
   541 
   542   val pos_num_ss = HOL_ss
   543     addsimps [@{thm Num.numeral_One}]
   544     addsimps [@{thm Num.neg_numeral_def}]
   545 
   546   fun norm_num_conv ctxt =
   547     SMT_Utils.if_conv (is_strange_number ctxt)
   548       (Simplifier.rewrite (Simplifier.context ctxt pos_num_ss)) Conv.no_conv
   549 in
   550 
   551 fun normalize_numerals_conv ctxt =
   552   SMT_Utils.if_exists_conv (is_strange_number ctxt)
   553     (Conv.top_sweep_conv norm_num_conv ctxt)
   554 
   555 end
   556 
   557 
   558 (** combined unfoldings and rewritings **)
   559 
   560 fun unfold_conv ctxt =
   561   rewrite_bool_case_conv ctxt then_conv
   562   unfold_abs_min_max_conv ctxt then_conv
   563   nat_as_int_conv ctxt then_conv
   564   Thm.beta_conversion true
   565 
   566 fun unfold1 ctxt = map (apsnd (Conv.fconv_rule (unfold_conv ctxt)))
   567 
   568 fun burrow_ids f ithms =
   569   let
   570     val (is, thms) = split_list ithms
   571     val (thms', extra_thms) = f thms
   572   in (is ~~ thms') @ map (pair ~1) extra_thms end
   573 
   574 fun unfold2 ctxt ithms =
   575   ithms
   576   |> map (apsnd (Conv.fconv_rule (normalize_numerals_conv ctxt)))
   577   |> burrow_ids add_nat_embedding
   578 
   579 
   580 
   581 (* overall normalization *)
   582 
   583 type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
   584 
   585 structure Extra_Norms = Generic_Data
   586 (
   587   type T = extra_norm SMT_Utils.dict
   588   val empty = []
   589   val extend = I
   590   fun merge data = SMT_Utils.dict_merge fst data
   591 )
   592 
   593 fun add_extra_norm (cs, norm) =
   594   Extra_Norms.map (SMT_Utils.dict_update (cs, norm))
   595 
   596 fun apply_extra_norms ctxt ithms =
   597   let
   598     val cs = SMT_Config.solver_class_of ctxt
   599     val es = SMT_Utils.dict_lookup (Extra_Norms.get (Context.Proof ctxt)) cs
   600   in burrow_ids (fold (fn e => e ctxt) es o rpair []) ithms end
   601 
   602 local
   603   val ignored = member (op =) [@{const_name All}, @{const_name Ex},
   604     @{const_name Let}, @{const_name If}, @{const_name HOL.eq}]
   605 
   606   val schematic_consts_of =
   607     let
   608       fun collect (@{const SMT.trigger} $ p $ t) =
   609             collect_trigger p #> collect t
   610         | collect (t $ u) = collect t #> collect u
   611         | collect (Abs (_, _, t)) = collect t
   612         | collect (t as Const (n, _)) = 
   613             if not (ignored n) then Monomorph.add_schematic_consts_of t else I
   614         | collect _ = I
   615       and collect_trigger t =
   616         let val dest = these o try HOLogic.dest_list 
   617         in fold (fold collect_pat o dest) (dest t) end
   618       and collect_pat (Const (@{const_name SMT.pat}, _) $ t) = collect t
   619         | collect_pat (Const (@{const_name SMT.nopat}, _) $ t) = collect t
   620         | collect_pat _ = I
   621     in (fn t => collect t Symtab.empty) end
   622 in
   623 
   624 fun monomorph ctxt xthms =
   625   let val (xs, thms) = split_list xthms
   626   in
   627     map (pair 1) thms
   628     |> Monomorph.monomorph schematic_consts_of ctxt
   629     |> maps (uncurry (map o pair)) o map2 pair xs o map (map snd)
   630   end
   631 
   632 end
   633 
   634 fun normalize iwthms ctxt =
   635   iwthms
   636   |> gen_normalize ctxt
   637   |> unfold1 ctxt
   638   |> monomorph ctxt
   639   |> unfold2 ctxt
   640   |> apply_extra_norms ctxt
   641   |> rpair ctxt
   642 
   643 val setup = Context.theory_map (
   644   setup_atomize #>
   645   setup_unfolded_quants #>
   646   setup_trigger #>
   647   setup_weight #>
   648   setup_bool_case #>
   649   setup_abs_min_max #>
   650   setup_nat_as_int)
   651 
   652 end