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