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