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