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