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