src/Pure/Isar/proof_context.ML
author wenzelm
Sun Feb 12 21:34:27 2006 +0100 (2006-02-12 ago)
changeset 19033 24e251657e56
parent 19019 412009243085
child 19062 0fd52e819c24
permissions -rw-r--r--
consts: maintain thy version for efficient transfer;
ins_sorts: Vartab.replace is slower than Vartab.update, but might avoid some copying of table structure;
wenzelm@5819
     1
(*  Title:      Pure/Isar/proof_context.ML
wenzelm@5819
     2
    ID:         $Id$
wenzelm@5819
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5819
     4
wenzelm@19001
     5
The key concept of Isar proof contexts: elevates primitive local
wenzelm@19001
     6
reasoning Gamma |- phi to a structured concept, with generic context
wenzelm@19001
     7
elements, polymorphic abbreviations, and extra-logical data.
wenzelm@5819
     8
*)
wenzelm@5819
     9
wenzelm@5819
    10
signature PROOF_CONTEXT =
wenzelm@5819
    11
sig
wenzelm@17756
    12
  type context (*= Context.proof*)
wenzelm@18672
    13
  type export
wenzelm@5819
    14
  val theory_of: context -> theory
wenzelm@18672
    15
  val init: theory -> context
wenzelm@18672
    16
  val set_body: bool -> context -> context
wenzelm@18743
    17
  val restore_body: context -> context -> context
wenzelm@17451
    18
  val assms_of: context -> term list
wenzelm@7557
    19
  val prems_of: context -> thm list
wenzelm@16031
    20
  val fact_index_of: context -> FactIndex.T
wenzelm@18672
    21
  val is_fixed: context -> string -> bool
wenzelm@18672
    22
  val is_known: context -> string -> bool
wenzelm@17072
    23
  val transfer: theory -> context -> context
wenzelm@19019
    24
  val map_theory: (theory -> theory) -> context -> context
wenzelm@14828
    25
  val pretty_term: context -> term -> Pretty.T
wenzelm@14828
    26
  val pretty_typ: context -> typ -> Pretty.T
wenzelm@14828
    27
  val pretty_sort: context -> sort -> Pretty.T
wenzelm@14828
    28
  val pp: context -> Pretty.pp
wenzelm@14828
    29
  val pretty_thm: context -> thm -> Pretty.T
wenzelm@14828
    30
  val pretty_thms: context -> thm list -> Pretty.T
wenzelm@14828
    31
  val pretty_fact: context -> string * thm list -> Pretty.T
wenzelm@17072
    32
  val pretty_proof: context -> Proofterm.proof -> Pretty.T
wenzelm@17072
    33
  val pretty_proof_of: context -> bool -> thm -> Pretty.T
wenzelm@17360
    34
  val string_of_typ: context -> typ -> string
wenzelm@14828
    35
  val string_of_term: context -> term -> string
wenzelm@17860
    36
  val string_of_thm: context -> thm -> string
wenzelm@18672
    37
  val used_types: context -> string list
wenzelm@12414
    38
  val default_type: context -> string -> typ option
wenzelm@5819
    39
  val read_typ: context -> string -> typ
wenzelm@16348
    40
  val read_typ_syntax: context -> string -> typ
wenzelm@16348
    41
  val read_typ_abbrev: context -> string -> typ
wenzelm@5819
    42
  val cert_typ: context -> typ -> typ
wenzelm@16348
    43
  val cert_typ_syntax: context -> typ -> typ
wenzelm@16348
    44
  val cert_typ_abbrev: context -> typ -> typ
wenzelm@10583
    45
  val get_skolem: context -> string -> string
wenzelm@18255
    46
  val revert_skolem: context -> string -> string
wenzelm@9133
    47
  val extern_skolem: context -> term -> term
wenzelm@14720
    48
  val read_termTs: context -> (string -> bool) -> (indexname -> typ option)
wenzelm@14720
    49
    -> (indexname -> sort option) -> string list -> (string * typ) list
wenzelm@14720
    50
    -> term list * (indexname * typ) list
wenzelm@14720
    51
  val read_termTs_schematic: context -> (string -> bool) -> (indexname -> typ option)
wenzelm@14720
    52
    -> (indexname -> sort option) -> string list -> (string * typ) list
wenzelm@14720
    53
    -> term list * (indexname * typ) list
wenzelm@18672
    54
  val read_term_legacy: context -> string -> term
wenzelm@5819
    55
  val read_term: context -> string -> term
wenzelm@5819
    56
  val read_prop: context -> string -> term
wenzelm@11925
    57
  val read_prop_schematic: context -> string -> term
wenzelm@10465
    58
  val read_terms: context -> string list -> term list
wenzelm@8096
    59
  val read_term_pats: typ -> context -> string list -> term list
wenzelm@8096
    60
  val read_prop_pats: context -> string list -> term list
wenzelm@5819
    61
  val cert_term: context -> term -> term
wenzelm@5819
    62
  val cert_prop: context -> term -> term
wenzelm@8096
    63
  val cert_term_pats: typ -> context -> term list -> term list
wenzelm@8096
    64
  val cert_prop_pats: context -> term list -> term list
wenzelm@5819
    65
  val declare_term: term -> context -> context
wenzelm@18770
    66
  val infer_type: context -> string -> typ
wenzelm@18770
    67
  val inferred_param: string -> context -> (string * typ) * context
wenzelm@18770
    68
  val inferred_fixes: context -> (string * typ) list * context
wenzelm@15703
    69
  val read_tyname: context -> string -> typ
wenzelm@15703
    70
  val read_const: context -> string -> term
wenzelm@19019
    71
  val rename_frees: context -> term list -> (string * 'a) list -> (string * 'a) list
wenzelm@7925
    72
  val warn_extra_tfrees: context -> context -> context
wenzelm@12550
    73
  val generalize: context -> context -> term list -> term list
wenzelm@19001
    74
  val polymorphic: context -> term list -> term list
wenzelm@18785
    75
  val export_standard: context -> context -> thm -> thm
wenzelm@18042
    76
  val exports: context -> context -> thm -> thm Seq.seq
wenzelm@18042
    77
  val goal_exports: context -> context -> thm -> thm Seq.seq
wenzelm@10810
    78
  val drop_schematic: indexname * term option -> indexname * term option
wenzelm@10810
    79
  val add_binds: (indexname * string option) list -> context -> context
wenzelm@10810
    80
  val add_binds_i: (indexname * term option) list -> context -> context
wenzelm@12147
    81
  val auto_bind_goal: term list -> context -> context
wenzelm@12147
    82
  val auto_bind_facts: term list -> context -> context
wenzelm@18310
    83
  val match_bind: bool -> (string list * string) list -> context -> term list * context
wenzelm@18310
    84
  val match_bind_i: bool -> (term list * term) list -> context -> term list * context
wenzelm@10465
    85
  val read_propp: context * (string * (string list * string list)) list list
wenzelm@10465
    86
    -> context * (term * (term list * term list)) list list
wenzelm@10465
    87
  val cert_propp: context * (term * (term list * term list)) list list
wenzelm@10465
    88
    -> context * (term * (term list * term list)) list list
wenzelm@10554
    89
  val read_propp_schematic: context * (string * (string list * string list)) list list
wenzelm@10554
    90
    -> context * (term * (term list * term list)) list list
wenzelm@10554
    91
  val cert_propp_schematic: context * (term * (term list * term list)) list list
wenzelm@10554
    92
    -> context * (term * (term list * term list)) list list
wenzelm@10465
    93
  val bind_propp: context * (string * (string list * string list)) list list
wenzelm@10465
    94
    -> context * (term list list * (context -> context))
wenzelm@10465
    95
  val bind_propp_i: context * (term * (term list * term list)) list list
wenzelm@10465
    96
    -> context * (term list list * (context -> context))
wenzelm@10554
    97
  val bind_propp_schematic: context * (string * (string list * string list)) list list
wenzelm@10554
    98
    -> context * (term list list * (context -> context))
wenzelm@10554
    99
  val bind_propp_schematic_i: context * (term * (term list * term list)) list list
wenzelm@10554
   100
    -> context * (term list list * (context -> context))
wenzelm@18042
   101
  val fact_tac: thm list -> int -> tactic
wenzelm@18042
   102
  val some_fact_tac: context -> int -> tactic
berghofe@15456
   103
  val get_thm: context -> thmref -> thm
berghofe@15456
   104
  val get_thm_closure: context -> thmref -> thm
berghofe@15456
   105
  val get_thms: context -> thmref -> thm list
berghofe@15456
   106
  val get_thms_closure: context -> thmref -> thm list
wenzelm@16031
   107
  val valid_thms: context -> string * thm list -> bool
wenzelm@16031
   108
  val lthms_containing: context -> FactIndex.spec -> (string * thm list) list
wenzelm@16348
   109
  val extern_thm: context -> string -> xstring
wenzelm@19019
   110
  val no_base_names: context -> context
wenzelm@16147
   111
  val qualified_names: context -> context
wenzelm@19019
   112
  val qualified_force_prefix: string -> context -> context
wenzelm@16147
   113
  val restore_naming: context -> context -> context
wenzelm@13425
   114
  val hide_thms: bool -> string list -> context -> context
wenzelm@17360
   115
  val put_thms: string * thm list option -> context -> context
wenzelm@14564
   116
  val note_thmss:
wenzelm@18728
   117
    ((bstring * attribute list) * (thmref * attribute list) list) list ->
wenzelm@17860
   118
      context -> (bstring * thm list) list * context
wenzelm@14564
   119
  val note_thmss_i:
wenzelm@18728
   120
    ((bstring * attribute list) * (thm list * attribute list) list) list ->
wenzelm@17860
   121
      context -> (bstring * thm list) list * context
wenzelm@18672
   122
  val read_vars: (string * string option * mixfix) list -> context ->
wenzelm@18672
   123
    (string * typ option * mixfix) list * context
wenzelm@18672
   124
  val cert_vars: (string * typ option * mixfix) list -> context ->
wenzelm@18672
   125
    (string * typ option * mixfix) list * context
wenzelm@18672
   126
  val read_vars_legacy: (string * string option * mixfix) list -> context ->
wenzelm@18672
   127
    (string * typ option * mixfix) list * context
wenzelm@18672
   128
  val cert_vars_legacy: (string * typ option * mixfix) list -> context ->
wenzelm@18672
   129
    (string * typ option * mixfix) list * context
wenzelm@18672
   130
  val add_fixes: (string * string option * mixfix) list -> context -> string list * context
wenzelm@18672
   131
  val add_fixes_i: (string * typ option * mixfix) list -> context -> string list * context
wenzelm@18672
   132
  val add_fixes_legacy: (string * typ option * mixfix) list -> context -> string list * context
wenzelm@18809
   133
  val invent_fixes: string list -> context -> string list * context
wenzelm@18672
   134
  val fix_frees: term -> context -> context
wenzelm@18672
   135
  val auto_fixes: context * (term list list * 'a) -> context * (term list list * 'a)
wenzelm@18672
   136
  val bind_fixes: string list -> context -> (term -> term) * context
wenzelm@18672
   137
  val add_assms: export ->
wenzelm@18728
   138
    ((string * attribute list) * (string * (string list * string list)) list) list ->
wenzelm@18672
   139
    context -> (bstring * thm list) list * context
wenzelm@18672
   140
  val add_assms_i: export ->
wenzelm@18728
   141
    ((string * attribute list) * (term * (term list * term list)) list) list ->
wenzelm@18672
   142
    context -> (bstring * thm list) list * context
wenzelm@18672
   143
  val assume_export: export
wenzelm@18672
   144
  val presume_export: export
wenzelm@17360
   145
  val add_view: context -> cterm list -> context -> context
wenzelm@18042
   146
  val export_view: cterm list -> context -> context -> thm -> thm
wenzelm@18609
   147
  val add_cases: bool -> (string * RuleCases.T option) list -> context -> context
wenzelm@18609
   148
  val apply_case: RuleCases.T -> context -> (string * term list) list * context
wenzelm@16147
   149
  val get_case: context -> string -> string option list -> RuleCases.T
wenzelm@19001
   150
  val add_abbrevs: bool -> (bstring * string * mixfix) list -> context -> context
wenzelm@19001
   151
  val add_abbrevs_i: bool -> (bstring * term * mixfix) list -> context -> context
wenzelm@10810
   152
  val verbose: bool ref
wenzelm@10810
   153
  val setmp_verbose: ('a -> 'b) -> 'a -> 'b
wenzelm@12072
   154
  val print_syntax: context -> unit
wenzelm@10810
   155
  val print_binds: context -> unit
wenzelm@12057
   156
  val print_lthms: context -> unit
wenzelm@10810
   157
  val print_cases: context -> unit
wenzelm@10810
   158
  val prems_limit: int ref
wenzelm@18672
   159
  val pretty_ctxt: context -> Pretty.T list
wenzelm@10810
   160
  val pretty_context: context -> Pretty.T list
wenzelm@18809
   161
  val debug: bool ref
wenzelm@18809
   162
  val pprint_context: context -> pprint_args -> unit
wenzelm@5819
   163
end;
wenzelm@5819
   164
wenzelm@16540
   165
structure ProofContext: PROOF_CONTEXT =
wenzelm@5819
   166
struct
wenzelm@5819
   167
wenzelm@16540
   168
type context = Context.proof;
wenzelm@5819
   169
wenzelm@16540
   170
val theory_of = Context.theory_of_proof;
wenzelm@19001
   171
val tsig_of = Sign.tsig_of o theory_of;
wenzelm@19001
   172
wenzelm@16540
   173
val init = Context.init_proof;
wenzelm@12057
   174
wenzelm@7270
   175
wenzelm@5819
   176
wenzelm@16540
   177
(** Isar proof context information **)
wenzelm@5819
   178
wenzelm@18672
   179
type export = bool -> cterm list -> thm -> thm Seq.seq;
wenzelm@5819
   180
wenzelm@16540
   181
datatype ctxt =
wenzelm@16540
   182
  Ctxt of
wenzelm@19001
   183
   {naming: NameSpace.naming,                     (*local naming conventions*)
wenzelm@19001
   184
    syntax: LocalSyntax.T,                        (*local syntax*)
wenzelm@19033
   185
    consts: Consts.T * Consts.T,                  (*global/local consts*)
wenzelm@19001
   186
    fixes: bool * (string * string) list,         (*fixes: !!x. _ with proof body flag*)
wenzelm@18672
   187
    assms:
wenzelm@19001
   188
      ((cterm list * export) list *               (*assumes and views: A ==> _*)
wenzelm@19001
   189
        (string * thm list) list),                (*prems: A |- A*)
wenzelm@19001
   190
    binds: (typ * term) Vartab.table,             (*term bindings*)
wenzelm@19001
   191
    thms: thm list NameSpace.table * FactIndex.T, (*local thms*)
wenzelm@19001
   192
    cases: (string * (RuleCases.T * bool)) list,  (*local contexts*)
wenzelm@18672
   193
    defaults:
wenzelm@19001
   194
      typ Vartab.table *                          (*type constraints*)
wenzelm@19001
   195
      sort Vartab.table *                         (*default sorts*)
wenzelm@19001
   196
      string list *                               (*used type variables*)
wenzelm@19001
   197
      term list Symtab.table};                    (*type variable occurrences*)
wenzelm@5819
   198
wenzelm@19001
   199
fun make_ctxt (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =
wenzelm@19001
   200
  Ctxt {naming = naming, syntax = syntax, consts = consts, fixes = fixes, assms = assms,
wenzelm@19001
   201
    binds = binds, thms = thms, cases = cases, defaults = defaults};
wenzelm@5819
   202
wenzelm@16540
   203
structure ContextData = ProofDataFun
wenzelm@18672
   204
(
wenzelm@16540
   205
  val name = "Isar/context";
wenzelm@16540
   206
  type T = ctxt;
wenzelm@16540
   207
  fun init thy =
wenzelm@19033
   208
    make_ctxt (NameSpace.default_naming, LocalSyntax.init thy,
wenzelm@19033
   209
      (Sign.consts_of thy, Sign.consts_of thy), (false, []), ([], []),
wenzelm@19033
   210
      Vartab.empty, (NameSpace.empty_table, FactIndex.empty), [],
wenzelm@16540
   211
      (Vartab.empty, Vartab.empty, [], Symtab.empty));
wenzelm@16540
   212
  fun print _ _ = ();
wenzelm@18672
   213
);
wenzelm@5819
   214
wenzelm@18708
   215
val _ = Context.add_setup ContextData.init;
wenzelm@5819
   216
wenzelm@16540
   217
fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args);
wenzelm@5819
   218
wenzelm@18672
   219
fun map_context f =
wenzelm@19001
   220
  ContextData.map (fn Ctxt {naming, syntax, consts, fixes, assms, binds, thms, cases, defaults} =>
wenzelm@19001
   221
    make_ctxt (f (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults)));
wenzelm@18672
   222
wenzelm@19001
   223
fun map_naming f =
wenzelm@19001
   224
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   225
    (f naming, syntax, consts, fixes, assms, binds, thms, cases, defaults));
wenzelm@18672
   226
wenzelm@19001
   227
fun map_syntax f =
wenzelm@19001
   228
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   229
    (naming, f syntax, consts, fixes, assms, binds, thms, cases, defaults));
wenzelm@18672
   230
wenzelm@19001
   231
fun map_consts f =
wenzelm@19001
   232
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   233
    (naming, syntax, f consts, fixes, assms, binds, thms, cases, defaults));
wenzelm@19001
   234
wenzelm@19001
   235
fun map_fixes f =
wenzelm@19001
   236
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   237
    (naming, syntax, consts, f fixes, assms, binds, thms, cases, defaults));
wenzelm@18971
   238
wenzelm@19001
   239
fun map_assms f =
wenzelm@19001
   240
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   241
    (naming, syntax, consts, fixes, f assms, binds, thms, cases, defaults));
wenzelm@18672
   242
wenzelm@19001
   243
fun map_binds f =
wenzelm@19001
   244
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   245
    (naming, syntax, consts, fixes, assms, f binds, thms, cases, defaults));
wenzelm@18672
   246
wenzelm@19001
   247
fun map_thms f =
wenzelm@19001
   248
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   249
    (naming, syntax, consts, fixes, assms, binds, f thms, cases, defaults));
wenzelm@18672
   250
wenzelm@19001
   251
fun map_cases f =
wenzelm@19001
   252
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   253
    (naming, syntax, consts, fixes, assms, binds, thms, f cases, defaults));
wenzelm@18672
   254
wenzelm@19001
   255
fun map_defaults f =
wenzelm@19001
   256
  map_context (fn (naming, syntax, consts, fixes, assms, binds, thms, cases, defaults) =>
wenzelm@19001
   257
    (naming, syntax, consts, fixes, assms, binds, thms, cases, f defaults));
wenzelm@19001
   258
wenzelm@19001
   259
val naming_of = #naming o rep_context;
wenzelm@5819
   260
wenzelm@16540
   261
val syntax_of = #syntax o rep_context;
wenzelm@19001
   262
val syn_of = LocalSyntax.syn_of o syntax_of;
wenzelm@19001
   263
wenzelm@19033
   264
val consts_of = #2 o #consts o rep_context;
wenzelm@5819
   265
wenzelm@18672
   266
val is_body = #1 o #fixes o rep_context;
wenzelm@18672
   267
fun set_body b = map_fixes (fn (_, fixes) => (b, fixes));
wenzelm@18743
   268
fun restore_body ctxt = set_body (is_body ctxt);
wenzelm@18672
   269
wenzelm@18672
   270
val fixes_of = #2 o #fixes o rep_context;
wenzelm@18672
   271
val fixed_names_of = map #2 o fixes_of;
wenzelm@18672
   272
wenzelm@18672
   273
val assumptions_of = #1 o #assms o rep_context;
wenzelm@17451
   274
val assms_of = map Thm.term_of o List.concat o map #1 o assumptions_of;
wenzelm@18672
   275
val prems_of = List.concat o map #2 o #2 o #assms o rep_context;
wenzelm@5819
   276
wenzelm@17860
   277
val binds_of = #binds o rep_context;
wenzelm@5819
   278
wenzelm@16540
   279
val thms_of = #thms o rep_context;
wenzelm@19001
   280
val fact_index_of = #2 o thms_of;
wenzelm@16540
   281
wenzelm@16540
   282
val cases_of = #cases o rep_context;
wenzelm@5819
   283
wenzelm@18672
   284
val defaults_of = #defaults o rep_context;
wenzelm@16540
   285
val type_occs_of = #4 o defaults_of;
wenzelm@5819
   286
wenzelm@19019
   287
fun is_fixed ctxt x = exists (fn (_, y) => x = y) (fixes_of ctxt);
wenzelm@16894
   288
fun is_known ctxt x = Vartab.defined (#1 (defaults_of ctxt)) (x, ~1) orelse is_fixed ctxt x;
wenzelm@5819
   289
wenzelm@5819
   290
wenzelm@19001
   291
(* transfer *)
wenzelm@12093
   292
wenzelm@19001
   293
fun transfer_syntax thy =
wenzelm@19001
   294
  map_syntax (LocalSyntax.rebuild thy) #>
wenzelm@19033
   295
  map_consts (fn consts as (global_consts, local_consts) =>
wenzelm@19033
   296
    let val thy_consts = Sign.consts_of thy in
wenzelm@19033
   297
      if Consts.eq_consts (thy_consts, global_consts) then consts
wenzelm@19033
   298
      else (thy_consts, Consts.merge (thy_consts, local_consts))
wenzelm@19033
   299
    end);
wenzelm@17072
   300
wenzelm@19001
   301
fun transfer thy = Context.transfer_proof thy #> transfer_syntax thy;
wenzelm@17072
   302
wenzelm@19019
   303
fun map_theory f ctxt = transfer (f (theory_of ctxt)) ctxt;
wenzelm@19019
   304
wenzelm@12093
   305
wenzelm@12093
   306
wenzelm@14828
   307
(** pretty printing **)
wenzelm@14828
   308
wenzelm@19001
   309
fun pretty_term' abbrevs ctxt t =
wenzelm@18971
   310
  let
wenzelm@19001
   311
    val thy = theory_of ctxt;
wenzelm@19001
   312
    val syntax = syntax_of ctxt;
wenzelm@18971
   313
    val consts = consts_of ctxt;
wenzelm@19001
   314
    val t' = t
wenzelm@19001
   315
      |> K abbrevs ? Pattern.rewrite_term thy (Consts.abbrevs_of consts) []
wenzelm@19001
   316
      |> LocalSyntax.extern_term (NameSpace.extern (Consts.space_of consts)) thy syntax;
wenzelm@19001
   317
  in Sign.pretty_term' (LocalSyntax.syn_of syntax) (Context.Proof ctxt) t' end;
wenzelm@18971
   318
wenzelm@19001
   319
val pretty_term = pretty_term' true;
wenzelm@16458
   320
fun pretty_typ ctxt T = Sign.pretty_typ (theory_of ctxt) T;
wenzelm@16458
   321
fun pretty_sort ctxt S = Sign.pretty_sort (theory_of ctxt) S;
wenzelm@16458
   322
fun pretty_classrel ctxt cs = Sign.pretty_classrel (theory_of ctxt) cs;
wenzelm@16458
   323
fun pretty_arity ctxt ar = Sign.pretty_arity (theory_of ctxt) ar;
wenzelm@14828
   324
wenzelm@14974
   325
fun pp ctxt = Pretty.pp (pretty_term ctxt, pretty_typ ctxt, pretty_sort ctxt,
wenzelm@14974
   326
  pretty_classrel ctxt, pretty_arity ctxt);
wenzelm@14828
   327
wenzelm@17451
   328
fun pretty_thm ctxt th =
wenzelm@17451
   329
  Display.pretty_thm_aux (pp ctxt) false true (assms_of ctxt) th;
wenzelm@14828
   330
wenzelm@14828
   331
fun pretty_thms ctxt [th] = pretty_thm ctxt th
wenzelm@14828
   332
  | pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths));
wenzelm@14828
   333
wenzelm@14828
   334
fun pretty_fact ctxt ("", ths) = pretty_thms ctxt ths
wenzelm@14828
   335
  | pretty_fact ctxt (a, [th]) =
wenzelm@14828
   336
      Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, pretty_thm ctxt th]
wenzelm@14828
   337
  | pretty_fact ctxt (a, ths) =
wenzelm@14828
   338
      Pretty.block (Pretty.fbreaks (Pretty.str (a ^ ":") :: map (pretty_thm ctxt) ths));
wenzelm@14828
   339
wenzelm@17072
   340
fun pretty_proof ctxt prf =
wenzelm@19001
   341
  pretty_term' true (ctxt |> transfer_syntax (ProofSyntax.proof_syntax prf (theory_of ctxt)))
wenzelm@17072
   342
    (ProofSyntax.term_of_proof prf);
wenzelm@17072
   343
wenzelm@17072
   344
fun pretty_proof_of ctxt full th =
wenzelm@17072
   345
  pretty_proof ctxt (ProofSyntax.proof_of full th);
wenzelm@17072
   346
wenzelm@17860
   347
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@17860
   348
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@17860
   349
val string_of_thm = Pretty.string_of oo pretty_thm;
wenzelm@17860
   350
wenzelm@14828
   351
wenzelm@14828
   352
wenzelm@7663
   353
(** default sorts and types **)
wenzelm@7663
   354
wenzelm@17412
   355
val def_sort = Vartab.lookup o #2 o defaults_of;
wenzelm@7663
   356
wenzelm@16540
   357
fun def_type ctxt pattern xi =
wenzelm@18672
   358
  let val {binds, defaults = (types, _, _, _), ...} = rep_context ctxt in
wenzelm@17412
   359
    (case Vartab.lookup types xi of
wenzelm@16540
   360
      NONE =>
wenzelm@16540
   361
        if pattern then NONE
wenzelm@18953
   362
        else Vartab.lookup binds xi |> Option.map (TypeInfer.polymorphicT o #1)
wenzelm@16540
   363
    | some => some)
wenzelm@16540
   364
  end;
wenzelm@7663
   365
wenzelm@18672
   366
val used_types = #3 o defaults_of;
wenzelm@18672
   367
wenzelm@17412
   368
fun default_type ctxt x = Vartab.lookup (#1 (defaults_of ctxt)) (x, ~1);
wenzelm@12414
   369
wenzelm@7663
   370
wenzelm@5819
   371
wenzelm@5819
   372
(** prepare types **)
wenzelm@5819
   373
wenzelm@9504
   374
local
wenzelm@9504
   375
wenzelm@9504
   376
fun read_typ_aux read ctxt s =
wenzelm@18857
   377
  read (syn_of ctxt) (Context.Proof ctxt) (def_sort ctxt) s;
wenzelm@5819
   378
wenzelm@10554
   379
fun cert_typ_aux cert ctxt raw_T =
wenzelm@18678
   380
  cert (theory_of ctxt) raw_T handle TYPE (msg, _, _) => error msg;
wenzelm@9504
   381
wenzelm@9504
   382
in
wenzelm@9504
   383
wenzelm@16348
   384
val read_typ        = read_typ_aux Sign.read_typ';
wenzelm@16348
   385
val read_typ_syntax = read_typ_aux Sign.read_typ_syntax';
wenzelm@16348
   386
val read_typ_abbrev = read_typ_aux Sign.read_typ_abbrev';
wenzelm@16348
   387
val cert_typ        = cert_typ_aux Sign.certify_typ;
wenzelm@16348
   388
val cert_typ_syntax = cert_typ_aux Sign.certify_typ_syntax;
wenzelm@16348
   389
val cert_typ_abbrev = cert_typ_aux Sign.certify_typ_abbrev;
wenzelm@9504
   390
wenzelm@9504
   391
end;
wenzelm@9504
   392
wenzelm@5819
   393
wenzelm@7679
   394
(* internalize Skolem constants *)
wenzelm@7679
   395
wenzelm@17184
   396
val lookup_skolem = AList.lookup (op =) o fixes_of;
wenzelm@18187
   397
fun get_skolem ctxt x = the_default x (lookup_skolem ctxt x);
wenzelm@7679
   398
wenzelm@18678
   399
fun no_skolem internal x =
wenzelm@9291
   400
  if can Syntax.dest_skolem x then
wenzelm@18678
   401
    error ("Illegal reference to internal Skolem constant: " ^ quote x)
wenzelm@12504
   402
  else if not internal andalso can Syntax.dest_internal x then
wenzelm@18678
   403
    error ("Illegal reference to internal variable: " ^ quote x)
wenzelm@7679
   404
  else x;
wenzelm@7679
   405
wenzelm@14720
   406
fun intern_skolem ctxt internal =
wenzelm@7679
   407
  let
wenzelm@7679
   408
    fun intern (t as Free (x, T)) =
wenzelm@14720
   409
          if internal x then t
wenzelm@14720
   410
          else
wenzelm@18678
   411
            (case lookup_skolem ctxt (no_skolem false x) of
skalberg@15531
   412
              SOME x' => Free (x', T)
skalberg@15531
   413
            | NONE => t)
wenzelm@7679
   414
      | intern (t $ u) = intern t $ intern u
wenzelm@7679
   415
      | intern (Abs (x, T, t)) = Abs (x, T, intern t)
wenzelm@7679
   416
      | intern a = a;
wenzelm@7679
   417
  in intern end;
wenzelm@7679
   418
wenzelm@7679
   419
wenzelm@18187
   420
(* externalize Skolem constants -- approximation only! *)
wenzelm@18187
   421
wenzelm@18255
   422
fun rev_skolem ctxt =
wenzelm@18187
   423
  let val rev_fixes = map Library.swap (fixes_of ctxt)
wenzelm@18187
   424
  in AList.lookup (op =) rev_fixes end;
wenzelm@9133
   425
wenzelm@18255
   426
fun revert_skolem ctxt x =
wenzelm@18255
   427
  (case rev_skolem ctxt x of
wenzelm@18255
   428
    SOME x' => x'
wenzelm@18375
   429
  | NONE => perhaps (try Syntax.dest_skolem) x);
wenzelm@18255
   430
wenzelm@9133
   431
fun extern_skolem ctxt =
wenzelm@9133
   432
  let
wenzelm@18255
   433
    val revert = rev_skolem ctxt;
wenzelm@9133
   434
    fun extern (t as Free (x, T)) =
wenzelm@18187
   435
        (case revert x of
wenzelm@18187
   436
          SOME x' => Free (if lookup_skolem ctxt x' = SOME x then x' else NameSpace.hidden x', T)
wenzelm@18187
   437
        | NONE => t)
wenzelm@9133
   438
      | extern (t $ u) = extern t $ extern u
wenzelm@9133
   439
      | extern (Abs (x, T, t)) = Abs (x, T, extern t)
wenzelm@9133
   440
      | extern a = a;
wenzelm@9133
   441
  in extern end
wenzelm@9133
   442
wenzelm@8096
   443
wenzelm@18187
   444
wenzelm@5819
   445
(** prepare terms and propositions **)
wenzelm@5819
   446
wenzelm@5819
   447
(*
wenzelm@16501
   448
  (1) read / certify wrt. theory of context
wenzelm@5819
   449
  (2) intern Skolem constants
wenzelm@5819
   450
  (3) expand term bindings
wenzelm@5819
   451
*)
wenzelm@5819
   452
wenzelm@5819
   453
wenzelm@16501
   454
(* read wrt. theory *)     (*exception ERROR*)
wenzelm@5819
   455
wenzelm@18857
   456
fun read_def_termTs freeze pp syn ctxt (types, sorts, used) sTs =
wenzelm@18971
   457
  Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn (consts_of ctxt)
wenzelm@18857
   458
    (Context.Proof ctxt) (types, sorts) used freeze sTs;
wenzelm@5874
   459
wenzelm@18857
   460
fun read_def_termT freeze pp syn ctxt defaults sT =
wenzelm@18857
   461
  apfst hd (read_def_termTs freeze pp syn ctxt defaults [sT]);
wenzelm@14828
   462
wenzelm@18672
   463
fun read_term_thy freeze pp syn thy defaults s =
wenzelm@18672
   464
  #1 (read_def_termT freeze pp syn thy defaults (s, TypeInfer.logicT));
wenzelm@5874
   465
wenzelm@18672
   466
fun read_prop_thy freeze pp syn thy defaults s =
wenzelm@18672
   467
  #1 (read_def_termT freeze pp syn thy defaults (s, propT));
wenzelm@12072
   468
wenzelm@18672
   469
fun read_terms_thy freeze pp syn thy defaults =
wenzelm@18672
   470
  #1 o read_def_termTs freeze pp syn thy defaults o map (rpair TypeInfer.logicT);
wenzelm@12072
   471
wenzelm@18672
   472
fun read_props_thy freeze pp syn thy defaults =
wenzelm@18672
   473
  #1 o read_def_termTs freeze pp syn thy defaults o map (rpair propT);
wenzelm@5819
   474
wenzelm@5819
   475
wenzelm@19001
   476
(* local abbreviations *)
wenzelm@5819
   477
wenzelm@19001
   478
fun expand_consts ctxt =
wenzelm@19001
   479
  Consts.certify (pp ctxt) (tsig_of ctxt) (consts_of ctxt);
wenzelm@19001
   480
wenzelm@19001
   481
fun expand_binds ctxt schematic =
wenzelm@5819
   482
  let
wenzelm@18971
   483
    val binds = binds_of ctxt;
wenzelm@5819
   484
wenzelm@19001
   485
    fun expand_var (xi, T) =
wenzelm@18971
   486
      (case Vartab.lookup binds xi of
wenzelm@19001
   487
        SOME t => Envir.expand_atom (tsig_of ctxt) T t
wenzelm@18971
   488
      | NONE =>
wenzelm@18971
   489
          if schematic then Var (xi, T)
wenzelm@18971
   490
          else error ("Unbound schematic variable: " ^ Syntax.string_of_vname xi));
wenzelm@19001
   491
  in Envir.beta_norm o Term.map_aterms (fn Var v => expand_var v | a => a) end;
wenzelm@5819
   492
wenzelm@5819
   493
wenzelm@6550
   494
(* dummy patterns *)
wenzelm@6550
   495
wenzelm@18310
   496
val prepare_dummies =
wenzelm@18310
   497
  let val next = ref 1 in
wenzelm@18310
   498
    fn t =>
wenzelm@18310
   499
      let val (i, u) = Term.replace_dummy_patterns (! next, t)
wenzelm@18310
   500
      in next := i; u end
wenzelm@18310
   501
  end;
wenzelm@6762
   502
wenzelm@18678
   503
fun reject_dummies t = Term.no_dummy_patterns t
wenzelm@18678
   504
  handle TERM _ => error "Illegal dummy pattern(s) in term";
wenzelm@6550
   505
wenzelm@6550
   506
wenzelm@5819
   507
(* read terms *)
wenzelm@5819
   508
wenzelm@10554
   509
local
wenzelm@10554
   510
skalberg@15531
   511
fun append_env e1 e2 x = (case e2 x of NONE => e1 x | some => some);
wenzelm@14720
   512
wenzelm@16031
   513
fun gen_read' read app pattern schematic
wenzelm@14720
   514
    ctxt internal more_types more_sorts more_used s =
ballarin@14174
   515
  let
wenzelm@14720
   516
    val types = append_env (def_type ctxt pattern) more_types;
wenzelm@14720
   517
    val sorts = append_env (def_sort ctxt) more_sorts;
wenzelm@14720
   518
    val used = used_types ctxt @ more_used;
ballarin@14174
   519
  in
wenzelm@18857
   520
    (read (pp ctxt) (syn_of ctxt) ctxt (types, sorts, used) s
wenzelm@18678
   521
      handle TERM (msg, _) => error msg)
wenzelm@14720
   522
    |> app (intern_skolem ctxt internal)
wenzelm@19001
   523
    |> app (expand_consts ctxt)
wenzelm@19001
   524
    |> app (if pattern then I else expand_binds ctxt schematic)
wenzelm@18678
   525
    |> app (if pattern then prepare_dummies else reject_dummies)
wenzelm@14720
   526
  end;
wenzelm@14720
   527
wenzelm@16031
   528
fun gen_read read app pattern schematic ctxt =
wenzelm@16031
   529
  gen_read' read app pattern schematic ctxt (K false) (K NONE) (K NONE) [];
wenzelm@14720
   530
wenzelm@10554
   531
in
wenzelm@10554
   532
wenzelm@16031
   533
val read_termTs           = gen_read' (read_def_termTs false) (apfst o map) false false;
wenzelm@16031
   534
val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false true;
wenzelm@8096
   535
wenzelm@14720
   536
fun read_term_pats T ctxt =
wenzelm@16031
   537
  #1 o gen_read (read_def_termTs false) (apfst o map) true false ctxt o map (rpair T);
wenzelm@8096
   538
val read_prop_pats = read_term_pats propT;
wenzelm@8096
   539
wenzelm@18672
   540
fun read_term_legacy ctxt =
wenzelm@16458
   541
  gen_read' (read_term_thy true) I false false ctxt (K true) (K NONE) (K NONE) [];
wenzelm@14720
   542
wenzelm@16458
   543
val read_term              = gen_read (read_term_thy true) I false false;
wenzelm@16458
   544
val read_prop              = gen_read (read_prop_thy true) I false false;
wenzelm@16458
   545
val read_prop_schematic    = gen_read (read_prop_thy true) I false true;
wenzelm@16458
   546
val read_terms             = gen_read (read_terms_thy true) map false false;
wenzelm@16458
   547
fun read_props schematic   = gen_read (read_props_thy true) map false schematic;
wenzelm@5819
   548
wenzelm@10554
   549
end;
wenzelm@10554
   550
wenzelm@5819
   551
wenzelm@5819
   552
(* certify terms *)
wenzelm@5819
   553
wenzelm@10554
   554
local
wenzelm@10554
   555
wenzelm@18971
   556
fun gen_cert prop pattern schematic ctxt t = t
wenzelm@19001
   557
  |> expand_consts ctxt
wenzelm@19001
   558
  |> (if pattern then I else expand_binds ctxt schematic)
wenzelm@18971
   559
  |> (fn t' => #1 (Sign.certify false prop (pp ctxt) (theory_of ctxt) t')
wenzelm@18678
   560
    handle TYPE (msg, _, _) => error msg
wenzelm@18678
   561
      | TERM (msg, _) => error msg);
wenzelm@16501
   562
wenzelm@10554
   563
in
wenzelm@8096
   564
wenzelm@18971
   565
val cert_term = gen_cert false false false;
wenzelm@18971
   566
val cert_prop = gen_cert true false false;
wenzelm@18971
   567
val cert_props = map oo gen_cert true false;
wenzelm@10554
   568
wenzelm@18971
   569
fun cert_term_pats _ = map o gen_cert false true false;
wenzelm@18971
   570
val cert_prop_pats = map o gen_cert true true false;
wenzelm@10554
   571
wenzelm@10554
   572
end;
wenzelm@5819
   573
wenzelm@5819
   574
wenzelm@5819
   575
(* declare terms *)
wenzelm@5819
   576
wenzelm@10381
   577
local
wenzelm@10381
   578
wenzelm@16861
   579
val ins_types = fold_aterms
wenzelm@17412
   580
  (fn Free (x, T) => Vartab.update ((x, ~1), T)
wenzelm@17412
   581
    | Var v => Vartab.update v
wenzelm@16861
   582
    | _ => I);
wenzelm@5819
   583
wenzelm@16861
   584
val ins_sorts = fold_types (fold_atyps
wenzelm@19033
   585
  (fn TFree (x, S) => Vartab.replace (op =) ((x, ~1), S)
wenzelm@19033
   586
    | TVar v => Vartab.replace (op =) v
wenzelm@16861
   587
    | _ => I));
wenzelm@5819
   588
wenzelm@16861
   589
val ins_used = fold_term_types (fn t =>
wenzelm@16861
   590
  fold_atyps (fn TFree (x, _) => insert (op =) x | _ => I));
wenzelm@12291
   591
wenzelm@16861
   592
val ins_occs = fold_term_types (fn t =>
wenzelm@18953
   593
  fold_atyps (fn TFree (x, _) => Symtab.update_list (x, t) | _ => I));
wenzelm@5819
   594
wenzelm@16861
   595
fun ins_skolem def_ty = fold_rev (fn (x, x') =>
wenzelm@16861
   596
  (case def_ty x' of
wenzelm@17412
   597
    SOME T => Vartab.update ((x, ~1), T)
wenzelm@16861
   598
  | NONE => I));
wenzelm@5994
   599
wenzelm@16031
   600
in
wenzelm@16031
   601
wenzelm@18672
   602
fun declare_syntax t =
wenzelm@18340
   603
  map_defaults (fn (types, sorts, used, occ) => (ins_types t types, sorts, used, occ))
wenzelm@18340
   604
  #> map_defaults (fn (types, sorts, used, occ) => (types, ins_sorts t sorts, used, occ))
wenzelm@18340
   605
  #> map_defaults (fn (types, sorts, used, occ) => (types, sorts, ins_used t used, occ));
wenzelm@10381
   606
wenzelm@19001
   607
fun declare_var (x, opt_T, mx) ctxt =
wenzelm@19001
   608
  let val T = (case opt_T of SOME T => T | NONE => TypeInfer.mixfixT mx)
wenzelm@19001
   609
  in ((x, T, mx), ctxt |> declare_syntax (Free (x, T))) end;
wenzelm@18672
   610
wenzelm@16540
   611
fun declare_term t ctxt =
wenzelm@16031
   612
  ctxt
wenzelm@18672
   613
  |> declare_syntax t
wenzelm@16861
   614
  |> map_defaults (fn (types, sorts, used, occ) => (types, sorts, used, ins_occs t occ))
wenzelm@12291
   615
  |> map_defaults (fn (types, sorts, used, occ) =>
wenzelm@18672
   616
      (ins_skolem (fn x => Vartab.lookup types (x, ~1)) (fixes_of ctxt) types, sorts, used, occ));
wenzelm@5819
   617
wenzelm@18770
   618
end;
wenzelm@18770
   619
wenzelm@18770
   620
wenzelm@18770
   621
(* inferred types of parameters *)
wenzelm@18770
   622
wenzelm@18770
   623
fun infer_type ctxt x =
wenzelm@18770
   624
  (case try (fn () =>
wenzelm@18971
   625
      Sign.infer_types (pp ctxt) (theory_of ctxt) (consts_of ctxt) (def_type ctxt false)
wenzelm@18971
   626
        (def_sort ctxt) (used_types ctxt) true ([Free (x, dummyT)], TypeInfer.logicT)) () of
wenzelm@18770
   627
    SOME (Free (_, T), _) => T
wenzelm@18770
   628
  | _ => error ("Failed to infer type of fixed variable " ^ quote x));
wenzelm@18770
   629
wenzelm@18770
   630
fun inferred_param x ctxt =
wenzelm@18619
   631
  let val T = infer_type ctxt x
wenzelm@19019
   632
  in ((x, T), ctxt |> declare_syntax (Free (x, T))) end;
wenzelm@18619
   633
wenzelm@18770
   634
fun inferred_fixes ctxt =
wenzelm@18770
   635
  fold_map inferred_param (rev (fixed_names_of ctxt)) ctxt;
wenzelm@5819
   636
wenzelm@5819
   637
wenzelm@15703
   638
(* type and constant names *)
wenzelm@15703
   639
wenzelm@15703
   640
fun read_tyname ctxt c =
wenzelm@18340
   641
  if member (op =) (used_types ctxt) c then
wenzelm@18187
   642
    TFree (c, the_default (Sign.defaultS (theory_of ctxt)) (def_sort ctxt (c, ~1)))
wenzelm@16458
   643
  else Sign.read_tyname (theory_of ctxt) c;
wenzelm@15703
   644
wenzelm@15703
   645
fun read_const ctxt c =
wenzelm@15703
   646
  (case lookup_skolem ctxt c of
wenzelm@15703
   647
    SOME c' => Free (c', dummyT)
wenzelm@18971
   648
  | NONE => Consts.read_const (consts_of ctxt) c);
wenzelm@15703
   649
wenzelm@15703
   650
wenzelm@19019
   651
(* renaming term/type frees *)
wenzelm@19019
   652
wenzelm@19019
   653
fun rename_frees ctxt ts frees =
wenzelm@19019
   654
  let
wenzelm@19019
   655
    val (types, sorts, _, _) = defaults_of (ctxt |> fold declare_syntax ts);
wenzelm@19019
   656
    fun rename (x, X) xs =
wenzelm@19019
   657
      let
wenzelm@19019
   658
        fun used y = member (op =) xs y orelse
wenzelm@19019
   659
          Vartab.defined types (y, ~1) orelse Vartab.defined sorts (y, ~1);
wenzelm@19019
   660
        val x' = Term.variant_name used x;
wenzelm@19019
   661
      in ((x', X), x' :: xs) end;
wenzelm@19019
   662
  in #1 (fold_map rename frees []) end;
wenzelm@19019
   663
wenzelm@19019
   664
wenzelm@8616
   665
wenzelm@8616
   666
(** Hindley-Milner polymorphism **)
wenzelm@8616
   667
wenzelm@7925
   668
(* warn_extra_tfrees *)
wenzelm@7925
   669
wenzelm@16540
   670
fun warn_extra_tfrees ctxt1 ctxt2 =
wenzelm@12130
   671
  let
wenzelm@17451
   672
    fun occs_typ a (Type (_, Ts)) = exists (occs_typ a) Ts
wenzelm@17451
   673
      | occs_typ a (TFree (b, _)) = a = b
wenzelm@17451
   674
      | occs_typ _ (TVar _) = false;
wenzelm@17451
   675
    fun occs_free a (Free (x, _)) =
wenzelm@17451
   676
          (case def_type ctxt1 false (x, ~1) of
wenzelm@17451
   677
            SOME T => if occs_typ a T then I else cons (a, x)
wenzelm@17451
   678
          | NONE => cons (a, x))
wenzelm@17451
   679
      | occs_free _ _ = I;
wenzelm@8616
   680
wenzelm@17451
   681
    val occs1 = type_occs_of ctxt1 and occs2 = type_occs_of ctxt2;
wenzelm@17451
   682
    val extras = Symtab.fold (fn (a, ts) =>
wenzelm@17451
   683
      if Symtab.defined occs1 a then I else fold (occs_free a) ts) occs2 [];
wenzelm@18428
   684
    val tfrees = map #1 extras |> sort_distinct string_ord;
wenzelm@18428
   685
    val frees = map #2 extras |> sort_distinct string_ord;
wenzelm@12130
   686
  in
wenzelm@12130
   687
    if null extras then ()
wenzelm@17451
   688
    else warning ("Introduced fixed type variable(s): " ^ commas tfrees ^ " in " ^
wenzelm@17860
   689
      space_implode " or " (map (string_of_term ctxt2 o Syntax.free) frees));
wenzelm@12130
   690
    ctxt2
wenzelm@12130
   691
  end;
wenzelm@8616
   692
wenzelm@8616
   693
wenzelm@8616
   694
(* generalize type variables *)
wenzelm@8616
   695
wenzelm@12550
   696
fun generalize_tfrees inner outer =
wenzelm@8616
   697
  let
wenzelm@12057
   698
    val extra_fixes = fixed_names_of inner \\ fixed_names_of outer;
wenzelm@18340
   699
    fun still_fixed (Free (x, _)) = not (member (op =) extra_fixes x)
wenzelm@8616
   700
      | still_fixed _ = false;
wenzelm@16540
   701
    val occs_inner = type_occs_of inner;
wenzelm@16540
   702
    val occs_outer = type_occs_of outer;
wenzelm@16031
   703
    fun add a gen =
wenzelm@16894
   704
      if Symtab.defined occs_outer a orelse
wenzelm@18953
   705
        exists still_fixed (Symtab.lookup_list occs_inner a)
wenzelm@8616
   706
      then gen else a :: gen;
wenzelm@16031
   707
  in fn tfrees => fold add tfrees [] end;
wenzelm@8616
   708
wenzelm@12550
   709
fun generalize inner outer ts =
wenzelm@8616
   710
  let
wenzelm@18187
   711
    val tfrees = generalize_tfrees inner outer (map #1 (fold Term.add_tfrees ts []));
wenzelm@18340
   712
    fun gen (x, S) = if member (op =) tfrees x then TVar ((x, 0), S) else TFree (x, S);
wenzelm@12550
   713
  in map (Term.map_term_types (Term.map_type_tfree gen)) ts end;
wenzelm@8616
   714
wenzelm@8616
   715
wenzelm@19001
   716
(* polymorphic terms *)
wenzelm@19001
   717
wenzelm@19001
   718
fun polymorphic ctxt ts =
wenzelm@19001
   719
  generalize (ctxt |> fold declare_term ts) ctxt ts;
wenzelm@19001
   720
wenzelm@19019
   721
fun extra_tvars t T =
wenzelm@19019
   722
  let val tvarsT = Term.add_tvarsT T [] in
wenzelm@19001
   723
    Term.fold_types (Term.fold_atyps
wenzelm@19001
   724
      (fn TVar v => if member (op =) tvarsT v then I else insert (op =) v | _ => I)) t []
wenzelm@19001
   725
  end;
wenzelm@19001
   726
wenzelm@19001
   727
wenzelm@9553
   728
wenzelm@9553
   729
(** export theorems **)
wenzelm@8616
   730
wenzelm@18042
   731
fun common_exports is_goal inner outer =
wenzelm@8616
   732
  let
wenzelm@12550
   733
    val gen = generalize_tfrees inner outer;
wenzelm@12057
   734
    val fixes = fixed_names_of inner \\ fixed_names_of outer;
wenzelm@12057
   735
    val asms = Library.drop (length (assumptions_of outer), assumptions_of inner);
wenzelm@11816
   736
    val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms;
wenzelm@16948
   737
  in
wenzelm@18255
   738
    Goal.norm_hhf_protect
wenzelm@16948
   739
    #> Seq.EVERY (rev exp_asms)
wenzelm@16948
   740
    #> Seq.map (fn rule =>
wenzelm@11816
   741
      let
wenzelm@16992
   742
        val thy = Thm.theory_of_thm rule;
wenzelm@16992
   743
        val prop = Thm.full_prop_of rule;
wenzelm@18152
   744
        val frees = map (Thm.cterm_of thy) (List.mapPartial (Term.find_free prop) fixes);
wenzelm@12550
   745
        val tfrees = gen (Term.add_term_tfree_names (prop, []));
wenzelm@11816
   746
      in
wenzelm@11816
   747
        rule
wenzelm@11816
   748
        |> Drule.forall_intr_list frees
wenzelm@18255
   749
        |> Goal.norm_hhf_protect
wenzelm@18138
   750
        |> Drule.tvars_intr_list tfrees |> #2
wenzelm@11816
   751
      end)
wenzelm@11816
   752
  end;
wenzelm@8616
   753
wenzelm@18785
   754
fun export_standard inner outer =
wenzelm@18122
   755
  let val exp = common_exports false inner outer in
wenzelm@13378
   756
    fn th =>
wenzelm@18122
   757
      (case Seq.pull (exp th) of
skalberg@15531
   758
        SOME (th', _) => th' |> Drule.local_standard
wenzelm@16850
   759
      | NONE => sys_error "Failed to export theorem")
wenzelm@13378
   760
  end;
wenzelm@12704
   761
wenzelm@18122
   762
val exports = common_exports false;
wenzelm@18122
   763
val goal_exports = common_exports true;
wenzelm@7925
   764
wenzelm@5819
   765
wenzelm@15758
   766
wenzelm@5819
   767
(** bindings **)
wenzelm@5819
   768
wenzelm@15758
   769
(* delete_update_binds *)
wenzelm@15758
   770
wenzelm@15758
   771
local
wenzelm@5819
   772
wenzelm@18672
   773
val del_bind = map_binds o Vartab.delete_safe;
wenzelm@7606
   774
wenzelm@15758
   775
fun upd_bind ((x, i), t) =
wenzelm@8616
   776
  let
wenzelm@8616
   777
    val T = Term.fastype_of t;
wenzelm@8616
   778
    val t' =
wenzelm@19019
   779
      if null (extra_tvars t T) then t
wenzelm@8637
   780
      else Var ((x ^ "_has_extra_type_vars_on_rhs", i), T);
wenzelm@18953
   781
  in declare_term t' #> map_binds (Vartab.update ((x, i), (T, t'))) end;
wenzelm@5819
   782
wenzelm@15758
   783
fun del_upd_bind (xi, NONE) = del_bind xi
wenzelm@15758
   784
  | del_upd_bind (xi, SOME t) = upd_bind (xi, t);
wenzelm@7606
   785
wenzelm@15758
   786
in
wenzelm@15758
   787
wenzelm@15758
   788
val delete_update_binds = fold del_upd_bind;
wenzelm@15758
   789
wenzelm@15758
   790
end;
wenzelm@7606
   791
wenzelm@5819
   792
wenzelm@8096
   793
(* simult_matches *)
wenzelm@8096
   794
wenzelm@8616
   795
fun simult_matches ctxt [] = []
wenzelm@8616
   796
  | simult_matches ctxt pairs =
wenzelm@8096
   797
      let
wenzelm@18678
   798
        fun fail () = error "Pattern match failed!";
wenzelm@10554
   799
wenzelm@16031
   800
        val maxidx = fold (fn (t1, t2) => fn i =>
wenzelm@16031
   801
          Int.max (Int.max (Term.maxidx_of_term t1, Term.maxidx_of_term t2), i)) pairs ~1;
wenzelm@16458
   802
        val envs = Unify.smash_unifiers (theory_of ctxt, Envir.empty maxidx,
wenzelm@10554
   803
          map swap pairs);    (*prefer assignment of variables from patterns*)
wenzelm@10554
   804
        val env =
wenzelm@8096
   805
          (case Seq.pull envs of
skalberg@15531
   806
            NONE => fail ()
skalberg@15531
   807
          | SOME (env, _) => env);    (*ignore further results*)
wenzelm@12309
   808
        val domain =
wenzelm@12309
   809
          filter_out Term.is_replaced_dummy_pattern (map #1 (Drule.vars_of_terms (map #1 pairs)));
wenzelm@10554
   810
        val _ =    (*may not assign variables from text*)
wenzelm@12309
   811
          if null (map #1 (Envir.alist_of env) inter (map #1 (Drule.vars_of_terms (map #2 pairs))))
wenzelm@12309
   812
          then () else fail ();
wenzelm@16948
   813
        fun norm_bind (xi, (_, t)) =
wenzelm@18310
   814
          if member (op =) domain xi then SOME (xi, Envir.norm_term env t) else NONE;
skalberg@15570
   815
      in List.mapPartial norm_bind (Envir.alist_of env) end;
wenzelm@8096
   816
wenzelm@8096
   817
wenzelm@8096
   818
(* add_binds(_i) *)
wenzelm@5819
   819
wenzelm@7925
   820
local
wenzelm@7925
   821
wenzelm@16031
   822
fun gen_bind prep (xi as (x, _), raw_t) ctxt =
skalberg@15570
   823
  ctxt |> delete_update_binds [(xi, Option.map (prep ctxt) raw_t)];
wenzelm@5819
   824
wenzelm@10810
   825
in
wenzelm@10810
   826
skalberg@15531
   827
fun drop_schematic (b as (xi, SOME t)) = if null (Term.term_vars t) then b else (xi, NONE)
wenzelm@10554
   828
  | drop_schematic b = b;
wenzelm@10554
   829
wenzelm@16031
   830
val add_binds = fold (gen_bind read_term);
wenzelm@16031
   831
val add_binds_i = fold (gen_bind cert_term);
wenzelm@8616
   832
wenzelm@16458
   833
fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts));
wenzelm@12147
   834
val auto_bind_goal = auto_bind AutoBind.goal;
wenzelm@12147
   835
val auto_bind_facts = auto_bind AutoBind.facts;
wenzelm@7925
   836
wenzelm@7925
   837
end;
wenzelm@5819
   838
wenzelm@5819
   839
wenzelm@8096
   840
(* match_bind(_i) *)
wenzelm@5819
   841
wenzelm@8096
   842
local
wenzelm@8096
   843
wenzelm@17860
   844
fun prep_bind prep_pats (raw_pats, t) ctxt =
wenzelm@5819
   845
  let
wenzelm@8096
   846
    val ctxt' = declare_term t ctxt;
wenzelm@8096
   847
    val pats = prep_pats (fastype_of t) ctxt' raw_pats;
wenzelm@8616
   848
    val binds = simult_matches ctxt' (map (rpair t) pats);
wenzelm@17860
   849
  in (binds, ctxt') end;
wenzelm@7670
   850
wenzelm@10465
   851
fun gen_binds prep_terms prep_pats gen raw_binds ctxt =
wenzelm@8616
   852
  let
wenzelm@10465
   853
    val ts = prep_terms ctxt (map snd raw_binds);
wenzelm@17860
   854
    val (binds, ctxt') =
wenzelm@17860
   855
      apfst List.concat (fold_map (prep_bind prep_pats) (map fst raw_binds ~~ ts) ctxt);
wenzelm@8616
   856
    val binds' =
wenzelm@12550
   857
      if gen then map #1 binds ~~ generalize ctxt' ctxt (map #2 binds)
wenzelm@8616
   858
      else binds;
skalberg@15531
   859
    val binds'' = map (apsnd SOME) binds';
wenzelm@18310
   860
    val ctxt'' =
wenzelm@18310
   861
      warn_extra_tfrees ctxt
wenzelm@18310
   862
       (if gen then
wenzelm@18310
   863
          ctxt (*sic!*) |> fold declare_term (map #2 binds') |> add_binds_i binds''
wenzelm@18310
   864
        else ctxt' |> add_binds_i binds'');
wenzelm@18310
   865
  in (ts, ctxt'') end;
wenzelm@8096
   866
wenzelm@8096
   867
in
wenzelm@5935
   868
wenzelm@10465
   869
val match_bind = gen_binds read_terms read_term_pats;
wenzelm@10465
   870
val match_bind_i = gen_binds (map o cert_term) cert_term_pats;
wenzelm@8096
   871
wenzelm@8096
   872
end;
wenzelm@5935
   873
wenzelm@5935
   874
wenzelm@10465
   875
(* propositions with patterns *)
wenzelm@5935
   876
wenzelm@10465
   877
local
wenzelm@8096
   878
wenzelm@10554
   879
fun prep_propp schematic prep_props prep_pats (context, args) =
wenzelm@10465
   880
  let
wenzelm@17860
   881
    fun prep (_, (raw_pats1, raw_pats2)) (ctxt, prop :: props) =
wenzelm@10465
   882
          let
wenzelm@10465
   883
            val ctxt' = declare_term prop ctxt;
wenzelm@10465
   884
            val pats = prep_pats ctxt' (raw_pats1 @ raw_pats2);    (*simultaneous type inference!*)
wenzelm@19019
   885
          in ((prop, chop (length raw_pats1) pats), (ctxt', props)) end
wenzelm@17860
   886
      | prep _ _ = sys_error "prep_propp";
wenzelm@17860
   887
    val (propp, (context', _)) = (fold_map o fold_map) prep args
wenzelm@17860
   888
      (context, prep_props schematic context (List.concat (map (map fst) args)));
wenzelm@10465
   889
  in (context', propp) end;
wenzelm@5935
   890
wenzelm@10465
   891
fun matches ctxt (prop, (pats1, pats2)) =
wenzelm@10465
   892
  simult_matches ctxt (map (rpair prop) pats1 @ map (rpair (Logic.strip_imp_concl prop)) pats2);
wenzelm@8096
   893
wenzelm@10465
   894
fun gen_bind_propp prepp (ctxt, raw_args) =
wenzelm@8096
   895
  let
wenzelm@10465
   896
    val (ctxt', args) = prepp (ctxt, raw_args);
skalberg@15570
   897
    val binds = List.concat (List.concat (map (map (matches ctxt')) args));
wenzelm@10465
   898
    val propss = map (map #1) args;
wenzelm@8616
   899
wenzelm@10554
   900
    (*generalize result: context evaluated now, binds added later*)
wenzelm@8616
   901
    val gen = generalize ctxt' ctxt;
skalberg@15531
   902
    fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
skalberg@15531
   903
  in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
wenzelm@8096
   904
wenzelm@10465
   905
in
wenzelm@10465
   906
wenzelm@11925
   907
val read_propp = prep_propp false read_props read_prop_pats;
wenzelm@11925
   908
val cert_propp = prep_propp false cert_props cert_prop_pats;
wenzelm@10554
   909
val read_propp_schematic = prep_propp true read_props read_prop_pats;
wenzelm@10554
   910
val cert_propp_schematic = prep_propp true cert_props cert_prop_pats;
wenzelm@10554
   911
wenzelm@11925
   912
val bind_propp = gen_bind_propp read_propp;
wenzelm@11925
   913
val bind_propp_i = gen_bind_propp cert_propp;
wenzelm@11925
   914
val bind_propp_schematic = gen_bind_propp read_propp_schematic;
wenzelm@10554
   915
val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic;
wenzelm@6789
   916
wenzelm@10465
   917
end;
wenzelm@10465
   918
wenzelm@6789
   919
wenzelm@5819
   920
wenzelm@5819
   921
(** theorems **)
wenzelm@5819
   922
wenzelm@18042
   923
(* fact_tac *)
wenzelm@18042
   924
wenzelm@18122
   925
fun comp_incr_tac [] _ st = no_tac st
wenzelm@18122
   926
  | comp_incr_tac (th :: ths) i st =
wenzelm@18122
   927
      (Goal.compose_hhf_tac (Drule.incr_indexes st th) i APPEND comp_incr_tac ths i) st;
wenzelm@18042
   928
wenzelm@18122
   929
fun fact_tac facts = Tactic.norm_hhf_tac THEN' comp_incr_tac facts;
wenzelm@18122
   930
wenzelm@18122
   931
fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) =>
wenzelm@18042
   932
  let
wenzelm@19001
   933
    val index = fact_index_of ctxt;
wenzelm@18042
   934
    val facts = FactIndex.could_unify index (Term.strip_all_body goal);
wenzelm@18042
   935
  in fact_tac facts i end);
wenzelm@18042
   936
wenzelm@18042
   937
wenzelm@6091
   938
(* get_thm(s) *)
wenzelm@5819
   939
wenzelm@18042
   940
fun retrieve_thms _ pick ctxt (Fact s) =
wenzelm@16501
   941
      let
wenzelm@18042
   942
        val thy = theory_of ctxt;
wenzelm@18042
   943
        val th = Goal.prove thy [] [] (read_prop ctxt s) (K (ALLGOALS (some_fact_tac ctxt)))
wenzelm@18678
   944
          handle ERROR msg => cat_error msg "Failed to retrieve literal fact.";
wenzelm@18042
   945
      in pick "" [th] end
wenzelm@18042
   946
  | retrieve_thms from_thy pick ctxt xthmref =
wenzelm@18042
   947
      let
wenzelm@18042
   948
        val thy = theory_of ctxt;
wenzelm@19001
   949
        val (space, tab) = #1 (thms_of ctxt);
wenzelm@16501
   950
        val thmref = PureThy.map_name_of_thmref (NameSpace.intern space) xthmref;
wenzelm@16501
   951
        val name = PureThy.name_of_thmref thmref;
wenzelm@16501
   952
      in
wenzelm@17412
   953
        (case Symtab.lookup tab name of
wenzelm@16540
   954
          SOME ths => map (Thm.transfer thy) (PureThy.select_thm thmref ths)
wenzelm@16540
   955
        | NONE => from_thy thy xthmref) |> pick name
wenzelm@18042
   956
      end;
wenzelm@5819
   957
wenzelm@9566
   958
val get_thm = retrieve_thms PureThy.get_thms PureThy.single_thm;
wenzelm@9566
   959
val get_thm_closure = retrieve_thms PureThy.get_thms_closure PureThy.single_thm;
wenzelm@9566
   960
val get_thms = retrieve_thms PureThy.get_thms (K I);
wenzelm@9566
   961
val get_thms_closure = retrieve_thms PureThy.get_thms_closure (K I);
wenzelm@5819
   962
wenzelm@5819
   963
wenzelm@16031
   964
(* valid_thms *)
wenzelm@16031
   965
wenzelm@16031
   966
fun valid_thms ctxt (name, ths) =
wenzelm@18678
   967
  (case try (fn () => get_thms ctxt (Name name)) () of
wenzelm@16031
   968
    NONE => false
wenzelm@16147
   969
  | SOME ths' => Thm.eq_thms (ths, ths'));
wenzelm@16031
   970
wenzelm@16031
   971
wenzelm@16031
   972
(* lthms_containing *)
wenzelm@16031
   973
wenzelm@16031
   974
fun lthms_containing ctxt spec =
wenzelm@16031
   975
  FactIndex.find (fact_index_of ctxt) spec
wenzelm@18043
   976
  |> map ((not o valid_thms ctxt) ? apfst (fn name =>
wenzelm@18042
   977
    NameSpace.hidden (if name = "" then "unnamed" else name)));
wenzelm@16031
   978
wenzelm@16031
   979
wenzelm@13425
   980
(* name space operations *)
wenzelm@12309
   981
wenzelm@19001
   982
val extern_thm = NameSpace.extern o #1 o #1 o thms_of;
wenzelm@12309
   983
wenzelm@19019
   984
val no_base_names = map_naming NameSpace.no_base_names;
wenzelm@16147
   985
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@19019
   986
val qualified_force_prefix = map_naming o NameSpace.qualified_force_prefix;
wenzelm@19001
   987
val restore_naming = map_naming o K o naming_of;
wenzelm@12309
   988
wenzelm@19001
   989
fun hide_thms fully names = map_thms (fn ((space, tab), index) =>
wenzelm@19001
   990
  ((fold (NameSpace.hide fully) names space, tab), index));
wenzelm@13425
   991
wenzelm@12309
   992
wenzelm@17360
   993
(* put_thms *)
wenzelm@5819
   994
wenzelm@18042
   995
fun put_thms ("", NONE) ctxt = ctxt
wenzelm@19001
   996
  | put_thms ("", SOME ths) ctxt = ctxt |> map_thms (fn (facts, index) =>
wenzelm@18672
   997
      let
wenzelm@18672
   998
        val index' = FactIndex.add_local (is_known ctxt) ("", ths) index;
wenzelm@19001
   999
      in (facts, index') end)
wenzelm@19001
  1000
  | put_thms (bname, NONE) ctxt = ctxt |> map_thms (fn ((space, tab), index) =>
wenzelm@18672
  1001
      let
wenzelm@19001
  1002
        val name = NameSpace.full (naming_of ctxt) bname;
wenzelm@18672
  1003
        val tab' = Symtab.delete_safe name tab;
wenzelm@19001
  1004
      in ((space, tab'), index) end)
wenzelm@19001
  1005
  | put_thms (bname, SOME ths) ctxt = ctxt |> map_thms (fn ((space, tab), index) =>
wenzelm@18672
  1006
      let
wenzelm@19001
  1007
        val name = NameSpace.full (naming_of ctxt) bname;
wenzelm@19001
  1008
        val space' = NameSpace.declare (naming_of ctxt) name space;
wenzelm@18672
  1009
        val tab' = Symtab.update (name, ths) tab;
wenzelm@18672
  1010
        val index' = FactIndex.add_local (is_known ctxt) (name, ths) index;
wenzelm@19001
  1011
      in ((space', tab'), index') end);
wenzelm@5819
  1012
wenzelm@7606
  1013
wenzelm@14564
  1014
(* note_thmss *)
wenzelm@5819
  1015
wenzelm@12711
  1016
local
wenzelm@16147
  1017
wenzelm@17860
  1018
fun gen_note_thmss get = fold_map (fn ((name, more_attrs), ths_attrs) => fn ctxt =>
wenzelm@5819
  1019
  let
wenzelm@17360
  1020
    fun app (th, attrs) (ct, ths) =
wenzelm@18728
  1021
      let val (ct', th') = foldl_map (Thm.proof_attributes (attrs @ more_attrs)) (ct, get ctxt th)
wenzelm@12711
  1022
      in (ct', th' :: ths) end;
wenzelm@17360
  1023
    val (ctxt', rev_thms) = fold app ths_attrs (ctxt, []);
skalberg@15570
  1024
    val thms = List.concat (rev rev_thms);
wenzelm@17860
  1025
  in ((name, thms), ctxt' |> put_thms (name, SOME thms)) end);
wenzelm@12711
  1026
wenzelm@12711
  1027
in
wenzelm@12711
  1028
wenzelm@16147
  1029
val note_thmss = gen_note_thmss get_thms;
wenzelm@16147
  1030
val note_thmss_i = gen_note_thmss (K I);
ballarin@15696
  1031
ballarin@15696
  1032
val note_thmss_accesses = gen_note_thmss get_thms;
ballarin@15696
  1033
val note_thmss_accesses_i = gen_note_thmss (K I);
wenzelm@12711
  1034
wenzelm@12711
  1035
end;
wenzelm@9196
  1036
wenzelm@5819
  1037
wenzelm@5819
  1038
wenzelm@18672
  1039
(** parameters **)
wenzelm@17360
  1040
wenzelm@8096
  1041
(* variables *)
wenzelm@8096
  1042
wenzelm@10381
  1043
local
wenzelm@10381
  1044
wenzelm@18672
  1045
fun prep_vars prep_typ internal legacy =
wenzelm@18672
  1046
  fold_map (fn (raw_x, raw_T, raw_mx) => fn ctxt =>
wenzelm@18672
  1047
    let
wenzelm@18672
  1048
      val x = Syntax.const_name raw_x raw_mx;
wenzelm@18672
  1049
      val mx = Syntax.fix_mixfix raw_x raw_mx;
wenzelm@18672
  1050
      val _ =
wenzelm@18678
  1051
        if not legacy andalso not (Syntax.is_identifier (no_skolem internal x)) then
wenzelm@18678
  1052
          error ("Illegal variable name: " ^ quote x)
wenzelm@18672
  1053
        else ();
wenzelm@12504
  1054
wenzelm@18672
  1055
      fun cond_tvars T =
wenzelm@18672
  1056
        if internal then T
wenzelm@18678
  1057
        else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
wenzelm@18672
  1058
      val opt_T = Option.map (cond_tvars o prep_typ ctxt) raw_T;
wenzelm@18672
  1059
      val var = (x, opt_T, mx);
wenzelm@19001
  1060
    in (var, ctxt |> declare_var var |> #2) end);
wenzelm@8096
  1061
wenzelm@10381
  1062
in
wenzelm@10381
  1063
wenzelm@18672
  1064
val read_vars        = prep_vars read_typ false false;
wenzelm@18672
  1065
val cert_vars        = prep_vars cert_typ true false;
wenzelm@18672
  1066
val read_vars_legacy = prep_vars read_typ true true;
wenzelm@18672
  1067
val cert_vars_legacy = prep_vars cert_typ true true;
wenzelm@8096
  1068
wenzelm@10381
  1069
end;
wenzelm@10381
  1070
wenzelm@8096
  1071
wenzelm@19001
  1072
(* abbreviations *)
wenzelm@19001
  1073
wenzelm@19001
  1074
local
wenzelm@19001
  1075
wenzelm@19001
  1076
fun gen_abbrevs prep_vars prep_term revert = fold (fn (raw_c, raw_t, raw_mx) => fn ctxt =>
wenzelm@19001
  1077
  let
wenzelm@19019
  1078
    val thy = theory_of ctxt and naming = naming_of ctxt;
wenzelm@19001
  1079
    val ([(c, _, mx)], _) = prep_vars [(raw_c, NONE, raw_mx)] ctxt;
wenzelm@19001
  1080
    val [t] = polymorphic ctxt [prep_term ctxt raw_t];
wenzelm@19019
  1081
    val T = Term.fastype_of t;
wenzelm@19001
  1082
    val _ =
wenzelm@19019
  1083
      if null (extra_tvars t T) then ()
wenzelm@19001
  1084
      else error ("Extra type variables on rhs of abbreviation: " ^ quote c);
wenzelm@19001
  1085
  in
wenzelm@19001
  1086
    ctxt
wenzelm@19001
  1087
    |> declare_term t
wenzelm@19033
  1088
    |> map_consts (apsnd (Consts.abbreviate (pp ctxt) (tsig_of ctxt) naming revert (c, t)))
wenzelm@19019
  1089
    |> map_syntax (LocalSyntax.add_syntax thy [(false, (NameSpace.full naming c, T, mx))])
wenzelm@19001
  1090
  end);
wenzelm@19001
  1091
wenzelm@19001
  1092
in
wenzelm@19001
  1093
wenzelm@19001
  1094
val add_abbrevs = gen_abbrevs read_vars read_term;
wenzelm@19001
  1095
val add_abbrevs_i = gen_abbrevs cert_vars cert_term;
wenzelm@19001
  1096
wenzelm@19001
  1097
end;
wenzelm@19001
  1098
wenzelm@19001
  1099
wenzelm@18672
  1100
(* fixes *)
wenzelm@5819
  1101
wenzelm@8096
  1102
local
wenzelm@8096
  1103
wenzelm@19001
  1104
fun prep_mixfix (x, T, mx) =
wenzelm@19019
  1105
  if mx <> NoSyn andalso mx <> Structure andalso
wenzelm@19019
  1106
      (can Syntax.dest_internal x orelse can Syntax.dest_skolem x) then
wenzelm@19001
  1107
    error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x)
wenzelm@19001
  1108
  else (true, (x, T, mx));
wenzelm@19001
  1109
wenzelm@18672
  1110
fun no_dups _ [] = ()
wenzelm@18900
  1111
  | no_dups ctxt dups = error ("Duplicate fixed variable(s): " ^ commas_quote dups);
wenzelm@11925
  1112
wenzelm@18844
  1113
fun gen_fixes prep raw_vars ctxt =
wenzelm@8096
  1114
  let
wenzelm@18672
  1115
    val (ys, zs) = split_list (fixes_of ctxt);
wenzelm@18672
  1116
    val (vars, ctxt') = prep raw_vars ctxt;
wenzelm@12130
  1117
    val xs = map #1 vars;
wenzelm@18971
  1118
    val _ = no_dups ctxt (duplicates (op =) xs);
wenzelm@18672
  1119
    val xs' =
wenzelm@18672
  1120
      if is_body ctxt then Term.variantlist (map Syntax.skolem xs, zs)
wenzelm@18672
  1121
      else (no_dups ctxt (xs inter_string ys); no_dups ctxt (xs inter_string zs); xs);
wenzelm@8096
  1122
  in
wenzelm@18672
  1123
    ctxt'
wenzelm@18672
  1124
    |> map_fixes (fn (b, fixes) => (b, rev (xs ~~ xs') @ fixes))
wenzelm@19001
  1125
    |> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars)
wenzelm@19019
  1126
    |-> (map_syntax o LocalSyntax.add_syntax (theory_of ctxt) o map prep_mixfix)
wenzelm@18672
  1127
    |> pair xs'
wenzelm@8096
  1128
  end;
wenzelm@5819
  1129
wenzelm@8096
  1130
in
wenzelm@7679
  1131
wenzelm@18844
  1132
val add_fixes = gen_fixes read_vars;
wenzelm@18844
  1133
val add_fixes_i = gen_fixes cert_vars;
wenzelm@18844
  1134
val add_fixes_legacy = gen_fixes cert_vars_legacy;
wenzelm@8096
  1135
wenzelm@8096
  1136
end;
wenzelm@5819
  1137
wenzelm@18672
  1138
wenzelm@18844
  1139
(* invent fixes *)
wenzelm@18844
  1140
wenzelm@18844
  1141
fun invent_fixes xs ctxt =
wenzelm@18844
  1142
  ctxt
wenzelm@18844
  1143
  |> set_body true
wenzelm@18844
  1144
  |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs)
wenzelm@18844
  1145
  ||> restore_body ctxt;
wenzelm@18844
  1146
wenzelm@18844
  1147
wenzelm@18672
  1148
(* fixes vs. frees *)
wenzelm@12016
  1149
wenzelm@18672
  1150
fun fix_frees t ctxt =
wenzelm@18672
  1151
  let
wenzelm@18672
  1152
    fun add (Free (x, T)) = if is_fixed ctxt x then I else insert (op =) (x, SOME T, NoSyn)
wenzelm@18672
  1153
      | add _ = I;
wenzelm@18672
  1154
    val fixes = rev (fold_aterms add t []);
wenzelm@18809
  1155
  in
wenzelm@18809
  1156
    ctxt
wenzelm@18809
  1157
    |> declare_term t
wenzelm@18809
  1158
    |> set_body false
wenzelm@18809
  1159
    |> (snd o add_fixes_i fixes)
wenzelm@18809
  1160
    |> restore_body ctxt
wenzelm@18809
  1161
  end;
wenzelm@6895
  1162
wenzelm@18672
  1163
fun auto_fixes (arg as (ctxt, (propss, x))) =
wenzelm@18672
  1164
  if is_body ctxt then arg
wenzelm@18672
  1165
  else ((fold o fold) fix_frees propss ctxt, (propss, x));
wenzelm@18672
  1166
wenzelm@18672
  1167
fun bind_fixes xs ctxt =
wenzelm@9291
  1168
  let
wenzelm@18672
  1169
    val (_, ctxt') = ctxt |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs);
wenzelm@9291
  1170
    fun bind (t as Free (x, T)) =
wenzelm@18340
  1171
          if member (op =) xs x then
skalberg@15531
  1172
            (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
wenzelm@9291
  1173
          else t
wenzelm@9291
  1174
      | bind (t $ u) = bind t $ bind u
wenzelm@9291
  1175
      | bind (Abs (x, T, t)) = Abs (x, T, bind t)
wenzelm@9291
  1176
      | bind a = a;
wenzelm@18672
  1177
  in (bind, ctxt') end;
wenzelm@18672
  1178
wenzelm@9291
  1179
wenzelm@9291
  1180
wenzelm@18672
  1181
(** assumptions **)
wenzelm@18187
  1182
wenzelm@18672
  1183
(* generic assms *)
wenzelm@18187
  1184
wenzelm@18187
  1185
local
wenzelm@18187
  1186
wenzelm@18672
  1187
fun gen_assm ((name, attrs), props) ctxt =
wenzelm@18187
  1188
  let
wenzelm@18187
  1189
    val cprops = map (Thm.cterm_of (theory_of ctxt)) props;
wenzelm@18187
  1190
    val asms = map (Goal.norm_hhf o Thm.assume) cprops;
wenzelm@18187
  1191
wenzelm@18187
  1192
    val ths = map (fn th => ([th], [])) asms;
wenzelm@18187
  1193
    val ([(_, thms)], ctxt') =
wenzelm@18187
  1194
      ctxt
wenzelm@18187
  1195
      |> auto_bind_facts props
wenzelm@18187
  1196
      |> note_thmss_i [((name, attrs), ths)];
wenzelm@18187
  1197
  in ((cprops, (name, asms), (name, thms)), ctxt') end;
wenzelm@18187
  1198
wenzelm@18187
  1199
fun gen_assms prepp exp args ctxt =
wenzelm@18187
  1200
  let
wenzelm@18672
  1201
    val (propss, ctxt1) = swap (prepp (ctxt, map snd args));
wenzelm@18672
  1202
    val (results, ctxt2) = fold_map gen_assm (map fst args ~~ propss) ctxt1;
wenzelm@18187
  1203
wenzelm@18672
  1204
    val new_asms = List.concat (map #1 results);
wenzelm@18672
  1205
    val new_prems = map #2 results;
wenzelm@18809
  1206
    val ctxt3 = ctxt2
wenzelm@18809
  1207
      |> map_assms (fn (asms, prems) => (asms @ [(new_asms, exp)], prems @ new_prems))
wenzelm@18809
  1208
    val ctxt4 = ctxt3
wenzelm@18809
  1209
      |> put_thms ("prems", SOME (prems_of ctxt3));
wenzelm@18672
  1210
  in (map #3 results, warn_extra_tfrees ctxt ctxt4) end;
wenzelm@18187
  1211
wenzelm@18187
  1212
in
wenzelm@18187
  1213
wenzelm@18672
  1214
val add_assms = gen_assms (apsnd #1 o bind_propp);
wenzelm@18672
  1215
val add_assms_i = gen_assms (apsnd #1 o bind_propp_i);
wenzelm@18187
  1216
wenzelm@18187
  1217
end;
wenzelm@18187
  1218
wenzelm@18187
  1219
wenzelm@18672
  1220
(* basic assumptions *)
wenzelm@18672
  1221
wenzelm@18879
  1222
(*
wenzelm@18879
  1223
    [A]
wenzelm@18971
  1224
     :
wenzelm@18879
  1225
     B
wenzelm@18879
  1226
  --------
wenzelm@18879
  1227
  #A ==> B
wenzelm@18879
  1228
*)
wenzelm@18672
  1229
fun assume_export true = Seq.single oo Drule.implies_intr_protected
wenzelm@18672
  1230
  | assume_export false = Seq.single oo Drule.implies_intr_list;
wenzelm@18672
  1231
wenzelm@18879
  1232
(*
wenzelm@18879
  1233
    [A]
wenzelm@18971
  1234
     :
wenzelm@18879
  1235
     B
wenzelm@18879
  1236
  -------
wenzelm@18879
  1237
  A ==> B
wenzelm@18879
  1238
*)
wenzelm@18672
  1239
fun presume_export _ = assume_export false;
wenzelm@18672
  1240
wenzelm@18672
  1241
wenzelm@18672
  1242
(* additional views *)
wenzelm@18672
  1243
wenzelm@18672
  1244
fun add_view outer view = map_assms (fn (asms, prems) =>
wenzelm@18672
  1245
  let
wenzelm@19019
  1246
    val (asms1, asms2) = chop (length (assumptions_of outer)) asms;
wenzelm@18672
  1247
    val asms' = asms1 @ [(view, assume_export)] @ asms2;
wenzelm@18672
  1248
  in (asms', prems) end);
wenzelm@18672
  1249
wenzelm@18785
  1250
fun export_view view inner outer = export_standard (add_view outer view inner) outer;
wenzelm@18672
  1251
wenzelm@18672
  1252
wenzelm@5819
  1253
wenzelm@8373
  1254
(** cases **)
wenzelm@8373
  1255
wenzelm@16147
  1256
local
wenzelm@16147
  1257
wenzelm@16668
  1258
fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
wenzelm@16147
  1259
wenzelm@18476
  1260
fun add_case _ ("", _) cases = cases
wenzelm@18476
  1261
  | add_case _ (name, NONE) cases = rem_case name cases
wenzelm@18476
  1262
  | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
wenzelm@16147
  1263
wenzelm@18678
  1264
fun prep_case name fxs c =
wenzelm@18609
  1265
  let
wenzelm@18609
  1266
    fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
wenzelm@18609
  1267
      | replace [] ys = ys
wenzelm@18678
  1268
      | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
wenzelm@18609
  1269
    val RuleCases.Case {fixes, assumes, binds, cases} = c;
wenzelm@18609
  1270
    val fixes' = replace fxs fixes;
wenzelm@18609
  1271
    val binds' = map drop_schematic binds;
wenzelm@18609
  1272
  in
wenzelm@18609
  1273
    if null (fold (Term.add_tvarsT o snd) fixes []) andalso
wenzelm@18609
  1274
      null (fold (fold Term.add_vars o snd) assumes []) then
wenzelm@18609
  1275
        RuleCases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
wenzelm@18678
  1276
    else error ("Illegal schematic variable(s) in case " ^ quote name)
wenzelm@18609
  1277
  end;
wenzelm@18609
  1278
wenzelm@18672
  1279
fun fix (x, T) ctxt =
wenzelm@18672
  1280
  let
wenzelm@18672
  1281
    val (bind, ctxt') = bind_fixes [x] ctxt;
wenzelm@18672
  1282
    val t = bind (Free (x, T));
wenzelm@18672
  1283
  in (t, ctxt' |> declare_syntax t) end;
wenzelm@18672
  1284
wenzelm@16147
  1285
in
wenzelm@16147
  1286
wenzelm@18672
  1287
fun add_cases is_proper = map_cases o fold (add_case is_proper);
wenzelm@18609
  1288
wenzelm@18609
  1289
fun case_result c ctxt =
wenzelm@18609
  1290
  let
wenzelm@18609
  1291
    val RuleCases.Case {fixes, ...} = c;
wenzelm@18672
  1292
    val (ts, ctxt') = ctxt |> fold_map fix fixes;
wenzelm@18672
  1293
    val RuleCases.Case {assumes, binds, cases, ...} = RuleCases.apply ts c;
wenzelm@18609
  1294
  in
wenzelm@18609
  1295
    ctxt'
wenzelm@18699
  1296
    |> add_binds_i (map drop_schematic binds)
wenzelm@18609
  1297
    |> add_cases true (map (apsnd SOME) cases)
wenzelm@18609
  1298
    |> pair (assumes, (binds, cases))
wenzelm@18609
  1299
  end;
wenzelm@18609
  1300
wenzelm@18609
  1301
val apply_case = apfst fst oo case_result;
wenzelm@18609
  1302
wenzelm@16540
  1303
fun get_case ctxt name xs =
wenzelm@17184
  1304
  (case AList.lookup (op =) (cases_of ctxt) name of
wenzelm@18678
  1305
    NONE => error ("Unknown case: " ^ quote name)
wenzelm@18678
  1306
  | SOME (c, _) => prep_case name xs c);
wenzelm@8373
  1307
wenzelm@16147
  1308
end;
wenzelm@8373
  1309
wenzelm@8373
  1310
wenzelm@8373
  1311
wenzelm@10810
  1312
(** print context information **)
wenzelm@10810
  1313
wenzelm@10810
  1314
val verbose = ref false;
wenzelm@10810
  1315
fun verb f x = if ! verbose then f (x ()) else [];
wenzelm@10810
  1316
wenzelm@10810
  1317
fun setmp_verbose f x = Library.setmp verbose true f x;
wenzelm@10810
  1318
wenzelm@10810
  1319
fun pretty_items prt name items =
wenzelm@10810
  1320
  let
wenzelm@10810
  1321
    fun prt_itms (name, [x]) = Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, prt x]
wenzelm@10810
  1322
      | prt_itms (name, xs) = Pretty.big_list (name ^ ":") (map prt xs);
wenzelm@10810
  1323
  in
wenzelm@10810
  1324
    if null items andalso not (! verbose) then []
wenzelm@10810
  1325
    else [Pretty.big_list name (map prt_itms items)]
wenzelm@10810
  1326
  end;
wenzelm@10810
  1327
wenzelm@10810
  1328
wenzelm@12072
  1329
(* local syntax *)
wenzelm@12072
  1330
wenzelm@12093
  1331
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@12072
  1332
wenzelm@12072
  1333
wenzelm@18971
  1334
(* local consts *)
wenzelm@18971
  1335
wenzelm@18971
  1336
fun pretty_consts ctxt =
wenzelm@18971
  1337
  let
wenzelm@19033
  1338
    val ((_, globals), (space, consts)) =
wenzelm@19033
  1339
      pairself (#constants o Consts.dest) (#consts (rep_context ctxt));
wenzelm@18971
  1340
    fun local_abbrev (_, (_, NONE)) = I
wenzelm@18971
  1341
      | local_abbrev (c, (T, SOME t)) =
wenzelm@18971
  1342
          if Symtab.defined globals c then I else cons (c, Logic.mk_equals (Const (c, T), t));
wenzelm@18971
  1343
    val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold local_abbrev consts []));
wenzelm@18971
  1344
  in
wenzelm@18971
  1345
    if null abbrevs andalso not (! verbose) then []
wenzelm@19001
  1346
    else [Pretty.big_list "abbreviations:" (map (pretty_term' false ctxt o #2) abbrevs)]
wenzelm@18971
  1347
  end;
wenzelm@18971
  1348
wenzelm@18971
  1349
wenzelm@10810
  1350
(* term bindings *)
wenzelm@10810
  1351
wenzelm@16540
  1352
fun pretty_binds ctxt =
wenzelm@10810
  1353
  let
wenzelm@16540
  1354
    val binds = binds_of ctxt;
wenzelm@19001
  1355
    fun prt_bind (xi, (T, t)) = pretty_term' false ctxt (Logic.mk_equals (Var (xi, T), t));
wenzelm@10810
  1356
  in
wenzelm@15758
  1357
    if Vartab.is_empty binds andalso not (! verbose) then []
wenzelm@15758
  1358
    else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
wenzelm@10810
  1359
  end;
wenzelm@10810
  1360
wenzelm@10810
  1361
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@10810
  1362
wenzelm@10810
  1363
wenzelm@10810
  1364
(* local theorems *)
wenzelm@10810
  1365
wenzelm@16540
  1366
fun pretty_lthms ctxt =
wenzelm@19001
  1367
  pretty_items (pretty_thm ctxt) "facts:" (NameSpace.extern_table (#1 (thms_of ctxt)));
wenzelm@10810
  1368
wenzelm@12057
  1369
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
wenzelm@10810
  1370
wenzelm@10810
  1371
wenzelm@10810
  1372
(* local contexts *)
wenzelm@10810
  1373
wenzelm@16540
  1374
fun pretty_cases ctxt =
wenzelm@10810
  1375
  let
wenzelm@12057
  1376
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1377
wenzelm@10810
  1378
    fun prt_let (xi, t) = Pretty.block
wenzelm@10818
  1379
      [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
wenzelm@10810
  1380
        Pretty.quote (prt_term t)];
wenzelm@10810
  1381
wenzelm@13425
  1382
    fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
wenzelm@13425
  1383
      ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
wenzelm@13425
  1384
wenzelm@10810
  1385
    fun prt_sect _ _ _ [] = []
wenzelm@10810
  1386
      | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s ::
skalberg@15570
  1387
            List.concat (Library.separate sep (map (Library.single o prt) xs))))];
wenzelm@10810
  1388
wenzelm@18609
  1389
    fun prt_case (name, (fixes, (asms, (lets, cs)))) = Pretty.block (Pretty.fbreaks
wenzelm@10810
  1390
      (Pretty.str (name ^ ":") ::
wenzelm@11915
  1391
        prt_sect "fix" [] (Pretty.str o fst) fixes @
wenzelm@10810
  1392
        prt_sect "let" [Pretty.str "and"] prt_let
skalberg@15570
  1393
          (List.mapPartial (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
wenzelm@13425
  1394
        (if forall (null o #2) asms then []
wenzelm@18609
  1395
          else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
wenzelm@18609
  1396
        prt_sect "subcases:" [] (Pretty.str o fst) cs));
wenzelm@16540
  1397
wenzelm@18476
  1398
    fun add_case (_, (_, false)) = I
wenzelm@18609
  1399
      | add_case (name, (c as RuleCases.Case {fixes, ...}, true)) =
wenzelm@18609
  1400
          cons (name, (fixes, #1 (case_result c ctxt)));
wenzelm@18476
  1401
    val cases = fold add_case (cases_of ctxt) [];
wenzelm@10810
  1402
  in
wenzelm@10810
  1403
    if null cases andalso not (! verbose) then []
wenzelm@18476
  1404
    else [Pretty.big_list "cases:" (map prt_case cases)]
wenzelm@10810
  1405
  end;
wenzelm@10810
  1406
wenzelm@10810
  1407
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@10810
  1408
wenzelm@10810
  1409
wenzelm@12057
  1410
(* core context *)
wenzelm@10810
  1411
wenzelm@10810
  1412
val prems_limit = ref 10;
wenzelm@10810
  1413
wenzelm@18672
  1414
fun pretty_ctxt ctxt =
wenzelm@10810
  1415
  let
wenzelm@12057
  1416
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1417
wenzelm@12093
  1418
    (*structures*)
wenzelm@19001
  1419
    val structs = LocalSyntax.structs_of (syntax_of ctxt);
wenzelm@12093
  1420
    val prt_structs = if null structs then []
wenzelm@12093
  1421
      else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
wenzelm@12093
  1422
        Pretty.commas (map Pretty.str structs))];
wenzelm@12093
  1423
wenzelm@12057
  1424
    (*fixes*)
wenzelm@12057
  1425
    fun prt_fix (x, x') =
wenzelm@12057
  1426
      if x = x' then Pretty.str x
wenzelm@12057
  1427
      else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@18340
  1428
    val fixes =
wenzelm@18340
  1429
      rev (filter_out ((can Syntax.dest_internal orf member (op =) structs) o #1) (fixes_of ctxt));
wenzelm@12093
  1430
    val prt_fixes = if null fixes then []
wenzelm@12093
  1431
      else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@12093
  1432
        Pretty.commas (map prt_fix fixes))];
wenzelm@12057
  1433
wenzelm@12057
  1434
    (*prems*)
wenzelm@10810
  1435
    val limit = ! prems_limit;
wenzelm@10810
  1436
    val prems = prems_of ctxt;
wenzelm@10810
  1437
    val len = length prems;
wenzelm@12093
  1438
    val prt_prems = if null prems then []
wenzelm@12093
  1439
      else [Pretty.big_list "prems:" ((if len <= limit then [] else [Pretty.str "..."]) @
wenzelm@12093
  1440
        map (pretty_thm ctxt) (Library.drop (len - limit, prems)))];
wenzelm@12093
  1441
wenzelm@12093
  1442
  in prt_structs @ prt_fixes @ prt_prems end;
wenzelm@10810
  1443
wenzelm@10810
  1444
wenzelm@10810
  1445
(* main context *)
wenzelm@10810
  1446
wenzelm@16540
  1447
fun pretty_context ctxt =
wenzelm@10810
  1448
  let
wenzelm@12057
  1449
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1450
    val prt_typ = pretty_typ ctxt;
wenzelm@12057
  1451
    val prt_sort = pretty_sort ctxt;
wenzelm@10810
  1452
wenzelm@10810
  1453
    (*theory*)
wenzelm@12057
  1454
    val pretty_thy = Pretty.block
wenzelm@17384
  1455
      [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
wenzelm@10810
  1456
wenzelm@10810
  1457
    (*defaults*)
wenzelm@10810
  1458
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@10810
  1459
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@10810
  1460
wenzelm@10810
  1461
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@10810
  1462
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@10810
  1463
wenzelm@10810
  1464
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@10810
  1465
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@10810
  1466
wenzelm@10810
  1467
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@10810
  1468
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@16540
  1469
wenzelm@16540
  1470
    val (types, sorts, used, _) = defaults_of ctxt;
wenzelm@10810
  1471
  in
wenzelm@18609
  1472
    verb single (K pretty_thy) @
wenzelm@18672
  1473
    pretty_ctxt ctxt @
wenzelm@18971
  1474
    verb pretty_consts (K ctxt) @
wenzelm@10810
  1475
    verb pretty_binds (K ctxt) @
wenzelm@12057
  1476
    verb pretty_lthms (K ctxt) @
wenzelm@10810
  1477
    verb pretty_cases (K ctxt) @
wenzelm@18609
  1478
    verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@18609
  1479
    verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @
wenzelm@18809
  1480
    verb single (fn () => Pretty.strs ("used type variable names:" :: rev used))
wenzelm@10810
  1481
  end;
wenzelm@10810
  1482
wenzelm@18809
  1483
wenzelm@18809
  1484
(* toplevel pretty printing *)
wenzelm@18809
  1485
wenzelm@18809
  1486
val debug = ref false;
wenzelm@18809
  1487
wenzelm@18809
  1488
fun pprint_context ctxt = Pretty.pprint
wenzelm@18809
  1489
 (if ! debug then Pretty.quote (Pretty.big_list "proof context:" (pretty_context ctxt))
wenzelm@18809
  1490
  else Pretty.str "<context>");
wenzelm@18809
  1491
wenzelm@5819
  1492
end;