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