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