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