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