src/Pure/Isar/proof_context.ML
author wenzelm
Thu Jul 27 13:43:12 2006 +0200 (2006-07-27 ago)
changeset 20234 7e0693474bcd
parent 20209 974d98969ba6
child 20244 89a407400874
permissions -rw-r--r--
added legacy_pretty_thm (with fall-back on ProtoPure.thy);
moved basic assumption operations to assumption.ML;
tuned;
wenzelm@5819
     1
(*  Title:      Pure/Isar/proof_context.ML
wenzelm@5819
     2
    ID:         $Id$
wenzelm@5819
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5819
     4
wenzelm@19001
     5
The key concept of Isar proof contexts: elevates primitive local
wenzelm@19001
     6
reasoning Gamma |- phi to a structured concept, with generic context
wenzelm@20234
     7
elements.  See also structure Variable and Assumption.
wenzelm@5819
     8
*)
wenzelm@5819
     9
wenzelm@5819
    10
signature PROOF_CONTEXT =
wenzelm@5819
    11
sig
wenzelm@17756
    12
  type context (*= Context.proof*)
wenzelm@5819
    13
  val theory_of: context -> theory
wenzelm@18672
    14
  val init: theory -> context
wenzelm@19387
    15
  val full_name: context -> bstring -> string
wenzelm@19663
    16
  val consts_of: context -> Consts.T
wenzelm@19543
    17
  val set_syntax_mode: string * bool -> context -> context
wenzelm@19543
    18
  val restore_syntax_mode: context -> context -> context
wenzelm@16031
    19
  val fact_index_of: context -> FactIndex.T
wenzelm@17072
    20
  val transfer: theory -> context -> context
wenzelm@19019
    21
  val map_theory: (theory -> theory) -> context -> context
wenzelm@14828
    22
  val pretty_term: context -> term -> Pretty.T
wenzelm@14828
    23
  val pretty_typ: context -> typ -> Pretty.T
wenzelm@14828
    24
  val pretty_sort: context -> sort -> Pretty.T
wenzelm@19411
    25
  val pretty_classrel: context -> class list -> Pretty.T
wenzelm@19411
    26
  val pretty_arity: context -> arity -> Pretty.T
wenzelm@14828
    27
  val pp: context -> Pretty.pp
wenzelm@20234
    28
  val legacy_pretty_thm: bool -> thm -> Pretty.T
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@5819
    37
  val read_typ: context -> string -> typ
wenzelm@16348
    38
  val read_typ_syntax: context -> string -> typ
wenzelm@16348
    39
  val read_typ_abbrev: context -> string -> typ
wenzelm@5819
    40
  val cert_typ: context -> typ -> typ
wenzelm@16348
    41
  val cert_typ_syntax: context -> typ -> typ
wenzelm@16348
    42
  val cert_typ_abbrev: context -> typ -> typ
wenzelm@10583
    43
  val get_skolem: context -> string -> string
wenzelm@18255
    44
  val revert_skolem: context -> string -> string
wenzelm@9133
    45
  val extern_skolem: context -> term -> term
wenzelm@14720
    46
  val read_termTs: context -> (string -> bool) -> (indexname -> typ option)
wenzelm@14720
    47
    -> (indexname -> sort option) -> string list -> (string * typ) list
wenzelm@14720
    48
    -> term list * (indexname * typ) list
wenzelm@14720
    49
  val read_termTs_schematic: 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@18672
    52
  val read_term_legacy: context -> string -> term
wenzelm@5819
    53
  val read_term: context -> string -> term
wenzelm@5819
    54
  val read_prop: context -> string -> term
wenzelm@11925
    55
  val read_prop_schematic: context -> string -> term
wenzelm@8096
    56
  val read_term_pats: typ -> context -> string list -> term list
wenzelm@8096
    57
  val read_prop_pats: context -> string list -> term list
wenzelm@5819
    58
  val cert_term: context -> term -> term
wenzelm@5819
    59
  val cert_prop: context -> term -> term
wenzelm@8096
    60
  val cert_term_pats: typ -> context -> term list -> term list
wenzelm@8096
    61
  val cert_prop_pats: context -> term list -> term list
wenzelm@18770
    62
  val infer_type: context -> string -> typ
wenzelm@18770
    63
  val inferred_param: string -> context -> (string * typ) * context
wenzelm@18770
    64
  val inferred_fixes: context -> (string * typ) list * context
wenzelm@15703
    65
  val read_tyname: context -> string -> typ
wenzelm@15703
    66
  val read_const: context -> string -> term
wenzelm@19916
    67
  val goal_exports: context -> context -> thm list -> thm list Seq.seq
wenzelm@19916
    68
  val exports: context -> context -> thm list -> thm list Seq.seq
wenzelm@19916
    69
  val export: context -> context -> thm list -> thm list
wenzelm@19916
    70
  val export_standard: context -> context -> thm list -> thm list
wenzelm@10810
    71
  val add_binds: (indexname * string option) list -> context -> context
wenzelm@10810
    72
  val add_binds_i: (indexname * term option) list -> context -> context
wenzelm@12147
    73
  val auto_bind_goal: term list -> context -> context
wenzelm@12147
    74
  val auto_bind_facts: term list -> context -> context
wenzelm@18310
    75
  val match_bind: bool -> (string list * string) list -> context -> term list * context
wenzelm@18310
    76
  val match_bind_i: bool -> (term list * term) list -> context -> term list * context
wenzelm@19585
    77
  val read_propp: context * (string * string list) list list
wenzelm@19585
    78
    -> context * (term * term list) list list
wenzelm@19585
    79
  val cert_propp: context * (term * term list) list list
wenzelm@19585
    80
    -> context * (term * term list) list list
wenzelm@19585
    81
  val read_propp_schematic: context * (string * string list) list list
wenzelm@19585
    82
    -> context * (term * term list) list list
wenzelm@19585
    83
  val cert_propp_schematic: context * (term * term list) list list
wenzelm@19585
    84
    -> context * (term * term list) list list
wenzelm@19585
    85
  val bind_propp: context * (string * string list) list list
wenzelm@10465
    86
    -> context * (term list list * (context -> context))
wenzelm@19585
    87
  val bind_propp_i: context * (term * term list) list list
wenzelm@10465
    88
    -> context * (term list list * (context -> context))
wenzelm@19585
    89
  val bind_propp_schematic: context * (string * string list) list list
wenzelm@10554
    90
    -> context * (term list list * (context -> context))
wenzelm@19585
    91
  val bind_propp_schematic_i: context * (term * term list) list list
wenzelm@10554
    92
    -> context * (term list list * (context -> context))
wenzelm@18042
    93
  val fact_tac: thm list -> int -> tactic
wenzelm@18042
    94
  val some_fact_tac: context -> int -> tactic
berghofe@15456
    95
  val get_thm: context -> thmref -> thm
berghofe@15456
    96
  val get_thm_closure: context -> thmref -> thm
berghofe@15456
    97
  val get_thms: context -> thmref -> thm list
berghofe@15456
    98
  val get_thms_closure: context -> thmref -> thm list
wenzelm@16031
    99
  val valid_thms: context -> string * thm list -> bool
wenzelm@16031
   100
  val lthms_containing: context -> FactIndex.spec -> (string * thm list) list
wenzelm@19019
   101
  val no_base_names: context -> context
wenzelm@16147
   102
  val qualified_names: context -> context
wenzelm@19062
   103
  val sticky_prefix: string -> context -> context
wenzelm@16147
   104
  val restore_naming: context -> context -> context
wenzelm@13425
   105
  val hide_thms: bool -> string list -> context -> context
wenzelm@19079
   106
  val put_thms: bool -> string * thm list option -> context -> context
wenzelm@19079
   107
  val put_thms_internal: string * thm list option -> context -> context
wenzelm@14564
   108
  val note_thmss:
wenzelm@18728
   109
    ((bstring * attribute list) * (thmref * attribute list) list) list ->
wenzelm@17860
   110
      context -> (bstring * thm list) list * context
wenzelm@14564
   111
  val note_thmss_i:
wenzelm@18728
   112
    ((bstring * attribute list) * (thm list * attribute list) list) list ->
wenzelm@17860
   113
      context -> (bstring * thm list) list * context
wenzelm@18672
   114
  val read_vars: (string * string option * mixfix) list -> context ->
wenzelm@18672
   115
    (string * typ option * mixfix) list * context
wenzelm@18672
   116
  val cert_vars: (string * typ option * mixfix) list -> context ->
wenzelm@18672
   117
    (string * typ option * mixfix) list * context
wenzelm@18672
   118
  val read_vars_legacy: (string * string option * mixfix) list -> context ->
wenzelm@18672
   119
    (string * typ option * mixfix) list * context
wenzelm@18672
   120
  val cert_vars_legacy: (string * typ option * mixfix) list -> context ->
wenzelm@18672
   121
    (string * typ option * mixfix) list * context
wenzelm@18672
   122
  val add_fixes: (string * string option * mixfix) list -> context -> string list * context
wenzelm@18672
   123
  val add_fixes_i: (string * typ option * mixfix) list -> context -> string list * context
wenzelm@18672
   124
  val add_fixes_legacy: (string * typ option * mixfix) list -> context -> string list * context
wenzelm@18672
   125
  val fix_frees: term -> context -> context
wenzelm@18672
   126
  val auto_fixes: context * (term list list * 'a) -> context * (term list list * 'a)
wenzelm@18672
   127
  val bind_fixes: string list -> context -> (term -> term) * context
wenzelm@20234
   128
  val add_assms: Assumption.export ->
wenzelm@19585
   129
    ((string * attribute list) * (string * string list) list) list ->
wenzelm@18672
   130
    context -> (bstring * thm list) list * context
wenzelm@20234
   131
  val add_assms_i: Assumption.export ->
wenzelm@19585
   132
    ((string * attribute list) * (term * term list) list) list ->
wenzelm@18672
   133
    context -> (bstring * thm list) list * context
wenzelm@18609
   134
  val add_cases: bool -> (string * RuleCases.T option) list -> context -> context
wenzelm@18609
   135
  val apply_case: RuleCases.T -> context -> (string * term list) list * context
wenzelm@16147
   136
  val get_case: context -> string -> string option list -> RuleCases.T
wenzelm@19371
   137
  val expand_abbrevs: bool -> context -> context
wenzelm@19663
   138
  val add_const_syntax: string * bool -> (string * mixfix) list -> context -> context
wenzelm@19681
   139
  val add_abbrevs: string * bool -> ((bstring * mixfix) * term) list -> context -> context
wenzelm@10810
   140
  val verbose: bool ref
wenzelm@10810
   141
  val setmp_verbose: ('a -> 'b) -> 'a -> 'b
wenzelm@12072
   142
  val print_syntax: context -> unit
wenzelm@10810
   143
  val print_binds: context -> unit
wenzelm@12057
   144
  val print_lthms: context -> unit
wenzelm@10810
   145
  val print_cases: context -> unit
wenzelm@10810
   146
  val prems_limit: int ref
wenzelm@18672
   147
  val pretty_ctxt: context -> Pretty.T list
wenzelm@10810
   148
  val pretty_context: context -> Pretty.T list
wenzelm@5819
   149
end;
wenzelm@5819
   150
wenzelm@16540
   151
structure ProofContext: PROOF_CONTEXT =
wenzelm@5819
   152
struct
wenzelm@5819
   153
wenzelm@16540
   154
type context = Context.proof;
wenzelm@5819
   155
wenzelm@16540
   156
val theory_of = Context.theory_of_proof;
wenzelm@19001
   157
val tsig_of = Sign.tsig_of o theory_of;
wenzelm@19001
   158
wenzelm@16540
   159
val init = Context.init_proof;
wenzelm@12057
   160
wenzelm@7270
   161
wenzelm@5819
   162
wenzelm@16540
   163
(** Isar proof context information **)
wenzelm@5819
   164
wenzelm@16540
   165
datatype ctxt =
wenzelm@16540
   166
  Ctxt of
wenzelm@19001
   167
   {naming: NameSpace.naming,                     (*local naming conventions*)
wenzelm@19001
   168
    syntax: LocalSyntax.T,                        (*local syntax*)
wenzelm@19033
   169
    consts: Consts.T * Consts.T,                  (*global/local consts*)
wenzelm@19001
   170
    thms: thm list NameSpace.table * FactIndex.T, (*local thms*)
wenzelm@19897
   171
    cases: (string * (RuleCases.T * bool)) list}; (*local contexts*)
wenzelm@5819
   172
wenzelm@20234
   173
fun make_ctxt (naming, syntax, consts, thms, cases) =
wenzelm@20234
   174
  Ctxt {naming = naming, syntax = syntax, consts = consts, thms = thms, cases = cases};
wenzelm@5819
   175
wenzelm@19079
   176
val local_naming = NameSpace.default_naming |> NameSpace.add_path "local";
wenzelm@19079
   177
wenzelm@16540
   178
structure ContextData = ProofDataFun
wenzelm@18672
   179
(
wenzelm@16540
   180
  val name = "Isar/context";
wenzelm@16540
   181
  type T = ctxt;
wenzelm@16540
   182
  fun init thy =
wenzelm@19079
   183
    make_ctxt (local_naming, LocalSyntax.init thy,
wenzelm@20234
   184
      (Sign.consts_of thy, Sign.consts_of thy),
wenzelm@19897
   185
      (NameSpace.empty_table, FactIndex.empty), []);
wenzelm@16540
   186
  fun print _ _ = ();
wenzelm@18672
   187
);
wenzelm@5819
   188
wenzelm@18708
   189
val _ = Context.add_setup ContextData.init;
wenzelm@5819
   190
wenzelm@16540
   191
fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args);
wenzelm@5819
   192
wenzelm@18672
   193
fun map_context f =
wenzelm@20234
   194
  ContextData.map (fn Ctxt {naming, syntax, consts, thms, cases} =>
wenzelm@20234
   195
    make_ctxt (f (naming, syntax, consts, thms, cases)));
wenzelm@18672
   196
wenzelm@19001
   197
fun map_naming f =
wenzelm@20234
   198
  map_context (fn (naming, syntax, consts, thms, cases) =>
wenzelm@20234
   199
    (f naming, syntax, consts, thms, cases));
wenzelm@18672
   200
wenzelm@19001
   201
fun map_syntax f =
wenzelm@20234
   202
  map_context (fn (naming, syntax, consts, thms, cases) =>
wenzelm@20234
   203
    (naming, f syntax, consts, thms, cases));
wenzelm@18672
   204
wenzelm@19001
   205
fun map_consts f =
wenzelm@20234
   206
  map_context (fn (naming, syntax, consts, thms, cases) =>
wenzelm@20234
   207
    (naming, syntax, f consts, thms, cases));
wenzelm@18672
   208
wenzelm@19001
   209
fun map_thms f =
wenzelm@20234
   210
  map_context (fn (naming, syntax, consts, thms, cases) =>
wenzelm@20234
   211
    (naming, syntax, consts, f thms, cases));
wenzelm@18672
   212
wenzelm@19001
   213
fun map_cases f =
wenzelm@20234
   214
  map_context (fn (naming, syntax, consts, thms, cases) =>
wenzelm@20234
   215
    (naming, syntax, consts, thms, f cases));
wenzelm@19001
   216
wenzelm@19001
   217
val naming_of = #naming o rep_context;
wenzelm@19387
   218
val full_name = NameSpace.full o naming_of;
wenzelm@5819
   219
wenzelm@16540
   220
val syntax_of = #syntax o rep_context;
wenzelm@19001
   221
val syn_of = LocalSyntax.syn_of o syntax_of;
wenzelm@19543
   222
val set_syntax_mode = map_syntax o LocalSyntax.set_mode;
wenzelm@19543
   223
val restore_syntax_mode = map_syntax o LocalSyntax.restore_mode o syntax_of;
wenzelm@19001
   224
wenzelm@19033
   225
val consts_of = #2 o #consts o rep_context;
wenzelm@5819
   226
wenzelm@16540
   227
val thms_of = #thms o rep_context;
wenzelm@19001
   228
val fact_index_of = #2 o thms_of;
wenzelm@16540
   229
wenzelm@16540
   230
val cases_of = #cases o rep_context;
wenzelm@5819
   231
wenzelm@5819
   232
wenzelm@19001
   233
(* transfer *)
wenzelm@12093
   234
wenzelm@19001
   235
fun transfer_syntax thy =
wenzelm@19001
   236
  map_syntax (LocalSyntax.rebuild thy) #>
wenzelm@19033
   237
  map_consts (fn consts as (global_consts, local_consts) =>
wenzelm@19033
   238
    let val thy_consts = Sign.consts_of thy in
wenzelm@19033
   239
      if Consts.eq_consts (thy_consts, global_consts) then consts
wenzelm@19033
   240
      else (thy_consts, Consts.merge (thy_consts, local_consts))
wenzelm@19033
   241
    end);
wenzelm@17072
   242
wenzelm@19001
   243
fun transfer thy = Context.transfer_proof thy #> transfer_syntax thy;
wenzelm@17072
   244
wenzelm@19019
   245
fun map_theory f ctxt = transfer (f (theory_of ctxt)) ctxt;
wenzelm@19019
   246
wenzelm@12093
   247
wenzelm@12093
   248
wenzelm@14828
   249
(** pretty printing **)
wenzelm@14828
   250
wenzelm@19310
   251
local
wenzelm@19310
   252
wenzelm@19371
   253
fun rewrite_term thy rews t =
wenzelm@19371
   254
  if can Term.type_of t then Pattern.rewrite_term thy rews [] t
wenzelm@19371
   255
  else (warning "Printing ill-typed term -- cannot expand abbreviations"; t);
wenzelm@19371
   256
wenzelm@19001
   257
fun pretty_term' abbrevs ctxt t =
wenzelm@18971
   258
  let
wenzelm@19001
   259
    val thy = theory_of ctxt;
wenzelm@19001
   260
    val syntax = syntax_of ctxt;
wenzelm@18971
   261
    val consts = consts_of ctxt;
wenzelm@19001
   262
    val t' = t
wenzelm@19371
   263
      |> K abbrevs ? rewrite_term thy (Consts.abbrevs_of consts (! print_mode @ [""]))
wenzelm@19371
   264
      |> Sign.extern_term (Consts.extern_early consts) thy
wenzelm@19371
   265
      |> LocalSyntax.extern_term syntax;
wenzelm@19371
   266
  in
wenzelm@19371
   267
    Sign.pretty_term' (Context.Proof ctxt) (LocalSyntax.syn_of syntax) (Consts.extern consts) t'
wenzelm@19371
   268
  end;
wenzelm@18971
   269
wenzelm@19310
   270
in
wenzelm@19310
   271
wenzelm@19001
   272
val pretty_term = pretty_term' true;
wenzelm@19310
   273
val pretty_term_no_abbrevs = pretty_term' false;
wenzelm@19310
   274
wenzelm@19310
   275
end;
wenzelm@19310
   276
wenzelm@16458
   277
fun pretty_typ ctxt T = Sign.pretty_typ (theory_of ctxt) T;
wenzelm@16458
   278
fun pretty_sort ctxt S = Sign.pretty_sort (theory_of ctxt) S;
wenzelm@16458
   279
fun pretty_classrel ctxt cs = Sign.pretty_classrel (theory_of ctxt) cs;
wenzelm@16458
   280
fun pretty_arity ctxt ar = Sign.pretty_arity (theory_of ctxt) ar;
wenzelm@14828
   281
wenzelm@14974
   282
fun pp ctxt = Pretty.pp (pretty_term ctxt, pretty_typ ctxt, pretty_sort ctxt,
wenzelm@14974
   283
  pretty_classrel ctxt, pretty_arity ctxt);
wenzelm@14828
   284
wenzelm@20234
   285
fun legacy_pretty_thm quote th =
wenzelm@20234
   286
  let
wenzelm@20234
   287
    val thy = Thm.theory_of_thm th;
wenzelm@20234
   288
    val pp =
wenzelm@20234
   289
      if Theory.eq_thy (thy, ProtoPure.thy) then Sign.pp thy
wenzelm@20234
   290
      else pp (init thy);
wenzelm@20234
   291
  in Display.pretty_thm_aux pp quote false [] th end;
wenzelm@20234
   292
wenzelm@17451
   293
fun pretty_thm ctxt th =
wenzelm@20234
   294
  Display.pretty_thm_aux (pp ctxt) false true (Assumption.assms_of ctxt) th;
wenzelm@14828
   295
wenzelm@14828
   296
fun pretty_thms ctxt [th] = pretty_thm ctxt th
wenzelm@14828
   297
  | pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths));
wenzelm@14828
   298
wenzelm@14828
   299
fun pretty_fact ctxt ("", ths) = pretty_thms ctxt ths
wenzelm@14828
   300
  | pretty_fact ctxt (a, [th]) =
wenzelm@14828
   301
      Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, pretty_thm ctxt th]
wenzelm@14828
   302
  | pretty_fact ctxt (a, ths) =
wenzelm@14828
   303
      Pretty.block (Pretty.fbreaks (Pretty.str (a ^ ":") :: map (pretty_thm ctxt) ths));
wenzelm@14828
   304
wenzelm@17072
   305
fun pretty_proof ctxt prf =
wenzelm@19310
   306
  pretty_term_no_abbrevs (ctxt |> transfer_syntax (ProofSyntax.proof_syntax prf (theory_of ctxt)))
wenzelm@17072
   307
    (ProofSyntax.term_of_proof prf);
wenzelm@17072
   308
wenzelm@17072
   309
fun pretty_proof_of ctxt full th =
wenzelm@17072
   310
  pretty_proof ctxt (ProofSyntax.proof_of full th);
wenzelm@17072
   311
wenzelm@17860
   312
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@17860
   313
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@17860
   314
val string_of_thm = Pretty.string_of oo pretty_thm;
wenzelm@17860
   315
wenzelm@14828
   316
wenzelm@14828
   317
wenzelm@5819
   318
(** prepare types **)
wenzelm@5819
   319
wenzelm@9504
   320
local
wenzelm@9504
   321
wenzelm@9504
   322
fun read_typ_aux read ctxt s =
wenzelm@19897
   323
  read (syn_of ctxt) (Context.Proof ctxt) (Variable.def_sort ctxt) s;
wenzelm@5819
   324
wenzelm@10554
   325
fun cert_typ_aux cert ctxt raw_T =
wenzelm@18678
   326
  cert (theory_of ctxt) raw_T handle TYPE (msg, _, _) => error msg;
wenzelm@9504
   327
wenzelm@9504
   328
in
wenzelm@9504
   329
wenzelm@16348
   330
val read_typ        = read_typ_aux Sign.read_typ';
wenzelm@16348
   331
val read_typ_syntax = read_typ_aux Sign.read_typ_syntax';
wenzelm@16348
   332
val read_typ_abbrev = read_typ_aux Sign.read_typ_abbrev';
wenzelm@16348
   333
val cert_typ        = cert_typ_aux Sign.certify_typ;
wenzelm@16348
   334
val cert_typ_syntax = cert_typ_aux Sign.certify_typ_syntax;
wenzelm@16348
   335
val cert_typ_abbrev = cert_typ_aux Sign.certify_typ_abbrev;
wenzelm@9504
   336
wenzelm@9504
   337
end;
wenzelm@9504
   338
wenzelm@5819
   339
wenzelm@7679
   340
(* internalize Skolem constants *)
wenzelm@7679
   341
wenzelm@19897
   342
val lookup_skolem = AList.lookup (op =) o Variable.fixes_of;
wenzelm@18187
   343
fun get_skolem ctxt x = the_default x (lookup_skolem ctxt x);
wenzelm@7679
   344
wenzelm@18678
   345
fun no_skolem internal x =
wenzelm@20086
   346
  if can Name.dest_skolem x then
wenzelm@18678
   347
    error ("Illegal reference to internal Skolem constant: " ^ quote x)
wenzelm@20086
   348
  else if not internal andalso can Name.dest_internal x then
wenzelm@18678
   349
    error ("Illegal reference to internal variable: " ^ quote x)
wenzelm@7679
   350
  else x;
wenzelm@7679
   351
wenzelm@14720
   352
fun intern_skolem ctxt internal =
wenzelm@7679
   353
  let
wenzelm@7679
   354
    fun intern (t as Free (x, T)) =
wenzelm@14720
   355
          if internal x then t
wenzelm@14720
   356
          else
wenzelm@18678
   357
            (case lookup_skolem ctxt (no_skolem false x) of
skalberg@15531
   358
              SOME x' => Free (x', T)
skalberg@15531
   359
            | NONE => t)
wenzelm@7679
   360
      | intern (t $ u) = intern t $ intern u
wenzelm@7679
   361
      | intern (Abs (x, T, t)) = Abs (x, T, intern t)
wenzelm@7679
   362
      | intern a = a;
wenzelm@7679
   363
  in intern end;
wenzelm@7679
   364
wenzelm@7679
   365
wenzelm@18187
   366
(* externalize Skolem constants -- approximation only! *)
wenzelm@18187
   367
wenzelm@18255
   368
fun rev_skolem ctxt =
wenzelm@19897
   369
  let val rev_fixes = map Library.swap (Variable.fixes_of ctxt)
wenzelm@18187
   370
  in AList.lookup (op =) rev_fixes end;
wenzelm@9133
   371
wenzelm@18255
   372
fun revert_skolem ctxt x =
wenzelm@18255
   373
  (case rev_skolem ctxt x of
wenzelm@18255
   374
    SOME x' => x'
wenzelm@20086
   375
  | NONE => perhaps (try Name.dest_skolem) x);
wenzelm@18255
   376
wenzelm@9133
   377
fun extern_skolem ctxt =
wenzelm@9133
   378
  let
wenzelm@18255
   379
    val revert = rev_skolem ctxt;
wenzelm@9133
   380
    fun extern (t as Free (x, T)) =
wenzelm@18187
   381
        (case revert x of
wenzelm@18187
   382
          SOME x' => Free (if lookup_skolem ctxt x' = SOME x then x' else NameSpace.hidden x', T)
wenzelm@18187
   383
        | NONE => t)
wenzelm@9133
   384
      | extern (t $ u) = extern t $ extern u
wenzelm@9133
   385
      | extern (Abs (x, T, t)) = Abs (x, T, extern t)
wenzelm@9133
   386
      | extern a = a;
wenzelm@9133
   387
  in extern end
wenzelm@9133
   388
wenzelm@8096
   389
wenzelm@18187
   390
wenzelm@5819
   391
(** prepare terms and propositions **)
wenzelm@5819
   392
wenzelm@5819
   393
(*
wenzelm@16501
   394
  (1) read / certify wrt. theory of context
wenzelm@5819
   395
  (2) intern Skolem constants
wenzelm@5819
   396
  (3) expand term bindings
wenzelm@5819
   397
*)
wenzelm@5819
   398
wenzelm@5819
   399
wenzelm@16501
   400
(* read wrt. theory *)     (*exception ERROR*)
wenzelm@5819
   401
wenzelm@18857
   402
fun read_def_termTs freeze pp syn ctxt (types, sorts, used) sTs =
wenzelm@18971
   403
  Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn (consts_of ctxt)
wenzelm@18857
   404
    (Context.Proof ctxt) (types, sorts) used freeze sTs;
wenzelm@5874
   405
wenzelm@18857
   406
fun read_def_termT freeze pp syn ctxt defaults sT =
wenzelm@18857
   407
  apfst hd (read_def_termTs freeze pp syn ctxt defaults [sT]);
wenzelm@14828
   408
wenzelm@18672
   409
fun read_term_thy freeze pp syn thy defaults s =
wenzelm@18672
   410
  #1 (read_def_termT freeze pp syn thy defaults (s, TypeInfer.logicT));
wenzelm@5874
   411
wenzelm@18672
   412
fun read_prop_thy freeze pp syn thy defaults s =
wenzelm@18672
   413
  #1 (read_def_termT freeze pp syn thy defaults (s, propT));
wenzelm@12072
   414
wenzelm@18672
   415
fun read_terms_thy freeze pp syn thy defaults =
wenzelm@18672
   416
  #1 o read_def_termTs freeze pp syn thy defaults o map (rpair TypeInfer.logicT);
wenzelm@12072
   417
wenzelm@18672
   418
fun read_props_thy freeze pp syn thy defaults =
wenzelm@18672
   419
  #1 o read_def_termTs freeze pp syn thy defaults o map (rpair propT);
wenzelm@5819
   420
wenzelm@5819
   421
wenzelm@19001
   422
(* local abbreviations *)
wenzelm@5819
   423
wenzelm@19371
   424
fun certify_consts ctxt =
wenzelm@19001
   425
  Consts.certify (pp ctxt) (tsig_of ctxt) (consts_of ctxt);
wenzelm@19001
   426
wenzelm@19897
   427
fun reject_schematic (Var (xi, _)) =
wenzelm@19897
   428
      error ("Unbound schematic variable: " ^ Syntax.string_of_vname xi)
wenzelm@19897
   429
  | reject_schematic (Abs (_, _, t)) = reject_schematic t
wenzelm@19897
   430
  | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
wenzelm@19897
   431
  | reject_schematic _ = ();
wenzelm@5819
   432
wenzelm@19897
   433
fun expand_binds ctxt schematic =
wenzelm@19897
   434
  Variable.expand_binds ctxt #> (if schematic then I else tap reject_schematic);
wenzelm@5819
   435
wenzelm@5819
   436
wenzelm@6550
   437
(* dummy patterns *)
wenzelm@6550
   438
wenzelm@18310
   439
val prepare_dummies =
wenzelm@18310
   440
  let val next = ref 1 in
wenzelm@18310
   441
    fn t =>
wenzelm@18310
   442
      let val (i, u) = Term.replace_dummy_patterns (! next, t)
wenzelm@18310
   443
      in next := i; u end
wenzelm@18310
   444
  end;
wenzelm@6762
   445
wenzelm@18678
   446
fun reject_dummies t = Term.no_dummy_patterns t
wenzelm@18678
   447
  handle TERM _ => error "Illegal dummy pattern(s) in term";
wenzelm@6550
   448
wenzelm@6550
   449
wenzelm@5819
   450
(* read terms *)
wenzelm@5819
   451
wenzelm@10554
   452
local
wenzelm@10554
   453
skalberg@15531
   454
fun append_env e1 e2 x = (case e2 x of NONE => e1 x | some => some);
wenzelm@14720
   455
wenzelm@16031
   456
fun gen_read' read app pattern schematic
wenzelm@14720
   457
    ctxt internal more_types more_sorts more_used s =
ballarin@14174
   458
  let
wenzelm@19897
   459
    val types = append_env (Variable.def_type ctxt pattern) more_types;
wenzelm@19897
   460
    val sorts = append_env (Variable.def_sort ctxt) more_sorts;
wenzelm@20163
   461
    val used = fold Name.declare more_used (Variable.names_of ctxt);
ballarin@14174
   462
  in
wenzelm@18857
   463
    (read (pp ctxt) (syn_of ctxt) ctxt (types, sorts, used) s
wenzelm@18678
   464
      handle TERM (msg, _) => error msg)
wenzelm@14720
   465
    |> app (intern_skolem ctxt internal)
wenzelm@19371
   466
    |> app (certify_consts ctxt)
wenzelm@19001
   467
    |> app (if pattern then I else expand_binds ctxt schematic)
wenzelm@18678
   468
    |> app (if pattern then prepare_dummies else reject_dummies)
wenzelm@14720
   469
  end;
wenzelm@14720
   470
wenzelm@16031
   471
fun gen_read read app pattern schematic ctxt =
wenzelm@16031
   472
  gen_read' read app pattern schematic ctxt (K false) (K NONE) (K NONE) [];
wenzelm@14720
   473
wenzelm@10554
   474
in
wenzelm@10554
   475
wenzelm@16031
   476
val read_termTs           = gen_read' (read_def_termTs false) (apfst o map) false false;
wenzelm@16031
   477
val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false true;
wenzelm@8096
   478
wenzelm@14720
   479
fun read_term_pats T ctxt =
wenzelm@16031
   480
  #1 o gen_read (read_def_termTs false) (apfst o map) true false ctxt o map (rpair T);
wenzelm@8096
   481
val read_prop_pats = read_term_pats propT;
wenzelm@8096
   482
wenzelm@18672
   483
fun read_term_legacy ctxt =
wenzelm@16458
   484
  gen_read' (read_term_thy true) I false false ctxt (K true) (K NONE) (K NONE) [];
wenzelm@14720
   485
wenzelm@19143
   486
val read_term            = gen_read (read_term_thy true) I false false;
wenzelm@19143
   487
val read_prop            = gen_read (read_prop_thy true) I false false;
wenzelm@19143
   488
val read_prop_schematic  = gen_read (read_prop_thy true) I false true;
wenzelm@19143
   489
val read_terms           = gen_read (read_terms_thy true) map false false;
wenzelm@19143
   490
fun read_props schematic = gen_read (read_props_thy true) map false schematic;
wenzelm@5819
   491
wenzelm@10554
   492
end;
wenzelm@10554
   493
wenzelm@5819
   494
wenzelm@5819
   495
(* certify terms *)
wenzelm@5819
   496
wenzelm@10554
   497
local
wenzelm@10554
   498
wenzelm@18971
   499
fun gen_cert prop pattern schematic ctxt t = t
wenzelm@19371
   500
  |> certify_consts ctxt
wenzelm@19001
   501
  |> (if pattern then I else expand_binds ctxt schematic)
wenzelm@19371
   502
  |> (fn t' => #1 (Sign.certify' false prop (pp ctxt) (consts_of ctxt) (theory_of ctxt) t')
wenzelm@18678
   503
    handle TYPE (msg, _, _) => error msg
wenzelm@18678
   504
      | TERM (msg, _) => error msg);
wenzelm@16501
   505
wenzelm@10554
   506
in
wenzelm@8096
   507
wenzelm@18971
   508
val cert_term = gen_cert false false false;
wenzelm@18971
   509
val cert_prop = gen_cert true false false;
wenzelm@18971
   510
val cert_props = map oo gen_cert true false;
wenzelm@10554
   511
wenzelm@18971
   512
fun cert_term_pats _ = map o gen_cert false true false;
wenzelm@18971
   513
val cert_prop_pats = map o gen_cert true true false;
wenzelm@10554
   514
wenzelm@10554
   515
end;
wenzelm@5819
   516
wenzelm@5819
   517
wenzelm@18770
   518
(* inferred types of parameters *)
wenzelm@18770
   519
wenzelm@18770
   520
fun infer_type ctxt x =
wenzelm@18770
   521
  (case try (fn () =>
wenzelm@19897
   522
      Sign.infer_types (pp ctxt) (theory_of ctxt) (consts_of ctxt) (Variable.def_type ctxt false)
wenzelm@20163
   523
        (Variable.def_sort ctxt) (Variable.names_of ctxt) true
wenzelm@19897
   524
        ([Free (x, dummyT)], TypeInfer.logicT)) () of
wenzelm@18770
   525
    SOME (Free (_, T), _) => T
wenzelm@18770
   526
  | _ => error ("Failed to infer type of fixed variable " ^ quote x));
wenzelm@18770
   527
wenzelm@18770
   528
fun inferred_param x ctxt =
wenzelm@18619
   529
  let val T = infer_type ctxt x
wenzelm@20163
   530
  in ((x, T), ctxt |> Variable.declare_term (Free (x, T))) end;
wenzelm@18619
   531
wenzelm@18770
   532
fun inferred_fixes ctxt =
wenzelm@20086
   533
  fold_map inferred_param (rev (map #2 (Variable.fixes_of ctxt))) ctxt;
wenzelm@5819
   534
wenzelm@5819
   535
wenzelm@15703
   536
(* type and constant names *)
wenzelm@15703
   537
wenzelm@15703
   538
fun read_tyname ctxt c =
wenzelm@20163
   539
  if Syntax.is_tid c then
wenzelm@19897
   540
    TFree (c, the_default (Sign.defaultS (theory_of ctxt)) (Variable.def_sort ctxt (c, ~1)))
wenzelm@16458
   541
  else Sign.read_tyname (theory_of ctxt) c;
wenzelm@15703
   542
wenzelm@15703
   543
fun read_const ctxt c =
wenzelm@15703
   544
  (case lookup_skolem ctxt c of
wenzelm@15703
   545
    SOME c' => Free (c', dummyT)
wenzelm@18971
   546
  | NONE => Consts.read_const (consts_of ctxt) c);
wenzelm@15703
   547
wenzelm@15703
   548
wenzelm@9553
   549
wenzelm@9553
   550
(** export theorems **)
wenzelm@8616
   551
wenzelm@18042
   552
fun common_exports is_goal inner outer =
wenzelm@20234
   553
  Assumption.exports is_goal inner outer
wenzelm@20234
   554
  #> Seq.map (Variable.export inner outer);
wenzelm@8616
   555
wenzelm@19847
   556
val goal_exports = common_exports true;
wenzelm@19847
   557
val exports = common_exports false;
wenzelm@19847
   558
wenzelm@19916
   559
fun export inner outer ths =
wenzelm@19916
   560
  (case Seq.pull (common_exports false inner outer ths) of
wenzelm@19916
   561
    SOME (ths', _) => ths'
wenzelm@19916
   562
  | NONE => raise THM ("Failed to export theorems", 0, ths));
wenzelm@12704
   563
wenzelm@19847
   564
fun export_standard inner outer =
wenzelm@19916
   565
  export inner outer #> map Drule.local_standard;
wenzelm@7925
   566
wenzelm@5819
   567
wenzelm@15758
   568
wenzelm@5819
   569
(** bindings **)
wenzelm@5819
   570
wenzelm@8096
   571
(* simult_matches *)
wenzelm@8096
   572
wenzelm@19867
   573
fun simult_matches ctxt (t, pats) =
wenzelm@19867
   574
  (case Seq.pull (Unify.matchers (theory_of ctxt) (map (rpair t) pats)) of
wenzelm@19867
   575
    NONE => error "Pattern match failed!"
wenzelm@19867
   576
  | SOME (env, _) => map (apsnd snd) (Envir.alist_of env));
wenzelm@8096
   577
wenzelm@8096
   578
wenzelm@8096
   579
(* add_binds(_i) *)
wenzelm@5819
   580
wenzelm@7925
   581
local
wenzelm@7925
   582
wenzelm@16031
   583
fun gen_bind prep (xi as (x, _), raw_t) ctxt =
wenzelm@19897
   584
  ctxt |> Variable.add_binds [(xi, Option.map (prep ctxt) raw_t)];
wenzelm@5819
   585
wenzelm@10810
   586
in
wenzelm@10810
   587
skalberg@15531
   588
fun drop_schematic (b as (xi, SOME t)) = if null (Term.term_vars t) then b else (xi, NONE)
wenzelm@10554
   589
  | drop_schematic b = b;
wenzelm@10554
   590
wenzelm@16031
   591
val add_binds = fold (gen_bind read_term);
wenzelm@16031
   592
val add_binds_i = fold (gen_bind cert_term);
wenzelm@8616
   593
wenzelm@16458
   594
fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts));
wenzelm@12147
   595
val auto_bind_goal = auto_bind AutoBind.goal;
wenzelm@12147
   596
val auto_bind_facts = auto_bind AutoBind.facts;
wenzelm@7925
   597
wenzelm@7925
   598
end;
wenzelm@5819
   599
wenzelm@5819
   600
wenzelm@8096
   601
(* match_bind(_i) *)
wenzelm@5819
   602
wenzelm@8096
   603
local
wenzelm@8096
   604
wenzelm@17860
   605
fun prep_bind prep_pats (raw_pats, t) ctxt =
wenzelm@5819
   606
  let
wenzelm@19897
   607
    val ctxt' = Variable.declare_term t ctxt;
wenzelm@19585
   608
    val pats = prep_pats (Term.fastype_of t) ctxt' raw_pats;
wenzelm@19585
   609
    val binds = simult_matches ctxt' (t, pats);
wenzelm@17860
   610
  in (binds, ctxt') end;
wenzelm@7670
   611
wenzelm@10465
   612
fun gen_binds prep_terms prep_pats gen raw_binds ctxt =
wenzelm@8616
   613
  let
wenzelm@10465
   614
    val ts = prep_terms ctxt (map snd raw_binds);
wenzelm@17860
   615
    val (binds, ctxt') =
wenzelm@19482
   616
      apfst flat (fold_map (prep_bind prep_pats) (map fst raw_binds ~~ ts) ctxt);
wenzelm@8616
   617
    val binds' =
wenzelm@19916
   618
      if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
wenzelm@8616
   619
      else binds;
skalberg@15531
   620
    val binds'' = map (apsnd SOME) binds';
wenzelm@18310
   621
    val ctxt'' =
wenzelm@19897
   622
      tap (Variable.warn_extra_tfrees ctxt)
wenzelm@18310
   623
       (if gen then
wenzelm@19897
   624
          ctxt (*sic!*) |> fold Variable.declare_term (map #2 binds') |> add_binds_i binds''
wenzelm@18310
   625
        else ctxt' |> add_binds_i binds'');
wenzelm@18310
   626
  in (ts, ctxt'') end;
wenzelm@8096
   627
wenzelm@8096
   628
in
wenzelm@5935
   629
wenzelm@10465
   630
val match_bind = gen_binds read_terms read_term_pats;
wenzelm@10465
   631
val match_bind_i = gen_binds (map o cert_term) cert_term_pats;
wenzelm@8096
   632
wenzelm@8096
   633
end;
wenzelm@5935
   634
wenzelm@5935
   635
wenzelm@10465
   636
(* propositions with patterns *)
wenzelm@5935
   637
wenzelm@10465
   638
local
wenzelm@8096
   639
wenzelm@10554
   640
fun prep_propp schematic prep_props prep_pats (context, args) =
wenzelm@10465
   641
  let
wenzelm@19585
   642
    fun prep (_, raw_pats) (ctxt, prop :: props) =
wenzelm@19897
   643
          let val ctxt' = Variable.declare_term prop ctxt
wenzelm@19585
   644
          in ((prop, prep_pats ctxt' raw_pats), (ctxt', props)) end
wenzelm@17860
   645
      | prep _ _ = sys_error "prep_propp";
wenzelm@17860
   646
    val (propp, (context', _)) = (fold_map o fold_map) prep args
wenzelm@19482
   647
      (context, prep_props schematic context (maps (map fst) args));
wenzelm@10465
   648
  in (context', propp) end;
wenzelm@5935
   649
wenzelm@10465
   650
fun gen_bind_propp prepp (ctxt, raw_args) =
wenzelm@8096
   651
  let
wenzelm@10465
   652
    val (ctxt', args) = prepp (ctxt, raw_args);
wenzelm@19585
   653
    val binds = flat (flat (map (map (simult_matches ctxt')) args));
wenzelm@10465
   654
    val propss = map (map #1) args;
wenzelm@8616
   655
wenzelm@10554
   656
    (*generalize result: context evaluated now, binds added later*)
wenzelm@19916
   657
    val gen = Variable.exportT_terms ctxt' ctxt;
skalberg@15531
   658
    fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
skalberg@15531
   659
  in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
wenzelm@8096
   660
wenzelm@10465
   661
in
wenzelm@10465
   662
wenzelm@11925
   663
val read_propp = prep_propp false read_props read_prop_pats;
wenzelm@11925
   664
val cert_propp = prep_propp false cert_props cert_prop_pats;
wenzelm@10554
   665
val read_propp_schematic = prep_propp true read_props read_prop_pats;
wenzelm@10554
   666
val cert_propp_schematic = prep_propp true cert_props cert_prop_pats;
wenzelm@10554
   667
wenzelm@11925
   668
val bind_propp = gen_bind_propp read_propp;
wenzelm@11925
   669
val bind_propp_i = gen_bind_propp cert_propp;
wenzelm@11925
   670
val bind_propp_schematic = gen_bind_propp read_propp_schematic;
wenzelm@10554
   671
val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic;
wenzelm@6789
   672
wenzelm@10465
   673
end;
wenzelm@10465
   674
wenzelm@6789
   675
wenzelm@5819
   676
wenzelm@5819
   677
(** theorems **)
wenzelm@5819
   678
wenzelm@18042
   679
(* fact_tac *)
wenzelm@18042
   680
wenzelm@18122
   681
fun comp_incr_tac [] _ st = no_tac st
wenzelm@18122
   682
  | comp_incr_tac (th :: ths) i st =
wenzelm@18122
   683
      (Goal.compose_hhf_tac (Drule.incr_indexes st th) i APPEND comp_incr_tac ths i) st;
wenzelm@18042
   684
wenzelm@18122
   685
fun fact_tac facts = Tactic.norm_hhf_tac THEN' comp_incr_tac facts;
wenzelm@18122
   686
wenzelm@18122
   687
fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) =>
wenzelm@18042
   688
  let
wenzelm@19001
   689
    val index = fact_index_of ctxt;
wenzelm@18042
   690
    val facts = FactIndex.could_unify index (Term.strip_all_body goal);
wenzelm@18042
   691
  in fact_tac facts i end);
wenzelm@18042
   692
wenzelm@18042
   693
wenzelm@6091
   694
(* get_thm(s) *)
wenzelm@5819
   695
wenzelm@18042
   696
fun retrieve_thms _ pick ctxt (Fact s) =
wenzelm@16501
   697
      let
wenzelm@20049
   698
        val th = Goal.prove ctxt [] [] (read_prop ctxt s) (K (ALLGOALS (some_fact_tac ctxt)))
wenzelm@18678
   699
          handle ERROR msg => cat_error msg "Failed to retrieve literal fact.";
wenzelm@18042
   700
      in pick "" [th] end
wenzelm@18042
   701
  | retrieve_thms from_thy pick ctxt xthmref =
wenzelm@18042
   702
      let
wenzelm@18042
   703
        val thy = theory_of ctxt;
wenzelm@19001
   704
        val (space, tab) = #1 (thms_of ctxt);
wenzelm@16501
   705
        val thmref = PureThy.map_name_of_thmref (NameSpace.intern space) xthmref;
wenzelm@16501
   706
        val name = PureThy.name_of_thmref thmref;
wenzelm@16501
   707
      in
wenzelm@17412
   708
        (case Symtab.lookup tab name of
wenzelm@16540
   709
          SOME ths => map (Thm.transfer thy) (PureThy.select_thm thmref ths)
wenzelm@16540
   710
        | NONE => from_thy thy xthmref) |> pick name
wenzelm@18042
   711
      end;
wenzelm@5819
   712
wenzelm@9566
   713
val get_thm = retrieve_thms PureThy.get_thms PureThy.single_thm;
wenzelm@9566
   714
val get_thm_closure = retrieve_thms PureThy.get_thms_closure PureThy.single_thm;
wenzelm@9566
   715
val get_thms = retrieve_thms PureThy.get_thms (K I);
wenzelm@9566
   716
val get_thms_closure = retrieve_thms PureThy.get_thms_closure (K I);
wenzelm@5819
   717
wenzelm@5819
   718
wenzelm@16031
   719
(* valid_thms *)
wenzelm@16031
   720
wenzelm@16031
   721
fun valid_thms ctxt (name, ths) =
wenzelm@18678
   722
  (case try (fn () => get_thms ctxt (Name name)) () of
wenzelm@16031
   723
    NONE => false
wenzelm@16147
   724
  | SOME ths' => Thm.eq_thms (ths, ths'));
wenzelm@16031
   725
wenzelm@16031
   726
wenzelm@16031
   727
(* lthms_containing *)
wenzelm@16031
   728
wenzelm@16031
   729
fun lthms_containing ctxt spec =
wenzelm@16031
   730
  FactIndex.find (fact_index_of ctxt) spec
wenzelm@18043
   731
  |> map ((not o valid_thms ctxt) ? apfst (fn name =>
wenzelm@18042
   732
    NameSpace.hidden (if name = "" then "unnamed" else name)));
wenzelm@16031
   733
wenzelm@16031
   734
wenzelm@13425
   735
(* name space operations *)
wenzelm@12309
   736
wenzelm@19062
   737
val no_base_names   = map_naming NameSpace.no_base_names;
wenzelm@16147
   738
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@19062
   739
val sticky_prefix   = map_naming o NameSpace.sticky_prefix;
wenzelm@19062
   740
val restore_naming  = map_naming o K o naming_of;
wenzelm@12309
   741
wenzelm@19001
   742
fun hide_thms fully names = map_thms (fn ((space, tab), index) =>
wenzelm@19001
   743
  ((fold (NameSpace.hide fully) names space, tab), index));
wenzelm@13425
   744
wenzelm@12309
   745
wenzelm@17360
   746
(* put_thms *)
wenzelm@5819
   747
wenzelm@19079
   748
fun put_thms _ ("", NONE) ctxt = ctxt
wenzelm@19143
   749
  | put_thms do_index ("", SOME ths) ctxt = ctxt |> map_thms (fn (facts, index) =>
wenzelm@18672
   750
      let
wenzelm@20163
   751
        val index' = FactIndex.add_local do_index ("", ths) index;
wenzelm@19001
   752
      in (facts, index') end)
wenzelm@19079
   753
  | put_thms _ (bname, NONE) ctxt = ctxt |> map_thms (fn ((space, tab), index) =>
wenzelm@18672
   754
      let
wenzelm@19387
   755
        val name = full_name ctxt bname;
wenzelm@18672
   756
        val tab' = Symtab.delete_safe name tab;
wenzelm@19001
   757
      in ((space, tab'), index) end)
wenzelm@19079
   758
  | put_thms do_index (bname, SOME ths) ctxt = ctxt |> map_thms (fn ((space, tab), index) =>
wenzelm@18672
   759
      let
wenzelm@19387
   760
        val name = full_name ctxt bname;
wenzelm@19001
   761
        val space' = NameSpace.declare (naming_of ctxt) name space;
wenzelm@18672
   762
        val tab' = Symtab.update (name, ths) tab;
wenzelm@20163
   763
        val index' = FactIndex.add_local do_index (name, ths) index;
wenzelm@19001
   764
      in ((space', tab'), index') end);
wenzelm@5819
   765
wenzelm@19079
   766
fun put_thms_internal thms ctxt =
wenzelm@19079
   767
  ctxt |> map_naming (K local_naming) |> put_thms false thms |> restore_naming ctxt;
wenzelm@19079
   768
wenzelm@7606
   769
wenzelm@14564
   770
(* note_thmss *)
wenzelm@5819
   771
wenzelm@12711
   772
local
wenzelm@16147
   773
wenzelm@17860
   774
fun gen_note_thmss get = fold_map (fn ((name, more_attrs), ths_attrs) => fn ctxt =>
wenzelm@5819
   775
  let
wenzelm@17360
   776
    fun app (th, attrs) (ct, ths) =
wenzelm@18728
   777
      let val (ct', th') = foldl_map (Thm.proof_attributes (attrs @ more_attrs)) (ct, get ctxt th)
wenzelm@12711
   778
      in (ct', th' :: ths) end;
wenzelm@17360
   779
    val (ctxt', rev_thms) = fold app ths_attrs (ctxt, []);
wenzelm@19482
   780
    val thms = flat (rev rev_thms);
wenzelm@19079
   781
  in ((name, thms), ctxt' |> put_thms true (name, SOME thms)) end);
wenzelm@12711
   782
wenzelm@12711
   783
in
wenzelm@12711
   784
wenzelm@16147
   785
val note_thmss = gen_note_thmss get_thms;
wenzelm@16147
   786
val note_thmss_i = gen_note_thmss (K I);
ballarin@15696
   787
ballarin@15696
   788
val note_thmss_accesses = gen_note_thmss get_thms;
ballarin@15696
   789
val note_thmss_accesses_i = gen_note_thmss (K I);
wenzelm@12711
   790
wenzelm@12711
   791
end;
wenzelm@9196
   792
wenzelm@5819
   793
wenzelm@5819
   794
wenzelm@18672
   795
(** parameters **)
wenzelm@17360
   796
wenzelm@8096
   797
(* variables *)
wenzelm@8096
   798
wenzelm@19897
   799
fun declare_var (x, opt_T, mx) ctxt =
wenzelm@19897
   800
  let val T = (case opt_T of SOME T => T | NONE => TypeInfer.mixfixT mx)
wenzelm@20163
   801
  in ((x, T, mx), ctxt |> Variable.declare_constraints (Free (x, T))) end;
wenzelm@19897
   802
wenzelm@10381
   803
local
wenzelm@10381
   804
wenzelm@18672
   805
fun prep_vars prep_typ internal legacy =
wenzelm@18672
   806
  fold_map (fn (raw_x, raw_T, raw_mx) => fn ctxt =>
wenzelm@18672
   807
    let
wenzelm@19371
   808
      val (x, mx) = Syntax.const_mixfix raw_x raw_mx;
wenzelm@18672
   809
      val _ =
wenzelm@18678
   810
        if not legacy andalso not (Syntax.is_identifier (no_skolem internal x)) then
wenzelm@18678
   811
          error ("Illegal variable name: " ^ quote x)
wenzelm@18672
   812
        else ();
wenzelm@12504
   813
wenzelm@18672
   814
      fun cond_tvars T =
wenzelm@18672
   815
        if internal then T
wenzelm@18678
   816
        else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
wenzelm@18672
   817
      val opt_T = Option.map (cond_tvars o prep_typ ctxt) raw_T;
wenzelm@18672
   818
      val var = (x, opt_T, mx);
wenzelm@19001
   819
    in (var, ctxt |> declare_var var |> #2) end);
wenzelm@8096
   820
wenzelm@10381
   821
in
wenzelm@10381
   822
wenzelm@18672
   823
val read_vars        = prep_vars read_typ false false;
wenzelm@18672
   824
val cert_vars        = prep_vars cert_typ true false;
wenzelm@18672
   825
val read_vars_legacy = prep_vars read_typ true true;
wenzelm@18672
   826
val cert_vars_legacy = prep_vars cert_typ true true;
wenzelm@8096
   827
wenzelm@10381
   828
end;
wenzelm@10381
   829
wenzelm@8096
   830
wenzelm@19681
   831
(* authentic constants *)
wenzelm@19663
   832
wenzelm@19663
   833
fun add_const_syntax prmode args ctxt =
wenzelm@19663
   834
  ctxt |> map_syntax
wenzelm@19663
   835
    (LocalSyntax.add_modesyntax (theory_of ctxt) prmode
wenzelm@19663
   836
      (map (pair false o Consts.syntax (consts_of ctxt)) args));
wenzelm@19663
   837
wenzelm@19681
   838
fun context_const_ast_tr context [Syntax.Variable c] =
wenzelm@19681
   839
      let
wenzelm@19681
   840
        val consts = Context.cases Sign.consts_of consts_of context;
wenzelm@19681
   841
        val c' = Consts.intern consts c;
wenzelm@19681
   842
        val _ = Consts.constraint consts c' handle TYPE (msg, _, _) => error msg;
wenzelm@19681
   843
      in Syntax.Constant (Syntax.constN ^ c') end
wenzelm@19681
   844
  | context_const_ast_tr _ asts = raise Syntax.AST ("const_ast_tr", asts);
wenzelm@19681
   845
wenzelm@19681
   846
val _ = Context.add_setup
wenzelm@19681
   847
 (Sign.add_syntax
wenzelm@19681
   848
   [("_context_const", "id => 'a", Delimfix "CONST _"),
wenzelm@19681
   849
    ("_context_const", "longid => 'a", Delimfix "CONST _")] #>
wenzelm@19681
   850
  Sign.add_advanced_trfuns ([("_context_const", context_const_ast_tr)], [], [], []));
wenzelm@19681
   851
wenzelm@19663
   852
wenzelm@19001
   853
(* abbreviations *)
wenzelm@19001
   854
wenzelm@19371
   855
val expand_abbrevs = map_consts o apsnd o Consts.expand_abbrevs;
wenzelm@19371
   856
wenzelm@19681
   857
fun add_abbrevs prmode = fold (fn ((raw_c, raw_mx), raw_t) => fn ctxt =>
wenzelm@19001
   858
  let
wenzelm@19663
   859
    val ([(c, _, mx)], _) = cert_vars [(raw_c, NONE, raw_mx)] ctxt;
wenzelm@19673
   860
    val c' = Syntax.constN ^ full_name ctxt c;
wenzelm@20008
   861
    val t0 = cert_term (ctxt |> expand_abbrevs false) raw_t;
wenzelm@20008
   862
    val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
wenzelm@19019
   863
    val T = Term.fastype_of t;
wenzelm@19001
   864
    val _ =
wenzelm@19897
   865
      if null (Variable.hidden_polymorphism t T) then ()
wenzelm@19001
   866
      else error ("Extra type variables on rhs of abbreviation: " ^ quote c);
wenzelm@19001
   867
  in
wenzelm@19001
   868
    ctxt
wenzelm@19897
   869
    |> Variable.declare_term t
wenzelm@19673
   870
    |> map_consts (apsnd
wenzelm@19681
   871
      (Consts.abbreviate (pp ctxt) (tsig_of ctxt) (naming_of ctxt) (#1 prmode) ((c, t), true)))
wenzelm@19663
   872
    |> map_syntax (LocalSyntax.add_modesyntax (theory_of ctxt) prmode [(false, (c', T, mx))])
wenzelm@19001
   873
  end);
wenzelm@19001
   874
wenzelm@19001
   875
wenzelm@18672
   876
(* fixes *)
wenzelm@5819
   877
wenzelm@8096
   878
local
wenzelm@8096
   879
wenzelm@19001
   880
fun prep_mixfix (x, T, mx) =
wenzelm@19019
   881
  if mx <> NoSyn andalso mx <> Structure andalso
wenzelm@20086
   882
      (can Name.dest_internal x orelse can Name.dest_skolem x) then
wenzelm@19001
   883
    error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x)
wenzelm@19001
   884
  else (true, (x, T, mx));
wenzelm@19001
   885
wenzelm@18844
   886
fun gen_fixes prep raw_vars ctxt =
wenzelm@8096
   887
  let
wenzelm@18672
   888
    val (vars, ctxt') = prep raw_vars ctxt;
wenzelm@19897
   889
    val (xs', ctxt'') = Variable.add_fixes (map #1 vars) ctxt';
wenzelm@8096
   890
  in
wenzelm@19897
   891
    ctxt''
wenzelm@19001
   892
    |> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars)
wenzelm@19897
   893
    |-> (map_syntax o LocalSyntax.add_syntax (theory_of ctxt) o map prep_mixfix)
wenzelm@18672
   894
    |> pair xs'
wenzelm@8096
   895
  end;
wenzelm@5819
   896
wenzelm@8096
   897
in
wenzelm@7679
   898
wenzelm@18844
   899
val add_fixes = gen_fixes read_vars;
wenzelm@18844
   900
val add_fixes_i = gen_fixes cert_vars;
wenzelm@18844
   901
val add_fixes_legacy = gen_fixes cert_vars_legacy;
wenzelm@8096
   902
wenzelm@8096
   903
end;
wenzelm@5819
   904
wenzelm@18672
   905
wenzelm@18672
   906
(* fixes vs. frees *)
wenzelm@12016
   907
wenzelm@18672
   908
fun fix_frees t ctxt =
wenzelm@18672
   909
  let
wenzelm@19897
   910
    fun add (Free (x, T)) =
wenzelm@19897
   911
          if Variable.is_fixed ctxt x then I else insert (op =) (x, SOME T, NoSyn)
wenzelm@18672
   912
      | add _ = I;
wenzelm@18672
   913
    val fixes = rev (fold_aterms add t []);
wenzelm@18809
   914
  in
wenzelm@18809
   915
    ctxt
wenzelm@19897
   916
    |> Variable.declare_term t
wenzelm@19897
   917
    |> Variable.set_body false
wenzelm@18809
   918
    |> (snd o add_fixes_i fixes)
wenzelm@19897
   919
    |> Variable.restore_body ctxt
wenzelm@18809
   920
  end;
wenzelm@6895
   921
wenzelm@18672
   922
fun auto_fixes (arg as (ctxt, (propss, x))) =
wenzelm@19897
   923
  if Variable.is_body ctxt then arg
wenzelm@18672
   924
  else ((fold o fold) fix_frees propss ctxt, (propss, x));
wenzelm@18672
   925
wenzelm@18672
   926
fun bind_fixes xs ctxt =
wenzelm@9291
   927
  let
wenzelm@18672
   928
    val (_, ctxt') = ctxt |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs);
wenzelm@9291
   929
    fun bind (t as Free (x, T)) =
wenzelm@18340
   930
          if member (op =) xs x then
skalberg@15531
   931
            (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
wenzelm@9291
   932
          else t
wenzelm@9291
   933
      | bind (t $ u) = bind t $ bind u
wenzelm@9291
   934
      | bind (Abs (x, T, t)) = Abs (x, T, bind t)
wenzelm@9291
   935
      | bind a = a;
wenzelm@18672
   936
  in (bind, ctxt') end;
wenzelm@18672
   937
wenzelm@9291
   938
wenzelm@9291
   939
wenzelm@18672
   940
(** assumptions **)
wenzelm@18187
   941
wenzelm@20209
   942
local
wenzelm@20209
   943
wenzelm@20209
   944
fun gen_assms prepp exp args ctxt =
wenzelm@20209
   945
  let
wenzelm@20234
   946
    val cert = Thm.cterm_of (theory_of ctxt);
wenzelm@20209
   947
    val (propss, ctxt1) = swap (prepp (ctxt, map snd args));
wenzelm@20234
   948
    val _ = Variable.warn_extra_tfrees ctxt ctxt1;
wenzelm@20234
   949
    val (premss, ctxt2) = fold_burrow (Assumption.add_assms exp o map cert) propss ctxt1;
wenzelm@20234
   950
  in
wenzelm@20234
   951
    ctxt2
wenzelm@20234
   952
    |> auto_bind_facts (flat propss)
wenzelm@20234
   953
    |> put_thms_internal (AutoBind.premsN, SOME (Assumption.prems_of ctxt2))
wenzelm@20234
   954
    |> note_thmss_i (map fst args ~~ map (map (fn th => ([th], []))) premss)
wenzelm@20234
   955
  end;
wenzelm@20209
   956
wenzelm@20209
   957
in
wenzelm@20209
   958
wenzelm@20209
   959
val add_assms = gen_assms (apsnd #1 o bind_propp);
wenzelm@20209
   960
val add_assms_i = gen_assms (apsnd #1 o bind_propp_i);
wenzelm@20209
   961
wenzelm@20209
   962
end;
wenzelm@20209
   963
wenzelm@20209
   964
wenzelm@5819
   965
wenzelm@8373
   966
(** cases **)
wenzelm@8373
   967
wenzelm@16147
   968
local
wenzelm@16147
   969
wenzelm@16668
   970
fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
wenzelm@16147
   971
wenzelm@18476
   972
fun add_case _ ("", _) cases = cases
wenzelm@18476
   973
  | add_case _ (name, NONE) cases = rem_case name cases
wenzelm@18476
   974
  | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
wenzelm@16147
   975
wenzelm@18678
   976
fun prep_case name fxs c =
wenzelm@18609
   977
  let
wenzelm@18609
   978
    fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
wenzelm@18609
   979
      | replace [] ys = ys
wenzelm@18678
   980
      | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
wenzelm@18609
   981
    val RuleCases.Case {fixes, assumes, binds, cases} = c;
wenzelm@18609
   982
    val fixes' = replace fxs fixes;
wenzelm@18609
   983
    val binds' = map drop_schematic binds;
wenzelm@18609
   984
  in
wenzelm@18609
   985
    if null (fold (Term.add_tvarsT o snd) fixes []) andalso
wenzelm@18609
   986
      null (fold (fold Term.add_vars o snd) assumes []) then
wenzelm@18609
   987
        RuleCases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
wenzelm@18678
   988
    else error ("Illegal schematic variable(s) in case " ^ quote name)
wenzelm@18609
   989
  end;
wenzelm@18609
   990
wenzelm@18672
   991
fun fix (x, T) ctxt =
wenzelm@18672
   992
  let
wenzelm@18672
   993
    val (bind, ctxt') = bind_fixes [x] ctxt;
wenzelm@18672
   994
    val t = bind (Free (x, T));
wenzelm@20163
   995
  in (t, ctxt' |> Variable.declare_constraints t) end;
wenzelm@18672
   996
wenzelm@16147
   997
in
wenzelm@16147
   998
wenzelm@18672
   999
fun add_cases is_proper = map_cases o fold (add_case is_proper);
wenzelm@18609
  1000
wenzelm@18609
  1001
fun case_result c ctxt =
wenzelm@18609
  1002
  let
wenzelm@18609
  1003
    val RuleCases.Case {fixes, ...} = c;
wenzelm@18672
  1004
    val (ts, ctxt') = ctxt |> fold_map fix fixes;
wenzelm@18672
  1005
    val RuleCases.Case {assumes, binds, cases, ...} = RuleCases.apply ts c;
wenzelm@18609
  1006
  in
wenzelm@18609
  1007
    ctxt'
wenzelm@18699
  1008
    |> add_binds_i (map drop_schematic binds)
wenzelm@18609
  1009
    |> add_cases true (map (apsnd SOME) cases)
wenzelm@18609
  1010
    |> pair (assumes, (binds, cases))
wenzelm@18609
  1011
  end;
wenzelm@18609
  1012
wenzelm@18609
  1013
val apply_case = apfst fst oo case_result;
wenzelm@18609
  1014
wenzelm@16540
  1015
fun get_case ctxt name xs =
wenzelm@17184
  1016
  (case AList.lookup (op =) (cases_of ctxt) name of
wenzelm@18678
  1017
    NONE => error ("Unknown case: " ^ quote name)
wenzelm@18678
  1018
  | SOME (c, _) => prep_case name xs c);
wenzelm@8373
  1019
wenzelm@16147
  1020
end;
wenzelm@8373
  1021
wenzelm@8373
  1022
wenzelm@8373
  1023
wenzelm@10810
  1024
(** print context information **)
wenzelm@10810
  1025
wenzelm@10810
  1026
val verbose = ref false;
wenzelm@10810
  1027
fun verb f x = if ! verbose then f (x ()) else [];
wenzelm@10810
  1028
wenzelm@10810
  1029
fun setmp_verbose f x = Library.setmp verbose true f x;
wenzelm@10810
  1030
wenzelm@10810
  1031
wenzelm@12072
  1032
(* local syntax *)
wenzelm@12072
  1033
wenzelm@12093
  1034
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@12072
  1035
wenzelm@12072
  1036
wenzelm@18971
  1037
(* local consts *)
wenzelm@18971
  1038
wenzelm@18971
  1039
fun pretty_consts ctxt =
wenzelm@18971
  1040
  let
wenzelm@19033
  1041
    val ((_, globals), (space, consts)) =
wenzelm@19033
  1042
      pairself (#constants o Consts.dest) (#consts (rep_context ctxt));
wenzelm@18971
  1043
    fun local_abbrev (_, (_, NONE)) = I
wenzelm@18971
  1044
      | local_abbrev (c, (T, SOME t)) =
wenzelm@18971
  1045
          if Symtab.defined globals c then I else cons (c, Logic.mk_equals (Const (c, T), t));
wenzelm@18971
  1046
    val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold local_abbrev consts []));
wenzelm@18971
  1047
  in
wenzelm@18971
  1048
    if null abbrevs andalso not (! verbose) then []
wenzelm@19310
  1049
    else [Pretty.big_list "abbreviations:" (map (pretty_term_no_abbrevs ctxt o #2) abbrevs)]
wenzelm@18971
  1050
  end;
wenzelm@18971
  1051
wenzelm@18971
  1052
wenzelm@10810
  1053
(* term bindings *)
wenzelm@10810
  1054
wenzelm@16540
  1055
fun pretty_binds ctxt =
wenzelm@10810
  1056
  let
wenzelm@19897
  1057
    val binds = Variable.binds_of ctxt;
wenzelm@19310
  1058
    fun prt_bind (xi, (T, t)) = pretty_term_no_abbrevs ctxt (Logic.mk_equals (Var (xi, T), t));
wenzelm@10810
  1059
  in
wenzelm@15758
  1060
    if Vartab.is_empty binds andalso not (! verbose) then []
wenzelm@15758
  1061
    else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
wenzelm@10810
  1062
  end;
wenzelm@10810
  1063
wenzelm@10810
  1064
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@10810
  1065
wenzelm@10810
  1066
wenzelm@10810
  1067
(* local theorems *)
wenzelm@10810
  1068
wenzelm@16540
  1069
fun pretty_lthms ctxt =
wenzelm@20012
  1070
  let
wenzelm@20012
  1071
    val props = FactIndex.props (fact_index_of ctxt);
wenzelm@20012
  1072
    val facts =
wenzelm@20012
  1073
      (if null props then I else cons ("unnamed", props))
wenzelm@20012
  1074
      (NameSpace.extern_table (#1 (thms_of ctxt)));
wenzelm@20012
  1075
  in
wenzelm@20012
  1076
    if null facts andalso not (! verbose) then []
wenzelm@20012
  1077
    else [Pretty.big_list "facts:" (map (pretty_fact ctxt) facts)]
wenzelm@20012
  1078
  end;
wenzelm@10810
  1079
wenzelm@12057
  1080
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
wenzelm@10810
  1081
wenzelm@10810
  1082
wenzelm@10810
  1083
(* local contexts *)
wenzelm@10810
  1084
wenzelm@16540
  1085
fun pretty_cases ctxt =
wenzelm@10810
  1086
  let
wenzelm@12057
  1087
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1088
wenzelm@10810
  1089
    fun prt_let (xi, t) = Pretty.block
wenzelm@10818
  1090
      [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
wenzelm@10810
  1091
        Pretty.quote (prt_term t)];
wenzelm@10810
  1092
wenzelm@13425
  1093
    fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
wenzelm@13425
  1094
      ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
wenzelm@13425
  1095
wenzelm@10810
  1096
    fun prt_sect _ _ _ [] = []
wenzelm@10810
  1097
      | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s ::
wenzelm@19482
  1098
            flat (Library.separate sep (map (Library.single o prt) xs))))];
wenzelm@10810
  1099
wenzelm@18609
  1100
    fun prt_case (name, (fixes, (asms, (lets, cs)))) = Pretty.block (Pretty.fbreaks
wenzelm@10810
  1101
      (Pretty.str (name ^ ":") ::
wenzelm@11915
  1102
        prt_sect "fix" [] (Pretty.str o fst) fixes @
wenzelm@10810
  1103
        prt_sect "let" [Pretty.str "and"] prt_let
wenzelm@19482
  1104
          (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
wenzelm@13425
  1105
        (if forall (null o #2) asms then []
wenzelm@18609
  1106
          else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
wenzelm@18609
  1107
        prt_sect "subcases:" [] (Pretty.str o fst) cs));
wenzelm@16540
  1108
wenzelm@18476
  1109
    fun add_case (_, (_, false)) = I
wenzelm@18609
  1110
      | add_case (name, (c as RuleCases.Case {fixes, ...}, true)) =
wenzelm@18609
  1111
          cons (name, (fixes, #1 (case_result c ctxt)));
wenzelm@18476
  1112
    val cases = fold add_case (cases_of ctxt) [];
wenzelm@10810
  1113
  in
wenzelm@10810
  1114
    if null cases andalso not (! verbose) then []
wenzelm@18476
  1115
    else [Pretty.big_list "cases:" (map prt_case cases)]
wenzelm@10810
  1116
  end;
wenzelm@10810
  1117
wenzelm@10810
  1118
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@10810
  1119
wenzelm@10810
  1120
wenzelm@12057
  1121
(* core context *)
wenzelm@10810
  1122
wenzelm@10810
  1123
val prems_limit = ref 10;
wenzelm@10810
  1124
wenzelm@18672
  1125
fun pretty_ctxt ctxt =
wenzelm@10810
  1126
  let
wenzelm@12057
  1127
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1128
wenzelm@12093
  1129
    (*structures*)
wenzelm@19001
  1130
    val structs = LocalSyntax.structs_of (syntax_of ctxt);
wenzelm@12093
  1131
    val prt_structs = if null structs then []
wenzelm@12093
  1132
      else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
wenzelm@12093
  1133
        Pretty.commas (map Pretty.str structs))];
wenzelm@12093
  1134
wenzelm@12057
  1135
    (*fixes*)
wenzelm@12057
  1136
    fun prt_fix (x, x') =
wenzelm@12057
  1137
      if x = x' then Pretty.str x
wenzelm@12057
  1138
      else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@18340
  1139
    val fixes =
wenzelm@20086
  1140
      rev (filter_out ((can Name.dest_internal orf member (op =) structs) o #1)
wenzelm@19897
  1141
        (Variable.fixes_of ctxt));
wenzelm@12093
  1142
    val prt_fixes = if null fixes then []
wenzelm@12093
  1143
      else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@12093
  1144
        Pretty.commas (map prt_fix fixes))];
wenzelm@12057
  1145
wenzelm@12057
  1146
    (*prems*)
wenzelm@10810
  1147
    val limit = ! prems_limit;
wenzelm@20234
  1148
    val prems = Assumption.prems_of ctxt;
wenzelm@10810
  1149
    val len = length prems;
wenzelm@12093
  1150
    val prt_prems = if null prems then []
wenzelm@12093
  1151
      else [Pretty.big_list "prems:" ((if len <= limit then [] else [Pretty.str "..."]) @
wenzelm@12093
  1152
        map (pretty_thm ctxt) (Library.drop (len - limit, prems)))];
wenzelm@12093
  1153
wenzelm@12093
  1154
  in prt_structs @ prt_fixes @ prt_prems end;
wenzelm@10810
  1155
wenzelm@10810
  1156
wenzelm@10810
  1157
(* main context *)
wenzelm@10810
  1158
wenzelm@16540
  1159
fun pretty_context ctxt =
wenzelm@10810
  1160
  let
wenzelm@12057
  1161
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1162
    val prt_typ = pretty_typ ctxt;
wenzelm@12057
  1163
    val prt_sort = pretty_sort ctxt;
wenzelm@10810
  1164
wenzelm@10810
  1165
    (*theory*)
wenzelm@12057
  1166
    val pretty_thy = Pretty.block
wenzelm@17384
  1167
      [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
wenzelm@10810
  1168
wenzelm@10810
  1169
    (*defaults*)
wenzelm@10810
  1170
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@10810
  1171
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@10810
  1172
wenzelm@10810
  1173
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@10810
  1174
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@10810
  1175
wenzelm@10810
  1176
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@10810
  1177
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@10810
  1178
wenzelm@10810
  1179
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@10810
  1180
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@16540
  1181
wenzelm@20163
  1182
    val (types, sorts) = Variable.constraints_of ctxt;
wenzelm@10810
  1183
  in
wenzelm@18609
  1184
    verb single (K pretty_thy) @
wenzelm@18672
  1185
    pretty_ctxt ctxt @
wenzelm@18971
  1186
    verb pretty_consts (K ctxt) @
wenzelm@10810
  1187
    verb pretty_binds (K ctxt) @
wenzelm@12057
  1188
    verb pretty_lthms (K ctxt) @
wenzelm@10810
  1189
    verb pretty_cases (K ctxt) @
wenzelm@18609
  1190
    verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@20163
  1191
    verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
wenzelm@10810
  1192
  end;
wenzelm@10810
  1193
wenzelm@5819
  1194
end;