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