src/HOL/Tools/SMT/smt_normalize.ML
changeset 36898 8e55aa1306c5
child 36899 bcd6fce5bf06
equal deleted inserted replaced
36897:6d1ecdb81ff0 36898:8e55aa1306c5
       
     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   * simplify trivial distincts (those with less than three elements),
       
     6   * rewrite bool case expressions as if expressions,
       
     7   * normalize numerals (e.g. replace negative numerals by negated positive
       
     8     numerals),
       
     9   * embed natural numbers into integers,
       
    10   * add extra rules specifying types and constants which occur frequently,
       
    11   * fully translate into object logic, add universal closure,
       
    12   * lift lambda terms,
       
    13   * make applications explicit for functions with varying number of arguments.
       
    14 *)
       
    15 
       
    16 signature SMT_NORMALIZE =
       
    17 sig
       
    18   type extra_norm = thm list -> Proof.context -> thm list * Proof.context
       
    19   val normalize: extra_norm -> thm list -> Proof.context ->
       
    20     thm list * Proof.context
       
    21   val eta_expand_conv: (Proof.context -> conv) -> Proof.context -> conv
       
    22 end
       
    23 
       
    24 structure SMT_Normalize: SMT_NORMALIZE =
       
    25 struct
       
    26 
       
    27 infix 2 ??
       
    28 fun (test ?? f) x = if test x then f x else x
       
    29 
       
    30 fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct
       
    31 fun if_true_conv c cv = if_conv c cv Conv.all_conv
       
    32 
       
    33 
       
    34 
       
    35 (* simplification of trivial distincts (distinct should have at least
       
    36    three elements in the argument list) *)
       
    37 
       
    38 local
       
    39   fun is_trivial_distinct (Const (@{const_name distinct}, _) $ t) =
       
    40         length (HOLogic.dest_list t) <= 2
       
    41     | is_trivial_distinct _ = false
       
    42 
       
    43   val thms = @{lemma
       
    44     "distinct [] == True"
       
    45     "distinct [x] == True"
       
    46     "distinct [x, y] == (x ~= y)"
       
    47     by simp_all}
       
    48   fun distinct_conv _ =
       
    49     if_true_conv is_trivial_distinct (More_Conv.rewrs_conv thms)
       
    50 in
       
    51 fun trivial_distinct ctxt =
       
    52   map ((Term.exists_subterm is_trivial_distinct o Thm.prop_of) ??
       
    53     Conv.fconv_rule (More_Conv.top_conv distinct_conv ctxt))
       
    54 end
       
    55 
       
    56 
       
    57 
       
    58 (* rewrite bool case expressions as if expressions *)
       
    59 
       
    60 local
       
    61   val is_bool_case = (fn
       
    62       Const (@{const_name "bool.bool_case"}, _) $ _ $ _ $ _ => true
       
    63     | _ => false)
       
    64 
       
    65   val thms = @{lemma
       
    66     "(case P of True => x | False => y) == (if P then x else y)"
       
    67     "(case P of False => y | True => x) == (if P then x else y)"
       
    68     by (rule eq_reflection, simp)+}
       
    69   val unfold_conv = if_true_conv is_bool_case (More_Conv.rewrs_conv thms)
       
    70 in
       
    71 fun rewrite_bool_cases ctxt =
       
    72   map ((Term.exists_subterm is_bool_case o Thm.prop_of) ??
       
    73     Conv.fconv_rule (More_Conv.top_conv (K unfold_conv) ctxt))
       
    74 end
       
    75 
       
    76 
       
    77 
       
    78 (* normalization of numerals: rewriting of negative integer numerals into
       
    79    positive numerals, Numeral0 into 0, Numeral1 into 1 *)
       
    80 
       
    81 local
       
    82   fun is_number_sort ctxt T =
       
    83     Sign.of_sort (ProofContext.theory_of ctxt) (T, @{sort number_ring})
       
    84 
       
    85   fun is_strange_number ctxt (t as Const (@{const_name number_of}, _) $ _) =
       
    86         (case try HOLogic.dest_number t of
       
    87           SOME (T, i) => is_number_sort ctxt T andalso i < 2
       
    88         | NONE => false)
       
    89     | is_strange_number _ _ = false
       
    90 
       
    91   val pos_numeral_ss = HOL_ss
       
    92     addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}]
       
    93     addsimps [@{thm Int.number_of_Pls}, @{thm Int.numeral_1_eq_1}]
       
    94     addsimps @{thms Int.pred_bin_simps}
       
    95     addsimps @{thms Int.normalize_bin_simps}
       
    96     addsimps @{lemma
       
    97       "Int.Min = - Int.Bit1 Int.Pls"
       
    98       "Int.Bit0 (- Int.Pls) = - Int.Pls"
       
    99       "Int.Bit0 (- k) = - Int.Bit0 k"
       
   100       "Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)"
       
   101       by simp_all (simp add: pred_def)}
       
   102 
       
   103   fun pos_conv ctxt = if_conv (is_strange_number ctxt)
       
   104     (Simplifier.rewrite (Simplifier.context ctxt pos_numeral_ss))
       
   105     Conv.no_conv
       
   106 in
       
   107 fun normalize_numerals ctxt =
       
   108   map ((Term.exists_subterm (is_strange_number ctxt) o Thm.prop_of) ??
       
   109     Conv.fconv_rule (More_Conv.top_sweep_conv pos_conv ctxt))
       
   110 end
       
   111 
       
   112 
       
   113 
       
   114 (* embedding of standard natural number operations into integer operations *)
       
   115 
       
   116 local
       
   117   val nat_embedding = @{lemma
       
   118     "nat (int n) = n"
       
   119     "i >= 0 --> int (nat i) = i"
       
   120     "i < 0 --> int (nat i) = 0"
       
   121     by simp_all}
       
   122 
       
   123   val nat_rewriting = @{lemma
       
   124     "0 = nat 0"
       
   125     "1 = nat 1"
       
   126     "number_of i = nat (number_of i)"
       
   127     "int (nat 0) = 0"
       
   128     "int (nat 1) = 1"
       
   129     "a < b = (int a < int b)"
       
   130     "a <= b = (int a <= int b)"
       
   131     "Suc a = nat (int a + 1)"
       
   132     "a + b = nat (int a + int b)"
       
   133     "a - b = nat (int a - int b)"
       
   134     "a * b = nat (int a * int b)"
       
   135     "a div b = nat (int a div int b)"
       
   136     "a mod b = nat (int a mod int b)"
       
   137     "min a b = nat (min (int a) (int b))"
       
   138     "max a b = nat (max (int a) (int b))"
       
   139     "int (nat (int a + int b)) = int a + int b"
       
   140     "int (nat (int a * int b)) = int a * int b"
       
   141     "int (nat (int a div int b)) = int a div int b"
       
   142     "int (nat (int a mod int b)) = int a mod int b"
       
   143     "int (nat (min (int a) (int b))) = min (int a) (int b)"
       
   144     "int (nat (max (int a) (int b))) = max (int a) (int b)"
       
   145     by (simp_all add: nat_mult_distrib nat_div_distrib nat_mod_distrib
       
   146       int_mult[symmetric] zdiv_int[symmetric] zmod_int[symmetric])}
       
   147 
       
   148   fun on_positive num f x = 
       
   149     (case try HOLogic.dest_number (Thm.term_of num) of
       
   150       SOME (_, i) => if i >= 0 then SOME (f x) else NONE
       
   151     | NONE => NONE)
       
   152 
       
   153   val cancel_int_nat_ss = HOL_ss
       
   154     addsimps [@{thm Nat_Numeral.nat_number_of}]
       
   155     addsimps [@{thm Nat_Numeral.int_nat_number_of}]
       
   156     addsimps @{thms neg_simps}
       
   157 
       
   158   fun cancel_int_nat_simproc _ ss ct = 
       
   159     let
       
   160       val num = Thm.dest_arg (Thm.dest_arg ct)
       
   161       val goal = Thm.mk_binop @{cterm "op == :: int => _"} ct num
       
   162       val simpset = Simplifier.inherit_context ss cancel_int_nat_ss
       
   163       fun tac _ = Simplifier.simp_tac simpset 1
       
   164     in on_positive num (Goal.prove_internal [] goal) tac end
       
   165 
       
   166   val nat_ss = HOL_ss
       
   167     addsimps nat_rewriting
       
   168     addsimprocs [Simplifier.make_simproc {
       
   169       name = "cancel_int_nat_num", lhss = [@{cpat "int (nat _)"}],
       
   170       proc = cancel_int_nat_simproc, identifier = [] }]
       
   171 
       
   172   fun conv ctxt = Simplifier.rewrite (Simplifier.context ctxt nat_ss)
       
   173 
       
   174   val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat}))
       
   175   val uses_nat_int =
       
   176     Term.exists_subterm (member (op aconv) [@{term int}, @{term nat}])
       
   177 in
       
   178 fun nat_as_int ctxt =
       
   179   map ((uses_nat_type o Thm.prop_of) ?? Conv.fconv_rule (conv ctxt)) #>
       
   180   exists (uses_nat_int o Thm.prop_of) ?? append nat_embedding
       
   181 end
       
   182 
       
   183 
       
   184 
       
   185 (* further normalizations: beta/eta, universal closure, atomize *)
       
   186 
       
   187 val eta_expand_eq = @{lemma "f == (%x. f x)" by (rule reflexive)}
       
   188 
       
   189 fun eta_expand_conv cv ctxt =
       
   190   Conv.rewr_conv eta_expand_eq then_conv Conv.abs_conv (cv o snd) ctxt
       
   191 
       
   192 local
       
   193   val eta_conv = eta_expand_conv
       
   194 
       
   195   fun keep_conv ctxt = More_Conv.binder_conv norm_conv ctxt
       
   196   and eta_binder_conv ctxt = Conv.arg_conv (eta_conv norm_conv ctxt)
       
   197   and keep_let_conv ctxt = Conv.combination_conv
       
   198     (Conv.arg_conv (norm_conv ctxt)) (Conv.abs_conv (norm_conv o snd) ctxt)
       
   199   and unfold_let_conv ctxt = Conv.combination_conv
       
   200     (Conv.arg_conv (norm_conv ctxt)) (eta_conv norm_conv ctxt)
       
   201   and unfold_conv thm ctxt = Conv.rewr_conv thm then_conv keep_conv ctxt
       
   202   and unfold_ex1_conv ctxt = unfold_conv @{thm Ex1_def} ctxt
       
   203   and unfold_ball_conv ctxt = unfold_conv @{thm Ball_def} ctxt
       
   204   and unfold_bex_conv ctxt = unfold_conv @{thm Bex_def} ctxt
       
   205   and norm_conv ctxt ct =
       
   206     (case Thm.term_of ct of
       
   207       Const (@{const_name All}, _) $ Abs _ => keep_conv
       
   208     | Const (@{const_name All}, _) $ _ => eta_binder_conv
       
   209     | Const (@{const_name All}, _) => eta_conv eta_binder_conv
       
   210     | Const (@{const_name Ex}, _) $ Abs _ => keep_conv
       
   211     | Const (@{const_name Ex}, _) $ _ => eta_binder_conv
       
   212     | Const (@{const_name Ex}, _) => eta_conv eta_binder_conv
       
   213     | Const (@{const_name Let}, _) $ _ $ Abs _ => keep_let_conv
       
   214     | Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv
       
   215     | Const (@{const_name Let}, _) $ _ => eta_conv unfold_let_conv
       
   216     | Const (@{const_name Let}, _) => eta_conv (eta_conv unfold_let_conv)
       
   217     | Const (@{const_name Ex1}, _) $ _ => unfold_ex1_conv
       
   218     | Const (@{const_name Ex1}, _) => eta_conv unfold_ex1_conv 
       
   219     | Const (@{const_name Ball}, _) $ _ $ _ => unfold_ball_conv
       
   220     | Const (@{const_name Ball}, _) $ _ => eta_conv unfold_ball_conv
       
   221     | Const (@{const_name Ball}, _) => eta_conv (eta_conv unfold_ball_conv)
       
   222     | Const (@{const_name Bex}, _) $ _ $ _ => unfold_bex_conv
       
   223     | Const (@{const_name Bex}, _) $ _ => eta_conv unfold_bex_conv
       
   224     | Const (@{const_name Bex}, _) => eta_conv (eta_conv unfold_bex_conv)
       
   225     | Abs _ => Conv.abs_conv (norm_conv o snd)
       
   226     | _ $ _ => Conv.comb_conv o norm_conv
       
   227     | _ => K Conv.all_conv) ctxt ct
       
   228 
       
   229   fun is_normed t =
       
   230     (case t of
       
   231       Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed u
       
   232     | Const (@{const_name All}, _) $ _ => false
       
   233     | Const (@{const_name All}, _) => false
       
   234     | Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed u
       
   235     | Const (@{const_name Ex}, _) $ _ => false
       
   236     | Const (@{const_name Ex}, _) => false
       
   237     | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
       
   238         is_normed u1 andalso is_normed u2
       
   239     | Const (@{const_name Let}, _) $ _ $ _ => false
       
   240     | Const (@{const_name Let}, _) $ _ => false
       
   241     | Const (@{const_name Let}, _) => false
       
   242     | Const (@{const_name Ex1}, _) => false
       
   243     | Const (@{const_name Ball}, _) => false
       
   244     | Const (@{const_name Bex}, _) => false
       
   245     | Abs (_, _, u) => is_normed u
       
   246     | u1 $ u2 => is_normed u1 andalso is_normed u2
       
   247     | _ => true)
       
   248 in
       
   249 fun norm_binder_conv ctxt = if_conv is_normed Conv.all_conv (norm_conv ctxt)
       
   250 end
       
   251 
       
   252 fun norm_def ctxt thm =
       
   253   (case Thm.prop_of thm of
       
   254     @{term Trueprop} $ (Const (@{const_name "op ="}, _) $ _ $ Abs _) =>
       
   255       norm_def ctxt (thm RS @{thm fun_cong})
       
   256   | Const (@{const_name "=="}, _) $ _ $ Abs _ =>
       
   257       norm_def ctxt (thm RS @{thm meta_eq_to_obj_eq})
       
   258   | _ => thm)
       
   259 
       
   260 fun atomize_conv ctxt ct =
       
   261   (case Thm.term_of ct of
       
   262     @{term "op ==>"} $ _ $ _ =>
       
   263       Conv.binop_conv (atomize_conv ctxt) then_conv
       
   264       Conv.rewr_conv @{thm atomize_imp}
       
   265   | Const (@{const_name "=="}, _) $ _ $ _ =>
       
   266       Conv.binop_conv (atomize_conv ctxt) then_conv
       
   267       Conv.rewr_conv @{thm atomize_eq}
       
   268   | Const (@{const_name all}, _) $ Abs _ =>
       
   269       More_Conv.binder_conv atomize_conv ctxt then_conv
       
   270       Conv.rewr_conv @{thm atomize_all}
       
   271   | _ => Conv.all_conv) ct
       
   272 
       
   273 fun normalize_rule ctxt =
       
   274   Conv.fconv_rule (
       
   275     (* reduce lambda abstractions, except at known binders: *)
       
   276     Thm.beta_conversion true then_conv
       
   277     Thm.eta_conversion then_conv
       
   278     norm_binder_conv ctxt) #>
       
   279   norm_def ctxt #>
       
   280   Drule.forall_intr_vars #>
       
   281   Conv.fconv_rule (atomize_conv ctxt)
       
   282 
       
   283 
       
   284 
       
   285 (* lift lambda terms into additional rules *)
       
   286 
       
   287 local
       
   288   val meta_eq = @{cpat "op =="}
       
   289   val meta_eqT = hd (Thm.dest_ctyp (Thm.ctyp_of_term meta_eq))
       
   290   fun inst_meta cT = Thm.instantiate_cterm ([(meta_eqT, cT)], []) meta_eq
       
   291   fun mk_meta_eq ct cu = Thm.mk_binop (inst_meta (Thm.ctyp_of_term ct)) ct cu
       
   292 
       
   293   fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt)
       
   294 
       
   295   fun used_vars cvs ct =
       
   296     let
       
   297       val lookup = AList.lookup (op aconv) (map (` Thm.term_of) cvs)
       
   298       val add = (fn SOME ct => insert (op aconvc) ct | _ => I)
       
   299     in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
       
   300 
       
   301   fun apply cv thm = 
       
   302     let val thm' = Thm.combination thm (Thm.reflexive cv)
       
   303     in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end
       
   304   fun apply_def cvs eq = Thm.symmetric (fold apply cvs eq)
       
   305 
       
   306   fun replace_lambda cvs ct (cx as (ctxt, defs)) =
       
   307     let
       
   308       val cvs' = used_vars cvs ct
       
   309       val ct' = fold_rev Thm.cabs cvs' ct
       
   310     in
       
   311       (case Termtab.lookup defs (Thm.term_of ct') of
       
   312         SOME eq => (apply_def cvs' eq, cx)
       
   313       | NONE =>
       
   314           let
       
   315             val {T, ...} = Thm.rep_cterm ct' and n = Name.uu
       
   316             val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt
       
   317             val cu = mk_meta_eq (cert ctxt (Free (n', T))) ct'
       
   318             val (eq, ctxt'') = yield_singleton Assumption.add_assumes cu ctxt'
       
   319             val defs' = Termtab.update (Thm.term_of ct', eq) defs
       
   320           in (apply_def cvs' eq, (ctxt'', defs')) end)
       
   321     end
       
   322 
       
   323   fun none ct cx = (Thm.reflexive ct, cx)
       
   324   fun in_comb f g ct cx =
       
   325     let val (cu1, cu2) = Thm.dest_comb ct
       
   326     in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
       
   327   fun in_arg f = in_comb none f
       
   328   fun in_abs f cvs ct (ctxt, defs) =
       
   329     let
       
   330       val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt
       
   331       val (cv, cu) = Thm.dest_abs (SOME n) ct
       
   332     in  (ctxt', defs) |> f (cv :: cvs) cu |>> Thm.abstract_rule n cv end
       
   333 
       
   334   fun traverse cvs ct =
       
   335     (case Thm.term_of ct of
       
   336       Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs traverse cvs)
       
   337     | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs traverse cvs)
       
   338     | Const (@{const_name Let}, _) $ _ $ Abs _ =>
       
   339         in_comb (in_arg (traverse cvs)) (in_abs traverse cvs)
       
   340     | Abs _ => at_lambda cvs
       
   341     | _ $ _ => in_comb (traverse cvs) (traverse cvs)
       
   342     | _ => none) ct
       
   343 
       
   344   and at_lambda cvs ct =
       
   345     in_abs traverse cvs ct #-> (fn thm =>
       
   346     replace_lambda cvs (Thm.rhs_of thm) #>> Thm.transitive thm)
       
   347 
       
   348   fun has_free_lambdas t =
       
   349     (case t of
       
   350       Const (@{const_name All}, _) $ Abs (_, _, u) => has_free_lambdas u
       
   351     | Const (@{const_name Ex}, _) $ Abs (_, _, u) => has_free_lambdas u
       
   352     | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
       
   353         has_free_lambdas u1 orelse has_free_lambdas u2
       
   354     | Abs _ => true
       
   355     | u1 $ u2 => has_free_lambdas u1 orelse has_free_lambdas u2
       
   356     | _ => false)
       
   357 
       
   358   fun lift_lm f thm cx =
       
   359     if not (has_free_lambdas (Thm.prop_of thm)) then (thm, cx)
       
   360     else cx |> f (Thm.cprop_of thm) |>> (fn thm' => Thm.equal_elim thm' thm)
       
   361 in
       
   362 fun lift_lambdas thms ctxt =
       
   363   let
       
   364     val cx = (ctxt, Termtab.empty)
       
   365     val (thms', (ctxt', defs)) = fold_map (lift_lm (traverse [])) thms cx
       
   366     val eqs = Termtab.fold (cons o normalize_rule ctxt' o snd) defs []
       
   367   in (eqs @ thms', ctxt') end
       
   368 end
       
   369 
       
   370 
       
   371 
       
   372 (* make application explicit for functions with varying number of arguments *)
       
   373 
       
   374 local
       
   375   val const = prefix "c" and free = prefix "f"
       
   376   fun min i (e as (_, j)) = if i <> j then (true, Int.min (i, j)) else e
       
   377   fun add t i = Symtab.map_default (t, (false, i)) (min i)
       
   378   fun traverse t =
       
   379     (case Term.strip_comb t of
       
   380       (Const (n, _), ts) => add (const n) (length ts) #> fold traverse ts 
       
   381     | (Free (n, _), ts) => add (free n) (length ts) #> fold traverse ts
       
   382     | (Abs (_, _, u), ts) => fold traverse (u :: ts)
       
   383     | (_, ts) => fold traverse ts)
       
   384   val prune = (fn (n, (true, i)) => Symtab.update (n, i) | _ => I)
       
   385   fun prune_tab tab = Symtab.fold prune tab Symtab.empty
       
   386 
       
   387   fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
       
   388   fun nary_conv conv1 conv2 ct =
       
   389     (Conv.combination_conv (nary_conv conv1 conv2) conv2 else_conv conv1) ct
       
   390   fun abs_conv conv tb = Conv.abs_conv (fn (cv, cx) =>
       
   391     let val n = fst (Term.dest_Free (Thm.term_of cv))
       
   392     in conv (Symtab.update (free n, 0) tb) cx end)
       
   393   val apply_rule = @{lemma "f x == apply f x" by (simp add: apply_def)}
       
   394 in
       
   395 fun explicit_application ctxt thms =
       
   396   let
       
   397     fun sub_conv tb ctxt ct =
       
   398       (case Term.strip_comb (Thm.term_of ct) of
       
   399         (Const (n, _), ts) => app_conv tb (const n) (length ts) ctxt
       
   400       | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt
       
   401       | (Abs _, _) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt)
       
   402       | (_, _) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct
       
   403     and app_conv tb n i ctxt =
       
   404       (case Symtab.lookup tb n of
       
   405         NONE => nary_conv Conv.all_conv (sub_conv tb ctxt)
       
   406       | SOME j => apply_conv tb ctxt (i - j))
       
   407     and apply_conv tb ctxt i ct = (
       
   408       if i = 0 then nary_conv Conv.all_conv (sub_conv tb ctxt)
       
   409       else
       
   410         Conv.rewr_conv apply_rule then_conv
       
   411         binop_conv (apply_conv tb ctxt (i-1)) (sub_conv tb ctxt)) ct
       
   412 
       
   413     fun needs_exp_app tab = Term.exists_subterm (fn
       
   414         Bound _ $ _ => true
       
   415       | Const (n, _) => Symtab.defined tab (const n)
       
   416       | Free (n, _) => Symtab.defined tab (free n)
       
   417       | _ => false)
       
   418 
       
   419     fun rewrite tab ctxt thm =
       
   420       if not (needs_exp_app tab (Thm.prop_of thm)) then thm
       
   421       else Conv.fconv_rule (sub_conv tab ctxt) thm
       
   422 
       
   423     val tab = prune_tab (fold (traverse o Thm.prop_of) thms Symtab.empty)
       
   424   in map (rewrite tab ctxt) thms end
       
   425 end
       
   426 
       
   427 
       
   428 
       
   429 (* combined normalization *)
       
   430 
       
   431 type extra_norm = thm list -> Proof.context -> thm list * Proof.context
       
   432 
       
   433 fun with_context f thms ctxt = (f ctxt thms, ctxt)
       
   434 
       
   435 fun normalize extra_norm thms ctxt =
       
   436   thms
       
   437   |> trivial_distinct ctxt
       
   438   |> rewrite_bool_cases ctxt
       
   439   |> normalize_numerals ctxt
       
   440   |> nat_as_int ctxt
       
   441   |> rpair ctxt
       
   442   |-> extra_norm
       
   443   |-> with_context (fn cx => map (normalize_rule cx))
       
   444   |-> SMT_Monomorph.monomorph
       
   445   |-> lift_lambdas
       
   446   |-> with_context explicit_application
       
   447 
       
   448 end