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