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