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