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