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