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