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