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