src/Pure/more_thm.ML
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66168 fcd09fc36d7f
child 67559 833d154ab189
permissions -rw-r--r--
more robust sorted_entries;
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@61268
    12
  val show_consts: bool Config.T
wenzelm@61268
    13
  val show_hyps: bool Config.T
wenzelm@61268
    14
  val show_tags: bool Config.T
wenzelm@32842
    15
  structure Ctermtab: TABLE
wenzelm@32842
    16
  structure Thmtab: TABLE
wenzelm@32842
    17
  val aconvc: cterm * cterm -> bool
wenzelm@45375
    18
  type attribute = Context.generic * thm -> Context.generic option * thm option
wenzelm@32842
    19
end;
wenzelm@32842
    20
wenzelm@22362
    21
signature THM =
wenzelm@22362
    22
sig
wenzelm@22362
    23
  include THM
wenzelm@32842
    24
  structure Ctermtab: TABLE
wenzelm@32842
    25
  structure Thmtab: TABLE
wenzelm@60952
    26
  val eq_ctyp: ctyp * ctyp -> bool
wenzelm@24948
    27
  val aconvc: cterm * cterm -> bool
wenzelm@60952
    28
  val add_tvars: thm -> ctyp list -> ctyp list
wenzelm@60818
    29
  val add_frees: thm -> cterm list -> cterm list
wenzelm@60818
    30
  val add_vars: thm -> cterm list -> cterm list
wenzelm@60938
    31
  val all_name: Proof.context -> string * cterm -> cterm -> cterm
wenzelm@60938
    32
  val all: Proof.context -> cterm -> cterm -> cterm
wenzelm@22907
    33
  val mk_binop: cterm -> cterm -> cterm -> cterm
wenzelm@22907
    34
  val dest_binop: cterm -> cterm * cterm
wenzelm@22907
    35
  val dest_implies: cterm -> cterm * cterm
wenzelm@22907
    36
  val dest_equals: cterm -> cterm * cterm
wenzelm@22907
    37
  val dest_equals_lhs: cterm -> cterm
wenzelm@22907
    38
  val dest_equals_rhs: cterm -> cterm
wenzelm@22907
    39
  val lhs_of: thm -> cterm
wenzelm@22907
    40
  val rhs_of: thm -> cterm
wenzelm@22362
    41
  val thm_ord: thm * thm -> order
wenzelm@32842
    42
  val cterm_cache: (cterm -> 'a) -> cterm -> 'a
wenzelm@32842
    43
  val thm_cache: (thm -> 'a) -> thm -> 'a
wenzelm@23599
    44
  val is_reflexive: thm -> bool
wenzelm@22362
    45
  val eq_thm: thm * thm -> bool
wenzelm@22362
    46
  val eq_thm_prop: thm * thm -> bool
wenzelm@52683
    47
  val eq_thm_strict: thm * thm -> bool
wenzelm@60817
    48
  val equiv_thm: theory -> thm * thm -> bool
wenzelm@31944
    49
  val class_triv: theory -> class -> thm
wenzelm@31944
    50
  val of_sort: ctyp * sort -> thm list
wenzelm@24048
    51
  val is_dummy: thm -> bool
wenzelm@22695
    52
  val plain_prop_of: thm -> term
wenzelm@24048
    53
  val add_thm: thm -> thm list -> thm list
wenzelm@24048
    54
  val del_thm: thm -> thm list -> thm list
wenzelm@24048
    55
  val merge_thms: thm list * thm list -> thm list
wenzelm@33453
    56
  val full_rules: thm Item_Net.T
wenzelm@30560
    57
  val intro_rules: thm Item_Net.T
wenzelm@30560
    58
  val elim_rules: thm Item_Net.T
wenzelm@54984
    59
  val declare_hyps: cterm -> Proof.context -> Proof.context
wenzelm@54984
    60
  val assume_hyps: cterm -> Proof.context -> thm * Proof.context
wenzelm@54993
    61
  val unchecked_hyps: Proof.context -> Proof.context
wenzelm@54993
    62
  val restore_hyps: Proof.context -> Proof.context -> Proof.context
wenzelm@55633
    63
  val undeclared_hyps: Context.generic -> thm -> term list
wenzelm@54993
    64
  val check_hyps: Context.generic -> thm -> thm
wenzelm@61508
    65
  val declare_term_sorts: term -> Proof.context -> Proof.context
wenzelm@61509
    66
  val extra_shyps': Proof.context -> thm -> sort list
wenzelm@61508
    67
  val check_shyps: Proof.context -> thm -> thm
wenzelm@61508
    68
  val weaken_sorts': Proof.context -> cterm -> cterm
wenzelm@27866
    69
  val elim_implies: thm -> thm -> thm
wenzelm@61339
    70
  val forall_intr_name: string * cterm -> thm -> thm
wenzelm@27866
    71
  val forall_elim_var: int -> thm -> thm
wenzelm@27866
    72
  val forall_elim_vars: int -> thm -> thm
wenzelm@60801
    73
  val instantiate': ctyp option list -> cterm option list -> thm -> thm
wenzelm@35985
    74
  val forall_intr_frees: thm -> thm
wenzelm@60825
    75
  val unvarify_global: theory -> thm -> thm
wenzelm@60825
    76
  val unvarify_axiom: theory -> string -> thm
wenzelm@27866
    77
  val close_derivation: thm -> thm
wenzelm@59969
    78
  val rename_params_rule: string list * int -> thm -> thm
wenzelm@59969
    79
  val rename_boundvars: term -> term -> thm -> thm
wenzelm@42375
    80
  val add_axiom: Proof.context -> binding * term -> theory -> (string * thm) * theory
wenzelm@42375
    81
  val add_axiom_global: binding * term -> theory -> (string * thm) * theory
wenzelm@61261
    82
  val add_def: Defs.context -> bool -> bool -> binding * term -> theory -> (string * thm) * theory
wenzelm@42375
    83
  val add_def_global: bool -> bool -> binding * term -> theory -> (string * thm) * theory
wenzelm@45375
    84
  type attribute = Context.generic * thm -> Context.generic option * thm option
wenzelm@30210
    85
  type binding = binding * attribute list
wenzelm@46830
    86
  val tag_rule: string * string -> thm -> thm
wenzelm@27866
    87
  val untag_rule: string -> thm -> thm
wenzelm@61852
    88
  val is_free_dummy: thm -> bool
wenzelm@61852
    89
  val tag_free_dummy: thm -> thm
wenzelm@30342
    90
  val def_name: string -> string
wenzelm@30342
    91
  val def_name_optional: string -> string -> string
wenzelm@35238
    92
  val def_binding: Binding.binding -> Binding.binding
wenzelm@30433
    93
  val def_binding_optional: Binding.binding -> Binding.binding -> Binding.binding
wenzelm@62093
    94
  val make_def_binding: bool -> 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@42473
    99
  val legacy_get_kind: thm -> string
wenzelm@27866
   100
  val kind_rule: string -> thm -> thm
wenzelm@61853
   101
  val rule_attribute: thm list -> (Context.generic -> thm -> thm) -> attribute
wenzelm@61853
   102
  val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
wenzelm@61853
   103
  val mixed_attribute: (Context.generic * thm -> Context.generic * thm) -> attribute
wenzelm@61853
   104
  val apply_attribute: attribute -> thm -> Context.generic -> thm * Context.generic
wenzelm@61853
   105
  val attribute_declaration: attribute -> thm -> Context.generic -> Context.generic
wenzelm@61853
   106
  val theory_attributes: attribute list -> thm -> theory -> thm * theory
wenzelm@61853
   107
  val proof_attributes: attribute list -> thm -> Proof.context -> thm * Proof.context
wenzelm@61853
   108
  val no_attributes: 'a -> 'a * 'b list
wenzelm@61853
   109
  val simple_fact: 'a -> ('a * 'b list) list
wenzelm@61853
   110
  val tag: string * string -> attribute
wenzelm@61853
   111
  val untag: string -> attribute
wenzelm@27866
   112
  val kind: string -> attribute
wenzelm@49062
   113
  val register_proofs: thm list -> theory -> theory
wenzelm@64574
   114
  val consolidate_theory: theory -> unit
wenzelm@61268
   115
  val show_consts_raw: Config.raw
wenzelm@61268
   116
  val show_consts: bool Config.T
wenzelm@61268
   117
  val show_hyps_raw: Config.raw
wenzelm@61268
   118
  val show_hyps: bool Config.T
wenzelm@61268
   119
  val show_tags_raw: Config.raw
wenzelm@61268
   120
  val show_tags: bool Config.T
wenzelm@61268
   121
  val pretty_flexpair: Proof.context -> term * term -> Pretty.T
wenzelm@61268
   122
  val pretty_thm_raw: Proof.context -> {quote: bool, show_hyps: bool} -> thm -> Pretty.T
wenzelm@61268
   123
  val pretty_thm: Proof.context -> thm -> Pretty.T
wenzelm@61268
   124
  val pretty_thm_item: Proof.context -> thm -> Pretty.T
wenzelm@61268
   125
  val pretty_thm_global: theory -> thm -> Pretty.T
wenzelm@61268
   126
  val string_of_thm: Proof.context -> thm -> string
wenzelm@61268
   127
  val string_of_thm_global: theory -> thm -> string
wenzelm@22362
   128
end;
wenzelm@22362
   129
wenzelm@22362
   130
structure Thm: THM =
wenzelm@22362
   131
struct
wenzelm@22362
   132
wenzelm@22695
   133
(** basic operations **)
wenzelm@22362
   134
wenzelm@60952
   135
(* collecting ctyps and cterms *)
wenzelm@23491
   136
wenzelm@60952
   137
val eq_ctyp = op = o apply2 Thm.typ_of;
wenzelm@59058
   138
val op aconvc = op aconv o apply2 Thm.term_of;
wenzelm@23491
   139
wenzelm@60952
   140
val add_tvars = Thm.fold_atomic_ctyps (fn a => is_TVar (Thm.typ_of a) ? insert eq_ctyp a);
wenzelm@60818
   141
val add_frees = Thm.fold_atomic_cterms (fn a => is_Free (Thm.term_of a) ? insert (op aconvc) a);
wenzelm@60818
   142
val add_vars = Thm.fold_atomic_cterms (fn a => is_Var (Thm.term_of a) ? insert (op aconvc) a);
wenzelm@23491
   143
wenzelm@23491
   144
wenzelm@22907
   145
(* cterm constructors and destructors *)
wenzelm@22907
   146
wenzelm@60938
   147
fun all_name ctxt (x, t) A =
wenzelm@32198
   148
  let
wenzelm@59586
   149
    val T = Thm.typ_of_cterm t;
wenzelm@60938
   150
    val all_const = Thm.cterm_of ctxt (Const ("Pure.all", (T --> propT) --> propT));
wenzelm@60938
   151
  in Thm.apply all_const (Thm.lambda_name (x, t) A) end;
wenzelm@32198
   152
wenzelm@60938
   153
fun all ctxt t A = all_name ctxt ("", t) A;
wenzelm@32198
   154
wenzelm@46497
   155
fun mk_binop c a b = Thm.apply (Thm.apply c a) b;
wenzelm@22907
   156
fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
wenzelm@22907
   157
wenzelm@22907
   158
fun dest_implies ct =
wenzelm@22907
   159
  (case Thm.term_of ct of
wenzelm@56245
   160
    Const ("Pure.imp", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   161
  | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
wenzelm@22907
   162
wenzelm@22907
   163
fun dest_equals ct =
wenzelm@22907
   164
  (case Thm.term_of ct of
wenzelm@56245
   165
    Const ("Pure.eq", _) $ _ $ _ => dest_binop ct
wenzelm@22907
   166
  | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
wenzelm@22907
   167
wenzelm@22907
   168
fun dest_equals_lhs ct =
wenzelm@22907
   169
  (case Thm.term_of ct of
wenzelm@56245
   170
    Const ("Pure.eq", _) $ _ $ _ => Thm.dest_arg1 ct
wenzelm@22907
   171
  | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
wenzelm@22907
   172
wenzelm@22907
   173
fun dest_equals_rhs ct =
wenzelm@22907
   174
  (case Thm.term_of ct of
wenzelm@56245
   175
    Const ("Pure.eq", _) $ _ $ _ => Thm.dest_arg ct
wenzelm@22907
   176
  | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
wenzelm@22907
   177
wenzelm@22907
   178
val lhs_of = dest_equals_lhs o Thm.cprop_of;
wenzelm@22907
   179
val rhs_of = dest_equals_rhs o Thm.cprop_of;
wenzelm@22907
   180
wenzelm@22907
   181
wenzelm@22907
   182
(* thm order: ignores theory context! *)
wenzelm@22682
   183
wenzelm@61039
   184
fun thm_ord ths =
wenzelm@61039
   185
  (case Term_Ord.fast_term_ord (apply2 Thm.prop_of ths) of
wenzelm@61039
   186
    EQUAL =>
wenzelm@61039
   187
      (case
wenzelm@61039
   188
        list_ord (prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord)
wenzelm@61039
   189
          (apply2 Thm.tpairs_of ths)
wenzelm@61039
   190
       of
wenzelm@61039
   191
        EQUAL =>
wenzelm@61039
   192
          (case list_ord Term_Ord.fast_term_ord (apply2 Thm.hyps_of ths) of
wenzelm@61039
   193
            EQUAL => list_ord Term_Ord.sort_ord (apply2 Thm.shyps_of ths)
wenzelm@61039
   194
          | ord => ord)
wenzelm@61039
   195
      | ord => ord)
wenzelm@61039
   196
  | ord => ord);
wenzelm@22362
   197
wenzelm@22682
   198
wenzelm@32842
   199
(* tables and caches *)
wenzelm@32842
   200
wenzelm@59058
   201
structure Ctermtab = Table(type key = cterm val ord = Term_Ord.fast_term_ord o apply2 Thm.term_of);
wenzelm@32842
   202
structure Thmtab = Table(type key = thm val ord = thm_ord);
wenzelm@32842
   203
wenzelm@32842
   204
fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
wenzelm@32842
   205
fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
wenzelm@32842
   206
wenzelm@32842
   207
wenzelm@22682
   208
(* equality *)
wenzelm@22682
   209
wenzelm@23599
   210
fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
wenzelm@23599
   211
  handle TERM _ => false;
wenzelm@23599
   212
wenzelm@55547
   213
val eq_thm = is_equal o thm_ord;
wenzelm@22362
   214
wenzelm@59058
   215
val eq_thm_prop = op aconv o apply2 Thm.full_prop_of;
wenzelm@22362
   216
wenzelm@52683
   217
fun eq_thm_strict ths =
wenzelm@55547
   218
  eq_thm ths andalso
wenzelm@65458
   219
  Context.eq_thy_id (apply2 Thm.theory_id ths) andalso
wenzelm@61040
   220
  op = (apply2 Thm.maxidx_of ths) andalso
wenzelm@61040
   221
  op = (apply2 Thm.get_tags ths);
wenzelm@52683
   222
wenzelm@22682
   223
wenzelm@22682
   224
(* pattern equivalence *)
wenzelm@22682
   225
wenzelm@60817
   226
fun equiv_thm thy ths =
wenzelm@60817
   227
  Pattern.equiv thy (apply2 (Thm.full_prop_of o Thm.transfer thy) ths);
wenzelm@22362
   228
wenzelm@22362
   229
wenzelm@31904
   230
(* type classes and sorts *)
wenzelm@31904
   231
wenzelm@31944
   232
fun class_triv thy c =
wenzelm@59621
   233
  Thm.of_class (Thm.global_ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
wenzelm@31944
   234
wenzelm@31944
   235
fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
wenzelm@28621
   236
wenzelm@28621
   237
wenzelm@22695
   238
(* misc operations *)
wenzelm@22695
   239
wenzelm@24048
   240
fun is_dummy thm =
wenzelm@24048
   241
  (case try Logic.dest_term (Thm.concl_of thm) of
wenzelm@24048
   242
    NONE => false
wenzelm@58001
   243
  | SOME t => Term.is_dummy_pattern (Term.head_of t));
wenzelm@24048
   244
wenzelm@22695
   245
fun plain_prop_of raw_thm =
wenzelm@22695
   246
  let
wenzelm@22695
   247
    val thm = Thm.strip_shyps raw_thm;
wenzelm@22695
   248
    fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
wenzelm@22695
   249
  in
wenzelm@61039
   250
    if not (null (Thm.hyps_of thm)) then
wenzelm@22695
   251
      err "theorem may not contain hypotheses"
wenzelm@22695
   252
    else if not (null (Thm.extra_shyps thm)) then
wenzelm@22695
   253
      err "theorem may not contain sort hypotheses"
wenzelm@61039
   254
    else if not (null (Thm.tpairs_of thm)) then
wenzelm@22695
   255
      err "theorem may not contain flex-flex pairs"
wenzelm@61039
   256
    else Thm.prop_of thm
wenzelm@22695
   257
  end;
wenzelm@22695
   258
wenzelm@22695
   259
wenzelm@30564
   260
(* collections of theorems in canonical order *)
wenzelm@24048
   261
wenzelm@24048
   262
val add_thm = update eq_thm_prop;
wenzelm@24048
   263
val del_thm = remove eq_thm_prop;
wenzelm@24048
   264
val merge_thms = merge eq_thm_prop;
wenzelm@24048
   265
wenzelm@33453
   266
val full_rules = Item_Net.init eq_thm_prop (single o Thm.full_prop_of);
wenzelm@33373
   267
val intro_rules = Item_Net.init eq_thm_prop (single o Thm.concl_of);
wenzelm@33373
   268
val elim_rules = Item_Net.init eq_thm_prop (single o Thm.major_prem_of);
wenzelm@30560
   269
wenzelm@30560
   270
wenzelm@22682
   271
wenzelm@61508
   272
(** declared hyps and sort hyps **)
wenzelm@54984
   273
wenzelm@54984
   274
structure Hyps = Proof_Data
wenzelm@54984
   275
(
wenzelm@61508
   276
  type T = {checked_hyps: bool, hyps: Termtab.set, shyps: sort Ord_List.T};
wenzelm@61508
   277
  fun init _ : T = {checked_hyps = true, hyps = Termtab.empty, shyps = []};
wenzelm@54984
   278
);
wenzelm@54984
   279
wenzelm@61508
   280
fun map_hyps f = Hyps.map (fn {checked_hyps, hyps, shyps} =>
wenzelm@61508
   281
  let val (checked_hyps', hyps', shyps') = f (checked_hyps, hyps, shyps)
wenzelm@61508
   282
  in {checked_hyps = checked_hyps', hyps = hyps', shyps = shyps'} end);
wenzelm@61508
   283
wenzelm@61508
   284
wenzelm@61508
   285
(* hyps *)
wenzelm@61508
   286
wenzelm@61508
   287
fun declare_hyps raw_ct ctxt = ctxt |> map_hyps (fn (checked_hyps, hyps, shyps) =>
wenzelm@61508
   288
  let
wenzelm@61508
   289
    val ct = Thm.transfer_cterm (Proof_Context.theory_of ctxt) raw_ct;
wenzelm@61508
   290
    val hyps' = Termtab.update (Thm.term_of ct, ()) hyps;
wenzelm@61508
   291
  in (checked_hyps, hyps', shyps) end);
wenzelm@54984
   292
wenzelm@54984
   293
fun assume_hyps ct ctxt = (Thm.assume ct, declare_hyps ct ctxt);
wenzelm@54984
   294
wenzelm@61508
   295
val unchecked_hyps = map_hyps (fn (_, hyps, shyps) => (false, hyps, shyps));
wenzelm@61508
   296
wenzelm@61508
   297
fun restore_hyps ctxt =
wenzelm@61508
   298
  map_hyps (fn (_, hyps, shyps) => (#checked_hyps (Hyps.get ctxt), hyps, shyps));
wenzelm@54993
   299
wenzelm@55633
   300
fun undeclared_hyps context th =
wenzelm@55633
   301
  Thm.hyps_of th
wenzelm@55633
   302
  |> filter_out
wenzelm@55633
   303
    (case context of
wenzelm@55633
   304
      Context.Theory _ => K false
wenzelm@55633
   305
    | Context.Proof ctxt =>
wenzelm@55633
   306
        (case Hyps.get ctxt of
wenzelm@61508
   307
          {checked_hyps = false, ...} => K true
wenzelm@61508
   308
        | {hyps, ...} => Termtab.defined hyps));
wenzelm@55633
   309
wenzelm@54993
   310
fun check_hyps context th =
wenzelm@55633
   311
  (case undeclared_hyps context th of
wenzelm@55633
   312
    [] => th
wenzelm@55633
   313
  | undeclared =>
wenzelm@61263
   314
      error (Pretty.string_of (Pretty.big_list "Undeclared hyps:"
wenzelm@61263
   315
        (map (Pretty.item o single o Syntax.pretty_term (Syntax.init_pretty context)) undeclared))));
wenzelm@54984
   316
wenzelm@54984
   317
wenzelm@61508
   318
(* shyps *)
wenzelm@61508
   319
wenzelm@61508
   320
fun declare_term_sorts t =
wenzelm@61508
   321
  map_hyps (fn (checked_hyps, hyps, shyps) =>
wenzelm@61508
   322
    (checked_hyps, hyps, Sorts.insert_term t shyps));
wenzelm@61508
   323
wenzelm@61509
   324
fun extra_shyps' ctxt th =
wenzelm@61509
   325
  Sorts.subtract (#shyps (Hyps.get ctxt)) (Thm.extra_shyps th);
wenzelm@61509
   326
wenzelm@61508
   327
fun check_shyps ctxt raw_th =
wenzelm@61508
   328
  let
wenzelm@61508
   329
    val th = Thm.strip_shyps raw_th;
wenzelm@61509
   330
    val extra_shyps = extra_shyps' ctxt th;
wenzelm@61508
   331
  in
wenzelm@61509
   332
    if null extra_shyps then th
wenzelm@61508
   333
    else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
wenzelm@61509
   334
      Pretty.brk 1 :: Pretty.commas (map (Syntax.pretty_sort ctxt) extra_shyps))))
wenzelm@61508
   335
  end;
wenzelm@61508
   336
wenzelm@61508
   337
val weaken_sorts' = Thm.weaken_sorts o #shyps o Hyps.get;
wenzelm@61508
   338
wenzelm@61508
   339
wenzelm@54984
   340
wenzelm@24980
   341
(** basic derived rules **)
wenzelm@24980
   342
wenzelm@24980
   343
(*Elimination of implication
wenzelm@24980
   344
  A    A ==> B
wenzelm@24980
   345
  ------------
wenzelm@24980
   346
        B
wenzelm@24980
   347
*)
wenzelm@24980
   348
fun elim_implies thA thAB = Thm.implies_elim thAB thA;
wenzelm@24980
   349
wenzelm@26653
   350
wenzelm@61339
   351
(* forall_intr_name *)
wenzelm@61339
   352
wenzelm@61339
   353
fun forall_intr_name (a, x) th =
wenzelm@61339
   354
  let
wenzelm@61339
   355
    val th' = Thm.forall_intr x th;
wenzelm@61339
   356
    val prop' = (case Thm.prop_of th' of all $ Abs (_, T, b) => all $ Abs (a, T, b));
wenzelm@61339
   357
  in Thm.renamed_prop prop' th' end;
wenzelm@61339
   358
wenzelm@61339
   359
wenzelm@26653
   360
(* forall_elim_var(s) *)
wenzelm@26653
   361
wenzelm@26653
   362
local
wenzelm@26653
   363
wenzelm@60951
   364
fun dest_all ct =
wenzelm@60951
   365
  (case Thm.term_of ct of
wenzelm@60951
   366
    Const ("Pure.all", _) $ Abs (a, _, _) =>
wenzelm@60951
   367
      let val (x, ct') = Thm.dest_abs NONE (Thm.dest_arg ct)
wenzelm@60951
   368
      in SOME ((a, Thm.ctyp_of_cterm x), ct') end
wenzelm@60951
   369
  | _ => NONE);
wenzelm@60951
   370
wenzelm@60951
   371
fun dest_all_list ct =
wenzelm@60951
   372
  (case dest_all ct of
wenzelm@60951
   373
    NONE => []
wenzelm@60951
   374
  | SOME (v, ct') => v :: dest_all_list ct');
wenzelm@60951
   375
wenzelm@60951
   376
fun forall_elim_vars_list vars i th =
wenzelm@26653
   377
  let
wenzelm@60950
   378
    val used =
wenzelm@60950
   379
      (Thm.fold_terms o Term.fold_aterms)
wenzelm@60950
   380
        (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I) th [];
wenzelm@60951
   381
    val vars' = (Name.variant_list used (map #1 vars), vars)
wenzelm@60951
   382
      |> ListPair.map (fn (x, (_, T)) => Thm.var ((x, i), T));
wenzelm@60951
   383
  in fold Thm.forall_elim vars' th end;
wenzelm@26653
   384
wenzelm@26653
   385
in
wenzelm@26653
   386
wenzelm@60950
   387
fun forall_elim_vars i th =
wenzelm@60951
   388
  forall_elim_vars_list (dest_all_list (Thm.cprop_of th)) i th;
wenzelm@26653
   389
wenzelm@33697
   390
fun forall_elim_var i th =
wenzelm@60950
   391
  let
wenzelm@60950
   392
    val vars =
wenzelm@60951
   393
      (case dest_all (Thm.cprop_of th) of
wenzelm@60951
   394
        SOME (v, _) => [v]
wenzelm@60951
   395
      | NONE => raise THM ("forall_elim_var", i, [th]));
wenzelm@60951
   396
  in forall_elim_vars_list vars i th end;
wenzelm@26653
   397
wenzelm@26653
   398
end;
wenzelm@26653
   399
wenzelm@26653
   400
wenzelm@60801
   401
(* instantiate by left-to-right occurrence of variables *)
wenzelm@60801
   402
wenzelm@60801
   403
fun instantiate' cTs cts thm =
wenzelm@60801
   404
  let
wenzelm@60801
   405
    fun err msg =
wenzelm@60801
   406
      raise TYPE ("instantiate': " ^ msg,
wenzelm@60801
   407
        map_filter (Option.map Thm.typ_of) cTs,
wenzelm@60801
   408
        map_filter (Option.map Thm.term_of) cts);
wenzelm@60801
   409
wenzelm@60801
   410
    fun zip_vars xs ys =
wenzelm@60801
   411
      zip_options xs ys handle ListPair.UnequalLengths =>
wenzelm@60801
   412
        err "more instantiations than variables in thm";
wenzelm@60801
   413
wenzelm@60801
   414
    val thm' =
wenzelm@60801
   415
      Thm.instantiate ((zip_vars (rev (Thm.fold_terms Term.add_tvars thm [])) cTs), []) thm;
wenzelm@60801
   416
    val thm'' =
wenzelm@60801
   417
      Thm.instantiate ([], zip_vars (rev (Thm.fold_terms Term.add_vars thm' [])) cts) thm';
wenzelm@60801
   418
  in thm'' end;
wenzelm@60801
   419
wenzelm@60801
   420
wenzelm@35985
   421
(* forall_intr_frees: generalization over all suitable Free variables *)
wenzelm@35985
   422
wenzelm@35985
   423
fun forall_intr_frees th =
wenzelm@35985
   424
  let
wenzelm@61041
   425
    val fixed =
wenzelm@61041
   426
      fold Term.add_frees (Thm.terms_of_tpairs (Thm.tpairs_of th) @ Thm.hyps_of th) [];
wenzelm@60821
   427
    val frees =
wenzelm@60821
   428
      Thm.fold_atomic_cterms (fn a =>
wenzelm@60821
   429
        (case Thm.term_of a of
wenzelm@60821
   430
          Free v => not (member (op =) fixed v) ? insert (op aconvc) a
wenzelm@60821
   431
        | _ => I)) th [];
wenzelm@60821
   432
  in fold Thm.forall_intr frees th end;
wenzelm@35985
   433
wenzelm@35985
   434
wenzelm@35845
   435
(* unvarify_global: global schematic variables *)
wenzelm@26653
   436
wenzelm@60825
   437
fun unvarify_global thy th =
wenzelm@24980
   438
  let
wenzelm@24980
   439
    val prop = Thm.full_prop_of th;
wenzelm@35845
   440
    val _ = map Logic.unvarify_global (prop :: Thm.hyps_of th)
wenzelm@24980
   441
      handle TERM (msg, _) => raise THM (msg, 0, [th]);
wenzelm@24980
   442
wenzelm@32279
   443
    val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
wenzelm@24980
   444
    val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
wenzelm@32279
   445
      let val T' = Term_Subst.instantiateT instT T
wenzelm@60805
   446
      in (((a, i), T'), Thm.global_cterm_of thy (Free ((a, T')))) end);
wenzelm@60805
   447
  in Thm.instantiate (map (apsnd (Thm.global_ctyp_of thy)) instT, inst) th end;
wenzelm@24980
   448
wenzelm@60825
   449
fun unvarify_axiom thy = unvarify_global thy o Thm.axiom thy;
wenzelm@60825
   450
wenzelm@26653
   451
wenzelm@26653
   452
(* close_derivation *)
wenzelm@26653
   453
wenzelm@26628
   454
fun close_derivation thm =
wenzelm@64568
   455
  if Thm.derivation_closed thm then thm else Thm.name_derivation "" thm;
wenzelm@26628
   456
wenzelm@24980
   457
wenzelm@59969
   458
(* user renaming of parameters in a subgoal *)
wenzelm@59969
   459
wenzelm@59969
   460
(*The names, if distinct, are used for the innermost parameters of subgoal i;
wenzelm@59969
   461
  preceding parameters may be renamed to make all parameters distinct.*)
wenzelm@59969
   462
fun rename_params_rule (names, i) st =
wenzelm@59969
   463
  let
wenzelm@59969
   464
    val (_, Bs, Bi, C) = Thm.dest_state (st, i);
wenzelm@59969
   465
    val params = map #1 (Logic.strip_params Bi);
wenzelm@59969
   466
    val short = length params - length names;
wenzelm@59969
   467
    val names' =
wenzelm@59969
   468
      if short < 0 then error "More names than parameters in subgoal!"
wenzelm@59969
   469
      else Name.variant_list names (take short params) @ names;
wenzelm@59969
   470
    val free_names = Term.fold_aterms (fn Free (x, _) => insert (op =) x | _ => I) Bi [];
wenzelm@59969
   471
    val Bi' = Logic.list_rename_params names' Bi;
wenzelm@59969
   472
  in
wenzelm@59969
   473
    (case duplicates (op =) names of
wenzelm@59969
   474
      a :: _ => (warning ("Can't rename.  Bound variables not distinct: " ^ a); st)
wenzelm@59969
   475
    | [] =>
wenzelm@59969
   476
      (case inter (op =) names free_names of
wenzelm@59969
   477
        a :: _ => (warning ("Can't rename.  Bound/Free variable clash: " ^ a); st)
wenzelm@59969
   478
      | [] => Thm.renamed_prop (Logic.list_implies (Bs @ [Bi'], C)) st))
wenzelm@59969
   479
  end;
wenzelm@59969
   480
wenzelm@59969
   481
wenzelm@59969
   482
(* preservation of bound variable names *)
wenzelm@59969
   483
wenzelm@59969
   484
fun rename_boundvars pat obj th =
wenzelm@59969
   485
  (case Term.rename_abs pat obj (Thm.prop_of th) of
wenzelm@59969
   486
    NONE => th
wenzelm@59969
   487
  | SOME prop' => Thm.renamed_prop prop' th);
wenzelm@59969
   488
wenzelm@59969
   489
wenzelm@24980
   490
wenzelm@24980
   491
(** specification primitives **)
wenzelm@24980
   492
wenzelm@30342
   493
(* rules *)
wenzelm@30342
   494
wenzelm@35855
   495
fun stripped_sorts thy t =
wenzelm@35855
   496
  let
wenzelm@60642
   497
    val tfrees = rev (Term.add_tfrees t []);
wenzelm@60642
   498
    val tfrees' = map (fn a => (a, [])) (Name.invent Name.context Name.aT (length tfrees));
wenzelm@60642
   499
    val recover =
wenzelm@60642
   500
      map2 (fn (a', S') => fn (a, S) => (((a', 0), S'), Thm.global_ctyp_of thy (TVar ((a, 0), S))))
wenzelm@60642
   501
        tfrees' tfrees;
wenzelm@60642
   502
    val strip = map (apply2 TFree) (tfrees ~~ tfrees');
wenzelm@35855
   503
    val t' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip))) t;
wenzelm@35855
   504
  in (strip, recover, t') end;
wenzelm@35855
   505
wenzelm@42375
   506
fun add_axiom ctxt (b, prop) thy =
wenzelm@24980
   507
  let
wenzelm@42375
   508
    val _ = Sign.no_vars ctxt prop;
wenzelm@35855
   509
    val (strip, recover, prop') = stripped_sorts thy prop;
wenzelm@35855
   510
    val constraints = map (fn (TFree (_, S), T) => (T, S)) strip;
wenzelm@60367
   511
    val of_sorts = maps (fn (T as TFree (_, S), _) => of_sort (Thm.ctyp_of ctxt T, S)) strip;
wenzelm@36106
   512
wenzelm@42375
   513
    val thy' = thy
wenzelm@51316
   514
      |> Theory.add_axiom ctxt (b, Logic.list_implies (maps Logic.mk_of_sort constraints, prop'));
wenzelm@51316
   515
    val axm_name = Sign.full_name thy' b;
wenzelm@36106
   516
    val axm' = Thm.axiom thy' axm_name;
wenzelm@35988
   517
    val thm =
wenzelm@35988
   518
      Thm.instantiate (recover, []) axm'
wenzelm@60825
   519
      |> unvarify_global thy'
wenzelm@35988
   520
      |> fold elim_implies of_sorts;
wenzelm@36106
   521
  in ((axm_name, thm), thy') end;
wenzelm@24980
   522
wenzelm@42375
   523
fun add_axiom_global arg thy = add_axiom (Syntax.init_pretty_global thy) arg thy;
wenzelm@42375
   524
wenzelm@61261
   525
fun add_def (context as (ctxt, _)) unchecked overloaded (b, prop) thy =
wenzelm@24980
   526
  let
wenzelm@42375
   527
    val _ = Sign.no_vars ctxt prop;
wenzelm@60367
   528
    val prems = map (Thm.cterm_of ctxt) (Logic.strip_imp_prems prop);
wenzelm@35988
   529
    val (_, recover, concl') = stripped_sorts thy (Logic.strip_imp_concl prop);
wenzelm@36106
   530
wenzelm@61261
   531
    val thy' = Theory.add_def context unchecked overloaded (b, concl') thy;
wenzelm@36106
   532
    val axm_name = Sign.full_name thy' b;
wenzelm@36106
   533
    val axm' = Thm.axiom thy' axm_name;
wenzelm@35988
   534
    val thm =
wenzelm@35988
   535
      Thm.instantiate (recover, []) axm'
wenzelm@60825
   536
      |> unvarify_global thy'
wenzelm@35988
   537
      |> fold_rev Thm.implies_intr prems;
wenzelm@36106
   538
  in ((axm_name, thm), thy') end;
wenzelm@24980
   539
wenzelm@42375
   540
fun add_def_global unchecked overloaded arg thy =
wenzelm@61262
   541
  add_def (Defs.global_context thy) unchecked overloaded arg thy;
wenzelm@42375
   542
wenzelm@27866
   543
wenzelm@27866
   544
wenzelm@27866
   545
(*** theorem tags ***)
wenzelm@27866
   546
wenzelm@27866
   547
(* add / delete tags *)
wenzelm@27866
   548
wenzelm@27866
   549
fun tag_rule tg = Thm.map_tags (insert (op =) tg);
wenzelm@27866
   550
fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
wenzelm@27866
   551
wenzelm@27866
   552
wenzelm@61852
   553
(* free dummy thm -- for abstract closure *)
wenzelm@61852
   554
wenzelm@61852
   555
val free_dummyN = "free_dummy";
wenzelm@61852
   556
fun is_free_dummy thm = Properties.defined (Thm.get_tags thm) free_dummyN;
wenzelm@61852
   557
val tag_free_dummy = tag_rule (free_dummyN, "");
wenzelm@61852
   558
wenzelm@61852
   559
wenzelm@30342
   560
(* def_name *)
wenzelm@30342
   561
wenzelm@30342
   562
fun def_name c = c ^ "_def";
wenzelm@30342
   563
wenzelm@30342
   564
fun def_name_optional c "" = def_name c
wenzelm@30342
   565
  | def_name_optional _ name = name;
wenzelm@30342
   566
wenzelm@63041
   567
val def_binding = Binding.map_name def_name #> Binding.reset_pos;
wenzelm@63041
   568
fun def_binding_optional b name = if Binding.is_empty name then def_binding b else name;
wenzelm@63041
   569
fun make_def_binding cond b = if cond then def_binding b else Binding.empty;
wenzelm@62093
   570
wenzelm@30342
   571
wenzelm@27866
   572
(* unofficial theorem names *)
wenzelm@27866
   573
wenzelm@27866
   574
fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
wenzelm@27866
   575
wenzelm@27866
   576
val has_name_hint = can the_name_hint;
wenzelm@27866
   577
val get_name_hint = the_default "??.unknown" o try the_name_hint;
wenzelm@27866
   578
wenzelm@27866
   579
fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
wenzelm@27866
   580
wenzelm@27866
   581
wenzelm@27866
   582
(* theorem kinds *)
wenzelm@27866
   583
wenzelm@27866
   584
val theoremK = "theorem";
wenzelm@27866
   585
wenzelm@42473
   586
fun legacy_get_kind thm = the_default "" (Properties.get (Thm.get_tags thm) Markup.kindN);
wenzelm@27866
   587
wenzelm@27866
   588
fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
wenzelm@61853
   589
wenzelm@61853
   590
wenzelm@61853
   591
wenzelm@61853
   592
(** attributes **)
wenzelm@61853
   593
wenzelm@61853
   594
(*attributes subsume any kind of rules or context modifiers*)
wenzelm@61853
   595
type attribute = Context.generic * thm -> Context.generic option * thm option;
wenzelm@61853
   596
wenzelm@61853
   597
type binding = binding * attribute list;
wenzelm@61853
   598
wenzelm@61853
   599
fun rule_attribute ths f (x, th) =
wenzelm@61853
   600
  (NONE,
wenzelm@61853
   601
    (case find_first is_free_dummy (th :: ths) of
wenzelm@61853
   602
      SOME th' => SOME th'
wenzelm@61853
   603
    | NONE => SOME (f x th)));
wenzelm@61853
   604
wenzelm@61853
   605
fun declaration_attribute f (x, th) =
wenzelm@61853
   606
  (if is_free_dummy th then NONE else SOME (f th x), NONE);
wenzelm@61853
   607
wenzelm@61853
   608
fun mixed_attribute f (x, th) =
wenzelm@61853
   609
  let val (x', th') = f (x, th) in (SOME x', SOME th') end;
wenzelm@61853
   610
wenzelm@61853
   611
fun apply_attribute (att: attribute) th x =
wenzelm@61853
   612
  let val (x', th') = att (x, check_hyps x (Thm.transfer (Context.theory_of x) th))
wenzelm@61853
   613
  in (the_default th th', the_default x x') end;
wenzelm@61853
   614
wenzelm@61853
   615
fun attribute_declaration att th x = #2 (apply_attribute att th x);
wenzelm@61853
   616
wenzelm@61853
   617
fun apply_attributes mk dest =
wenzelm@61853
   618
  let
wenzelm@61853
   619
    fun app [] th x = (th, x)
wenzelm@61853
   620
      | app (att :: atts) th x = apply_attribute att th (mk x) ||> dest |-> app atts;
wenzelm@61853
   621
  in app end;
wenzelm@61853
   622
wenzelm@61853
   623
val theory_attributes = apply_attributes Context.Theory Context.the_theory;
wenzelm@61853
   624
val proof_attributes = apply_attributes Context.Proof Context.the_proof;
wenzelm@61853
   625
wenzelm@61853
   626
fun no_attributes x = (x, []);
wenzelm@61853
   627
fun simple_fact x = [(x, [])];
wenzelm@61853
   628
wenzelm@61853
   629
fun tag tg = rule_attribute [] (K (tag_rule tg));
wenzelm@61853
   630
fun untag s = rule_attribute [] (K (untag_rule s));
wenzelm@61853
   631
fun kind k = rule_attribute [] (K (k <> "" ? kind_rule k));
wenzelm@27866
   632
wenzelm@27866
   633
wenzelm@49011
   634
(* forked proofs *)
wenzelm@49010
   635
wenzelm@49062
   636
structure Proofs = Theory_Data
wenzelm@49010
   637
(
wenzelm@49062
   638
  type T = thm list;
wenzelm@49062
   639
  val empty = [];
wenzelm@49010
   640
  fun extend _ = empty;
wenzelm@49010
   641
  fun merge _ = empty;
wenzelm@49010
   642
);
wenzelm@49010
   643
wenzelm@61059
   644
fun register_proofs more_thms =
wenzelm@61059
   645
  Proofs.map (fold (cons o Thm.trim_context) more_thms);
wenzelm@61059
   646
wenzelm@64574
   647
fun consolidate_theory thy =
wenzelm@66168
   648
  Thm.consolidate (map (Thm.transfer thy) (rev (Proofs.get thy)));
wenzelm@49010
   649
wenzelm@49010
   650
wenzelm@61268
   651
wenzelm@61268
   652
(** print theorems **)
wenzelm@61268
   653
wenzelm@61268
   654
(* options *)
wenzelm@61268
   655
wenzelm@64556
   656
val show_consts_raw = Config.declare_option ("show_consts", \<^here>);
wenzelm@61268
   657
val show_consts = Config.bool show_consts_raw;
wenzelm@61268
   658
wenzelm@64556
   659
val show_hyps_raw = Config.declare ("show_hyps", \<^here>) (fn _ => Config.Bool false);
wenzelm@61268
   660
val show_hyps = Config.bool show_hyps_raw;
wenzelm@61268
   661
wenzelm@64556
   662
val show_tags_raw = Config.declare ("show_tags", \<^here>) (fn _ => Config.Bool false);
wenzelm@61268
   663
val show_tags = Config.bool show_tags_raw;
wenzelm@61268
   664
wenzelm@61268
   665
wenzelm@61268
   666
(* pretty_thm etc. *)
wenzelm@61268
   667
wenzelm@61268
   668
fun pretty_tag (name, arg) = Pretty.strs [name, quote arg];
wenzelm@61268
   669
val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
wenzelm@61268
   670
wenzelm@61268
   671
fun pretty_flexpair ctxt (t, u) = Pretty.block
wenzelm@61268
   672
  [Syntax.pretty_term ctxt t, Pretty.str " =?=", Pretty.brk 1, Syntax.pretty_term ctxt u];
wenzelm@61268
   673
wenzelm@61268
   674
fun pretty_thm_raw ctxt {quote, show_hyps = show_hyps'} raw_th =
wenzelm@61268
   675
  let
wenzelm@61268
   676
    val show_tags = Config.get ctxt show_tags;
wenzelm@61268
   677
    val show_hyps = Config.get ctxt show_hyps;
wenzelm@61268
   678
wenzelm@61268
   679
    val th = raw_th
wenzelm@61268
   680
      |> perhaps (try (Thm.transfer (Proof_Context.theory_of ctxt)))
wenzelm@61268
   681
      |> perhaps (try Thm.strip_shyps);
wenzelm@61268
   682
wenzelm@61268
   683
    val hyps = if show_hyps then Thm.hyps_of th else undeclared_hyps (Context.Proof ctxt) th;
wenzelm@61509
   684
    val extra_shyps = extra_shyps' ctxt th;
wenzelm@61268
   685
    val tags = Thm.get_tags th;
wenzelm@61268
   686
    val tpairs = Thm.tpairs_of th;
wenzelm@61268
   687
wenzelm@61268
   688
    val q = if quote then Pretty.quote else I;
wenzelm@61268
   689
    val prt_term = q o Syntax.pretty_term ctxt;
wenzelm@61268
   690
wenzelm@61268
   691
wenzelm@61268
   692
    val hlen = length extra_shyps + length hyps + length tpairs;
wenzelm@61268
   693
    val hsymbs =
wenzelm@61268
   694
      if hlen = 0 then []
wenzelm@61268
   695
      else if show_hyps orelse show_hyps' then
wenzelm@61268
   696
        [Pretty.brk 2, Pretty.list "[" "]"
wenzelm@61268
   697
          (map (q o pretty_flexpair ctxt) tpairs @ map prt_term hyps @
wenzelm@61268
   698
           map (Syntax.pretty_sort ctxt) extra_shyps)]
wenzelm@61268
   699
      else [Pretty.brk 2, Pretty.str ("[" ^ replicate_string hlen "." ^ "]")];
wenzelm@61268
   700
    val tsymbs =
wenzelm@61268
   701
      if null tags orelse not show_tags then []
wenzelm@61268
   702
      else [Pretty.brk 1, pretty_tags tags];
wenzelm@61268
   703
  in Pretty.block (prt_term (Thm.prop_of th) :: (hsymbs @ tsymbs)) end;
wenzelm@61268
   704
wenzelm@61268
   705
fun pretty_thm ctxt = pretty_thm_raw ctxt {quote = false, show_hyps = true};
wenzelm@61268
   706
fun pretty_thm_item ctxt th = Pretty.item [pretty_thm ctxt th];
wenzelm@61268
   707
wenzelm@61268
   708
fun pretty_thm_global thy =
wenzelm@61268
   709
  pretty_thm_raw (Syntax.init_pretty_global thy) {quote = false, show_hyps = false};
wenzelm@61268
   710
wenzelm@61268
   711
val string_of_thm = Pretty.string_of oo pretty_thm;
wenzelm@61268
   712
val string_of_thm_global = Pretty.string_of oo pretty_thm_global;
wenzelm@61268
   713
wenzelm@61268
   714
wenzelm@22362
   715
open Thm;
wenzelm@22362
   716
wenzelm@22362
   717
end;
wenzelm@22362
   718
wenzelm@32842
   719
structure Basic_Thm: BASIC_THM = Thm;
wenzelm@32842
   720
open Basic_Thm;