src/Pure/Isar/proof_context.ML
author wenzelm
Mon Nov 14 16:52:19 2011 +0100 (2011-11-14 ago)
changeset 45488 6d71d9e52369
parent 45453 304437f43869
child 45650 d314a4e8038f
permissions -rw-r--r--
pass positions for named targets, for formal links in the document model;
wenzelm@5819
     1
(*  Title:      Pure/Isar/proof_context.ML
wenzelm@5819
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5819
     3
wenzelm@19001
     4
The key concept of Isar proof contexts: elevates primitive local
wenzelm@19001
     5
reasoning Gamma |- phi to a structured concept, with generic context
wenzelm@20234
     6
elements.  See also structure Variable and Assumption.
wenzelm@5819
     7
*)
wenzelm@5819
     8
wenzelm@5819
     9
signature PROOF_CONTEXT =
wenzelm@5819
    10
sig
wenzelm@20310
    11
  val theory_of: Proof.context -> theory
wenzelm@36610
    12
  val init_global: theory -> Proof.context
wenzelm@24388
    13
  type mode
wenzelm@24501
    14
  val mode_default: mode
wenzelm@24501
    15
  val mode_stmt: mode
wenzelm@24501
    16
  val mode_pattern: mode
wenzelm@24501
    17
  val mode_schematic: mode
wenzelm@24501
    18
  val mode_abbrev: mode
wenzelm@24388
    19
  val set_mode: mode -> Proof.context -> Proof.context
wenzelm@24388
    20
  val get_mode: Proof.context -> mode
wenzelm@24388
    21
  val restore_mode: Proof.context -> Proof.context -> Proof.context
wenzelm@27286
    22
  val abbrev_mode: Proof.context -> bool
wenzelm@21667
    23
  val set_stmt: bool -> Proof.context -> Proof.context
wenzelm@33165
    24
  val local_naming: Name_Space.naming
wenzelm@33165
    25
  val map_naming: (Name_Space.naming -> Name_Space.naming) -> Proof.context -> Proof.context
wenzelm@33095
    26
  val naming_of: Proof.context -> Name_Space.naming
wenzelm@33165
    27
  val restore_naming: Proof.context -> Proof.context -> Proof.context
haftmann@29581
    28
  val full_name: Proof.context -> binding -> string
wenzelm@42241
    29
  val syntax_of: Proof.context -> Local_Syntax.T
wenzelm@35111
    30
  val syn_of: Proof.context -> Syntax.syntax
wenzelm@35669
    31
  val tsig_of: Proof.context -> Type.tsig
wenzelm@36451
    32
  val set_defsort: sort -> Proof.context -> Proof.context
wenzelm@35680
    33
  val default_sort: Proof.context -> indexname -> sort
wenzelm@20310
    34
  val consts_of: Proof.context -> Consts.T
haftmann@24752
    35
  val the_const_constraint: Proof.context -> string -> typ
wenzelm@20784
    36
  val set_syntax_mode: Syntax.mode -> Proof.context -> Proof.context
wenzelm@20310
    37
  val restore_syntax_mode: Proof.context -> Proof.context -> Proof.context
wenzelm@26284
    38
  val facts_of: Proof.context -> Facts.T
blanchet@34982
    39
  val cases_of: Proof.context -> (string * (Rule_Cases.T * bool)) list
wenzelm@42359
    40
  val class_space: Proof.context -> Name_Space.T
wenzelm@42359
    41
  val type_space: Proof.context -> Name_Space.T
wenzelm@42359
    42
  val const_space: Proof.context -> Name_Space.T
wenzelm@42359
    43
  val intern_class: Proof.context -> xstring -> string
wenzelm@42359
    44
  val intern_type: Proof.context -> xstring -> string
wenzelm@42359
    45
  val intern_const: Proof.context -> xstring -> string
wenzelm@42359
    46
  val extern_class: Proof.context -> string -> xstring
wenzelm@42359
    47
  val extern_type: Proof.context -> string -> xstring
wenzelm@42359
    48
  val extern_const: Proof.context -> string -> xstring
wenzelm@27259
    49
  val transfer_syntax: theory -> Proof.context -> Proof.context
wenzelm@20310
    50
  val transfer: theory -> Proof.context -> Proof.context
wenzelm@38756
    51
  val background_theory: (theory -> theory) -> Proof.context -> Proof.context
wenzelm@38756
    52
  val background_theory_result: (theory -> 'a * theory) -> Proof.context -> 'a * Proof.context
wenzelm@28212
    53
  val extern_fact: Proof.context -> string -> xstring
wenzelm@21728
    54
  val pretty_term_abbrev: Proof.context -> term -> Pretty.T
wenzelm@30723
    55
  val pretty_fact_aux: Proof.context -> bool -> string * thm list -> Pretty.T
wenzelm@20310
    56
  val pretty_fact: Proof.context -> string * thm list -> Pretty.T
wenzelm@35669
    57
  val read_class: Proof.context -> xstring -> class
wenzelm@35669
    58
  val read_arity: Proof.context -> xstring * string list * string -> arity
wenzelm@35669
    59
  val cert_arity: Proof.context -> arity -> arity
wenzelm@20310
    60
  val read_typ: Proof.context -> string -> typ
wenzelm@20310
    61
  val read_typ_syntax: Proof.context -> string -> typ
wenzelm@20310
    62
  val read_typ_abbrev: Proof.context -> string -> typ
wenzelm@20310
    63
  val cert_typ: Proof.context -> typ -> typ
wenzelm@20310
    64
  val cert_typ_syntax: Proof.context -> typ -> typ
wenzelm@20310
    65
  val cert_typ_abbrev: Proof.context -> typ -> typ
wenzelm@36505
    66
  val infer_type: Proof.context -> string * typ -> typ
wenzelm@28082
    67
  val inferred_param: string -> Proof.context -> typ * Proof.context
wenzelm@25328
    68
  val inferred_fixes: Proof.context -> (string * typ) list * Proof.context
wenzelm@35360
    69
  val read_type_name: Proof.context -> bool -> string -> typ
wenzelm@35669
    70
  val read_type_name_proper: Proof.context -> bool -> string -> typ
wenzelm@35399
    71
  val read_const_proper: Proof.context -> bool -> string -> term
wenzelm@36505
    72
  val read_const: Proof.context -> bool -> typ -> string -> term
wenzelm@27259
    73
  val allow_dummies: Proof.context -> Proof.context
wenzelm@45453
    74
  val prepare_sorts: Proof.context -> typ list -> typ list
wenzelm@36152
    75
  val check_tfree: Proof.context -> string * sort -> string * sort
wenzelm@42250
    76
  val intern_skolem: Proof.context -> string -> string option
wenzelm@24684
    77
  val read_term_pattern: Proof.context -> string -> term
wenzelm@24684
    78
  val read_term_schematic: Proof.context -> string -> term
wenzelm@24684
    79
  val read_term_abbrev: Proof.context -> string -> term
wenzelm@40879
    80
  val show_abbrevs_raw: Config.raw
wenzelm@40879
    81
  val show_abbrevs: bool Config.T
wenzelm@25345
    82
  val expand_abbrevs: Proof.context -> term -> term
wenzelm@20310
    83
  val cert_term: Proof.context -> term -> term
wenzelm@20310
    84
  val cert_prop: Proof.context -> term -> term
wenzelm@35616
    85
  val def_type: Proof.context -> indexname -> typ option
wenzelm@45429
    86
  val standard_typ_check: Proof.context -> typ list -> typ list
wenzelm@45429
    87
  val standard_term_check_finish: Proof.context -> term list -> term list
wenzelm@45429
    88
  val standard_term_uncheck: Proof.context -> term list -> term list
wenzelm@20310
    89
  val goal_export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@20310
    90
  val export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@21531
    91
  val export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@28396
    92
  val norm_export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@30757
    93
  val bind_terms: (indexname * term option) list -> Proof.context -> Proof.context
wenzelm@20310
    94
  val auto_bind_goal: term list -> Proof.context -> Proof.context
wenzelm@20310
    95
  val auto_bind_facts: term list -> Proof.context -> Proof.context
wenzelm@20310
    96
  val match_bind: bool -> (string list * string) list -> Proof.context -> term list * Proof.context
wenzelm@20310
    97
  val match_bind_i: bool -> (term list * term) list -> Proof.context -> term list * Proof.context
wenzelm@45327
    98
  val read_propp: (string * string list) list list -> Proof.context ->
wenzelm@45327
    99
    (term * term list) list list * Proof.context
wenzelm@45327
   100
  val cert_propp: (term * term list) list list -> Proof.context ->
wenzelm@45327
   101
    (term * term list) list list * Proof.context
wenzelm@45327
   102
  val read_propp_schematic: (string * string list) list list -> Proof.context ->
wenzelm@45327
   103
    (term * term list) list list * Proof.context
wenzelm@45327
   104
  val cert_propp_schematic: (term * term list) list list -> Proof.context ->
wenzelm@45327
   105
    (term * term list) list list * Proof.context
wenzelm@45327
   106
  val bind_propp: (string * string list) list list -> Proof.context ->
wenzelm@45327
   107
    (term list list * (Proof.context -> Proof.context)) * Proof.context
wenzelm@45327
   108
  val bind_propp_i: (term * term list) list list -> Proof.context ->
wenzelm@45327
   109
    (term list list * (Proof.context -> Proof.context)) * Proof.context
wenzelm@45327
   110
  val bind_propp_schematic: (string * string list) list list -> Proof.context ->
wenzelm@45327
   111
    (term list list * (Proof.context -> Proof.context)) * Proof.context
wenzelm@45327
   112
  val bind_propp_schematic_i: (term * term list) list list -> Proof.context ->
wenzelm@45327
   113
    (term list list * (Proof.context -> Proof.context)) * Proof.context
wenzelm@18042
   114
  val fact_tac: thm list -> int -> tactic
wenzelm@20310
   115
  val some_fact_tac: Proof.context -> int -> tactic
wenzelm@26346
   116
  val get_fact: Proof.context -> Facts.ref -> thm list
wenzelm@26346
   117
  val get_fact_single: Proof.context -> Facts.ref -> thm
wenzelm@26346
   118
  val get_thms: Proof.context -> xstring -> thm list
wenzelm@26346
   119
  val get_thm: Proof.context -> xstring -> thm
wenzelm@30761
   120
  val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list ->
wenzelm@30211
   121
    Proof.context -> (string * thm list) list * Proof.context
wenzelm@28082
   122
  val put_thms: bool -> string * thm list option -> Proof.context -> Proof.context
haftmann@29581
   123
  val read_vars: (binding * string option * mixfix) list -> Proof.context ->
haftmann@29581
   124
    (binding * typ option * mixfix) list * Proof.context
haftmann@29581
   125
  val cert_vars: (binding * typ option * mixfix) list -> Proof.context ->
haftmann@29581
   126
    (binding * typ option * mixfix) list * Proof.context
wenzelm@30763
   127
  val add_fixes: (binding * typ option * mixfix) list -> Proof.context ->
wenzelm@30763
   128
    string list * Proof.context
wenzelm@20234
   129
  val add_assms: Assumption.export ->
wenzelm@30211
   130
    (Thm.binding * (string * string list) list) list ->
wenzelm@28082
   131
    Proof.context -> (string * thm list) list * Proof.context
wenzelm@20234
   132
  val add_assms_i: Assumption.export ->
wenzelm@30211
   133
    (Thm.binding * (term * term list) list) list ->
wenzelm@28082
   134
    Proof.context -> (string * thm list) list * Proof.context
wenzelm@33368
   135
  val add_cases: bool -> (string * Rule_Cases.T option) list -> Proof.context -> Proof.context
wenzelm@33368
   136
  val apply_case: Rule_Cases.T -> Proof.context -> (string * term list) list * Proof.context
wenzelm@42496
   137
  val get_case: Proof.context -> string -> binding option list -> Rule_Cases.T
wenzelm@35412
   138
  val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> Proof.context -> Proof.context
wenzelm@24949
   139
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> Proof.context -> Proof.context
wenzelm@35412
   140
  val target_type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> morphism ->
wenzelm@35412
   141
    Context.generic -> Context.generic
wenzelm@24949
   142
  val target_notation: bool -> Syntax.mode -> (term * mixfix) list -> morphism ->
wenzelm@21744
   143
    Context.generic -> Context.generic
wenzelm@35680
   144
  val class_alias: binding -> class -> Proof.context -> Proof.context
wenzelm@35680
   145
  val type_alias: binding -> string -> Proof.context -> Proof.context
wenzelm@35680
   146
  val const_alias: binding -> string -> Proof.context -> Proof.context
wenzelm@24767
   147
  val add_const_constraint: string * typ option -> Proof.context -> Proof.context
wenzelm@33173
   148
  val add_abbrev: string -> binding * term -> Proof.context -> (term * term) * Proof.context
wenzelm@25052
   149
  val revert_abbrev: string -> string -> Proof.context -> Proof.context
wenzelm@20310
   150
  val print_syntax: Proof.context -> unit
wenzelm@21728
   151
  val print_abbrevs: Proof.context -> unit
wenzelm@20310
   152
  val print_binds: Proof.context -> unit
wenzelm@20310
   153
  val print_lthms: Proof.context -> unit
wenzelm@20310
   154
  val print_cases: Proof.context -> unit
wenzelm@42717
   155
  val debug: bool Config.T
wenzelm@42717
   156
  val verbose: bool Config.T
wenzelm@20310
   157
  val pretty_ctxt: Proof.context -> Pretty.T list
wenzelm@20310
   158
  val pretty_context: Proof.context -> Pretty.T list
wenzelm@5819
   159
end;
wenzelm@5819
   160
wenzelm@42360
   161
structure Proof_Context: PROOF_CONTEXT =
wenzelm@5819
   162
struct
wenzelm@5819
   163
wenzelm@42363
   164
val theory_of = Proof_Context.theory_of;
wenzelm@42363
   165
val init_global = Proof_Context.init_global;
wenzelm@42363
   166
wenzelm@12057
   167
wenzelm@7270
   168
wenzelm@24388
   169
(** inner syntax mode **)
wenzelm@24388
   170
wenzelm@24388
   171
datatype mode =
wenzelm@24388
   172
  Mode of
wenzelm@24486
   173
   {stmt: bool,                (*inner statement mode*)
wenzelm@24388
   174
    pattern: bool,             (*pattern binding schematic variables*)
wenzelm@24388
   175
    schematic: bool,           (*term referencing loose schematic variables*)
wenzelm@24388
   176
    abbrev: bool};             (*abbrev mode -- no normalization*)
wenzelm@24388
   177
wenzelm@24486
   178
fun make_mode (stmt, pattern, schematic, abbrev) =
wenzelm@24486
   179
  Mode {stmt = stmt, pattern = pattern, schematic = schematic, abbrev = abbrev};
wenzelm@24388
   180
wenzelm@24486
   181
val mode_default   = make_mode (false, false, false, false);
wenzelm@24486
   182
val mode_stmt      = make_mode (true, false, false, false);
wenzelm@24486
   183
val mode_pattern   = make_mode (false, true, false, false);
wenzelm@24486
   184
val mode_schematic = make_mode (false, false, true, false);
wenzelm@24486
   185
val mode_abbrev    = make_mode (false, false, false, true);
wenzelm@24388
   186
wenzelm@24388
   187
wenzelm@5819
   188
wenzelm@16540
   189
(** Isar proof context information **)
wenzelm@5819
   190
wenzelm@16540
   191
datatype ctxt =
wenzelm@16540
   192
  Ctxt of
wenzelm@36451
   193
   {mode: mode,                  (*inner syntax mode*)
wenzelm@36451
   194
    naming: Name_Space.naming,   (*local naming conventions*)
wenzelm@36451
   195
    syntax: Local_Syntax.T,      (*local syntax*)
wenzelm@36451
   196
    tsig: Type.tsig * Type.tsig, (*local/global type signature -- local name space / defsort only*)
wenzelm@36451
   197
    consts: Consts.T * Consts.T, (*local/global consts -- local name space / abbrevs only*)
wenzelm@36451
   198
    facts: Facts.T,              (*local facts*)
wenzelm@33368
   199
    cases: (string * (Rule_Cases.T * bool)) list};    (*named case contexts*)
wenzelm@5819
   200
wenzelm@36450
   201
fun make_ctxt (mode, naming, syntax, tsig, consts, facts, cases) =
haftmann@26240
   202
  Ctxt {mode = mode, naming = naming, syntax = syntax,
wenzelm@36450
   203
    tsig = tsig, consts = consts, facts = facts, cases = cases};
wenzelm@5819
   204
wenzelm@33095
   205
val local_naming = Name_Space.default_naming |> Name_Space.add_path "local";
wenzelm@19079
   206
wenzelm@37216
   207
structure Data = Proof_Data
wenzelm@18672
   208
(
wenzelm@16540
   209
  type T = ctxt;
wenzelm@16540
   210
  fun init thy =
wenzelm@33387
   211
    make_ctxt (mode_default, local_naming, Local_Syntax.init thy,
wenzelm@35669
   212
      (Sign.tsig_of thy, Sign.tsig_of thy),
wenzelm@26284
   213
      (Sign.consts_of thy, Sign.consts_of thy), Facts.empty, []);
wenzelm@18672
   214
);
wenzelm@5819
   215
wenzelm@37216
   216
fun rep_context ctxt = Data.get ctxt |> (fn Ctxt args => args);
wenzelm@5819
   217
wenzelm@18672
   218
fun map_context f =
wenzelm@37216
   219
  Data.map (fn Ctxt {mode, naming, syntax, tsig, consts, facts, cases} =>
wenzelm@36450
   220
    make_ctxt (f (mode, naming, syntax, tsig, consts, facts, cases)));
wenzelm@24388
   221
wenzelm@36450
   222
fun set_mode mode = map_context (fn (_, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   223
  (mode, naming, syntax, tsig, consts, facts, cases));
wenzelm@21443
   224
wenzelm@24388
   225
fun map_mode f =
wenzelm@36450
   226
  map_context (fn (Mode {stmt, pattern, schematic, abbrev}, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   227
    (make_mode (f (stmt, pattern, schematic, abbrev)), naming, syntax, tsig, consts, facts, cases));
wenzelm@18672
   228
wenzelm@19001
   229
fun map_naming f =
wenzelm@36450
   230
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   231
    (mode, f naming, syntax, tsig, consts, facts, cases));
wenzelm@18672
   232
wenzelm@19001
   233
fun map_syntax f =
wenzelm@36450
   234
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   235
    (mode, naming, f syntax, tsig, consts, facts, cases));
wenzelm@35669
   236
wenzelm@36450
   237
fun map_tsig f =
wenzelm@36450
   238
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   239
    (mode, naming, syntax, f tsig, consts, facts, cases));
wenzelm@18672
   240
wenzelm@19001
   241
fun map_consts f =
wenzelm@36450
   242
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   243
    (mode, naming, syntax, tsig, f consts, facts, cases));
wenzelm@18672
   244
wenzelm@26284
   245
fun map_facts f =
wenzelm@36450
   246
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   247
    (mode, naming, syntax, tsig, consts, f facts, cases));
wenzelm@18672
   248
wenzelm@19001
   249
fun map_cases f =
wenzelm@36450
   250
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   251
    (mode, naming, syntax, tsig, consts, facts, f cases));
wenzelm@24388
   252
wenzelm@24388
   253
val get_mode = #mode o rep_context;
wenzelm@28407
   254
val restore_mode = set_mode o get_mode;
wenzelm@27286
   255
val abbrev_mode = get_mode #> (fn Mode {abbrev, ...} => abbrev);
wenzelm@21443
   256
wenzelm@24486
   257
fun set_stmt stmt =
wenzelm@24486
   258
  map_mode (fn (_, pattern, schematic, abbrev) => (stmt, pattern, schematic, abbrev));
wenzelm@19001
   259
wenzelm@19001
   260
val naming_of = #naming o rep_context;
wenzelm@33165
   261
val restore_naming = map_naming o K o naming_of
wenzelm@33095
   262
val full_name = Name_Space.full_name o naming_of;
wenzelm@5819
   263
wenzelm@16540
   264
val syntax_of = #syntax o rep_context;
wenzelm@33387
   265
val syn_of = Local_Syntax.syn_of o syntax_of;
wenzelm@33387
   266
val set_syntax_mode = map_syntax o Local_Syntax.set_mode;
wenzelm@33387
   267
val restore_syntax_mode = map_syntax o Local_Syntax.restore_mode o syntax_of;
wenzelm@19001
   268
wenzelm@36450
   269
val tsig_of = #1 o #tsig o rep_context;
wenzelm@36451
   270
val set_defsort = map_tsig o apfst o Type.set_defsort;
wenzelm@35680
   271
fun default_sort ctxt = the_default (Type.defaultS (tsig_of ctxt)) o Variable.def_sort ctxt;
wenzelm@35669
   272
wenzelm@25052
   273
val consts_of = #1 o #consts o rep_context;
haftmann@24752
   274
val the_const_constraint = Consts.the_constraint o consts_of;
wenzelm@5819
   275
wenzelm@26284
   276
val facts_of = #facts o rep_context;
wenzelm@16540
   277
val cases_of = #cases o rep_context;
wenzelm@5819
   278
wenzelm@5819
   279
wenzelm@42359
   280
(* name spaces *)
wenzelm@42359
   281
wenzelm@42359
   282
val class_space = Type.class_space o tsig_of;
wenzelm@42359
   283
val type_space = Type.type_space o tsig_of;
wenzelm@42359
   284
val const_space = Consts.space_of o consts_of;
wenzelm@42359
   285
wenzelm@42359
   286
val intern_class = Name_Space.intern o class_space;
wenzelm@42359
   287
val intern_type = Name_Space.intern o type_space;
wenzelm@42359
   288
val intern_const = Name_Space.intern o const_space;
wenzelm@42359
   289
wenzelm@42359
   290
fun extern_class ctxt = Name_Space.extern ctxt (class_space ctxt);
wenzelm@42359
   291
fun extern_type ctxt = Name_Space.extern ctxt (type_space ctxt);
wenzelm@42359
   292
fun extern_const ctxt = Name_Space.extern ctxt (const_space ctxt);
wenzelm@42359
   293
wenzelm@42359
   294
wenzelm@20367
   295
(* theory transfer *)
wenzelm@12093
   296
wenzelm@35669
   297
fun transfer_syntax thy ctxt = ctxt |>
wenzelm@35669
   298
  map_syntax (Local_Syntax.rebuild thy) |>
wenzelm@36450
   299
  map_tsig (fn tsig as (local_tsig, global_tsig) =>
wenzelm@35669
   300
    let val thy_tsig = Sign.tsig_of thy in
wenzelm@36450
   301
      if Type.eq_tsig (thy_tsig, global_tsig) then tsig
wenzelm@42387
   302
      else (Type.merge_tsig ctxt (local_tsig, thy_tsig), thy_tsig)
wenzelm@35669
   303
    end) |>
wenzelm@30424
   304
  map_consts (fn consts as (local_consts, global_consts) =>
wenzelm@30424
   305
    let val thy_consts = Sign.consts_of thy in
wenzelm@30424
   306
      if Consts.eq_consts (thy_consts, global_consts) then consts
wenzelm@30424
   307
      else (Consts.merge (local_consts, thy_consts), thy_consts)
wenzelm@30424
   308
    end);
wenzelm@17072
   309
wenzelm@33031
   310
fun transfer thy = Context.raw_transfer thy #> transfer_syntax thy;
wenzelm@17072
   311
wenzelm@38756
   312
fun background_theory f ctxt = transfer (f (theory_of ctxt)) ctxt;
wenzelm@20367
   313
wenzelm@38756
   314
fun background_theory_result f ctxt =
wenzelm@20367
   315
  let val (res, thy') = f (theory_of ctxt)
wenzelm@20367
   316
  in (res, ctxt |> transfer thy') end;
wenzelm@19019
   317
wenzelm@12093
   318
wenzelm@12093
   319
wenzelm@14828
   320
(** pretty printing **)
wenzelm@14828
   321
wenzelm@28212
   322
(* extern *)
wenzelm@28212
   323
wenzelm@42378
   324
fun which_facts ctxt name =
wenzelm@28212
   325
  let
wenzelm@28212
   326
    val local_facts = facts_of ctxt;
wenzelm@39557
   327
    val global_facts = Global_Theory.facts_of (theory_of ctxt);
wenzelm@28212
   328
  in
wenzelm@28212
   329
    if is_some (Facts.lookup (Context.Proof ctxt) local_facts name)
wenzelm@42378
   330
    then local_facts else global_facts
wenzelm@28212
   331
  end;
wenzelm@28212
   332
wenzelm@42379
   333
fun markup_fact ctxt name = Name_Space.markup (Facts.space_of (which_facts ctxt name)) name;
wenzelm@42378
   334
wenzelm@42378
   335
fun extern_fact ctxt name = Facts.extern ctxt (which_facts ctxt name) name;
wenzelm@42378
   336
wenzelm@28212
   337
wenzelm@28212
   338
(* pretty *)
wenzelm@28212
   339
wenzelm@24922
   340
fun pretty_term_abbrev ctxt = Syntax.pretty_term (set_mode mode_abbrev ctxt);
wenzelm@14828
   341
wenzelm@42378
   342
fun pretty_fact_name ctxt a =
wenzelm@42378
   343
  Pretty.block [Pretty.mark_str (markup_fact ctxt a, extern_fact ctxt a), Pretty.str ":"];
wenzelm@28209
   344
wenzelm@32090
   345
fun pretty_fact_aux ctxt flag ("", ths) =
wenzelm@32090
   346
      Display.pretty_thms_aux ctxt flag ths
wenzelm@30723
   347
  | pretty_fact_aux ctxt flag (a, [th]) = Pretty.block
wenzelm@32090
   348
      [pretty_fact_name ctxt a, Pretty.brk 1, Display.pretty_thm_aux ctxt flag th]
wenzelm@30723
   349
  | pretty_fact_aux ctxt flag (a, ths) = Pretty.block
wenzelm@32090
   350
      (Pretty.fbreaks (pretty_fact_name ctxt a :: map (Display.pretty_thm_aux ctxt flag) ths));
wenzelm@30723
   351
wenzelm@30723
   352
fun pretty_fact ctxt = pretty_fact_aux ctxt true;
wenzelm@14828
   353
wenzelm@14828
   354
wenzelm@14828
   355
wenzelm@5819
   356
(** prepare types **)
wenzelm@5819
   357
wenzelm@35669
   358
(* classes *)
wenzelm@35669
   359
wenzelm@35669
   360
fun read_class ctxt text =
wenzelm@35669
   361
  let
wenzelm@35669
   362
    val tsig = tsig_of ctxt;
wenzelm@35669
   363
    val (syms, pos) = Syntax.read_token text;
wenzelm@35669
   364
    val c = Type.cert_class tsig (Type.intern_class tsig (Symbol_Pos.content syms))
wenzelm@35669
   365
      handle TYPE (msg, _, _) => error (msg ^ Position.str_of pos);
wenzelm@42467
   366
    val _ = Context_Position.report ctxt pos (Name_Space.markup (Type.class_space tsig) c);
wenzelm@35669
   367
  in c end;
wenzelm@35669
   368
wenzelm@35669
   369
wenzelm@35669
   370
(* type arities *)
wenzelm@35669
   371
wenzelm@35669
   372
local
wenzelm@35669
   373
wenzelm@35669
   374
fun prep_arity prep_tycon prep_sort ctxt (t, Ss, S) =
wenzelm@35669
   375
  let val arity = (prep_tycon ctxt t, map (prep_sort ctxt) Ss, prep_sort ctxt S)
wenzelm@42387
   376
  in Type.add_arity ctxt arity (tsig_of ctxt); arity end;
wenzelm@35669
   377
wenzelm@35669
   378
in
wenzelm@35669
   379
wenzelm@42359
   380
val read_arity = prep_arity intern_type Syntax.read_sort;
wenzelm@35669
   381
val cert_arity = prep_arity (K I) (Type.cert_sort o tsig_of);
wenzelm@35669
   382
wenzelm@35669
   383
end;
wenzelm@35669
   384
wenzelm@35669
   385
wenzelm@35669
   386
(* types *)
wenzelm@24277
   387
wenzelm@24277
   388
fun read_typ_mode mode ctxt s =
wenzelm@24486
   389
  Syntax.read_typ (Type.set_mode mode ctxt) s;
wenzelm@24277
   390
wenzelm@35669
   391
val read_typ = read_typ_mode Type.mode_default;
wenzelm@24277
   392
val read_typ_syntax = read_typ_mode Type.mode_syntax;
wenzelm@24277
   393
val read_typ_abbrev = read_typ_mode Type.mode_abbrev;
wenzelm@24277
   394
wenzelm@24277
   395
wenzelm@24277
   396
fun cert_typ_mode mode ctxt T =
wenzelm@35669
   397
  Type.cert_typ_mode mode (tsig_of ctxt) T
wenzelm@24277
   398
    handle TYPE (msg, _, _) => error msg;
wenzelm@24277
   399
wenzelm@35669
   400
val cert_typ = cert_typ_mode Type.mode_default;
wenzelm@24277
   401
val cert_typ_syntax = cert_typ_mode Type.mode_syntax;
wenzelm@24277
   402
val cert_typ_abbrev = cert_typ_mode Type.mode_abbrev;
wenzelm@24277
   403
wenzelm@24277
   404
wenzelm@24388
   405
wenzelm@24388
   406
(** prepare variables **)
wenzelm@24388
   407
wenzelm@42488
   408
(* check Skolem constants *)
wenzelm@7679
   409
wenzelm@18678
   410
fun no_skolem internal x =
wenzelm@20086
   411
  if can Name.dest_skolem x then
wenzelm@18678
   412
    error ("Illegal reference to internal Skolem constant: " ^ quote x)
wenzelm@20086
   413
  else if not internal andalso can Name.dest_internal x then
wenzelm@18678
   414
    error ("Illegal reference to internal variable: " ^ quote x)
wenzelm@7679
   415
  else x;
wenzelm@7679
   416
wenzelm@7679
   417
wenzelm@18187
   418
wenzelm@5819
   419
(** prepare terms and propositions **)
wenzelm@5819
   420
wenzelm@25328
   421
(* inferred types of parameters *)
wenzelm@25328
   422
wenzelm@25328
   423
fun infer_type ctxt x =
wenzelm@36505
   424
  Term.fastype_of (singleton (Syntax.check_terms (set_mode mode_schematic ctxt)) (Free x));
wenzelm@25328
   425
wenzelm@25328
   426
fun inferred_param x ctxt =
wenzelm@36505
   427
  let val T = infer_type ctxt (x, dummyT)
wenzelm@28082
   428
  in (T, ctxt |> Variable.declare_term (Free (x, T))) end;
wenzelm@25328
   429
wenzelm@25328
   430
fun inferred_fixes ctxt =
wenzelm@28082
   431
  let
wenzelm@42488
   432
    val xs = map #2 (Variable.dest_fixes ctxt);
wenzelm@28082
   433
    val (Ts, ctxt') = fold_map inferred_param xs ctxt;
wenzelm@28082
   434
  in (xs ~~ Ts, ctxt') end;
wenzelm@25328
   435
wenzelm@25328
   436
wenzelm@25328
   437
(* type and constant names *)
wenzelm@25328
   438
wenzelm@27821
   439
local
wenzelm@27821
   440
wenzelm@30573
   441
val token_content = Syntax.read_token #>> Symbol_Pos.content;
wenzelm@27821
   442
wenzelm@35399
   443
fun prep_const_proper ctxt strict (c, pos) =
wenzelm@35399
   444
  let
wenzelm@35399
   445
    fun err msg = error (msg ^ Position.str_of pos);
wenzelm@35399
   446
    val consts = consts_of ctxt;
wenzelm@35399
   447
    val t as Const (d, _) =
wenzelm@35399
   448
      (case Variable.lookup_const ctxt c of
wenzelm@35399
   449
        SOME d =>
wenzelm@35399
   450
          Const (d, Consts.type_scheme (consts_of ctxt) d handle TYPE (msg, _, _) => err msg)
wenzelm@42469
   451
      | NONE => Consts.read_const consts (c, pos));
wenzelm@35399
   452
    val _ =
wenzelm@43794
   453
      if strict then ignore (Consts.the_const consts d) handle TYPE (msg, _, _) => err msg
wenzelm@35399
   454
      else ();
wenzelm@42467
   455
    val _ = Context_Position.report ctxt pos (Name_Space.markup (Consts.space_of consts) d);
wenzelm@35399
   456
  in t end;
wenzelm@27821
   457
wenzelm@27821
   458
in
wenzelm@25328
   459
wenzelm@35399
   460
fun read_type_name ctxt strict text =
wenzelm@27821
   461
  let
wenzelm@35669
   462
    val tsig = tsig_of ctxt;
wenzelm@35399
   463
    val (c, pos) = token_content text;
wenzelm@27821
   464
  in
wenzelm@42290
   465
    if Lexicon.is_tid c then
wenzelm@39507
   466
     (Context_Position.report ctxt pos Markup.tfree;
wenzelm@35680
   467
      TFree (c, default_sort ctxt (c, ~1)))
wenzelm@27821
   468
    else
wenzelm@27821
   469
      let
wenzelm@42359
   470
        val d = intern_type ctxt c;
wenzelm@42468
   471
        val decl = Type.the_decl tsig (d, pos);
wenzelm@42467
   472
        fun err () = error ("Bad type name: " ^ quote d ^ Position.str_of pos);
wenzelm@35360
   473
        val args =
wenzelm@35360
   474
          (case decl of
wenzelm@35360
   475
            Type.LogicalType n => n
wenzelm@35360
   476
          | Type.Abbreviation (vs, _, _) => if strict then err () else length vs
wenzelm@35360
   477
          | Type.Nonterminal => if strict then err () else 0);
wenzelm@42467
   478
        val _ = Context_Position.report ctxt pos (Name_Space.markup (Type.type_space tsig) d);
wenzelm@35360
   479
      in Type (d, replicate args dummyT) end
wenzelm@27821
   480
  end;
wenzelm@25328
   481
wenzelm@35669
   482
fun read_type_name_proper ctxt strict text =
wenzelm@35669
   483
  (case read_type_name ctxt strict text of
wenzelm@35669
   484
    T as Type _ => T
wenzelm@35669
   485
  | T => error ("Not a type constructor: " ^ Syntax.string_of_typ ctxt T));
wenzelm@35669
   486
wenzelm@35669
   487
wenzelm@35399
   488
fun read_const_proper ctxt strict = prep_const_proper ctxt strict o token_content;
wenzelm@27821
   489
wenzelm@36505
   490
fun read_const ctxt strict ty text =
wenzelm@36542
   491
  let
wenzelm@36542
   492
    val (c, pos) = token_content text;
wenzelm@36542
   493
    val _ = no_skolem false c;
wenzelm@36542
   494
  in
wenzelm@42488
   495
    (case (Variable.lookup_fixed ctxt c, Variable.is_const ctxt c) of
wenzelm@27821
   496
      (SOME x, false) =>
wenzelm@39507
   497
        (Context_Position.report ctxt pos
wenzelm@39507
   498
            (Markup.name x (if can Name.dest_skolem x then Markup.skolem else Markup.free));
wenzelm@36505
   499
          Free (x, infer_type ctxt (x, ty)))
wenzelm@35399
   500
    | _ => prep_const_proper ctxt strict (c, pos))
wenzelm@27821
   501
  end;
wenzelm@27821
   502
wenzelm@27821
   503
end;
wenzelm@25328
   504
wenzelm@25328
   505
wenzelm@42250
   506
(* skolem variables *)
wenzelm@42250
   507
wenzelm@42250
   508
fun intern_skolem ctxt x =
wenzelm@42250
   509
  let
wenzelm@42250
   510
    val _ = no_skolem false x;
wenzelm@42488
   511
    val sko = Variable.lookup_fixed ctxt x;
wenzelm@42250
   512
    val is_const = can (read_const_proper ctxt false) x orelse Long_Name.is_qualified x;
wenzelm@42250
   513
    val is_declared = is_some (Variable.def_type ctxt false (x, ~1));
wenzelm@42250
   514
  in
wenzelm@42250
   515
    if Variable.is_const ctxt x then NONE
wenzelm@42250
   516
    else if is_some sko then sko
wenzelm@42250
   517
    else if not is_const orelse is_declared then SOME x
wenzelm@42250
   518
    else NONE
wenzelm@42250
   519
  end;
wenzelm@42250
   520
wenzelm@42250
   521
wenzelm@24684
   522
(* read_term *)
wenzelm@24684
   523
wenzelm@24684
   524
fun read_term_mode mode ctxt = Syntax.read_term (set_mode mode ctxt);
wenzelm@24684
   525
wenzelm@24684
   526
val read_term_pattern   = read_term_mode mode_pattern;
wenzelm@24684
   527
val read_term_schematic = read_term_mode mode_schematic;
wenzelm@24684
   528
val read_term_abbrev    = read_term_mode mode_abbrev;
wenzelm@24684
   529
wenzelm@24684
   530
wenzelm@19001
   531
(* local abbreviations *)
wenzelm@5819
   532
wenzelm@24501
   533
local
wenzelm@24501
   534
wenzelm@42383
   535
fun certify_consts ctxt = Consts.certify (Context.pretty ctxt) (tsig_of ctxt)
wenzelm@27286
   536
  (not (abbrev_mode ctxt)) (consts_of ctxt);
wenzelm@19001
   537
wenzelm@38979
   538
fun expand_binds ctxt =
wenzelm@38979
   539
  let
wenzelm@38979
   540
    val Mode {pattern, schematic, ...} = get_mode ctxt;
wenzelm@5819
   541
wenzelm@38979
   542
    fun reject_schematic (t as Var _) =
wenzelm@38979
   543
          error ("Unbound schematic variable: " ^ Syntax.string_of_term ctxt t)
wenzelm@38979
   544
      | reject_schematic (Abs (_, _, t)) = reject_schematic t
wenzelm@38979
   545
      | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
wenzelm@38979
   546
      | reject_schematic _ = ();
wenzelm@38979
   547
  in
wenzelm@24495
   548
    if pattern then I
wenzelm@24495
   549
    else Variable.expand_binds ctxt #> (if schematic then I else tap reject_schematic)
wenzelm@24495
   550
  end;
wenzelm@5819
   551
wenzelm@24501
   552
in
wenzelm@24501
   553
wenzelm@24501
   554
fun expand_abbrevs ctxt = certify_consts ctxt #> expand_binds ctxt;
wenzelm@24501
   555
wenzelm@24501
   556
end;
wenzelm@24501
   557
wenzelm@40879
   558
val show_abbrevs_raw = Config.declare "show_abbrevs" (fn _ => Config.Bool true);
wenzelm@40879
   559
val show_abbrevs = Config.bool show_abbrevs_raw;
wenzelm@5819
   560
wenzelm@24922
   561
fun contract_abbrevs ctxt t =
wenzelm@24922
   562
  let
wenzelm@24922
   563
    val thy = theory_of ctxt;
wenzelm@24922
   564
    val consts = consts_of ctxt;
wenzelm@24922
   565
    val Mode {abbrev, ...} = get_mode ctxt;
wenzelm@30566
   566
    val retrieve = Consts.retrieve_abbrevs consts (print_mode_value () @ [""]);
wenzelm@30566
   567
    fun match_abbrev u = Option.map #1 (get_first (Pattern.match_rew thy u) (retrieve u));
wenzelm@24922
   568
  in
wenzelm@40879
   569
    if abbrev orelse not (Config.get ctxt show_abbrevs) orelse not (can Term.type_of t) then t
berghofe@35211
   570
    else Pattern.rewrite_term_top thy [] [match_abbrev] t
wenzelm@24922
   571
  end;
wenzelm@24922
   572
wenzelm@24922
   573
wenzelm@24518
   574
(* patterns *)
wenzelm@24518
   575
wenzelm@32003
   576
fun prepare_patternT ctxt T =
wenzelm@32003
   577
  let
wenzelm@32003
   578
    val Mode {pattern, schematic, ...} = get_mode ctxt;
wenzelm@32003
   579
    val _ =
wenzelm@32003
   580
      pattern orelse schematic orelse
wenzelm@32003
   581
        T |> Term.exists_subtype
wenzelm@38979
   582
          (fn T as TVar (xi, _) =>
wenzelm@37145
   583
            not (Type_Infer.is_param xi) andalso
wenzelm@38979
   584
              error ("Illegal schematic type variable: " ^ Syntax.string_of_typ ctxt T)
wenzelm@32003
   585
          | _ => false)
wenzelm@32003
   586
  in T end;
wenzelm@24518
   587
wenzelm@22712
   588
wenzelm@24505
   589
local
wenzelm@6550
   590
wenzelm@42360
   591
val dummies = Config.bool (Config.declare "Proof_Context.dummies" (K (Config.Bool false)));
wenzelm@27259
   592
wenzelm@27259
   593
fun check_dummies ctxt t =
wenzelm@39508
   594
  if Config.get ctxt dummies then t
wenzelm@27259
   595
  else Term.no_dummy_patterns t handle TERM _ => error "Illegal dummy pattern(s) in term";
wenzelm@27259
   596
wenzelm@24767
   597
fun prepare_dummies ts = #1 (fold_map Term.replace_dummy_patterns ts 1);
wenzelm@6762
   598
wenzelm@27259
   599
in
wenzelm@6550
   600
wenzelm@39508
   601
val allow_dummies = Config.put dummies true;
wenzelm@24505
   602
wenzelm@24684
   603
fun prepare_patterns ctxt =
wenzelm@24518
   604
  let val Mode {pattern, ...} = get_mode ctxt in
wenzelm@39296
   605
    Type_Infer.fixate ctxt #>
wenzelm@24767
   606
    pattern ? Variable.polymorphic ctxt #>
wenzelm@24684
   607
    (map o Term.map_types) (prepare_patternT ctxt) #>
wenzelm@27259
   608
    (if pattern then prepare_dummies else map (check_dummies ctxt))
wenzelm@24505
   609
  end;
wenzelm@24505
   610
wenzelm@24505
   611
end;
wenzelm@24505
   612
wenzelm@6550
   613
wenzelm@42250
   614
(* sort constraints *)
wenzelm@27286
   615
wenzelm@45453
   616
fun prepare_sorts ctxt tys =
wenzelm@27286
   617
  let
wenzelm@35669
   618
    val tsig = tsig_of ctxt;
wenzelm@45453
   619
    val defaultS = Type.defaultS tsig;
wenzelm@27286
   620
wenzelm@45453
   621
    fun constraint (xi, S) env =
wenzelm@45453
   622
      if S = dummyS then env
wenzelm@45453
   623
      else
wenzelm@45453
   624
        Vartab.insert (op =) (xi, Type.minimize_sort tsig S) env
wenzelm@45453
   625
          handle Vartab.DUP _ =>
wenzelm@45453
   626
            error ("Inconsistent sort constraints for type variable " ^
wenzelm@45453
   627
              quote (Term.string_of_vname' xi));
wenzelm@45453
   628
    val env =
wenzelm@45453
   629
      (fold o fold_atyps)
wenzelm@45453
   630
        (fn TFree (x, S) => constraint ((x, ~1), S)
wenzelm@45453
   631
          | TVar v => constraint v
wenzelm@45453
   632
          | _ => I) tys Vartab.empty;
wenzelm@36152
   633
wenzelm@45453
   634
    fun get_sort xi =
wenzelm@45453
   635
      (case (Vartab.lookup env xi, Variable.def_sort ctxt xi) of
wenzelm@45453
   636
        (NONE, NONE) => defaultS
wenzelm@27286
   637
      | (NONE, SOME S) => S
wenzelm@27286
   638
      | (SOME S, NONE) => S
wenzelm@27286
   639
      | (SOME S, SOME S') =>
wenzelm@27286
   640
          if Type.eq_sort tsig (S, S') then S'
wenzelm@45453
   641
          else
wenzelm@45453
   642
            error ("Sort constraint " ^ Syntax.string_of_sort ctxt S ^
wenzelm@45453
   643
              " inconsistent with default " ^ Syntax.string_of_sort ctxt S' ^
wenzelm@45453
   644
              " for type variable " ^ quote (Term.string_of_vname' xi)));
wenzelm@45453
   645
  in
wenzelm@45453
   646
    (map o map_atyps)
wenzelm@45453
   647
      (fn T =>
wenzelm@45453
   648
        if Term_Position.is_positionT T then T
wenzelm@45453
   649
        else
wenzelm@45453
   650
          (case T of
wenzelm@45453
   651
            TFree (x, _) => TFree (x, get_sort (x, ~1))
wenzelm@45453
   652
          | TVar (xi, _) => TVar (xi, get_sort xi)
wenzelm@45453
   653
          | _ => T)) tys
wenzelm@45453
   654
  end;
wenzelm@27286
   655
wenzelm@45453
   656
fun check_tfree ctxt v = dest_TFree (singleton (prepare_sorts ctxt) (TFree v));
wenzelm@36152
   657
wenzelm@5819
   658
wenzelm@5819
   659
(* certify terms *)
wenzelm@5819
   660
wenzelm@10554
   661
local
wenzelm@10554
   662
wenzelm@24684
   663
fun gen_cert prop ctxt t =
wenzelm@24684
   664
  t
wenzelm@24684
   665
  |> expand_abbrevs ctxt
wenzelm@42383
   666
  |> (fn t' =>
wenzelm@42383
   667
      #1 (Sign.certify' prop (Context.pretty ctxt) false (consts_of ctxt) (theory_of ctxt) t')
wenzelm@42383
   668
        handle TYPE (msg, _, _) => error msg | TERM (msg, _) => error msg);
wenzelm@16501
   669
wenzelm@10554
   670
in
wenzelm@8096
   671
wenzelm@24684
   672
val cert_term = gen_cert false;
wenzelm@24684
   673
val cert_prop = gen_cert true;
wenzelm@10554
   674
wenzelm@10554
   675
end;
wenzelm@5819
   676
wenzelm@5819
   677
wenzelm@42405
   678
(* check/uncheck *)
wenzelm@22701
   679
wenzelm@35616
   680
fun def_type ctxt =
wenzelm@35616
   681
  let val Mode {pattern, ...} = get_mode ctxt
wenzelm@35616
   682
  in Variable.def_type ctxt pattern end;
wenzelm@35616
   683
wenzelm@24518
   684
fun standard_typ_check ctxt =
wenzelm@45429
   685
  map (cert_typ_mode (Type.get_mode ctxt) ctxt #> prepare_patternT ctxt);
wenzelm@24922
   686
wenzelm@45429
   687
val standard_term_check_finish = prepare_patterns;
wenzelm@24518
   688
wenzelm@45429
   689
fun standard_term_uncheck ctxt = map (contract_abbrevs ctxt);
wenzelm@22701
   690
wenzelm@22701
   691
wenzelm@9553
   692
wenzelm@21610
   693
(** export results **)
wenzelm@21531
   694
wenzelm@20310
   695
fun common_export is_goal inner outer =
wenzelm@20310
   696
  map (Assumption.export is_goal inner outer) #>
wenzelm@20310
   697
  Variable.export inner outer;
wenzelm@8616
   698
wenzelm@20310
   699
val goal_export = common_export true;
wenzelm@20310
   700
val export = common_export false;
wenzelm@12704
   701
wenzelm@21531
   702
fun export_morphism inner outer =
wenzelm@21531
   703
  Assumption.export_morphism inner outer $>
wenzelm@21531
   704
  Variable.export_morphism inner outer;
wenzelm@21531
   705
wenzelm@28396
   706
fun norm_export_morphism inner outer =
wenzelm@28396
   707
  export_morphism inner outer $>
wenzelm@28396
   708
  Morphism.thm_morphism Goal.norm_result;
wenzelm@28396
   709
wenzelm@21531
   710
wenzelm@15758
   711
wenzelm@30757
   712
(** term bindings **)
wenzelm@5819
   713
wenzelm@8096
   714
(* simult_matches *)
wenzelm@8096
   715
wenzelm@19867
   716
fun simult_matches ctxt (t, pats) =
wenzelm@19867
   717
  (case Seq.pull (Unify.matchers (theory_of ctxt) (map (rpair t) pats)) of
wenzelm@19867
   718
    NONE => error "Pattern match failed!"
wenzelm@32032
   719
  | SOME (env, _) => Vartab.fold (fn (v, (_, t)) => cons (v, t)) (Envir.term_env env) []);
wenzelm@8096
   720
wenzelm@8096
   721
wenzelm@30757
   722
(* bind_terms *)
wenzelm@7925
   723
wenzelm@30757
   724
val bind_terms = fold (fn (xi, t) => fn ctxt =>
wenzelm@24511
   725
  ctxt
wenzelm@30757
   726
  |> Variable.bind_term (xi, Option.map (cert_term (set_mode mode_default ctxt)) t));
wenzelm@5819
   727
wenzelm@30757
   728
wenzelm@30757
   729
(* auto_bind *)
wenzelm@10810
   730
wenzelm@20330
   731
fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
wenzelm@10554
   732
  | drop_schematic b = b;
wenzelm@10554
   733
wenzelm@30757
   734
fun auto_bind f ts ctxt = ctxt |> bind_terms (map drop_schematic (f (theory_of ctxt) ts));
wenzelm@8616
   735
wenzelm@33386
   736
val auto_bind_goal = auto_bind Auto_Bind.goal;
wenzelm@33386
   737
val auto_bind_facts = auto_bind Auto_Bind.facts;
wenzelm@7925
   738
wenzelm@5819
   739
wenzelm@8096
   740
(* match_bind(_i) *)
wenzelm@5819
   741
wenzelm@8096
   742
local
wenzelm@8096
   743
wenzelm@24684
   744
fun gen_bind prep_terms gen raw_binds ctxt =
wenzelm@5819
   745
  let
wenzelm@24684
   746
    fun prep_bind (raw_pats, t) ctxt1 =
wenzelm@24684
   747
      let
wenzelm@24684
   748
        val T = Term.fastype_of t;
wenzelm@24684
   749
        val ctxt2 = Variable.declare_term t ctxt1;
wenzelm@24684
   750
        val pats = prep_terms (set_mode mode_pattern ctxt2) T raw_pats;
wenzelm@24684
   751
        val binds = simult_matches ctxt2 (t, pats);
wenzelm@24684
   752
      in (binds, ctxt2) end;
wenzelm@7670
   753
wenzelm@24686
   754
    val ts = prep_terms ctxt dummyT (map snd raw_binds);
wenzelm@24686
   755
    val (binds, ctxt') = apfst flat (fold_map prep_bind (map fst raw_binds ~~ ts) ctxt);
wenzelm@8616
   756
    val binds' =
wenzelm@19916
   757
      if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
wenzelm@8616
   758
      else binds;
skalberg@15531
   759
    val binds'' = map (apsnd SOME) binds';
wenzelm@18310
   760
    val ctxt'' =
wenzelm@19897
   761
      tap (Variable.warn_extra_tfrees ctxt)
wenzelm@18310
   762
       (if gen then
wenzelm@30757
   763
          ctxt (*sic!*) |> fold Variable.declare_term (map #2 binds') |> bind_terms binds''
wenzelm@30757
   764
        else ctxt' |> bind_terms binds'');
wenzelm@18310
   765
  in (ts, ctxt'') end;
wenzelm@8096
   766
wenzelm@8096
   767
in
wenzelm@5935
   768
wenzelm@24684
   769
fun read_terms ctxt T =
wenzelm@39288
   770
  map (Syntax.parse_term ctxt #> Type.constraint T) #> Syntax.check_terms ctxt;
wenzelm@24684
   771
wenzelm@24684
   772
val match_bind = gen_bind read_terms;
wenzelm@24684
   773
val match_bind_i = gen_bind (fn ctxt => fn _ => map (cert_term ctxt));
wenzelm@8096
   774
wenzelm@8096
   775
end;
wenzelm@5935
   776
wenzelm@5935
   777
wenzelm@10465
   778
(* propositions with patterns *)
wenzelm@5935
   779
wenzelm@10465
   780
local
wenzelm@8096
   781
wenzelm@45327
   782
fun prep_propp mode prep_props args context =
wenzelm@10465
   783
  let
wenzelm@19585
   784
    fun prep (_, raw_pats) (ctxt, prop :: props) =
wenzelm@24684
   785
      let val ctxt' = Variable.declare_term prop ctxt
wenzelm@24684
   786
      in ((prop, prep_props (set_mode mode_pattern ctxt') raw_pats), (ctxt', props)) end;
wenzelm@24684
   787
wenzelm@45327
   788
    val (propp, (context', _)) =
wenzelm@45327
   789
      (fold_map o fold_map) prep args
wenzelm@45327
   790
        (context, prep_props (set_mode mode context) (maps (map fst) args));
wenzelm@45327
   791
  in (propp, context') end;
wenzelm@5935
   792
wenzelm@45327
   793
fun gen_bind_propp mode parse_prop raw_args ctxt =
wenzelm@8096
   794
  let
wenzelm@45327
   795
    val (args, ctxt') = prep_propp mode parse_prop raw_args ctxt;
wenzelm@19585
   796
    val binds = flat (flat (map (map (simult_matches ctxt')) args));
wenzelm@10465
   797
    val propss = map (map #1) args;
wenzelm@45330
   798
    fun gen_binds ctxt0 = ctxt0
wenzelm@45330
   799
      |> bind_terms (map #1 binds ~~
wenzelm@45330
   800
          map (SOME o Term.close_schematic_term) (Variable.export_terms ctxt' ctxt0 (map #2 binds)));
wenzelm@45327
   801
  in ((propss, gen_binds), ctxt' |> bind_terms (map (apsnd SOME) binds)) end;
wenzelm@8096
   802
wenzelm@10465
   803
in
wenzelm@10465
   804
wenzelm@24684
   805
val read_propp           = prep_propp mode_default Syntax.read_props;
wenzelm@24684
   806
val cert_propp           = prep_propp mode_default (map o cert_prop);
wenzelm@24684
   807
val read_propp_schematic = prep_propp mode_schematic Syntax.read_props;
wenzelm@24684
   808
val cert_propp_schematic = prep_propp mode_schematic (map o cert_prop);
wenzelm@10554
   809
wenzelm@24684
   810
val bind_propp             = gen_bind_propp mode_default Syntax.read_props;
wenzelm@24684
   811
val bind_propp_i           = gen_bind_propp mode_default (map o cert_prop);
wenzelm@24684
   812
val bind_propp_schematic   = gen_bind_propp mode_schematic Syntax.read_props;
wenzelm@24684
   813
val bind_propp_schematic_i = gen_bind_propp mode_schematic (map o cert_prop);
wenzelm@6789
   814
wenzelm@10465
   815
end;
wenzelm@10465
   816
wenzelm@6789
   817
wenzelm@5819
   818
wenzelm@5819
   819
(** theorems **)
wenzelm@5819
   820
wenzelm@18042
   821
(* fact_tac *)
wenzelm@18042
   822
wenzelm@27867
   823
fun comp_incr_tac [] _ = no_tac
wenzelm@27867
   824
  | comp_incr_tac (th :: ths) i =
wenzelm@27867
   825
      (fn st => Goal.compose_hhf_tac (Drule.incr_indexes st th) i st) APPEND comp_incr_tac ths i;
wenzelm@18042
   826
wenzelm@21687
   827
fun fact_tac facts = Goal.norm_hhf_tac THEN' comp_incr_tac facts;
wenzelm@18122
   828
wenzelm@27867
   829
fun potential_facts ctxt prop =
wenzelm@27867
   830
  Facts.could_unify (facts_of ctxt) (Term.strip_all_body prop);
wenzelm@27867
   831
wenzelm@27867
   832
fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) => fact_tac (potential_facts ctxt goal) i);
wenzelm@18042
   833
wenzelm@18042
   834
wenzelm@6091
   835
(* get_thm(s) *)
wenzelm@5819
   836
wenzelm@26361
   837
local
wenzelm@26361
   838
wenzelm@26687
   839
fun retrieve_thms pick ctxt (Facts.Fact s) =
wenzelm@16501
   840
      let
wenzelm@27867
   841
        val (_, pos) = Syntax.read_token s;
wenzelm@42502
   842
        val prop =
wenzelm@42502
   843
          Syntax.read_prop (ctxt |> set_mode mode_default |> allow_dummies) s
wenzelm@24511
   844
          |> singleton (Variable.polymorphic ctxt);
wenzelm@42502
   845
        fun err msg = error (msg ^ Position.str_of pos ^ ":\n" ^ Syntax.string_of_term ctxt prop);
wenzelm@27867
   846
wenzelm@42502
   847
        val (prop', _) = Term.replace_dummy_patterns prop (Variable.maxidx_of ctxt + 1);
wenzelm@27867
   848
        fun prove_fact th =
wenzelm@42502
   849
          Goal.prove ctxt [] [] prop' (K (ALLGOALS (fact_tac [th])));
wenzelm@42502
   850
        val results = map_filter (try prove_fact) (potential_facts ctxt prop');
wenzelm@27867
   851
        val res =
wenzelm@42502
   852
          (case distinct Thm.eq_thm_prop results of
wenzelm@42502
   853
            [res] => res
wenzelm@42502
   854
          | [] => err "Failed to retrieve literal fact"
wenzelm@42502
   855
          | _ => err "Ambiguous specification of literal fact");
wenzelm@27867
   856
      in pick "" [res] end
wenzelm@26687
   857
  | retrieve_thms pick ctxt xthmref =
wenzelm@18042
   858
      let
wenzelm@18042
   859
        val thy = theory_of ctxt;
wenzelm@26284
   860
        val local_facts = facts_of ctxt;
wenzelm@26673
   861
        val thmref = Facts.map_name_of_ref (Facts.intern local_facts) xthmref;
wenzelm@26336
   862
        val name = Facts.name_of_ref thmref;
wenzelm@27821
   863
        val pos = Facts.pos_of_ref xthmref;
wenzelm@24012
   864
        val thms =
wenzelm@24012
   865
          if name = "" then [Thm.transfer thy Drule.dummy_thm]
wenzelm@24012
   866
          else
wenzelm@26393
   867
            (case Facts.lookup (Context.Proof ctxt) local_facts name of
wenzelm@39507
   868
              SOME (_, ths) =>
wenzelm@42378
   869
                (Context_Position.report ctxt pos
wenzelm@42379
   870
                  (Name_Space.markup (Facts.space_of local_facts) name);
wenzelm@42378
   871
                 map (Thm.transfer thy) (Facts.select thmref ths))
wenzelm@39557
   872
            | NONE => Global_Theory.get_fact (Context.Proof ctxt) thy xthmref);
wenzelm@24012
   873
      in pick name thms end;
wenzelm@5819
   874
wenzelm@26361
   875
in
wenzelm@26346
   876
wenzelm@26687
   877
val get_fact = retrieve_thms (K I);
wenzelm@26687
   878
val get_fact_single = retrieve_thms Facts.the_single;
wenzelm@26346
   879
wenzelm@26361
   880
fun get_thms ctxt = get_fact ctxt o Facts.named;
wenzelm@26361
   881
fun get_thm ctxt = get_fact_single ctxt o Facts.named;
wenzelm@26361
   882
wenzelm@26361
   883
end;
wenzelm@5819
   884
wenzelm@5819
   885
wenzelm@26284
   886
(* facts *)
wenzelm@5819
   887
wenzelm@28082
   888
local
wenzelm@28082
   889
haftmann@28965
   890
fun update_thms _ (b, NONE) ctxt = ctxt |> map_facts (Facts.del (full_name ctxt b))
haftmann@28861
   891
  | update_thms do_props (b, SOME ths) ctxt = ctxt |> map_facts
wenzelm@42375
   892
      (Facts.add_local ctxt do_props (naming_of ctxt) (b, ths) #> snd);
wenzelm@5819
   893
wenzelm@30761
   894
in
wenzelm@30761
   895
wenzelm@30761
   896
fun note_thmss kind = fold_map (fn ((b, more_attrs), raw_facts) => fn ctxt =>
wenzelm@5819
   897
  let
haftmann@28965
   898
    val name = full_name ctxt b;
wenzelm@39557
   899
    val facts = Global_Theory.name_thmss false name raw_facts;
wenzelm@21443
   900
    fun app (th, attrs) x =
wenzelm@30761
   901
      swap (Library.foldl_map
wenzelm@30761
   902
        (Thm.proof_attributes (surround (Thm.kind kind) (attrs @ more_attrs))) (x, th));
wenzelm@21443
   903
    val (res, ctxt') = fold_map app facts ctxt;
wenzelm@39557
   904
    val thms = Global_Theory.name_thms false false name (flat res);
wenzelm@24388
   905
    val Mode {stmt, ...} = get_mode ctxt;
haftmann@28861
   906
  in ((name, thms), ctxt' |> update_thms stmt (b, SOME thms)) end);
wenzelm@12711
   907
wenzelm@28417
   908
fun put_thms do_props thms ctxt = ctxt
wenzelm@28417
   909
  |> map_naming (K local_naming)
wenzelm@33383
   910
  |> Context_Position.set_visible false
haftmann@28965
   911
  |> update_thms do_props (apfst Binding.name thms)
wenzelm@33383
   912
  |> Context_Position.restore_visible ctxt
wenzelm@28417
   913
  |> restore_naming ctxt;
wenzelm@28082
   914
wenzelm@12711
   915
end;
wenzelm@9196
   916
wenzelm@5819
   917
wenzelm@5819
   918
wenzelm@35680
   919
(** basic logical entities **)
wenzelm@17360
   920
wenzelm@8096
   921
(* variables *)
wenzelm@8096
   922
wenzelm@19897
   923
fun declare_var (x, opt_T, mx) ctxt =
wenzelm@42287
   924
  let val T = (case opt_T of SOME T => T | NONE => Mixfix.mixfixT mx)
wenzelm@20163
   925
  in ((x, T, mx), ctxt |> Variable.declare_constraints (Free (x, T))) end;
wenzelm@19897
   926
wenzelm@10381
   927
local
wenzelm@10381
   928
wenzelm@25353
   929
fun prep_vars prep_typ internal =
wenzelm@35129
   930
  fold_map (fn (b, raw_T, mx) => fn ctxt =>
wenzelm@18672
   931
    let
wenzelm@42494
   932
      val x = Variable.check_name b;
wenzelm@42290
   933
      val _ = Lexicon.is_identifier (no_skolem internal x) orelse
wenzelm@42381
   934
        error ("Illegal variable name: " ^ Binding.print b);
wenzelm@12504
   935
wenzelm@18672
   936
      fun cond_tvars T =
wenzelm@18672
   937
        if internal then T
wenzelm@18678
   938
        else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
wenzelm@24277
   939
      val opt_T = Option.map (cond_tvars o cert_typ ctxt o prep_typ ctxt) raw_T;
wenzelm@35129
   940
      val (_, ctxt') = ctxt |> declare_var (x, opt_T, mx);
wenzelm@35129
   941
    in ((b, opt_T, mx), ctxt') end);
wenzelm@8096
   942
wenzelm@10381
   943
in
wenzelm@10381
   944
wenzelm@45427
   945
val read_vars = prep_vars Syntax.read_typ false;
wenzelm@25353
   946
val cert_vars = prep_vars (K I) true;
wenzelm@8096
   947
wenzelm@10381
   948
end;
wenzelm@10381
   949
wenzelm@8096
   950
wenzelm@21744
   951
(* notation *)
wenzelm@21744
   952
wenzelm@24949
   953
local
wenzelm@24949
   954
wenzelm@35429
   955
fun type_syntax (Type (c, args), mx) =
wenzelm@42290
   956
      SOME (Local_Syntax.Type, (Lexicon.mark_type c, Mixfix.make_type (length args), mx))
wenzelm@35412
   957
  | type_syntax _ = NONE;
wenzelm@35412
   958
wenzelm@35412
   959
fun const_syntax _ (Free (x, T), mx) = SOME (Local_Syntax.Fixed, (x, T, mx))
wenzelm@24949
   960
  | const_syntax ctxt (Const (c, _), mx) =
wenzelm@35255
   961
      (case try (Consts.type_scheme (consts_of ctxt)) c of
wenzelm@42290
   962
        SOME T => SOME (Local_Syntax.Const, (Lexicon.mark_const c, T, mx))
wenzelm@35255
   963
      | NONE => NONE)
wenzelm@24949
   964
  | const_syntax _ _ = NONE;
wenzelm@24949
   965
wenzelm@35412
   966
fun gen_notation syntax add mode args ctxt =
wenzelm@35412
   967
  ctxt |> map_syntax
wenzelm@35412
   968
    (Local_Syntax.update_modesyntax (theory_of ctxt) add mode (map_filter (syntax ctxt) args));
wenzelm@35412
   969
wenzelm@24949
   970
in
wenzelm@21744
   971
wenzelm@35412
   972
val type_notation = gen_notation (K type_syntax);
wenzelm@35412
   973
val notation = gen_notation const_syntax;
wenzelm@35412
   974
wenzelm@38328
   975
fun target_type_notation add mode args phi =
wenzelm@35412
   976
  let
wenzelm@35412
   977
    val args' = args |> map_filter (fn (T, mx) =>
wenzelm@35412
   978
      let
wenzelm@35412
   979
        val T' = Morphism.typ phi T;
wenzelm@35412
   980
        val similar = (case (T, T') of (Type (c, _), Type (c', _)) => c = c' | _ => false);
wenzelm@35412
   981
      in if similar then SOME (T', mx) else NONE end);
wenzelm@35412
   982
  in Context.mapping (Sign.type_notation add mode args') (type_notation add mode args') end;
wenzelm@24949
   983
wenzelm@24949
   984
fun target_notation add mode args phi =
wenzelm@33537
   985
  let
wenzelm@33537
   986
    val args' = args |> map_filter (fn (t, mx) =>
wenzelm@33537
   987
      let val t' = Morphism.term phi t
wenzelm@33537
   988
      in if Term.aconv_untyped (t, t') then SOME (t', mx) else NONE end);
wenzelm@24949
   989
  in Context.mapping (Sign.notation add mode args') (notation add mode args') end;
wenzelm@24949
   990
wenzelm@35680
   991
end;
wenzelm@35412
   992
wenzelm@35680
   993
wenzelm@35680
   994
(* aliases *)
wenzelm@35680
   995
wenzelm@36450
   996
fun class_alias b c ctxt = (map_tsig o apfst) (Type.class_alias (naming_of ctxt) b c) ctxt;
wenzelm@36450
   997
fun type_alias b c ctxt = (map_tsig o apfst) (Type.type_alias (naming_of ctxt) b c) ctxt;
wenzelm@35680
   998
fun const_alias b c ctxt = (map_consts o apfst) (Consts.alias (naming_of ctxt) b c) ctxt;
wenzelm@21744
   999
wenzelm@21744
  1000
wenzelm@24767
  1001
(* local constants *)
wenzelm@24767
  1002
wenzelm@24767
  1003
fun add_const_constraint (c, opt_T) ctxt =
wenzelm@24767
  1004
  let
wenzelm@24767
  1005
    fun prepT raw_T =
wenzelm@24767
  1006
      let val T = cert_typ ctxt raw_T
wenzelm@24767
  1007
      in cert_term ctxt (Const (c, T)); T end;
haftmann@25039
  1008
  in ctxt |> (map_consts o apfst) (Consts.constrain (c, Option.map prepT opt_T)) end;
wenzelm@19001
  1009
wenzelm@33173
  1010
fun add_abbrev mode (b, raw_t) ctxt =
wenzelm@19001
  1011
  let
wenzelm@24675
  1012
    val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
wenzelm@42381
  1013
      handle ERROR msg => cat_error msg ("in constant abbreviation " ^ Binding.print b);
wenzelm@20008
  1014
    val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
wenzelm@21807
  1015
    val ((lhs, rhs), consts') = consts_of ctxt
wenzelm@42375
  1016
      |> Consts.abbreviate ctxt (tsig_of ctxt) (naming_of ctxt) mode (b, t);
wenzelm@19001
  1017
  in
wenzelm@19001
  1018
    ctxt
haftmann@25039
  1019
    |> (map_consts o apfst) (K consts')
wenzelm@21803
  1020
    |> Variable.declare_term rhs
wenzelm@21803
  1021
    |> pair (lhs, rhs)
wenzelm@21704
  1022
  end;
wenzelm@19001
  1023
wenzelm@25052
  1024
fun revert_abbrev mode c = (map_consts o apfst) (Consts.revert_abbrev mode c);
wenzelm@25052
  1025
wenzelm@19001
  1026
wenzelm@18672
  1027
(* fixes *)
wenzelm@5819
  1028
wenzelm@30763
  1029
fun add_fixes raw_vars ctxt =
wenzelm@8096
  1030
  let
wenzelm@42491
  1031
    val thy = theory_of ctxt;
wenzelm@42491
  1032
    val vars = #1 (cert_vars raw_vars ctxt);
wenzelm@42491
  1033
  in
wenzelm@42491
  1034
    ctxt
wenzelm@42491
  1035
    |> Variable.add_fixes_binding (map #1 vars)
wenzelm@42491
  1036
    |-> (fn xs =>
wenzelm@42491
  1037
      fold_map declare_var (map2 (fn x => fn (_, T, mx) => (x, T, mx)) xs vars)
wenzelm@42491
  1038
      #-> (map_syntax o Local_Syntax.add_syntax thy o map (pair Local_Syntax.Fixed))
wenzelm@42491
  1039
      #> pair xs)
wenzelm@42491
  1040
  end;
wenzelm@5819
  1041
wenzelm@9291
  1042
wenzelm@9291
  1043
wenzelm@18672
  1044
(** assumptions **)
wenzelm@18187
  1045
wenzelm@20209
  1046
local
wenzelm@20209
  1047
wenzelm@20209
  1048
fun gen_assms prepp exp args ctxt =
wenzelm@20209
  1049
  let
wenzelm@20234
  1050
    val cert = Thm.cterm_of (theory_of ctxt);
wenzelm@45327
  1051
    val ((propss, _), ctxt1) = prepp (map snd args) ctxt;
wenzelm@20234
  1052
    val _ = Variable.warn_extra_tfrees ctxt ctxt1;
wenzelm@20234
  1053
    val (premss, ctxt2) = fold_burrow (Assumption.add_assms exp o map cert) propss ctxt1;
wenzelm@20234
  1054
  in
wenzelm@20234
  1055
    ctxt2
wenzelm@20234
  1056
    |> auto_bind_facts (flat propss)
wenzelm@33644
  1057
    |> note_thmss "" (map fst args ~~ map (map (fn th => ([th], []))) premss)
wenzelm@20234
  1058
  end;
wenzelm@20209
  1059
wenzelm@20209
  1060
in
wenzelm@20209
  1061
wenzelm@45327
  1062
val add_assms = gen_assms bind_propp;
wenzelm@45327
  1063
val add_assms_i = gen_assms bind_propp_i;
wenzelm@20209
  1064
wenzelm@20209
  1065
end;
wenzelm@20209
  1066
wenzelm@20209
  1067
wenzelm@5819
  1068
wenzelm@8373
  1069
(** cases **)
wenzelm@8373
  1070
wenzelm@16147
  1071
local
wenzelm@16147
  1072
wenzelm@16668
  1073
fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
wenzelm@16147
  1074
wenzelm@18476
  1075
fun add_case _ ("", _) cases = cases
wenzelm@18476
  1076
  | add_case _ (name, NONE) cases = rem_case name cases
wenzelm@18476
  1077
  | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
wenzelm@16147
  1078
wenzelm@18678
  1079
fun prep_case name fxs c =
wenzelm@18609
  1080
  let
wenzelm@18609
  1081
    fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
wenzelm@18609
  1082
      | replace [] ys = ys
wenzelm@18678
  1083
      | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
wenzelm@33368
  1084
    val Rule_Cases.Case {fixes, assumes, binds, cases} = c;
wenzelm@18609
  1085
    val fixes' = replace fxs fixes;
wenzelm@18609
  1086
    val binds' = map drop_schematic binds;
wenzelm@18609
  1087
  in
wenzelm@18609
  1088
    if null (fold (Term.add_tvarsT o snd) fixes []) andalso
wenzelm@18609
  1089
      null (fold (fold Term.add_vars o snd) assumes []) then
wenzelm@33368
  1090
        Rule_Cases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
wenzelm@18678
  1091
    else error ("Illegal schematic variable(s) in case " ^ quote name)
wenzelm@18609
  1092
  end;
wenzelm@18609
  1093
wenzelm@42501
  1094
fun fix (b, T) ctxt =
wenzelm@42501
  1095
  let val ([x], ctxt') = add_fixes [(b, SOME T, NoSyn)] ctxt
wenzelm@42501
  1096
  in (Free (x, T), ctxt') end;
wenzelm@18672
  1097
wenzelm@16147
  1098
in
wenzelm@16147
  1099
wenzelm@18672
  1100
fun add_cases is_proper = map_cases o fold (add_case is_proper);
wenzelm@18609
  1101
wenzelm@18609
  1102
fun case_result c ctxt =
wenzelm@18609
  1103
  let
wenzelm@33368
  1104
    val Rule_Cases.Case {fixes, ...} = c;
wenzelm@18672
  1105
    val (ts, ctxt') = ctxt |> fold_map fix fixes;
wenzelm@33368
  1106
    val Rule_Cases.Case {assumes, binds, cases, ...} = Rule_Cases.apply ts c;
wenzelm@18609
  1107
  in
wenzelm@18609
  1108
    ctxt'
wenzelm@30757
  1109
    |> bind_terms (map drop_schematic binds)
wenzelm@18609
  1110
    |> add_cases true (map (apsnd SOME) cases)
wenzelm@18609
  1111
    |> pair (assumes, (binds, cases))
wenzelm@18609
  1112
  end;
wenzelm@18609
  1113
wenzelm@18609
  1114
val apply_case = apfst fst oo case_result;
wenzelm@18609
  1115
wenzelm@16540
  1116
fun get_case ctxt name xs =
wenzelm@17184
  1117
  (case AList.lookup (op =) (cases_of ctxt) name of
wenzelm@18678
  1118
    NONE => error ("Unknown case: " ^ quote name)
wenzelm@18678
  1119
  | SOME (c, _) => prep_case name xs c);
wenzelm@8373
  1120
wenzelm@16147
  1121
end;
wenzelm@8373
  1122
wenzelm@8373
  1123
wenzelm@8373
  1124
wenzelm@10810
  1125
(** print context information **)
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@42358
  1142
    val abbrevs =
wenzelm@42358
  1143
      Name_Space.extern_table ctxt (space, Symtab.make (Symtab.fold add_abbr consts []));
wenzelm@18971
  1144
  in
wenzelm@35141
  1145
    if null abbrevs then []
wenzelm@21728
  1146
    else [Pretty.big_list "abbreviations:" (map (pretty_term_abbrev ctxt o #2) abbrevs)]
wenzelm@18971
  1147
  end;
wenzelm@18971
  1148
wenzelm@21728
  1149
val print_abbrevs = Pretty.writeln o Pretty.chunks o pretty_abbrevs true;
wenzelm@21728
  1150
wenzelm@18971
  1151
wenzelm@10810
  1152
(* term bindings *)
wenzelm@10810
  1153
wenzelm@16540
  1154
fun pretty_binds ctxt =
wenzelm@10810
  1155
  let
wenzelm@19897
  1156
    val binds = Variable.binds_of ctxt;
wenzelm@21728
  1157
    fun prt_bind (xi, (T, t)) = pretty_term_abbrev ctxt (Logic.mk_equals (Var (xi, T), t));
wenzelm@10810
  1158
  in
wenzelm@35141
  1159
    if Vartab.is_empty binds then []
wenzelm@15758
  1160
    else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
wenzelm@10810
  1161
  end;
wenzelm@10810
  1162
wenzelm@10810
  1163
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@10810
  1164
wenzelm@10810
  1165
wenzelm@10810
  1166
(* local theorems *)
wenzelm@10810
  1167
wenzelm@16540
  1168
fun pretty_lthms ctxt =
wenzelm@20012
  1169
  let
wenzelm@26284
  1170
    val local_facts = facts_of ctxt;
wenzelm@26284
  1171
    val props = Facts.props local_facts;
wenzelm@26673
  1172
    val facts =
wenzelm@35141
  1173
      (if null props then [] else [("<unnamed>", props)]) @
wenzelm@28212
  1174
      Facts.dest_static [] local_facts;
wenzelm@20012
  1175
  in
wenzelm@35141
  1176
    if null facts then []
wenzelm@28212
  1177
    else [Pretty.big_list "facts:" (map #1 (sort_wrt (#1 o #2) (map (`(pretty_fact ctxt)) facts)))]
wenzelm@20012
  1178
  end;
wenzelm@10810
  1179
wenzelm@12057
  1180
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
wenzelm@10810
  1181
wenzelm@10810
  1182
wenzelm@10810
  1183
(* local contexts *)
wenzelm@10810
  1184
wenzelm@26722
  1185
local
wenzelm@26722
  1186
wenzelm@26722
  1187
fun pretty_case (name, (fixes, ((asms, (lets, cs)), ctxt))) =
wenzelm@10810
  1188
  let
wenzelm@24922
  1189
    val prt_term = Syntax.pretty_term ctxt;
wenzelm@12057
  1190
wenzelm@10810
  1191
    fun prt_let (xi, t) = Pretty.block
wenzelm@10818
  1192
      [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
wenzelm@10810
  1193
        Pretty.quote (prt_term t)];
wenzelm@10810
  1194
wenzelm@13425
  1195
    fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
wenzelm@13425
  1196
      ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
wenzelm@13425
  1197
wenzelm@10810
  1198
    fun prt_sect _ _ _ [] = []
wenzelm@35141
  1199
      | prt_sect s sep prt xs =
wenzelm@35141
  1200
          [Pretty.block (Pretty.breaks (Pretty.str s ::
wenzelm@35141
  1201
            flat (separate sep (map (single o prt) xs))))];
wenzelm@26722
  1202
  in
wenzelm@26722
  1203
    Pretty.block (Pretty.fbreaks
wenzelm@10810
  1204
      (Pretty.str (name ^ ":") ::
wenzelm@42496
  1205
        prt_sect "fix" [] (Pretty.str o Binding.name_of o fst) fixes @
wenzelm@10810
  1206
        prt_sect "let" [Pretty.str "and"] prt_let
wenzelm@19482
  1207
          (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
wenzelm@13425
  1208
        (if forall (null o #2) asms then []
wenzelm@18609
  1209
          else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
wenzelm@26722
  1210
        prt_sect "subcases:" [] (Pretty.str o fst) cs))
wenzelm@26722
  1211
  end;
wenzelm@16540
  1212
wenzelm@26722
  1213
in
wenzelm@26722
  1214
wenzelm@26722
  1215
fun pretty_cases ctxt =
wenzelm@26722
  1216
  let
wenzelm@18476
  1217
    fun add_case (_, (_, false)) = I
wenzelm@33368
  1218
      | add_case (name, (c as Rule_Cases.Case {fixes, ...}, true)) =
wenzelm@26722
  1219
          cons (name, (fixes, case_result c ctxt));
wenzelm@18476
  1220
    val cases = fold add_case (cases_of ctxt) [];
wenzelm@10810
  1221
  in
wenzelm@35141
  1222
    if null cases then []
wenzelm@26722
  1223
    else [Pretty.big_list "cases:" (map pretty_case cases)]
wenzelm@10810
  1224
  end;
wenzelm@10810
  1225
wenzelm@10810
  1226
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@10810
  1227
wenzelm@26722
  1228
end;
wenzelm@26722
  1229
wenzelm@10810
  1230
wenzelm@12057
  1231
(* core context *)
wenzelm@10810
  1232
wenzelm@42717
  1233
val debug = Config.bool (Config.declare "Proof_Context.debug" (K (Config.Bool false)));
wenzelm@42717
  1234
val verbose = Config.bool (Config.declare "Proof_Context.verbose" (K (Config.Bool false)));
wenzelm@10810
  1235
wenzelm@18672
  1236
fun pretty_ctxt ctxt =
wenzelm@42717
  1237
  if not (Config.get ctxt debug) then []
wenzelm@20310
  1238
  else
wenzelm@20310
  1239
    let
wenzelm@24922
  1240
      val prt_term = Syntax.pretty_term ctxt;
wenzelm@12057
  1241
wenzelm@20310
  1242
      (*structures*)
wenzelm@35429
  1243
      val {structs, ...} = Local_Syntax.idents_of (syntax_of ctxt);
wenzelm@35139
  1244
      val prt_structs =
wenzelm@35139
  1245
        if null structs then []
wenzelm@20310
  1246
        else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
wenzelm@20310
  1247
          Pretty.commas (map Pretty.str structs))];
wenzelm@12093
  1248
wenzelm@20310
  1249
      (*fixes*)
wenzelm@20310
  1250
      fun prt_fix (x, x') =
wenzelm@20310
  1251
        if x = x' then Pretty.str x
wenzelm@20310
  1252
        else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@20310
  1253
      val fixes =
wenzelm@42488
  1254
        filter_out ((can Name.dest_internal orf member (op =) structs) o #1)
wenzelm@42488
  1255
          (Variable.dest_fixes ctxt);
wenzelm@35139
  1256
      val prt_fixes =
wenzelm@35139
  1257
        if null fixes then []
wenzelm@20310
  1258
        else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@20310
  1259
          Pretty.commas (map prt_fix fixes))];
wenzelm@12057
  1260
wenzelm@20310
  1261
      (*prems*)
wenzelm@30473
  1262
      val prems = Assumption.all_prems_of ctxt;
wenzelm@35139
  1263
      val prt_prems =
wenzelm@35139
  1264
        if null prems then []
wenzelm@39165
  1265
        else [Pretty.big_list "prems:" (map (Display.pretty_thm ctxt) prems)];
wenzelm@20310
  1266
    in prt_structs @ prt_fixes @ prt_prems end;
wenzelm@10810
  1267
wenzelm@10810
  1268
wenzelm@10810
  1269
(* main context *)
wenzelm@10810
  1270
wenzelm@16540
  1271
fun pretty_context ctxt =
wenzelm@10810
  1272
  let
wenzelm@42717
  1273
    val verbose = Config.get ctxt verbose;
wenzelm@42717
  1274
    fun verb f x = if verbose then f (x ()) else [];
wenzelm@35141
  1275
wenzelm@24922
  1276
    val prt_term = Syntax.pretty_term ctxt;
wenzelm@24922
  1277
    val prt_typ = Syntax.pretty_typ ctxt;
wenzelm@24922
  1278
    val prt_sort = Syntax.pretty_sort ctxt;
wenzelm@10810
  1279
wenzelm@10810
  1280
    (*theory*)
wenzelm@12057
  1281
    val pretty_thy = Pretty.block
wenzelm@17384
  1282
      [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
wenzelm@10810
  1283
wenzelm@10810
  1284
    (*defaults*)
wenzelm@10810
  1285
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@10810
  1286
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@10810
  1287
wenzelm@10810
  1288
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@10810
  1289
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@10810
  1290
wenzelm@10810
  1291
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@10810
  1292
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@10810
  1293
wenzelm@10810
  1294
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@10810
  1295
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@16540
  1296
wenzelm@20163
  1297
    val (types, sorts) = Variable.constraints_of ctxt;
wenzelm@10810
  1298
  in
wenzelm@18609
  1299
    verb single (K pretty_thy) @
wenzelm@18672
  1300
    pretty_ctxt ctxt @
wenzelm@21728
  1301
    verb (pretty_abbrevs false) (K ctxt) @
wenzelm@10810
  1302
    verb pretty_binds (K ctxt) @
wenzelm@12057
  1303
    verb pretty_lthms (K ctxt) @
wenzelm@10810
  1304
    verb pretty_cases (K ctxt) @
wenzelm@18609
  1305
    verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@20163
  1306
    verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
wenzelm@10810
  1307
  end;
wenzelm@10810
  1308
wenzelm@5819
  1309
end;
wenzelm@35429
  1310
wenzelm@42360
  1311
val show_abbrevs = Proof_Context.show_abbrevs;
wenzelm@40879
  1312