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