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