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