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