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