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