src/Pure/more_thm.ML
author wenzelm
Thu Sep 24 23:33:29 2015 +0200 (2015-09-24)
changeset 61261 ddb2da7cb2e4
parent 61059 0306e209fa9e
child 61262 7bd1eb4b056e
permissions -rw-r--r--
more explicit Defs.context: use proper name spaces as far as possible;
wenzelm@22362
     1
(*  Title:      Pure/more_thm.ML
wenzelm@22362
     2
    Author:     Makarius
wenzelm@22362
     3
wenzelm@22907
     4
Further operations on type ctyp/cterm/thm, outside the inference kernel.
wenzelm@22362
     5
*)
wenzelm@22362
     6
wenzelm@23169
     7
infix aconvc;
wenzelm@23169
     8
wenzelm@32842
     9
signature BASIC_THM =
wenzelm@32842
    10
sig
wenzelm@32842
    11
  include BASIC_THM
wenzelm@32842
    12
  structure Ctermtab: TABLE
wenzelm@32842
    13
  structure Thmtab: TABLE
wenzelm@32842
    14
  val aconvc: cterm * cterm -> bool
wenzelm@45375
    15
  type attribute = Context.generic * thm -> Context.generic option * thm option
wenzelm@32842
    16
end;
wenzelm@32842
    17
wenzelm@22362
    18
signature THM =
wenzelm@22362
    19
sig
wenzelm@22362
    20
  include THM
wenzelm@32842
    21
  structure Ctermtab: TABLE
wenzelm@32842
    22
  structure Thmtab: TABLE
wenzelm@60952
    23
  val eq_ctyp: ctyp * ctyp -> bool
wenzelm@24948
    24
  val aconvc: cterm * cterm -> bool
wenzelm@60952
    25
  val add_tvars: thm -> ctyp list -> ctyp list
wenzelm@60818
    26
  val add_frees: thm -> cterm list -> cterm list
wenzelm@60818
    27
  val add_vars: thm -> cterm list -> cterm list
wenzelm@60938
    28
  val all_name: Proof.context -> string * cterm -> cterm -> cterm
wenzelm@60938
    29
  val all: Proof.context -> cterm -> cterm -> cterm
wenzelm@22907
    30
  val mk_binop: cterm -> cterm -> cterm -> cterm
wenzelm@22907
    31
  val dest_binop: cterm -> cterm * cterm
wenzelm@22907
    32
  val dest_implies: cterm -> cterm * cterm
wenzelm@22907
    33
  val dest_equals: cterm -> cterm * cterm
wenzelm@22907
    34
  val dest_equals_lhs: cterm -> cterm
wenzelm@22907
    35
  val dest_equals_rhs: cterm -> cterm
wenzelm@22907
    36
  val lhs_of: thm -> cterm
wenzelm@22907
    37
  val rhs_of: thm -> cterm
wenzelm@22362
    38
  val thm_ord: thm * thm -> order
wenzelm@32842
    39
  val cterm_cache: (cterm -> 'a) -> cterm -> 'a
wenzelm@32842
    40
  val thm_cache: (thm -> 'a) -> thm -> 'a
wenzelm@23599
    41
  val is_reflexive: thm -> bool
wenzelm@22362
    42
  val eq_thm: thm * thm -> bool
wenzelm@22362
    43
  val eq_thm_prop: thm * thm -> bool
wenzelm@52683
    44
  val eq_thm_strict: thm * thm -> bool
wenzelm@60817
    45
  val equiv_thm: theory -> thm * thm -> bool
wenzelm@31944
    46
  val class_triv: theory -> class -> thm
wenzelm@31944
    47
  val of_sort: ctyp * sort -> thm list
wenzelm@60819
    48
  val check_shyps: Proof.context -> sort list -> thm -> thm
wenzelm@24048
    49
  val is_dummy: thm -> bool
wenzelm@22695
    50
  val plain_prop_of: thm -> term
wenzelm@24048
    51
  val add_thm: thm -> thm list -> thm list
wenzelm@24048
    52
  val del_thm: thm -> thm list -> thm list
wenzelm@24048
    53
  val merge_thms: thm list * thm list -> thm list
wenzelm@33453
    54
  val full_rules: thm Item_Net.T
wenzelm@30560
    55
  val intro_rules: thm Item_Net.T
wenzelm@30560
    56
  val elim_rules: thm Item_Net.T
wenzelm@54984
    57
  val declare_hyps: cterm -> Proof.context -> Proof.context
wenzelm@54984
    58
  val assume_hyps: cterm -> Proof.context -> thm * Proof.context
wenzelm@54993
    59
  val unchecked_hyps: Proof.context -> Proof.context
wenzelm@54993
    60
  val restore_hyps: Proof.context -> Proof.context -> Proof.context
wenzelm@55633
    61
  val undeclared_hyps: Context.generic -> thm -> term list
wenzelm@54993
    62
  val check_hyps: Context.generic -> thm -> thm
wenzelm@27866
    63
  val elim_implies: thm -> thm -> thm
wenzelm@27866
    64
  val forall_elim_var: int -> thm -> thm
wenzelm@27866
    65
  val forall_elim_vars: int -> thm -> thm
wenzelm@60801
    66
  val instantiate': ctyp option list -> cterm option list -> thm -> thm
wenzelm@35985
    67
  val forall_intr_frees: thm -> thm
wenzelm@60825
    68
  val unvarify_global: theory -> thm -> thm
wenzelm@60825
    69
  val unvarify_axiom: theory -> string -> thm
wenzelm@27866
    70
  val close_derivation: thm -> thm
wenzelm@59969
    71
  val rename_params_rule: string list * int -> thm -> thm
wenzelm@59969
    72
  val rename_boundvars: term -> term -> thm -> thm
wenzelm@42375
    73
  val add_axiom: Proof.context -> binding * term -> theory -> (string * thm) * theory
wenzelm@42375
    74
  val add_axiom_global: binding * term -> theory -> (string * thm) * theory
wenzelm@61261
    75
  val add_def: Defs.context -> bool -> bool -> binding * term -> theory -> (string * thm) * theory
wenzelm@42375
    76
  val add_def_global: bool -> bool -> binding * term -> theory -> (string * thm) * theory
wenzelm@45375
    77
  type attribute = Context.generic * thm -> Context.generic option * thm option
wenzelm@30210
    78
  type binding = binding * attribute list
wenzelm@30210
    79
  val empty_binding: binding
wenzelm@27866
    80
  val rule_attribute: (Context.generic -> thm -> thm) -> attribute
wenzelm@27866
    81
  val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
wenzelm@45375
    82
  val mixed_attribute: (Context.generic * thm -> Context.generic * thm) -> attribute
wenzelm@46775
    83
  val apply_attribute: attribute -> thm -> Context.generic -> thm * Context.generic
wenzelm@45375
    84
  val attribute_declaration: attribute -> thm -> Context.generic -> Context.generic
wenzelm@46775
    85
  val theory_attributes: attribute list -> thm -> theory -> thm * theory
wenzelm@46775
    86
  val proof_attributes: attribute list -> thm -> Proof.context -> thm * Proof.context
wenzelm@27866
    87
  val no_attributes: 'a -> 'a * 'b list
wenzelm@27866
    88
  val simple_fact: 'a -> ('a * 'b list) list
wenzelm@46830
    89
  val tag_rule: string * string -> thm -> thm
wenzelm@27866
    90
  val untag_rule: string -> thm -> thm
wenzelm@46830
    91
  val tag: string * string -> attribute
wenzelm@27866
    92
  val untag: string -> attribute
wenzelm@30342
    93
  val def_name: string -> string
wenzelm@30342
    94
  val def_name_optional: string -> string -> string
wenzelm@35238
    95
  val def_binding: Binding.binding -> Binding.binding
wenzelm@30433
    96
  val def_binding_optional: Binding.binding -> Binding.binding -> Binding.binding
wenzelm@27866
    97
  val has_name_hint: thm -> bool
wenzelm@27866
    98
  val get_name_hint: thm -> string
wenzelm@27866
    99
  val put_name_hint: string -> thm -> thm
wenzelm@22362
   100
  val theoremK: string
wenzelm@22362
   101
  val lemmaK: string
wenzelm@22362
   102
  val corollaryK: string
wenzelm@42473
   103
  val legacy_get_kind: thm -> string
wenzelm@27866
   104
  val kind_rule: string -> thm -> thm
wenzelm@27866
   105
  val kind: string -> attribute
wenzelm@49062
   106
  val register_proofs: thm list -> theory -> theory
wenzelm@49011
   107
  val join_theory_proofs: theory -> unit
wenzelm@22362
   108
end;
wenzelm@22362
   109
wenzelm@22362
   110
structure Thm: THM =
wenzelm@22362
   111
struct
wenzelm@22362
   112
wenzelm@22695
   113
(** basic operations **)
wenzelm@22362
   114
wenzelm@60952
   115
(* collecting ctyps and cterms *)
wenzelm@23491
   116
wenzelm@60952
   117
val eq_ctyp = op = o apply2 Thm.typ_of;
wenzelm@59058
   118
val op aconvc = op aconv o apply2 Thm.term_of;
wenzelm@23491
   119
wenzelm@60952
   120
val add_tvars = Thm.fold_atomic_ctyps (fn a => is_TVar (Thm.typ_of a) ? insert eq_ctyp a);
wenzelm@60818
   121
val add_frees = Thm.fold_atomic_cterms (fn a => is_Free (Thm.term_of a) ? insert (op aconvc) a);
wenzelm@60818
   122
val add_vars = Thm.fold_atomic_cterms (fn a => is_Var (Thm.term_of a) ? insert (op aconvc) a);
wenzelm@23491
   123
wenzelm@23491
   124
wenzelm@22907
   125
(* cterm constructors and destructors *)
wenzelm@22907
   126
wenzelm@60938
   127
fun all_name ctxt (x, t) A =
wenzelm@32198
   128
  let
wenzelm@59586
   129
    val T = Thm.typ_of_cterm t;
wenzelm@60938
   130
    val all_const = Thm.cterm_of ctxt (Const ("Pure.all", (T --> propT) --> propT));
wenzelm@60938
   131
  in Thm.apply all_const (Thm.lambda_name (x, t) A) end;
wenzelm@32198
   132
wenzelm@60938
   133
fun all ctxt t A = all_name ctxt ("", t) A;
wenzelm@32198
   134
wenzelm@46497
   135
fun mk_binop c a b = Thm.apply (Thm.apply c a) b;
wenzelm@22907
   136
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
   137
wenzelm@22907
   138
fun dest_implies ct =
wenzelm@22907
   139
  (case Thm.term_of ct of
wenzelm@56245
   140
    Const ("Pure.imp", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   141
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
   142
wenzelm@22907
   143
fun dest_equals ct =
wenzelm@22907
   144
  (case Thm.term_of ct of
wenzelm@56245
   145
    Const ("Pure.eq", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   146
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
   147
wenzelm@22907
   148
fun dest_equals_lhs ct =
wenzelm@22907
   149
  (case Thm.term_of ct of
wenzelm@56245
   150
    Const ("Pure.eq", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
   151
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
   152
wenzelm@22907
   153
fun dest_equals_rhs ct =
wenzelm@22907
   154
  (case Thm.term_of ct of
wenzelm@56245
   155
    Const ("Pure.eq", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
   156
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
   157
wenzelm@22907
   158
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
   159
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
   160
wenzelm@22907
   161
wenzelm@22907
   162
(* thm order: ignores theory context! *)
wenzelm@22682
   163
wenzelm@61039
   164
fun thm_ord ths =
wenzelm@61039
   165
  (case Term_Ord.fast_term_ord (apply2 Thm.prop_of ths) of
wenzelm@61039
   166
    EQUAL =>
wenzelm@61039
   167
      (case
wenzelm@61039
   168
        list_ord (prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord)
wenzelm@61039
   169
          (apply2 Thm.tpairs_of ths)
wenzelm@61039
   170
       of
wenzelm@61039
   171
        EQUAL =>
wenzelm@61039
   172
          (case list_ord Term_Ord.fast_term_ord (apply2 Thm.hyps_of ths) of
wenzelm@61039
   173
            EQUAL => list_ord Term_Ord.sort_ord (apply2 Thm.shyps_of ths)
wenzelm@61039
   174
          | ord => ord)
wenzelm@61039
   175
      | ord => ord)
wenzelm@61039
   176
  | ord => ord);
wenzelm@22362
   177
wenzelm@22682
   178
wenzelm@32842
   179
(* tables and caches *)
wenzelm@32842
   180
wenzelm@59058
   181
structure Ctermtab = Table(type key = cterm val ord = Term_Ord.fast_term_ord o apply2 Thm.term_of);
wenzelm@32842
   182
structure Thmtab = Table(type key = thm val ord = thm_ord);
wenzelm@32842
   183
wenzelm@32842
   184
fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
wenzelm@32842
   185
fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
wenzelm@32842
   186
wenzelm@32842
   187
wenzelm@22682
   188
(* equality *)
wenzelm@22682
   189
wenzelm@23599
   190
fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
wenzelm@23599
   191
  handle TERM _ => false;
wenzelm@23599
   192
wenzelm@55547
   193
val eq_thm = is_equal o thm_ord;
wenzelm@22362
   194
wenzelm@59058
   195
val eq_thm_prop = op aconv o apply2 Thm.full_prop_of;
wenzelm@22362
   196
wenzelm@52683
   197
fun eq_thm_strict ths =
wenzelm@55547
   198
  eq_thm ths andalso
wenzelm@61040
   199
  Context.eq_thy_id (apply2 Thm.theory_id_of_thm ths) andalso
wenzelm@61040
   200
  op = (apply2 Thm.maxidx_of ths) andalso
wenzelm@61040
   201
  op = (apply2 Thm.get_tags ths);
wenzelm@52683
   202
wenzelm@22682
   203
wenzelm@22682
   204
(* pattern equivalence *)
wenzelm@22682
   205
wenzelm@60817
   206
fun equiv_thm thy ths =
wenzelm@60817
   207
  Pattern.equiv thy (apply2 (Thm.full_prop_of o Thm.transfer thy) ths);
wenzelm@22362
   208
wenzelm@22362
   209
wenzelm@31904
   210
(* type classes and sorts *)
wenzelm@31904
   211
wenzelm@31944
   212
fun class_triv thy c =
wenzelm@59621
   213
  Thm.of_class (Thm.global_ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
wenzelm@31944
   214
wenzelm@31944
   215
fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
wenzelm@28621
   216
wenzelm@60819
   217
fun check_shyps ctxt sorts raw_th =
wenzelm@28621
   218
  let
wenzelm@28621
   219
    val th = Thm.strip_shyps raw_th;
wenzelm@28621
   220
    val pending = Sorts.subtract sorts (Thm.extra_shyps th);
wenzelm@28621
   221
  in
wenzelm@28621
   222
    if null pending then th
wenzelm@28621
   223
    else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
wenzelm@60819
   224
      Pretty.brk 1 :: Pretty.commas (map (Syntax.pretty_sort ctxt) pending))))
wenzelm@28621
   225
  end;
wenzelm@28621
   226
wenzelm@28621
   227
wenzelm@22695
   228
(* misc operations *)
wenzelm@22695
   229
wenzelm@24048
   230
fun is_dummy thm =
wenzelm@24048
   231
  (case try Logic.dest_term (Thm.concl_of thm) of
wenzelm@24048
   232
    NONE => false
wenzelm@58001
   233
  | SOME t => Term.is_dummy_pattern (Term.head_of t));
wenzelm@24048
   234
wenzelm@22695
   235
fun plain_prop_of raw_thm =
wenzelm@22695
   236
  let
wenzelm@22695
   237
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   238
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   239
  in
wenzelm@61039
   240
    if not (null (Thm.hyps_of thm)) then
wenzelm@22695
   241
      err "theorem may not contain hypotheses"
wenzelm@22695
   242
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
   243
      err "theorem may not contain sort hypotheses"
wenzelm@61039
   244
    else if not (null (Thm.tpairs_of thm)) then
wenzelm@22695
   245
      err "theorem may not contain flex-flex pairs"
wenzelm@61039
   246
    else Thm.prop_of thm
wenzelm@22695
   247
  end;
wenzelm@22695
   248
wenzelm@22695
   249
wenzelm@30564
   250
(* collections of theorems in canonical order *)
wenzelm@24048
   251
wenzelm@24048
   252
val add_thm = update eq_thm_prop;
wenzelm@24048
   253
val del_thm = remove eq_thm_prop;
wenzelm@24048
   254
val merge_thms = merge eq_thm_prop;
wenzelm@24048
   255
wenzelm@33453
   256
val full_rules = Item_Net.init eq_thm_prop (single o Thm.full_prop_of);
wenzelm@33373
   257
val intro_rules = Item_Net.init eq_thm_prop (single o Thm.concl_of);
wenzelm@33373
   258
val elim_rules = Item_Net.init eq_thm_prop (single o Thm.major_prem_of);
wenzelm@30560
   259
wenzelm@30560
   260
wenzelm@22682
   261
wenzelm@54984
   262
(** declared hyps **)
wenzelm@54984
   263
wenzelm@54984
   264
structure Hyps = Proof_Data
wenzelm@54984
   265
(
wenzelm@54993
   266
  type T = Termtab.set * bool;
wenzelm@54993
   267
  fun init _ : T = (Termtab.empty, true);
wenzelm@54984
   268
);
wenzelm@54984
   269
wenzelm@60324
   270
fun declare_hyps raw_ct ctxt =
wenzelm@60324
   271
  let val ct = Thm.transfer_cterm (Proof_Context.theory_of ctxt) raw_ct
wenzelm@60324
   272
  in (Hyps.map o apfst) (Termtab.update (Thm.term_of ct, ())) ctxt end;
wenzelm@54984
   273
wenzelm@54984
   274
fun assume_hyps ct ctxt = (Thm.assume ct, declare_hyps ct ctxt);
wenzelm@54984
   275
wenzelm@54993
   276
val unchecked_hyps = (Hyps.map o apsnd) (K false);
wenzelm@54993
   277
fun restore_hyps ctxt = (Hyps.map o apsnd) (K (#2 (Hyps.get ctxt)));
wenzelm@54993
   278
wenzelm@55633
   279
fun undeclared_hyps context th =
wenzelm@55633
   280
  Thm.hyps_of th
wenzelm@55633
   281
  |> filter_out
wenzelm@55633
   282
    (case context of
wenzelm@55633
   283
      Context.Theory _ => K false
wenzelm@55633
   284
    | Context.Proof ctxt =>
wenzelm@55633
   285
        (case Hyps.get ctxt of
wenzelm@55633
   286
          (_, false) => K true
wenzelm@55633
   287
        | (hyps, _) => Termtab.defined hyps));
wenzelm@55633
   288
wenzelm@54993
   289
fun check_hyps context th =
wenzelm@55633
   290
  (case undeclared_hyps context th of
wenzelm@55633
   291
    [] => th
wenzelm@55633
   292
  | undeclared =>
wenzelm@54993
   293
      let
wenzelm@54993
   294
        val ctxt = Context.cases Syntax.init_pretty_global I context;
wenzelm@54993
   295
      in
wenzelm@54993
   296
        error (Pretty.string_of (Pretty.big_list "Undeclared hyps:"
wenzelm@54993
   297
          (map (Pretty.item o single o Syntax.pretty_term ctxt) undeclared)))
wenzelm@55633
   298
      end);
wenzelm@54984
   299
wenzelm@54984
   300
wenzelm@54984
   301
wenzelm@24980
   302
(** basic derived rules **)
wenzelm@24980
   303
wenzelm@24980
   304
(*Elimination of implication
wenzelm@24980
   305
  A    A ==> B
wenzelm@24980
   306
  ------------
wenzelm@24980
   307
        B
wenzelm@24980
   308
*)
wenzelm@24980
   309
fun elim_implies thA thAB = Thm.implies_elim thAB thA;
wenzelm@24980
   310
wenzelm@26653
   311
wenzelm@26653
   312
(* forall_elim_var(s) *)
wenzelm@26653
   313
wenzelm@26653
   314
local
wenzelm@26653
   315
wenzelm@60951
   316
fun dest_all ct =
wenzelm@60951
   317
  (case Thm.term_of ct of
wenzelm@60951
   318
    Const ("Pure.all", _) $ Abs (a, _, _) =>
wenzelm@60951
   319
      let val (x, ct') = Thm.dest_abs NONE (Thm.dest_arg ct)
wenzelm@60951
   320
      in SOME ((a, Thm.ctyp_of_cterm x), ct') end
wenzelm@60951
   321
  | _ => NONE);
wenzelm@60951
   322
wenzelm@60951
   323
fun dest_all_list ct =
wenzelm@60951
   324
  (case dest_all ct of
wenzelm@60951
   325
    NONE => []
wenzelm@60951
   326
  | SOME (v, ct') => v :: dest_all_list ct');
wenzelm@60951
   327
wenzelm@60951
   328
fun forall_elim_vars_list vars i th =
wenzelm@26653
   329
  let
wenzelm@60950
   330
    val used =
wenzelm@60950
   331
      (Thm.fold_terms o Term.fold_aterms)
wenzelm@60950
   332
        (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I) th [];
wenzelm@60951
   333
    val vars' = (Name.variant_list used (map #1 vars), vars)
wenzelm@60951
   334
      |> ListPair.map (fn (x, (_, T)) => Thm.var ((x, i), T));
wenzelm@60951
   335
  in fold Thm.forall_elim vars' th end;
wenzelm@26653
   336
wenzelm@26653
   337
in
wenzelm@26653
   338
wenzelm@60950
   339
fun forall_elim_vars i th =
wenzelm@60951
   340
  forall_elim_vars_list (dest_all_list (Thm.cprop_of th)) i th;
wenzelm@26653
   341
wenzelm@33697
   342
fun forall_elim_var i th =
wenzelm@60950
   343
  let
wenzelm@60950
   344
    val vars =
wenzelm@60951
   345
      (case dest_all (Thm.cprop_of th) of
wenzelm@60951
   346
        SOME (v, _) => [v]
wenzelm@60951
   347
      | NONE => raise THM ("forall_elim_var", i, [th]));
wenzelm@60951
   348
  in forall_elim_vars_list vars i th end;
wenzelm@26653
   349
wenzelm@26653
   350
end;
wenzelm@26653
   351
wenzelm@26653
   352
wenzelm@60801
   353
(* instantiate by left-to-right occurrence of variables *)
wenzelm@60801
   354
wenzelm@60801
   355
fun instantiate' cTs cts thm =
wenzelm@60801
   356
  let
wenzelm@60801
   357
    fun err msg =
wenzelm@60801
   358
      raise TYPE ("instantiate': " ^ msg,
wenzelm@60801
   359
        map_filter (Option.map Thm.typ_of) cTs,
wenzelm@60801
   360
        map_filter (Option.map Thm.term_of) cts);
wenzelm@60801
   361
wenzelm@60801
   362
    fun zip_vars xs ys =
wenzelm@60801
   363
      zip_options xs ys handle ListPair.UnequalLengths =>
wenzelm@60801
   364
        err "more instantiations than variables in thm";
wenzelm@60801
   365
wenzelm@60801
   366
    val thm' =
wenzelm@60801
   367
      Thm.instantiate ((zip_vars (rev (Thm.fold_terms Term.add_tvars thm [])) cTs), []) thm;
wenzelm@60801
   368
    val thm'' =
wenzelm@60801
   369
      Thm.instantiate ([], zip_vars (rev (Thm.fold_terms Term.add_vars thm' [])) cts) thm';
wenzelm@60801
   370
  in thm'' end;
wenzelm@60801
   371
wenzelm@60801
   372
wenzelm@35985
   373
(* forall_intr_frees: generalization over all suitable Free variables *)
wenzelm@35985
   374
wenzelm@35985
   375
fun forall_intr_frees th =
wenzelm@35985
   376
  let
wenzelm@61041
   377
    val fixed =
wenzelm@61041
   378
      fold Term.add_frees (Thm.terms_of_tpairs (Thm.tpairs_of th) @ Thm.hyps_of th) [];
wenzelm@60821
   379
    val frees =
wenzelm@60821
   380
      Thm.fold_atomic_cterms (fn a =>
wenzelm@60821
   381
        (case Thm.term_of a of
wenzelm@60821
   382
          Free v => not (member (op =) fixed v) ? insert (op aconvc) a
wenzelm@60821
   383
        | _ => I)) th [];
wenzelm@60821
   384
  in fold Thm.forall_intr frees th end;
wenzelm@35985
   385
wenzelm@35985
   386
wenzelm@35845
   387
(* unvarify_global: global schematic variables *)
wenzelm@26653
   388
wenzelm@60825
   389
fun unvarify_global thy th =
wenzelm@24980
   390
  let
wenzelm@24980
   391
    val prop = Thm.full_prop_of th;
wenzelm@35845
   392
    val _ = map Logic.unvarify_global (prop :: Thm.hyps_of th)
wenzelm@24980
   393
      handle TERM (msg, _) => raise THM (msg, 0, [th]);
wenzelm@24980
   394
wenzelm@32279
   395
    val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
wenzelm@24980
   396
    val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
wenzelm@32279
   397
      let val T' = Term_Subst.instantiateT instT T
wenzelm@60805
   398
      in (((a, i), T'), Thm.global_cterm_of thy (Free ((a, T')))) end);
wenzelm@60805
   399
  in Thm.instantiate (map (apsnd (Thm.global_ctyp_of thy)) instT, inst) th end;
wenzelm@24980
   400
wenzelm@60825
   401
fun unvarify_axiom thy = unvarify_global thy o Thm.axiom thy;
wenzelm@60825
   402
wenzelm@26653
   403
wenzelm@26653
   404
(* close_derivation *)
wenzelm@26653
   405
wenzelm@26628
   406
fun close_derivation thm =
wenzelm@36744
   407
  if Thm.derivation_name thm = "" then Thm.name_derivation "" thm
wenzelm@26628
   408
  else thm;
wenzelm@26628
   409
wenzelm@24980
   410
wenzelm@59969
   411
(* user renaming of parameters in a subgoal *)
wenzelm@59969
   412
wenzelm@59969
   413
(*The names, if distinct, are used for the innermost parameters of subgoal i;
wenzelm@59969
   414
  preceding parameters may be renamed to make all parameters distinct.*)
wenzelm@59969
   415
fun rename_params_rule (names, i) st =
wenzelm@59969
   416
  let
wenzelm@59969
   417
    val (_, Bs, Bi, C) = Thm.dest_state (st, i);
wenzelm@59969
   418
    val params = map #1 (Logic.strip_params Bi);
wenzelm@59969
   419
    val short = length params - length names;
wenzelm@59969
   420
    val names' =
wenzelm@59969
   421
      if short < 0 then error "More names than parameters in subgoal!"
wenzelm@59969
   422
      else Name.variant_list names (take short params) @ names;
wenzelm@59969
   423
    val free_names = Term.fold_aterms (fn Free (x, _) => insert (op =) x | _ => I) Bi [];
wenzelm@59969
   424
    val Bi' = Logic.list_rename_params names' Bi;
wenzelm@59969
   425
  in
wenzelm@59969
   426
    (case duplicates (op =) names of
wenzelm@59969
   427
      a :: _ => (warning ("Can't rename.  Bound variables not distinct: " ^ a); st)
wenzelm@59969
   428
    | [] =>
wenzelm@59969
   429
      (case inter (op =) names free_names of
wenzelm@59969
   430
        a :: _ => (warning ("Can't rename.  Bound/Free variable clash: " ^ a); st)
wenzelm@59969
   431
      | [] => Thm.renamed_prop (Logic.list_implies (Bs @ [Bi'], C)) st))
wenzelm@59969
   432
  end;
wenzelm@59969
   433
wenzelm@59969
   434
wenzelm@59969
   435
(* preservation of bound variable names *)
wenzelm@59969
   436
wenzelm@59969
   437
fun rename_boundvars pat obj th =
wenzelm@59969
   438
  (case Term.rename_abs pat obj (Thm.prop_of th) of
wenzelm@59969
   439
    NONE => th
wenzelm@59969
   440
  | SOME prop' => Thm.renamed_prop prop' th);
wenzelm@59969
   441
wenzelm@59969
   442
wenzelm@24980
   443
wenzelm@24980
   444
(** specification primitives **)
wenzelm@24980
   445
wenzelm@30342
   446
(* rules *)
wenzelm@30342
   447
wenzelm@35855
   448
fun stripped_sorts thy t =
wenzelm@35855
   449
  let
wenzelm@60642
   450
    val tfrees = rev (Term.add_tfrees t []);
wenzelm@60642
   451
    val tfrees' = map (fn a => (a, [])) (Name.invent Name.context Name.aT (length tfrees));
wenzelm@60642
   452
    val recover =
wenzelm@60642
   453
      map2 (fn (a', S') => fn (a, S) => (((a', 0), S'), Thm.global_ctyp_of thy (TVar ((a, 0), S))))
wenzelm@60642
   454
        tfrees' tfrees;
wenzelm@60642
   455
    val strip = map (apply2 TFree) (tfrees ~~ tfrees');
wenzelm@35855
   456
    val t' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip))) t;
wenzelm@35855
   457
  in (strip, recover, t') end;
wenzelm@35855
   458
wenzelm@42375
   459
fun add_axiom ctxt (b, prop) thy =
wenzelm@24980
   460
  let
wenzelm@42375
   461
    val _ = Sign.no_vars ctxt prop;
wenzelm@35855
   462
    val (strip, recover, prop') = stripped_sorts thy prop;
wenzelm@35855
   463
    val constraints = map (fn (TFree (_, S), T) => (T, S)) strip;
wenzelm@60367
   464
    val of_sorts = maps (fn (T as TFree (_, S), _) => of_sort (Thm.ctyp_of ctxt T, S)) strip;
wenzelm@36106
   465
wenzelm@42375
   466
    val thy' = thy
wenzelm@51316
   467
      |> Theory.add_axiom ctxt (b, Logic.list_implies (maps Logic.mk_of_sort constraints, prop'));
wenzelm@51316
   468
    val axm_name = Sign.full_name thy' b;
wenzelm@36106
   469
    val axm' = Thm.axiom thy' axm_name;
wenzelm@35988
   470
    val thm =
wenzelm@35988
   471
      Thm.instantiate (recover, []) axm'
wenzelm@60825
   472
      |> unvarify_global thy'
wenzelm@35988
   473
      |> fold elim_implies of_sorts;
wenzelm@36106
   474
  in ((axm_name, thm), thy') end;
wenzelm@24980
   475
wenzelm@42375
   476
fun add_axiom_global arg thy = add_axiom (Syntax.init_pretty_global thy) arg thy;
wenzelm@42375
   477
wenzelm@61261
   478
fun add_def (context as (ctxt, _)) unchecked overloaded (b, prop) thy =
wenzelm@24980
   479
  let
wenzelm@42375
   480
    val _ = Sign.no_vars ctxt prop;
wenzelm@60367
   481
    val prems = map (Thm.cterm_of ctxt) (Logic.strip_imp_prems prop);
wenzelm@35988
   482
    val (_, recover, concl') = stripped_sorts thy (Logic.strip_imp_concl prop);
wenzelm@36106
   483
wenzelm@61261
   484
    val thy' = Theory.add_def context unchecked overloaded (b, concl') thy;
wenzelm@36106
   485
    val axm_name = Sign.full_name thy' b;
wenzelm@36106
   486
    val axm' = Thm.axiom thy' axm_name;
wenzelm@35988
   487
    val thm =
wenzelm@35988
   488
      Thm.instantiate (recover, []) axm'
wenzelm@60825
   489
      |> unvarify_global thy'
wenzelm@35988
   490
      |> fold_rev Thm.implies_intr prems;
wenzelm@36106
   491
  in ((axm_name, thm), thy') end;
wenzelm@24980
   492
wenzelm@42375
   493
fun add_def_global unchecked overloaded arg thy =
wenzelm@61261
   494
  add_def (Syntax.init_pretty_global thy, NONE) unchecked overloaded arg thy;
wenzelm@42375
   495
wenzelm@27866
   496
wenzelm@27866
   497
wenzelm@27866
   498
(** attributes **)
wenzelm@27866
   499
wenzelm@40238
   500
(*attributes subsume any kind of rules or context modifiers*)
wenzelm@45375
   501
type attribute = Context.generic * thm -> Context.generic option * thm option;
wenzelm@40238
   502
wenzelm@30210
   503
type binding = binding * attribute list;
wenzelm@30210
   504
val empty_binding: binding = (Binding.empty, []);
wenzelm@30210
   505
wenzelm@45375
   506
fun rule_attribute f (x, th) = (NONE, SOME (f x th));
wenzelm@45375
   507
fun declaration_attribute f (x, th) = (SOME (f th x), NONE);
wenzelm@45375
   508
fun mixed_attribute f (x, th) = let val (x', th') = f (x, th) in (SOME x', SOME th') end;
wenzelm@45375
   509
wenzelm@46775
   510
fun apply_attribute (att: attribute) th x =
wenzelm@54996
   511
  let val (x', th') = att (x, check_hyps x (Thm.transfer (Context.theory_of x) th))
wenzelm@46775
   512
  in (the_default th th', the_default x x') end;
wenzelm@45375
   513
wenzelm@46775
   514
fun attribute_declaration att th x = #2 (apply_attribute att th x);
wenzelm@27866
   515
wenzelm@27866
   516
fun apply_attributes mk dest =
wenzelm@27866
   517
  let
wenzelm@46775
   518
    fun app [] th x = (th, x)
wenzelm@46775
   519
      | app (att :: atts) th x = apply_attribute att th (mk x) ||> dest |-> app atts;
wenzelm@27866
   520
  in app end;
wenzelm@27866
   521
wenzelm@27866
   522
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@27866
   523
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@27866
   524
wenzelm@27866
   525
fun no_attributes x = (x, []);
wenzelm@27866
   526
fun simple_fact x = [(x, [])];
wenzelm@27866
   527
wenzelm@27866
   528
wenzelm@27866
   529
wenzelm@27866
   530
(*** theorem tags ***)
wenzelm@27866
   531
wenzelm@27866
   532
(* add / delete tags *)
wenzelm@27866
   533
wenzelm@27866
   534
fun tag_rule tg = Thm.map_tags (insert (op =) tg);
wenzelm@27866
   535
fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
wenzelm@27866
   536
wenzelm@45375
   537
fun tag tg = rule_attribute (K (tag_rule tg));
wenzelm@45375
   538
fun untag s = rule_attribute (K (untag_rule s));
wenzelm@27866
   539
wenzelm@27866
   540
wenzelm@30342
   541
(* def_name *)
wenzelm@30342
   542
wenzelm@30342
   543
fun def_name c = c ^ "_def";
wenzelm@30342
   544
wenzelm@30342
   545
fun def_name_optional c "" = def_name c
wenzelm@30342
   546
  | def_name_optional _ name = name;
wenzelm@30342
   547
wenzelm@35238
   548
val def_binding = Binding.map_name def_name;
wenzelm@35238
   549
wenzelm@30433
   550
fun def_binding_optional b name =
wenzelm@35238
   551
  if Binding.is_empty name then def_binding b else name;
wenzelm@30433
   552
wenzelm@30342
   553
wenzelm@27866
   554
(* unofficial theorem names *)
wenzelm@27866
   555
wenzelm@27866
   556
fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
wenzelm@27866
   557
wenzelm@27866
   558
val has_name_hint = can the_name_hint;
wenzelm@27866
   559
val get_name_hint = the_default "??.unknown" o try the_name_hint;
wenzelm@27866
   560
wenzelm@27866
   561
fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
wenzelm@27866
   562
wenzelm@27866
   563
wenzelm@27866
   564
(* theorem kinds *)
wenzelm@27866
   565
wenzelm@27866
   566
val theoremK = "theorem";
wenzelm@27866
   567
val lemmaK = "lemma";
wenzelm@27866
   568
val corollaryK = "corollary";
wenzelm@27866
   569
wenzelm@42473
   570
fun legacy_get_kind thm = the_default "" (Properties.get (Thm.get_tags thm) Markup.kindN);
wenzelm@27866
   571
wenzelm@27866
   572
fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
wenzelm@45375
   573
fun kind k = rule_attribute (K (k <> "" ? kind_rule k));
wenzelm@27866
   574
wenzelm@27866
   575
wenzelm@49011
   576
(* forked proofs *)
wenzelm@49010
   577
wenzelm@49062
   578
structure Proofs = Theory_Data
wenzelm@49010
   579
(
wenzelm@49062
   580
  type T = thm list;
wenzelm@49062
   581
  val empty = [];
wenzelm@49010
   582
  fun extend _ = empty;
wenzelm@49010
   583
  fun merge _ = empty;
wenzelm@49010
   584
);
wenzelm@49010
   585
wenzelm@61059
   586
fun register_proofs more_thms =
wenzelm@61059
   587
  Proofs.map (fold (cons o Thm.trim_context) more_thms);
wenzelm@61059
   588
wenzelm@61059
   589
fun join_theory_proofs thy =
wenzelm@61059
   590
  Thm.join_proofs (map (Thm.transfer thy) (rev (Proofs.get thy)));
wenzelm@49010
   591
wenzelm@49010
   592
wenzelm@22362
   593
open Thm;
wenzelm@22362
   594
wenzelm@22362
   595
end;
wenzelm@22362
   596
wenzelm@32842
   597
structure Basic_Thm: BASIC_THM = Thm;
wenzelm@32842
   598
open Basic_Thm;