src/Pure/more_thm.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32842 98702c579ad0
child 33167 f02b804305d6
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
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@32842
    15
end;
wenzelm@32842
    16
wenzelm@22362
    17
signature THM =
wenzelm@22362
    18
sig
wenzelm@22362
    19
  include THM
wenzelm@32842
    20
  structure Ctermtab: TABLE
wenzelm@32842
    21
  structure Thmtab: TABLE
wenzelm@24948
    22
  val aconvc: cterm * cterm -> bool
wenzelm@23491
    23
  val add_cterm_frees: cterm -> cterm list -> cterm list
wenzelm@32198
    24
  val all_name: string * cterm -> cterm -> cterm
wenzelm@32198
    25
  val all: cterm -> cterm -> cterm
wenzelm@22907
    26
  val mk_binop: cterm -> cterm -> cterm -> cterm
wenzelm@22907
    27
  val dest_binop: cterm -> cterm * cterm
wenzelm@22907
    28
  val dest_implies: cterm -> cterm * cterm
wenzelm@22907
    29
  val dest_equals: cterm -> cterm * cterm
wenzelm@22907
    30
  val dest_equals_lhs: cterm -> cterm
wenzelm@22907
    31
  val dest_equals_rhs: cterm -> cterm
wenzelm@22907
    32
  val lhs_of: thm -> cterm
wenzelm@22907
    33
  val rhs_of: thm -> cterm
wenzelm@22362
    34
  val thm_ord: thm * thm -> order
wenzelm@32842
    35
  val cterm_cache: (cterm -> 'a) -> cterm -> 'a
wenzelm@32842
    36
  val thm_cache: (thm -> 'a) -> thm -> 'a
wenzelm@23599
    37
  val is_reflexive: thm -> bool
wenzelm@22362
    38
  val eq_thm: thm * thm -> bool
wenzelm@22362
    39
  val eq_thms: thm list * thm list -> bool
wenzelm@22362
    40
  val eq_thm_thy: thm * thm -> bool
wenzelm@22362
    41
  val eq_thm_prop: thm * thm -> bool
wenzelm@22362
    42
  val equiv_thm: thm * thm -> bool
wenzelm@31944
    43
  val class_triv: theory -> class -> thm
wenzelm@31944
    44
  val of_sort: ctyp * sort -> thm list
wenzelm@28621
    45
  val check_shyps: sort list -> thm -> thm
wenzelm@24048
    46
  val is_dummy: thm -> bool
wenzelm@22695
    47
  val plain_prop_of: thm -> term
wenzelm@24048
    48
  val add_thm: thm -> thm list -> thm list
wenzelm@24048
    49
  val del_thm: thm -> thm list -> thm list
wenzelm@24048
    50
  val merge_thms: thm list * thm list -> thm list
wenzelm@30560
    51
  val intro_rules: thm Item_Net.T
wenzelm@30560
    52
  val elim_rules: thm Item_Net.T
wenzelm@27866
    53
  val elim_implies: thm -> thm -> thm
wenzelm@27866
    54
  val forall_elim_var: int -> thm -> thm
wenzelm@27866
    55
  val forall_elim_vars: int -> thm -> thm
wenzelm@32279
    56
  val certify_inst: theory ->
wenzelm@32279
    57
    ((indexname * sort) * typ) list * ((indexname * typ) * term) list ->
wenzelm@32279
    58
    (ctyp * ctyp) list * (cterm * cterm) list
wenzelm@32279
    59
  val certify_instantiate:
wenzelm@32279
    60
    ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> thm -> thm
wenzelm@27866
    61
  val unvarify: thm -> thm
wenzelm@27866
    62
  val close_derivation: thm -> thm
haftmann@29579
    63
  val add_axiom: binding * term -> theory -> thm * theory
haftmann@29579
    64
  val add_def: bool -> bool -> binding * term -> theory -> thm * theory
wenzelm@30210
    65
  type binding = binding * attribute list
wenzelm@30210
    66
  val empty_binding: binding
wenzelm@27866
    67
  val rule_attribute: (Context.generic -> thm -> thm) -> attribute
wenzelm@27866
    68
  val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
wenzelm@27866
    69
  val theory_attributes: attribute list -> theory * thm -> theory * thm
wenzelm@27866
    70
  val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
wenzelm@27866
    71
  val no_attributes: 'a -> 'a * 'b list
wenzelm@27866
    72
  val simple_fact: 'a -> ('a * 'b list) list
wenzelm@28017
    73
  val tag_rule: Properties.property -> thm -> thm
wenzelm@27866
    74
  val untag_rule: string -> thm -> thm
wenzelm@28017
    75
  val tag: Properties.property -> attribute
wenzelm@27866
    76
  val untag: string -> attribute
wenzelm@27866
    77
  val position_of: thm -> Position.T
wenzelm@27866
    78
  val default_position: Position.T -> thm -> thm
wenzelm@27866
    79
  val default_position_of: thm -> thm -> thm
wenzelm@30342
    80
  val def_name: string -> string
wenzelm@30342
    81
  val def_name_optional: string -> string -> string
wenzelm@30433
    82
  val def_binding_optional: Binding.binding -> Binding.binding -> Binding.binding
wenzelm@27866
    83
  val has_name_hint: thm -> bool
wenzelm@27866
    84
  val get_name_hint: thm -> string
wenzelm@27866
    85
  val put_name_hint: string -> thm -> thm
wenzelm@27866
    86
  val get_group: thm -> string option
wenzelm@27866
    87
  val put_group: string -> thm -> thm
wenzelm@27866
    88
  val group: string -> attribute
wenzelm@22362
    89
  val axiomK: string
wenzelm@22362
    90
  val assumptionK: string
wenzelm@22362
    91
  val definitionK: string
wenzelm@22362
    92
  val theoremK: string
haftmann@31177
    93
  val generatedK : string
wenzelm@22362
    94
  val lemmaK: string
wenzelm@22362
    95
  val corollaryK: string
wenzelm@22362
    96
  val internalK: string
wenzelm@27866
    97
  val has_kind: thm -> bool
wenzelm@27866
    98
  val get_kind: thm -> string
wenzelm@27866
    99
  val kind_rule: string -> thm -> thm
wenzelm@27866
   100
  val kind: string -> attribute
wenzelm@27866
   101
  val kind_internal: attribute
wenzelm@28017
   102
  val has_internal: Properties.property list -> bool
wenzelm@27866
   103
  val is_internal: thm -> bool
wenzelm@22362
   104
end;
wenzelm@22362
   105
wenzelm@22362
   106
structure Thm: THM =
wenzelm@22362
   107
struct
wenzelm@22362
   108
wenzelm@22695
   109
(** basic operations **)
wenzelm@22362
   110
wenzelm@23491
   111
(* collecting cterms *)
wenzelm@23491
   112
wenzelm@23491
   113
val op aconvc = op aconv o pairself Thm.term_of;
wenzelm@23491
   114
wenzelm@23491
   115
fun add_cterm_frees ct =
wenzelm@23491
   116
  let
wenzelm@23491
   117
    val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
wenzelm@23491
   118
    val t = Thm.term_of ct;
wenzelm@23491
   119
  in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
wenzelm@23491
   120
wenzelm@23491
   121
wenzelm@22907
   122
(* cterm constructors and destructors *)
wenzelm@22907
   123
wenzelm@32198
   124
fun all_name (x, t) A =
wenzelm@32198
   125
  let
wenzelm@32198
   126
    val cert = Thm.cterm_of (Thm.theory_of_cterm t);
wenzelm@32198
   127
    val T = #T (Thm.rep_cterm t);
wenzelm@32198
   128
  in Thm.capply (cert (Const ("all", (T --> propT) --> propT))) (Thm.cabs_name (x, t) A) end;
wenzelm@32198
   129
wenzelm@32198
   130
fun all t A = all_name ("", t) A;
wenzelm@32198
   131
wenzelm@22907
   132
fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
wenzelm@22907
   133
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
   134
wenzelm@22907
   135
fun dest_implies ct =
wenzelm@22907
   136
  (case Thm.term_of ct of
wenzelm@22907
   137
    Const ("==>", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   138
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
   139
wenzelm@22907
   140
fun dest_equals ct =
wenzelm@22907
   141
  (case Thm.term_of ct of
wenzelm@22907
   142
    Const ("==", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   143
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
   144
wenzelm@22907
   145
fun dest_equals_lhs ct =
wenzelm@22907
   146
  (case Thm.term_of ct of
wenzelm@22907
   147
    Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
   148
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
   149
wenzelm@22907
   150
fun dest_equals_rhs ct =
wenzelm@22907
   151
  (case Thm.term_of ct of
wenzelm@22907
   152
    Const ("==", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
   153
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
   154
wenzelm@22907
   155
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
   156
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
   157
wenzelm@22907
   158
wenzelm@22907
   159
(* thm order: ignores theory context! *)
wenzelm@22682
   160
wenzelm@22362
   161
fun thm_ord (th1, th2) =
wenzelm@22362
   162
  let
wenzelm@22362
   163
    val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
wenzelm@22362
   164
    val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
wenzelm@22362
   165
  in
wenzelm@29269
   166
    (case TermOrd.fast_term_ord (prop1, prop2) of
wenzelm@22362
   167
      EQUAL =>
wenzelm@29269
   168
        (case list_ord (prod_ord TermOrd.fast_term_ord TermOrd.fast_term_ord) (tpairs1, tpairs2) of
wenzelm@22362
   169
          EQUAL =>
wenzelm@29269
   170
            (case list_ord TermOrd.fast_term_ord (hyps1, hyps2) of
wenzelm@29269
   171
              EQUAL => list_ord TermOrd.sort_ord (shyps1, shyps2)
wenzelm@22362
   172
            | ord => ord)
wenzelm@22362
   173
        | ord => ord)
wenzelm@22362
   174
    | ord => ord)
wenzelm@22362
   175
  end;
wenzelm@22362
   176
wenzelm@22682
   177
wenzelm@32842
   178
(* tables and caches *)
wenzelm@32842
   179
wenzelm@32842
   180
structure Ctermtab = Table(type key = cterm val ord = TermOrd.fast_term_ord o pairself Thm.term_of);
wenzelm@32842
   181
structure Thmtab = Table(type key = thm val ord = thm_ord);
wenzelm@32842
   182
wenzelm@32842
   183
fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
wenzelm@32842
   184
fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
wenzelm@32842
   185
wenzelm@32842
   186
wenzelm@22682
   187
(* equality *)
wenzelm@22682
   188
wenzelm@23599
   189
fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
wenzelm@23599
   190
  handle TERM _ => false;
wenzelm@23599
   191
wenzelm@22362
   192
fun eq_thm ths =
wenzelm@22362
   193
  Context.joinable (pairself Thm.theory_of_thm ths) andalso
wenzelm@26653
   194
  is_equal (thm_ord ths);
wenzelm@22362
   195
wenzelm@22362
   196
val eq_thms = eq_list eq_thm;
wenzelm@22362
   197
wenzelm@26665
   198
val eq_thm_thy = Theory.eq_thy o pairself Thm.theory_of_thm;
wenzelm@22362
   199
val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
wenzelm@22362
   200
wenzelm@22682
   201
wenzelm@22682
   202
(* pattern equivalence *)
wenzelm@22682
   203
wenzelm@22362
   204
fun equiv_thm ths =
wenzelm@22362
   205
  Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
wenzelm@22362
   206
wenzelm@22362
   207
wenzelm@31904
   208
(* type classes and sorts *)
wenzelm@31904
   209
wenzelm@31944
   210
fun class_triv thy c =
wenzelm@31944
   211
  Thm.of_class (Thm.ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
wenzelm@31944
   212
wenzelm@31944
   213
fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
wenzelm@28621
   214
wenzelm@28621
   215
fun check_shyps sorts raw_th =
wenzelm@28621
   216
  let
wenzelm@28621
   217
    val th = Thm.strip_shyps raw_th;
wenzelm@28621
   218
    val prt_sort = Syntax.pretty_sort_global (Thm.theory_of_thm th);
wenzelm@28621
   219
    val pending = Sorts.subtract sorts (Thm.extra_shyps th);
wenzelm@28621
   220
  in
wenzelm@28621
   221
    if null pending then th
wenzelm@28621
   222
    else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
wenzelm@28621
   223
      Pretty.brk 1 :: Pretty.commas (map prt_sort pending))))
wenzelm@28621
   224
  end;
wenzelm@28621
   225
wenzelm@28621
   226
wenzelm@22695
   227
(* misc operations *)
wenzelm@22695
   228
wenzelm@24048
   229
fun is_dummy thm =
wenzelm@24048
   230
  (case try Logic.dest_term (Thm.concl_of thm) of
wenzelm@24048
   231
    NONE => false
wenzelm@24048
   232
  | SOME t => Term.is_dummy_pattern t);
wenzelm@24048
   233
wenzelm@22695
   234
fun plain_prop_of raw_thm =
wenzelm@22695
   235
  let
wenzelm@22695
   236
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   237
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   238
    val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
wenzelm@22695
   239
  in
wenzelm@22695
   240
    if not (null hyps) 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@22695
   244
    else if not (null tpairs) then
wenzelm@22695
   245
      err "theorem may not contain flex-flex pairs"
wenzelm@22695
   246
    else prop
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@30560
   256
val intro_rules = Item_Net.init eq_thm_prop Thm.concl_of;
wenzelm@30560
   257
val elim_rules = Item_Net.init eq_thm_prop Thm.major_prem_of;
wenzelm@30560
   258
wenzelm@30560
   259
wenzelm@22682
   260
wenzelm@24980
   261
(** basic derived rules **)
wenzelm@24980
   262
wenzelm@24980
   263
(*Elimination of implication
wenzelm@24980
   264
  A    A ==> B
wenzelm@24980
   265
  ------------
wenzelm@24980
   266
        B
wenzelm@24980
   267
*)
wenzelm@24980
   268
fun elim_implies thA thAB = Thm.implies_elim thAB thA;
wenzelm@24980
   269
wenzelm@26653
   270
wenzelm@26653
   271
(* forall_elim_var(s) *)
wenzelm@26653
   272
wenzelm@26653
   273
local
wenzelm@26653
   274
wenzelm@26653
   275
fun forall_elim_vars_aux strip_vars i th =
wenzelm@26653
   276
  let
wenzelm@26653
   277
    val thy = Thm.theory_of_thm th;
wenzelm@26653
   278
    val {tpairs, prop, ...} = Thm.rep_thm th;
wenzelm@26653
   279
    val add_used = Term.fold_aterms
wenzelm@26653
   280
      (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
wenzelm@26653
   281
    val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
wenzelm@26653
   282
    val vars = strip_vars prop;
wenzelm@26653
   283
    val cvars = (Name.variant_list used (map #1 vars), vars)
wenzelm@26653
   284
      |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
wenzelm@26653
   285
  in fold Thm.forall_elim cvars th end;
wenzelm@26653
   286
wenzelm@26653
   287
in
wenzelm@26653
   288
wenzelm@26653
   289
val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
wenzelm@26653
   290
wenzelm@26653
   291
fun forall_elim_var i th = forall_elim_vars_aux
wenzelm@26653
   292
  (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
wenzelm@26653
   293
  | _ => raise THM ("forall_elim_vars", i, [th])) i th;
wenzelm@26653
   294
wenzelm@26653
   295
end;
wenzelm@26653
   296
wenzelm@26653
   297
wenzelm@32279
   298
(* certify_instantiate *)
wenzelm@32279
   299
wenzelm@32279
   300
fun certify_inst thy (instT, inst) =
wenzelm@32279
   301
  (map (fn (v, T) => (Thm.ctyp_of thy (TVar v), Thm.ctyp_of thy T)) instT,
wenzelm@32279
   302
    map (fn (v, t) => (Thm.cterm_of thy (Var v), Thm.cterm_of thy t)) inst);
wenzelm@32279
   303
wenzelm@32279
   304
fun certify_instantiate insts th =
wenzelm@32279
   305
  Thm.instantiate (certify_inst (Thm.theory_of_thm th) insts) th;
wenzelm@32279
   306
wenzelm@32279
   307
wenzelm@26653
   308
(* unvarify: global schematic variables *)
wenzelm@26653
   309
wenzelm@24980
   310
fun unvarify th =
wenzelm@24980
   311
  let
wenzelm@24980
   312
    val prop = Thm.full_prop_of th;
wenzelm@24980
   313
    val _ = map Logic.unvarify (prop :: Thm.hyps_of th)
wenzelm@24980
   314
      handle TERM (msg, _) => raise THM (msg, 0, [th]);
wenzelm@24980
   315
wenzelm@32279
   316
    val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
wenzelm@24980
   317
    val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
wenzelm@32279
   318
      let val T' = Term_Subst.instantiateT instT T
wenzelm@32279
   319
      in (((a, i), T'), Free ((a, T'))) end);
wenzelm@32279
   320
  in certify_instantiate (instT, inst) th end;
wenzelm@24980
   321
wenzelm@26653
   322
wenzelm@26653
   323
(* close_derivation *)
wenzelm@26653
   324
wenzelm@26628
   325
fun close_derivation thm =
wenzelm@26628
   326
  if Thm.get_name thm = "" then Thm.put_name "" thm
wenzelm@26628
   327
  else thm;
wenzelm@26628
   328
wenzelm@24980
   329
wenzelm@24980
   330
wenzelm@24980
   331
(** specification primitives **)
wenzelm@24980
   332
wenzelm@30342
   333
(* rules *)
wenzelm@30342
   334
haftmann@29579
   335
fun add_axiom (b, prop) thy =
wenzelm@24980
   336
  let
haftmann@29579
   337
    val b' = if Binding.is_empty b
haftmann@29579
   338
      then Binding.name ("axiom_" ^ serial_string ()) else b;
haftmann@29579
   339
    val thy' = thy |> Theory.add_axioms_i [(b', prop)];
haftmann@29579
   340
    val axm = unvarify (Thm.axiom thy' (Sign.full_name thy' b'));
wenzelm@28116
   341
  in (axm, thy') end;
wenzelm@24980
   342
haftmann@29579
   343
fun add_def unchecked overloaded (b, prop) thy =
wenzelm@24980
   344
  let
wenzelm@24980
   345
    val tfrees = rev (map TFree (Term.add_tfrees prop []));
wenzelm@24980
   346
    val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
wenzelm@24980
   347
    val strip_sorts = tfrees ~~ tfrees';
wenzelm@24980
   348
    val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
wenzelm@24980
   349
wenzelm@24980
   350
    val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
haftmann@29579
   351
    val thy' = Theory.add_defs_i unchecked overloaded [(b, prop')] thy;
haftmann@29579
   352
    val axm' = Thm.axiom thy' (Sign.full_name thy' b);
wenzelm@24980
   353
    val thm = unvarify (Thm.instantiate (recover_sorts, []) axm');
wenzelm@24980
   354
  in (thm, thy') end;
wenzelm@24980
   355
wenzelm@27866
   356
wenzelm@27866
   357
wenzelm@27866
   358
(** attributes **)
wenzelm@27866
   359
wenzelm@30210
   360
type binding = binding * attribute list;
wenzelm@30210
   361
val empty_binding: binding = (Binding.empty, []);
wenzelm@30210
   362
wenzelm@27866
   363
fun rule_attribute f (x, th) = (x, f x th);
wenzelm@27866
   364
fun declaration_attribute f (x, th) = (f th x, th);
wenzelm@27866
   365
wenzelm@27866
   366
fun apply_attributes mk dest =
wenzelm@27866
   367
  let
wenzelm@27866
   368
    fun app [] = I
wenzelm@27866
   369
      | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
wenzelm@27866
   370
  in app end;
wenzelm@27866
   371
wenzelm@27866
   372
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@27866
   373
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@27866
   374
wenzelm@27866
   375
fun no_attributes x = (x, []);
wenzelm@27866
   376
fun simple_fact x = [(x, [])];
wenzelm@27866
   377
wenzelm@27866
   378
wenzelm@27866
   379
wenzelm@27866
   380
(*** theorem tags ***)
wenzelm@27866
   381
wenzelm@27866
   382
(* add / delete tags *)
wenzelm@27866
   383
wenzelm@27866
   384
fun tag_rule tg = Thm.map_tags (insert (op =) tg);
wenzelm@27866
   385
fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
wenzelm@27866
   386
wenzelm@27866
   387
fun tag tg x = rule_attribute (K (tag_rule tg)) x;
wenzelm@27866
   388
fun untag s x = rule_attribute (K (untag_rule s)) x;
wenzelm@27866
   389
wenzelm@27866
   390
wenzelm@27866
   391
(* position *)
wenzelm@27866
   392
wenzelm@27866
   393
val position_of = Position.of_properties o Thm.get_tags;
wenzelm@27866
   394
wenzelm@27866
   395
fun default_position pos = Thm.map_tags (Position.default_properties pos);
wenzelm@27866
   396
val default_position_of = default_position o position_of;
wenzelm@27866
   397
wenzelm@27866
   398
wenzelm@30342
   399
(* def_name *)
wenzelm@30342
   400
wenzelm@30342
   401
fun def_name c = c ^ "_def";
wenzelm@30342
   402
wenzelm@30342
   403
fun def_name_optional c "" = def_name c
wenzelm@30342
   404
  | def_name_optional _ name = name;
wenzelm@30342
   405
wenzelm@30433
   406
fun def_binding_optional b name =
wenzelm@30433
   407
  if Binding.is_empty name then Binding.map_name def_name b else name;
wenzelm@30433
   408
wenzelm@30342
   409
wenzelm@27866
   410
(* unofficial theorem names *)
wenzelm@27866
   411
wenzelm@27866
   412
fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
wenzelm@27866
   413
wenzelm@27866
   414
val has_name_hint = can the_name_hint;
wenzelm@27866
   415
val get_name_hint = the_default "??.unknown" o try the_name_hint;
wenzelm@27866
   416
wenzelm@27866
   417
fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
wenzelm@27866
   418
wenzelm@27866
   419
wenzelm@27866
   420
(* theorem groups *)
wenzelm@27866
   421
wenzelm@28017
   422
fun get_group thm = Properties.get (Thm.get_tags thm) Markup.groupN;
wenzelm@27866
   423
wenzelm@28017
   424
fun put_group name = if name = "" then I else Thm.map_tags (Properties.put (Markup.groupN, name));
wenzelm@27866
   425
wenzelm@27866
   426
fun group name = rule_attribute (K (put_group name));
wenzelm@27866
   427
wenzelm@27866
   428
wenzelm@27866
   429
(* theorem kinds *)
wenzelm@27866
   430
wenzelm@27866
   431
val axiomK = "axiom";
wenzelm@27866
   432
val assumptionK = "assumption";
wenzelm@27866
   433
val definitionK = "definition";
wenzelm@27866
   434
val theoremK = "theorem";
haftmann@31177
   435
val generatedK = "generatedK"
wenzelm@27866
   436
val lemmaK = "lemma";
wenzelm@27866
   437
val corollaryK = "corollary";
wenzelm@27866
   438
val internalK = Markup.internalK;
wenzelm@27866
   439
wenzelm@28017
   440
fun the_kind thm = the (Properties.get (Thm.get_tags thm) Markup.kindN);
wenzelm@27866
   441
wenzelm@27866
   442
val has_kind = can the_kind;
wenzelm@27866
   443
val get_kind = the_default "" o try the_kind;
wenzelm@27866
   444
wenzelm@27866
   445
fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
wenzelm@27866
   446
fun kind k x = if k = "" then x else rule_attribute (K (kind_rule k)) x;
wenzelm@27866
   447
fun kind_internal x = kind internalK x;
wenzelm@27866
   448
fun has_internal tags = exists (fn tg => tg = (Markup.kindN, internalK)) tags;
wenzelm@27866
   449
val is_internal = has_internal o Thm.get_tags;
wenzelm@27866
   450
wenzelm@27866
   451
wenzelm@22362
   452
open Thm;
wenzelm@22362
   453
wenzelm@22362
   454
end;
wenzelm@22362
   455
wenzelm@32842
   456
structure Basic_Thm: BASIC_THM = Thm;
wenzelm@32842
   457
open Basic_Thm;
wenzelm@23170
   458