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