src/HOL/Tools/SMT/smt_normalize.ML
author boehmes
Mon Nov 08 12:13:44 2010 +0100 (2010-11-08)
changeset 40424 7550b2cba1cb
parent 40279 96365b4ae7b6
child 40579 98ebd2300823
permissions -rw-r--r--
better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
     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   * monomorphize (create instances of schematic rules),
    13   * lift lambda terms,
    14   * make applications explicit for functions with varying number of arguments.
    15   * add (hypothetical definitions for) missing datatype selectors,
    16 *)
    17 
    18 signature SMT_NORMALIZE =
    19 sig
    20   type extra_norm = bool -> (int * thm) list -> Proof.context ->
    21     (int * thm) list * Proof.context
    22   val normalize: extra_norm -> bool -> (int * thm) list -> Proof.context ->
    23     (int * thm) list * Proof.context
    24   val atomize_conv: Proof.context -> conv
    25   val eta_expand_conv: (Proof.context -> conv) -> Proof.context -> conv
    26 end
    27 
    28 structure SMT_Normalize: SMT_NORMALIZE =
    29 struct
    30 
    31 infix 2 ??
    32 fun (test ?? f) x = if test x then f x else x
    33 
    34 fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct
    35 fun if_true_conv c cv = if_conv c cv Conv.all_conv
    36 
    37 
    38 
    39 (* simplification of trivial distincts (distinct should have at least
    40    three elements in the argument list) *)
    41 
    42 local
    43   fun is_trivial_distinct (Const (@{const_name SMT.distinct}, _) $ t) =
    44        (length (HOLogic.dest_list t) <= 2
    45         handle TERM _ => error ("SMT: constant " ^
    46           quote @{const_name SMT.distinct} ^ " must be applied to " ^
    47           "an explicit list."))
    48     | is_trivial_distinct _ = false
    49 
    50   val thms = map mk_meta_eq @{lemma
    51     "SMT.distinct [] = True"
    52     "SMT.distinct [x] = True"
    53     "SMT.distinct [x, y] = (x ~= y)"
    54     by (simp_all add: distinct_def)}
    55   fun distinct_conv _ =
    56     if_true_conv is_trivial_distinct (Conv.rewrs_conv thms)
    57 in
    58 fun trivial_distinct ctxt =
    59   map (apsnd ((Term.exists_subterm is_trivial_distinct o Thm.prop_of) ??
    60     Conv.fconv_rule (Conv.top_conv distinct_conv ctxt)))
    61 end
    62 
    63 
    64 
    65 (* rewrite bool case expressions as if expressions *)
    66 
    67 local
    68   val is_bool_case = (fn
    69       Const (@{const_name "bool.bool_case"}, _) $ _ $ _ $ _ => true
    70     | _ => false)
    71 
    72   val thm = mk_meta_eq @{lemma
    73     "(case P of True => x | False => y) = (if P then x else y)" by simp}
    74   val unfold_conv = if_true_conv is_bool_case (Conv.rewr_conv thm)
    75 in
    76 fun rewrite_bool_cases ctxt =
    77   map (apsnd ((Term.exists_subterm is_bool_case o Thm.prop_of) ??
    78     Conv.fconv_rule (Conv.top_conv (K unfold_conv) ctxt)))
    79 end
    80 
    81 
    82 
    83 (* normalization of numerals: rewriting of negative integer numerals into
    84    positive numerals, Numeral0 into 0, Numeral1 into 1 *)
    85 
    86 local
    87   fun is_number_sort ctxt T =
    88     Sign.of_sort (ProofContext.theory_of ctxt) (T, @{sort number_ring})
    89 
    90   fun is_strange_number ctxt (t as Const (@{const_name number_of}, _) $ _) =
    91         (case try HOLogic.dest_number t of
    92           SOME (T, i) => is_number_sort ctxt T andalso i < 2
    93         | NONE => false)
    94     | is_strange_number _ _ = false
    95 
    96   val pos_numeral_ss = HOL_ss
    97     addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}]
    98     addsimps [@{thm Int.number_of_Pls}, @{thm Int.numeral_1_eq_1}]
    99     addsimps @{thms Int.pred_bin_simps}
   100     addsimps @{thms Int.normalize_bin_simps}
   101     addsimps @{lemma
   102       "Int.Min = - Int.Bit1 Int.Pls"
   103       "Int.Bit0 (- Int.Pls) = - Int.Pls"
   104       "Int.Bit0 (- k) = - Int.Bit0 k"
   105       "Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)"
   106       by simp_all (simp add: pred_def)}
   107 
   108   fun pos_conv ctxt = if_conv (is_strange_number ctxt)
   109     (Simplifier.rewrite (Simplifier.context ctxt pos_numeral_ss))
   110     Conv.no_conv
   111 in
   112 fun normalize_numerals ctxt =
   113   map (apsnd ((Term.exists_subterm (is_strange_number ctxt) o Thm.prop_of) ??
   114     Conv.fconv_rule (Conv.top_sweep_conv pos_conv ctxt)))
   115 end
   116 
   117 
   118 
   119 (* embedding of standard natural number operations into integer operations *)
   120 
   121 local
   122   val nat_embedding = map (pair ~1) @{lemma
   123     "nat (int n) = n"
   124     "i >= 0 --> int (nat i) = i"
   125     "i < 0 --> int (nat i) = 0"
   126     by simp_all}
   127 
   128   val nat_rewriting = @{lemma
   129     "0 = nat 0"
   130     "1 = nat 1"
   131     "(number_of :: int => nat) = (%i. nat (number_of i))"
   132     "int (nat 0) = 0"
   133     "int (nat 1) = 1"
   134     "op < = (%a b. int a < int b)"
   135     "op <= = (%a b. int a <= int b)"
   136     "Suc = (%a. nat (int a + 1))"
   137     "op + = (%a b. nat (int a + int b))"
   138     "op - = (%a b. nat (int a - int b))"
   139     "op * = (%a b. nat (int a * int b))"
   140     "op div = (%a b. nat (int a div int b))"
   141     "op mod = (%a b. nat (int a mod int b))"
   142     "min = (%a b. nat (min (int a) (int b)))"
   143     "max = (%a b. nat (max (int a) (int b)))"
   144     "int (nat (int a + int b)) = int a + int b"
   145     "int (nat (int a + 1)) = int a + 1"  (* special rule due to Suc above *)
   146     "int (nat (int a * int b)) = int a * int b"
   147     "int (nat (int a div int b)) = int a div int b"
   148     "int (nat (int a mod int b)) = int a mod int b"
   149     "int (nat (min (int a) (int b))) = min (int a) (int b)"
   150     "int (nat (max (int a) (int b))) = max (int a) (int b)"
   151     by (auto intro!: ext simp add: nat_mult_distrib nat_div_distrib
   152       nat_mod_distrib int_mult[symmetric] zdiv_int[symmetric]
   153       zmod_int[symmetric])}
   154 
   155   fun on_positive num f x = 
   156     (case try HOLogic.dest_number (Thm.term_of num) of
   157       SOME (_, i) => if i >= 0 then SOME (f x) else NONE
   158     | NONE => NONE)
   159 
   160   val cancel_int_nat_ss = HOL_ss
   161     addsimps [@{thm Nat_Numeral.nat_number_of}]
   162     addsimps [@{thm Nat_Numeral.int_nat_number_of}]
   163     addsimps @{thms neg_simps}
   164 
   165   fun cancel_int_nat_simproc _ ss ct = 
   166     let
   167       val num = Thm.dest_arg (Thm.dest_arg ct)
   168       val goal = Thm.mk_binop @{cterm "op == :: int => _"} ct num
   169       val simpset = Simplifier.inherit_context ss cancel_int_nat_ss
   170       fun tac _ = Simplifier.simp_tac simpset 1
   171     in on_positive num (Goal.prove_internal [] goal) tac end
   172 
   173   val nat_ss = HOL_ss
   174     addsimps nat_rewriting
   175     addsimprocs [
   176       Simplifier.make_simproc {
   177         name = "cancel_int_nat_num", lhss = [@{cpat "int (nat _)"}],
   178         proc = cancel_int_nat_simproc, identifier = [] }]
   179 
   180   fun conv ctxt = Simplifier.rewrite (Simplifier.context ctxt nat_ss)
   181 
   182   val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat}))
   183   val uses_nat_int =
   184     Term.exists_subterm (member (op aconv) [@{term int}, @{term nat}])
   185 in
   186 fun nat_as_int ctxt =
   187   map (apsnd ((uses_nat_type o Thm.prop_of) ?? Conv.fconv_rule (conv ctxt))) #>
   188   exists (uses_nat_int o Thm.prop_of o snd) ?? append nat_embedding
   189 end
   190 
   191 
   192 
   193 (* further normalizations: beta/eta, universal closure, atomize *)
   194 
   195 val eta_expand_eq = @{lemma "f == (%x. f x)" by (rule reflexive)}
   196 
   197 fun eta_expand_conv cv ctxt =
   198   Conv.rewr_conv eta_expand_eq then_conv Conv.abs_conv (cv o snd) ctxt
   199 
   200 local
   201   val eta_conv = eta_expand_conv
   202 
   203   fun args_conv cv ct =
   204     (case Thm.term_of ct of
   205       _ $ _ => Conv.combination_conv (args_conv cv) cv
   206     | _ => Conv.all_conv) ct
   207 
   208   fun eta_args_conv cv 0 = args_conv o cv
   209     | eta_args_conv cv i = eta_conv (eta_args_conv cv (i-1))
   210 
   211   fun keep_conv ctxt = Conv.binder_conv (norm_conv o snd) ctxt
   212   and eta_binder_conv ctxt = Conv.arg_conv (eta_conv norm_conv ctxt)
   213   and keep_let_conv ctxt = Conv.combination_conv
   214     (Conv.arg_conv (norm_conv ctxt)) (Conv.abs_conv (norm_conv o snd) ctxt)
   215   and unfold_let_conv ctxt = Conv.combination_conv
   216     (Conv.arg_conv (norm_conv ctxt)) (eta_conv norm_conv ctxt)
   217   and unfold_conv thm ctxt = Conv.rewr_conv thm then_conv keep_conv ctxt
   218   and unfold_ex1_conv ctxt = unfold_conv @{thm Ex1_def} ctxt
   219   and unfold_ball_conv ctxt = unfold_conv (mk_meta_eq @{thm Ball_def}) ctxt
   220   and unfold_bex_conv ctxt = unfold_conv (mk_meta_eq @{thm Bex_def}) ctxt
   221   and norm_conv ctxt ct =
   222     (case Thm.term_of ct of
   223       Const (@{const_name All}, _) $ Abs _ => keep_conv
   224     | Const (@{const_name All}, _) $ _ => eta_binder_conv
   225     | Const (@{const_name All}, _) => eta_conv eta_binder_conv
   226     | Const (@{const_name Ex}, _) $ Abs _ => keep_conv
   227     | Const (@{const_name Ex}, _) $ _ => eta_binder_conv
   228     | Const (@{const_name Ex}, _) => eta_conv eta_binder_conv
   229     | Const (@{const_name Let}, _) $ _ $ Abs _ => keep_let_conv
   230     | Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv
   231     | Const (@{const_name Let}, _) $ _ => eta_conv unfold_let_conv
   232     | Const (@{const_name Let}, _) => eta_conv (eta_conv unfold_let_conv)
   233     | Const (@{const_name Ex1}, _) $ _ => unfold_ex1_conv
   234     | Const (@{const_name Ex1}, _) => eta_conv unfold_ex1_conv 
   235     | Const (@{const_name Ball}, _) $ _ $ _ => unfold_ball_conv
   236     | Const (@{const_name Ball}, _) $ _ => eta_conv unfold_ball_conv
   237     | Const (@{const_name Ball}, _) => eta_conv (eta_conv unfold_ball_conv)
   238     | Const (@{const_name Bex}, _) $ _ $ _ => unfold_bex_conv
   239     | Const (@{const_name Bex}, _) $ _ => eta_conv unfold_bex_conv
   240     | Const (@{const_name Bex}, _) => eta_conv (eta_conv unfold_bex_conv)
   241     | Abs _ => Conv.abs_conv (norm_conv o snd)
   242     | _ =>
   243         (case Term.strip_comb (Thm.term_of ct) of
   244           (Const (c as (_, T)), ts) =>
   245             if SMT_Builtin.is_builtin ctxt c
   246             then eta_args_conv norm_conv
   247               (length (Term.binder_types T) - length ts)
   248             else args_conv o norm_conv
   249         | _ => args_conv o norm_conv)) ctxt ct
   250 
   251   fun is_normed ctxt t =
   252     (case t of
   253       Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed ctxt u
   254     | Const (@{const_name All}, _) $ _ => false
   255     | Const (@{const_name All}, _) => false
   256     | Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed ctxt u
   257     | Const (@{const_name Ex}, _) $ _ => false
   258     | Const (@{const_name Ex}, _) => false
   259     | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
   260         is_normed ctxt u1 andalso is_normed ctxt u2
   261     | Const (@{const_name Let}, _) $ _ $ _ => false
   262     | Const (@{const_name Let}, _) $ _ => false
   263     | Const (@{const_name Let}, _) => false
   264     | Const (@{const_name Ex1}, _) $ _ => false
   265     | Const (@{const_name Ex1}, _) => false
   266     | Const (@{const_name Ball}, _) $ _ $ _ => false
   267     | Const (@{const_name Ball}, _) $ _ => false
   268     | Const (@{const_name Ball}, _) => false
   269     | Const (@{const_name Bex}, _) $ _ $ _ => false
   270     | Const (@{const_name Bex}, _) $ _ => false
   271     | Const (@{const_name Bex}, _) => false
   272     | Abs (_, _, u) => is_normed ctxt u
   273     | _ =>
   274         (case Term.strip_comb t of
   275           (Const (c as (_, T)), ts) =>
   276             if SMT_Builtin.is_builtin ctxt c
   277             then length (Term.binder_types T) = length ts andalso
   278               forall (is_normed ctxt) ts
   279             else forall (is_normed ctxt) ts
   280         | (_, ts) => forall (is_normed ctxt) ts))
   281 in
   282 fun norm_binder_conv ctxt =
   283   if_conv (is_normed ctxt) Conv.all_conv (norm_conv ctxt)
   284 end
   285 
   286 fun norm_def ctxt thm =
   287   (case Thm.prop_of thm of
   288     @{term Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) =>
   289       norm_def ctxt (thm RS @{thm fun_cong})
   290   | Const (@{const_name "=="}, _) $ _ $ Abs _ =>
   291       norm_def ctxt (thm RS @{thm meta_eq_to_obj_eq})
   292   | _ => thm)
   293 
   294 fun atomize_conv ctxt ct =
   295   (case Thm.term_of ct of
   296     @{term "op ==>"} $ _ $ _ =>
   297       Conv.binop_conv (atomize_conv ctxt) then_conv
   298       Conv.rewr_conv @{thm atomize_imp}
   299   | Const (@{const_name "=="}, _) $ _ $ _ =>
   300       Conv.binop_conv (atomize_conv ctxt) then_conv
   301       Conv.rewr_conv @{thm atomize_eq}
   302   | Const (@{const_name all}, _) $ Abs _ =>
   303       Conv.binder_conv (atomize_conv o snd) ctxt then_conv
   304       Conv.rewr_conv @{thm atomize_all}
   305   | _ => Conv.all_conv) ct
   306 
   307 fun normalize_rule ctxt =
   308   Conv.fconv_rule (
   309     (* reduce lambda abstractions, except at known binders: *)
   310     Thm.beta_conversion true then_conv
   311     Thm.eta_conversion then_conv
   312     norm_binder_conv ctxt) #>
   313   norm_def ctxt #>
   314   Drule.forall_intr_vars #>
   315   Conv.fconv_rule (atomize_conv ctxt)
   316 
   317 
   318 
   319 (* lift lambda terms into additional rules *)
   320 
   321 local
   322   val meta_eq = @{cpat "op =="}
   323   val meta_eqT = hd (Thm.dest_ctyp (Thm.ctyp_of_term meta_eq))
   324   fun inst_meta cT = Thm.instantiate_cterm ([(meta_eqT, cT)], []) meta_eq
   325   fun mk_meta_eq ct cu = Thm.mk_binop (inst_meta (Thm.ctyp_of_term ct)) ct cu
   326 
   327   fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt)
   328 
   329   fun used_vars cvs ct =
   330     let
   331       val lookup = AList.lookup (op aconv) (map (` Thm.term_of) cvs)
   332       val add = (fn SOME ct => insert (op aconvc) ct | _ => I)
   333     in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
   334 
   335   fun apply cv thm = 
   336     let val thm' = Thm.combination thm (Thm.reflexive cv)
   337     in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end
   338   fun apply_def cvs eq = Thm.symmetric (fold apply cvs eq)
   339 
   340   fun replace_lambda cvs ct (cx as (ctxt, defs)) =
   341     let
   342       val cvs' = used_vars cvs ct
   343       val ct' = fold_rev Thm.cabs cvs' ct
   344     in
   345       (case Termtab.lookup defs (Thm.term_of ct') of
   346         SOME eq => (apply_def cvs' eq, cx)
   347       | NONE =>
   348           let
   349             val {T, ...} = Thm.rep_cterm ct' and n = Name.uu
   350             val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt
   351             val cu = mk_meta_eq (cert ctxt (Free (n', T))) ct'
   352             val (eq, ctxt'') = yield_singleton Assumption.add_assumes cu ctxt'
   353             val defs' = Termtab.update (Thm.term_of ct', eq) defs
   354           in (apply_def cvs' eq, (ctxt'', defs')) end)
   355     end
   356 
   357   fun none ct cx = (Thm.reflexive ct, cx)
   358   fun in_comb f g ct cx =
   359     let val (cu1, cu2) = Thm.dest_comb ct
   360     in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
   361   fun in_arg f = in_comb none f
   362   fun in_abs f cvs ct (ctxt, defs) =
   363     let
   364       val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt
   365       val (cv, cu) = Thm.dest_abs (SOME n) ct
   366     in  (ctxt', defs) |> f (cv :: cvs) cu |>> Thm.abstract_rule n cv end
   367 
   368   fun traverse cvs ct =
   369     (case Thm.term_of ct of
   370       Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs traverse cvs)
   371     | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs traverse cvs)
   372     | Const (@{const_name Let}, _) $ _ $ Abs _ =>
   373         in_comb (in_arg (traverse cvs)) (in_abs traverse cvs)
   374     | Abs _ => at_lambda cvs
   375     | _ $ _ => in_comb (traverse cvs) (traverse cvs)
   376     | _ => none) ct
   377 
   378   and at_lambda cvs ct =
   379     in_abs traverse cvs ct #-> (fn thm =>
   380     replace_lambda cvs (Thm.rhs_of thm) #>> Thm.transitive thm)
   381 
   382   fun has_free_lambdas t =
   383     (case t of
   384       Const (@{const_name All}, _) $ Abs (_, _, u) => has_free_lambdas u
   385     | Const (@{const_name Ex}, _) $ Abs (_, _, u) => has_free_lambdas u
   386     | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
   387         has_free_lambdas u1 orelse has_free_lambdas u2
   388     | Abs _ => true
   389     | u1 $ u2 => has_free_lambdas u1 orelse has_free_lambdas u2
   390     | _ => false)
   391 
   392   fun lift_lm f thm cx =
   393     if not (has_free_lambdas (Thm.prop_of thm)) then (thm, cx)
   394     else cx |> f (Thm.cprop_of thm) |>> (fn thm' => Thm.equal_elim thm' thm)
   395 in
   396 fun lift_lambdas irules ctxt =
   397   let
   398     val cx = (ctxt, Termtab.empty)
   399     val (idxs, thms) = split_list irules
   400     val (thms', (ctxt', defs)) = fold_map (lift_lm (traverse [])) thms cx
   401     val eqs = Termtab.fold (cons o normalize_rule ctxt' o snd) defs []
   402   in (map (pair ~1) eqs @ (idxs ~~ thms'), ctxt') end
   403 end
   404 
   405 
   406 
   407 (* make application explicit for functions with varying number of arguments *)
   408 
   409 local
   410   val const = prefix "c" and free = prefix "f"
   411   fun min i (e as (_, j)) = if i <> j then (true, Int.min (i, j)) else e
   412   fun add t i = Symtab.map_default (t, (false, i)) (min i)
   413   fun traverse t =
   414     (case Term.strip_comb t of
   415       (Const (n, _), ts) => add (const n) (length ts) #> fold traverse ts 
   416     | (Free (n, _), ts) => add (free n) (length ts) #> fold traverse ts
   417     | (Abs (_, _, u), ts) => fold traverse (u :: ts)
   418     | (_, ts) => fold traverse ts)
   419   fun prune tab = Symtab.fold (fn (n, (true, i)) =>
   420     Symtab.update (n, i) | _ => I) tab Symtab.empty
   421 
   422   fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
   423   fun nary_conv conv1 conv2 ct =
   424     (Conv.combination_conv (nary_conv conv1 conv2) conv2 else_conv conv1) ct
   425   fun abs_conv conv tb = Conv.abs_conv (fn (cv, cx) =>
   426     let val n = fst (Term.dest_Free (Thm.term_of cv))
   427     in conv (Symtab.update (free n, 0) tb) cx end)
   428   val fun_app_rule = @{lemma "f x == fun_app f x" by (simp add: fun_app_def)}
   429 in
   430 fun explicit_application ctxt irules =
   431   let
   432     fun sub_conv tb ctxt ct =
   433       (case Term.strip_comb (Thm.term_of ct) of
   434         (Const (n, _), ts) => app_conv tb (const n) (length ts) ctxt
   435       | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt
   436       | (Abs _, _) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt)
   437       | (_, _) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct
   438     and app_conv tb n i ctxt =
   439       (case Symtab.lookup tb n of
   440         NONE => nary_conv Conv.all_conv (sub_conv tb ctxt)
   441       | SOME j => fun_app_conv tb ctxt (i - j))
   442     and fun_app_conv tb ctxt i ct = (
   443       if i = 0 then nary_conv Conv.all_conv (sub_conv tb ctxt)
   444       else
   445         Conv.rewr_conv fun_app_rule then_conv
   446         binop_conv (fun_app_conv tb ctxt (i-1)) (sub_conv tb ctxt)) ct
   447 
   448     fun needs_exp_app tab = Term.exists_subterm (fn
   449         Bound _ $ _ => true
   450       | Const (n, _) => Symtab.defined tab (const n)
   451       | Free (n, _) => Symtab.defined tab (free n)
   452       | _ => false)
   453 
   454     fun rewrite tab ctxt thm =
   455       if not (needs_exp_app tab (Thm.prop_of thm)) then thm
   456       else Conv.fconv_rule (sub_conv tab ctxt) thm
   457 
   458     val tab = prune (fold (traverse o Thm.prop_of o snd) irules Symtab.empty)
   459   in map (apsnd (rewrite tab ctxt)) irules end
   460 end
   461 
   462 
   463 
   464 (* add missing datatype selectors via hypothetical definitions *)
   465 
   466 local
   467   val add = (fn Type (n, _) => Symtab.update (n, ()) | _ => I)
   468 
   469   fun collect t =
   470     (case Term.strip_comb t of
   471       (Abs (_, T, t), _) => add T #> collect t
   472     | (Const (_, T), ts) => collects T ts
   473     | (Free (_, T), ts) => collects T ts
   474     | _ => I)
   475   and collects T ts =
   476     let val ((Ts, Us), U) = Term.strip_type T |> apfst (chop (length ts))
   477     in fold add Ts #> add (Us ---> U) #> fold collect ts end
   478 
   479   fun add_constructors thy n =
   480     (case Datatype.get_info thy n of
   481       NONE => I
   482     | SOME {descr, ...} => fold (fn (_, (_, _, cs)) => fold (fn (n, ds) =>
   483         fold (insert (op =) o pair n) (1 upto length ds)) cs) descr)
   484 
   485   fun add_selector (c as (n, i)) ctxt =
   486     (case Datatype_Selectors.lookup_selector ctxt c of
   487       SOME _ => ctxt
   488     | NONE =>
   489         let
   490           val T = Sign.the_const_type (ProofContext.theory_of ctxt) n
   491           val U = Term.body_type T --> nth (Term.binder_types T) (i-1)
   492         in
   493           ctxt
   494           |> yield_singleton Variable.variant_fixes Name.uu
   495           |>> pair ((n, T), i) o rpair U
   496           |-> Context.proof_map o Datatype_Selectors.add_selector
   497         end)
   498 in
   499 
   500 fun datatype_selectors irules ctxt =
   501   let
   502     val ns = Symtab.keys (fold (collect o Thm.prop_of o snd) irules Symtab.empty)
   503     val cs = fold (add_constructors (ProofContext.theory_of ctxt)) ns []
   504   in (irules, fold add_selector cs ctxt) end
   505     (* FIXME: also generate hypothetical definitions for the selectors *)
   506 
   507 end
   508 
   509 
   510 
   511 (* combined normalization *)
   512 
   513 type extra_norm = bool -> (int * thm) list -> Proof.context ->
   514   (int * thm) list * Proof.context
   515 
   516 fun with_context f irules ctxt = (f ctxt irules, ctxt)
   517 
   518 fun normalize extra_norm with_datatypes irules ctxt =
   519   let
   520     fun norm f ctxt' (i, thm) =
   521       if Config.get ctxt' SMT_Config.drop_bad_facts then
   522         (case try (f ctxt') thm of
   523           SOME thm' => SOME (i, thm')
   524         | NONE => (SMT_Config.verbose_msg ctxt' (prefix ("Warning: " ^
   525             "dropping assumption: ") o Display.string_of_thm ctxt') thm; NONE))
   526       else SOME (i, f ctxt' thm)
   527   in
   528     irules
   529     |> trivial_distinct ctxt
   530     |> rewrite_bool_cases ctxt
   531     |> normalize_numerals ctxt
   532     |> nat_as_int ctxt
   533     |> rpair ctxt
   534     |-> extra_norm with_datatypes
   535     |-> with_context (map_filter o norm normalize_rule)
   536     |-> SMT_Monomorph.monomorph
   537     |-> lift_lambdas
   538     |-> with_context explicit_application
   539     |-> (if with_datatypes then datatype_selectors else pair)
   540   end
   541 
   542 end