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