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