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