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