src/Pure/Isar/proof_context.ML
author wenzelm
Wed Apr 28 11:41:27 2010 +0200 (2010-04-28 ago)
changeset 36451 ddc965e172c4
parent 36450 62eaaffe6e47
child 36503 bd4e2821482a
permissions -rw-r--r--
localized default sort;
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@20310
    12
  val init: theory -> Proof.context
wenzelm@24388
    13
  type mode
wenzelm@24501
    14
  val mode_default: mode
wenzelm@24501
    15
  val mode_stmt: mode
wenzelm@24501
    16
  val mode_pattern: mode
wenzelm@24501
    17
  val mode_schematic: mode
wenzelm@24501
    18
  val mode_abbrev: mode
wenzelm@24388
    19
  val set_mode: mode -> Proof.context -> Proof.context
wenzelm@24388
    20
  val get_mode: Proof.context -> mode
wenzelm@24388
    21
  val restore_mode: Proof.context -> Proof.context -> Proof.context
wenzelm@27286
    22
  val abbrev_mode: Proof.context -> bool
wenzelm@21667
    23
  val set_stmt: bool -> Proof.context -> Proof.context
wenzelm@33165
    24
  val local_naming: Name_Space.naming
wenzelm@33165
    25
  val map_naming: (Name_Space.naming -> Name_Space.naming) -> Proof.context -> Proof.context
wenzelm@33095
    26
  val naming_of: Proof.context -> Name_Space.naming
wenzelm@33165
    27
  val restore_naming: Proof.context -> Proof.context -> Proof.context
haftmann@29581
    28
  val full_name: Proof.context -> binding -> string
wenzelm@35111
    29
  val syn_of: Proof.context -> Syntax.syntax
wenzelm@35669
    30
  val tsig_of: Proof.context -> Type.tsig
wenzelm@36451
    31
  val set_defsort: sort -> Proof.context -> Proof.context
wenzelm@35680
    32
  val default_sort: Proof.context -> indexname -> sort
wenzelm@20310
    33
  val consts_of: Proof.context -> Consts.T
haftmann@24752
    34
  val the_const_constraint: Proof.context -> string -> typ
wenzelm@20784
    35
  val set_syntax_mode: Syntax.mode -> Proof.context -> Proof.context
wenzelm@20310
    36
  val restore_syntax_mode: Proof.context -> Proof.context -> Proof.context
wenzelm@26284
    37
  val facts_of: Proof.context -> Facts.T
blanchet@34982
    38
  val cases_of: Proof.context -> (string * (Rule_Cases.T * bool)) list
wenzelm@27259
    39
  val transfer_syntax: theory -> Proof.context -> Proof.context
wenzelm@20310
    40
  val transfer: theory -> Proof.context -> Proof.context
wenzelm@20367
    41
  val theory: (theory -> theory) -> Proof.context -> Proof.context
wenzelm@20367
    42
  val theory_result: (theory -> 'a * theory) -> Proof.context -> 'a * Proof.context
wenzelm@28212
    43
  val extern_fact: Proof.context -> string -> xstring
wenzelm@21728
    44
  val pretty_term_abbrev: Proof.context -> term -> Pretty.T
wenzelm@30723
    45
  val pretty_fact_aux: Proof.context -> bool -> string * thm list -> Pretty.T
wenzelm@20310
    46
  val pretty_fact: Proof.context -> string * thm list -> Pretty.T
wenzelm@35669
    47
  val read_class: Proof.context -> xstring -> class
wenzelm@35669
    48
  val read_arity: Proof.context -> xstring * string list * string -> arity
wenzelm@35669
    49
  val cert_arity: Proof.context -> arity -> arity
wenzelm@20310
    50
  val read_typ: Proof.context -> string -> typ
wenzelm@20310
    51
  val read_typ_syntax: Proof.context -> string -> typ
wenzelm@20310
    52
  val read_typ_abbrev: Proof.context -> string -> typ
wenzelm@20310
    53
  val cert_typ: Proof.context -> typ -> typ
wenzelm@20310
    54
  val cert_typ_syntax: Proof.context -> typ -> typ
wenzelm@20310
    55
  val cert_typ_abbrev: Proof.context -> typ -> typ
wenzelm@20310
    56
  val get_skolem: Proof.context -> string -> string
wenzelm@20310
    57
  val revert_skolem: Proof.context -> string -> string
wenzelm@25328
    58
  val infer_type: Proof.context -> string -> typ
wenzelm@28082
    59
  val inferred_param: string -> Proof.context -> typ * Proof.context
wenzelm@25328
    60
  val inferred_fixes: Proof.context -> (string * typ) list * Proof.context
wenzelm@35360
    61
  val read_type_name: Proof.context -> bool -> string -> typ
wenzelm@35669
    62
  val read_type_name_proper: Proof.context -> bool -> string -> typ
wenzelm@35399
    63
  val read_const_proper: Proof.context -> bool -> string -> term
wenzelm@35399
    64
  val read_const: Proof.context -> bool -> string -> term
wenzelm@27259
    65
  val allow_dummies: Proof.context -> Proof.context
wenzelm@36152
    66
  val check_tvar: Proof.context -> indexname * sort -> indexname * sort
wenzelm@36152
    67
  val check_tfree: Proof.context -> string * sort -> string * sort
wenzelm@22763
    68
  val decode_term: Proof.context -> term -> term
wenzelm@25406
    69
  val standard_infer_types: Proof.context -> term list -> term list
wenzelm@24684
    70
  val read_term_pattern: Proof.context -> string -> term
wenzelm@24684
    71
  val read_term_schematic: Proof.context -> string -> term
wenzelm@24684
    72
  val read_term_abbrev: Proof.context -> string -> term
wenzelm@25345
    73
  val expand_abbrevs: Proof.context -> term -> term
wenzelm@20310
    74
  val cert_term: Proof.context -> term -> term
wenzelm@20310
    75
  val cert_prop: Proof.context -> term -> term
wenzelm@35616
    76
  val def_type: Proof.context -> indexname -> typ option
wenzelm@20310
    77
  val goal_export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@20310
    78
  val export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@21531
    79
  val export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@28396
    80
  val norm_export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@30757
    81
  val bind_terms: (indexname * term option) list -> Proof.context -> Proof.context
wenzelm@20310
    82
  val auto_bind_goal: term list -> Proof.context -> Proof.context
wenzelm@20310
    83
  val auto_bind_facts: term list -> Proof.context -> Proof.context
wenzelm@20310
    84
  val match_bind: bool -> (string list * string) list -> Proof.context -> term list * Proof.context
wenzelm@20310
    85
  val match_bind_i: bool -> (term list * term) list -> Proof.context -> term list * Proof.context
wenzelm@20310
    86
  val read_propp: Proof.context * (string * string list) list list
wenzelm@20310
    87
    -> Proof.context * (term * term list) list list
wenzelm@20310
    88
  val cert_propp: Proof.context * (term * term list) list list
wenzelm@20310
    89
    -> Proof.context * (term * term list) list list
wenzelm@20310
    90
  val read_propp_schematic: Proof.context * (string * string list) list list
wenzelm@20310
    91
    -> Proof.context * (term * term list) list list
wenzelm@20310
    92
  val cert_propp_schematic: Proof.context * (term * term list) list list
wenzelm@20310
    93
    -> Proof.context * (term * term list) list list
wenzelm@20310
    94
  val bind_propp: Proof.context * (string * string list) list list
wenzelm@20310
    95
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@20310
    96
  val bind_propp_i: Proof.context * (term * term list) list list
wenzelm@20310
    97
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@20310
    98
  val bind_propp_schematic: Proof.context * (string * string list) list list
wenzelm@20310
    99
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@20310
   100
  val bind_propp_schematic_i: Proof.context * (term * term list) list list
wenzelm@20310
   101
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@18042
   102
  val fact_tac: thm list -> int -> tactic
wenzelm@20310
   103
  val some_fact_tac: Proof.context -> int -> tactic
wenzelm@26346
   104
  val get_fact: Proof.context -> Facts.ref -> thm list
wenzelm@26346
   105
  val get_fact_single: Proof.context -> Facts.ref -> thm
wenzelm@26346
   106
  val get_thms: Proof.context -> xstring -> thm list
wenzelm@26346
   107
  val get_thm: Proof.context -> xstring -> thm
wenzelm@30761
   108
  val note_thmss: string -> (Thm.binding * (thm list * attribute list) list) list ->
wenzelm@30211
   109
    Proof.context -> (string * thm list) list * Proof.context
wenzelm@28082
   110
  val put_thms: bool -> string * thm list option -> Proof.context -> Proof.context
haftmann@29581
   111
  val read_vars: (binding * string option * mixfix) list -> Proof.context ->
haftmann@29581
   112
    (binding * typ option * mixfix) list * Proof.context
haftmann@29581
   113
  val cert_vars: (binding * typ option * mixfix) list -> Proof.context ->
haftmann@29581
   114
    (binding * typ option * mixfix) list * Proof.context
wenzelm@30763
   115
  val add_fixes: (binding * typ option * mixfix) list -> Proof.context ->
wenzelm@30763
   116
    string list * Proof.context
wenzelm@20310
   117
  val auto_fixes: Proof.context * (term list list * 'a) -> Proof.context * (term list list * 'a)
wenzelm@20310
   118
  val bind_fixes: string list -> Proof.context -> (term -> term) * Proof.context
wenzelm@20234
   119
  val add_assms: Assumption.export ->
wenzelm@30211
   120
    (Thm.binding * (string * string list) list) list ->
wenzelm@28082
   121
    Proof.context -> (string * thm list) list * Proof.context
wenzelm@20234
   122
  val add_assms_i: Assumption.export ->
wenzelm@30211
   123
    (Thm.binding * (term * term list) list) list ->
wenzelm@28082
   124
    Proof.context -> (string * thm list) list * Proof.context
wenzelm@33368
   125
  val add_cases: bool -> (string * Rule_Cases.T option) list -> Proof.context -> Proof.context
wenzelm@33368
   126
  val apply_case: Rule_Cases.T -> Proof.context -> (string * term list) list * Proof.context
wenzelm@33368
   127
  val get_case: Proof.context -> string -> string option list -> Rule_Cases.T
wenzelm@35412
   128
  val type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> Proof.context -> Proof.context
wenzelm@24949
   129
  val notation: bool -> Syntax.mode -> (term * mixfix) list -> Proof.context -> Proof.context
wenzelm@35412
   130
  val target_type_notation: bool -> Syntax.mode -> (typ * mixfix) list -> morphism ->
wenzelm@35412
   131
    Context.generic -> Context.generic
wenzelm@24949
   132
  val target_notation: bool -> Syntax.mode -> (term * mixfix) list -> morphism ->
wenzelm@21744
   133
    Context.generic -> Context.generic
wenzelm@35680
   134
  val class_alias: binding -> class -> Proof.context -> Proof.context
wenzelm@35680
   135
  val type_alias: binding -> string -> Proof.context -> Proof.context
wenzelm@35680
   136
  val const_alias: binding -> string -> Proof.context -> Proof.context
wenzelm@24767
   137
  val add_const_constraint: string * typ option -> Proof.context -> Proof.context
wenzelm@33173
   138
  val add_abbrev: string -> binding * term -> Proof.context -> (term * term) * Proof.context
wenzelm@25052
   139
  val revert_abbrev: string -> string -> Proof.context -> Proof.context
wenzelm@20310
   140
  val print_syntax: Proof.context -> unit
wenzelm@21728
   141
  val print_abbrevs: Proof.context -> unit
wenzelm@20310
   142
  val print_binds: Proof.context -> unit
wenzelm@20310
   143
  val print_lthms: Proof.context -> unit
wenzelm@20310
   144
  val print_cases: Proof.context -> unit
wenzelm@32738
   145
  val debug: bool Unsynchronized.ref
wenzelm@35141
   146
  val verbose: bool Unsynchronized.ref
wenzelm@32738
   147
  val prems_limit: int Unsynchronized.ref
wenzelm@20310
   148
  val pretty_ctxt: Proof.context -> Pretty.T list
wenzelm@20310
   149
  val pretty_context: Proof.context -> Pretty.T list
wenzelm@5819
   150
end;
wenzelm@5819
   151
wenzelm@16540
   152
structure ProofContext: PROOF_CONTEXT =
wenzelm@5819
   153
struct
wenzelm@5819
   154
wenzelm@33031
   155
open ProofContext;
wenzelm@12057
   156
wenzelm@7270
   157
wenzelm@24388
   158
(** inner syntax mode **)
wenzelm@24388
   159
wenzelm@24388
   160
datatype mode =
wenzelm@24388
   161
  Mode of
wenzelm@24486
   162
   {stmt: bool,                (*inner statement mode*)
wenzelm@24388
   163
    pattern: bool,             (*pattern binding schematic variables*)
wenzelm@24388
   164
    schematic: bool,           (*term referencing loose schematic variables*)
wenzelm@24388
   165
    abbrev: bool};             (*abbrev mode -- no normalization*)
wenzelm@24388
   166
wenzelm@24486
   167
fun make_mode (stmt, pattern, schematic, abbrev) =
wenzelm@24486
   168
  Mode {stmt = stmt, pattern = pattern, schematic = schematic, abbrev = abbrev};
wenzelm@24388
   169
wenzelm@24486
   170
val mode_default   = make_mode (false, false, false, false);
wenzelm@24486
   171
val mode_stmt      = make_mode (true, false, false, false);
wenzelm@24486
   172
val mode_pattern   = make_mode (false, true, false, false);
wenzelm@24486
   173
val mode_schematic = make_mode (false, false, true, false);
wenzelm@24486
   174
val mode_abbrev    = make_mode (false, false, false, true);
wenzelm@24388
   175
wenzelm@24388
   176
wenzelm@5819
   177
wenzelm@16540
   178
(** Isar proof context information **)
wenzelm@5819
   179
wenzelm@16540
   180
datatype ctxt =
wenzelm@16540
   181
  Ctxt of
wenzelm@36451
   182
   {mode: mode,                  (*inner syntax mode*)
wenzelm@36451
   183
    naming: Name_Space.naming,   (*local naming conventions*)
wenzelm@36451
   184
    syntax: Local_Syntax.T,      (*local syntax*)
wenzelm@36451
   185
    tsig: Type.tsig * Type.tsig, (*local/global type signature -- local name space / defsort only*)
wenzelm@36451
   186
    consts: Consts.T * Consts.T, (*local/global consts -- local name space / abbrevs only*)
wenzelm@36451
   187
    facts: Facts.T,              (*local facts*)
wenzelm@33368
   188
    cases: (string * (Rule_Cases.T * bool)) list};    (*named case contexts*)
wenzelm@5819
   189
wenzelm@36450
   190
fun make_ctxt (mode, naming, syntax, tsig, consts, facts, cases) =
haftmann@26240
   191
  Ctxt {mode = mode, naming = naming, syntax = syntax,
wenzelm@36450
   192
    tsig = tsig, consts = consts, facts = facts, cases = cases};
wenzelm@5819
   193
wenzelm@33095
   194
val local_naming = Name_Space.default_naming |> Name_Space.add_path "local";
wenzelm@19079
   195
wenzelm@33519
   196
structure ContextData = Proof_Data
wenzelm@18672
   197
(
wenzelm@16540
   198
  type T = ctxt;
wenzelm@16540
   199
  fun init thy =
wenzelm@33387
   200
    make_ctxt (mode_default, local_naming, Local_Syntax.init thy,
wenzelm@35669
   201
      (Sign.tsig_of thy, Sign.tsig_of thy),
wenzelm@26284
   202
      (Sign.consts_of thy, Sign.consts_of thy), Facts.empty, []);
wenzelm@18672
   203
);
wenzelm@5819
   204
wenzelm@16540
   205
fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args);
wenzelm@5819
   206
wenzelm@18672
   207
fun map_context f =
wenzelm@36450
   208
  ContextData.map (fn Ctxt {mode, naming, syntax, tsig, consts, facts, cases} =>
wenzelm@36450
   209
    make_ctxt (f (mode, naming, syntax, tsig, consts, facts, cases)));
wenzelm@24388
   210
wenzelm@36450
   211
fun set_mode mode = map_context (fn (_, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   212
  (mode, naming, syntax, tsig, consts, facts, cases));
wenzelm@21443
   213
wenzelm@24388
   214
fun map_mode f =
wenzelm@36450
   215
  map_context (fn (Mode {stmt, pattern, schematic, abbrev}, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   216
    (make_mode (f (stmt, pattern, schematic, abbrev)), naming, syntax, tsig, consts, facts, cases));
wenzelm@18672
   217
wenzelm@19001
   218
fun map_naming f =
wenzelm@36450
   219
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   220
    (mode, f naming, syntax, tsig, consts, facts, cases));
wenzelm@18672
   221
wenzelm@19001
   222
fun map_syntax f =
wenzelm@36450
   223
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   224
    (mode, naming, f syntax, tsig, consts, facts, cases));
wenzelm@35669
   225
wenzelm@36450
   226
fun map_tsig f =
wenzelm@36450
   227
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   228
    (mode, naming, syntax, f tsig, consts, facts, cases));
wenzelm@18672
   229
wenzelm@19001
   230
fun map_consts f =
wenzelm@36450
   231
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   232
    (mode, naming, syntax, tsig, f consts, facts, cases));
wenzelm@18672
   233
wenzelm@26284
   234
fun map_facts f =
wenzelm@36450
   235
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   236
    (mode, naming, syntax, tsig, consts, f facts, cases));
wenzelm@18672
   237
wenzelm@19001
   238
fun map_cases f =
wenzelm@36450
   239
  map_context (fn (mode, naming, syntax, tsig, consts, facts, cases) =>
wenzelm@36450
   240
    (mode, naming, syntax, tsig, consts, facts, f cases));
wenzelm@24388
   241
wenzelm@24388
   242
val get_mode = #mode o rep_context;
wenzelm@28407
   243
val restore_mode = set_mode o get_mode;
wenzelm@27286
   244
val abbrev_mode = get_mode #> (fn Mode {abbrev, ...} => abbrev);
wenzelm@21443
   245
wenzelm@24486
   246
fun set_stmt stmt =
wenzelm@24486
   247
  map_mode (fn (_, pattern, schematic, abbrev) => (stmt, pattern, schematic, abbrev));
wenzelm@19001
   248
wenzelm@19001
   249
val naming_of = #naming o rep_context;
wenzelm@33165
   250
val restore_naming = map_naming o K o naming_of
wenzelm@33095
   251
val full_name = Name_Space.full_name o naming_of;
wenzelm@5819
   252
wenzelm@16540
   253
val syntax_of = #syntax o rep_context;
wenzelm@33387
   254
val syn_of = Local_Syntax.syn_of o syntax_of;
wenzelm@33387
   255
val set_syntax_mode = map_syntax o Local_Syntax.set_mode;
wenzelm@33387
   256
val restore_syntax_mode = map_syntax o Local_Syntax.restore_mode o syntax_of;
wenzelm@19001
   257
wenzelm@36450
   258
val tsig_of = #1 o #tsig o rep_context;
wenzelm@36451
   259
val set_defsort = map_tsig o apfst o Type.set_defsort;
wenzelm@35680
   260
fun default_sort ctxt = the_default (Type.defaultS (tsig_of ctxt)) o Variable.def_sort ctxt;
wenzelm@35669
   261
wenzelm@25052
   262
val consts_of = #1 o #consts o rep_context;
haftmann@24752
   263
val the_const_constraint = Consts.the_constraint o consts_of;
wenzelm@5819
   264
wenzelm@26284
   265
val facts_of = #facts o rep_context;
wenzelm@16540
   266
val cases_of = #cases o rep_context;
wenzelm@5819
   267
wenzelm@5819
   268
wenzelm@20367
   269
(* theory transfer *)
wenzelm@12093
   270
wenzelm@35669
   271
fun transfer_syntax thy ctxt = ctxt |>
wenzelm@35669
   272
  map_syntax (Local_Syntax.rebuild thy) |>
wenzelm@36450
   273
  map_tsig (fn tsig as (local_tsig, global_tsig) =>
wenzelm@35669
   274
    let val thy_tsig = Sign.tsig_of thy in
wenzelm@36450
   275
      if Type.eq_tsig (thy_tsig, global_tsig) then tsig
wenzelm@36450
   276
      else (Type.merge_tsig (Syntax.pp ctxt) (local_tsig, thy_tsig), thy_tsig)
wenzelm@35669
   277
    end) |>
wenzelm@30424
   278
  map_consts (fn consts as (local_consts, global_consts) =>
wenzelm@30424
   279
    let val thy_consts = Sign.consts_of thy in
wenzelm@30424
   280
      if Consts.eq_consts (thy_consts, global_consts) then consts
wenzelm@30424
   281
      else (Consts.merge (local_consts, thy_consts), thy_consts)
wenzelm@30424
   282
    end);
wenzelm@17072
   283
wenzelm@33031
   284
fun transfer thy = Context.raw_transfer thy #> transfer_syntax thy;
wenzelm@17072
   285
wenzelm@20367
   286
fun theory f ctxt = transfer (f (theory_of ctxt)) ctxt;
wenzelm@20367
   287
wenzelm@20367
   288
fun theory_result f ctxt =
wenzelm@20367
   289
  let val (res, thy') = f (theory_of ctxt)
wenzelm@20367
   290
  in (res, ctxt |> transfer thy') end;
wenzelm@19019
   291
wenzelm@12093
   292
wenzelm@12093
   293
wenzelm@14828
   294
(** pretty printing **)
wenzelm@14828
   295
wenzelm@28212
   296
(* extern *)
wenzelm@28212
   297
wenzelm@28212
   298
fun extern_fact ctxt name =
wenzelm@28212
   299
  let
wenzelm@28212
   300
    val local_facts = facts_of ctxt;
wenzelm@28212
   301
    val global_facts = PureThy.facts_of (theory_of ctxt);
wenzelm@28212
   302
  in
wenzelm@28212
   303
    if is_some (Facts.lookup (Context.Proof ctxt) local_facts name)
wenzelm@28212
   304
    then Facts.extern local_facts name
wenzelm@28212
   305
    else Facts.extern global_facts name
wenzelm@28212
   306
  end;
wenzelm@28212
   307
wenzelm@28212
   308
wenzelm@28212
   309
(* pretty *)
wenzelm@28212
   310
wenzelm@24922
   311
fun pretty_term_abbrev ctxt = Syntax.pretty_term (set_mode mode_abbrev ctxt);
wenzelm@14828
   312
wenzelm@28212
   313
fun pretty_fact_name ctxt a = Pretty.block
wenzelm@28212
   314
  [Pretty.markup (Markup.fact a) [Pretty.str (extern_fact ctxt a)], Pretty.str ":"];
wenzelm@28209
   315
wenzelm@32090
   316
fun pretty_fact_aux ctxt flag ("", ths) =
wenzelm@32090
   317
      Display.pretty_thms_aux ctxt flag ths
wenzelm@30723
   318
  | pretty_fact_aux ctxt flag (a, [th]) = Pretty.block
wenzelm@32090
   319
      [pretty_fact_name ctxt a, Pretty.brk 1, Display.pretty_thm_aux ctxt flag th]
wenzelm@30723
   320
  | pretty_fact_aux ctxt flag (a, ths) = Pretty.block
wenzelm@32090
   321
      (Pretty.fbreaks (pretty_fact_name ctxt a :: map (Display.pretty_thm_aux ctxt flag) ths));
wenzelm@30723
   322
wenzelm@30723
   323
fun pretty_fact ctxt = pretty_fact_aux ctxt true;
wenzelm@14828
   324
wenzelm@14828
   325
wenzelm@14828
   326
wenzelm@5819
   327
(** prepare types **)
wenzelm@5819
   328
wenzelm@35669
   329
(* classes *)
wenzelm@35669
   330
wenzelm@35669
   331
fun read_class ctxt text =
wenzelm@35669
   332
  let
wenzelm@35669
   333
    val tsig = tsig_of ctxt;
wenzelm@35669
   334
    val (syms, pos) = Syntax.read_token text;
wenzelm@35669
   335
    val c = Type.cert_class tsig (Type.intern_class tsig (Symbol_Pos.content syms))
wenzelm@35669
   336
      handle TYPE (msg, _, _) => error (msg ^ Position.str_of pos);
wenzelm@35669
   337
    val _ = Position.report (Markup.tclass c) pos;
wenzelm@35669
   338
  in c end;
wenzelm@35669
   339
wenzelm@35669
   340
wenzelm@35669
   341
(* type arities *)
wenzelm@35669
   342
wenzelm@35669
   343
local
wenzelm@35669
   344
wenzelm@35669
   345
fun prep_arity prep_tycon prep_sort ctxt (t, Ss, S) =
wenzelm@35669
   346
  let val arity = (prep_tycon ctxt t, map (prep_sort ctxt) Ss, prep_sort ctxt S)
wenzelm@35669
   347
  in Type.add_arity (Syntax.pp ctxt) arity (tsig_of ctxt); arity end;
wenzelm@35669
   348
wenzelm@35669
   349
in
wenzelm@35669
   350
wenzelm@35669
   351
val read_arity = prep_arity (Type.intern_type o tsig_of) Syntax.read_sort;
wenzelm@35669
   352
val cert_arity = prep_arity (K I) (Type.cert_sort o tsig_of);
wenzelm@35669
   353
wenzelm@35669
   354
end;
wenzelm@35669
   355
wenzelm@35669
   356
wenzelm@35669
   357
(* types *)
wenzelm@24277
   358
wenzelm@24277
   359
fun read_typ_mode mode ctxt s =
wenzelm@24486
   360
  Syntax.read_typ (Type.set_mode mode ctxt) s;
wenzelm@24277
   361
wenzelm@35669
   362
val read_typ = read_typ_mode Type.mode_default;
wenzelm@24277
   363
val read_typ_syntax = read_typ_mode Type.mode_syntax;
wenzelm@24277
   364
val read_typ_abbrev = read_typ_mode Type.mode_abbrev;
wenzelm@24277
   365
wenzelm@24277
   366
wenzelm@24277
   367
fun cert_typ_mode mode ctxt T =
wenzelm@35669
   368
  Type.cert_typ_mode mode (tsig_of ctxt) T
wenzelm@24277
   369
    handle TYPE (msg, _, _) => error msg;
wenzelm@24277
   370
wenzelm@35669
   371
val cert_typ = cert_typ_mode Type.mode_default;
wenzelm@24277
   372
val cert_typ_syntax = cert_typ_mode Type.mode_syntax;
wenzelm@24277
   373
val cert_typ_abbrev = cert_typ_mode Type.mode_abbrev;
wenzelm@24277
   374
wenzelm@24277
   375
wenzelm@24388
   376
wenzelm@24388
   377
(** prepare variables **)
wenzelm@24388
   378
wenzelm@7679
   379
(* internalize Skolem constants *)
wenzelm@7679
   380
wenzelm@19897
   381
val lookup_skolem = AList.lookup (op =) o Variable.fixes_of;
wenzelm@18187
   382
fun get_skolem ctxt x = the_default x (lookup_skolem ctxt x);
wenzelm@7679
   383
wenzelm@18678
   384
fun no_skolem internal x =
wenzelm@20086
   385
  if can Name.dest_skolem x then
wenzelm@18678
   386
    error ("Illegal reference to internal Skolem constant: " ^ quote x)
wenzelm@20086
   387
  else if not internal andalso can Name.dest_internal x then
wenzelm@18678
   388
    error ("Illegal reference to internal variable: " ^ quote x)
wenzelm@7679
   389
  else x;
wenzelm@7679
   390
wenzelm@7679
   391
wenzelm@26717
   392
(* revert Skolem constants -- if possible *)
wenzelm@18255
   393
wenzelm@26717
   394
fun revert_skolem ctxt x =
wenzelm@26717
   395
  (case find_first (fn (_, y) => y = x) (Variable.fixes_of ctxt) of
wenzelm@26717
   396
    SOME (x', _) => if lookup_skolem ctxt x' = SOME x then x' else x
wenzelm@26717
   397
  | NONE => x);
wenzelm@9133
   398
wenzelm@8096
   399
wenzelm@26705
   400
(* default token translations *)
wenzelm@26705
   401
wenzelm@26705
   402
local
wenzelm@26705
   403
wenzelm@26717
   404
fun free_or_skolem ctxt x =
wenzelm@26717
   405
  (if can Name.dest_skolem x then Pretty.mark Markup.skolem (Pretty.str (revert_skolem ctxt x))
wenzelm@26717
   406
   else Pretty.mark Markup.free (Pretty.str x))
wenzelm@26930
   407
  |> Pretty.mark
wenzelm@26939
   408
    (if Variable.is_fixed ctxt x orelse Syntax.is_pretty_global ctxt then Markup.fixed x
wenzelm@26930
   409
     else Markup.hilite);
wenzelm@26705
   410
wenzelm@26705
   411
fun var_or_skolem _ s =
wenzelm@26705
   412
  (case Lexicon.read_variable s of
wenzelm@26705
   413
    SOME (x, i) =>
wenzelm@26705
   414
      (case try Name.dest_skolem x of
wenzelm@26705
   415
        NONE => Pretty.mark Markup.var (Pretty.str s)
wenzelm@26705
   416
      | SOME x' => Pretty.mark Markup.skolem
wenzelm@32966
   417
          (Pretty.str (setmp_CRITICAL show_question_marks true Term.string_of_vname (x', i))))
wenzelm@26705
   418
  | NONE => Pretty.mark Markup.var (Pretty.str s));
wenzelm@26705
   419
wenzelm@26705
   420
fun plain_markup m _ s = Pretty.mark m (Pretty.str s);
wenzelm@26705
   421
wenzelm@26705
   422
val token_trans =
wenzelm@26705
   423
 Syntax.tokentrans_mode ""
wenzelm@35429
   424
  [("tfree", plain_markup Markup.tfree),
wenzelm@26705
   425
   ("tvar", plain_markup Markup.tvar),
wenzelm@26705
   426
   ("free", free_or_skolem),
wenzelm@26705
   427
   ("bound", plain_markup Markup.bound),
wenzelm@26705
   428
   ("var", var_or_skolem),
wenzelm@29318
   429
   ("numeral", plain_markup Markup.numeral),
wenzelm@29318
   430
   ("inner_string", plain_markup Markup.inner_string)];
wenzelm@26705
   431
wenzelm@26705
   432
in val _ = Context.>> (Context.map_theory (Sign.add_tokentrfuns token_trans)) end;
wenzelm@26705
   433
wenzelm@26705
   434
wenzelm@18187
   435
wenzelm@5819
   436
(** prepare terms and propositions **)
wenzelm@5819
   437
wenzelm@25328
   438
(* inferred types of parameters *)
wenzelm@25328
   439
wenzelm@25328
   440
fun infer_type ctxt x =
wenzelm@25328
   441
  Term.fastype_of (singleton (Syntax.check_terms (set_mode mode_schematic ctxt))
wenzelm@25328
   442
    (Free (x, dummyT)));
wenzelm@25328
   443
wenzelm@25328
   444
fun inferred_param x ctxt =
wenzelm@25328
   445
  let val T = infer_type ctxt x
wenzelm@28082
   446
  in (T, ctxt |> Variable.declare_term (Free (x, T))) end;
wenzelm@25328
   447
wenzelm@25328
   448
fun inferred_fixes ctxt =
wenzelm@28082
   449
  let
wenzelm@28082
   450
    val xs = rev (map #2 (Variable.fixes_of ctxt));
wenzelm@28082
   451
    val (Ts, ctxt') = fold_map inferred_param xs ctxt;
wenzelm@28082
   452
  in (xs ~~ Ts, ctxt') end;
wenzelm@25328
   453
wenzelm@25328
   454
wenzelm@25328
   455
(* type and constant names *)
wenzelm@25328
   456
wenzelm@27821
   457
local
wenzelm@27821
   458
wenzelm@30573
   459
val token_content = Syntax.read_token #>> Symbol_Pos.content;
wenzelm@27821
   460
wenzelm@35399
   461
fun prep_const_proper ctxt strict (c, pos) =
wenzelm@35399
   462
  let
wenzelm@35399
   463
    fun err msg = error (msg ^ Position.str_of pos);
wenzelm@35399
   464
    val consts = consts_of ctxt;
wenzelm@35399
   465
    val t as Const (d, _) =
wenzelm@35399
   466
      (case Variable.lookup_const ctxt c of
wenzelm@35399
   467
        SOME d =>
wenzelm@35399
   468
          Const (d, Consts.type_scheme (consts_of ctxt) d handle TYPE (msg, _, _) => err msg)
wenzelm@35399
   469
      | NONE => Consts.read_const consts c);
wenzelm@35399
   470
    val _ =
wenzelm@35399
   471
      if strict then ignore (Consts.the_type consts d) handle TYPE (msg, _, _) => err msg
wenzelm@35399
   472
      else ();
wenzelm@35399
   473
    val _ = Position.report (Markup.const d) pos;
wenzelm@35399
   474
  in t end;
wenzelm@27821
   475
wenzelm@27821
   476
in
wenzelm@25328
   477
wenzelm@35399
   478
fun read_type_name ctxt strict text =
wenzelm@27821
   479
  let
wenzelm@35669
   480
    val tsig = tsig_of ctxt;
wenzelm@35399
   481
    val (c, pos) = token_content text;
wenzelm@27821
   482
  in
wenzelm@27821
   483
    if Syntax.is_tid c then
wenzelm@28411
   484
     (Position.report Markup.tfree pos;
wenzelm@35680
   485
      TFree (c, default_sort ctxt (c, ~1)))
wenzelm@27821
   486
    else
wenzelm@27821
   487
      let
wenzelm@35669
   488
        val d = Type.intern_type tsig c;
wenzelm@35669
   489
        val decl = Type.the_decl tsig d;
wenzelm@28411
   490
        val _ = Position.report (Markup.tycon d) pos;
wenzelm@35360
   491
        fun err () = error ("Bad type name: " ^ quote d);
wenzelm@35360
   492
        val args =
wenzelm@35360
   493
          (case decl of
wenzelm@35360
   494
            Type.LogicalType n => n
wenzelm@35360
   495
          | Type.Abbreviation (vs, _, _) => if strict then err () else length vs
wenzelm@35360
   496
          | Type.Nonterminal => if strict then err () else 0);
wenzelm@35360
   497
      in Type (d, replicate args dummyT) end
wenzelm@27821
   498
  end;
wenzelm@25328
   499
wenzelm@35669
   500
fun read_type_name_proper ctxt strict text =
wenzelm@35669
   501
  (case read_type_name ctxt strict text of
wenzelm@35669
   502
    T as Type _ => T
wenzelm@35669
   503
  | T => error ("Not a type constructor: " ^ Syntax.string_of_typ ctxt T));
wenzelm@35669
   504
wenzelm@35669
   505
wenzelm@35399
   506
fun read_const_proper ctxt strict = prep_const_proper ctxt strict o token_content;
wenzelm@27821
   507
wenzelm@35399
   508
fun read_const ctxt strict text =
wenzelm@35399
   509
  let val (c, pos) = token_content text in
wenzelm@27821
   510
    (case (lookup_skolem ctxt c, Variable.is_const ctxt c) of
wenzelm@27821
   511
      (SOME x, false) =>
wenzelm@28411
   512
        (Position.report (Markup.name x
wenzelm@27821
   513
            (if can Name.dest_skolem x then Markup.skolem else Markup.free)) pos;
wenzelm@27821
   514
          Free (x, infer_type ctxt x))
wenzelm@35399
   515
    | _ => prep_const_proper ctxt strict (c, pos))
wenzelm@27821
   516
  end;
wenzelm@27821
   517
wenzelm@27821
   518
end;
wenzelm@25328
   519
wenzelm@25328
   520
wenzelm@24684
   521
(* read_term *)
wenzelm@24684
   522
wenzelm@24684
   523
fun read_term_mode mode ctxt = Syntax.read_term (set_mode mode ctxt);
wenzelm@24684
   524
wenzelm@24684
   525
val read_term_pattern   = read_term_mode mode_pattern;
wenzelm@24684
   526
val read_term_schematic = read_term_mode mode_schematic;
wenzelm@24684
   527
val read_term_abbrev    = read_term_mode mode_abbrev;
wenzelm@24684
   528
wenzelm@24684
   529
wenzelm@19001
   530
(* local abbreviations *)
wenzelm@5819
   531
wenzelm@24501
   532
local
wenzelm@24501
   533
haftmann@26731
   534
fun certify_consts ctxt = Consts.certify (Syntax.pp ctxt) (tsig_of ctxt)
wenzelm@27286
   535
  (not (abbrev_mode ctxt)) (consts_of ctxt);
wenzelm@19001
   536
wenzelm@19897
   537
fun reject_schematic (Var (xi, _)) =
wenzelm@22678
   538
      error ("Unbound schematic variable: " ^ Term.string_of_vname xi)
wenzelm@19897
   539
  | reject_schematic (Abs (_, _, t)) = reject_schematic t
wenzelm@19897
   540
  | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
wenzelm@19897
   541
  | reject_schematic _ = ();
wenzelm@5819
   542
wenzelm@24495
   543
fun expand_binds ctxt =
wenzelm@24495
   544
  let val Mode {pattern, schematic, ...} = get_mode ctxt in
wenzelm@24495
   545
    if pattern then I
wenzelm@24495
   546
    else Variable.expand_binds ctxt #> (if schematic then I else tap reject_schematic)
wenzelm@24495
   547
  end;
wenzelm@5819
   548
wenzelm@24501
   549
in
wenzelm@24501
   550
wenzelm@24501
   551
fun expand_abbrevs ctxt = certify_consts ctxt #> expand_binds ctxt;
wenzelm@24501
   552
wenzelm@24501
   553
end;
wenzelm@24501
   554
wenzelm@5819
   555
wenzelm@24922
   556
fun contract_abbrevs ctxt t =
wenzelm@24922
   557
  let
wenzelm@24922
   558
    val thy = theory_of ctxt;
wenzelm@24922
   559
    val consts = consts_of ctxt;
wenzelm@24922
   560
    val Mode {abbrev, ...} = get_mode ctxt;
wenzelm@30566
   561
    val retrieve = Consts.retrieve_abbrevs consts (print_mode_value () @ [""]);
wenzelm@30566
   562
    fun match_abbrev u = Option.map #1 (get_first (Pattern.match_rew thy u) (retrieve u));
wenzelm@24922
   563
  in
wenzelm@25052
   564
    if abbrev orelse print_mode_active "no_abbrevs" orelse not (can Term.type_of t) then t
berghofe@35211
   565
    else Pattern.rewrite_term_top thy [] [match_abbrev] t
wenzelm@24922
   566
  end;
wenzelm@24922
   567
wenzelm@24922
   568
wenzelm@24518
   569
(* patterns *)
wenzelm@24518
   570
wenzelm@32003
   571
fun prepare_patternT ctxt T =
wenzelm@32003
   572
  let
wenzelm@32003
   573
    val Mode {pattern, schematic, ...} = get_mode ctxt;
wenzelm@32003
   574
    val _ =
wenzelm@32003
   575
      pattern orelse schematic orelse
wenzelm@32003
   576
        T |> Term.exists_subtype
wenzelm@32003
   577
          (fn TVar (xi, _) =>
wenzelm@32003
   578
            not (TypeInfer.is_param xi) andalso
wenzelm@32003
   579
              error ("Illegal schematic type variable: " ^ Term.string_of_vname xi)
wenzelm@32003
   580
          | _ => false)
wenzelm@32003
   581
  in T end;
wenzelm@24518
   582
wenzelm@22712
   583
wenzelm@24505
   584
local
wenzelm@6550
   585
wenzelm@33519
   586
structure Allow_Dummies = Proof_Data(type T = bool fun init _ = false);
wenzelm@27259
   587
wenzelm@27259
   588
fun check_dummies ctxt t =
wenzelm@30758
   589
  if Allow_Dummies.get ctxt then t
wenzelm@27259
   590
  else Term.no_dummy_patterns t handle TERM _ => error "Illegal dummy pattern(s) in term";
wenzelm@27259
   591
wenzelm@24767
   592
fun prepare_dummies ts = #1 (fold_map Term.replace_dummy_patterns ts 1);
wenzelm@6762
   593
wenzelm@27259
   594
in
wenzelm@6550
   595
wenzelm@30758
   596
val allow_dummies = Allow_Dummies.put true;
wenzelm@24505
   597
wenzelm@24684
   598
fun prepare_patterns ctxt =
wenzelm@24518
   599
  let val Mode {pattern, ...} = get_mode ctxt in
wenzelm@24767
   600
    TypeInfer.fixate_params (Variable.names_of ctxt) #>
wenzelm@24767
   601
    pattern ? Variable.polymorphic ctxt #>
wenzelm@24684
   602
    (map o Term.map_types) (prepare_patternT ctxt) #>
wenzelm@27259
   603
    (if pattern then prepare_dummies else map (check_dummies ctxt))
wenzelm@24505
   604
  end;
wenzelm@24505
   605
wenzelm@24505
   606
end;
wenzelm@24505
   607
wenzelm@6550
   608
wenzelm@22763
   609
(* decoding raw terms (syntax trees) *)
wenzelm@22763
   610
wenzelm@27286
   611
(* types *)
wenzelm@27286
   612
wenzelm@36448
   613
fun get_sort ctxt raw_text =
wenzelm@27286
   614
  let
wenzelm@35669
   615
    val tsig = tsig_of ctxt;
wenzelm@27286
   616
wenzelm@36448
   617
    val text = distinct (op =) (map (apsnd (Type.minimize_sort tsig)) raw_text);
wenzelm@36152
   618
    val _ =
wenzelm@36448
   619
      (case duplicates (eq_fst (op =)) text of
wenzelm@36152
   620
        [] => ()
wenzelm@27286
   621
      | dups => error ("Inconsistent sort constraints for type variable(s) "
wenzelm@27286
   622
          ^ commas_quote (map (Term.string_of_vname' o fst) dups)));
wenzelm@27286
   623
wenzelm@36152
   624
    fun lookup xi =
wenzelm@36448
   625
      (case AList.lookup (op =) text xi of
wenzelm@36152
   626
        NONE => NONE
wenzelm@36152
   627
      | SOME S => if S = dummyS then NONE else SOME S);
wenzelm@36152
   628
wenzelm@27286
   629
    fun get xi =
wenzelm@36152
   630
      (case (lookup xi, Variable.def_sort ctxt xi) of
wenzelm@27286
   631
        (NONE, NONE) => Type.defaultS tsig
wenzelm@27286
   632
      | (NONE, SOME S) => S
wenzelm@27286
   633
      | (SOME S, NONE) => S
wenzelm@27286
   634
      | (SOME S, SOME S') =>
wenzelm@27286
   635
          if Type.eq_sort tsig (S, S') then S'
wenzelm@35669
   636
          else error ("Sort constraint " ^ Syntax.string_of_sort ctxt S ^
wenzelm@35669
   637
            " inconsistent with default " ^ Syntax.string_of_sort ctxt S' ^
wenzelm@30815
   638
            " for type variable " ^ quote (Term.string_of_vname' xi)));
wenzelm@36448
   639
  in get end;
wenzelm@27286
   640
wenzelm@36152
   641
fun check_tvar ctxt (xi, S) = (xi, get_sort ctxt [(xi, S)] xi);
wenzelm@36152
   642
fun check_tfree ctxt (x, S) = apfst fst (check_tvar ctxt ((x, ~1), S));
wenzelm@36152
   643
wenzelm@25353
   644
local
wenzelm@25353
   645
wenzelm@25353
   646
fun intern_skolem ctxt def_type x =
wenzelm@22763
   647
  let
wenzelm@25353
   648
    val _ = no_skolem false x;
wenzelm@22763
   649
    val sko = lookup_skolem ctxt x;
wenzelm@35399
   650
    val is_const = can (read_const_proper ctxt false) x orelse Long_Name.is_qualified x;
wenzelm@22763
   651
    val is_declared = is_some (def_type (x, ~1));
wenzelm@25319
   652
  in
wenzelm@25353
   653
    if Variable.is_const ctxt x then NONE
wenzelm@25353
   654
    else if is_some sko then sko
wenzelm@25353
   655
    else if not is_const orelse is_declared then SOME x
wenzelm@25353
   656
    else NONE
wenzelm@25319
   657
  end;
wenzelm@22763
   658
wenzelm@25353
   659
in
wenzelm@25353
   660
wenzelm@24371
   661
fun term_context ctxt =
wenzelm@36152
   662
  {get_sort = get_sort ctxt,
wenzelm@35669
   663
   map_const = fn a => ((true, #1 (Term.dest_Const (read_const_proper ctxt false a)))
wenzelm@35669
   664
     handle ERROR _ => (false, Consts.intern (consts_of ctxt) a)),
wenzelm@35669
   665
   map_free = intern_skolem ctxt (Variable.def_type ctxt false)};
wenzelm@24371
   666
wenzelm@22763
   667
fun decode_term ctxt =
wenzelm@35429
   668
  let val {get_sort, map_const, map_free} = term_context ctxt
wenzelm@35429
   669
  in Syntax.decode_term get_sort map_const map_free end;
wenzelm@22763
   670
wenzelm@10554
   671
end;
wenzelm@10554
   672
wenzelm@5819
   673
wenzelm@5819
   674
(* certify terms *)
wenzelm@5819
   675
wenzelm@10554
   676
local
wenzelm@10554
   677
wenzelm@24684
   678
fun gen_cert prop ctxt t =
wenzelm@24684
   679
  t
wenzelm@24684
   680
  |> expand_abbrevs ctxt
wenzelm@24961
   681
  |> (fn t' => #1 (Sign.certify' prop (Syntax.pp ctxt) false (consts_of ctxt) (theory_of ctxt) t')
wenzelm@24684
   682
    handle TYPE (msg, _, _) => error msg
wenzelm@24684
   683
      | TERM (msg, _) => error msg);
wenzelm@16501
   684
wenzelm@10554
   685
in
wenzelm@8096
   686
wenzelm@24684
   687
val cert_term = gen_cert false;
wenzelm@24684
   688
val cert_prop = gen_cert true;
wenzelm@10554
   689
wenzelm@10554
   690
end;
wenzelm@5819
   691
wenzelm@5819
   692
wenzelm@24495
   693
(* type checking/inference *)
wenzelm@22701
   694
wenzelm@35616
   695
fun def_type ctxt =
wenzelm@35616
   696
  let val Mode {pattern, ...} = get_mode ctxt
wenzelm@35616
   697
  in Variable.def_type ctxt pattern end;
wenzelm@35616
   698
wenzelm@24495
   699
fun standard_infer_types ctxt ts =
wenzelm@35616
   700
  TypeInfer.infer_types (Syntax.pp ctxt) (tsig_of ctxt) (Syntax.check_typs ctxt)
wenzelm@35616
   701
    (try (Consts.the_constraint (consts_of ctxt))) (def_type ctxt)
wenzelm@35616
   702
    (Variable.names_of ctxt) (Variable.maxidx_of ctxt) ts
wenzelm@35616
   703
  handle TYPE (msg, _, _) => error msg;
wenzelm@24486
   704
wenzelm@25406
   705
local
wenzelm@25406
   706
wenzelm@24518
   707
fun standard_typ_check ctxt =
wenzelm@24518
   708
  map (cert_typ_mode (Type.get_mode ctxt) ctxt) #>
wenzelm@24518
   709
  map (prepare_patternT ctxt);
wenzelm@24518
   710
wenzelm@24769
   711
fun standard_term_check ctxt =
wenzelm@24769
   712
  standard_infer_types ctxt #>
wenzelm@24769
   713
  map (expand_abbrevs ctxt);
wenzelm@24922
   714
wenzelm@24922
   715
fun standard_term_uncheck ctxt =
wenzelm@24922
   716
  map (contract_abbrevs ctxt);
wenzelm@24922
   717
wenzelm@26463
   718
fun add eq what f = Context.>> (what (fn xs => fn ctxt =>
wenzelm@26463
   719
  let val xs' = f ctxt xs in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end));
wenzelm@24767
   720
wenzelm@24518
   721
in
wenzelm@24518
   722
wenzelm@25060
   723
val _ = add (op =) (Syntax.add_typ_check 0 "standard") standard_typ_check;
wenzelm@25060
   724
val _ = add (op aconv) (Syntax.add_term_check 0 "standard") standard_term_check;
wenzelm@25060
   725
val _ = add (op aconv) (Syntax.add_term_check 100 "fixate") prepare_patterns;
wenzelm@24922
   726
wenzelm@25060
   727
val _ = add (op aconv) (Syntax.add_term_uncheck 0 "standard") standard_term_uncheck;
wenzelm@22701
   728
wenzelm@24518
   729
end;
wenzelm@22701
   730
wenzelm@22701
   731
wenzelm@9553
   732
wenzelm@24767
   733
(** inner syntax operations **)
wenzelm@24371
   734
wenzelm@24371
   735
local
wenzelm@24371
   736
wenzelm@27754
   737
fun parse_sort ctxt text =
wenzelm@27754
   738
  let
wenzelm@28411
   739
    val (syms, pos) = Syntax.parse_token Markup.sort text;
wenzelm@35429
   740
    val S = Syntax.standard_parse_sort ctxt (syn_of ctxt) (syms, pos)
wenzelm@27754
   741
      handle ERROR msg => cat_error msg  ("Failed to parse sort" ^ Position.str_of pos)
wenzelm@36448
   742
  in Type.minimize_sort (tsig_of ctxt) S end;
wenzelm@27754
   743
wenzelm@27754
   744
fun parse_typ ctxt text =
wenzelm@24371
   745
  let
wenzelm@28411
   746
    val (syms, pos) = Syntax.parse_token Markup.typ text;
wenzelm@36152
   747
    val T = Syntax.standard_parse_typ ctxt (syn_of ctxt) (get_sort ctxt) (syms, pos)
wenzelm@35429
   748
      handle ERROR msg => cat_error msg ("Failed to parse type" ^ Position.str_of pos);
wenzelm@27754
   749
  in T end;
wenzelm@24371
   750
wenzelm@27754
   751
fun parse_term T ctxt text =
wenzelm@24371
   752
  let
wenzelm@35429
   753
    val {get_sort, map_const, map_free} = term_context ctxt;
wenzelm@27754
   754
wenzelm@25168
   755
    val (T', _) = TypeInfer.paramify_dummies T 0;
wenzelm@27821
   756
    val (markup, kind) = if T' = propT then (Markup.prop, "proposition") else (Markup.term, "term");
wenzelm@28411
   757
    val (syms, pos) = Syntax.parse_token markup text;
wenzelm@27754
   758
wenzelm@25476
   759
    fun check t = (Syntax.check_term ctxt (TypeInfer.constrain T' t); NONE)
wenzelm@25476
   760
      handle ERROR msg => SOME msg;
wenzelm@35429
   761
    val t =
wenzelm@35429
   762
      Syntax.standard_parse_term (Syntax.pp ctxt) check get_sort map_const map_free
wenzelm@35669
   763
        ctxt (Type.is_logtype (tsig_of ctxt)) (syn_of ctxt) T' (syms, pos)
wenzelm@35680
   764
      handle ERROR msg => cat_error msg ("Failed to parse " ^ kind ^ Position.str_of pos);
wenzelm@27754
   765
  in t end;
wenzelm@24371
   766
wenzelm@24922
   767
wenzelm@35429
   768
fun unparse_sort ctxt =
wenzelm@35669
   769
  Syntax.standard_unparse_sort {extern_class = Type.extern_class (tsig_of ctxt)}
wenzelm@35429
   770
    ctxt (syn_of ctxt);
wenzelm@24922
   771
wenzelm@35429
   772
fun unparse_typ ctxt =
wenzelm@35429
   773
  let
wenzelm@35669
   774
    val tsig = tsig_of ctxt;
wenzelm@35669
   775
    val extern = {extern_class = Type.extern_class tsig, extern_type = Type.extern_type tsig};
wenzelm@35429
   776
  in Syntax.standard_unparse_typ extern ctxt (syn_of ctxt) end;
wenzelm@24922
   777
wenzelm@35429
   778
fun unparse_term ctxt =
wenzelm@24922
   779
  let
wenzelm@35669
   780
    val tsig = tsig_of ctxt;
wenzelm@24922
   781
    val syntax = syntax_of ctxt;
wenzelm@24922
   782
    val consts = consts_of ctxt;
wenzelm@35429
   783
    val extern =
wenzelm@35669
   784
     {extern_class = Type.extern_class tsig,
wenzelm@35669
   785
      extern_type = Type.extern_type tsig,
wenzelm@35429
   786
      extern_const = Consts.extern consts};
wenzelm@24922
   787
  in
wenzelm@35429
   788
    Syntax.standard_unparse_term (Local_Syntax.idents_of syntax) extern ctxt
wenzelm@35669
   789
      (Local_Syntax.syn_of syntax) (not (PureThy.old_appl_syntax (theory_of ctxt)))
wenzelm@24922
   790
  end;
wenzelm@24922
   791
wenzelm@24371
   792
in
wenzelm@24371
   793
wenzelm@24767
   794
val _ = Syntax.install_operations
wenzelm@24767
   795
  {parse_sort = parse_sort,
wenzelm@24767
   796
   parse_typ = parse_typ,
wenzelm@24767
   797
   parse_term = parse_term dummyT,
wenzelm@24767
   798
   parse_prop = parse_term propT,
wenzelm@24922
   799
   unparse_sort = unparse_sort,
wenzelm@24922
   800
   unparse_typ = unparse_typ,
wenzelm@24922
   801
   unparse_term = unparse_term};
wenzelm@24371
   802
wenzelm@24371
   803
end;
wenzelm@24371
   804
wenzelm@24371
   805
wenzelm@24371
   806
wenzelm@21610
   807
(** export results **)
wenzelm@21531
   808
wenzelm@20310
   809
fun common_export is_goal inner outer =
wenzelm@20310
   810
  map (Assumption.export is_goal inner outer) #>
wenzelm@20310
   811
  Variable.export inner outer;
wenzelm@8616
   812
wenzelm@20310
   813
val goal_export = common_export true;
wenzelm@20310
   814
val export = common_export false;
wenzelm@12704
   815
wenzelm@21531
   816
fun export_morphism inner outer =
wenzelm@21531
   817
  Assumption.export_morphism inner outer $>
wenzelm@21531
   818
  Variable.export_morphism inner outer;
wenzelm@21531
   819
wenzelm@28396
   820
fun norm_export_morphism inner outer =
wenzelm@28396
   821
  export_morphism inner outer $>
wenzelm@28396
   822
  Morphism.thm_morphism Goal.norm_result;
wenzelm@28396
   823
wenzelm@21531
   824
wenzelm@15758
   825
wenzelm@30757
   826
(** term bindings **)
wenzelm@5819
   827
wenzelm@8096
   828
(* simult_matches *)
wenzelm@8096
   829
wenzelm@19867
   830
fun simult_matches ctxt (t, pats) =
wenzelm@19867
   831
  (case Seq.pull (Unify.matchers (theory_of ctxt) (map (rpair t) pats)) of
wenzelm@19867
   832
    NONE => error "Pattern match failed!"
wenzelm@32032
   833
  | SOME (env, _) => Vartab.fold (fn (v, (_, t)) => cons (v, t)) (Envir.term_env env) []);
wenzelm@8096
   834
wenzelm@8096
   835
wenzelm@30757
   836
(* bind_terms *)
wenzelm@7925
   837
wenzelm@30757
   838
val bind_terms = fold (fn (xi, t) => fn ctxt =>
wenzelm@24511
   839
  ctxt
wenzelm@30757
   840
  |> Variable.bind_term (xi, Option.map (cert_term (set_mode mode_default ctxt)) t));
wenzelm@5819
   841
wenzelm@30757
   842
wenzelm@30757
   843
(* auto_bind *)
wenzelm@10810
   844
wenzelm@20330
   845
fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
wenzelm@10554
   846
  | drop_schematic b = b;
wenzelm@10554
   847
wenzelm@30757
   848
fun auto_bind f ts ctxt = ctxt |> bind_terms (map drop_schematic (f (theory_of ctxt) ts));
wenzelm@8616
   849
wenzelm@33386
   850
val auto_bind_goal = auto_bind Auto_Bind.goal;
wenzelm@33386
   851
val auto_bind_facts = auto_bind Auto_Bind.facts;
wenzelm@7925
   852
wenzelm@5819
   853
wenzelm@8096
   854
(* match_bind(_i) *)
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;
skalberg@15531
   873
    val binds'' = map (apsnd SOME) binds';
wenzelm@18310
   874
    val ctxt'' =
wenzelm@19897
   875
      tap (Variable.warn_extra_tfrees ctxt)
wenzelm@18310
   876
       (if gen then
wenzelm@30757
   877
          ctxt (*sic!*) |> fold Variable.declare_term (map #2 binds') |> bind_terms binds''
wenzelm@30757
   878
        else ctxt' |> bind_terms binds'');
wenzelm@18310
   879
  in (ts, ctxt'') end;
wenzelm@8096
   880
wenzelm@8096
   881
in
wenzelm@5935
   882
wenzelm@24684
   883
fun read_terms ctxt T =
wenzelm@24684
   884
  map (Syntax.parse_term ctxt #> TypeInfer.constrain T) #> Syntax.check_terms ctxt;
wenzelm@24684
   885
wenzelm@24684
   886
val match_bind = gen_bind read_terms;
wenzelm@24684
   887
val match_bind_i = gen_bind (fn ctxt => fn _ => map (cert_term ctxt));
wenzelm@8096
   888
wenzelm@8096
   889
end;
wenzelm@5935
   890
wenzelm@5935
   891
wenzelm@10465
   892
(* propositions with patterns *)
wenzelm@5935
   893
wenzelm@10465
   894
local
wenzelm@8096
   895
wenzelm@24684
   896
fun prep_propp mode prep_props (context, args) =
wenzelm@10465
   897
  let
wenzelm@19585
   898
    fun prep (_, raw_pats) (ctxt, prop :: props) =
wenzelm@24684
   899
      let val ctxt' = Variable.declare_term prop ctxt
wenzelm@24684
   900
      in ((prop, prep_props (set_mode mode_pattern ctxt') raw_pats), (ctxt', props)) end;
wenzelm@24684
   901
wenzelm@17860
   902
    val (propp, (context', _)) = (fold_map o fold_map) prep args
wenzelm@24684
   903
      (context, prep_props (set_mode mode context) (maps (map fst) args));
wenzelm@10465
   904
  in (context', propp) end;
wenzelm@5935
   905
wenzelm@24684
   906
fun gen_bind_propp mode parse_prop (ctxt, raw_args) =
wenzelm@8096
   907
  let
wenzelm@24684
   908
    val (ctxt', args) = prep_propp mode parse_prop (ctxt, raw_args);
wenzelm@19585
   909
    val binds = flat (flat (map (map (simult_matches ctxt')) args));
wenzelm@10465
   910
    val propss = map (map #1) args;
wenzelm@8616
   911
wenzelm@10554
   912
    (*generalize result: context evaluated now, binds added later*)
wenzelm@19916
   913
    val gen = Variable.exportT_terms ctxt' ctxt;
wenzelm@30757
   914
    fun gen_binds c = c |> bind_terms (map #1 binds ~~ map SOME (gen (map #2 binds)));
wenzelm@30757
   915
  in (ctxt' |> bind_terms (map (apsnd SOME) binds), (propss, gen_binds)) end;
wenzelm@8096
   916
wenzelm@10465
   917
in
wenzelm@10465
   918
wenzelm@24684
   919
val read_propp           = prep_propp mode_default Syntax.read_props;
wenzelm@24684
   920
val cert_propp           = prep_propp mode_default (map o cert_prop);
wenzelm@24684
   921
val read_propp_schematic = prep_propp mode_schematic Syntax.read_props;
wenzelm@24684
   922
val cert_propp_schematic = prep_propp mode_schematic (map o cert_prop);
wenzelm@10554
   923
wenzelm@24684
   924
val bind_propp             = gen_bind_propp mode_default Syntax.read_props;
wenzelm@24684
   925
val bind_propp_i           = gen_bind_propp mode_default (map o cert_prop);
wenzelm@24684
   926
val bind_propp_schematic   = gen_bind_propp mode_schematic Syntax.read_props;
wenzelm@24684
   927
val bind_propp_schematic_i = gen_bind_propp mode_schematic (map o cert_prop);
wenzelm@6789
   928
wenzelm@10465
   929
end;
wenzelm@10465
   930
wenzelm@6789
   931
wenzelm@5819
   932
wenzelm@5819
   933
(** theorems **)
wenzelm@5819
   934
wenzelm@18042
   935
(* fact_tac *)
wenzelm@18042
   936
wenzelm@27867
   937
fun comp_incr_tac [] _ = no_tac
wenzelm@27867
   938
  | comp_incr_tac (th :: ths) i =
wenzelm@27867
   939
      (fn st => Goal.compose_hhf_tac (Drule.incr_indexes st th) i st) APPEND comp_incr_tac ths i;
wenzelm@18042
   940
wenzelm@21687
   941
fun fact_tac facts = Goal.norm_hhf_tac THEN' comp_incr_tac facts;
wenzelm@18122
   942
wenzelm@27867
   943
fun potential_facts ctxt prop =
wenzelm@27867
   944
  Facts.could_unify (facts_of ctxt) (Term.strip_all_body prop);
wenzelm@27867
   945
wenzelm@27867
   946
fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) => fact_tac (potential_facts ctxt goal) i);
wenzelm@18042
   947
wenzelm@18042
   948
wenzelm@6091
   949
(* get_thm(s) *)
wenzelm@5819
   950
wenzelm@26361
   951
local
wenzelm@26361
   952
wenzelm@26687
   953
fun retrieve_thms pick ctxt (Facts.Fact s) =
wenzelm@16501
   954
      let
wenzelm@27867
   955
        val (_, pos) = Syntax.read_token s;
wenzelm@24511
   956
        val prop = Syntax.read_prop (set_mode mode_default ctxt) s
wenzelm@24511
   957
          |> singleton (Variable.polymorphic ctxt);
wenzelm@27867
   958
wenzelm@27867
   959
        fun prove_fact th =
wenzelm@33700
   960
          Goal.prove ctxt [] [] prop (K (ALLGOALS (fact_tac [th])));
wenzelm@27867
   961
        val res =
wenzelm@27867
   962
          (case get_first (try prove_fact) (potential_facts ctxt prop) of
wenzelm@27867
   963
            SOME res => res
wenzelm@27867
   964
          | NONE => error ("Failed to retrieve literal fact" ^ Position.str_of pos ^ ":\n" ^
wenzelm@27867
   965
              Syntax.string_of_term ctxt prop))
wenzelm@27867
   966
      in pick "" [res] end
wenzelm@26687
   967
  | retrieve_thms pick ctxt xthmref =
wenzelm@18042
   968
      let
wenzelm@18042
   969
        val thy = theory_of ctxt;
wenzelm@26284
   970
        val local_facts = facts_of ctxt;
wenzelm@26673
   971
        val thmref = Facts.map_name_of_ref (Facts.intern local_facts) xthmref;
wenzelm@26336
   972
        val name = Facts.name_of_ref thmref;
wenzelm@27821
   973
        val pos = Facts.pos_of_ref xthmref;
wenzelm@24012
   974
        val thms =
wenzelm@24012
   975
          if name = "" then [Thm.transfer thy Drule.dummy_thm]
wenzelm@24012
   976
          else
wenzelm@26393
   977
            (case Facts.lookup (Context.Proof ctxt) local_facts name of
wenzelm@28411
   978
              SOME (_, ths) => (Position.report (Markup.local_fact name) pos;
wenzelm@27821
   979
                map (Thm.transfer thy) (Facts.select thmref ths))
wenzelm@26687
   980
            | NONE => PureThy.get_fact (Context.Proof ctxt) thy xthmref);
wenzelm@24012
   981
      in pick name thms end;
wenzelm@5819
   982
wenzelm@26361
   983
in
wenzelm@26346
   984
wenzelm@26687
   985
val get_fact = retrieve_thms (K I);
wenzelm@26687
   986
val get_fact_single = retrieve_thms Facts.the_single;
wenzelm@26346
   987
wenzelm@26361
   988
fun get_thms ctxt = get_fact ctxt o Facts.named;
wenzelm@26361
   989
fun get_thm ctxt = get_fact_single ctxt o Facts.named;
wenzelm@26361
   990
wenzelm@26361
   991
end;
wenzelm@5819
   992
wenzelm@5819
   993
wenzelm@26284
   994
(* facts *)
wenzelm@5819
   995
wenzelm@28082
   996
local
wenzelm@28082
   997
haftmann@28965
   998
fun update_thms _ (b, NONE) ctxt = ctxt |> map_facts (Facts.del (full_name ctxt b))
haftmann@28861
   999
  | update_thms do_props (b, SOME ths) ctxt = ctxt |> map_facts
haftmann@28864
  1000
      (Facts.add_local do_props (naming_of ctxt) (b, ths) #> snd);
wenzelm@5819
  1001
wenzelm@30761
  1002
in
wenzelm@30761
  1003
wenzelm@30761
  1004
fun note_thmss kind = fold_map (fn ((b, more_attrs), raw_facts) => fn ctxt =>
wenzelm@5819
  1005
  let
haftmann@28941
  1006
    val pos = Binding.pos_of b;
haftmann@28965
  1007
    val name = full_name ctxt b;
wenzelm@33383
  1008
    val _ = Context_Position.report_visible ctxt (Markup.local_fact_decl name) pos;
wenzelm@28082
  1009
wenzelm@33700
  1010
    val facts = PureThy.name_thmss false name raw_facts;
wenzelm@21443
  1011
    fun app (th, attrs) x =
wenzelm@30761
  1012
      swap (Library.foldl_map
wenzelm@30761
  1013
        (Thm.proof_attributes (surround (Thm.kind kind) (attrs @ more_attrs))) (x, th));
wenzelm@21443
  1014
    val (res, ctxt') = fold_map app facts ctxt;
wenzelm@33700
  1015
    val thms = PureThy.name_thms false false name (flat res);
wenzelm@24388
  1016
    val Mode {stmt, ...} = get_mode ctxt;
haftmann@28861
  1017
  in ((name, thms), ctxt' |> update_thms stmt (b, SOME thms)) end);
wenzelm@12711
  1018
wenzelm@28417
  1019
fun put_thms do_props thms ctxt = ctxt
wenzelm@28417
  1020
  |> map_naming (K local_naming)
wenzelm@33383
  1021
  |> Context_Position.set_visible false
haftmann@28965
  1022
  |> update_thms do_props (apfst Binding.name thms)
wenzelm@33383
  1023
  |> Context_Position.restore_visible ctxt
wenzelm@28417
  1024
  |> restore_naming ctxt;
wenzelm@28082
  1025
wenzelm@12711
  1026
end;
wenzelm@9196
  1027
wenzelm@5819
  1028
wenzelm@5819
  1029
wenzelm@35680
  1030
(** basic logical entities **)
wenzelm@17360
  1031
wenzelm@8096
  1032
(* variables *)
wenzelm@8096
  1033
wenzelm@19897
  1034
fun declare_var (x, opt_T, mx) ctxt =
wenzelm@22701
  1035
  let val T = (case opt_T of SOME T => T | NONE => Syntax.mixfixT mx)
wenzelm@20163
  1036
  in ((x, T, mx), ctxt |> Variable.declare_constraints (Free (x, T))) end;
wenzelm@19897
  1037
wenzelm@10381
  1038
local
wenzelm@10381
  1039
wenzelm@25353
  1040
fun prep_vars prep_typ internal =
wenzelm@35129
  1041
  fold_map (fn (b, raw_T, mx) => fn ctxt =>
wenzelm@18672
  1042
    let
wenzelm@35129
  1043
      val x = Name.of_binding b;
wenzelm@25353
  1044
      val _ = Syntax.is_identifier (no_skolem internal x) orelse
wenzelm@35129
  1045
        error ("Illegal variable name: " ^ quote (Binding.str_of b));
wenzelm@12504
  1046
wenzelm@18672
  1047
      fun cond_tvars T =
wenzelm@18672
  1048
        if internal then T
wenzelm@18678
  1049
        else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
wenzelm@24277
  1050
      val opt_T = Option.map (cond_tvars o cert_typ ctxt o prep_typ ctxt) raw_T;
wenzelm@35129
  1051
      val (_, ctxt') = ctxt |> declare_var (x, opt_T, mx);
wenzelm@35129
  1052
    in ((b, opt_T, mx), ctxt') end);
wenzelm@8096
  1053
wenzelm@10381
  1054
in
wenzelm@10381
  1055
wenzelm@25353
  1056
val read_vars = prep_vars Syntax.parse_typ false;
wenzelm@25353
  1057
val cert_vars = prep_vars (K I) true;
wenzelm@8096
  1058
wenzelm@10381
  1059
end;
wenzelm@10381
  1060
wenzelm@8096
  1061
wenzelm@35680
  1062
(* authentic syntax *)
wenzelm@35669
  1063
wenzelm@35669
  1064
val _ = Context.>>
wenzelm@35669
  1065
  (Context.map_theory
wenzelm@35669
  1066
    (Sign.add_advanced_trfuns
wenzelm@35669
  1067
      (Syntax.type_ast_trs
wenzelm@35669
  1068
        {read_class = read_class,
wenzelm@35669
  1069
          read_type = fn ctxt => #1 o dest_Type o read_type_name_proper ctxt false}, [], [], [])));
wenzelm@19663
  1070
wenzelm@25159
  1071
local
wenzelm@25159
  1072
wenzelm@25159
  1073
fun const_ast_tr intern ctxt [Syntax.Variable c] =
wenzelm@19681
  1074
      let
wenzelm@35399
  1075
        val Const (c', _) = read_const_proper ctxt false c;
wenzelm@35262
  1076
        val d = if intern then Syntax.mark_const c' else c;
wenzelm@25159
  1077
      in Syntax.Constant d end
wenzelm@25159
  1078
  | const_ast_tr _ _ asts = raise Syntax.AST ("const_ast_tr", asts);
wenzelm@25159
  1079
wenzelm@35429
  1080
val typ = Simple_Syntax.read_typ;
wenzelm@35429
  1081
wenzelm@25159
  1082
in
wenzelm@19681
  1083
wenzelm@26463
  1084
val _ = Context.>> (Context.map_theory
wenzelm@35429
  1085
 (Sign.add_syntax_i
wenzelm@35429
  1086
   [("_context_const", typ "id => logic", Delimfix "CONST _"),
wenzelm@35429
  1087
    ("_context_const", typ "id => aprop", Delimfix "CONST _"),
wenzelm@35429
  1088
    ("_context_const", typ "longid => logic", Delimfix "CONST _"),
wenzelm@35429
  1089
    ("_context_const", typ "longid => aprop", Delimfix "CONST _"),
wenzelm@35429
  1090
    ("_context_xconst", typ "id => logic", Delimfix "XCONST _"),
wenzelm@35429
  1091
    ("_context_xconst", typ "id => aprop", Delimfix "XCONST _"),
wenzelm@35429
  1092
    ("_context_xconst", typ "longid => logic", Delimfix "XCONST _"),
wenzelm@35429
  1093
    ("_context_xconst", typ "longid => aprop", Delimfix "XCONST _")] #>
wenzelm@25159
  1094
  Sign.add_advanced_trfuns
wenzelm@26463
  1095
    ([("_context_const", const_ast_tr true), ("_context_xconst", const_ast_tr false)], [], [], [])));
wenzelm@25159
  1096
wenzelm@25159
  1097
end;
wenzelm@19681
  1098
wenzelm@19663
  1099
wenzelm@21744
  1100
(* notation *)
wenzelm@21744
  1101
wenzelm@24949
  1102
local
wenzelm@24949
  1103
wenzelm@35429
  1104
fun type_syntax (Type (c, args), mx) =
wenzelm@35429
  1105
      SOME (Local_Syntax.Type, (Syntax.mark_type c, Syntax.make_type (length args), mx))
wenzelm@35412
  1106
  | type_syntax _ = NONE;
wenzelm@35412
  1107
wenzelm@35412
  1108
fun const_syntax _ (Free (x, T), mx) = SOME (Local_Syntax.Fixed, (x, T, mx))
wenzelm@24949
  1109
  | const_syntax ctxt (Const (c, _), mx) =
wenzelm@35255
  1110
      (case try (Consts.type_scheme (consts_of ctxt)) c of
wenzelm@35412
  1111
        SOME T => SOME (Local_Syntax.Const, (Syntax.mark_const c, T, mx))
wenzelm@35255
  1112
      | NONE => NONE)
wenzelm@24949
  1113
  | const_syntax _ _ = NONE;
wenzelm@24949
  1114
wenzelm@35412
  1115
fun gen_notation syntax add mode args ctxt =
wenzelm@35412
  1116
  ctxt |> map_syntax
wenzelm@35412
  1117
    (Local_Syntax.update_modesyntax (theory_of ctxt) add mode (map_filter (syntax ctxt) args));
wenzelm@35412
  1118
wenzelm@24949
  1119
in
wenzelm@21744
  1120
wenzelm@35412
  1121
val type_notation = gen_notation (K type_syntax);
wenzelm@35412
  1122
val notation = gen_notation const_syntax;
wenzelm@35412
  1123
wenzelm@35412
  1124
fun target_type_notation add  mode args phi =
wenzelm@35412
  1125
  let
wenzelm@35412
  1126
    val args' = args |> map_filter (fn (T, mx) =>
wenzelm@35412
  1127
      let
wenzelm@35412
  1128
        val T' = Morphism.typ phi T;
wenzelm@35412
  1129
        val similar = (case (T, T') of (Type (c, _), Type (c', _)) => c = c' | _ => false);
wenzelm@35412
  1130
      in if similar then SOME (T', mx) else NONE end);
wenzelm@35412
  1131
  in Context.mapping (Sign.type_notation add mode args') (type_notation add mode args') end;
wenzelm@24949
  1132
wenzelm@24949
  1133
fun target_notation add mode args phi =
wenzelm@33537
  1134
  let
wenzelm@33537
  1135
    val args' = args |> map_filter (fn (t, mx) =>
wenzelm@33537
  1136
      let val t' = Morphism.term phi t
wenzelm@33537
  1137
      in if Term.aconv_untyped (t, t') then SOME (t', mx) else NONE end);
wenzelm@24949
  1138
  in Context.mapping (Sign.notation add mode args') (notation add mode args') end;
wenzelm@24949
  1139
wenzelm@35680
  1140
end;
wenzelm@35412
  1141
wenzelm@35680
  1142
wenzelm@35680
  1143
(* aliases *)
wenzelm@35680
  1144
wenzelm@36450
  1145
fun class_alias b c ctxt = (map_tsig o apfst) (Type.class_alias (naming_of ctxt) b c) ctxt;
wenzelm@36450
  1146
fun type_alias b c ctxt = (map_tsig o apfst) (Type.type_alias (naming_of ctxt) b c) ctxt;
wenzelm@35680
  1147
fun const_alias b c ctxt = (map_consts o apfst) (Consts.alias (naming_of ctxt) b c) ctxt;
wenzelm@21744
  1148
wenzelm@21744
  1149
wenzelm@24767
  1150
(* local constants *)
wenzelm@24767
  1151
wenzelm@24767
  1152
fun add_const_constraint (c, opt_T) ctxt =
wenzelm@24767
  1153
  let
wenzelm@24767
  1154
    fun prepT raw_T =
wenzelm@24767
  1155
      let val T = cert_typ ctxt raw_T
wenzelm@24767
  1156
      in cert_term ctxt (Const (c, T)); T end;
haftmann@25039
  1157
  in ctxt |> (map_consts o apfst) (Consts.constrain (c, Option.map prepT opt_T)) end;
wenzelm@19001
  1158
wenzelm@33173
  1159
fun add_abbrev mode (b, raw_t) ctxt =
wenzelm@19001
  1160
  let
wenzelm@24675
  1161
    val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
wenzelm@30218
  1162
      handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote (Binding.str_of b));
wenzelm@20008
  1163
    val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
wenzelm@21807
  1164
    val ((lhs, rhs), consts') = consts_of ctxt
wenzelm@33173
  1165
      |> Consts.abbreviate (Syntax.pp ctxt) (tsig_of ctxt) (naming_of ctxt) mode (b, t);
wenzelm@19001
  1166
  in
wenzelm@19001
  1167
    ctxt
haftmann@25039
  1168
    |> (map_consts o apfst) (K consts')
wenzelm@21803
  1169
    |> Variable.declare_term rhs
wenzelm@21803
  1170
    |> pair (lhs, rhs)
wenzelm@21704
  1171
  end;
wenzelm@19001
  1172
wenzelm@25052
  1173
fun revert_abbrev mode c = (map_consts o apfst) (Consts.revert_abbrev mode c);
wenzelm@25052
  1174
wenzelm@19001
  1175
wenzelm@18672
  1176
(* fixes *)
wenzelm@5819
  1177
wenzelm@8096
  1178
local
wenzelm@8096
  1179
wenzelm@19001
  1180
fun prep_mixfix (x, T, mx) =
wenzelm@19019
  1181
  if mx <> NoSyn andalso mx <> Structure andalso
wenzelm@20086
  1182
      (can Name.dest_internal x orelse can Name.dest_skolem x) then
wenzelm@19001
  1183
    error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x)
wenzelm@35412
  1184
  else (Local_Syntax.Fixed, (x, T, mx));
wenzelm@19001
  1185
wenzelm@30763
  1186
in
wenzelm@30763
  1187
wenzelm@30763
  1188
fun add_fixes raw_vars ctxt =
wenzelm@8096
  1189
  let
wenzelm@30763
  1190
    val (vars, _) = cert_vars raw_vars ctxt;
wenzelm@30585
  1191
    val (xs', ctxt') = Variable.add_fixes (map (Name.of_binding o #1) vars) ctxt;
wenzelm@28082
  1192
    val ctxt'' =
wenzelm@28082
  1193
      ctxt'
wenzelm@28082
  1194
      |> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars)
wenzelm@33387
  1195
      |-> (map_syntax o Local_Syntax.add_syntax (theory_of ctxt) o map prep_mixfix);
haftmann@28861
  1196
    val _ = (vars ~~ xs') |> List.app (fn ((b, _, _), x') =>
wenzelm@33383
  1197
      Context_Position.report_visible ctxt (Markup.fixed_decl x') (Binding.pos_of b));
wenzelm@28082
  1198
  in (xs', ctxt'') end;
wenzelm@5819
  1199
wenzelm@8096
  1200
end;
wenzelm@5819
  1201
wenzelm@18672
  1202
wenzelm@18672
  1203
(* fixes vs. frees *)
wenzelm@12016
  1204
wenzelm@32784
  1205
fun auto_fixes (ctxt, (propss, x)) =
wenzelm@21370
  1206
  ((fold o fold) Variable.auto_fixes propss ctxt, (propss, x));
wenzelm@18672
  1207
wenzelm@18672
  1208
fun bind_fixes xs ctxt =
wenzelm@9291
  1209
  let
wenzelm@30763
  1210
    val (_, ctxt') = ctxt |> add_fixes (map (fn x => (Binding.name x, NONE, NoSyn)) xs);
wenzelm@9291
  1211
    fun bind (t as Free (x, T)) =
wenzelm@18340
  1212
          if member (op =) xs x then
skalberg@15531
  1213
            (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
wenzelm@9291
  1214
          else t
wenzelm@9291
  1215
      | bind (t $ u) = bind t $ bind u
wenzelm@9291
  1216
      | bind (Abs (x, T, t)) = Abs (x, T, bind t)
wenzelm@9291
  1217
      | bind a = a;
wenzelm@18672
  1218
  in (bind, ctxt') end;
wenzelm@18672
  1219
wenzelm@9291
  1220
wenzelm@9291
  1221
wenzelm@18672
  1222
(** assumptions **)
wenzelm@18187
  1223
wenzelm@20209
  1224
local
wenzelm@20209
  1225
wenzelm@20209
  1226
fun gen_assms prepp exp args ctxt =
wenzelm@20209
  1227
  let
wenzelm@20234
  1228
    val cert = Thm.cterm_of (theory_of ctxt);
wenzelm@20209
  1229
    val (propss, ctxt1) = swap (prepp (ctxt, map snd args));
wenzelm@20234
  1230
    val _ = Variable.warn_extra_tfrees ctxt ctxt1;
wenzelm@20234
  1231
    val (premss, ctxt2) = fold_burrow (Assumption.add_assms exp o map cert) propss ctxt1;
wenzelm@20234
  1232
  in
wenzelm@20234
  1233
    ctxt2
wenzelm@20234
  1234
    |> auto_bind_facts (flat propss)
wenzelm@33644
  1235
    |> note_thmss "" (map fst args ~~ map (map (fn th => ([th], []))) premss)
wenzelm@20234
  1236
  end;
wenzelm@20209
  1237
wenzelm@20209
  1238
in
wenzelm@20209
  1239
wenzelm@20209
  1240
val add_assms = gen_assms (apsnd #1 o bind_propp);
wenzelm@20209
  1241
val add_assms_i = gen_assms (apsnd #1 o bind_propp_i);
wenzelm@20209
  1242
wenzelm@20209
  1243
end;
wenzelm@20209
  1244
wenzelm@20209
  1245
wenzelm@5819
  1246
wenzelm@8373
  1247
(** cases **)
wenzelm@8373
  1248
wenzelm@16147
  1249
local
wenzelm@16147
  1250
wenzelm@16668
  1251
fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
wenzelm@16147
  1252
wenzelm@18476
  1253
fun add_case _ ("", _) cases = cases
wenzelm@18476
  1254
  | add_case _ (name, NONE) cases = rem_case name cases
wenzelm@18476
  1255
  | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
wenzelm@16147
  1256
wenzelm@18678
  1257
fun prep_case name fxs c =
wenzelm@18609
  1258
  let
wenzelm@18609
  1259
    fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
wenzelm@18609
  1260
      | replace [] ys = ys
wenzelm@18678
  1261
      | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
wenzelm@33368
  1262
    val Rule_Cases.Case {fixes, assumes, binds, cases} = c;
wenzelm@18609
  1263
    val fixes' = replace fxs fixes;
wenzelm@18609
  1264
    val binds' = map drop_schematic binds;
wenzelm@18609
  1265
  in
wenzelm@18609
  1266
    if null (fold (Term.add_tvarsT o snd) fixes []) andalso
wenzelm@18609
  1267
      null (fold (fold Term.add_vars o snd) assumes []) then
wenzelm@33368
  1268
        Rule_Cases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
wenzelm@18678
  1269
    else error ("Illegal schematic variable(s) in case " ^ quote name)
wenzelm@18609
  1270
  end;
wenzelm@18609
  1271
wenzelm@18672
  1272
fun fix (x, T) ctxt =
wenzelm@18672
  1273
  let
wenzelm@18672
  1274
    val (bind, ctxt') = bind_fixes [x] ctxt;
wenzelm@18672
  1275
    val t = bind (Free (x, T));
wenzelm@20163
  1276
  in (t, ctxt' |> Variable.declare_constraints t) end;
wenzelm@18672
  1277
wenzelm@16147
  1278
in
wenzelm@16147
  1279
wenzelm@18672
  1280
fun add_cases is_proper = map_cases o fold (add_case is_proper);
wenzelm@18609
  1281
wenzelm@18609
  1282
fun case_result c ctxt =
wenzelm@18609
  1283
  let
wenzelm@33368
  1284
    val Rule_Cases.Case {fixes, ...} = c;
wenzelm@18672
  1285
    val (ts, ctxt') = ctxt |> fold_map fix fixes;
wenzelm@33368
  1286
    val Rule_Cases.Case {assumes, binds, cases, ...} = Rule_Cases.apply ts c;
wenzelm@18609
  1287
  in
wenzelm@18609
  1288
    ctxt'
wenzelm@30757
  1289
    |> bind_terms (map drop_schematic binds)
wenzelm@18609
  1290
    |> add_cases true (map (apsnd SOME) cases)
wenzelm@18609
  1291
    |> pair (assumes, (binds, cases))
wenzelm@18609
  1292
  end;
wenzelm@18609
  1293
wenzelm@18609
  1294
val apply_case = apfst fst oo case_result;
wenzelm@18609
  1295
wenzelm@16540
  1296
fun get_case ctxt name xs =
wenzelm@17184
  1297
  (case AList.lookup (op =) (cases_of ctxt) name of
wenzelm@18678
  1298
    NONE => error ("Unknown case: " ^ quote name)
wenzelm@18678
  1299
  | SOME (c, _) => prep_case name xs c);
wenzelm@8373
  1300
wenzelm@16147
  1301
end;
wenzelm@8373
  1302
wenzelm@8373
  1303
wenzelm@8373
  1304
wenzelm@10810
  1305
(** print context information **)
wenzelm@10810
  1306
wenzelm@12072
  1307
(* local syntax *)
wenzelm@12072
  1308
wenzelm@12093
  1309
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@12072
  1310
wenzelm@12072
  1311
wenzelm@21728
  1312
(* abbreviations *)
wenzelm@18971
  1313
wenzelm@21728
  1314
fun pretty_abbrevs show_globals ctxt =
wenzelm@18971
  1315
  let
wenzelm@25052
  1316
    val ((space, consts), (_, globals)) =
wenzelm@19033
  1317
      pairself (#constants o Consts.dest) (#consts (rep_context ctxt));
wenzelm@21803
  1318
    fun add_abbr (_, (_, NONE)) = I
wenzelm@25406
  1319
      | add_abbr (c, (T, SOME t)) =
wenzelm@21728
  1320
          if not show_globals andalso Symtab.defined globals c then I
wenzelm@21728
  1321
          else cons (c, Logic.mk_equals (Const (c, T), t));
wenzelm@33095
  1322
    val abbrevs = Name_Space.extern_table (space, Symtab.make (Symtab.fold add_abbr consts []));
wenzelm@18971
  1323
  in
wenzelm@35141
  1324
    if null abbrevs then []
wenzelm@21728
  1325
    else [Pretty.big_list "abbreviations:" (map (pretty_term_abbrev ctxt o #2) abbrevs)]
wenzelm@18971
  1326
  end;
wenzelm@18971
  1327
wenzelm@21728
  1328
val print_abbrevs = Pretty.writeln o Pretty.chunks o pretty_abbrevs true;
wenzelm@21728
  1329
wenzelm@18971
  1330
wenzelm@10810
  1331
(* term bindings *)
wenzelm@10810
  1332
wenzelm@16540
  1333
fun pretty_binds ctxt =
wenzelm@10810
  1334
  let
wenzelm@19897
  1335
    val binds = Variable.binds_of ctxt;
wenzelm@21728
  1336
    fun prt_bind (xi, (T, t)) = pretty_term_abbrev ctxt (Logic.mk_equals (Var (xi, T), t));
wenzelm@10810
  1337
  in
wenzelm@35141
  1338
    if Vartab.is_empty binds then []
wenzelm@15758
  1339
    else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
wenzelm@10810
  1340
  end;
wenzelm@10810
  1341
wenzelm@10810
  1342
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@10810
  1343
wenzelm@10810
  1344
wenzelm@10810
  1345
(* local theorems *)
wenzelm@10810
  1346
wenzelm@16540
  1347
fun pretty_lthms ctxt =
wenzelm@20012
  1348
  let
wenzelm@26284
  1349
    val local_facts = facts_of ctxt;
wenzelm@26284
  1350
    val props = Facts.props local_facts;
wenzelm@26673
  1351
    val facts =
wenzelm@35141
  1352
      (if null props then [] else [("<unnamed>", props)]) @
wenzelm@28212
  1353
      Facts.dest_static [] local_facts;
wenzelm@20012
  1354
  in
wenzelm@35141
  1355
    if null facts then []
wenzelm@28212
  1356
    else [Pretty.big_list "facts:" (map #1 (sort_wrt (#1 o #2) (map (`(pretty_fact ctxt)) facts)))]
wenzelm@20012
  1357
  end;
wenzelm@10810
  1358
wenzelm@12057
  1359
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
wenzelm@10810
  1360
wenzelm@10810
  1361
wenzelm@10810
  1362
(* local contexts *)
wenzelm@10810
  1363
wenzelm@26722
  1364
local
wenzelm@26722
  1365
wenzelm@26722
  1366
fun pretty_case (name, (fixes, ((asms, (lets, cs)), ctxt))) =
wenzelm@10810
  1367
  let
wenzelm@24922
  1368
    val prt_term = Syntax.pretty_term ctxt;
wenzelm@12057
  1369
wenzelm@10810
  1370
    fun prt_let (xi, t) = Pretty.block
wenzelm@10818
  1371
      [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
wenzelm@10810
  1372
        Pretty.quote (prt_term t)];
wenzelm@10810
  1373
wenzelm@13425
  1374
    fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
wenzelm@13425
  1375
      ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
wenzelm@13425
  1376
wenzelm@10810
  1377
    fun prt_sect _ _ _ [] = []
wenzelm@35141
  1378
      | prt_sect s sep prt xs =
wenzelm@35141
  1379
          [Pretty.block (Pretty.breaks (Pretty.str s ::
wenzelm@35141
  1380
            flat (separate sep (map (single o prt) xs))))];
wenzelm@26722
  1381
  in
wenzelm@26722
  1382
    Pretty.block (Pretty.fbreaks
wenzelm@10810
  1383
      (Pretty.str (name ^ ":") ::
wenzelm@11915
  1384
        prt_sect "fix" [] (Pretty.str o fst) fixes @
wenzelm@10810
  1385
        prt_sect "let" [Pretty.str "and"] prt_let
wenzelm@19482
  1386
          (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
wenzelm@13425
  1387
        (if forall (null o #2) asms then []
wenzelm@18609
  1388
          else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
wenzelm@26722
  1389
        prt_sect "subcases:" [] (Pretty.str o fst) cs))
wenzelm@26722
  1390
  end;
wenzelm@16540
  1391
wenzelm@26722
  1392
in
wenzelm@26722
  1393
wenzelm@26722
  1394
fun pretty_cases ctxt =
wenzelm@26722
  1395
  let
wenzelm@18476
  1396
    fun add_case (_, (_, false)) = I
wenzelm@33368
  1397
      | add_case (name, (c as Rule_Cases.Case {fixes, ...}, true)) =
wenzelm@26722
  1398
          cons (name, (fixes, case_result c ctxt));
wenzelm@18476
  1399
    val cases = fold add_case (cases_of ctxt) [];
wenzelm@10810
  1400
  in
wenzelm@35141
  1401
    if null cases then []
wenzelm@26722
  1402
    else [Pretty.big_list "cases:" (map pretty_case cases)]
wenzelm@10810
  1403
  end;
wenzelm@10810
  1404
wenzelm@10810
  1405
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@10810
  1406
wenzelm@26722
  1407
end;
wenzelm@26722
  1408
wenzelm@10810
  1409
wenzelm@12057
  1410
(* core context *)
wenzelm@10810
  1411
wenzelm@35141
  1412
val debug = Unsynchronized.ref false;
wenzelm@35141
  1413
val verbose = Unsynchronized.ref false;
wenzelm@32738
  1414
val prems_limit = Unsynchronized.ref ~1;
wenzelm@10810
  1415
wenzelm@18672
  1416
fun pretty_ctxt ctxt =
wenzelm@20310
  1417
  if ! prems_limit < 0 andalso not (! debug) then []
wenzelm@20310
  1418
  else
wenzelm@20310
  1419
    let
wenzelm@24922
  1420
      val prt_term = Syntax.pretty_term ctxt;
wenzelm@12057
  1421
wenzelm@20310
  1422
      (*structures*)
wenzelm@35429
  1423
      val {structs, ...} = Local_Syntax.idents_of (syntax_of ctxt);
wenzelm@35139
  1424
      val prt_structs =
wenzelm@35139
  1425
        if null structs then []
wenzelm@20310
  1426
        else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
wenzelm@20310
  1427
          Pretty.commas (map Pretty.str structs))];
wenzelm@12093
  1428
wenzelm@20310
  1429
      (*fixes*)
wenzelm@20310
  1430
      fun prt_fix (x, x') =
wenzelm@20310
  1431
        if x = x' then Pretty.str x
wenzelm@20310
  1432
        else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@20310
  1433
      val fixes =
wenzelm@20310
  1434
        rev (filter_out ((can Name.dest_internal orf member (op =) structs) o #1)
wenzelm@20310
  1435
          (Variable.fixes_of ctxt));
wenzelm@35139
  1436
      val prt_fixes =
wenzelm@35139
  1437
        if null fixes then []
wenzelm@20310
  1438
        else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@20310
  1439
          Pretty.commas (map prt_fix fixes))];
wenzelm@12057
  1440
wenzelm@20310
  1441
      (*prems*)
wenzelm@30473
  1442
      val prems = Assumption.all_prems_of ctxt;
wenzelm@20310
  1443
      val len = length prems;
wenzelm@20367
  1444
      val suppressed = len - ! prems_limit;
wenzelm@35139
  1445
      val prt_prems =
wenzelm@35139
  1446
        if null prems then []
wenzelm@20367
  1447
        else [Pretty.big_list "prems:" ((if suppressed <= 0 then [] else [Pretty.str "..."]) @
haftmann@33957
  1448
          map (Display.pretty_thm ctxt) (drop suppressed prems))];
wenzelm@20310
  1449
    in prt_structs @ prt_fixes @ prt_prems end;
wenzelm@10810
  1450
wenzelm@10810
  1451
wenzelm@10810
  1452
(* main context *)
wenzelm@10810
  1453
wenzelm@16540
  1454
fun pretty_context ctxt =
wenzelm@10810
  1455
  let
wenzelm@35141
  1456
    val is_verbose = ! verbose;
wenzelm@35141
  1457
    fun verb f x = if is_verbose then f (x ()) else [];
wenzelm@35141
  1458
wenzelm@24922
  1459
    val prt_term = Syntax.pretty_term ctxt;
wenzelm@24922
  1460
    val prt_typ = Syntax.pretty_typ ctxt;
wenzelm@24922
  1461
    val prt_sort = Syntax.pretty_sort ctxt;
wenzelm@10810
  1462
wenzelm@10810
  1463
    (*theory*)
wenzelm@12057
  1464
    val pretty_thy = Pretty.block
wenzelm@17384
  1465
      [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
wenzelm@10810
  1466
wenzelm@10810
  1467
    (*defaults*)
wenzelm@10810
  1468
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@10810
  1469
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@10810
  1470
wenzelm@10810
  1471
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@10810
  1472
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@10810
  1473
wenzelm@10810
  1474
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@10810
  1475
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@10810
  1476
wenzelm@10810
  1477
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@10810
  1478
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@16540
  1479
wenzelm@20163
  1480
    val (types, sorts) = Variable.constraints_of ctxt;
wenzelm@10810
  1481
  in
wenzelm@18609
  1482
    verb single (K pretty_thy) @
wenzelm@18672
  1483
    pretty_ctxt ctxt @
wenzelm@21728
  1484
    verb (pretty_abbrevs false) (K ctxt) @
wenzelm@10810
  1485
    verb pretty_binds (K ctxt) @
wenzelm@12057
  1486
    verb pretty_lthms (K ctxt) @
wenzelm@10810
  1487
    verb pretty_cases (K ctxt) @
wenzelm@18609
  1488
    verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@20163
  1489
    verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
wenzelm@10810
  1490
  end;
wenzelm@10810
  1491
wenzelm@5819
  1492
end;
wenzelm@35429
  1493