src/Pure/Isar/proof_context.ML
author wenzelm
Wed Aug 06 00:12:02 2008 +0200 (2008-08-06 ago)
changeset 27754 36df922b82d4
parent 27738 66596d7aa899
child 27785 3bf65bfda540
permissions -rw-r--r--
parse_sort/typ/term/prop: report markup;
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@24778
   134
  val add_abbrev: string -> Markup.property list ->
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@27314
   148
  val query_type: Proof.context -> string -> Markup.property list
wenzelm@27314
   149
  val query_const: Proof.context -> string -> Markup.property list
wenzelm@27314
   150
  val query_class: Proof.context -> string -> Markup.property list
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@26705
   376
  Pretty.mark (Markup.classN, []) (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@25328
   414
fun read_tyname ctxt c =
wenzelm@25328
   415
  if Syntax.is_tid c then
wenzelm@25328
   416
    TFree (c, the_default (Sign.defaultS (theory_of ctxt)) (Variable.def_sort ctxt (c, ~1)))
wenzelm@25328
   417
  else
wenzelm@25328
   418
    let
wenzelm@25328
   419
      val thy = theory_of ctxt;
wenzelm@25328
   420
      val d = Sign.intern_type thy c;
wenzelm@25328
   421
    in Type (d, replicate (Sign.arity_number thy d) dummyT) end;
wenzelm@25328
   422
wenzelm@25345
   423
fun read_const_proper ctxt c =
wenzelm@25328
   424
  (case Variable.lookup_const ctxt c of
wenzelm@25328
   425
    SOME d => Const (d, Consts.type_scheme (consts_of ctxt) d handle TYPE (msg, _, _) => error msg)
wenzelm@25328
   426
  | NONE => Consts.read_const (consts_of ctxt) c);
wenzelm@25328
   427
wenzelm@25328
   428
fun read_const ctxt c =
wenzelm@25328
   429
  (case (lookup_skolem ctxt c, Variable.is_const ctxt c) of
wenzelm@25328
   430
    (SOME x, false) => Free (x, infer_type ctxt x)
wenzelm@25345
   431
  | _ => read_const_proper ctxt c);
wenzelm@25328
   432
wenzelm@25328
   433
wenzelm@24684
   434
(* read_term *)
wenzelm@24684
   435
wenzelm@24684
   436
fun read_term_mode mode ctxt = Syntax.read_term (set_mode mode ctxt);
wenzelm@24684
   437
wenzelm@24684
   438
val read_term_pattern   = read_term_mode mode_pattern;
wenzelm@24684
   439
val read_term_schematic = read_term_mode mode_schematic;
wenzelm@24684
   440
val read_term_abbrev    = read_term_mode mode_abbrev;
wenzelm@24684
   441
wenzelm@24684
   442
wenzelm@19001
   443
(* local abbreviations *)
wenzelm@5819
   444
haftmann@26240
   445
val tsig_of = Sign.tsig_of o ProofContext.theory_of;
haftmann@26240
   446
wenzelm@24501
   447
local
wenzelm@24501
   448
haftmann@26731
   449
fun certify_consts ctxt = Consts.certify (Syntax.pp ctxt) (tsig_of ctxt)
wenzelm@27286
   450
  (not (abbrev_mode ctxt)) (consts_of ctxt);
wenzelm@19001
   451
wenzelm@19897
   452
fun reject_schematic (Var (xi, _)) =
wenzelm@22678
   453
      error ("Unbound schematic variable: " ^ Term.string_of_vname xi)
wenzelm@19897
   454
  | reject_schematic (Abs (_, _, t)) = reject_schematic t
wenzelm@19897
   455
  | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
wenzelm@19897
   456
  | reject_schematic _ = ();
wenzelm@5819
   457
wenzelm@24495
   458
fun expand_binds ctxt =
wenzelm@24495
   459
  let val Mode {pattern, schematic, ...} = get_mode ctxt in
wenzelm@24495
   460
    if pattern then I
wenzelm@24495
   461
    else Variable.expand_binds ctxt #> (if schematic then I else tap reject_schematic)
wenzelm@24495
   462
  end;
wenzelm@5819
   463
wenzelm@24501
   464
in
wenzelm@24501
   465
wenzelm@24501
   466
fun expand_abbrevs ctxt = certify_consts ctxt #> expand_binds ctxt;
wenzelm@24501
   467
wenzelm@24501
   468
end;
wenzelm@24501
   469
wenzelm@5819
   470
wenzelm@24922
   471
fun contract_abbrevs ctxt t =
wenzelm@24922
   472
  let
wenzelm@24922
   473
    val thy = theory_of ctxt;
wenzelm@24922
   474
    val consts = consts_of ctxt;
wenzelm@24922
   475
    val Mode {abbrev, ...} = get_mode ctxt;
wenzelm@24922
   476
  in
wenzelm@25052
   477
    if abbrev orelse print_mode_active "no_abbrevs" orelse not (can Term.type_of t) then t
wenzelm@25052
   478
    else t |> Pattern.rewrite_term thy (Consts.abbrevs_of consts (print_mode_value () @ [""])) []
wenzelm@24922
   479
  end;
wenzelm@24922
   480
wenzelm@24922
   481
wenzelm@24518
   482
(* patterns *)
wenzelm@24518
   483
wenzelm@24518
   484
fun prepare_patternT ctxt =
wenzelm@24518
   485
  let val Mode {pattern, schematic, ...} = get_mode ctxt in
wenzelm@24518
   486
    if pattern orelse schematic then I
wenzelm@24518
   487
    else Term.map_atyps
wenzelm@24518
   488
      (fn T as TVar (xi, _) =>
wenzelm@24518
   489
            if not (TypeInfer.is_param xi)
wenzelm@24518
   490
            then error ("Illegal schematic type variable: " ^ Term.string_of_vname xi)
wenzelm@24518
   491
            else T
wenzelm@24518
   492
        | T => T)
wenzelm@24518
   493
  end;
wenzelm@24518
   494
wenzelm@22712
   495
wenzelm@24505
   496
local
wenzelm@6550
   497
wenzelm@27259
   498
structure AllowDummies = ProofDataFun(type T = bool fun init _ = false);
wenzelm@27259
   499
wenzelm@27259
   500
fun check_dummies ctxt t =
wenzelm@27259
   501
  if AllowDummies.get ctxt then t
wenzelm@27259
   502
  else Term.no_dummy_patterns t handle TERM _ => error "Illegal dummy pattern(s) in term";
wenzelm@27259
   503
wenzelm@24767
   504
fun prepare_dummies ts = #1 (fold_map Term.replace_dummy_patterns ts 1);
wenzelm@6762
   505
wenzelm@27259
   506
in
wenzelm@6550
   507
wenzelm@27259
   508
val allow_dummies = AllowDummies.put true;
wenzelm@24505
   509
wenzelm@24684
   510
fun prepare_patterns ctxt =
wenzelm@24518
   511
  let val Mode {pattern, ...} = get_mode ctxt in
wenzelm@24767
   512
    TypeInfer.fixate_params (Variable.names_of ctxt) #>
wenzelm@24767
   513
    pattern ? Variable.polymorphic ctxt #>
wenzelm@24684
   514
    (map o Term.map_types) (prepare_patternT ctxt) #>
wenzelm@27259
   515
    (if pattern then prepare_dummies else map (check_dummies ctxt))
wenzelm@24505
   516
  end;
wenzelm@24505
   517
wenzelm@24505
   518
end;
wenzelm@24505
   519
wenzelm@6550
   520
wenzelm@22763
   521
(* decoding raw terms (syntax trees) *)
wenzelm@22763
   522
wenzelm@27286
   523
(* types *)
wenzelm@27286
   524
wenzelm@27286
   525
fun get_sort thy def_sort raw_env =
wenzelm@27286
   526
  let
wenzelm@27286
   527
    val tsig = Sign.tsig_of thy;
wenzelm@27286
   528
wenzelm@27286
   529
    fun eq ((xi, S), (xi', S')) =
wenzelm@27286
   530
      Term.eq_ix (xi, xi') andalso Type.eq_sort tsig (S, S');
wenzelm@27286
   531
    val env = distinct eq raw_env;
wenzelm@27286
   532
    val _ = (case duplicates (eq_fst (op =)) env of [] => ()
wenzelm@27286
   533
      | dups => error ("Inconsistent sort constraints for type variable(s) "
wenzelm@27286
   534
          ^ commas_quote (map (Term.string_of_vname' o fst) dups)));
wenzelm@27286
   535
wenzelm@27286
   536
    fun get xi =
wenzelm@27286
   537
      (case (AList.lookup (op =) env xi, def_sort xi) of
wenzelm@27286
   538
        (NONE, NONE) => Type.defaultS tsig
wenzelm@27286
   539
      | (NONE, SOME S) => S
wenzelm@27286
   540
      | (SOME S, NONE) => S
wenzelm@27286
   541
      | (SOME S, SOME S') =>
wenzelm@27286
   542
          if Type.eq_sort tsig (S, S') then S'
wenzelm@27286
   543
          else error ("Sort constraint inconsistent with default for type variable " ^
wenzelm@27286
   544
            quote (Term.string_of_vname' xi)));
wenzelm@27286
   545
  in get end;
wenzelm@27286
   546
wenzelm@25353
   547
local
wenzelm@25353
   548
wenzelm@25353
   549
fun intern_skolem ctxt def_type x =
wenzelm@22763
   550
  let
wenzelm@25353
   551
    val _ = no_skolem false x;
wenzelm@22763
   552
    val sko = lookup_skolem ctxt x;
wenzelm@25452
   553
    val is_const = can (read_const_proper ctxt) x orelse NameSpace.is_qualified x;
wenzelm@22763
   554
    val is_declared = is_some (def_type (x, ~1));
wenzelm@25319
   555
  in
wenzelm@25353
   556
    if Variable.is_const ctxt x then NONE
wenzelm@25353
   557
    else if is_some sko then sko
wenzelm@25353
   558
    else if not is_const orelse is_declared then SOME x
wenzelm@25353
   559
    else NONE
wenzelm@25319
   560
  end;
wenzelm@22763
   561
wenzelm@25353
   562
in
wenzelm@25353
   563
wenzelm@24371
   564
fun term_context ctxt =
wenzelm@24371
   565
  let val thy = theory_of ctxt in
wenzelm@27286
   566
   {get_sort = get_sort thy (Variable.def_sort ctxt),
wenzelm@27195
   567
    map_const = fn a => ((true, #1 (Term.dest_Const (read_const_proper ctxt a)))
wenzelm@27195
   568
      handle ERROR _ => (false, Consts.intern (consts_of ctxt) a)),
wenzelm@25353
   569
    map_free = intern_skolem ctxt (Variable.def_type ctxt false),
wenzelm@24371
   570
    map_type = Sign.intern_tycons thy,
wenzelm@24371
   571
    map_sort = Sign.intern_sort thy}
wenzelm@24371
   572
  end;
wenzelm@24371
   573
wenzelm@22763
   574
fun decode_term ctxt =
wenzelm@24371
   575
  let val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt
wenzelm@22763
   576
  in Syntax.decode_term get_sort map_const map_free map_type map_sort end;
wenzelm@22763
   577
wenzelm@10554
   578
end;
wenzelm@10554
   579
wenzelm@5819
   580
wenzelm@5819
   581
(* certify terms *)
wenzelm@5819
   582
wenzelm@10554
   583
local
wenzelm@10554
   584
wenzelm@24684
   585
fun gen_cert prop ctxt t =
wenzelm@24684
   586
  t
wenzelm@24684
   587
  |> expand_abbrevs ctxt
wenzelm@24961
   588
  |> (fn t' => #1 (Sign.certify' prop (Syntax.pp ctxt) false (consts_of ctxt) (theory_of ctxt) t')
wenzelm@24684
   589
    handle TYPE (msg, _, _) => error msg
wenzelm@24684
   590
      | TERM (msg, _) => error msg);
wenzelm@16501
   591
wenzelm@10554
   592
in
wenzelm@8096
   593
wenzelm@24684
   594
val cert_term = gen_cert false;
wenzelm@24684
   595
val cert_prop = gen_cert true;
wenzelm@10554
   596
wenzelm@10554
   597
end;
wenzelm@5819
   598
wenzelm@5819
   599
wenzelm@24495
   600
(* type checking/inference *)
wenzelm@22701
   601
wenzelm@24495
   602
fun standard_infer_types ctxt ts =
wenzelm@24495
   603
  let val Mode {pattern, ...} = get_mode ctxt in
wenzelm@24961
   604
    TypeInfer.infer_types (Syntax.pp ctxt) (tsig_of ctxt) (Syntax.check_typs ctxt)
wenzelm@24495
   605
      (try (Consts.the_constraint (consts_of ctxt))) (Variable.def_type ctxt pattern)
wenzelm@27264
   606
      (Variable.names_of ctxt) (Variable.maxidx_of ctxt) ts
wenzelm@24495
   607
    handle TYPE (msg, _, _) => error msg
wenzelm@24495
   608
  end;
wenzelm@24486
   609
wenzelm@25406
   610
local
wenzelm@25406
   611
wenzelm@24518
   612
fun standard_typ_check ctxt =
wenzelm@24518
   613
  map (cert_typ_mode (Type.get_mode ctxt) ctxt) #>
wenzelm@24518
   614
  map (prepare_patternT ctxt);
wenzelm@24518
   615
wenzelm@24769
   616
fun standard_term_check ctxt =
wenzelm@24769
   617
  standard_infer_types ctxt #>
wenzelm@24769
   618
  map (expand_abbrevs ctxt);
wenzelm@24922
   619
wenzelm@24922
   620
fun standard_term_uncheck ctxt =
wenzelm@24922
   621
  map (contract_abbrevs ctxt);
wenzelm@24922
   622
wenzelm@26463
   623
fun add eq what f = Context.>> (what (fn xs => fn ctxt =>
wenzelm@26463
   624
  let val xs' = f ctxt xs in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end));
wenzelm@24767
   625
wenzelm@24518
   626
in
wenzelm@24518
   627
wenzelm@25060
   628
val _ = add (op =) (Syntax.add_typ_check 0 "standard") standard_typ_check;
wenzelm@25060
   629
val _ = add (op aconv) (Syntax.add_term_check 0 "standard") standard_term_check;
wenzelm@25060
   630
val _ = add (op aconv) (Syntax.add_term_check 100 "fixate") prepare_patterns;
wenzelm@24922
   631
wenzelm@25060
   632
val _ = add (op aconv) (Syntax.add_term_uncheck 0 "standard") standard_term_uncheck;
wenzelm@22701
   633
wenzelm@24518
   634
end;
wenzelm@22701
   635
wenzelm@22701
   636
wenzelm@9553
   637
wenzelm@24767
   638
(** inner syntax operations **)
wenzelm@24371
   639
wenzelm@24371
   640
local
wenzelm@24371
   641
wenzelm@27754
   642
fun parse_token markup text =
wenzelm@27754
   643
  if YXML.detect text then
wenzelm@27754
   644
    (case YXML.parse text of
wenzelm@27754
   645
      XML.Elem ("token", props, [XML.Text str]) =>
wenzelm@27754
   646
        let
wenzelm@27754
   647
          val pos = Position.of_properties props;
wenzelm@27754
   648
          val _ = Position.report markup pos;
wenzelm@27754
   649
        in (translate_string (fn c => if c = Symbol.DEL then "" else c) str, pos) end
wenzelm@27754
   650
    | _ => (text, Position.none))
wenzelm@27754
   651
  else (text, Position.none);
wenzelm@24371
   652
wenzelm@27754
   653
fun parse_sort ctxt text =
wenzelm@27754
   654
  let
wenzelm@27754
   655
    val (str, pos) = parse_token Markup.sort text;
wenzelm@27754
   656
    val S = Syntax.standard_parse_sort ctxt (syn_of ctxt) (Sign.intern_sort (theory_of ctxt)) str
wenzelm@27754
   657
      handle ERROR msg => cat_error msg  ("Failed to parse sort" ^ Position.str_of pos)
wenzelm@27754
   658
  in S end;
wenzelm@27754
   659
wenzelm@27754
   660
fun parse_typ ctxt text =
wenzelm@24371
   661
  let
wenzelm@24371
   662
    val thy = ProofContext.theory_of ctxt;
wenzelm@27286
   663
    val get_sort = get_sort thy (Variable.def_sort ctxt);
wenzelm@27754
   664
wenzelm@27754
   665
    val (str, pos) = parse_token Markup.typ text;
wenzelm@24371
   666
    val T = Sign.intern_tycons thy
wenzelm@27754
   667
        (Syntax.standard_parse_typ ctxt (syn_of ctxt) get_sort (Sign.intern_sort thy) str)
wenzelm@27754
   668
      handle ERROR msg => cat_error msg  ("Failed to parse type" ^ Position.str_of pos);
wenzelm@27754
   669
  in T end;
wenzelm@24371
   670
wenzelm@27754
   671
fun parse_term T ctxt text =
wenzelm@24371
   672
  let
wenzelm@24371
   673
    val thy = theory_of ctxt;
wenzelm@27754
   674
    val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt;
wenzelm@27754
   675
wenzelm@25168
   676
    val (T', _) = TypeInfer.paramify_dummies T 0;
wenzelm@27754
   677
    val markup as (kind, _) = if T' = propT then Markup.proposition else Markup.term;
wenzelm@27754
   678
    val (str, pos) = parse_token markup text;
wenzelm@27754
   679
wenzelm@25476
   680
    fun check t = (Syntax.check_term ctxt (TypeInfer.constrain T' t); NONE)
wenzelm@25476
   681
      handle ERROR msg => SOME msg;
wenzelm@27754
   682
    val t = Syntax.standard_parse_term (Syntax.pp ctxt) check get_sort
wenzelm@27754
   683
        map_const map_free map_type map_sort ctxt (Sign.is_logtype thy) (syn_of ctxt) T' str
wenzelm@27754
   684
      handle ERROR msg => cat_error msg  ("Failed to parse " ^ kind ^ Position.str_of pos);
wenzelm@27754
   685
  in t end;
wenzelm@24371
   686
wenzelm@24922
   687
wenzelm@24922
   688
fun unparse_sort ctxt S =
wenzelm@24922
   689
  Syntax.standard_unparse_sort ctxt (syn_of ctxt) (Sign.extern_sort (theory_of ctxt) S);
wenzelm@24922
   690
wenzelm@24922
   691
fun unparse_typ ctxt T =
wenzelm@24922
   692
  Syntax.standard_unparse_typ ctxt (syn_of ctxt) (Sign.extern_typ (theory_of ctxt) T);
wenzelm@24922
   693
wenzelm@24922
   694
fun unparse_term ctxt t =
wenzelm@24922
   695
  let
wenzelm@24922
   696
    val thy = theory_of ctxt;
wenzelm@24922
   697
    val syntax = syntax_of ctxt;
wenzelm@24922
   698
    val consts = consts_of ctxt;
wenzelm@24922
   699
  in
wenzelm@24922
   700
    t
wenzelm@24922
   701
    |> Sign.extern_term (Consts.extern_early consts) thy
wenzelm@24922
   702
    |> LocalSyntax.extern_term syntax
wenzelm@24922
   703
    |> Syntax.standard_unparse_term (Consts.extern consts) ctxt (LocalSyntax.syn_of syntax)
wenzelm@26960
   704
        (not (PureThy.old_appl_syntax thy))
wenzelm@24922
   705
  end;
wenzelm@24922
   706
wenzelm@24371
   707
in
wenzelm@24371
   708
wenzelm@24767
   709
val _ = Syntax.install_operations
wenzelm@24767
   710
  {parse_sort = parse_sort,
wenzelm@24767
   711
   parse_typ = parse_typ,
wenzelm@24767
   712
   parse_term = parse_term dummyT,
wenzelm@24767
   713
   parse_prop = parse_term propT,
wenzelm@24922
   714
   unparse_sort = unparse_sort,
wenzelm@24922
   715
   unparse_typ = unparse_typ,
wenzelm@24922
   716
   unparse_term = unparse_term};
wenzelm@24371
   717
wenzelm@24371
   718
end;
wenzelm@24371
   719
wenzelm@24371
   720
wenzelm@24371
   721
wenzelm@21610
   722
(** export results **)
wenzelm@21531
   723
wenzelm@20310
   724
fun common_export is_goal inner outer =
wenzelm@20310
   725
  map (Assumption.export is_goal inner outer) #>
wenzelm@20310
   726
  Variable.export inner outer;
wenzelm@8616
   727
wenzelm@20310
   728
val goal_export = common_export true;
wenzelm@20310
   729
val export = common_export false;
wenzelm@12704
   730
wenzelm@21531
   731
fun export_morphism inner outer =
wenzelm@21531
   732
  Assumption.export_morphism inner outer $>
wenzelm@21531
   733
  Variable.export_morphism inner outer;
wenzelm@21531
   734
wenzelm@21531
   735
wenzelm@15758
   736
wenzelm@5819
   737
(** 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@19867
   744
  | SOME (env, _) => map (apsnd snd) (Envir.alist_of env));
wenzelm@8096
   745
wenzelm@8096
   746
wenzelm@8096
   747
(* add_binds(_i) *)
wenzelm@5819
   748
wenzelm@7925
   749
local
wenzelm@7925
   750
wenzelm@16031
   751
fun gen_bind prep (xi as (x, _), raw_t) ctxt =
wenzelm@24511
   752
  ctxt
wenzelm@24675
   753
  |> Variable.add_binds [(xi, Option.map (prep (set_mode mode_default ctxt)) raw_t)];
wenzelm@5819
   754
wenzelm@10810
   755
in
wenzelm@10810
   756
wenzelm@20330
   757
fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
wenzelm@10554
   758
  | drop_schematic b = b;
wenzelm@10554
   759
wenzelm@24511
   760
val add_binds = fold (gen_bind Syntax.read_term);
wenzelm@16031
   761
val add_binds_i = fold (gen_bind cert_term);
wenzelm@8616
   762
wenzelm@16458
   763
fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts));
wenzelm@12147
   764
val auto_bind_goal = auto_bind AutoBind.goal;
wenzelm@12147
   765
val auto_bind_facts = auto_bind AutoBind.facts;
wenzelm@7925
   766
wenzelm@7925
   767
end;
wenzelm@5819
   768
wenzelm@5819
   769
wenzelm@8096
   770
(* match_bind(_i) *)
wenzelm@5819
   771
wenzelm@8096
   772
local
wenzelm@8096
   773
wenzelm@24684
   774
fun gen_bind prep_terms gen raw_binds ctxt =
wenzelm@5819
   775
  let
wenzelm@24684
   776
    fun prep_bind (raw_pats, t) ctxt1 =
wenzelm@24684
   777
      let
wenzelm@24684
   778
        val T = Term.fastype_of t;
wenzelm@24684
   779
        val ctxt2 = Variable.declare_term t ctxt1;
wenzelm@24684
   780
        val pats = prep_terms (set_mode mode_pattern ctxt2) T raw_pats;
wenzelm@24684
   781
        val binds = simult_matches ctxt2 (t, pats);
wenzelm@24684
   782
      in (binds, ctxt2) end;
wenzelm@7670
   783
wenzelm@24686
   784
    val ts = prep_terms ctxt dummyT (map snd raw_binds);
wenzelm@24686
   785
    val (binds, ctxt') = apfst flat (fold_map prep_bind (map fst raw_binds ~~ ts) ctxt);
wenzelm@8616
   786
    val binds' =
wenzelm@19916
   787
      if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
wenzelm@8616
   788
      else binds;
skalberg@15531
   789
    val binds'' = map (apsnd SOME) binds';
wenzelm@18310
   790
    val ctxt'' =
wenzelm@19897
   791
      tap (Variable.warn_extra_tfrees ctxt)
wenzelm@18310
   792
       (if gen then
wenzelm@19897
   793
          ctxt (*sic!*) |> fold Variable.declare_term (map #2 binds') |> add_binds_i binds''
wenzelm@18310
   794
        else ctxt' |> add_binds_i binds'');
wenzelm@18310
   795
  in (ts, ctxt'') end;
wenzelm@8096
   796
wenzelm@8096
   797
in
wenzelm@5935
   798
wenzelm@24684
   799
fun read_terms ctxt T =
wenzelm@24684
   800
  map (Syntax.parse_term ctxt #> TypeInfer.constrain T) #> Syntax.check_terms ctxt;
wenzelm@24684
   801
wenzelm@24684
   802
val match_bind = gen_bind read_terms;
wenzelm@24684
   803
val match_bind_i = gen_bind (fn ctxt => fn _ => map (cert_term ctxt));
wenzelm@8096
   804
wenzelm@8096
   805
end;
wenzelm@5935
   806
wenzelm@5935
   807
wenzelm@10465
   808
(* propositions with patterns *)
wenzelm@5935
   809
wenzelm@10465
   810
local
wenzelm@8096
   811
wenzelm@24684
   812
fun prep_propp mode prep_props (context, args) =
wenzelm@10465
   813
  let
wenzelm@19585
   814
    fun prep (_, raw_pats) (ctxt, prop :: props) =
wenzelm@24684
   815
      let val ctxt' = Variable.declare_term prop ctxt
wenzelm@24684
   816
      in ((prop, prep_props (set_mode mode_pattern ctxt') raw_pats), (ctxt', props)) end;
wenzelm@24684
   817
wenzelm@17860
   818
    val (propp, (context', _)) = (fold_map o fold_map) prep args
wenzelm@24684
   819
      (context, prep_props (set_mode mode context) (maps (map fst) args));
wenzelm@10465
   820
  in (context', propp) end;
wenzelm@5935
   821
wenzelm@24684
   822
fun gen_bind_propp mode parse_prop (ctxt, raw_args) =
wenzelm@8096
   823
  let
wenzelm@24684
   824
    val (ctxt', args) = prep_propp mode parse_prop (ctxt, raw_args);
wenzelm@19585
   825
    val binds = flat (flat (map (map (simult_matches ctxt')) args));
wenzelm@10465
   826
    val propss = map (map #1) args;
wenzelm@8616
   827
wenzelm@10554
   828
    (*generalize result: context evaluated now, binds added later*)
wenzelm@19916
   829
    val gen = Variable.exportT_terms ctxt' ctxt;
skalberg@15531
   830
    fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
skalberg@15531
   831
  in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
wenzelm@8096
   832
wenzelm@10465
   833
in
wenzelm@10465
   834
wenzelm@24684
   835
val read_propp           = prep_propp mode_default Syntax.read_props;
wenzelm@24684
   836
val cert_propp           = prep_propp mode_default (map o cert_prop);
wenzelm@24684
   837
val read_propp_schematic = prep_propp mode_schematic Syntax.read_props;
wenzelm@24684
   838
val cert_propp_schematic = prep_propp mode_schematic (map o cert_prop);
wenzelm@10554
   839
wenzelm@24684
   840
val bind_propp             = gen_bind_propp mode_default Syntax.read_props;
wenzelm@24684
   841
val bind_propp_i           = gen_bind_propp mode_default (map o cert_prop);
wenzelm@24684
   842
val bind_propp_schematic   = gen_bind_propp mode_schematic Syntax.read_props;
wenzelm@24684
   843
val bind_propp_schematic_i = gen_bind_propp mode_schematic (map o cert_prop);
wenzelm@6789
   844
wenzelm@10465
   845
end;
wenzelm@10465
   846
wenzelm@6789
   847
wenzelm@5819
   848
wenzelm@5819
   849
(** theorems **)
wenzelm@5819
   850
wenzelm@18042
   851
(* fact_tac *)
wenzelm@18042
   852
wenzelm@18122
   853
fun comp_incr_tac [] _ st = no_tac st
wenzelm@18122
   854
  | comp_incr_tac (th :: ths) i st =
wenzelm@18122
   855
      (Goal.compose_hhf_tac (Drule.incr_indexes st th) i APPEND comp_incr_tac ths i) st;
wenzelm@18042
   856
wenzelm@21687
   857
fun fact_tac facts = Goal.norm_hhf_tac THEN' comp_incr_tac facts;
wenzelm@18122
   858
wenzelm@18122
   859
fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) =>
wenzelm@26284
   860
  fact_tac (Facts.could_unify (facts_of ctxt) (Term.strip_all_body goal)) i);
wenzelm@18042
   861
wenzelm@18042
   862
wenzelm@6091
   863
(* get_thm(s) *)
wenzelm@5819
   864
wenzelm@26361
   865
local
wenzelm@26361
   866
wenzelm@26687
   867
fun retrieve_thms pick ctxt (Facts.Fact s) =
wenzelm@16501
   868
      let
wenzelm@24511
   869
        val prop = Syntax.read_prop (set_mode mode_default ctxt) s
wenzelm@24511
   870
          |> singleton (Variable.polymorphic ctxt);
wenzelm@24511
   871
        val th = Goal.prove ctxt [] [] prop (K (ALLGOALS (some_fact_tac ctxt)))
wenzelm@18678
   872
          handle ERROR msg => cat_error msg "Failed to retrieve literal fact.";
wenzelm@18042
   873
      in pick "" [th] end
wenzelm@26687
   874
  | retrieve_thms pick ctxt xthmref =
wenzelm@18042
   875
      let
wenzelm@18042
   876
        val thy = theory_of ctxt;
wenzelm@26284
   877
        val local_facts = facts_of ctxt;
wenzelm@26673
   878
        val thmref = Facts.map_name_of_ref (Facts.intern local_facts) xthmref;
wenzelm@26336
   879
        val name = Facts.name_of_ref thmref;
wenzelm@24012
   880
        val thms =
wenzelm@24012
   881
          if name = "" then [Thm.transfer thy Drule.dummy_thm]
wenzelm@24012
   882
          else
wenzelm@26393
   883
            (case Facts.lookup (Context.Proof ctxt) local_facts name of
wenzelm@27738
   884
              SOME (_, ths) => map (Thm.transfer thy) (Facts.select thmref ths)
wenzelm@26687
   885
            | NONE => PureThy.get_fact (Context.Proof ctxt) thy xthmref);
wenzelm@24012
   886
      in pick name thms end;
wenzelm@5819
   887
wenzelm@26361
   888
in
wenzelm@26346
   889
wenzelm@26687
   890
val get_fact = retrieve_thms (K I);
wenzelm@26687
   891
val get_fact_single = retrieve_thms Facts.the_single;
wenzelm@26346
   892
wenzelm@26361
   893
fun get_thms ctxt = get_fact ctxt o Facts.named;
wenzelm@26361
   894
fun get_thm ctxt = get_fact_single ctxt o Facts.named;
wenzelm@26361
   895
wenzelm@26361
   896
end;
wenzelm@5819
   897
wenzelm@5819
   898
wenzelm@13425
   899
(* name space operations *)
wenzelm@12309
   900
haftmann@22352
   901
val add_path        = map_naming o NameSpace.add_path;
wenzelm@19062
   902
val no_base_names   = map_naming NameSpace.no_base_names;
wenzelm@16147
   903
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@19062
   904
val sticky_prefix   = map_naming o NameSpace.sticky_prefix;
wenzelm@19062
   905
val restore_naming  = map_naming o K o naming_of;
wenzelm@21728
   906
val reset_naming    = map_naming (K local_naming);
wenzelm@12309
   907
wenzelm@13425
   908
wenzelm@26284
   909
(* facts *)
wenzelm@5819
   910
wenzelm@26284
   911
fun update_thms _ (bname, NONE) ctxt = ctxt |> map_facts (Facts.del (full_name ctxt bname))
wenzelm@26284
   912
  | update_thms do_props (bname, SOME ths) ctxt = ctxt |> map_facts
wenzelm@26309
   913
      (Facts.add_local do_props (naming_of ctxt) (full_name ctxt bname, ths));
wenzelm@5819
   914
wenzelm@26250
   915
fun put_thms do_props thms ctxt =
wenzelm@26284
   916
  ctxt |> map_naming (K local_naming) |> update_thms do_props thms |> restore_naming ctxt;
wenzelm@5819
   917
wenzelm@12711
   918
local
wenzelm@16147
   919
wenzelm@21443
   920
fun gen_note_thmss get k = fold_map (fn ((bname, more_attrs), raw_facts) => fn ctxt =>
wenzelm@5819
   921
  let
wenzelm@21643
   922
    val name = full_name ctxt bname;
wenzelm@21643
   923
    val facts = PureThy.name_thmss false name (map (apfst (get ctxt)) raw_facts);
wenzelm@21443
   924
    fun app (th, attrs) x =
wenzelm@21643
   925
      swap (foldl_map (Thm.proof_attributes (attrs @ more_attrs @ [PureThy.kind k])) (x, th));
wenzelm@21443
   926
    val (res, ctxt') = fold_map app facts ctxt;
wenzelm@21643
   927
    val thms = PureThy.name_thms false false name (flat res);
wenzelm@24388
   928
    val Mode {stmt, ...} = get_mode ctxt;
wenzelm@26284
   929
  in ((bname, thms), ctxt' |> update_thms stmt (bname, SOME thms)) end);
wenzelm@12711
   930
wenzelm@12711
   931
in
wenzelm@12711
   932
wenzelm@26346
   933
fun note_thmss k = gen_note_thmss get_fact k;
haftmann@21622
   934
fun note_thmss_i k = gen_note_thmss (K I) k;
ballarin@15696
   935
wenzelm@12711
   936
end;
wenzelm@9196
   937
wenzelm@5819
   938
wenzelm@5819
   939
wenzelm@18672
   940
(** parameters **)
wenzelm@17360
   941
wenzelm@8096
   942
(* variables *)
wenzelm@8096
   943
wenzelm@19897
   944
fun declare_var (x, opt_T, mx) ctxt =
wenzelm@22701
   945
  let val T = (case opt_T of SOME T => T | NONE => Syntax.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@18672
   951
  fold_map (fn (raw_x, raw_T, raw_mx) => fn ctxt =>
wenzelm@18672
   952
    let
wenzelm@19371
   953
      val (x, mx) = Syntax.const_mixfix raw_x raw_mx;
wenzelm@25353
   954
      val _ = Syntax.is_identifier (no_skolem internal x) orelse
wenzelm@25353
   955
        error ("Illegal variable name: " ^ quote x);
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@18672
   961
      val var = (x, opt_T, mx);
wenzelm@19001
   962
    in (var, ctxt |> declare_var var |> #2) 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@19681
   972
(* authentic constants *)
wenzelm@19663
   973
wenzelm@25159
   974
local
wenzelm@25159
   975
wenzelm@25159
   976
fun const_ast_tr intern ctxt [Syntax.Variable c] =
wenzelm@19681
   977
      let
wenzelm@25345
   978
        val Const (c', _) = read_const_proper ctxt c;
wenzelm@25159
   979
        val d = if intern then const_syntax_name ctxt c' else c;
wenzelm@25159
   980
      in Syntax.Constant d end
wenzelm@25159
   981
  | const_ast_tr _ _ asts = raise Syntax.AST ("const_ast_tr", asts);
wenzelm@25159
   982
wenzelm@25159
   983
in
wenzelm@19681
   984
wenzelm@26463
   985
val _ = Context.>> (Context.map_theory
wenzelm@19681
   986
 (Sign.add_syntax
wenzelm@19681
   987
   [("_context_const", "id => 'a", Delimfix "CONST _"),
wenzelm@25159
   988
    ("_context_const", "longid => 'a", Delimfix "CONST _"),
wenzelm@25159
   989
    ("_context_xconst", "id => 'a", Delimfix "XCONST _"),
wenzelm@25159
   990
    ("_context_xconst", "longid => 'a", Delimfix "XCONST _")] #>
wenzelm@25159
   991
  Sign.add_advanced_trfuns
wenzelm@26463
   992
    ([("_context_const", const_ast_tr true), ("_context_xconst", const_ast_tr false)], [], [], [])));
wenzelm@25159
   993
wenzelm@25159
   994
end;
wenzelm@19681
   995
wenzelm@19663
   996
wenzelm@21744
   997
(* notation *)
wenzelm@21744
   998
wenzelm@24949
   999
local
wenzelm@24949
  1000
wenzelm@24949
  1001
fun const_syntax ctxt (Free (x, T), mx) = SOME (true, (x, T, mx))
wenzelm@24949
  1002
  | const_syntax ctxt (Const (c, _), mx) =
wenzelm@24949
  1003
      Option.map (pair false) (try (Consts.syntax (consts_of ctxt)) (c, mx))
wenzelm@24949
  1004
  | const_syntax _ _ = NONE;
wenzelm@24949
  1005
wenzelm@24949
  1006
in
wenzelm@21744
  1007
wenzelm@24949
  1008
fun notation add mode args ctxt =
wenzelm@24949
  1009
  ctxt |> map_syntax
wenzelm@24949
  1010
    (LocalSyntax.update_modesyntax (theory_of ctxt) add mode (map_filter (const_syntax ctxt) args));
wenzelm@24949
  1011
wenzelm@24949
  1012
fun target_notation add mode args phi =
wenzelm@24982
  1013
  let val args' = filter (fn (t, _) => Type.similar_types (t, Morphism.term phi t)) args;
wenzelm@24949
  1014
  in Context.mapping (Sign.notation add mode args') (notation add mode args') end;
wenzelm@24949
  1015
wenzelm@24949
  1016
end;
wenzelm@21744
  1017
wenzelm@21744
  1018
wenzelm@24767
  1019
(* local constants *)
wenzelm@24767
  1020
wenzelm@24767
  1021
fun add_const_constraint (c, opt_T) ctxt =
wenzelm@24767
  1022
  let
wenzelm@24767
  1023
    fun prepT raw_T =
wenzelm@24767
  1024
      let val T = cert_typ ctxt raw_T
wenzelm@24767
  1025
      in cert_term ctxt (Const (c, T)); T end;
haftmann@25039
  1026
  in ctxt |> (map_consts o apfst) (Consts.constrain (c, Option.map prepT opt_T)) end;
wenzelm@19001
  1027
wenzelm@24778
  1028
fun add_abbrev mode tags (c, raw_t) ctxt =
wenzelm@19001
  1029
  let
wenzelm@24675
  1030
    val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
wenzelm@21681
  1031
      handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
wenzelm@20008
  1032
    val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
wenzelm@21807
  1033
    val ((lhs, rhs), consts') = consts_of ctxt
wenzelm@24961
  1034
      |> Consts.abbreviate (Syntax.pp ctxt) (tsig_of ctxt) (naming_of ctxt) mode tags (c, t);
wenzelm@19001
  1035
  in
wenzelm@19001
  1036
    ctxt
haftmann@25039
  1037
    |> (map_consts o apfst) (K consts')
wenzelm@21803
  1038
    |> Variable.declare_term rhs
wenzelm@21803
  1039
    |> pair (lhs, rhs)
wenzelm@21704
  1040
  end;
wenzelm@19001
  1041
wenzelm@25052
  1042
fun revert_abbrev mode c = (map_consts o apfst) (Consts.revert_abbrev mode c);
wenzelm@25052
  1043
wenzelm@19001
  1044
wenzelm@18672
  1045
(* fixes *)
wenzelm@5819
  1046
wenzelm@8096
  1047
local
wenzelm@8096
  1048
wenzelm@19001
  1049
fun prep_mixfix (x, T, mx) =
wenzelm@19019
  1050
  if mx <> NoSyn andalso mx <> Structure andalso
wenzelm@20086
  1051
      (can Name.dest_internal x orelse can Name.dest_skolem x) then
wenzelm@19001
  1052
    error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x)
wenzelm@19001
  1053
  else (true, (x, T, mx));
wenzelm@19001
  1054
wenzelm@18844
  1055
fun gen_fixes prep raw_vars ctxt =
wenzelm@8096
  1056
  let
wenzelm@22712
  1057
    val (vars, _) = prep raw_vars ctxt;
wenzelm@22712
  1058
    val (xs', ctxt') = Variable.add_fixes (map #1 vars) ctxt;
wenzelm@8096
  1059
  in
wenzelm@22712
  1060
    ctxt'
wenzelm@19001
  1061
    |> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars)
wenzelm@19897
  1062
    |-> (map_syntax o LocalSyntax.add_syntax (theory_of ctxt) o map prep_mixfix)
wenzelm@18672
  1063
    |> pair xs'
wenzelm@8096
  1064
  end;
wenzelm@5819
  1065
wenzelm@8096
  1066
in
wenzelm@7679
  1067
wenzelm@18844
  1068
val add_fixes = gen_fixes read_vars;
wenzelm@18844
  1069
val add_fixes_i = gen_fixes cert_vars;
wenzelm@8096
  1070
wenzelm@8096
  1071
end;
wenzelm@5819
  1072
wenzelm@18672
  1073
wenzelm@18672
  1074
(* fixes vs. frees *)
wenzelm@12016
  1075
wenzelm@18672
  1076
fun auto_fixes (arg as (ctxt, (propss, x))) =
wenzelm@21370
  1077
  ((fold o fold) Variable.auto_fixes propss ctxt, (propss, x));
wenzelm@18672
  1078
wenzelm@18672
  1079
fun bind_fixes xs ctxt =
wenzelm@9291
  1080
  let
wenzelm@18672
  1081
    val (_, ctxt') = ctxt |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs);
wenzelm@9291
  1082
    fun bind (t as Free (x, T)) =
wenzelm@18340
  1083
          if member (op =) xs x then
skalberg@15531
  1084
            (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
wenzelm@9291
  1085
          else t
wenzelm@9291
  1086
      | bind (t $ u) = bind t $ bind u
wenzelm@9291
  1087
      | bind (Abs (x, T, t)) = Abs (x, T, bind t)
wenzelm@9291
  1088
      | bind a = a;
wenzelm@18672
  1089
  in (bind, ctxt') end;
wenzelm@18672
  1090
wenzelm@9291
  1091
wenzelm@9291
  1092
wenzelm@18672
  1093
(** assumptions **)
wenzelm@18187
  1094
wenzelm@20209
  1095
local
wenzelm@20209
  1096
wenzelm@20209
  1097
fun gen_assms prepp exp args ctxt =
wenzelm@20209
  1098
  let
wenzelm@20234
  1099
    val cert = Thm.cterm_of (theory_of ctxt);
wenzelm@20209
  1100
    val (propss, ctxt1) = swap (prepp (ctxt, map snd args));
wenzelm@20234
  1101
    val _ = Variable.warn_extra_tfrees ctxt ctxt1;
wenzelm@20234
  1102
    val (premss, ctxt2) = fold_burrow (Assumption.add_assms exp o map cert) propss ctxt1;
wenzelm@20234
  1103
  in
wenzelm@20234
  1104
    ctxt2
wenzelm@20234
  1105
    |> auto_bind_facts (flat propss)
wenzelm@21443
  1106
    |> note_thmss_i Thm.assumptionK (map fst args ~~ map (map (fn th => ([th], []))) premss)
wenzelm@20234
  1107
  end;
wenzelm@20209
  1108
wenzelm@20209
  1109
in
wenzelm@20209
  1110
wenzelm@20209
  1111
val add_assms = gen_assms (apsnd #1 o bind_propp);
wenzelm@20209
  1112
val add_assms_i = gen_assms (apsnd #1 o bind_propp_i);
wenzelm@20209
  1113
wenzelm@20209
  1114
end;
wenzelm@20209
  1115
wenzelm@20209
  1116
wenzelm@5819
  1117
wenzelm@8373
  1118
(** cases **)
wenzelm@8373
  1119
wenzelm@16147
  1120
local
wenzelm@16147
  1121
wenzelm@16668
  1122
fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
wenzelm@16147
  1123
wenzelm@18476
  1124
fun add_case _ ("", _) cases = cases
wenzelm@18476
  1125
  | add_case _ (name, NONE) cases = rem_case name cases
wenzelm@18476
  1126
  | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
wenzelm@16147
  1127
wenzelm@18678
  1128
fun prep_case name fxs c =
wenzelm@18609
  1129
  let
wenzelm@18609
  1130
    fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
wenzelm@18609
  1131
      | replace [] ys = ys
wenzelm@18678
  1132
      | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
wenzelm@18609
  1133
    val RuleCases.Case {fixes, assumes, binds, cases} = c;
wenzelm@18609
  1134
    val fixes' = replace fxs fixes;
wenzelm@18609
  1135
    val binds' = map drop_schematic binds;
wenzelm@18609
  1136
  in
wenzelm@18609
  1137
    if null (fold (Term.add_tvarsT o snd) fixes []) andalso
wenzelm@18609
  1138
      null (fold (fold Term.add_vars o snd) assumes []) then
wenzelm@18609
  1139
        RuleCases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
wenzelm@18678
  1140
    else error ("Illegal schematic variable(s) in case " ^ quote name)
wenzelm@18609
  1141
  end;
wenzelm@18609
  1142
wenzelm@18672
  1143
fun fix (x, T) ctxt =
wenzelm@18672
  1144
  let
wenzelm@18672
  1145
    val (bind, ctxt') = bind_fixes [x] ctxt;
wenzelm@18672
  1146
    val t = bind (Free (x, T));
wenzelm@20163
  1147
  in (t, ctxt' |> Variable.declare_constraints t) end;
wenzelm@18672
  1148
wenzelm@16147
  1149
in
wenzelm@16147
  1150
wenzelm@18672
  1151
fun add_cases is_proper = map_cases o fold (add_case is_proper);
wenzelm@18609
  1152
wenzelm@18609
  1153
fun case_result c ctxt =
wenzelm@18609
  1154
  let
wenzelm@18609
  1155
    val RuleCases.Case {fixes, ...} = c;
wenzelm@18672
  1156
    val (ts, ctxt') = ctxt |> fold_map fix fixes;
wenzelm@18672
  1157
    val RuleCases.Case {assumes, binds, cases, ...} = RuleCases.apply ts c;
wenzelm@18609
  1158
  in
wenzelm@18609
  1159
    ctxt'
wenzelm@18699
  1160
    |> add_binds_i (map drop_schematic binds)
wenzelm@18609
  1161
    |> add_cases true (map (apsnd SOME) cases)
wenzelm@18609
  1162
    |> pair (assumes, (binds, cases))
wenzelm@18609
  1163
  end;
wenzelm@18609
  1164
wenzelm@18609
  1165
val apply_case = apfst fst oo case_result;
wenzelm@18609
  1166
wenzelm@16540
  1167
fun get_case ctxt name xs =
wenzelm@17184
  1168
  (case AList.lookup (op =) (cases_of ctxt) name of
wenzelm@18678
  1169
    NONE => error ("Unknown case: " ^ quote name)
wenzelm@18678
  1170
  | SOME (c, _) => prep_case name xs c);
wenzelm@8373
  1171
wenzelm@16147
  1172
end;
wenzelm@8373
  1173
wenzelm@8373
  1174
wenzelm@8373
  1175
wenzelm@10810
  1176
(** print context information **)
wenzelm@10810
  1177
wenzelm@20310
  1178
val debug = ref false;
wenzelm@20310
  1179
wenzelm@10810
  1180
val verbose = ref false;
wenzelm@10810
  1181
fun verb f x = if ! verbose then f (x ()) else [];
wenzelm@10810
  1182
wenzelm@10810
  1183
fun setmp_verbose f x = Library.setmp verbose true f x;
wenzelm@10810
  1184
wenzelm@10810
  1185
wenzelm@12072
  1186
(* local syntax *)
wenzelm@12072
  1187
wenzelm@12093
  1188
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@12072
  1189
wenzelm@12072
  1190
wenzelm@21728
  1191
(* abbreviations *)
wenzelm@18971
  1192
wenzelm@21728
  1193
fun pretty_abbrevs show_globals ctxt =
wenzelm@18971
  1194
  let
wenzelm@25052
  1195
    val ((space, consts), (_, globals)) =
wenzelm@19033
  1196
      pairself (#constants o Consts.dest) (#consts (rep_context ctxt));
wenzelm@21803
  1197
    fun add_abbr (_, (_, NONE)) = I
wenzelm@25406
  1198
      | add_abbr (c, (T, SOME t)) =
wenzelm@21728
  1199
          if not show_globals andalso Symtab.defined globals c then I
wenzelm@21728
  1200
          else cons (c, Logic.mk_equals (Const (c, T), t));
wenzelm@21803
  1201
    val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold add_abbr consts []));
wenzelm@18971
  1202
  in
wenzelm@18971
  1203
    if null abbrevs andalso not (! verbose) then []
wenzelm@21728
  1204
    else [Pretty.big_list "abbreviations:" (map (pretty_term_abbrev ctxt o #2) abbrevs)]
wenzelm@18971
  1205
  end;
wenzelm@18971
  1206
wenzelm@21728
  1207
val print_abbrevs = Pretty.writeln o Pretty.chunks o pretty_abbrevs true;
wenzelm@21728
  1208
wenzelm@18971
  1209
wenzelm@10810
  1210
(* term bindings *)
wenzelm@10810
  1211
wenzelm@16540
  1212
fun pretty_binds ctxt =
wenzelm@10810
  1213
  let
wenzelm@19897
  1214
    val binds = Variable.binds_of ctxt;
wenzelm@21728
  1215
    fun prt_bind (xi, (T, t)) = pretty_term_abbrev ctxt (Logic.mk_equals (Var (xi, T), t));
wenzelm@10810
  1216
  in
wenzelm@15758
  1217
    if Vartab.is_empty binds andalso not (! verbose) then []
wenzelm@15758
  1218
    else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
wenzelm@10810
  1219
  end;
wenzelm@10810
  1220
wenzelm@10810
  1221
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@10810
  1222
wenzelm@10810
  1223
wenzelm@10810
  1224
(* local theorems *)
wenzelm@10810
  1225
wenzelm@16540
  1226
fun pretty_lthms ctxt =
wenzelm@20012
  1227
  let
wenzelm@26284
  1228
    val local_facts = facts_of ctxt;
wenzelm@26284
  1229
    val props = Facts.props local_facts;
wenzelm@26673
  1230
    val facts =
wenzelm@27173
  1231
      (if null props then [] else [("unnamed", props)]) @
wenzelm@27173
  1232
      Facts.extern_static [] local_facts;
wenzelm@20012
  1233
  in
wenzelm@20012
  1234
    if null facts andalso not (! verbose) then []
wenzelm@20012
  1235
    else [Pretty.big_list "facts:" (map (pretty_fact ctxt) facts)]
wenzelm@20012
  1236
  end;
wenzelm@10810
  1237
wenzelm@12057
  1238
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
wenzelm@10810
  1239
wenzelm@10810
  1240
wenzelm@10810
  1241
(* local contexts *)
wenzelm@10810
  1242
wenzelm@26722
  1243
local
wenzelm@26722
  1244
wenzelm@26722
  1245
fun pretty_case (name, (fixes, ((asms, (lets, cs)), ctxt))) =
wenzelm@10810
  1246
  let
wenzelm@24922
  1247
    val prt_term = Syntax.pretty_term ctxt;
wenzelm@12057
  1248
wenzelm@10810
  1249
    fun prt_let (xi, t) = Pretty.block
wenzelm@10818
  1250
      [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
wenzelm@10810
  1251
        Pretty.quote (prt_term t)];
wenzelm@10810
  1252
wenzelm@13425
  1253
    fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
wenzelm@13425
  1254
      ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
wenzelm@13425
  1255
wenzelm@10810
  1256
    fun prt_sect _ _ _ [] = []
wenzelm@10810
  1257
      | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s ::
wenzelm@19482
  1258
            flat (Library.separate sep (map (Library.single o prt) xs))))];
wenzelm@26722
  1259
  in
wenzelm@26722
  1260
    Pretty.block (Pretty.fbreaks
wenzelm@10810
  1261
      (Pretty.str (name ^ ":") ::
wenzelm@11915
  1262
        prt_sect "fix" [] (Pretty.str o fst) fixes @
wenzelm@10810
  1263
        prt_sect "let" [Pretty.str "and"] prt_let
wenzelm@19482
  1264
          (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
wenzelm@13425
  1265
        (if forall (null o #2) asms then []
wenzelm@18609
  1266
          else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
wenzelm@26722
  1267
        prt_sect "subcases:" [] (Pretty.str o fst) cs))
wenzelm@26722
  1268
  end;
wenzelm@16540
  1269
wenzelm@26722
  1270
in
wenzelm@26722
  1271
wenzelm@26722
  1272
fun pretty_cases ctxt =
wenzelm@26722
  1273
  let
wenzelm@18476
  1274
    fun add_case (_, (_, false)) = I
wenzelm@18609
  1275
      | add_case (name, (c as RuleCases.Case {fixes, ...}, true)) =
wenzelm@26722
  1276
          cons (name, (fixes, case_result c ctxt));
wenzelm@18476
  1277
    val cases = fold add_case (cases_of ctxt) [];
wenzelm@10810
  1278
  in
wenzelm@10810
  1279
    if null cases andalso not (! verbose) then []
wenzelm@26722
  1280
    else [Pretty.big_list "cases:" (map pretty_case cases)]
wenzelm@10810
  1281
  end;
wenzelm@10810
  1282
wenzelm@10810
  1283
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@10810
  1284
wenzelm@26722
  1285
end;
wenzelm@26722
  1286
wenzelm@10810
  1287
wenzelm@12057
  1288
(* core context *)
wenzelm@10810
  1289
wenzelm@20367
  1290
val prems_limit = ref ~1;
wenzelm@10810
  1291
wenzelm@18672
  1292
fun pretty_ctxt ctxt =
wenzelm@20310
  1293
  if ! prems_limit < 0 andalso not (! debug) then []
wenzelm@20310
  1294
  else
wenzelm@20310
  1295
    let
wenzelm@24922
  1296
      val prt_term = Syntax.pretty_term ctxt;
wenzelm@12057
  1297
wenzelm@20310
  1298
      (*structures*)
wenzelm@20310
  1299
      val structs = LocalSyntax.structs_of (syntax_of ctxt);
wenzelm@20310
  1300
      val prt_structs = if null structs then []
wenzelm@20310
  1301
        else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
wenzelm@20310
  1302
          Pretty.commas (map Pretty.str structs))];
wenzelm@12093
  1303
wenzelm@20310
  1304
      (*fixes*)
wenzelm@20310
  1305
      fun prt_fix (x, x') =
wenzelm@20310
  1306
        if x = x' then Pretty.str x
wenzelm@20310
  1307
        else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@20310
  1308
      val fixes =
wenzelm@20310
  1309
        rev (filter_out ((can Name.dest_internal orf member (op =) structs) o #1)
wenzelm@20310
  1310
          (Variable.fixes_of ctxt));
wenzelm@20310
  1311
      val prt_fixes = if null fixes then []
wenzelm@20310
  1312
        else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@20310
  1313
          Pretty.commas (map prt_fix fixes))];
wenzelm@12057
  1314
wenzelm@20310
  1315
      (*prems*)
wenzelm@20310
  1316
      val prems = Assumption.prems_of ctxt;
wenzelm@20310
  1317
      val len = length prems;
wenzelm@20367
  1318
      val suppressed = len - ! prems_limit;
wenzelm@20310
  1319
      val prt_prems = if null prems then []
wenzelm@20367
  1320
        else [Pretty.big_list "prems:" ((if suppressed <= 0 then [] else [Pretty.str "..."]) @
wenzelm@20367
  1321
          map (pretty_thm ctxt) (Library.drop (suppressed, prems)))];
wenzelm@20310
  1322
    in prt_structs @ prt_fixes @ prt_prems end;
wenzelm@10810
  1323
wenzelm@10810
  1324
wenzelm@10810
  1325
(* main context *)
wenzelm@10810
  1326
wenzelm@16540
  1327
fun pretty_context ctxt =
wenzelm@10810
  1328
  let
wenzelm@24922
  1329
    val prt_term = Syntax.pretty_term ctxt;
wenzelm@24922
  1330
    val prt_typ = Syntax.pretty_typ ctxt;
wenzelm@24922
  1331
    val prt_sort = Syntax.pretty_sort ctxt;
wenzelm@10810
  1332
wenzelm@10810
  1333
    (*theory*)
wenzelm@12057
  1334
    val pretty_thy = Pretty.block
wenzelm@17384
  1335
      [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
wenzelm@10810
  1336
wenzelm@10810
  1337
    (*defaults*)
wenzelm@10810
  1338
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@10810
  1339
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@10810
  1340
wenzelm@10810
  1341
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@10810
  1342
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@10810
  1343
wenzelm@10810
  1344
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@10810
  1345
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@10810
  1346
wenzelm@10810
  1347
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@10810
  1348
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@16540
  1349
wenzelm@20163
  1350
    val (types, sorts) = Variable.constraints_of ctxt;
wenzelm@10810
  1351
  in
wenzelm@18609
  1352
    verb single (K pretty_thy) @
wenzelm@18672
  1353
    pretty_ctxt ctxt @
wenzelm@21728
  1354
    verb (pretty_abbrevs false) (K ctxt) @
wenzelm@10810
  1355
    verb pretty_binds (K ctxt) @
wenzelm@12057
  1356
    verb pretty_lthms (K ctxt) @
wenzelm@10810
  1357
    verb pretty_cases (K ctxt) @
wenzelm@18609
  1358
    verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@20163
  1359
    verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
wenzelm@10810
  1360
  end;
wenzelm@10810
  1361
wenzelm@27314
  1362
wenzelm@27314
  1363
(* query meta data *)
wenzelm@27314
  1364
wenzelm@27314
  1365
val query_type = Type.the_tags o tsig_of;
wenzelm@27314
  1366
wenzelm@27314
  1367
fun query_const ctxt name =
wenzelm@27314
  1368
  Consts.the_tags (consts_of ctxt) name handle TYPE (msg, _, _) => error msg;
wenzelm@27314
  1369
wenzelm@27314
  1370
fun query_class ctxt name = query_const ctxt (Logic.const_of_class name);
wenzelm@27314
  1371
wenzelm@5819
  1372
end;