src/Pure/more_thm.ML
author wenzelm
Wed, 15 Feb 2012 23:19:30 +0100
changeset 47368 89ccf66aa73d
parent 46253 3a9f84ad31e7
child 47659 6287653e63ec
permissions -rw-r--r--
renamed Thm.capply to Thm.apply, and Thm.cabs to Thm.lambda in conformance with similar operations in structure Term and Logic;
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@32846
     9
signature BASIC_THM =
wenzelm@32846
    10
sig
wenzelm@32846
    11
  include BASIC_THM
wenzelm@32846
    12
  structure Ctermtab: TABLE
wenzelm@32846
    13
  structure Thmtab: TABLE
wenzelm@32846
    14
  val aconvc: cterm * cterm -> bool
wenzelm@46246
    15
  type attribute = Context.generic * thm -> Context.generic option * thm option
wenzelm@32846
    16
end;
wenzelm@32846
    17
wenzelm@22362
    18
signature THM =
wenzelm@22362
    19
sig
wenzelm@22362
    20
  include THM
wenzelm@32846
    21
  structure Ctermtab: TABLE
wenzelm@32846
    22
  structure Thmtab: TABLE
wenzelm@24948
    23
  val aconvc: cterm * cterm -> bool
wenzelm@23491
    24
  val add_cterm_frees: cterm -> cterm list -> cterm list
wenzelm@32204
    25
  val all_name: string * cterm -> cterm -> cterm
wenzelm@32204
    26
  val all: cterm -> cterm -> cterm
wenzelm@22907
    27
  val mk_binop: cterm -> cterm -> cterm -> cterm
wenzelm@22907
    28
  val dest_binop: cterm -> cterm * cterm
wenzelm@22907
    29
  val dest_implies: cterm -> cterm * cterm
wenzelm@22907
    30
  val dest_equals: cterm -> cterm * cterm
wenzelm@22907
    31
  val dest_equals_lhs: cterm -> cterm
wenzelm@22907
    32
  val dest_equals_rhs: cterm -> cterm
wenzelm@22907
    33
  val lhs_of: thm -> cterm
wenzelm@22907
    34
  val rhs_of: thm -> cterm
wenzelm@22362
    35
  val thm_ord: thm * thm -> order
wenzelm@32846
    36
  val cterm_cache: (cterm -> 'a) -> cterm -> 'a
wenzelm@32846
    37
  val thm_cache: (thm -> 'a) -> thm -> 'a
wenzelm@23599
    38
  val is_reflexive: thm -> bool
wenzelm@22362
    39
  val eq_thm: thm * thm -> bool
wenzelm@22362
    40
  val eq_thms: thm list * thm list -> bool
wenzelm@22362
    41
  val eq_thm_thy: thm * thm -> bool
wenzelm@22362
    42
  val eq_thm_prop: thm * thm -> bool
wenzelm@22362
    43
  val equiv_thm: thm * thm -> bool
wenzelm@31944
    44
  val class_triv: theory -> class -> thm
wenzelm@31944
    45
  val of_sort: ctyp * sort -> thm list
wenzelm@28621
    46
  val check_shyps: sort list -> thm -> thm
wenzelm@24048
    47
  val is_dummy: thm -> bool
wenzelm@22695
    48
  val plain_prop_of: thm -> term
wenzelm@24048
    49
  val add_thm: thm -> thm list -> thm list
wenzelm@24048
    50
  val del_thm: thm -> thm list -> thm list
wenzelm@24048
    51
  val merge_thms: thm list * thm list -> thm list
wenzelm@33459
    52
  val full_rules: thm Item_Net.T
wenzelm@30562
    53
  val intro_rules: thm Item_Net.T
wenzelm@30562
    54
  val elim_rules: thm Item_Net.T
wenzelm@27866
    55
  val elim_implies: thm -> thm -> thm
wenzelm@27866
    56
  val forall_elim_var: int -> thm -> thm
wenzelm@27866
    57
  val forall_elim_vars: int -> thm -> thm
wenzelm@32282
    58
  val certify_inst: theory ->
wenzelm@32282
    59
    ((indexname * sort) * typ) list * ((indexname * typ) * term) list ->
wenzelm@32282
    60
    (ctyp * ctyp) list * (cterm * cterm) list
wenzelm@32282
    61
  val certify_instantiate:
wenzelm@32282
    62
    ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> thm -> thm
wenzelm@35986
    63
  val forall_intr_frees: thm -> thm
wenzelm@35845
    64
  val unvarify_global: thm -> thm
wenzelm@27866
    65
  val close_derivation: thm -> thm
wenzelm@43246
    66
  val add_axiom: Proof.context -> binding * term -> theory -> (string * thm) * theory
wenzelm@43246
    67
  val add_axiom_global: binding * term -> theory -> (string * thm) * theory
wenzelm@43246
    68
  val add_def: Proof.context -> bool -> bool -> binding * term -> theory -> (string * thm) * theory
wenzelm@43246
    69
  val add_def_global: bool -> bool -> binding * term -> theory -> (string * thm) * theory
wenzelm@46246
    70
  type attribute = Context.generic * thm -> Context.generic option * thm option
wenzelm@30214
    71
  type binding = binding * attribute list
wenzelm@30214
    72
  val empty_binding: binding
wenzelm@27866
    73
  val rule_attribute: (Context.generic -> thm -> thm) -> attribute
wenzelm@27866
    74
  val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
wenzelm@46246
    75
  val mixed_attribute: (Context.generic * thm -> Context.generic * thm) -> attribute
wenzelm@46246
    76
  val apply_attribute: attribute -> Context.generic * thm -> Context.generic * thm
wenzelm@46246
    77
  val attribute_declaration: attribute -> thm -> Context.generic -> Context.generic
wenzelm@27866
    78
  val theory_attributes: attribute list -> theory * thm -> theory * thm
wenzelm@27866
    79
  val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
wenzelm@27866
    80
  val no_attributes: 'a -> 'a * 'b list
wenzelm@27866
    81
  val simple_fact: 'a -> ('a * 'b list) list
wenzelm@44654
    82
  val tag_rule: Properties.entry -> thm -> thm
wenzelm@27866
    83
  val untag_rule: string -> thm -> thm
wenzelm@44654
    84
  val tag: Properties.entry -> attribute
wenzelm@27866
    85
  val untag: string -> attribute
wenzelm@30348
    86
  val def_name: string -> string
wenzelm@30348
    87
  val def_name_optional: string -> string -> string
wenzelm@35238
    88
  val def_binding: Binding.binding -> Binding.binding
wenzelm@30447
    89
  val def_binding_optional: Binding.binding -> Binding.binding -> Binding.binding
wenzelm@27866
    90
  val has_name_hint: thm -> bool
wenzelm@27866
    91
  val get_name_hint: thm -> string
wenzelm@27866
    92
  val put_name_hint: string -> thm -> thm
wenzelm@22362
    93
  val theoremK: string
wenzelm@22362
    94
  val lemmaK: string
wenzelm@22362
    95
  val corollaryK: string
wenzelm@43344
    96
  val legacy_get_kind: thm -> string
wenzelm@27866
    97
  val kind_rule: string -> thm -> thm
wenzelm@27866
    98
  val kind: string -> attribute
wenzelm@22362
    99
end;
wenzelm@22362
   100
wenzelm@22362
   101
structure Thm: THM =
wenzelm@22362
   102
struct
wenzelm@22362
   103
wenzelm@22695
   104
(** basic operations **)
wenzelm@22362
   105
wenzelm@23491
   106
(* collecting cterms *)
wenzelm@23491
   107
wenzelm@23491
   108
val op aconvc = op aconv o pairself Thm.term_of;
wenzelm@23491
   109
wenzelm@23491
   110
fun add_cterm_frees ct =
wenzelm@23491
   111
  let
wenzelm@23491
   112
    val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
wenzelm@23491
   113
    val t = Thm.term_of ct;
wenzelm@23491
   114
  in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
wenzelm@23491
   115
wenzelm@23491
   116
wenzelm@22907
   117
(* cterm constructors and destructors *)
wenzelm@22907
   118
wenzelm@32204
   119
fun all_name (x, t) A =
wenzelm@32204
   120
  let
wenzelm@32204
   121
    val cert = Thm.cterm_of (Thm.theory_of_cterm t);
wenzelm@32204
   122
    val T = #T (Thm.rep_cterm t);
wenzelm@47368
   123
  in Thm.apply (cert (Const ("all", (T --> propT) --> propT))) (Thm.lambda_name (x, t) A) end;
wenzelm@32204
   124
wenzelm@32204
   125
fun all t A = all_name ("", t) A;
wenzelm@32204
   126
wenzelm@47368
   127
fun mk_binop c a b = Thm.apply (Thm.apply c a) b;
wenzelm@22907
   128
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
   129
wenzelm@22907
   130
fun dest_implies ct =
wenzelm@22907
   131
  (case Thm.term_of ct of
wenzelm@22907
   132
    Const ("==>", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   133
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
   134
wenzelm@22907
   135
fun dest_equals ct =
wenzelm@22907
   136
  (case Thm.term_of ct of
wenzelm@22907
   137
    Const ("==", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   138
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
   139
wenzelm@22907
   140
fun dest_equals_lhs ct =
wenzelm@22907
   141
  (case Thm.term_of ct of
wenzelm@22907
   142
    Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
   143
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
   144
wenzelm@22907
   145
fun dest_equals_rhs ct =
wenzelm@22907
   146
  (case Thm.term_of ct of
wenzelm@22907
   147
    Const ("==", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
   148
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
   149
wenzelm@22907
   150
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
   151
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
   152
wenzelm@22907
   153
wenzelm@22907
   154
(* thm order: ignores theory context! *)
wenzelm@22682
   155
wenzelm@22362
   156
fun thm_ord (th1, th2) =
wenzelm@22362
   157
  let
wenzelm@22362
   158
    val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
wenzelm@22362
   159
    val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
wenzelm@22362
   160
  in
wenzelm@35408
   161
    (case Term_Ord.fast_term_ord (prop1, prop2) of
wenzelm@22362
   162
      EQUAL =>
wenzelm@35408
   163
        (case list_ord (prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord) (tpairs1, tpairs2) of
wenzelm@22362
   164
          EQUAL =>
wenzelm@35408
   165
            (case list_ord Term_Ord.fast_term_ord (hyps1, hyps2) of
wenzelm@35408
   166
              EQUAL => list_ord Term_Ord.sort_ord (shyps1, shyps2)
wenzelm@22362
   167
            | ord => ord)
wenzelm@22362
   168
        | ord => ord)
wenzelm@22362
   169
    | ord => ord)
wenzelm@22362
   170
  end;
wenzelm@22362
   171
wenzelm@22682
   172
wenzelm@32846
   173
(* tables and caches *)
wenzelm@32846
   174
wenzelm@35408
   175
structure Ctermtab = Table(type key = cterm val ord = Term_Ord.fast_term_ord o pairself Thm.term_of);
wenzelm@32846
   176
structure Thmtab = Table(type key = thm val ord = thm_ord);
wenzelm@32846
   177
wenzelm@32846
   178
fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
wenzelm@32846
   179
fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
wenzelm@32846
   180
wenzelm@32846
   181
wenzelm@22682
   182
(* equality *)
wenzelm@22682
   183
wenzelm@23599
   184
fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
wenzelm@23599
   185
  handle TERM _ => false;
wenzelm@23599
   186
wenzelm@22362
   187
fun eq_thm ths =
wenzelm@22362
   188
  Context.joinable (pairself Thm.theory_of_thm ths) andalso
wenzelm@26653
   189
  is_equal (thm_ord ths);
wenzelm@22362
   190
wenzelm@22362
   191
val eq_thms = eq_list eq_thm;
wenzelm@22362
   192
wenzelm@26665
   193
val eq_thm_thy = Theory.eq_thy o pairself Thm.theory_of_thm;
wenzelm@22362
   194
val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
wenzelm@22362
   195
wenzelm@22682
   196
wenzelm@22682
   197
(* pattern equivalence *)
wenzelm@22682
   198
wenzelm@22362
   199
fun equiv_thm ths =
wenzelm@22362
   200
  Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
wenzelm@22362
   201
wenzelm@22362
   202
wenzelm@31915
   203
(* type classes and sorts *)
wenzelm@31915
   204
wenzelm@31944
   205
fun class_triv thy c =
wenzelm@31944
   206
  Thm.of_class (Thm.ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
wenzelm@31944
   207
wenzelm@31944
   208
fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
wenzelm@28621
   209
wenzelm@28621
   210
fun check_shyps sorts raw_th =
wenzelm@28621
   211
  let
wenzelm@28621
   212
    val th = Thm.strip_shyps raw_th;
wenzelm@28621
   213
    val prt_sort = Syntax.pretty_sort_global (Thm.theory_of_thm th);
wenzelm@28621
   214
    val pending = Sorts.subtract sorts (Thm.extra_shyps th);
wenzelm@28621
   215
  in
wenzelm@28621
   216
    if null pending then th
wenzelm@28621
   217
    else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
wenzelm@28621
   218
      Pretty.brk 1 :: Pretty.commas (map prt_sort pending))))
wenzelm@28621
   219
  end;
wenzelm@28621
   220
wenzelm@28621
   221
wenzelm@22695
   222
(* misc operations *)
wenzelm@22695
   223
wenzelm@24048
   224
fun is_dummy thm =
wenzelm@24048
   225
  (case try Logic.dest_term (Thm.concl_of thm) of
wenzelm@24048
   226
    NONE => false
wenzelm@24048
   227
  | SOME t => Term.is_dummy_pattern t);
wenzelm@24048
   228
wenzelm@22695
   229
fun plain_prop_of raw_thm =
wenzelm@22695
   230
  let
wenzelm@22695
   231
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   232
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   233
    val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
wenzelm@22695
   234
  in
wenzelm@22695
   235
    if not (null hyps) then
wenzelm@22695
   236
      err "theorem may not contain hypotheses"
wenzelm@22695
   237
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
   238
      err "theorem may not contain sort hypotheses"
wenzelm@22695
   239
    else if not (null tpairs) then
wenzelm@22695
   240
      err "theorem may not contain flex-flex pairs"
wenzelm@22695
   241
    else prop
wenzelm@22695
   242
  end;
wenzelm@22695
   243
wenzelm@22695
   244
wenzelm@30564
   245
(* collections of theorems in canonical order *)
wenzelm@24048
   246
wenzelm@24048
   247
val add_thm = update eq_thm_prop;
wenzelm@24048
   248
val del_thm = remove eq_thm_prop;
wenzelm@24048
   249
val merge_thms = merge eq_thm_prop;
wenzelm@24048
   250
wenzelm@33459
   251
val full_rules = Item_Net.init eq_thm_prop (single o Thm.full_prop_of);
wenzelm@33373
   252
val intro_rules = Item_Net.init eq_thm_prop (single o Thm.concl_of);
wenzelm@33373
   253
val elim_rules = Item_Net.init eq_thm_prop (single o Thm.major_prem_of);
wenzelm@30562
   254
wenzelm@30562
   255
wenzelm@22682
   256
wenzelm@24980
   257
(** basic derived rules **)
wenzelm@24980
   258
wenzelm@24980
   259
(*Elimination of implication
wenzelm@24980
   260
  A    A ==> B
wenzelm@24980
   261
  ------------
wenzelm@24980
   262
        B
wenzelm@24980
   263
*)
wenzelm@24980
   264
fun elim_implies thA thAB = Thm.implies_elim thAB thA;
wenzelm@24980
   265
wenzelm@26653
   266
wenzelm@26653
   267
(* forall_elim_var(s) *)
wenzelm@26653
   268
wenzelm@26653
   269
local
wenzelm@26653
   270
wenzelm@26653
   271
fun forall_elim_vars_aux strip_vars i th =
wenzelm@26653
   272
  let
wenzelm@26653
   273
    val thy = Thm.theory_of_thm th;
wenzelm@26653
   274
    val {tpairs, prop, ...} = Thm.rep_thm th;
wenzelm@26653
   275
    val add_used = Term.fold_aterms
wenzelm@26653
   276
      (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
wenzelm@26653
   277
    val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
wenzelm@26653
   278
    val vars = strip_vars prop;
wenzelm@26653
   279
    val cvars = (Name.variant_list used (map #1 vars), vars)
wenzelm@26653
   280
      |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
wenzelm@26653
   281
  in fold Thm.forall_elim cvars th end;
wenzelm@26653
   282
wenzelm@26653
   283
in
wenzelm@26653
   284
wenzelm@26653
   285
val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
wenzelm@26653
   286
wenzelm@33697
   287
fun forall_elim_var i th =
wenzelm@33697
   288
  forall_elim_vars_aux
wenzelm@33697
   289
    (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
wenzelm@33697
   290
      | _ => raise THM ("forall_elim_vars", i, [th])) i th;
wenzelm@26653
   291
wenzelm@26653
   292
end;
wenzelm@26653
   293
wenzelm@26653
   294
wenzelm@32282
   295
(* certify_instantiate *)
wenzelm@32282
   296
wenzelm@32282
   297
fun certify_inst thy (instT, inst) =
wenzelm@32282
   298
  (map (fn (v, T) => (Thm.ctyp_of thy (TVar v), Thm.ctyp_of thy T)) instT,
wenzelm@32282
   299
    map (fn (v, t) => (Thm.cterm_of thy (Var v), Thm.cterm_of thy t)) inst);
wenzelm@32282
   300
wenzelm@32282
   301
fun certify_instantiate insts th =
wenzelm@32282
   302
  Thm.instantiate (certify_inst (Thm.theory_of_thm th) insts) th;
wenzelm@32282
   303
wenzelm@32282
   304
wenzelm@35986
   305
(* forall_intr_frees: generalization over all suitable Free variables *)
wenzelm@35986
   306
wenzelm@35986
   307
fun forall_intr_frees th =
wenzelm@35986
   308
  let
wenzelm@35986
   309
    val thy = Thm.theory_of_thm th;
wenzelm@35986
   310
    val {prop, hyps, tpairs, ...} = Thm.rep_thm th;
wenzelm@35986
   311
    val fixed = fold Term.add_frees (Thm.terms_of_tpairs tpairs @ hyps) [];
wenzelm@35986
   312
    val frees = Term.fold_aterms (fn Free v =>
wenzelm@35986
   313
      if member (op =) fixed v then I else insert (op =) v | _ => I) prop [];
wenzelm@35986
   314
  in fold (Thm.forall_intr o Thm.cterm_of thy o Free) frees th end;
wenzelm@35986
   315
wenzelm@35986
   316
wenzelm@35845
   317
(* unvarify_global: global schematic variables *)
wenzelm@26653
   318
wenzelm@35845
   319
fun unvarify_global th =
wenzelm@24980
   320
  let
wenzelm@24980
   321
    val prop = Thm.full_prop_of th;
wenzelm@35845
   322
    val _ = map Logic.unvarify_global (prop :: Thm.hyps_of th)
wenzelm@24980
   323
      handle TERM (msg, _) => raise THM (msg, 0, [th]);
wenzelm@24980
   324
wenzelm@32282
   325
    val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
wenzelm@24980
   326
    val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
wenzelm@32282
   327
      let val T' = Term_Subst.instantiateT instT T
wenzelm@32282
   328
      in (((a, i), T'), Free ((a, T'))) end);
wenzelm@32282
   329
  in certify_instantiate (instT, inst) th end;
wenzelm@24980
   330
wenzelm@26653
   331
wenzelm@26653
   332
(* close_derivation *)
wenzelm@26653
   333
wenzelm@26628
   334
fun close_derivation thm =
wenzelm@36753
   335
  if Thm.derivation_name thm = "" then Thm.name_derivation "" thm
wenzelm@26628
   336
  else thm;
wenzelm@26628
   337
wenzelm@24980
   338
wenzelm@24980
   339
wenzelm@24980
   340
(** specification primitives **)
wenzelm@24980
   341
wenzelm@30348
   342
(* rules *)
wenzelm@30348
   343
wenzelm@35855
   344
fun stripped_sorts thy t =
wenzelm@35855
   345
  let
wenzelm@35855
   346
    val tfrees = rev (map TFree (Term.add_tfrees t []));
wenzelm@44211
   347
    val tfrees' = map (fn a => TFree (a, [])) (Name.invent Name.context Name.aT (length tfrees));
wenzelm@35855
   348
    val strip = tfrees ~~ tfrees';
wenzelm@35855
   349
    val recover = map (pairself (Thm.ctyp_of thy o Logic.varifyT_global) o swap) strip;
wenzelm@35855
   350
    val t' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip))) t;
wenzelm@35855
   351
  in (strip, recover, t') end;
wenzelm@35855
   352
wenzelm@43246
   353
fun add_axiom ctxt (b, prop) thy =
wenzelm@24980
   354
  let
wenzelm@35853
   355
    val b' = if Binding.is_empty b then Binding.name ("unnamed_axiom_" ^ serial_string ()) else b;
wenzelm@36106
   356
wenzelm@43246
   357
    val _ = Sign.no_vars ctxt prop;
wenzelm@35855
   358
    val (strip, recover, prop') = stripped_sorts thy prop;
wenzelm@35855
   359
    val constraints = map (fn (TFree (_, S), T) => (T, S)) strip;
wenzelm@35855
   360
    val of_sorts = maps (fn (T as TFree (_, S), _) => of_sort (Thm.ctyp_of thy T, S)) strip;
wenzelm@36106
   361
wenzelm@43246
   362
    val thy' = thy
wenzelm@43246
   363
      |> Theory.add_axiom ctxt (b', Logic.list_implies (maps Logic.mk_of_sort constraints, prop'));
wenzelm@36106
   364
    val axm_name = Sign.full_name thy' b';
wenzelm@36106
   365
    val axm' = Thm.axiom thy' axm_name;
wenzelm@35989
   366
    val thm =
wenzelm@35989
   367
      Thm.instantiate (recover, []) axm'
wenzelm@35989
   368
      |> unvarify_global
wenzelm@35989
   369
      |> fold elim_implies of_sorts;
wenzelm@36106
   370
  in ((axm_name, thm), thy') end;
wenzelm@24980
   371
wenzelm@43246
   372
fun add_axiom_global arg thy = add_axiom (Syntax.init_pretty_global thy) arg thy;
wenzelm@43246
   373
wenzelm@43246
   374
fun add_def ctxt unchecked overloaded (b, prop) thy =
wenzelm@24980
   375
  let
wenzelm@43246
   376
    val _ = Sign.no_vars ctxt prop;
wenzelm@35989
   377
    val prems = map (Thm.cterm_of thy) (Logic.strip_imp_prems prop);
wenzelm@35989
   378
    val (_, recover, concl') = stripped_sorts thy (Logic.strip_imp_concl prop);
wenzelm@36106
   379
wenzelm@43246
   380
    val thy' = Theory.add_def ctxt unchecked overloaded (b, concl') thy;
wenzelm@36106
   381
    val axm_name = Sign.full_name thy' b;
wenzelm@36106
   382
    val axm' = Thm.axiom thy' axm_name;
wenzelm@35989
   383
    val thm =
wenzelm@35989
   384
      Thm.instantiate (recover, []) axm'
wenzelm@35989
   385
      |> unvarify_global
wenzelm@35989
   386
      |> fold_rev Thm.implies_intr prems;
wenzelm@36106
   387
  in ((axm_name, thm), thy') end;
wenzelm@24980
   388
wenzelm@43246
   389
fun add_def_global unchecked overloaded arg thy =
wenzelm@43246
   390
  add_def (Syntax.init_pretty_global thy) unchecked overloaded arg thy;
wenzelm@43246
   391
wenzelm@27866
   392
wenzelm@27866
   393
wenzelm@27866
   394
(** attributes **)
wenzelm@27866
   395
wenzelm@40494
   396
(*attributes subsume any kind of rules or context modifiers*)
wenzelm@46246
   397
type attribute = Context.generic * thm -> Context.generic option * thm option;
wenzelm@40494
   398
wenzelm@30214
   399
type binding = binding * attribute list;
wenzelm@30214
   400
val empty_binding: binding = (Binding.empty, []);
wenzelm@30214
   401
wenzelm@46246
   402
fun rule_attribute f (x, th) = (NONE, SOME (f x th));
wenzelm@46246
   403
fun declaration_attribute f (x, th) = (SOME (f th x), NONE);
wenzelm@46246
   404
fun mixed_attribute f (x, th) = let val (x', th') = f (x, th) in (SOME x', SOME th') end;
wenzelm@46246
   405
wenzelm@46253
   406
fun apply_attribute (att: attribute) (x, th) =
wenzelm@46246
   407
  let val (x', th') = att (x, th)
wenzelm@46246
   408
  in (the_default x x', the_default th th') end;
wenzelm@46246
   409
wenzelm@46246
   410
fun attribute_declaration att th x = #1 (apply_attribute att (x, th));
wenzelm@27866
   411
wenzelm@27866
   412
fun apply_attributes mk dest =
wenzelm@27866
   413
  let
wenzelm@27866
   414
    fun app [] = I
wenzelm@46246
   415
      | app (att :: atts) = fn (x, th) => apply_attribute att (mk x, th) |>> dest |> app atts;
wenzelm@27866
   416
  in app end;
wenzelm@27866
   417
wenzelm@27866
   418
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@27866
   419
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@27866
   420
wenzelm@27866
   421
fun no_attributes x = (x, []);
wenzelm@27866
   422
fun simple_fact x = [(x, [])];
wenzelm@27866
   423
wenzelm@27866
   424
wenzelm@27866
   425
wenzelm@27866
   426
(*** theorem tags ***)
wenzelm@27866
   427
wenzelm@27866
   428
(* add / delete tags *)
wenzelm@27866
   429
wenzelm@27866
   430
fun tag_rule tg = Thm.map_tags (insert (op =) tg);
wenzelm@27866
   431
fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
wenzelm@27866
   432
wenzelm@46246
   433
fun tag tg = rule_attribute (K (tag_rule tg));
wenzelm@46246
   434
fun untag s = rule_attribute (K (untag_rule s));
wenzelm@27866
   435
wenzelm@27866
   436
wenzelm@30348
   437
(* def_name *)
wenzelm@30348
   438
wenzelm@30348
   439
fun def_name c = c ^ "_def";
wenzelm@30348
   440
wenzelm@30348
   441
fun def_name_optional c "" = def_name c
wenzelm@30348
   442
  | def_name_optional _ name = name;
wenzelm@30348
   443
wenzelm@35238
   444
val def_binding = Binding.map_name def_name;
wenzelm@35238
   445
wenzelm@30447
   446
fun def_binding_optional b name =
wenzelm@35238
   447
  if Binding.is_empty name then def_binding b else name;
wenzelm@30447
   448
wenzelm@30348
   449
wenzelm@27866
   450
(* unofficial theorem names *)
wenzelm@27866
   451
wenzelm@27866
   452
fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
wenzelm@27866
   453
wenzelm@27866
   454
val has_name_hint = can the_name_hint;
wenzelm@27866
   455
val get_name_hint = the_default "??.unknown" o try the_name_hint;
wenzelm@27866
   456
wenzelm@27866
   457
fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
wenzelm@27866
   458
wenzelm@27866
   459
wenzelm@27866
   460
(* theorem kinds *)
wenzelm@27866
   461
wenzelm@27866
   462
val theoremK = "theorem";
wenzelm@27866
   463
val lemmaK = "lemma";
wenzelm@27866
   464
val corollaryK = "corollary";
wenzelm@27866
   465
wenzelm@43344
   466
fun legacy_get_kind thm = the_default "" (Properties.get (Thm.get_tags thm) Markup.kindN);
wenzelm@27866
   467
wenzelm@27866
   468
fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
wenzelm@46246
   469
fun kind k = rule_attribute (K (k <> "" ? kind_rule k));
wenzelm@27866
   470
wenzelm@27866
   471
wenzelm@22362
   472
open Thm;
wenzelm@22362
   473
wenzelm@22362
   474
end;
wenzelm@22362
   475
wenzelm@32846
   476
structure Basic_Thm: BASIC_THM = Thm;
wenzelm@32846
   477
open Basic_Thm;
wenzelm@23170
   478