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