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