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