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