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