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