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