src/HOL/Tools/SMT2/smt2_normalize.ML
changeset 56078 624faeda77b5
child 56090 34bd10a9a2ad
equal deleted inserted replaced
56077:d397030fb27e 56078:624faeda77b5
       
     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_Utils.class * extra_norm -> Context.generic ->
       
    13     Context.generic
       
    14   val normalize: (int * (int option * thm)) list -> Proof.context ->
       
    15     (int * thm) list * Proof.context
       
    16 end
       
    17 
       
    18 structure SMT2_Normalize: SMT2_NORMALIZE =
       
    19 struct
       
    20 
       
    21 fun drop_fact_warning ctxt =
       
    22   SMT2_Config.verbose_msg ctxt (prefix "Warning: dropping assumption: " o
       
    23     Display.string_of_thm ctxt)
       
    24 
       
    25 
       
    26 (* general theorem normalizations *)
       
    27 
       
    28 (** instantiate elimination rules **)
       
    29  
       
    30 local
       
    31   val (cpfalse, cfalse) =
       
    32     `SMT2_Utils.mk_cprop (Thm.cterm_of @{theory} @{const False})
       
    33 
       
    34   fun inst f ct thm =
       
    35     let val cv = f (Drule.strip_imp_concl (Thm.cprop_of thm))
       
    36     in Thm.instantiate ([], [(cv, ct)]) thm end
       
    37 in
       
    38 
       
    39 fun instantiate_elim thm =
       
    40   (case Thm.concl_of thm of
       
    41     @{const Trueprop} $ Var (_, @{typ bool}) => inst Thm.dest_arg cfalse thm
       
    42   | Var _ => inst I cpfalse thm
       
    43   | _ => thm)
       
    44 
       
    45 end
       
    46 
       
    47 
       
    48 (** normalize definitions **)
       
    49 
       
    50 fun norm_def thm =
       
    51   (case Thm.prop_of thm of
       
    52     @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) =>
       
    53       norm_def (thm RS @{thm fun_cong})
       
    54   | Const (@{const_name "=="}, _) $ _ $ Abs _ =>
       
    55       norm_def (thm RS @{thm meta_eq_to_obj_eq})
       
    56   | _ => thm)
       
    57 
       
    58 
       
    59 (** atomization **)
       
    60 
       
    61 fun atomize_conv ctxt ct =
       
    62   (case Thm.term_of ct of
       
    63     @{const "==>"} $ _ $ _ =>
       
    64       Conv.binop_conv (atomize_conv ctxt) then_conv
       
    65       Conv.rewr_conv @{thm atomize_imp}
       
    66   | Const (@{const_name "=="}, _) $ _ $ _ =>
       
    67       Conv.binop_conv (atomize_conv ctxt) then_conv
       
    68       Conv.rewr_conv @{thm atomize_eq}
       
    69   | Const (@{const_name all}, _) $ Abs _ =>
       
    70       Conv.binder_conv (atomize_conv o snd) ctxt then_conv
       
    71       Conv.rewr_conv @{thm atomize_all}
       
    72   | _ => Conv.all_conv) ct
       
    73 
       
    74 val setup_atomize =
       
    75   fold SMT2_Builtin.add_builtin_fun_ext'' [@{const_name "==>"},
       
    76     @{const_name "=="}, @{const_name all}, @{const_name Trueprop}]
       
    77 
       
    78 
       
    79 (** unfold special quantifiers **)
       
    80 
       
    81 local
       
    82   val ex1_def = mk_meta_eq @{lemma
       
    83     "Ex1 = (%P. EX x. P x & (ALL y. P y --> y = x))"
       
    84     by (rule ext) (simp only: Ex1_def)}
       
    85 
       
    86   val ball_def = mk_meta_eq @{lemma "Ball = (%A P. ALL x. x : A --> P x)"
       
    87     by (rule ext)+ (rule Ball_def)}
       
    88 
       
    89   val bex_def = mk_meta_eq @{lemma "Bex = (%A P. EX x. x : A & P x)"
       
    90     by (rule ext)+ (rule Bex_def)}
       
    91 
       
    92   val special_quants = [(@{const_name Ex1}, ex1_def),
       
    93     (@{const_name Ball}, ball_def), (@{const_name Bex}, bex_def)]
       
    94   
       
    95   fun special_quant (Const (n, _)) = AList.lookup (op =) special_quants n
       
    96     | special_quant _ = NONE
       
    97 
       
    98   fun special_quant_conv _ ct =
       
    99     (case special_quant (Thm.term_of ct) of
       
   100       SOME thm => Conv.rewr_conv thm
       
   101     | NONE => Conv.all_conv) ct
       
   102 in
       
   103 
       
   104 fun unfold_special_quants_conv ctxt =
       
   105   SMT2_Utils.if_exists_conv (is_some o special_quant)
       
   106     (Conv.top_conv special_quant_conv ctxt)
       
   107 
       
   108 val setup_unfolded_quants =
       
   109   fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) special_quants
       
   110 
       
   111 end
       
   112 
       
   113 
       
   114 (** trigger inference **)
       
   115 
       
   116 local
       
   117   (*** check trigger syntax ***)
       
   118 
       
   119   fun dest_trigger (Const (@{const_name pat}, _) $ _) = SOME true
       
   120     | dest_trigger (Const (@{const_name nopat}, _) $ _) = SOME false
       
   121     | dest_trigger _ = NONE
       
   122 
       
   123   fun eq_list [] = false
       
   124     | eq_list (b :: bs) = forall (equal b) bs
       
   125 
       
   126   fun proper_trigger t =
       
   127     t
       
   128     |> these o try HOLogic.dest_list
       
   129     |> map (map_filter dest_trigger o these o try HOLogic.dest_list)
       
   130     |> (fn [] => false | bss => forall eq_list bss)
       
   131 
       
   132   fun proper_quant inside f t =
       
   133     (case t of
       
   134       Const (@{const_name All}, _) $ Abs (_, _, u) => proper_quant true f u
       
   135     | Const (@{const_name Ex}, _) $ Abs (_, _, u) => proper_quant true f u
       
   136     | @{const trigger} $ p $ u =>
       
   137         (if inside then f p else false) andalso proper_quant false f u
       
   138     | Abs (_, _, u) => proper_quant false f u
       
   139     | u1 $ u2 => proper_quant false f u1 andalso proper_quant false f u2
       
   140     | _ => true)
       
   141 
       
   142   fun check_trigger_error ctxt t =
       
   143     error ("SMT triggers must only occur under quantifier and multipatterns " ^
       
   144       "must have the same kind: " ^ Syntax.string_of_term ctxt t)
       
   145 
       
   146   fun check_trigger_conv ctxt ct =
       
   147     if proper_quant false proper_trigger (SMT2_Utils.term_of ct) then
       
   148       Conv.all_conv ct
       
   149     else check_trigger_error ctxt (Thm.term_of ct)
       
   150 
       
   151 
       
   152   (*** infer simple triggers ***)
       
   153 
       
   154   fun dest_cond_eq ct =
       
   155     (case Thm.term_of ct of
       
   156       Const (@{const_name HOL.eq}, _) $ _ $ _ => Thm.dest_binop ct
       
   157     | @{const HOL.implies} $ _ $ _ => dest_cond_eq (Thm.dest_arg ct)
       
   158     | _ => raise CTERM ("no equation", [ct]))
       
   159 
       
   160   fun get_constrs thy (Type (n, _)) = these (Datatype.get_constrs thy n)
       
   161     | get_constrs _ _ = []
       
   162 
       
   163   fun is_constr thy (n, T) =
       
   164     let fun match (m, U) = m = n andalso Sign.typ_instance thy (T, U)
       
   165     in can (the o find_first match o get_constrs thy o Term.body_type) T end
       
   166 
       
   167   fun is_constr_pat thy t =
       
   168     (case Term.strip_comb t of
       
   169       (Free _, []) => true
       
   170     | (Const c, ts) => is_constr thy c andalso forall (is_constr_pat thy) ts
       
   171     | _ => false)
       
   172 
       
   173   fun is_simp_lhs ctxt t =
       
   174     (case Term.strip_comb t of
       
   175       (Const c, ts as _ :: _) =>
       
   176         not (SMT2_Builtin.is_builtin_fun_ext ctxt c ts) andalso
       
   177         forall (is_constr_pat (Proof_Context.theory_of ctxt)) ts
       
   178     | _ => false)
       
   179 
       
   180   fun has_all_vars vs t =
       
   181     subset (op aconv) (vs, map Free (Term.add_frees t []))
       
   182 
       
   183   fun minimal_pats vs ct =
       
   184     if has_all_vars vs (Thm.term_of ct) then
       
   185       (case Thm.term_of ct of
       
   186         _ $ _ =>
       
   187           (case pairself (minimal_pats vs) (Thm.dest_comb ct) of
       
   188             ([], []) => [[ct]]
       
   189           | (ctss, ctss') => union (eq_set (op aconvc)) ctss ctss')
       
   190       | _ => [])
       
   191     else []
       
   192 
       
   193   fun proper_mpat _ _ _ [] = false
       
   194     | proper_mpat thy gen u cts =
       
   195         let
       
   196           val tps = (op ~~) (`gen (map Thm.term_of cts))
       
   197           fun some_match u = tps |> exists (fn (t', t) =>
       
   198             Pattern.matches thy (t', u) andalso not (t aconv u))
       
   199         in not (Term.exists_subterm some_match u) end
       
   200 
       
   201   val pat =
       
   202     SMT2_Utils.mk_const_pat @{theory} @{const_name SMT2.pat} SMT2_Utils.destT1
       
   203   fun mk_pat ct = Thm.apply (SMT2_Utils.instT' ct pat) ct
       
   204 
       
   205   fun mk_clist T = pairself (Thm.cterm_of @{theory})
       
   206     (HOLogic.cons_const T, HOLogic.nil_const T)
       
   207   fun mk_list (ccons, cnil) f cts = fold_rev (Thm.mk_binop ccons o f) cts cnil
       
   208   val mk_pat_list = mk_list (mk_clist @{typ SMT2.pattern})
       
   209   val mk_mpat_list = mk_list (mk_clist @{typ "SMT2.pattern list"})  
       
   210   fun mk_trigger ctss = mk_mpat_list (mk_pat_list mk_pat) ctss
       
   211 
       
   212   val trigger_eq =
       
   213     mk_meta_eq @{lemma "p = SMT2.trigger t p" by (simp add: trigger_def)}
       
   214 
       
   215   fun insert_trigger_conv [] ct = Conv.all_conv ct
       
   216     | insert_trigger_conv ctss ct =
       
   217         let val (ctr, cp) = Thm.dest_binop (Thm.rhs_of trigger_eq) ||> rpair ct
       
   218         in Thm.instantiate ([], [cp, (ctr, mk_trigger ctss)]) trigger_eq end
       
   219 
       
   220   fun infer_trigger_eq_conv outer_ctxt (ctxt, cvs) ct =
       
   221     let
       
   222       val (lhs, rhs) = dest_cond_eq ct
       
   223 
       
   224       val vs = map Thm.term_of cvs
       
   225       val thy = Proof_Context.theory_of ctxt
       
   226 
       
   227       fun get_mpats ct =
       
   228         if is_simp_lhs ctxt (Thm.term_of ct) then minimal_pats vs ct
       
   229         else []
       
   230       val gen = Variable.export_terms ctxt outer_ctxt
       
   231       val filter_mpats = filter (proper_mpat thy gen (Thm.term_of rhs))
       
   232 
       
   233     in insert_trigger_conv (filter_mpats (get_mpats lhs)) ct end
       
   234 
       
   235   fun has_trigger (@{const SMT2.trigger} $ _ $ _) = true
       
   236     | has_trigger _ = false
       
   237 
       
   238   fun try_trigger_conv cv ct =
       
   239     if SMT2_Utils.under_quant has_trigger (SMT2_Utils.term_of ct) then
       
   240       Conv.all_conv ct
       
   241     else Conv.try_conv cv ct
       
   242 
       
   243   fun infer_trigger_conv ctxt =
       
   244     if Config.get ctxt SMT2_Config.infer_triggers then
       
   245       try_trigger_conv
       
   246         (SMT2_Utils.under_quant_conv (infer_trigger_eq_conv ctxt) ctxt)
       
   247     else Conv.all_conv
       
   248 in
       
   249 
       
   250 fun trigger_conv ctxt =
       
   251   SMT2_Utils.prop_conv
       
   252     (check_trigger_conv ctxt then_conv infer_trigger_conv ctxt)
       
   253 
       
   254 val setup_trigger =
       
   255   fold SMT2_Builtin.add_builtin_fun_ext''
       
   256     [@{const_name SMT2.pat}, @{const_name SMT2.nopat}, @{const_name SMT2.trigger}]
       
   257 
       
   258 end
       
   259 
       
   260 
       
   261 (** adding quantifier weights **)
       
   262 
       
   263 local
       
   264   (*** check weight syntax ***)
       
   265 
       
   266   val has_no_weight =
       
   267     not o Term.exists_subterm (fn @{const SMT2.weight} => true | _ => false)
       
   268 
       
   269   fun is_weight (@{const SMT2.weight} $ w $ t) =
       
   270         (case try HOLogic.dest_number w of
       
   271           SOME (_, i) => i >= 0 andalso has_no_weight t
       
   272         | _ => false)
       
   273     | is_weight t = has_no_weight t
       
   274 
       
   275   fun proper_trigger (@{const SMT2.trigger} $ _ $ t) = is_weight t
       
   276     | proper_trigger t = is_weight t 
       
   277 
       
   278   fun check_weight_error ctxt t =
       
   279     error ("SMT weight must be a non-negative number and must only occur " ^
       
   280       "under the top-most quantifier and an optional trigger: " ^
       
   281       Syntax.string_of_term ctxt t)
       
   282 
       
   283   fun check_weight_conv ctxt ct =
       
   284     if SMT2_Utils.under_quant proper_trigger (SMT2_Utils.term_of ct) then
       
   285       Conv.all_conv ct
       
   286     else check_weight_error ctxt (Thm.term_of ct)
       
   287 
       
   288 
       
   289   (*** insertion of weights ***)
       
   290 
       
   291   fun under_trigger_conv cv ct =
       
   292     (case Thm.term_of ct of
       
   293       @{const SMT2.trigger} $ _ $ _ => Conv.arg_conv cv
       
   294     | _ => cv) ct
       
   295 
       
   296   val weight_eq =
       
   297     mk_meta_eq @{lemma "p = SMT2.weight i p" by (simp add: weight_def)}
       
   298   fun mk_weight_eq w =
       
   299     let val cv = Thm.dest_arg1 (Thm.rhs_of weight_eq)
       
   300     in
       
   301       Thm.instantiate ([], [(cv, Numeral.mk_cnumber @{ctyp int} w)]) weight_eq
       
   302     end
       
   303 
       
   304   fun add_weight_conv NONE _ = Conv.all_conv
       
   305     | add_weight_conv (SOME weight) ctxt =
       
   306         let val cv = Conv.rewr_conv (mk_weight_eq weight)
       
   307         in SMT2_Utils.under_quant_conv (K (under_trigger_conv cv)) ctxt end
       
   308 in
       
   309 
       
   310 fun weight_conv weight ctxt = 
       
   311   SMT2_Utils.prop_conv
       
   312     (check_weight_conv ctxt then_conv add_weight_conv weight ctxt)
       
   313 
       
   314 val setup_weight = SMT2_Builtin.add_builtin_fun_ext'' @{const_name SMT2.weight}
       
   315 
       
   316 end
       
   317 
       
   318 
       
   319 (** combined general normalizations **)
       
   320 
       
   321 fun gen_normalize1_conv ctxt weight =
       
   322   atomize_conv ctxt then_conv
       
   323   unfold_special_quants_conv ctxt then_conv
       
   324   Thm.beta_conversion true then_conv
       
   325   trigger_conv ctxt then_conv
       
   326   weight_conv weight ctxt
       
   327 
       
   328 fun gen_normalize1 ctxt weight thm =
       
   329   thm
       
   330   |> instantiate_elim
       
   331   |> norm_def
       
   332   |> Conv.fconv_rule (Thm.beta_conversion true then_conv Thm.eta_conversion)
       
   333   |> Drule.forall_intr_vars
       
   334   |> Conv.fconv_rule (gen_normalize1_conv ctxt weight)
       
   335   (* Z3 4.3.1 silently normalizes "P --> Q --> R" to "P & Q --> R" *)
       
   336   |> Raw_Simplifier.rewrite_rule ctxt @{thms HOL.imp_conjL[symmetric, THEN eq_reflection]}
       
   337 
       
   338 fun gen_norm1_safe ctxt (i, (weight, thm)) =
       
   339   (case try (gen_normalize1 ctxt weight) thm of
       
   340     SOME thm' => SOME (i, thm')
       
   341   | NONE => (drop_fact_warning ctxt thm; NONE))
       
   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_case_bool (Const (@{const_name "bool.case_bool"}, _)) = true
       
   361     | is_case_bool _ = false
       
   362 
       
   363   val thm = mk_meta_eq @{lemma
       
   364     "case_bool = (%x y P. if P then x else y)" by (rule ext)+ simp}
       
   365 
       
   366   fun unfold_conv _ =
       
   367     SMT2_Utils.if_true_conv (is_case_bool o Term.head_of)
       
   368       (expand_head_conv (Conv.rewr_conv thm))
       
   369 in
       
   370 
       
   371 fun rewrite_case_bool_conv ctxt =
       
   372   SMT2_Utils.if_exists_conv is_case_bool (Conv.top_conv unfold_conv ctxt)
       
   373 
       
   374 val setup_case_bool =
       
   375   SMT2_Builtin.add_builtin_fun_ext'' @{const_name "bool.case_bool"}
       
   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     SMT2_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   SMT2_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 (SMT2_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 = SMT2_Utils.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
       
   482       val ctxt' = put_simpset HOL_ss ctxt addsimps @{thms Int.int_numeral}
       
   483       val tac = HEADGOAL (Simplifier.simp_tac ctxt')
       
   484     in Goal.norm_result ctxt (Goal.prove_internal ctxt [] eq (K tac)) end
       
   485 
       
   486   fun ite_conv cv1 cv2 =
       
   487     Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
       
   488 
       
   489   fun int_conv ctxt ct =
       
   490     (case Thm.term_of ct of
       
   491       @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
       
   492         Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
       
   493     | @{const of_nat (int)} $ _ =>
       
   494         (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
       
   495         (Conv.rewr_conv int_if then_conv
       
   496           ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
       
   497         Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
       
   498     | _ => Conv.no_conv) ct
       
   499 
       
   500   and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
       
   501 
       
   502   and expand_conv ctxt =
       
   503     SMT2_Utils.if_conv (is_nat_const o Term.head_of)
       
   504       (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt)
       
   505       (int_conv ctxt)
       
   506 
       
   507   and nat_conv ctxt = SMT2_Utils.if_exists_conv is_nat_const' (Conv.top_sweep_conv expand_conv ctxt)
       
   508 
       
   509   val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
       
   510 in
       
   511 
       
   512 val nat_as_int_conv = nat_conv
       
   513 
       
   514 fun add_nat_embedding thms =
       
   515   if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding)
       
   516   else (thms, [])
       
   517 
       
   518 val setup_nat_as_int =
       
   519   SMT2_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
       
   520   fold (SMT2_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
       
   521 
       
   522 end
       
   523 
       
   524 
       
   525 (** normalize numerals **)
       
   526 
       
   527 local
       
   528   (*
       
   529     rewrite Numeral1 into 1
       
   530     rewrite - 0 into 0
       
   531   *)
       
   532 
       
   533   fun is_irregular_number (Const (@{const_name numeral}, _) $ Const (@{const_name num.One}, _)) =
       
   534         true
       
   535     | is_irregular_number (Const (@{const_name uminus}, _) $ Const (@{const_name Groups.zero}, _)) =
       
   536         true
       
   537     | is_irregular_number _ =
       
   538         false;
       
   539 
       
   540   fun is_strange_number ctxt t = is_irregular_number t andalso SMT2_Builtin.is_builtin_num ctxt t;
       
   541 
       
   542   val proper_num_ss =
       
   543     simpset_of (put_simpset HOL_ss @{context}
       
   544       addsimps @{thms Num.numeral_One minus_zero})
       
   545 
       
   546   fun norm_num_conv ctxt =
       
   547     SMT2_Utils.if_conv (is_strange_number ctxt)
       
   548       (Simplifier.rewrite (put_simpset proper_num_ss ctxt)) Conv.no_conv
       
   549 in
       
   550 
       
   551 fun normalize_numerals_conv ctxt =
       
   552   SMT2_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_case_bool_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 SMT2_Utils.dict
       
   588   val empty = []
       
   589   val extend = I
       
   590   fun merge data = SMT2_Utils.dict_merge fst data
       
   591 )
       
   592 
       
   593 fun add_extra_norm (cs, norm) =
       
   594   Extra_Norms.map (SMT2_Utils.dict_update (cs, norm))
       
   595 
       
   596 fun apply_extra_norms ctxt ithms =
       
   597   let
       
   598     val cs = SMT2_Config.solver_class_of ctxt
       
   599     val es = SMT2_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 SMT2.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 SMT2.pat}, _) $ t) = collect t
       
   619         | collect_pat (Const (@{const_name SMT2.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 _ = Theory.setup (Context.theory_map (
       
   644   setup_atomize #>
       
   645   setup_unfolded_quants #>
       
   646   setup_trigger #>
       
   647   setup_weight #>
       
   648   setup_case_bool #>
       
   649   setup_abs_min_max #>
       
   650   setup_nat_as_int))
       
   651 
       
   652 end