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