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