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