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