src/Pure/drule.ML
author wenzelm
Wed May 29 18:52:35 2013 +0200 (2013-05-29)
changeset 52224 6ba76ad4e679
parent 52223 5bb6ae8acb87
child 52465 4970437fe092
permissions -rw-r--r--
more precise "incremented" indication, which might be relevant in corner cases, e.g. instantiation of leading to vars with different types (which is a potential problem nonetheless);
wenzelm@252
     1
(*  Title:      Pure/drule.ML
wenzelm@252
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     3
wenzelm@3766
     4
Derived rules and other operations on theorems.
clasohm@0
     5
*)
clasohm@0
     6
wenzelm@46470
     7
infix 0 RS RSN RL RLN MRS OF COMP INCR_COMP COMP_INCR;
clasohm@0
     8
wenzelm@5903
     9
signature BASIC_DRULE =
wenzelm@3766
    10
sig
wenzelm@18179
    11
  val mk_implies: cterm * cterm -> cterm
wenzelm@18179
    12
  val list_implies: cterm list * cterm -> cterm
wenzelm@18179
    13
  val strip_imp_prems: cterm -> cterm list
wenzelm@18179
    14
  val strip_imp_concl: cterm -> cterm
wenzelm@18179
    15
  val cprems_of: thm -> cterm list
wenzelm@18179
    16
  val cterm_fun: (term -> term) -> (cterm -> cterm)
wenzelm@18179
    17
  val ctyp_fun: (typ -> typ) -> (ctyp -> ctyp)
wenzelm@18179
    18
  val forall_intr_list: cterm list -> thm -> thm
wenzelm@18179
    19
  val forall_intr_vars: thm -> thm
wenzelm@18179
    20
  val forall_elim_list: cterm list -> thm -> thm
wenzelm@18179
    21
  val gen_all: thm -> thm
wenzelm@18179
    22
  val lift_all: cterm -> thm -> thm
wenzelm@18179
    23
  val implies_elim_list: thm -> thm list -> thm
wenzelm@18179
    24
  val implies_intr_list: cterm list -> thm -> thm
wenzelm@43333
    25
  val instantiate_normalize: (ctyp * ctyp) list * (cterm * cterm) list -> thm -> thm
wenzelm@21603
    26
  val zero_var_indexes_list: thm list -> thm list
wenzelm@18179
    27
  val zero_var_indexes: thm -> thm
wenzelm@18179
    28
  val implies_intr_hyps: thm -> thm
wenzelm@18179
    29
  val rotate_prems: int -> thm -> thm
wenzelm@18179
    30
  val rearrange_prems: int list -> thm -> thm
wenzelm@18179
    31
  val RSN: thm * (int * thm) -> thm
wenzelm@18179
    32
  val RS: thm * thm -> thm
wenzelm@18179
    33
  val RLN: thm list * (int * thm list) -> thm list
wenzelm@18179
    34
  val RL: thm list * thm list -> thm list
wenzelm@18179
    35
  val MRS: thm list * thm -> thm
wenzelm@18179
    36
  val OF: thm * thm list -> thm
wenzelm@18179
    37
  val compose: thm * int * thm -> thm list
wenzelm@18179
    38
  val COMP: thm * thm -> thm
wenzelm@21578
    39
  val INCR_COMP: thm * thm -> thm
wenzelm@21578
    40
  val COMP_INCR: thm * thm -> thm
wenzelm@46186
    41
  val cterm_instantiate: (cterm * cterm) list -> thm -> thm
wenzelm@18179
    42
  val size_of_thm: thm -> int
wenzelm@18179
    43
  val reflexive_thm: thm
wenzelm@18179
    44
  val symmetric_thm: thm
wenzelm@18179
    45
  val transitive_thm: thm
wenzelm@18179
    46
  val extensional: thm -> thm
wenzelm@18179
    47
  val asm_rl: thm
wenzelm@18179
    48
  val cut_rl: thm
wenzelm@18179
    49
  val revcut_rl: thm
wenzelm@18179
    50
  val thin_rl: thm
wenzelm@18179
    51
  val instantiate': ctyp option list -> cterm option list -> thm -> thm
wenzelm@5903
    52
end;
wenzelm@5903
    53
wenzelm@5903
    54
signature DRULE =
wenzelm@5903
    55
sig
wenzelm@5903
    56
  include BASIC_DRULE
wenzelm@19999
    57
  val generalize: string list * string list -> thm -> thm
paulson@15949
    58
  val list_comb: cterm * cterm list -> cterm
berghofe@12908
    59
  val strip_comb: cterm -> cterm * cterm list
berghofe@15262
    60
  val strip_type: ctyp -> ctyp list * ctyp
paulson@15949
    61
  val beta_conv: cterm -> cterm -> cterm
wenzelm@27156
    62
  val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
berghofe@17713
    63
  val flexflex_unique: thm -> thm
wenzelm@35021
    64
  val export_without_context: thm -> thm
wenzelm@35021
    65
  val export_without_context_open: thm -> thm
wenzelm@33277
    66
  val store_thm: binding -> thm -> thm
wenzelm@33277
    67
  val store_standard_thm: binding -> thm -> thm
wenzelm@33277
    68
  val store_thm_open: binding -> thm -> thm
wenzelm@33277
    69
  val store_standard_thm_open: binding -> thm -> thm
wenzelm@47427
    70
  val multi_resolve: thm list -> thm -> thm Seq.seq
wenzelm@47427
    71
  val multi_resolves: thm list -> thm list -> thm Seq.seq
wenzelm@11975
    72
  val compose_single: thm * int * thm -> thm
wenzelm@46186
    73
  val equals_cong: thm
wenzelm@46186
    74
  val imp_cong: thm
wenzelm@46186
    75
  val swap_prems_eq: thm
wenzelm@18468
    76
  val imp_cong_rule: thm -> thm -> thm
wenzelm@22939
    77
  val arg_cong_rule: cterm -> thm -> thm
wenzelm@23568
    78
  val binop_cong_rule: cterm -> thm -> thm -> thm
wenzelm@22939
    79
  val fun_cong_rule: thm -> cterm -> thm
skalberg@15001
    80
  val beta_eta_conversion: cterm -> thm
berghofe@15925
    81
  val eta_long_conversion: cterm -> thm
paulson@20861
    82
  val eta_contraction_rule: thm -> thm
wenzelm@11975
    83
  val norm_hhf_eq: thm
wenzelm@28618
    84
  val norm_hhf_eqs: thm list
wenzelm@12800
    85
  val is_norm_hhf: term -> bool
wenzelm@16425
    86
  val norm_hhf: theory -> term -> term
wenzelm@20298
    87
  val norm_hhf_cterm: cterm -> cterm
wenzelm@18025
    88
  val protect: cterm -> cterm
wenzelm@18025
    89
  val protectI: thm
wenzelm@18025
    90
  val protectD: thm
wenzelm@18179
    91
  val protect_cong: thm
wenzelm@18025
    92
  val implies_intr_protected: cterm list -> thm -> thm
wenzelm@19775
    93
  val termI: thm
wenzelm@19775
    94
  val mk_term: cterm -> thm
wenzelm@19775
    95
  val dest_term: thm -> cterm
wenzelm@21519
    96
  val cterm_rule: (thm -> thm) -> cterm -> cterm
wenzelm@24005
    97
  val dummy_thm: thm
wenzelm@28618
    98
  val sort_constraintI: thm
wenzelm@28618
    99
  val sort_constraint_eq: thm
wenzelm@23423
   100
  val with_subgoal: int -> (thm -> thm) -> thm -> thm
wenzelm@29344
   101
  val comp_no_flatten: thm * int -> int -> thm -> thm
berghofe@14081
   102
  val rename_bvars: (string * string) list -> thm -> thm
berghofe@14081
   103
  val rename_bvars': string option list -> thm -> thm
wenzelm@19124
   104
  val incr_indexes: thm -> thm -> thm
wenzelm@19124
   105
  val incr_indexes2: thm -> thm -> thm -> thm
wenzelm@46186
   106
  val triv_forall_equality: thm
wenzelm@46186
   107
  val distinct_prems_rl: thm
wenzelm@46186
   108
  val equal_intr_rule: thm
wenzelm@46186
   109
  val equal_elim_rule1: thm
wenzelm@46186
   110
  val equal_elim_rule2: thm
wenzelm@12297
   111
  val remdups_rl: thm
berghofe@13325
   112
  val abs_def: thm -> thm
wenzelm@3766
   113
end;
clasohm@0
   114
wenzelm@5903
   115
structure Drule: DRULE =
clasohm@0
   116
struct
clasohm@0
   117
wenzelm@3991
   118
wenzelm@16682
   119
(** some cterm->cterm operations: faster than calling cterm_of! **)
lcp@708
   120
lcp@708
   121
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
paulson@2004
   122
fun strip_imp_prems ct =
wenzelm@22906
   123
  let val (cA, cB) = Thm.dest_implies ct
wenzelm@20579
   124
  in cA :: strip_imp_prems cB end
wenzelm@20579
   125
  handle TERM _ => [];
lcp@708
   126
paulson@2004
   127
(* A1==>...An==>B  goes to B, where B is not an implication *)
paulson@2004
   128
fun strip_imp_concl ct =
wenzelm@20579
   129
  (case Thm.term_of ct of
wenzelm@20579
   130
    Const ("==>", _) $ _ $ _ => strip_imp_concl (Thm.dest_arg ct)
wenzelm@20579
   131
  | _ => ct);
paulson@2004
   132
lcp@708
   133
(*The premises of a theorem, as a cterm list*)
berghofe@13659
   134
val cprems_of = strip_imp_prems o cprop_of;
lcp@708
   135
wenzelm@26627
   136
fun cterm_fun f ct = Thm.cterm_of (Thm.theory_of_cterm ct) (f (Thm.term_of ct));
wenzelm@26627
   137
fun ctyp_fun f cT = Thm.ctyp_of (Thm.theory_of_ctyp cT) (f (Thm.typ_of cT));
berghofe@15797
   138
wenzelm@26487
   139
fun certify t = Thm.cterm_of (Context.the_theory (Context.the_thread_data ())) t;
paulson@9547
   140
wenzelm@27333
   141
val implies = certify Logic.implies;
wenzelm@46497
   142
fun mk_implies (A, B) = Thm.apply (Thm.apply implies A) B;
paulson@9547
   143
paulson@9547
   144
(*cterm version of list_implies: [A1,...,An], B  goes to [|A1;==>;An|]==>B *)
paulson@9547
   145
fun list_implies([], B) = B
paulson@9547
   146
  | list_implies(A::AS, B) = mk_implies (A, list_implies(AS,B));
paulson@9547
   147
paulson@15949
   148
(*cterm version of list_comb: maps  (f, [t1,...,tn])  to  f(t1,...,tn) *)
paulson@15949
   149
fun list_comb (f, []) = f
wenzelm@46497
   150
  | list_comb (f, t::ts) = list_comb (Thm.apply f t, ts);
paulson@15949
   151
berghofe@12908
   152
(*cterm version of strip_comb: maps  f(t1,...,tn)  to  (f, [t1,...,tn]) *)
wenzelm@18179
   153
fun strip_comb ct =
berghofe@12908
   154
  let
berghofe@12908
   155
    fun stripc (p as (ct, cts)) =
berghofe@12908
   156
      let val (ct1, ct2) = Thm.dest_comb ct
berghofe@12908
   157
      in stripc (ct1, ct2 :: cts) end handle CTERM _ => p
berghofe@12908
   158
  in stripc (ct, []) end;
berghofe@12908
   159
berghofe@15262
   160
(* cterm version of strip_type: maps  [T1,...,Tn]--->T  to   ([T1,T2,...,Tn], T) *)
berghofe@15262
   161
fun strip_type cT = (case Thm.typ_of cT of
berghofe@15262
   162
    Type ("fun", _) =>
berghofe@15262
   163
      let
berghofe@15262
   164
        val [cT1, cT2] = Thm.dest_ctyp cT;
berghofe@15262
   165
        val (cTs, cT') = strip_type cT2
berghofe@15262
   166
      in (cT1 :: cTs, cT') end
berghofe@15262
   167
  | _ => ([], cT));
berghofe@15262
   168
paulson@15949
   169
(*Beta-conversion for cterms, where x is an abstraction. Simply returns the rhs
paulson@15949
   170
  of the meta-equality returned by the beta_conversion rule.*)
wenzelm@18179
   171
fun beta_conv x y =
wenzelm@46497
   172
  Thm.dest_arg (cprop_of (Thm.beta_conversion false (Thm.apply x y)));
paulson@15949
   173
wenzelm@15875
   174
lcp@708
   175
wenzelm@252
   176
(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
clasohm@0
   177
     Used for establishing default types (of variables) and sorts (of
clasohm@0
   178
     type variables) when reading another term.
clasohm@0
   179
     Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
clasohm@0
   180
***)
clasohm@0
   181
clasohm@0
   182
fun types_sorts thm =
wenzelm@20329
   183
  let
wenzelm@22695
   184
    val vars = Thm.fold_terms Term.add_vars thm [];
wenzelm@22695
   185
    val frees = Thm.fold_terms Term.add_frees thm [];
wenzelm@22695
   186
    val tvars = Thm.fold_terms Term.add_tvars thm [];
wenzelm@22695
   187
    val tfrees = Thm.fold_terms Term.add_tfrees thm [];
wenzelm@20329
   188
    fun types (a, i) =
wenzelm@20329
   189
      if i < 0 then AList.lookup (op =) frees a else AList.lookup (op =) vars (a, i);
wenzelm@20329
   190
    fun sorts (a, i) =
wenzelm@20329
   191
      if i < 0 then AList.lookup (op =) tfrees a else AList.lookup (op =) tvars (a, i);
wenzelm@20329
   192
  in (types, sorts) end;
clasohm@0
   193
wenzelm@15669
   194
wenzelm@7636
   195
wenzelm@9455
   196
clasohm@0
   197
(** Standardization of rules **)
clasohm@0
   198
wenzelm@19730
   199
(*Generalization over a list of variables*)
wenzelm@36944
   200
val forall_intr_list = fold_rev Thm.forall_intr;
clasohm@0
   201
wenzelm@18535
   202
(*Generalization over Vars -- canonical order*)
wenzelm@18535
   203
fun forall_intr_vars th =
wenzelm@36944
   204
  fold Thm.forall_intr
wenzelm@22695
   205
    (map (Thm.cterm_of (Thm.theory_of_thm th) o Var) (Thm.fold_terms Term.add_vars th [])) th;
wenzelm@18535
   206
wenzelm@18025
   207
fun outer_params t =
wenzelm@20077
   208
  let val vs = Term.strip_all_vars t
wenzelm@20077
   209
  in Name.variant_list [] (map (Name.clean o #1) vs) ~~ map #2 vs end;
wenzelm@18025
   210
wenzelm@18025
   211
(*generalize outermost parameters*)
wenzelm@18025
   212
fun gen_all th =
wenzelm@12719
   213
  let
wenzelm@26627
   214
    val thy = Thm.theory_of_thm th;
wenzelm@26627
   215
    val {prop, maxidx, ...} = Thm.rep_thm th;
wenzelm@18025
   216
    val cert = Thm.cterm_of thy;
wenzelm@18025
   217
    fun elim (x, T) = Thm.forall_elim (cert (Var ((x, maxidx + 1), T)));
wenzelm@18025
   218
  in fold elim (outer_params prop) th end;
wenzelm@18025
   219
wenzelm@18025
   220
(*lift vars wrt. outermost goal parameters
wenzelm@18118
   221
  -- reverses the effect of gen_all modulo higher-order unification*)
wenzelm@18025
   222
fun lift_all goal th =
wenzelm@18025
   223
  let
wenzelm@18025
   224
    val thy = Theory.merge (Thm.theory_of_cterm goal, Thm.theory_of_thm th);
wenzelm@18025
   225
    val cert = Thm.cterm_of thy;
wenzelm@19421
   226
    val maxidx = Thm.maxidx_of th;
wenzelm@18025
   227
    val ps = outer_params (Thm.term_of goal)
wenzelm@18025
   228
      |> map (fn (x, T) => Var ((x, maxidx + 1), Logic.incr_tvar (maxidx + 1) T));
wenzelm@18025
   229
    val Ts = map Term.fastype_of ps;
wenzelm@22695
   230
    val inst = Thm.fold_terms Term.add_vars th [] |> map (fn (xi, T) =>
wenzelm@18025
   231
      (cert (Var (xi, T)), cert (Term.list_comb (Var (xi, Ts ---> T), ps))));
wenzelm@18025
   232
  in
wenzelm@18025
   233
    th |> Thm.instantiate ([], inst)
wenzelm@18025
   234
    |> fold_rev (Thm.forall_intr o cert) ps
wenzelm@18025
   235
  end;
wenzelm@18025
   236
wenzelm@19999
   237
(*direct generalization*)
wenzelm@19999
   238
fun generalize names th = Thm.generalize names (Thm.maxidx_of th + 1) th;
wenzelm@9554
   239
wenzelm@16949
   240
(*specialization over a list of cterms*)
wenzelm@36944
   241
val forall_elim_list = fold Thm.forall_elim;
clasohm@0
   242
wenzelm@16949
   243
(*maps A1,...,An |- B  to  [| A1;...;An |] ==> B*)
wenzelm@36944
   244
val implies_intr_list = fold_rev Thm.implies_intr;
clasohm@0
   245
wenzelm@16949
   246
(*maps [| A1;...;An |] ==> B and [A1,...,An]  to  B*)
wenzelm@24978
   247
fun implies_elim_list impth ths = fold Thm.elim_implies ths impth;
clasohm@0
   248
clasohm@0
   249
(*Reset Var indexes to zero, renaming to preserve distinctness*)
wenzelm@21603
   250
fun zero_var_indexes_list [] = []
wenzelm@21603
   251
  | zero_var_indexes_list ths =
wenzelm@21603
   252
      let
wenzelm@21603
   253
        val thy = Theory.merge_list (map Thm.theory_of_thm ths);
wenzelm@21603
   254
        val certT = Thm.ctyp_of thy and cert = Thm.cterm_of thy;
wenzelm@31977
   255
        val (instT, inst) = Term_Subst.zero_var_indexes_inst (map Thm.full_prop_of ths);
wenzelm@21603
   256
        val cinstT = map (fn (v, T) => (certT (TVar v), certT T)) instT;
wenzelm@21603
   257
        val cinst = map (fn (v, t) => (cert (Var v), cert t)) inst;
wenzelm@21603
   258
      in map (Thm.adjust_maxidx_thm ~1 o Thm.instantiate (cinstT, cinst)) ths end;
wenzelm@21603
   259
wenzelm@21603
   260
val zero_var_indexes = singleton zero_var_indexes_list;
clasohm@0
   261
clasohm@0
   262
paulson@14394
   263
(** Standard form of object-rule: no hypotheses, flexflex constraints,
paulson@14394
   264
    Frees, or outer quantifiers; all generality expressed by Vars of index 0.**)
wenzelm@10515
   265
wenzelm@16595
   266
(*Discharge all hypotheses.*)
wenzelm@16595
   267
fun implies_intr_hyps th =
wenzelm@16595
   268
  fold Thm.implies_intr (#hyps (Thm.crep_thm th)) th;
wenzelm@16595
   269
paulson@14394
   270
(*Squash a theorem's flexflex constraints provided it can be done uniquely.
paulson@14394
   271
  This step can lose information.*)
paulson@14387
   272
fun flexflex_unique th =
wenzelm@38709
   273
  if null (Thm.tpairs_of th) then th else
wenzelm@36944
   274
    case distinct Thm.eq_thm (Seq.list_of (Thm.flexflex_rule th)) of
paulson@23439
   275
      [th] => th
paulson@23439
   276
    | []   => raise THM("flexflex_unique: impossible constraints", 0, [th])
paulson@23439
   277
    |  _   => raise THM("flexflex_unique: multiple unifiers", 0, [th]);
paulson@14387
   278
wenzelm@21603
   279
wenzelm@35021
   280
(* old-style export without context *)
wenzelm@21603
   281
wenzelm@35021
   282
val export_without_context_open =
wenzelm@16949
   283
  implies_intr_hyps
wenzelm@35985
   284
  #> Thm.forall_intr_frees
wenzelm@19421
   285
  #> `Thm.maxidx_of
wenzelm@16949
   286
  #-> (fn maxidx =>
wenzelm@26653
   287
    Thm.forall_elim_vars (maxidx + 1)
wenzelm@20904
   288
    #> Thm.strip_shyps
wenzelm@16949
   289
    #> zero_var_indexes
wenzelm@35845
   290
    #> Thm.varifyT_global);
wenzelm@1218
   291
wenzelm@35021
   292
val export_without_context =
wenzelm@21600
   293
  flexflex_unique
wenzelm@35021
   294
  #> export_without_context_open
wenzelm@26627
   295
  #> Thm.close_derivation;
berghofe@11512
   296
clasohm@0
   297
paulson@7248
   298
(*Rotates a rule's premises to the left by k*)
wenzelm@23537
   299
fun rotate_prems 0 = I
wenzelm@31945
   300
  | rotate_prems k = Thm.permute_prems 0 k;
wenzelm@23537
   301
wenzelm@23423
   302
fun with_subgoal i f = rotate_prems (i - 1) #> f #> rotate_prems (1 - i);
paulson@4610
   303
wenzelm@31945
   304
(*Permute prems, where the i-th position in the argument list (counting from 0)
wenzelm@31945
   305
  gives the position within the original thm to be transferred to position i.
wenzelm@31945
   306
  Any remaining trailing positions are left unchanged.*)
wenzelm@31945
   307
val rearrange_prems =
wenzelm@31945
   308
  let
wenzelm@31945
   309
    fun rearr new [] thm = thm
wenzelm@31945
   310
      | rearr new (p :: ps) thm =
wenzelm@31945
   311
          rearr (new + 1)
wenzelm@31945
   312
            (map (fn q => if new <= q andalso q < p then q + 1 else q) ps)
wenzelm@31945
   313
            (Thm.permute_prems (new + 1) (new - p) (Thm.permute_prems new (p - new) thm))
oheimb@11163
   314
  in rearr 0 end;
paulson@4610
   315
wenzelm@47427
   316
wenzelm@47427
   317
(*Resolution: multiple arguments, multiple results*)
wenzelm@47427
   318
local
wenzelm@47427
   319
  fun res th i rule =
wenzelm@47427
   320
    Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
clasohm@0
   321
wenzelm@47427
   322
  fun multi_res _ [] rule = Seq.single rule
wenzelm@47427
   323
    | multi_res i (th :: ths) rule = Seq.maps (res th i) (multi_res (i + 1) ths rule);
wenzelm@47427
   324
in
wenzelm@47427
   325
  val multi_resolve = multi_res 1;
wenzelm@47427
   326
  fun multi_resolves facts rules = Seq.maps (multi_resolve facts) (Seq.of_list rules);
wenzelm@47427
   327
end;
wenzelm@47427
   328
wenzelm@47427
   329
(*Resolution: exactly one resolvent must be produced*)
wenzelm@47427
   330
fun tha RSN (i, thb) =
wenzelm@47427
   331
  (case Seq.chop 2 (Thm.biresolution false [(false, tha)] i thb) of
wenzelm@47427
   332
    ([th], _) => th
wenzelm@47427
   333
  | ([], _) => raise THM ("RSN: no unifiers", i, [tha, thb])
wenzelm@47427
   334
  | _ => raise THM ("RSN: multiple unifiers", i, [tha, thb]));
wenzelm@47427
   335
wenzelm@47427
   336
(*Resolution: P==>Q, Q==>R gives P==>R*)
clasohm@0
   337
fun tha RS thb = tha RSN (1,thb);
clasohm@0
   338
clasohm@0
   339
(*For joining lists of rules*)
wenzelm@47427
   340
fun thas RLN (i, thbs) =
wenzelm@31945
   341
  let val resolve = Thm.biresolution false (map (pair false) thas) i
wenzelm@4270
   342
      fun resb thb = Seq.list_of (resolve thb) handle THM _ => []
wenzelm@19482
   343
  in maps resb thbs end;
clasohm@0
   344
wenzelm@47427
   345
fun thas RL thbs = thas RLN (1, thbs);
wenzelm@47427
   346
wenzelm@47427
   347
(*Isar-style multi-resolution*)
wenzelm@47427
   348
fun bottom_rl OF rls =
wenzelm@47427
   349
  (case Seq.chop 2 (multi_resolve rls bottom_rl) of
wenzelm@47427
   350
    ([th], _) => th
wenzelm@47427
   351
  | ([], _) => raise THM ("OF: no unifiers", 0, bottom_rl :: rls)
wenzelm@47427
   352
  | _ => raise THM ("OF: multiple unifiers", 0, bottom_rl :: rls));
clasohm@0
   353
lcp@11
   354
(*Resolve a list of rules against bottom_rl from right to left;
lcp@11
   355
  makes proof trees*)
wenzelm@47427
   356
fun rls MRS bottom_rl = bottom_rl OF rls;
wenzelm@9288
   357
wenzelm@252
   358
(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
clasohm@0
   359
  with no lifting or renaming!  Q may contain ==> or meta-quants
clasohm@0
   360
  ALWAYS deletes premise i *)
wenzelm@252
   361
fun compose(tha,i,thb) =
wenzelm@52223
   362
  distinct Thm.eq_thm
wenzelm@52223
   363
    (Seq.list_of
wenzelm@52223
   364
      (Thm.bicompose {flatten = true, match = false, incremented = false} (false, tha, 0) i thb));
clasohm@0
   365
wenzelm@6946
   366
fun compose_single (tha,i,thb) =
wenzelm@47427
   367
  (case compose (tha,i,thb) of
wenzelm@6946
   368
    [th] => th
wenzelm@47427
   369
  | _ => raise THM ("compose: unique result expected", i, [tha,thb]));
wenzelm@6946
   370
wenzelm@13105
   371
wenzelm@4016
   372
(** theorem equality **)
clasohm@0
   373
clasohm@0
   374
(*Useful "distance" function for BEST_FIRST*)
wenzelm@16720
   375
val size_of_thm = size_of_term o Thm.full_prop_of;
clasohm@0
   376
lcp@1194
   377
lcp@1194
   378
clasohm@0
   379
(*** Meta-Rewriting Rules ***)
clasohm@0
   380
wenzelm@33384
   381
val read_prop = certify o Simple_Syntax.read_prop;
wenzelm@26487
   382
wenzelm@26487
   383
fun store_thm name th =
wenzelm@39557
   384
  Context.>>> (Context.map_theory_result (Global_Theory.store_thm (name, th)));
paulson@4610
   385
wenzelm@26487
   386
fun store_thm_open name th =
wenzelm@39557
   387
  Context.>>> (Context.map_theory_result (Global_Theory.store_thm_open (name, th)));
wenzelm@26487
   388
wenzelm@35021
   389
fun store_standard_thm name th = store_thm name (export_without_context th);
wenzelm@35021
   390
fun store_standard_thm_open name thm = store_thm_open name (export_without_context_open thm);
wenzelm@4016
   391
clasohm@0
   392
val reflexive_thm =
wenzelm@26487
   393
  let val cx = certify (Var(("x",0),TVar(("'a",0),[])))
wenzelm@33277
   394
  in store_standard_thm_open (Binding.name "reflexive") (Thm.reflexive cx) end;
clasohm@0
   395
clasohm@0
   396
val symmetric_thm =
wenzelm@33277
   397
  let
wenzelm@33277
   398
    val xy = read_prop "x::'a == y::'a";
wenzelm@33277
   399
    val thm = Thm.implies_intr xy (Thm.symmetric (Thm.assume xy));
wenzelm@33277
   400
  in store_standard_thm_open (Binding.name "symmetric") thm end;
clasohm@0
   401
clasohm@0
   402
val transitive_thm =
wenzelm@33277
   403
  let
wenzelm@33277
   404
    val xy = read_prop "x::'a == y::'a";
wenzelm@33277
   405
    val yz = read_prop "y::'a == z::'a";
wenzelm@33277
   406
    val xythm = Thm.assume xy;
wenzelm@33277
   407
    val yzthm = Thm.assume yz;
wenzelm@33277
   408
    val thm = Thm.implies_intr yz (Thm.transitive xythm yzthm);
wenzelm@33277
   409
  in store_standard_thm_open (Binding.name "transitive") thm end;
clasohm@0
   410
berghofe@11512
   411
fun extensional eq =
berghofe@11512
   412
  let val eq' =
wenzelm@36944
   413
    Thm.abstract_rule "x" (Thm.dest_arg (fst (Thm.dest_equals (cprop_of eq)))) eq
wenzelm@36944
   414
  in Thm.equal_elim (Thm.eta_conversion (cprop_of eq')) eq' end;
berghofe@11512
   415
wenzelm@18820
   416
val equals_cong =
wenzelm@33277
   417
  store_standard_thm_open (Binding.name "equals_cong")
wenzelm@33277
   418
    (Thm.reflexive (read_prop "x::'a == y::'a"));
wenzelm@18820
   419
berghofe@10414
   420
val imp_cong =
berghofe@10414
   421
  let
wenzelm@24241
   422
    val ABC = read_prop "A ==> B::prop == C::prop"
wenzelm@24241
   423
    val AB = read_prop "A ==> B"
wenzelm@24241
   424
    val AC = read_prop "A ==> C"
wenzelm@24241
   425
    val A = read_prop "A"
berghofe@10414
   426
  in
wenzelm@36944
   427
    store_standard_thm_open (Binding.name "imp_cong") (Thm.implies_intr ABC (Thm.equal_intr
wenzelm@36944
   428
      (Thm.implies_intr AB (Thm.implies_intr A
wenzelm@36944
   429
        (Thm.equal_elim (Thm.implies_elim (Thm.assume ABC) (Thm.assume A))
wenzelm@36944
   430
          (Thm.implies_elim (Thm.assume AB) (Thm.assume A)))))
wenzelm@36944
   431
      (Thm.implies_intr AC (Thm.implies_intr A
wenzelm@36944
   432
        (Thm.equal_elim (Thm.symmetric (Thm.implies_elim (Thm.assume ABC) (Thm.assume A)))
wenzelm@36944
   433
          (Thm.implies_elim (Thm.assume AC) (Thm.assume A)))))))
berghofe@10414
   434
  end;
berghofe@10414
   435
berghofe@10414
   436
val swap_prems_eq =
berghofe@10414
   437
  let
wenzelm@24241
   438
    val ABC = read_prop "A ==> B ==> C"
wenzelm@24241
   439
    val BAC = read_prop "B ==> A ==> C"
wenzelm@24241
   440
    val A = read_prop "A"
wenzelm@24241
   441
    val B = read_prop "B"
berghofe@10414
   442
  in
wenzelm@33277
   443
    store_standard_thm_open (Binding.name "swap_prems_eq")
wenzelm@36944
   444
      (Thm.equal_intr
wenzelm@36944
   445
        (Thm.implies_intr ABC (Thm.implies_intr B (Thm.implies_intr A
wenzelm@36944
   446
          (Thm.implies_elim (Thm.implies_elim (Thm.assume ABC) (Thm.assume A)) (Thm.assume B)))))
wenzelm@36944
   447
        (Thm.implies_intr BAC (Thm.implies_intr A (Thm.implies_intr B
wenzelm@36944
   448
          (Thm.implies_elim (Thm.implies_elim (Thm.assume BAC) (Thm.assume B)) (Thm.assume A))))))
berghofe@10414
   449
  end;
lcp@229
   450
wenzelm@22938
   451
val imp_cong_rule = Thm.combination o Thm.combination (Thm.reflexive implies);
wenzelm@22938
   452
wenzelm@23537
   453
fun arg_cong_rule ct th = Thm.combination (Thm.reflexive ct) th;    (*AP_TERM in LCF/HOL*)
wenzelm@23537
   454
fun fun_cong_rule th ct = Thm.combination th (Thm.reflexive ct);    (*AP_THM in LCF/HOL*)
wenzelm@23568
   455
fun binop_cong_rule ct th1 th2 = Thm.combination (arg_cong_rule ct th1) th2;
clasohm@0
   456
skalberg@15001
   457
local
wenzelm@22906
   458
  val dest_eq = Thm.dest_equals o cprop_of
skalberg@15001
   459
  val rhs_of = snd o dest_eq
skalberg@15001
   460
in
skalberg@15001
   461
fun beta_eta_conversion t =
wenzelm@36944
   462
  let val thm = Thm.beta_conversion true t
wenzelm@36944
   463
  in Thm.transitive thm (Thm.eta_conversion (rhs_of thm)) end
skalberg@15001
   464
end;
skalberg@15001
   465
wenzelm@36944
   466
fun eta_long_conversion ct =
wenzelm@36944
   467
  Thm.transitive
wenzelm@36944
   468
    (beta_eta_conversion ct)
wenzelm@52131
   469
    (Thm.symmetric (beta_eta_conversion (cterm_fun (Envir.eta_long []) ct)));
berghofe@15925
   470
paulson@20861
   471
(*Contract all eta-redexes in the theorem, lest they give rise to needless abstractions*)
paulson@20861
   472
fun eta_contraction_rule th =
wenzelm@36944
   473
  Thm.equal_elim (Thm.eta_conversion (cprop_of th)) th;
paulson@20861
   474
wenzelm@24947
   475
wenzelm@24947
   476
(* abs_def *)
wenzelm@24947
   477
wenzelm@24947
   478
(*
wenzelm@24947
   479
   f ?x1 ... ?xn == u
wenzelm@24947
   480
  --------------------
wenzelm@24947
   481
   f == %x1 ... xn. u
wenzelm@24947
   482
*)
wenzelm@24947
   483
wenzelm@24947
   484
local
wenzelm@24947
   485
wenzelm@24947
   486
fun contract_lhs th =
wenzelm@24947
   487
  Thm.transitive (Thm.symmetric (beta_eta_conversion
wenzelm@24947
   488
    (fst (Thm.dest_equals (cprop_of th))))) th;
wenzelm@24947
   489
wenzelm@24947
   490
fun var_args ct =
wenzelm@24947
   491
  (case try Thm.dest_comb ct of
wenzelm@24947
   492
    SOME (f, arg) =>
wenzelm@24947
   493
      (case Thm.term_of arg of
wenzelm@24947
   494
        Var ((x, _), _) => update (eq_snd (op aconvc)) (x, arg) (var_args f)
wenzelm@24947
   495
      | _ => [])
wenzelm@24947
   496
  | NONE => []);
wenzelm@24947
   497
wenzelm@24947
   498
in
wenzelm@24947
   499
wenzelm@24947
   500
fun abs_def th =
wenzelm@18337
   501
  let
wenzelm@24947
   502
    val th' = contract_lhs th;
wenzelm@24947
   503
    val args = var_args (Thm.lhs_of th');
wenzelm@24947
   504
  in contract_lhs (fold (uncurry Thm.abstract_rule) args th') end;
wenzelm@24947
   505
wenzelm@24947
   506
end;
wenzelm@24947
   507
wenzelm@18337
   508
wenzelm@18468
   509
wenzelm@15669
   510
(*** Some useful meta-theorems ***)
clasohm@0
   511
clasohm@0
   512
(*The rule V/V, obtains assumption solving for eresolve_tac*)
wenzelm@33277
   513
val asm_rl = store_standard_thm_open (Binding.name "asm_rl") (Thm.trivial (read_prop "?psi"));
clasohm@0
   514
clasohm@0
   515
(*Meta-level cut rule: [| V==>W; V |] ==> W *)
wenzelm@4016
   516
val cut_rl =
wenzelm@33277
   517
  store_standard_thm_open (Binding.name "cut_rl")
wenzelm@24241
   518
    (Thm.trivial (read_prop "?psi ==> ?theta"));
clasohm@0
   519
wenzelm@252
   520
(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
clasohm@0
   521
     [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
clasohm@0
   522
val revcut_rl =
wenzelm@33277
   523
  let
wenzelm@33277
   524
    val V = read_prop "V";
wenzelm@33277
   525
    val VW = read_prop "V ==> W";
wenzelm@4016
   526
  in
wenzelm@33277
   527
    store_standard_thm_open (Binding.name "revcut_rl")
wenzelm@36944
   528
      (Thm.implies_intr V (Thm.implies_intr VW (Thm.implies_elim (Thm.assume VW) (Thm.assume V))))
clasohm@0
   529
  end;
clasohm@0
   530
lcp@668
   531
(*for deleting an unwanted assumption*)
lcp@668
   532
val thin_rl =
wenzelm@33277
   533
  let
wenzelm@33277
   534
    val V = read_prop "V";
wenzelm@33277
   535
    val W = read_prop "W";
wenzelm@36944
   536
    val thm = Thm.implies_intr V (Thm.implies_intr W (Thm.assume W));
wenzelm@33277
   537
  in store_standard_thm_open (Binding.name "thin_rl") thm end;
lcp@668
   538
clasohm@0
   539
(* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
clasohm@0
   540
val triv_forall_equality =
wenzelm@33277
   541
  let
wenzelm@33277
   542
    val V = read_prop "V";
wenzelm@33277
   543
    val QV = read_prop "!!x::'a. V";
wenzelm@33277
   544
    val x = certify (Free ("x", Term.aT []));
wenzelm@4016
   545
  in
wenzelm@33277
   546
    store_standard_thm_open (Binding.name "triv_forall_equality")
wenzelm@36944
   547
      (Thm.equal_intr (Thm.implies_intr QV (Thm.forall_elim x (Thm.assume QV)))
wenzelm@36944
   548
        (Thm.implies_intr V (Thm.forall_intr x (Thm.assume V))))
clasohm@0
   549
  end;
clasohm@0
   550
wenzelm@19051
   551
(* (PROP ?Phi ==> PROP ?Phi ==> PROP ?Psi) ==>
wenzelm@19051
   552
   (PROP ?Phi ==> PROP ?Psi)
wenzelm@19051
   553
*)
wenzelm@19051
   554
val distinct_prems_rl =
wenzelm@19051
   555
  let
wenzelm@33277
   556
    val AAB = read_prop "Phi ==> Phi ==> Psi";
wenzelm@24241
   557
    val A = read_prop "Phi";
wenzelm@19051
   558
  in
wenzelm@33277
   559
    store_standard_thm_open (Binding.name "distinct_prems_rl")
wenzelm@36944
   560
      (implies_intr_list [AAB, A] (implies_elim_list (Thm.assume AAB) [Thm.assume A, Thm.assume A]))
wenzelm@19051
   561
  end;
wenzelm@19051
   562
nipkow@3653
   563
(* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
nipkow@3653
   564
   ==> PROP ?phi == PROP ?psi
wenzelm@8328
   565
   Introduction rule for == as a meta-theorem.
nipkow@3653
   566
*)
nipkow@3653
   567
val equal_intr_rule =
wenzelm@33277
   568
  let
wenzelm@33277
   569
    val PQ = read_prop "phi ==> psi";
wenzelm@33277
   570
    val QP = read_prop "psi ==> phi";
wenzelm@4016
   571
  in
wenzelm@33277
   572
    store_standard_thm_open (Binding.name "equal_intr_rule")
wenzelm@36944
   573
      (Thm.implies_intr PQ (Thm.implies_intr QP (Thm.equal_intr (Thm.assume PQ) (Thm.assume QP))))
nipkow@3653
   574
  end;
nipkow@3653
   575
wenzelm@19421
   576
(* PROP ?phi == PROP ?psi ==> PROP ?phi ==> PROP ?psi *)
wenzelm@13368
   577
val equal_elim_rule1 =
wenzelm@33277
   578
  let
wenzelm@33277
   579
    val eq = read_prop "phi::prop == psi::prop";
wenzelm@33277
   580
    val P = read_prop "phi";
wenzelm@33277
   581
  in
wenzelm@33277
   582
    store_standard_thm_open (Binding.name "equal_elim_rule1")
wenzelm@36944
   583
      (Thm.equal_elim (Thm.assume eq) (Thm.assume P) |> implies_intr_list [eq, P])
wenzelm@13368
   584
  end;
wenzelm@4285
   585
wenzelm@19421
   586
(* PROP ?psi == PROP ?phi ==> PROP ?phi ==> PROP ?psi *)
wenzelm@19421
   587
val equal_elim_rule2 =
wenzelm@33277
   588
  store_standard_thm_open (Binding.name "equal_elim_rule2")
wenzelm@33277
   589
    (symmetric_thm RS equal_elim_rule1);
wenzelm@19421
   590
wenzelm@28618
   591
(* PROP ?phi ==> PROP ?phi ==> PROP ?psi ==> PROP ?psi *)
wenzelm@12297
   592
val remdups_rl =
wenzelm@33277
   593
  let
wenzelm@33277
   594
    val P = read_prop "phi";
wenzelm@33277
   595
    val Q = read_prop "psi";
wenzelm@33277
   596
    val thm = implies_intr_list [P, P, Q] (Thm.assume Q);
wenzelm@33277
   597
  in store_standard_thm_open (Binding.name "remdups_rl") thm end;
wenzelm@12297
   598
wenzelm@12297
   599
wenzelm@28618
   600
wenzelm@28618
   601
(** embedded terms and types **)
wenzelm@28618
   602
wenzelm@28618
   603
local
wenzelm@28618
   604
  val A = certify (Free ("A", propT));
wenzelm@35845
   605
  val axiom = Thm.unvarify_global o Thm.axiom (Context.the_theory (Context.the_thread_data ()));
wenzelm@28674
   606
  val prop_def = axiom "Pure.prop_def";
wenzelm@28674
   607
  val term_def = axiom "Pure.term_def";
wenzelm@28674
   608
  val sort_constraint_def = axiom "Pure.sort_constraint_def";
wenzelm@28618
   609
  val C = Thm.lhs_of sort_constraint_def;
wenzelm@28618
   610
  val T = Thm.dest_arg C;
wenzelm@28618
   611
  val CA = mk_implies (C, A);
wenzelm@28618
   612
in
wenzelm@28618
   613
wenzelm@28618
   614
(* protect *)
wenzelm@28618
   615
wenzelm@46497
   616
val protect = Thm.apply (certify Logic.protectC);
wenzelm@28618
   617
wenzelm@33277
   618
val protectI =
wenzelm@35021
   619
  store_standard_thm (Binding.conceal (Binding.name "protectI"))
wenzelm@35021
   620
    (Thm.equal_elim (Thm.symmetric prop_def) (Thm.assume A));
wenzelm@28618
   621
wenzelm@33277
   622
val protectD =
wenzelm@35021
   623
  store_standard_thm (Binding.conceal (Binding.name "protectD"))
wenzelm@35021
   624
    (Thm.equal_elim prop_def (Thm.assume (protect A)));
wenzelm@28618
   625
wenzelm@33277
   626
val protect_cong =
wenzelm@33277
   627
  store_standard_thm_open (Binding.name "protect_cong") (Thm.reflexive (protect A));
wenzelm@28618
   628
wenzelm@28618
   629
fun implies_intr_protected asms th =
wenzelm@28618
   630
  let val asms' = map protect asms in
wenzelm@28618
   631
    implies_elim_list
wenzelm@28618
   632
      (implies_intr_list asms th)
wenzelm@28618
   633
      (map (fn asm' => Thm.assume asm' RS protectD) asms')
wenzelm@28618
   634
    |> implies_intr_list asms'
wenzelm@28618
   635
  end;
wenzelm@28618
   636
wenzelm@28618
   637
wenzelm@28618
   638
(* term *)
wenzelm@28618
   639
wenzelm@33277
   640
val termI =
wenzelm@35021
   641
  store_standard_thm (Binding.conceal (Binding.name "termI"))
wenzelm@35021
   642
    (Thm.equal_elim (Thm.symmetric term_def) (Thm.forall_intr A (Thm.trivial A)));
wenzelm@9554
   643
wenzelm@28618
   644
fun mk_term ct =
wenzelm@28618
   645
  let
wenzelm@28618
   646
    val thy = Thm.theory_of_cterm ct;
wenzelm@28618
   647
    val cert = Thm.cterm_of thy;
wenzelm@28618
   648
    val certT = Thm.ctyp_of thy;
wenzelm@28618
   649
    val T = Thm.typ_of (Thm.ctyp_of_term ct);
wenzelm@28618
   650
    val a = certT (TVar (("'a", 0), []));
wenzelm@28618
   651
    val x = cert (Var (("x", 0), T));
wenzelm@28618
   652
  in Thm.instantiate ([(a, certT T)], [(x, ct)]) termI end;
wenzelm@28618
   653
wenzelm@28618
   654
fun dest_term th =
wenzelm@28618
   655
  let val cprop = strip_imp_concl (Thm.cprop_of th) in
wenzelm@28618
   656
    if can Logic.dest_term (Thm.term_of cprop) then
wenzelm@28618
   657
      Thm.dest_arg cprop
wenzelm@28618
   658
    else raise THM ("dest_term", 0, [th])
wenzelm@28618
   659
  end;
wenzelm@28618
   660
wenzelm@28618
   661
fun cterm_rule f = dest_term o f o mk_term;
wenzelm@28618
   662
wenzelm@45156
   663
val dummy_thm = mk_term (certify Term.dummy_prop);
wenzelm@28618
   664
wenzelm@28618
   665
wenzelm@28618
   666
(* sort_constraint *)
wenzelm@28618
   667
wenzelm@33277
   668
val sort_constraintI =
wenzelm@35021
   669
  store_standard_thm (Binding.conceal (Binding.name "sort_constraintI"))
wenzelm@35021
   670
    (Thm.equal_elim (Thm.symmetric sort_constraint_def) (mk_term T));
wenzelm@28618
   671
wenzelm@33277
   672
val sort_constraint_eq =
wenzelm@35021
   673
  store_standard_thm (Binding.conceal (Binding.name "sort_constraint_eq"))
wenzelm@35021
   674
    (Thm.equal_intr
wenzelm@35845
   675
      (Thm.implies_intr CA (Thm.implies_elim (Thm.assume CA)
wenzelm@35845
   676
        (Thm.unvarify_global sort_constraintI)))
wenzelm@35021
   677
      (implies_intr_list [A, C] (Thm.assume A)));
wenzelm@28618
   678
wenzelm@28618
   679
end;
wenzelm@28618
   680
wenzelm@28618
   681
wenzelm@28618
   682
(* HHF normalization *)
wenzelm@28618
   683
wenzelm@46214
   684
(* (PROP ?phi ==> (!!x. PROP ?psi x)) == (!!x. PROP ?phi ==> PROP ?psi x) *)
wenzelm@9554
   685
val norm_hhf_eq =
wenzelm@9554
   686
  let
wenzelm@14854
   687
    val aT = TFree ("'a", []);
wenzelm@9554
   688
    val x = Free ("x", aT);
wenzelm@9554
   689
    val phi = Free ("phi", propT);
wenzelm@9554
   690
    val psi = Free ("psi", aT --> propT);
wenzelm@9554
   691
wenzelm@26487
   692
    val cx = certify x;
wenzelm@26487
   693
    val cphi = certify phi;
wenzelm@46214
   694
    val lhs = certify (Logic.mk_implies (phi, Logic.all x (psi $ x)));
wenzelm@46214
   695
    val rhs = certify (Logic.all x (Logic.mk_implies (phi, psi $ x)));
wenzelm@9554
   696
  in
wenzelm@9554
   697
    Thm.equal_intr
wenzelm@9554
   698
      (Thm.implies_elim (Thm.assume lhs) (Thm.assume cphi)
wenzelm@9554
   699
        |> Thm.forall_elim cx
wenzelm@9554
   700
        |> Thm.implies_intr cphi
wenzelm@9554
   701
        |> Thm.forall_intr cx
wenzelm@9554
   702
        |> Thm.implies_intr lhs)
wenzelm@9554
   703
      (Thm.implies_elim
wenzelm@9554
   704
          (Thm.assume rhs |> Thm.forall_elim cx) (Thm.assume cphi)
wenzelm@9554
   705
        |> Thm.forall_intr cx
wenzelm@9554
   706
        |> Thm.implies_intr cphi
wenzelm@9554
   707
        |> Thm.implies_intr rhs)
wenzelm@33277
   708
    |> store_standard_thm_open (Binding.name "norm_hhf_eq")
wenzelm@9554
   709
  end;
wenzelm@9554
   710
wenzelm@18179
   711
val norm_hhf_prop = Logic.dest_equals (Thm.prop_of norm_hhf_eq);
wenzelm@28618
   712
val norm_hhf_eqs = [norm_hhf_eq, sort_constraint_eq];
wenzelm@18179
   713
wenzelm@30553
   714
fun is_norm_hhf (Const ("Pure.sort_constraint", _)) = false
wenzelm@30553
   715
  | is_norm_hhf (Const ("==>", _) $ _ $ (Const ("all", _) $ _)) = false
wenzelm@30553
   716
  | is_norm_hhf (Abs _ $ _) = false
wenzelm@30553
   717
  | is_norm_hhf (t $ u) = is_norm_hhf t andalso is_norm_hhf u
wenzelm@30553
   718
  | is_norm_hhf (Abs (_, _, t)) = is_norm_hhf t
wenzelm@30553
   719
  | is_norm_hhf _ = true;
wenzelm@12800
   720
wenzelm@16425
   721
fun norm_hhf thy t =
wenzelm@12800
   722
  if is_norm_hhf t then t
wenzelm@18179
   723
  else Pattern.rewrite_term thy [norm_hhf_prop] [] t;
wenzelm@18179
   724
wenzelm@20298
   725
fun norm_hhf_cterm ct =
wenzelm@20298
   726
  if is_norm_hhf (Thm.term_of ct) then ct
wenzelm@20298
   727
  else cterm_fun (Pattern.rewrite_term (Thm.theory_of_cterm ct) [norm_hhf_prop] []) ct;
wenzelm@20298
   728
wenzelm@12800
   729
wenzelm@21603
   730
(* var indexes *)
wenzelm@21603
   731
wenzelm@21603
   732
fun incr_indexes th = Thm.incr_indexes (Thm.maxidx_of th + 1);
wenzelm@21603
   733
wenzelm@21603
   734
fun incr_indexes2 th1 th2 =
wenzelm@21603
   735
  Thm.incr_indexes (Int.max (Thm.maxidx_of th1, Thm.maxidx_of th2) + 1);
wenzelm@21603
   736
wenzelm@52224
   737
local
wenzelm@52224
   738
wenzelm@52224
   739
(*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
wenzelm@52224
   740
fun comp incremented th1 th2 =
wenzelm@52224
   741
  Thm.bicompose {flatten = true, match = false, incremented = incremented} (false, th1, 0) 1 th2
wenzelm@52224
   742
  |> Seq.list_of |> distinct Thm.eq_thm
wenzelm@52224
   743
  |> (fn [th] => th | _ => raise THM ("COMP", 1, [th1, th2]));
wenzelm@52224
   744
wenzelm@52224
   745
in
wenzelm@52224
   746
wenzelm@52224
   747
fun th1 COMP th2 = comp false th1 th2;
wenzelm@52224
   748
fun th1 INCR_COMP th2 = comp true (incr_indexes th2 th1) th2;
wenzelm@52224
   749
fun th1 COMP_INCR th2 = comp true th1 (incr_indexes th1 th2);
wenzelm@52224
   750
wenzelm@52224
   751
end;
wenzelm@21603
   752
wenzelm@29344
   753
fun comp_no_flatten (th, n) i rule =
wenzelm@29344
   754
  (case distinct Thm.eq_thm (Seq.list_of
wenzelm@52223
   755
      (Thm.bicompose {flatten = false, match = false, incremented = true}
wenzelm@52223
   756
        (false, th, n) i (incr_indexes th rule))) of
wenzelm@29344
   757
    [th'] => th'
wenzelm@29344
   758
  | [] => raise THM ("comp_no_flatten", i, [th, rule])
wenzelm@29344
   759
  | _ => raise THM ("comp_no_flatten: unique result expected", i, [th, rule]));
wenzelm@29344
   760
wenzelm@29344
   761
wenzelm@9554
   762
wenzelm@45348
   763
(** variations on Thm.instantiate **)
paulson@8129
   764
wenzelm@43333
   765
fun instantiate_normalize instpair th =
wenzelm@21603
   766
  Thm.adjust_maxidx_thm ~1 (Thm.instantiate instpair th COMP_INCR asm_rl);
paulson@8129
   767
wenzelm@45347
   768
(*Left-to-right replacements: tpairs = [..., (vi, ti), ...].
wenzelm@45347
   769
  Instantiates distinct Vars by terms, inferring type instantiations.*)
paulson@8129
   770
local
wenzelm@45347
   771
  fun add_types (ct, cu) (thy, tye, maxidx) =
wenzelm@26627
   772
    let
wenzelm@45347
   773
      val {t, T, maxidx = maxt, ...} = Thm.rep_cterm ct;
wenzelm@45347
   774
      val {t = u, T = U, maxidx = maxu, ...} = Thm.rep_cterm cu;
wenzelm@45347
   775
      val maxi = Int.max (maxidx, Int.max (maxt, maxu));
wenzelm@45347
   776
      val thy' = Theory.merge (thy, Theory.merge (Thm.theory_of_cterm ct, Thm.theory_of_cterm cu));
wenzelm@45347
   777
      val (tye', maxi') = Sign.typ_unify thy' (T, U) (tye, maxi)
wenzelm@45347
   778
        handle Type.TUNIFY => raise TYPE ("Ill-typed instantiation:\nType\n" ^
wenzelm@45347
   779
          Syntax.string_of_typ_global thy' (Envir.norm_type tye T) ^
wenzelm@45347
   780
          "\nof variable " ^
wenzelm@45347
   781
          Syntax.string_of_term_global thy' (Term.map_types (Envir.norm_type tye) t) ^
wenzelm@45347
   782
          "\ncannot be unified with type\n" ^
wenzelm@45347
   783
          Syntax.string_of_typ_global thy' (Envir.norm_type tye U) ^ "\nof term " ^
wenzelm@45347
   784
          Syntax.string_of_term_global thy' (Term.map_types (Envir.norm_type tye) u),
wenzelm@45347
   785
          [T, U], [t, u])
wenzelm@45347
   786
    in (thy', tye', maxi') end;
paulson@8129
   787
in
wenzelm@45347
   788
paulson@22561
   789
fun cterm_instantiate [] th = th
wenzelm@45348
   790
  | cterm_instantiate ctpairs th =
wenzelm@45347
   791
      let
wenzelm@45348
   792
        val (thy, tye, _) = fold_rev add_types ctpairs (Thm.theory_of_thm th, Vartab.empty, 0);
wenzelm@45347
   793
        val certT = ctyp_of thy;
wenzelm@45348
   794
        val instT =
wenzelm@45348
   795
          Vartab.fold (fn (xi, (S, T)) =>
wenzelm@45348
   796
            cons (certT (TVar (xi, S)), certT (Envir.norm_type tye T))) tye [];
wenzelm@45348
   797
        val inst = map (pairself (Thm.instantiate_cterm (instT, []))) ctpairs;
wenzelm@45348
   798
      in instantiate_normalize (instT, inst) th end
wenzelm@45348
   799
      handle TERM (msg, _) => raise THM (msg, 0, [th])
wenzelm@45347
   800
        | TYPE (msg, _, _) => raise THM (msg, 0, [th]);
paulson@8129
   801
end;
paulson@8129
   802
paulson@8129
   803
wenzelm@4285
   804
(* instantiate by left-to-right occurrence of variables *)
wenzelm@4285
   805
wenzelm@4285
   806
fun instantiate' cTs cts thm =
wenzelm@4285
   807
  let
wenzelm@4285
   808
    fun err msg =
wenzelm@4285
   809
      raise TYPE ("instantiate': " ^ msg,
wenzelm@19482
   810
        map_filter (Option.map Thm.typ_of) cTs,
wenzelm@19482
   811
        map_filter (Option.map Thm.term_of) cts);
wenzelm@4285
   812
wenzelm@4285
   813
    fun inst_of (v, ct) =
wenzelm@16425
   814
      (Thm.cterm_of (Thm.theory_of_cterm ct) (Var v), ct)
wenzelm@4285
   815
        handle TYPE (msg, _, _) => err msg;
wenzelm@4285
   816
berghofe@15797
   817
    fun tyinst_of (v, cT) =
wenzelm@16425
   818
      (Thm.ctyp_of (Thm.theory_of_ctyp cT) (TVar v), cT)
berghofe@15797
   819
        handle TYPE (msg, _, _) => err msg;
berghofe@15797
   820
wenzelm@20298
   821
    fun zip_vars xs ys =
wenzelm@40722
   822
      zip_options xs ys handle ListPair.UnequalLengths =>
wenzelm@20298
   823
        err "more instantiations than variables in thm";
wenzelm@4285
   824
wenzelm@4285
   825
    (*instantiate types first!*)
wenzelm@4285
   826
    val thm' =
wenzelm@4285
   827
      if forall is_none cTs then thm
wenzelm@20298
   828
      else Thm.instantiate
wenzelm@22695
   829
        (map tyinst_of (zip_vars (rev (Thm.fold_terms Term.add_tvars thm [])) cTs), []) thm;
wenzelm@20579
   830
    val thm'' =
wenzelm@4285
   831
      if forall is_none cts then thm'
wenzelm@20298
   832
      else Thm.instantiate
wenzelm@22695
   833
        ([], map inst_of (zip_vars (rev (Thm.fold_terms Term.add_vars thm' [])) cts)) thm';
wenzelm@20298
   834
    in thm'' end;
wenzelm@4285
   835
wenzelm@4285
   836
berghofe@14081
   837
berghofe@14081
   838
(** renaming of bound variables **)
berghofe@14081
   839
berghofe@14081
   840
(* replace bound variables x_i in thm by y_i *)
berghofe@14081
   841
(* where vs = [(x_1, y_1), ..., (x_n, y_n)]  *)
berghofe@14081
   842
berghofe@14081
   843
fun rename_bvars [] thm = thm
berghofe@14081
   844
  | rename_bvars vs thm =
wenzelm@26627
   845
      let
wenzelm@26627
   846
        val cert = Thm.cterm_of (Thm.theory_of_thm thm);
wenzelm@26627
   847
        fun ren (Abs (x, T, t)) = Abs (AList.lookup (op =) vs x |> the_default x, T, ren t)
wenzelm@26627
   848
          | ren (t $ u) = ren t $ ren u
wenzelm@26627
   849
          | ren t = t;
wenzelm@36944
   850
      in Thm.equal_elim (Thm.reflexive (cert (ren (Thm.prop_of thm)))) thm end;
berghofe@14081
   851
berghofe@14081
   852
berghofe@14081
   853
(* renaming in left-to-right order *)
berghofe@14081
   854
berghofe@14081
   855
fun rename_bvars' xs thm =
berghofe@14081
   856
  let
wenzelm@26627
   857
    val cert = Thm.cterm_of (Thm.theory_of_thm thm);
wenzelm@26627
   858
    val prop = Thm.prop_of thm;
berghofe@14081
   859
    fun rename [] t = ([], t)
berghofe@14081
   860
      | rename (x' :: xs) (Abs (x, T, t)) =
berghofe@14081
   861
          let val (xs', t') = rename xs t
wenzelm@18929
   862
          in (xs', Abs (the_default x x', T, t')) end
berghofe@14081
   863
      | rename xs (t $ u) =
berghofe@14081
   864
          let
berghofe@14081
   865
            val (xs', t') = rename xs t;
berghofe@14081
   866
            val (xs'', u') = rename xs' u
berghofe@14081
   867
          in (xs'', t' $ u') end
berghofe@14081
   868
      | rename xs t = (xs, t);
berghofe@14081
   869
  in case rename xs prop of
wenzelm@36944
   870
      ([], prop') => Thm.equal_elim (Thm.reflexive (cert prop')) thm
berghofe@14081
   871
    | _ => error "More names than abstractions in theorem"
berghofe@14081
   872
  end;
berghofe@14081
   873
wenzelm@11975
   874
end;
wenzelm@5903
   875
wenzelm@35021
   876
structure Basic_Drule: BASIC_DRULE = Drule;
wenzelm@35021
   877
open Basic_Drule;