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