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