src/Pure/Isar/proof_context.ML
author wenzelm
Sat Sep 01 15:47:04 2007 +0200 (2007-09-01 ago)
changeset 24511 69d270cc7e4f
parent 24505 9e6d91f8bb73
child 24518 4dd086997bab
permissions -rw-r--r--
removed obsolete read/cert variations (cf. Syntax.read/check);
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@24495
    15
  val join_mode: mode -> mode -> mode
wenzelm@24501
    16
  val mode_default: mode
wenzelm@24501
    17
  val mode_stmt: mode
wenzelm@24501
    18
  val mode_pattern: mode
wenzelm@24501
    19
  val mode_schematic: mode
wenzelm@24501
    20
  val mode_abbrev: mode
wenzelm@24388
    21
  val set_mode: mode -> Proof.context -> Proof.context
wenzelm@24388
    22
  val get_mode: Proof.context -> mode
wenzelm@24388
    23
  val restore_mode: Proof.context -> Proof.context -> Proof.context
wenzelm@21667
    24
  val set_stmt: bool -> Proof.context -> Proof.context
wenzelm@21667
    25
  val naming_of: Proof.context -> NameSpace.naming
wenzelm@20310
    26
  val full_name: Proof.context -> bstring -> string
wenzelm@20310
    27
  val consts_of: Proof.context -> Consts.T
wenzelm@21183
    28
  val const_syntax_name: Proof.context -> string -> string
wenzelm@20784
    29
  val set_syntax_mode: Syntax.mode -> Proof.context -> Proof.context
wenzelm@20310
    30
  val restore_syntax_mode: Proof.context -> Proof.context -> Proof.context
wenzelm@22358
    31
  val theorems_of: Proof.context -> thm list NameSpace.table
wenzelm@20310
    32
  val fact_index_of: Proof.context -> FactIndex.T
wenzelm@20310
    33
  val transfer: theory -> Proof.context -> Proof.context
wenzelm@20367
    34
  val theory: (theory -> theory) -> Proof.context -> Proof.context
wenzelm@20367
    35
  val theory_result: (theory -> 'a * theory) -> Proof.context -> 'a * Proof.context
wenzelm@20310
    36
  val pretty_term: Proof.context -> term -> Pretty.T
wenzelm@21728
    37
  val pretty_term_abbrev: Proof.context -> term -> Pretty.T
wenzelm@20310
    38
  val pretty_typ: Proof.context -> typ -> Pretty.T
wenzelm@20310
    39
  val pretty_sort: Proof.context -> sort -> Pretty.T
wenzelm@20310
    40
  val pretty_classrel: Proof.context -> class list -> Pretty.T
wenzelm@20310
    41
  val pretty_arity: Proof.context -> arity -> Pretty.T
wenzelm@20310
    42
  val pp: Proof.context -> Pretty.pp
wenzelm@22874
    43
  val pretty_thm_legacy: thm -> Pretty.T
wenzelm@20310
    44
  val pretty_thm: Proof.context -> thm -> Pretty.T
wenzelm@20310
    45
  val pretty_thms: Proof.context -> thm list -> Pretty.T
wenzelm@20310
    46
  val pretty_fact: Proof.context -> string * thm list -> Pretty.T
wenzelm@20310
    47
  val pretty_proof: Proof.context -> Proofterm.proof -> Pretty.T
wenzelm@20310
    48
  val pretty_proof_of: Proof.context -> bool -> thm -> Pretty.T
wenzelm@20310
    49
  val string_of_typ: Proof.context -> typ -> string
wenzelm@20310
    50
  val string_of_term: Proof.context -> term -> string
wenzelm@20310
    51
  val string_of_thm: Proof.context -> thm -> string
wenzelm@20310
    52
  val read_typ: Proof.context -> string -> typ
wenzelm@20310
    53
  val read_typ_syntax: Proof.context -> string -> typ
wenzelm@20310
    54
  val read_typ_abbrev: Proof.context -> string -> typ
wenzelm@20310
    55
  val cert_typ: Proof.context -> typ -> typ
wenzelm@20310
    56
  val cert_typ_syntax: Proof.context -> typ -> typ
wenzelm@20310
    57
  val cert_typ_abbrev: Proof.context -> typ -> typ
wenzelm@20310
    58
  val get_skolem: Proof.context -> string -> string
wenzelm@20310
    59
  val revert_skolem: Proof.context -> string -> string
wenzelm@20310
    60
  val revert_skolems: Proof.context -> term -> term
wenzelm@22763
    61
  val decode_term: Proof.context -> term -> term
wenzelm@20310
    62
  val read_termTs_schematic: Proof.context -> (string -> bool) -> (indexname -> typ option)
wenzelm@14720
    63
    -> (indexname -> sort option) -> string list -> (string * typ) list
wenzelm@14720
    64
    -> term list * (indexname * typ) list
wenzelm@22712
    65
  val read_prop_legacy: Proof.context -> string -> term
wenzelm@22773
    66
  val read_termTs: Proof.context -> (string * typ) list -> term list
wenzelm@20310
    67
  val read_term_pats: typ -> Proof.context -> string list -> term list
wenzelm@20310
    68
  val read_prop_pats: Proof.context -> string list -> term list
wenzelm@21728
    69
  val read_term_abbrev: Proof.context -> string -> term
wenzelm@20310
    70
  val cert_term: Proof.context -> term -> term
wenzelm@22773
    71
  val cert_terms: Proof.context -> term list -> term list
wenzelm@20310
    72
  val cert_prop: Proof.context -> term -> term
wenzelm@20310
    73
  val cert_term_pats: typ -> Proof.context -> term list -> term list
wenzelm@20310
    74
  val cert_prop_pats: Proof.context -> term list -> term list
wenzelm@20310
    75
  val infer_type: Proof.context -> string -> typ
wenzelm@20310
    76
  val inferred_param: string -> Proof.context -> (string * typ) * Proof.context
wenzelm@20310
    77
  val inferred_fixes: Proof.context -> (string * typ) list * Proof.context
wenzelm@20310
    78
  val read_tyname: Proof.context -> string -> typ
wenzelm@20310
    79
  val read_const: Proof.context -> string -> term
wenzelm@20310
    80
  val goal_export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@20310
    81
  val export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@21531
    82
  val export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@20310
    83
  val add_binds: (indexname * string option) list -> Proof.context -> Proof.context
wenzelm@20310
    84
  val add_binds_i: (indexname * term option) list -> Proof.context -> Proof.context
wenzelm@20310
    85
  val auto_bind_goal: term list -> Proof.context -> Proof.context
wenzelm@20310
    86
  val auto_bind_facts: term list -> Proof.context -> Proof.context
wenzelm@20310
    87
  val match_bind: bool -> (string list * string) list -> Proof.context -> term list * Proof.context
wenzelm@20310
    88
  val match_bind_i: bool -> (term list * term) list -> Proof.context -> term list * Proof.context
wenzelm@20310
    89
  val read_propp: Proof.context * (string * string list) list list
wenzelm@20310
    90
    -> Proof.context * (term * term list) list list
wenzelm@20310
    91
  val cert_propp: Proof.context * (term * term list) list list
wenzelm@20310
    92
    -> Proof.context * (term * term list) list list
wenzelm@20310
    93
  val read_propp_schematic: Proof.context * (string * string list) list list
wenzelm@20310
    94
    -> Proof.context * (term * term list) list list
wenzelm@20310
    95
  val cert_propp_schematic: Proof.context * (term * term list) list list
wenzelm@20310
    96
    -> Proof.context * (term * term list) list list
wenzelm@20310
    97
  val bind_propp: Proof.context * (string * string list) list list
wenzelm@20310
    98
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@20310
    99
  val bind_propp_i: Proof.context * (term * term list) list list
wenzelm@20310
   100
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@20310
   101
  val bind_propp_schematic: Proof.context * (string * string list) list list
wenzelm@20310
   102
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@20310
   103
  val bind_propp_schematic_i: Proof.context * (term * term list) list list
wenzelm@20310
   104
    -> Proof.context * (term list list * (Proof.context -> Proof.context))
wenzelm@18042
   105
  val fact_tac: thm list -> int -> tactic
wenzelm@20310
   106
  val some_fact_tac: Proof.context -> int -> tactic
wenzelm@20310
   107
  val get_thm: Proof.context -> thmref -> thm
wenzelm@20310
   108
  val get_thm_closure: Proof.context -> thmref -> thm
wenzelm@20310
   109
  val get_thms: Proof.context -> thmref -> thm list
wenzelm@20310
   110
  val get_thms_closure: Proof.context -> thmref -> thm list
wenzelm@20310
   111
  val valid_thms: Proof.context -> string * thm list -> bool
wenzelm@20310
   112
  val lthms_containing: Proof.context -> FactIndex.spec -> (string * thm list) list
haftmann@22352
   113
  val add_path: string -> Proof.context -> Proof.context
wenzelm@20310
   114
  val no_base_names: Proof.context -> Proof.context
wenzelm@20310
   115
  val qualified_names: Proof.context -> Proof.context
wenzelm@20310
   116
  val sticky_prefix: string -> Proof.context -> Proof.context
wenzelm@20310
   117
  val restore_naming: Proof.context -> Proof.context -> Proof.context
wenzelm@21728
   118
  val reset_naming: Proof.context -> Proof.context
wenzelm@20310
   119
  val hide_thms: bool -> string list -> Proof.context -> Proof.context
wenzelm@21443
   120
  val put_thms: string * thm list option -> Proof.context -> Proof.context
wenzelm@21443
   121
  val note_thmss: string ->
wenzelm@18728
   122
    ((bstring * attribute list) * (thmref * attribute list) list) list ->
wenzelm@20310
   123
      Proof.context -> (bstring * thm list) list * Proof.context
wenzelm@21443
   124
  val note_thmss_i: string ->
wenzelm@18728
   125
    ((bstring * attribute list) * (thm list * attribute list) list) list ->
wenzelm@20310
   126
      Proof.context -> (bstring * thm list) list * Proof.context
wenzelm@20310
   127
  val read_vars: (string * string option * mixfix) list -> Proof.context ->
wenzelm@20310
   128
    (string * typ option * mixfix) list * Proof.context
wenzelm@20310
   129
  val cert_vars: (string * typ option * mixfix) list -> Proof.context ->
wenzelm@20310
   130
    (string * typ option * mixfix) list * Proof.context
wenzelm@20310
   131
  val read_vars_legacy: (string * string option * mixfix) list -> Proof.context ->
wenzelm@20310
   132
    (string * typ option * mixfix) list * Proof.context
wenzelm@20310
   133
  val cert_vars_legacy: (string * typ option * mixfix) list -> Proof.context ->
wenzelm@20310
   134
    (string * typ option * mixfix) list * Proof.context
wenzelm@20310
   135
  val add_fixes: (string * string option * mixfix) list ->
wenzelm@20310
   136
    Proof.context -> string list * Proof.context
wenzelm@20310
   137
  val add_fixes_i: (string * typ option * mixfix) list ->
wenzelm@20310
   138
    Proof.context -> string list * Proof.context
wenzelm@20310
   139
  val add_fixes_legacy: (string * typ option * mixfix) list ->
wenzelm@20310
   140
    Proof.context -> string list * Proof.context
wenzelm@20310
   141
  val auto_fixes: Proof.context * (term list list * 'a) -> Proof.context * (term list list * 'a)
wenzelm@20310
   142
  val bind_fixes: string list -> Proof.context -> (term -> term) * Proof.context
wenzelm@20234
   143
  val add_assms: Assumption.export ->
wenzelm@19585
   144
    ((string * attribute list) * (string * string list) list) list ->
wenzelm@20310
   145
    Proof.context -> (bstring * thm list) list * Proof.context
wenzelm@20234
   146
  val add_assms_i: Assumption.export ->
wenzelm@19585
   147
    ((string * attribute list) * (term * term list) list) list ->
wenzelm@20310
   148
    Proof.context -> (bstring * thm list) list * Proof.context
wenzelm@20310
   149
  val add_cases: bool -> (string * RuleCases.T option) list -> Proof.context -> Proof.context
wenzelm@20310
   150
  val apply_case: RuleCases.T -> Proof.context -> (string * term list) list * Proof.context
wenzelm@20310
   151
  val get_case: Proof.context -> string -> string option list -> RuleCases.T
wenzelm@21208
   152
  val add_notation: Syntax.mode -> (term * mixfix) list ->
wenzelm@21208
   153
    Proof.context -> Proof.context
wenzelm@21744
   154
  val target_notation: Syntax.mode -> (term * mixfix) list -> morphism ->
wenzelm@21744
   155
    Context.generic -> Context.generic
wenzelm@21728
   156
  val set_expand_abbrevs: bool -> Proof.context -> Proof.context
wenzelm@21803
   157
  val add_abbrev: string -> bstring * term -> Proof.context -> (term * term) * Proof.context
wenzelm@10810
   158
  val verbose: bool ref
wenzelm@10810
   159
  val setmp_verbose: ('a -> 'b) -> 'a -> 'b
wenzelm@20310
   160
  val print_syntax: Proof.context -> unit
wenzelm@21728
   161
  val print_abbrevs: Proof.context -> unit
wenzelm@20310
   162
  val print_binds: Proof.context -> unit
wenzelm@20310
   163
  val print_lthms: Proof.context -> unit
wenzelm@20310
   164
  val print_cases: Proof.context -> unit
wenzelm@20310
   165
  val debug: bool ref
wenzelm@10810
   166
  val prems_limit: int ref
wenzelm@20310
   167
  val pretty_ctxt: Proof.context -> Pretty.T list
wenzelm@20310
   168
  val pretty_context: Proof.context -> Pretty.T list
wenzelm@5819
   169
end;
wenzelm@5819
   170
wenzelm@16540
   171
structure ProofContext: PROOF_CONTEXT =
wenzelm@5819
   172
struct
wenzelm@5819
   173
wenzelm@16540
   174
val theory_of = Context.theory_of_proof;
wenzelm@19001
   175
val tsig_of = Sign.tsig_of o theory_of;
wenzelm@19001
   176
wenzelm@16540
   177
val init = Context.init_proof;
wenzelm@12057
   178
wenzelm@7270
   179
wenzelm@24388
   180
(** inner syntax mode **)
wenzelm@24388
   181
wenzelm@24388
   182
datatype mode =
wenzelm@24388
   183
  Mode of
wenzelm@24486
   184
   {stmt: bool,                (*inner statement mode*)
wenzelm@24388
   185
    pattern: bool,             (*pattern binding schematic variables*)
wenzelm@24388
   186
    schematic: bool,           (*term referencing loose schematic variables*)
wenzelm@24388
   187
    abbrev: bool};             (*abbrev mode -- no normalization*)
wenzelm@24388
   188
wenzelm@24486
   189
fun make_mode (stmt, pattern, schematic, abbrev) =
wenzelm@24486
   190
  Mode {stmt = stmt, pattern = pattern, schematic = schematic, abbrev = abbrev};
wenzelm@24388
   191
wenzelm@24495
   192
fun join_mode
wenzelm@24495
   193
    (Mode {stmt = stmt1, pattern = pattern1, schematic = schematic1, abbrev = abbrev1})
wenzelm@24495
   194
    (Mode {stmt = stmt2, pattern = pattern2, schematic = schematic2, abbrev = abbrev2}) =
wenzelm@24495
   195
  make_mode (stmt1 orelse stmt2, pattern1 orelse pattern2,
wenzelm@24495
   196
    schematic1 orelse schematic2, abbrev1 orelse abbrev2);
wenzelm@24495
   197
wenzelm@24486
   198
val mode_default   = make_mode (false, false, false, false);
wenzelm@24486
   199
val mode_stmt      = make_mode (true, false, false, false);
wenzelm@24486
   200
val mode_pattern   = make_mode (false, true, false, false);
wenzelm@24486
   201
val mode_schematic = make_mode (false, false, true, false);
wenzelm@24486
   202
val mode_abbrev    = make_mode (false, false, false, true);
wenzelm@24388
   203
wenzelm@24388
   204
wenzelm@5819
   205
wenzelm@16540
   206
(** Isar proof context information **)
wenzelm@5819
   207
wenzelm@16540
   208
datatype ctxt =
wenzelm@16540
   209
  Ctxt of
wenzelm@24388
   210
   {mode: mode,                                   (*inner syntax mode*)
wenzelm@21443
   211
    naming: NameSpace.naming,                     (*local naming conventions*)
wenzelm@19001
   212
    syntax: LocalSyntax.T,                        (*local syntax*)
wenzelm@19033
   213
    consts: Consts.T * Consts.T,                  (*global/local consts*)
wenzelm@19001
   214
    thms: thm list NameSpace.table * FactIndex.T, (*local thms*)
wenzelm@19897
   215
    cases: (string * (RuleCases.T * bool)) list}; (*local contexts*)
wenzelm@5819
   216
wenzelm@24388
   217
fun make_ctxt (mode, naming, syntax, consts, thms, cases) =
wenzelm@24388
   218
  Ctxt {mode = mode, naming = naming, syntax = syntax,
wenzelm@21443
   219
    consts = consts, thms = thms, cases = cases};
wenzelm@5819
   220
wenzelm@19079
   221
val local_naming = NameSpace.default_naming |> NameSpace.add_path "local";
wenzelm@19079
   222
wenzelm@16540
   223
structure ContextData = ProofDataFun
wenzelm@18672
   224
(
wenzelm@16540
   225
  type T = ctxt;
wenzelm@16540
   226
  fun init thy =
wenzelm@24388
   227
    make_ctxt (mode_default, local_naming, LocalSyntax.init thy,
wenzelm@20234
   228
      (Sign.consts_of thy, Sign.consts_of thy),
wenzelm@19897
   229
      (NameSpace.empty_table, FactIndex.empty), []);
wenzelm@18672
   230
);
wenzelm@5819
   231
wenzelm@16540
   232
fun rep_context ctxt = ContextData.get ctxt |> (fn Ctxt args => args);
wenzelm@5819
   233
wenzelm@18672
   234
fun map_context f =
wenzelm@24388
   235
  ContextData.map (fn Ctxt {mode, naming, syntax, consts, thms, cases} =>
wenzelm@24388
   236
    make_ctxt (f (mode, naming, syntax, consts, thms, cases)));
wenzelm@24388
   237
wenzelm@24388
   238
fun set_mode mode = map_context (fn (_, naming, syntax, consts, thms, cases) =>
wenzelm@24388
   239
  (mode, naming, syntax, consts, thms, cases));
wenzelm@21443
   240
wenzelm@24388
   241
fun map_mode f =
wenzelm@24486
   242
  map_context (fn (Mode {stmt, pattern, schematic, abbrev}, naming, syntax, consts, thms, cases) =>
wenzelm@24486
   243
    (make_mode (f (stmt, pattern, schematic, abbrev)), naming, syntax, consts, thms, cases));
wenzelm@18672
   244
wenzelm@19001
   245
fun map_naming f =
wenzelm@24388
   246
  map_context (fn (mode, naming, syntax, consts, thms, cases) =>
wenzelm@24388
   247
    (mode, f naming, syntax, consts, thms, cases));
wenzelm@18672
   248
wenzelm@19001
   249
fun map_syntax f =
wenzelm@24388
   250
  map_context (fn (mode, naming, syntax, consts, thms, cases) =>
wenzelm@24388
   251
    (mode, naming, f syntax, consts, thms, cases));
wenzelm@18672
   252
wenzelm@19001
   253
fun map_consts f =
wenzelm@24388
   254
  map_context (fn (mode, naming, syntax, consts, thms, cases) =>
wenzelm@24388
   255
    (mode, naming, syntax, f consts, thms, cases));
wenzelm@18672
   256
wenzelm@19001
   257
fun map_thms f =
wenzelm@24388
   258
  map_context (fn (mode, naming, syntax, consts, thms, cases) =>
wenzelm@24388
   259
    (mode, naming, syntax, consts, f thms, cases));
wenzelm@18672
   260
wenzelm@19001
   261
fun map_cases f =
wenzelm@24388
   262
  map_context (fn (mode, naming, syntax, consts, thms, cases) =>
wenzelm@24388
   263
    (mode, naming, syntax, consts, thms, f cases));
wenzelm@24388
   264
wenzelm@24388
   265
val get_mode = #mode o rep_context;
wenzelm@24388
   266
fun restore_mode ctxt = set_mode (get_mode ctxt);
wenzelm@21443
   267
wenzelm@24486
   268
fun set_stmt stmt =
wenzelm@24486
   269
  map_mode (fn (_, pattern, schematic, abbrev) => (stmt, pattern, schematic, abbrev));
wenzelm@19001
   270
wenzelm@19001
   271
val naming_of = #naming o rep_context;
wenzelm@19387
   272
val full_name = NameSpace.full o naming_of;
wenzelm@5819
   273
wenzelm@16540
   274
val syntax_of = #syntax o rep_context;
wenzelm@19001
   275
val syn_of = LocalSyntax.syn_of o syntax_of;
wenzelm@19543
   276
val set_syntax_mode = map_syntax o LocalSyntax.set_mode;
wenzelm@19543
   277
val restore_syntax_mode = map_syntax o LocalSyntax.restore_mode o syntax_of;
wenzelm@19001
   278
wenzelm@19033
   279
val consts_of = #2 o #consts o rep_context;
wenzelm@21183
   280
val const_syntax_name = Consts.syntax_name o consts_of;
wenzelm@5819
   281
wenzelm@16540
   282
val thms_of = #thms o rep_context;
wenzelm@22358
   283
val theorems_of = #1 o thms_of;
wenzelm@19001
   284
val fact_index_of = #2 o thms_of;
wenzelm@16540
   285
wenzelm@16540
   286
val cases_of = #cases o rep_context;
wenzelm@5819
   287
wenzelm@5819
   288
wenzelm@20367
   289
(* theory transfer *)
wenzelm@12093
   290
wenzelm@19001
   291
fun transfer_syntax thy =
wenzelm@19001
   292
  map_syntax (LocalSyntax.rebuild thy) #>
wenzelm@19033
   293
  map_consts (fn consts as (global_consts, local_consts) =>
wenzelm@19033
   294
    let val thy_consts = Sign.consts_of thy in
wenzelm@19033
   295
      if Consts.eq_consts (thy_consts, global_consts) then consts
wenzelm@19033
   296
      else (thy_consts, Consts.merge (thy_consts, local_consts))
wenzelm@19033
   297
    end);
wenzelm@17072
   298
wenzelm@19001
   299
fun transfer thy = Context.transfer_proof thy #> transfer_syntax thy;
wenzelm@17072
   300
wenzelm@20367
   301
fun theory f ctxt = transfer (f (theory_of ctxt)) ctxt;
wenzelm@20367
   302
wenzelm@20367
   303
fun theory_result f ctxt =
wenzelm@20367
   304
  let val (res, thy') = f (theory_of ctxt)
wenzelm@20367
   305
  in (res, ctxt |> transfer thy') end;
wenzelm@19019
   306
wenzelm@12093
   307
wenzelm@12093
   308
wenzelm@14828
   309
(** pretty printing **)
wenzelm@14828
   310
wenzelm@19310
   311
local
wenzelm@19310
   312
wenzelm@21744
   313
fun rewrite_term warn thy rews t =
wenzelm@19371
   314
  if can Term.type_of t then Pattern.rewrite_term thy rews [] t
wenzelm@21744
   315
  else (if warn then warning "Printing malformed term -- cannot expand abbreviations" else (); t);
wenzelm@19371
   316
wenzelm@19001
   317
fun pretty_term' abbrevs ctxt t =
wenzelm@18971
   318
  let
wenzelm@19001
   319
    val thy = theory_of ctxt;
wenzelm@19001
   320
    val syntax = syntax_of ctxt;
wenzelm@18971
   321
    val consts = consts_of ctxt;
wenzelm@22587
   322
    val do_abbrevs = abbrevs andalso not (print_mode_active "no_abbrevs");
wenzelm@19001
   323
    val t' = t
wenzelm@21744
   324
      |> do_abbrevs ? rewrite_term true thy (Consts.abbrevs_of consts (! print_mode @ [""]))
wenzelm@21824
   325
      |> do_abbrevs ? rewrite_term false thy (Consts.abbrevs_of consts [Syntax.internalM])
wenzelm@19371
   326
      |> Sign.extern_term (Consts.extern_early consts) thy
wenzelm@19371
   327
      |> LocalSyntax.extern_term syntax;
wenzelm@21772
   328
  in Sign.pretty_term' ctxt (LocalSyntax.syn_of syntax) (Consts.extern consts) t' end;
wenzelm@18971
   329
wenzelm@19310
   330
in
wenzelm@19310
   331
wenzelm@19001
   332
val pretty_term = pretty_term' true;
wenzelm@21728
   333
val pretty_term_abbrev = pretty_term' false;
wenzelm@19310
   334
wenzelm@19310
   335
end;
wenzelm@19310
   336
wenzelm@16458
   337
fun pretty_typ ctxt T = Sign.pretty_typ (theory_of ctxt) T;
wenzelm@16458
   338
fun pretty_sort ctxt S = Sign.pretty_sort (theory_of ctxt) S;
wenzelm@16458
   339
fun pretty_classrel ctxt cs = Sign.pretty_classrel (theory_of ctxt) cs;
wenzelm@16458
   340
fun pretty_arity ctxt ar = Sign.pretty_arity (theory_of ctxt) ar;
wenzelm@14828
   341
wenzelm@14974
   342
fun pp ctxt = Pretty.pp (pretty_term ctxt, pretty_typ ctxt, pretty_sort ctxt,
wenzelm@14974
   343
  pretty_classrel ctxt, pretty_arity ctxt);
wenzelm@14828
   344
wenzelm@22874
   345
fun pretty_thm_legacy th =
wenzelm@22874
   346
  let val thy = Thm.theory_of_thm th
wenzelm@22874
   347
  in Display.pretty_thm_aux (pp (init thy)) true false [] th end;
wenzelm@20234
   348
wenzelm@17451
   349
fun pretty_thm ctxt th =
wenzelm@22874
   350
  let val asms = map Thm.term_of (Assumption.assms_of ctxt)
wenzelm@22874
   351
  in Display.pretty_thm_aux (pp ctxt) false true asms th end;
wenzelm@14828
   352
wenzelm@14828
   353
fun pretty_thms ctxt [th] = pretty_thm ctxt th
wenzelm@14828
   354
  | pretty_thms ctxt ths = Pretty.blk (0, Pretty.fbreaks (map (pretty_thm ctxt) ths));
wenzelm@14828
   355
wenzelm@14828
   356
fun pretty_fact ctxt ("", ths) = pretty_thms ctxt ths
wenzelm@14828
   357
  | pretty_fact ctxt (a, [th]) =
wenzelm@14828
   358
      Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1, pretty_thm ctxt th]
wenzelm@14828
   359
  | pretty_fact ctxt (a, ths) =
wenzelm@14828
   360
      Pretty.block (Pretty.fbreaks (Pretty.str (a ^ ":") :: map (pretty_thm ctxt) ths));
wenzelm@14828
   361
wenzelm@17072
   362
fun pretty_proof ctxt prf =
wenzelm@21728
   363
  pretty_term_abbrev (ctxt |> transfer_syntax (ProofSyntax.proof_syntax prf (theory_of ctxt)))
wenzelm@17072
   364
    (ProofSyntax.term_of_proof prf);
wenzelm@17072
   365
wenzelm@17072
   366
fun pretty_proof_of ctxt full th =
wenzelm@17072
   367
  pretty_proof ctxt (ProofSyntax.proof_of full th);
wenzelm@17072
   368
wenzelm@17860
   369
val string_of_typ = Pretty.string_of oo pretty_typ;
wenzelm@17860
   370
val string_of_term = Pretty.string_of oo pretty_term;
wenzelm@17860
   371
val string_of_thm = Pretty.string_of oo pretty_thm;
wenzelm@17860
   372
wenzelm@14828
   373
wenzelm@14828
   374
wenzelm@5819
   375
(** prepare types **)
wenzelm@5819
   376
wenzelm@24277
   377
(* read_typ *)
wenzelm@24277
   378
wenzelm@24277
   379
fun read_typ_mode mode ctxt s =
wenzelm@24486
   380
  Syntax.read_typ (Type.set_mode mode ctxt) s;
wenzelm@24277
   381
wenzelm@24277
   382
val read_typ        = read_typ_mode Type.mode_default;
wenzelm@24277
   383
val read_typ_syntax = read_typ_mode Type.mode_syntax;
wenzelm@24277
   384
val read_typ_abbrev = read_typ_mode Type.mode_abbrev;
wenzelm@24277
   385
wenzelm@24277
   386
wenzelm@24277
   387
(* cert_typ *)
wenzelm@24277
   388
wenzelm@24277
   389
fun cert_typ_mode mode ctxt T =
wenzelm@24277
   390
  Sign.certify_typ_mode mode (theory_of ctxt) T
wenzelm@24277
   391
    handle TYPE (msg, _, _) => error msg;
wenzelm@24277
   392
wenzelm@24277
   393
val cert_typ        = cert_typ_mode Type.mode_default;
wenzelm@24277
   394
val cert_typ_syntax = cert_typ_mode Type.mode_syntax;
wenzelm@24277
   395
val cert_typ_abbrev = cert_typ_mode Type.mode_abbrev;
wenzelm@24277
   396
wenzelm@24277
   397
wenzelm@24388
   398
wenzelm@24388
   399
(** prepare variables **)
wenzelm@24388
   400
wenzelm@7679
   401
(* internalize Skolem constants *)
wenzelm@7679
   402
wenzelm@19897
   403
val lookup_skolem = AList.lookup (op =) o Variable.fixes_of;
wenzelm@18187
   404
fun get_skolem ctxt x = the_default x (lookup_skolem ctxt x);
wenzelm@7679
   405
wenzelm@18678
   406
fun no_skolem internal x =
wenzelm@20086
   407
  if can Name.dest_skolem x then
wenzelm@18678
   408
    error ("Illegal reference to internal Skolem constant: " ^ quote x)
wenzelm@20086
   409
  else if not internal andalso can Name.dest_internal x then
wenzelm@18678
   410
    error ("Illegal reference to internal variable: " ^ quote x)
wenzelm@7679
   411
  else x;
wenzelm@7679
   412
wenzelm@7679
   413
wenzelm@20244
   414
(* revert Skolem constants -- approximation only! *)
wenzelm@18255
   415
wenzelm@20244
   416
fun revert_skolem ctxt =
wenzelm@9133
   417
  let
wenzelm@20244
   418
    val rev_fixes = map Library.swap (Variable.fixes_of ctxt);
wenzelm@20244
   419
    val revert = AList.lookup (op =) rev_fixes;
wenzelm@20244
   420
  in
wenzelm@20244
   421
    fn x =>
wenzelm@20244
   422
      (case revert x of
wenzelm@20244
   423
        SOME x' => x'
wenzelm@20244
   424
      | NONE => perhaps (try Name.dest_skolem) x)
wenzelm@20244
   425
   end;
wenzelm@20244
   426
wenzelm@20244
   427
fun revert_skolems ctxt =
wenzelm@20244
   428
  let
wenzelm@20244
   429
    val revert = revert_skolem ctxt;
wenzelm@20244
   430
    fun reverts (Free (x, T)) = Free (revert x, T)
wenzelm@20244
   431
      | reverts (t $ u) = reverts t $ reverts u
wenzelm@20244
   432
      | reverts (Abs (x, T, t)) = Abs (x, T, reverts t)
wenzelm@20244
   433
      | reverts a = a;
wenzelm@24495
   434
  in reverts end;
wenzelm@9133
   435
wenzelm@8096
   436
wenzelm@18187
   437
wenzelm@5819
   438
(** prepare terms and propositions **)
wenzelm@5819
   439
wenzelm@16501
   440
(* read wrt. theory *)     (*exception ERROR*)
wenzelm@5819
   441
wenzelm@22712
   442
fun read_def_termTs freeze pp syn ctxt (types, sorts, used) fixed sTs =
wenzelm@22712
   443
  Sign.read_def_terms' pp (Sign.is_logtype (theory_of ctxt)) syn (consts_of ctxt) fixed
wenzelm@21772
   444
    ctxt (types, sorts) used freeze sTs;
wenzelm@5874
   445
wenzelm@22712
   446
fun read_def_termT freeze pp syn ctxt defaults fixed sT =
wenzelm@22712
   447
  apfst hd (read_def_termTs freeze pp syn ctxt defaults fixed [sT]);
wenzelm@14828
   448
wenzelm@22712
   449
fun read_term_thy freeze pp syn thy defaults fixed s =
wenzelm@22712
   450
  #1 (read_def_termT freeze pp syn thy defaults fixed (s, dummyT));
wenzelm@5874
   451
wenzelm@22712
   452
fun read_prop_thy freeze pp syn thy defaults fixed s =
wenzelm@22712
   453
  #1 (read_def_termT freeze pp syn thy defaults fixed (s, propT));
wenzelm@12072
   454
wenzelm@22712
   455
fun read_terms_thy freeze pp syn thy defaults fixed =
wenzelm@22773
   456
  #1 o read_def_termTs freeze pp syn thy defaults fixed;
wenzelm@12072
   457
wenzelm@22712
   458
fun read_props_thy freeze pp syn thy defaults fixed =
wenzelm@22712
   459
  #1 o read_def_termTs freeze pp syn thy defaults fixed o map (rpair propT);
wenzelm@5819
   460
wenzelm@5819
   461
wenzelm@19001
   462
(* local abbreviations *)
wenzelm@5819
   463
wenzelm@24501
   464
local
wenzelm@24501
   465
wenzelm@19371
   466
fun certify_consts ctxt =
wenzelm@19001
   467
  Consts.certify (pp ctxt) (tsig_of ctxt) (consts_of ctxt);
wenzelm@19001
   468
wenzelm@19897
   469
fun reject_schematic (Var (xi, _)) =
wenzelm@22678
   470
      error ("Unbound schematic variable: " ^ Term.string_of_vname xi)
wenzelm@19897
   471
  | reject_schematic (Abs (_, _, t)) = reject_schematic t
wenzelm@19897
   472
  | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
wenzelm@19897
   473
  | reject_schematic _ = ();
wenzelm@5819
   474
wenzelm@24495
   475
fun expand_binds ctxt =
wenzelm@24495
   476
  let val Mode {pattern, schematic, ...} = get_mode ctxt in
wenzelm@24495
   477
    if pattern then I
wenzelm@24495
   478
    else Variable.expand_binds ctxt #> (if schematic then I else tap reject_schematic)
wenzelm@24495
   479
  end;
wenzelm@5819
   480
wenzelm@24501
   481
in
wenzelm@24501
   482
wenzelm@24501
   483
fun expand_abbrevs ctxt = certify_consts ctxt #> expand_binds ctxt;
wenzelm@24501
   484
wenzelm@24501
   485
end;
wenzelm@24501
   486
wenzelm@5819
   487
wenzelm@24505
   488
(* term patterns *)
wenzelm@22712
   489
wenzelm@24505
   490
local
wenzelm@6550
   491
wenzelm@18310
   492
val prepare_dummies =
wenzelm@18310
   493
  let val next = ref 1 in
wenzelm@24363
   494
    fn t => NAMED_CRITICAL "prepare_dummies" (fn () =>
wenzelm@18310
   495
      let val (i, u) = Term.replace_dummy_patterns (! next, t)
wenzelm@23922
   496
      in next := i; u end)
wenzelm@18310
   497
  end;
wenzelm@6762
   498
wenzelm@18678
   499
fun reject_dummies t = Term.no_dummy_patterns t
wenzelm@18678
   500
  handle TERM _ => error "Illegal dummy pattern(s) in term";
wenzelm@6550
   501
wenzelm@24505
   502
val reject_tvars = (Term.map_types o Term.map_atyps)
wenzelm@24505
   503
  (fn T as TVar (xi, _) =>
wenzelm@24505
   504
        if not (TypeInfer.is_param xi)
wenzelm@24505
   505
        then error ("Illegal schematic type variable: " ^ Term.string_of_vname xi)
wenzelm@24505
   506
        else T
wenzelm@24505
   507
    | T => T);
wenzelm@24505
   508
wenzelm@24505
   509
in
wenzelm@24505
   510
wenzelm@24505
   511
fun prepare_pattern ctxt =
wenzelm@24505
   512
  let val Mode {pattern, schematic, ...} = get_mode ctxt in
wenzelm@24505
   513
    (if pattern orelse schematic then I else reject_tvars) #>
wenzelm@24505
   514
    (if pattern then prepare_dummies else reject_dummies)
wenzelm@24505
   515
  end;
wenzelm@24505
   516
wenzelm@24505
   517
end;
wenzelm@24505
   518
wenzelm@6550
   519
wenzelm@22763
   520
(* decoding raw terms (syntax trees) *)
wenzelm@22763
   521
wenzelm@22763
   522
fun legacy_intern_skolem ctxt internal def_type x =    (* FIXME cleanup this mess *)
wenzelm@22763
   523
  let
wenzelm@22763
   524
    val sko = lookup_skolem ctxt x;
wenzelm@22763
   525
    val is_const = can (Consts.read_const (consts_of ctxt)) x;
wenzelm@22763
   526
    val is_free = is_some sko orelse not is_const;
wenzelm@22763
   527
    val is_internal = internal x;
wenzelm@22763
   528
    val is_declared = is_some (def_type (x, ~1));
wenzelm@22763
   529
    val res =
wenzelm@22763
   530
      if is_free andalso not is_internal then (no_skolem false x; sko)
wenzelm@23361
   531
      else ((no_skolem false x; ()) handle ERROR msg =>
wenzelm@23361
   532
          legacy_feature (msg ^ ContextPosition.str_of ctxt);
wenzelm@22763
   533
        if is_internal andalso is_declared then SOME x else NONE);
wenzelm@22763
   534
  in if is_some res then res else if is_declared then SOME x else NONE end;
wenzelm@22763
   535
wenzelm@24371
   536
fun term_context ctxt =
wenzelm@24371
   537
  let val thy = theory_of ctxt in
wenzelm@24371
   538
   {get_sort = Sign.get_sort thy (Variable.def_sort ctxt),
wenzelm@24371
   539
    map_const = try (#1 o Term.dest_Const o Consts.read_const (consts_of ctxt)),
wenzelm@24371
   540
    map_free = legacy_intern_skolem ctxt (K false) (Variable.def_type ctxt false),
wenzelm@24371
   541
    map_type = Sign.intern_tycons thy,
wenzelm@24371
   542
    map_sort = Sign.intern_sort thy}
wenzelm@24371
   543
  end;
wenzelm@24371
   544
wenzelm@22763
   545
fun decode_term ctxt =
wenzelm@24371
   546
  let val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt
wenzelm@22763
   547
  in Syntax.decode_term get_sort map_const map_free map_type map_sort end;
wenzelm@22763
   548
wenzelm@22763
   549
wenzelm@5819
   550
(* read terms *)
wenzelm@5819
   551
wenzelm@10554
   552
local
wenzelm@10554
   553
skalberg@15531
   554
fun append_env e1 e2 x = (case e2 x of NONE => e1 x | some => some);
wenzelm@14720
   555
wenzelm@24505
   556
fun gen_read' read app pattern schematic ctxt0 internal more_types more_sorts more_used s =
ballarin@14174
   557
  let
wenzelm@24505
   558
    val ctxt = ctxt0
wenzelm@24505
   559
      |> set_mode (Mode {stmt = false, pattern = pattern, schematic = schematic, abbrev = false});
wenzelm@19897
   560
    val types = append_env (Variable.def_type ctxt pattern) more_types;
wenzelm@19897
   561
    val sorts = append_env (Variable.def_sort ctxt) more_sorts;
wenzelm@20163
   562
    val used = fold Name.declare more_used (Variable.names_of ctxt);
ballarin@14174
   563
  in
wenzelm@22763
   564
    (read (pp ctxt) (syn_of ctxt) ctxt (types, sorts, used)
wenzelm@22763
   565
        (legacy_intern_skolem ctxt internal types) s
wenzelm@18678
   566
      handle TERM (msg, _) => error msg)
wenzelm@24505
   567
    |> app (expand_abbrevs ctxt)
wenzelm@24505
   568
    |> app (prepare_pattern ctxt)
wenzelm@14720
   569
  end;
wenzelm@14720
   570
wenzelm@16031
   571
fun gen_read read app pattern schematic ctxt =
wenzelm@16031
   572
  gen_read' read app pattern schematic ctxt (K false) (K NONE) (K NONE) [];
wenzelm@14720
   573
wenzelm@10554
   574
in
wenzelm@10554
   575
wenzelm@16031
   576
val read_termTs_schematic = gen_read' (read_def_termTs false) (apfst o map) false true;
wenzelm@8096
   577
wenzelm@14720
   578
fun read_term_pats T ctxt =
wenzelm@16031
   579
  #1 o gen_read (read_def_termTs false) (apfst o map) true false ctxt o map (rpair T);
wenzelm@8096
   580
val read_prop_pats = read_term_pats propT;
wenzelm@8096
   581
wenzelm@22712
   582
fun read_prop_legacy ctxt =
wenzelm@22712
   583
  gen_read' (read_prop_thy true) I false false ctxt (K true) (K NONE) (K NONE) [];
wenzelm@14720
   584
wenzelm@22773
   585
val read_termTs          = gen_read (read_terms_thy true) map false false;
wenzelm@19143
   586
fun read_props schematic = gen_read (read_props_thy true) map false schematic;
wenzelm@5819
   587
wenzelm@10554
   588
end;
wenzelm@10554
   589
wenzelm@5819
   590
wenzelm@5819
   591
(* certify terms *)
wenzelm@5819
   592
wenzelm@10554
   593
local
wenzelm@10554
   594
wenzelm@24495
   595
fun gen_cert prop mode ctxt0 t =
wenzelm@24495
   596
  let val ctxt = set_mode mode ctxt0 in
wenzelm@24495
   597
    t
wenzelm@24501
   598
    |> expand_abbrevs ctxt
wenzelm@24495
   599
    |> (fn t' => #1 (Sign.certify' false prop (pp ctxt) (consts_of ctxt) (theory_of ctxt) t')
wenzelm@24495
   600
      handle TYPE (msg, _, _) => error msg
wenzelm@24495
   601
        | TERM (msg, _) => error msg)
wenzelm@24495
   602
   end;
wenzelm@16501
   603
wenzelm@10554
   604
in
wenzelm@8096
   605
wenzelm@24495
   606
val cert_term = gen_cert false mode_default;
wenzelm@22773
   607
val cert_terms = map o cert_term;
wenzelm@24495
   608
val cert_prop = gen_cert true mode_default;
wenzelm@24495
   609
fun cert_props schematic = map o gen_cert true (if schematic then mode_schematic else mode_default);
wenzelm@10554
   610
wenzelm@24495
   611
fun cert_term_pats _ = map o gen_cert false mode_pattern;
wenzelm@24495
   612
val cert_prop_pats = map o gen_cert true mode_pattern;
wenzelm@10554
   613
wenzelm@10554
   614
end;
wenzelm@5819
   615
wenzelm@5819
   616
wenzelm@24495
   617
(* type checking/inference *)
wenzelm@22701
   618
wenzelm@24495
   619
fun standard_infer_types ctxt ts =
wenzelm@24495
   620
  let val Mode {pattern, ...} = get_mode ctxt in
wenzelm@24495
   621
    TypeInfer.infer_types (pp ctxt) (tsig_of ctxt) (Syntax.check_typs ctxt)
wenzelm@24495
   622
      (try (Consts.the_constraint (consts_of ctxt))) (Variable.def_type ctxt pattern)
wenzelm@24495
   623
      (Variable.names_of ctxt) (not pattern) (map (rpair dummyT) ts)
wenzelm@24495
   624
    |> #1 |> map #1
wenzelm@24495
   625
    handle TYPE (msg, _, _) => error msg
wenzelm@24495
   626
  end;
wenzelm@24486
   627
wenzelm@24505
   628
fun standard_term_check ctxt =
wenzelm@24505
   629
  standard_infer_types ctxt #>
wenzelm@24505
   630
  map (expand_abbrevs ctxt) #>
wenzelm@24505
   631
  map (prepare_pattern ctxt);
wenzelm@24505
   632
wenzelm@24495
   633
val _ = Context.add_setup (Context.theory_map (Syntax.add_term_check
wenzelm@24505
   634
    (fn ts => fn ctxt => (standard_term_check ctxt ts, ctxt))));
wenzelm@22701
   635
wenzelm@24495
   636
val _ = Context.add_setup (Context.theory_map (Syntax.add_typ_check
wenzelm@24495
   637
    (fn Ts => fn ctxt => (map (cert_typ_mode (Type.get_mode ctxt) ctxt) Ts, ctxt))));
wenzelm@22701
   638
wenzelm@22701
   639
wenzelm@18770
   640
(* inferred types of parameters *)
wenzelm@18770
   641
wenzelm@18770
   642
fun infer_type ctxt x =
wenzelm@24505
   643
  Term.fastype_of (singleton (Syntax.check_terms (set_mode mode_schematic ctxt))
wenzelm@24505
   644
    (Free (x, dummyT)));
wenzelm@18770
   645
wenzelm@18770
   646
fun inferred_param x ctxt =
wenzelm@18619
   647
  let val T = infer_type ctxt x
wenzelm@20163
   648
  in ((x, T), ctxt |> Variable.declare_term (Free (x, T))) end;
wenzelm@18619
   649
wenzelm@18770
   650
fun inferred_fixes ctxt =
wenzelm@20086
   651
  fold_map inferred_param (rev (map #2 (Variable.fixes_of ctxt))) ctxt;
wenzelm@5819
   652
wenzelm@5819
   653
wenzelm@15703
   654
(* type and constant names *)
wenzelm@15703
   655
wenzelm@15703
   656
fun read_tyname ctxt c =
wenzelm@20163
   657
  if Syntax.is_tid c then
wenzelm@19897
   658
    TFree (c, the_default (Sign.defaultS (theory_of ctxt)) (Variable.def_sort ctxt (c, ~1)))
wenzelm@16458
   659
  else Sign.read_tyname (theory_of ctxt) c;
wenzelm@15703
   660
wenzelm@15703
   661
fun read_const ctxt c =
wenzelm@15703
   662
  (case lookup_skolem ctxt c of
wenzelm@21208
   663
    SOME x => Free (x, infer_type ctxt x)
wenzelm@18971
   664
  | NONE => Consts.read_const (consts_of ctxt) c);
wenzelm@15703
   665
wenzelm@15703
   666
wenzelm@9553
   667
wenzelm@24371
   668
(** inner syntax parsers **)
wenzelm@24371
   669
wenzelm@24371
   670
local
wenzelm@24371
   671
wenzelm@24371
   672
fun parse_sort ctxt str =
wenzelm@24371
   673
  Syntax.standard_parse_sort ctxt (syn_of ctxt) (Sign.intern_sort (theory_of ctxt)) str;
wenzelm@24371
   674
wenzelm@24371
   675
fun parse_typ ctxt str =
wenzelm@24371
   676
  let
wenzelm@24371
   677
    val thy = ProofContext.theory_of ctxt;
wenzelm@24371
   678
    val get_sort = Sign.get_sort thy (Variable.def_sort ctxt);
wenzelm@24371
   679
    val T = Sign.intern_tycons thy
wenzelm@24371
   680
      (Syntax.standard_parse_typ ctxt (syn_of ctxt) get_sort (Sign.intern_sort thy) str);
wenzelm@24371
   681
  in T end
wenzelm@24371
   682
  handle ERROR msg => cat_error msg ("The error(s) above occurred in type " ^ quote str);
wenzelm@24371
   683
wenzelm@24371
   684
fun parse_term T ctxt str =
wenzelm@24371
   685
  let
wenzelm@24371
   686
    val thy = theory_of ctxt;
wenzelm@24495
   687
    fun infer t = singleton (standard_infer_types (Type.set_mode Type.mode_default ctxt))
wenzelm@24495
   688
        (TypeInfer.constrain t T);
wenzelm@24371
   689
    fun check t = Exn.Result (infer t) handle ERROR msg => Exn.Exn (ERROR msg);
wenzelm@24371
   690
    val {get_sort, map_const, map_free, map_type, map_sort} = term_context ctxt;
wenzelm@24511
   691
    val read = Syntax.standard_parse_term (pp ctxt) check get_sort map_const map_free map_type
wenzelm@24511
   692
      map_sort ctxt (Sign.is_logtype thy) (syn_of ctxt) ((#1 (TypeInfer.paramify_dummies T 0)));
wenzelm@24371
   693
  in read str end;
wenzelm@24371
   694
wenzelm@24371
   695
in
wenzelm@24371
   696
wenzelm@24371
   697
val _ = Syntax.install_parsers
wenzelm@24371
   698
  {sort = parse_sort, typ = parse_typ, term = parse_term dummyT, prop = parse_term propT};
wenzelm@24371
   699
wenzelm@24371
   700
end;
wenzelm@24371
   701
wenzelm@24371
   702
wenzelm@24371
   703
wenzelm@21610
   704
(** export results **)
wenzelm@21531
   705
wenzelm@20310
   706
fun common_export is_goal inner outer =
wenzelm@20310
   707
  map (Assumption.export is_goal inner outer) #>
wenzelm@20310
   708
  Variable.export inner outer;
wenzelm@8616
   709
wenzelm@20310
   710
val goal_export = common_export true;
wenzelm@20310
   711
val export = common_export false;
wenzelm@12704
   712
wenzelm@21531
   713
fun export_morphism inner outer =
wenzelm@21531
   714
  Assumption.export_morphism inner outer $>
wenzelm@21531
   715
  Variable.export_morphism inner outer;
wenzelm@21531
   716
wenzelm@21531
   717
wenzelm@15758
   718
wenzelm@5819
   719
(** bindings **)
wenzelm@5819
   720
wenzelm@8096
   721
(* simult_matches *)
wenzelm@8096
   722
wenzelm@19867
   723
fun simult_matches ctxt (t, pats) =
wenzelm@19867
   724
  (case Seq.pull (Unify.matchers (theory_of ctxt) (map (rpair t) pats)) of
wenzelm@19867
   725
    NONE => error "Pattern match failed!"
wenzelm@19867
   726
  | SOME (env, _) => map (apsnd snd) (Envir.alist_of env));
wenzelm@8096
   727
wenzelm@8096
   728
wenzelm@8096
   729
(* add_binds(_i) *)
wenzelm@5819
   730
wenzelm@7925
   731
local
wenzelm@7925
   732
wenzelm@16031
   733
fun gen_bind prep (xi as (x, _), raw_t) ctxt =
wenzelm@24511
   734
  ctxt
wenzelm@24511
   735
  |> set_mode mode_default
wenzelm@24511
   736
  |> Variable.add_binds [(xi, Option.map (prep ctxt) raw_t)];
wenzelm@5819
   737
wenzelm@10810
   738
in
wenzelm@10810
   739
wenzelm@20330
   740
fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
wenzelm@10554
   741
  | drop_schematic b = b;
wenzelm@10554
   742
wenzelm@24511
   743
val add_binds = fold (gen_bind Syntax.read_term);
wenzelm@16031
   744
val add_binds_i = fold (gen_bind cert_term);
wenzelm@8616
   745
wenzelm@16458
   746
fun auto_bind f ts ctxt = ctxt |> add_binds_i (map drop_schematic (f (theory_of ctxt) ts));
wenzelm@12147
   747
val auto_bind_goal = auto_bind AutoBind.goal;
wenzelm@12147
   748
val auto_bind_facts = auto_bind AutoBind.facts;
wenzelm@7925
   749
wenzelm@7925
   750
end;
wenzelm@5819
   751
wenzelm@5819
   752
wenzelm@8096
   753
(* match_bind(_i) *)
wenzelm@5819
   754
wenzelm@8096
   755
local
wenzelm@8096
   756
wenzelm@17860
   757
fun prep_bind prep_pats (raw_pats, t) ctxt =
wenzelm@5819
   758
  let
wenzelm@19897
   759
    val ctxt' = Variable.declare_term t ctxt;
wenzelm@19585
   760
    val pats = prep_pats (Term.fastype_of t) ctxt' raw_pats;
wenzelm@19585
   761
    val binds = simult_matches ctxt' (t, pats);
wenzelm@17860
   762
  in (binds, ctxt') end;
wenzelm@7670
   763
wenzelm@10465
   764
fun gen_binds prep_terms prep_pats gen raw_binds ctxt =
wenzelm@8616
   765
  let
wenzelm@24511
   766
    val ts = prep_terms (set_mode mode_default ctxt) (map snd raw_binds);
wenzelm@17860
   767
    val (binds, ctxt') =
wenzelm@19482
   768
      apfst flat (fold_map (prep_bind prep_pats) (map fst raw_binds ~~ ts) ctxt);
wenzelm@8616
   769
    val binds' =
wenzelm@19916
   770
      if gen then map #1 binds ~~ Variable.exportT_terms ctxt' ctxt (map #2 binds)
wenzelm@8616
   771
      else binds;
skalberg@15531
   772
    val binds'' = map (apsnd SOME) binds';
wenzelm@18310
   773
    val ctxt'' =
wenzelm@19897
   774
      tap (Variable.warn_extra_tfrees ctxt)
wenzelm@18310
   775
       (if gen then
wenzelm@19897
   776
          ctxt (*sic!*) |> fold Variable.declare_term (map #2 binds') |> add_binds_i binds''
wenzelm@18310
   777
        else ctxt' |> add_binds_i binds'');
wenzelm@18310
   778
  in (ts, ctxt'') end;
wenzelm@8096
   779
wenzelm@8096
   780
in
wenzelm@5935
   781
wenzelm@24511
   782
val match_bind = gen_binds Syntax.read_terms read_term_pats;
wenzelm@22773
   783
val match_bind_i = gen_binds cert_terms cert_term_pats;
wenzelm@8096
   784
wenzelm@8096
   785
end;
wenzelm@5935
   786
wenzelm@5935
   787
wenzelm@10465
   788
(* propositions with patterns *)
wenzelm@5935
   789
wenzelm@10465
   790
local
wenzelm@8096
   791
wenzelm@10554
   792
fun prep_propp schematic prep_props prep_pats (context, args) =
wenzelm@10465
   793
  let
wenzelm@19585
   794
    fun prep (_, raw_pats) (ctxt, prop :: props) =
wenzelm@19897
   795
          let val ctxt' = Variable.declare_term prop ctxt
wenzelm@19585
   796
          in ((prop, prep_pats ctxt' raw_pats), (ctxt', props)) end
wenzelm@17860
   797
      | prep _ _ = sys_error "prep_propp";
wenzelm@17860
   798
    val (propp, (context', _)) = (fold_map o fold_map) prep args
wenzelm@19482
   799
      (context, prep_props schematic context (maps (map fst) args));
wenzelm@10465
   800
  in (context', propp) end;
wenzelm@5935
   801
wenzelm@10465
   802
fun gen_bind_propp prepp (ctxt, raw_args) =
wenzelm@8096
   803
  let
wenzelm@10465
   804
    val (ctxt', args) = prepp (ctxt, raw_args);
wenzelm@19585
   805
    val binds = flat (flat (map (map (simult_matches ctxt')) args));
wenzelm@10465
   806
    val propss = map (map #1) args;
wenzelm@8616
   807
wenzelm@10554
   808
    (*generalize result: context evaluated now, binds added later*)
wenzelm@19916
   809
    val gen = Variable.exportT_terms ctxt' ctxt;
skalberg@15531
   810
    fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map SOME (gen (map #2 binds)));
skalberg@15531
   811
  in (ctxt' |> add_binds_i (map (apsnd SOME) binds), (propss, gen_binds)) end;
wenzelm@8096
   812
wenzelm@10465
   813
in
wenzelm@10465
   814
wenzelm@11925
   815
val read_propp = prep_propp false read_props read_prop_pats;
wenzelm@11925
   816
val cert_propp = prep_propp false cert_props cert_prop_pats;
wenzelm@10554
   817
val read_propp_schematic = prep_propp true read_props read_prop_pats;
wenzelm@10554
   818
val cert_propp_schematic = prep_propp true cert_props cert_prop_pats;
wenzelm@10554
   819
wenzelm@11925
   820
val bind_propp = gen_bind_propp read_propp;
wenzelm@11925
   821
val bind_propp_i = gen_bind_propp cert_propp;
wenzelm@11925
   822
val bind_propp_schematic = gen_bind_propp read_propp_schematic;
wenzelm@10554
   823
val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic;
wenzelm@6789
   824
wenzelm@10465
   825
end;
wenzelm@10465
   826
wenzelm@6789
   827
wenzelm@5819
   828
wenzelm@5819
   829
(** theorems **)
wenzelm@5819
   830
wenzelm@18042
   831
(* fact_tac *)
wenzelm@18042
   832
wenzelm@18122
   833
fun comp_incr_tac [] _ st = no_tac st
wenzelm@18122
   834
  | comp_incr_tac (th :: ths) i st =
wenzelm@18122
   835
      (Goal.compose_hhf_tac (Drule.incr_indexes st th) i APPEND comp_incr_tac ths i) st;
wenzelm@18042
   836
wenzelm@21687
   837
fun fact_tac facts = Goal.norm_hhf_tac THEN' comp_incr_tac facts;
wenzelm@18122
   838
wenzelm@18122
   839
fun some_fact_tac ctxt = SUBGOAL (fn (goal, i) =>
wenzelm@18042
   840
  let
wenzelm@19001
   841
    val index = fact_index_of ctxt;
wenzelm@18042
   842
    val facts = FactIndex.could_unify index (Term.strip_all_body goal);
wenzelm@18042
   843
  in fact_tac facts i end);
wenzelm@18042
   844
wenzelm@18042
   845
wenzelm@6091
   846
(* get_thm(s) *)
wenzelm@5819
   847
wenzelm@18042
   848
fun retrieve_thms _ pick ctxt (Fact s) =
wenzelm@16501
   849
      let
wenzelm@24511
   850
        val prop = Syntax.read_prop (set_mode mode_default ctxt) s
wenzelm@24511
   851
          |> singleton (Variable.polymorphic ctxt);
wenzelm@24511
   852
        val th = Goal.prove ctxt [] [] prop (K (ALLGOALS (some_fact_tac ctxt)))
wenzelm@18678
   853
          handle ERROR msg => cat_error msg "Failed to retrieve literal fact.";
wenzelm@18042
   854
      in pick "" [th] end
wenzelm@18042
   855
  | retrieve_thms from_thy pick ctxt xthmref =
wenzelm@18042
   856
      let
wenzelm@18042
   857
        val thy = theory_of ctxt;
wenzelm@19001
   858
        val (space, tab) = #1 (thms_of ctxt);
wenzelm@16501
   859
        val thmref = PureThy.map_name_of_thmref (NameSpace.intern space) xthmref;
wenzelm@16501
   860
        val name = PureThy.name_of_thmref thmref;
wenzelm@24012
   861
        val thms =
wenzelm@24012
   862
          if name = "" then [Thm.transfer thy Drule.dummy_thm]
wenzelm@24012
   863
          else
wenzelm@24012
   864
            (case Symtab.lookup tab name of
wenzelm@24012
   865
              SOME ths => map (Thm.transfer thy) (PureThy.select_thm thmref ths)
wenzelm@24012
   866
            | NONE => from_thy thy xthmref);
wenzelm@24012
   867
      in pick name thms end;
wenzelm@5819
   868
wenzelm@9566
   869
val get_thm = retrieve_thms PureThy.get_thms PureThy.single_thm;
wenzelm@9566
   870
val get_thm_closure = retrieve_thms PureThy.get_thms_closure PureThy.single_thm;
wenzelm@9566
   871
val get_thms = retrieve_thms PureThy.get_thms (K I);
wenzelm@9566
   872
val get_thms_closure = retrieve_thms PureThy.get_thms_closure (K I);
wenzelm@5819
   873
wenzelm@5819
   874
wenzelm@16031
   875
(* valid_thms *)
wenzelm@16031
   876
wenzelm@16031
   877
fun valid_thms ctxt (name, ths) =
wenzelm@18678
   878
  (case try (fn () => get_thms ctxt (Name name)) () of
wenzelm@16031
   879
    NONE => false
wenzelm@16147
   880
  | SOME ths' => Thm.eq_thms (ths, ths'));
wenzelm@16031
   881
wenzelm@16031
   882
wenzelm@16031
   883
(* lthms_containing *)
wenzelm@16031
   884
wenzelm@16031
   885
fun lthms_containing ctxt spec =
wenzelm@16031
   886
  FactIndex.find (fact_index_of ctxt) spec
wenzelm@21569
   887
  |> map (fn (name, ths) =>
wenzelm@21569
   888
    if valid_thms ctxt (name, ths) then (name, ths)
wenzelm@21569
   889
    else (NameSpace.hidden (if name = "" then "unnamed" else name), ths));
wenzelm@16031
   890
wenzelm@16031
   891
wenzelm@13425
   892
(* name space operations *)
wenzelm@12309
   893
haftmann@22352
   894
val add_path        = map_naming o NameSpace.add_path;
wenzelm@19062
   895
val no_base_names   = map_naming NameSpace.no_base_names;
wenzelm@16147
   896
val qualified_names = map_naming NameSpace.qualified_names;
wenzelm@19062
   897
val sticky_prefix   = map_naming o NameSpace.sticky_prefix;
wenzelm@19062
   898
val restore_naming  = map_naming o K o naming_of;
wenzelm@21728
   899
val reset_naming    = map_naming (K local_naming);
wenzelm@12309
   900
wenzelm@19001
   901
fun hide_thms fully names = map_thms (fn ((space, tab), index) =>
wenzelm@19001
   902
  ((fold (NameSpace.hide fully) names space, tab), index));
wenzelm@13425
   903
wenzelm@12309
   904
wenzelm@17360
   905
(* put_thms *)
wenzelm@5819
   906
wenzelm@21443
   907
fun update_thms _ ("", NONE) ctxt = ctxt
wenzelm@21648
   908
  | update_thms opts ("", SOME ths) ctxt = ctxt |> map_thms (fn (facts, index) =>
wenzelm@18672
   909
      let
wenzelm@21648
   910
        val index' = uncurry FactIndex.add_local opts ("", ths) index;
wenzelm@19001
   911
      in (facts, index') end)
wenzelm@21443
   912
  | update_thms _ (bname, NONE) ctxt = ctxt |> map_thms (fn ((space, tab), index) =>
wenzelm@18672
   913
      let
wenzelm@19387
   914
        val name = full_name ctxt bname;
wenzelm@18672
   915
        val tab' = Symtab.delete_safe name tab;
wenzelm@19001
   916
      in ((space, tab'), index) end)
wenzelm@21648
   917
  | update_thms opts (bname, SOME ths) ctxt = ctxt |> map_thms (fn ((space, tab), index) =>
wenzelm@18672
   918
      let
wenzelm@19387
   919
        val name = full_name ctxt bname;
wenzelm@19001
   920
        val space' = NameSpace.declare (naming_of ctxt) name space;
wenzelm@18672
   921
        val tab' = Symtab.update (name, ths) tab;
wenzelm@21648
   922
        val index' = uncurry FactIndex.add_local opts (name, ths) index;
wenzelm@19001
   923
      in ((space', tab'), index') end);
wenzelm@5819
   924
wenzelm@21443
   925
fun put_thms thms ctxt =
wenzelm@21648
   926
  ctxt |> map_naming (K local_naming) |> update_thms (true, false) thms |> restore_naming ctxt;
wenzelm@19079
   927
wenzelm@7606
   928
wenzelm@14564
   929
(* note_thmss *)
wenzelm@5819
   930
wenzelm@12711
   931
local
wenzelm@16147
   932
wenzelm@21443
   933
fun gen_note_thmss get k = fold_map (fn ((bname, more_attrs), raw_facts) => fn ctxt =>
wenzelm@5819
   934
  let
wenzelm@21643
   935
    val name = full_name ctxt bname;
wenzelm@21643
   936
    val facts = PureThy.name_thmss false name (map (apfst (get ctxt)) raw_facts);
wenzelm@21443
   937
    fun app (th, attrs) x =
wenzelm@21643
   938
      swap (foldl_map (Thm.proof_attributes (attrs @ more_attrs @ [PureThy.kind k])) (x, th));
wenzelm@21443
   939
    val (res, ctxt') = fold_map app facts ctxt;
wenzelm@21643
   940
    val thms = PureThy.name_thms false false name (flat res);
wenzelm@24388
   941
    val Mode {stmt, ...} = get_mode ctxt;
wenzelm@24388
   942
  in ((bname, thms), ctxt' |> update_thms (stmt, true) (bname, SOME thms)) end);
wenzelm@12711
   943
wenzelm@12711
   944
in
wenzelm@12711
   945
haftmann@21622
   946
fun note_thmss k = gen_note_thmss get_thms k;
haftmann@21622
   947
fun note_thmss_i k = gen_note_thmss (K I) k;
ballarin@15696
   948
wenzelm@12711
   949
end;
wenzelm@9196
   950
wenzelm@5819
   951
wenzelm@5819
   952
wenzelm@18672
   953
(** parameters **)
wenzelm@17360
   954
wenzelm@8096
   955
(* variables *)
wenzelm@8096
   956
wenzelm@19897
   957
fun declare_var (x, opt_T, mx) ctxt =
wenzelm@22701
   958
  let val T = (case opt_T of SOME T => T | NONE => Syntax.mixfixT mx)
wenzelm@20163
   959
  in ((x, T, mx), ctxt |> Variable.declare_constraints (Free (x, T))) end;
wenzelm@19897
   960
wenzelm@10381
   961
local
wenzelm@10381
   962
wenzelm@18672
   963
fun prep_vars prep_typ internal legacy =
wenzelm@18672
   964
  fold_map (fn (raw_x, raw_T, raw_mx) => fn ctxt =>
wenzelm@18672
   965
    let
wenzelm@19371
   966
      val (x, mx) = Syntax.const_mixfix raw_x raw_mx;
wenzelm@18672
   967
      val _ =
wenzelm@18678
   968
        if not legacy andalso not (Syntax.is_identifier (no_skolem internal x)) then
wenzelm@18678
   969
          error ("Illegal variable name: " ^ quote x)
wenzelm@18672
   970
        else ();
wenzelm@12504
   971
wenzelm@18672
   972
      fun cond_tvars T =
wenzelm@18672
   973
        if internal then T
wenzelm@18678
   974
        else Type.no_tvars T handle TYPE (msg, _, _) => error msg;
wenzelm@24277
   975
      val opt_T = Option.map (cond_tvars o cert_typ ctxt o prep_typ ctxt) raw_T;
wenzelm@18672
   976
      val var = (x, opt_T, mx);
wenzelm@19001
   977
    in (var, ctxt |> declare_var var |> #2) end);
wenzelm@8096
   978
wenzelm@10381
   979
in
wenzelm@10381
   980
wenzelm@24277
   981
val read_vars        = prep_vars Syntax.parse_typ false false;
wenzelm@24277
   982
val cert_vars        = prep_vars (K I) true false;
wenzelm@24277
   983
val read_vars_legacy = prep_vars Syntax.parse_typ true true;
wenzelm@24277
   984
val cert_vars_legacy = prep_vars (K I) true true;
wenzelm@8096
   985
wenzelm@10381
   986
end;
wenzelm@10381
   987
wenzelm@8096
   988
wenzelm@19681
   989
(* authentic constants *)
wenzelm@19663
   990
wenzelm@21208
   991
fun const_syntax ctxt (Free (x, T), mx) = SOME (true, (x, T, mx))
wenzelm@21667
   992
  | const_syntax ctxt (Const (c, _), mx) =
wenzelm@21667
   993
      Option.map (pair false) (try (Consts.syntax (consts_of ctxt)) (c, mx))
wenzelm@21208
   994
  | const_syntax _ _ = NONE;
wenzelm@21208
   995
wenzelm@21772
   996
fun context_const_ast_tr ctxt [Syntax.Variable c] =
wenzelm@19681
   997
      let
wenzelm@21772
   998
        val consts = consts_of ctxt;
wenzelm@19681
   999
        val c' = Consts.intern consts c;
wenzelm@21728
  1000
        val _ = Consts.the_constraint consts c' handle TYPE (msg, _, _) => error msg;
wenzelm@19681
  1001
      in Syntax.Constant (Syntax.constN ^ c') end
wenzelm@19681
  1002
  | context_const_ast_tr _ asts = raise Syntax.AST ("const_ast_tr", asts);
wenzelm@19681
  1003
wenzelm@19681
  1004
val _ = Context.add_setup
wenzelm@19681
  1005
 (Sign.add_syntax
wenzelm@19681
  1006
   [("_context_const", "id => 'a", Delimfix "CONST _"),
wenzelm@19681
  1007
    ("_context_const", "longid => 'a", Delimfix "CONST _")] #>
wenzelm@19681
  1008
  Sign.add_advanced_trfuns ([("_context_const", context_const_ast_tr)], [], [], []));
wenzelm@19681
  1009
wenzelm@19663
  1010
wenzelm@21744
  1011
(* notation *)
wenzelm@21744
  1012
wenzelm@21744
  1013
fun add_notation mode args ctxt =
wenzelm@21744
  1014
  ctxt |> map_syntax
wenzelm@21744
  1015
    (LocalSyntax.add_modesyntax (theory_of ctxt) mode (map_filter (const_syntax ctxt) args));
wenzelm@21744
  1016
wenzelm@21803
  1017
fun target_notation mode args phi =
wenzelm@21803
  1018
  let val args' = filter (fn (t, _) => Term.equiv_types (t, Morphism.term phi t)) args;
wenzelm@21744
  1019
  in Context.mapping (Sign.add_notation mode args') (add_notation mode args') end;
wenzelm@21744
  1020
wenzelm@21744
  1021
wenzelm@19001
  1022
(* abbreviations *)
wenzelm@19001
  1023
wenzelm@21728
  1024
val set_expand_abbrevs = map_consts o apsnd o Consts.set_expand;
wenzelm@24511
  1025
fun read_term_abbrev ctxt = Syntax.read_term (set_expand_abbrevs false ctxt);
wenzelm@19371
  1026
wenzelm@21704
  1027
fun add_abbrev mode (c, raw_t) ctxt =
wenzelm@19001
  1028
  let
wenzelm@21728
  1029
    val t0 = cert_term (ctxt |> set_expand_abbrevs false) raw_t
wenzelm@21681
  1030
      handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
wenzelm@20008
  1031
    val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
wenzelm@21807
  1032
    val ((lhs, rhs), consts') = consts_of ctxt
wenzelm@21728
  1033
      |> Consts.abbreviate (pp ctxt) (tsig_of ctxt) (naming_of ctxt) mode (c, t);
wenzelm@19001
  1034
  in
wenzelm@19001
  1035
    ctxt
wenzelm@21681
  1036
    |> map_consts (apsnd (K consts'))
wenzelm@21803
  1037
    |> Variable.declare_term rhs
wenzelm@21803
  1038
    |> pair (lhs, rhs)
wenzelm@21704
  1039
  end;
wenzelm@19001
  1040
wenzelm@19001
  1041
wenzelm@18672
  1042
(* fixes *)
wenzelm@5819
  1043
wenzelm@8096
  1044
local
wenzelm@8096
  1045
wenzelm@19001
  1046
fun prep_mixfix (x, T, mx) =
wenzelm@19019
  1047
  if mx <> NoSyn andalso mx <> Structure andalso
wenzelm@20086
  1048
      (can Name.dest_internal x orelse can Name.dest_skolem x) then
wenzelm@19001
  1049
    error ("Illegal mixfix syntax for internal/skolem constant " ^ quote x)
wenzelm@19001
  1050
  else (true, (x, T, mx));
wenzelm@19001
  1051
wenzelm@18844
  1052
fun gen_fixes prep raw_vars ctxt =
wenzelm@8096
  1053
  let
wenzelm@22712
  1054
    val (vars, _) = prep raw_vars ctxt;
wenzelm@22712
  1055
    val (xs', ctxt') = Variable.add_fixes (map #1 vars) ctxt;
wenzelm@8096
  1056
  in
wenzelm@22712
  1057
    ctxt'
wenzelm@19001
  1058
    |> fold_map declare_var (map2 (fn x' => fn (_, T, mx) => (x', T, mx)) xs' vars)
wenzelm@19897
  1059
    |-> (map_syntax o LocalSyntax.add_syntax (theory_of ctxt) o map prep_mixfix)
wenzelm@18672
  1060
    |> pair xs'
wenzelm@8096
  1061
  end;
wenzelm@5819
  1062
wenzelm@8096
  1063
in
wenzelm@7679
  1064
wenzelm@18844
  1065
val add_fixes = gen_fixes read_vars;
wenzelm@18844
  1066
val add_fixes_i = gen_fixes cert_vars;
wenzelm@18844
  1067
val add_fixes_legacy = gen_fixes cert_vars_legacy;
wenzelm@8096
  1068
wenzelm@8096
  1069
end;
wenzelm@5819
  1070
wenzelm@18672
  1071
wenzelm@18672
  1072
(* fixes vs. frees *)
wenzelm@12016
  1073
wenzelm@18672
  1074
fun auto_fixes (arg as (ctxt, (propss, x))) =
wenzelm@21370
  1075
  ((fold o fold) Variable.auto_fixes propss ctxt, (propss, x));
wenzelm@18672
  1076
wenzelm@18672
  1077
fun bind_fixes xs ctxt =
wenzelm@9291
  1078
  let
wenzelm@18672
  1079
    val (_, ctxt') = ctxt |> add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs);
wenzelm@9291
  1080
    fun bind (t as Free (x, T)) =
wenzelm@18340
  1081
          if member (op =) xs x then
skalberg@15531
  1082
            (case lookup_skolem ctxt' x of SOME x' => Free (x', T) | NONE => t)
wenzelm@9291
  1083
          else t
wenzelm@9291
  1084
      | bind (t $ u) = bind t $ bind u
wenzelm@9291
  1085
      | bind (Abs (x, T, t)) = Abs (x, T, bind t)
wenzelm@9291
  1086
      | bind a = a;
wenzelm@18672
  1087
  in (bind, ctxt') end;
wenzelm@18672
  1088
wenzelm@9291
  1089
wenzelm@9291
  1090
wenzelm@18672
  1091
(** assumptions **)
wenzelm@18187
  1092
wenzelm@20209
  1093
local
wenzelm@20209
  1094
wenzelm@20209
  1095
fun gen_assms prepp exp args ctxt =
wenzelm@20209
  1096
  let
wenzelm@20234
  1097
    val cert = Thm.cterm_of (theory_of ctxt);
wenzelm@20209
  1098
    val (propss, ctxt1) = swap (prepp (ctxt, map snd args));
wenzelm@20234
  1099
    val _ = Variable.warn_extra_tfrees ctxt ctxt1;
wenzelm@20234
  1100
    val (premss, ctxt2) = fold_burrow (Assumption.add_assms exp o map cert) propss ctxt1;
wenzelm@20234
  1101
  in
wenzelm@20234
  1102
    ctxt2
wenzelm@20234
  1103
    |> auto_bind_facts (flat propss)
wenzelm@21443
  1104
    |> put_thms (AutoBind.premsN, SOME (Assumption.prems_of ctxt2))
wenzelm@21443
  1105
    |> note_thmss_i Thm.assumptionK (map fst args ~~ map (map (fn th => ([th], []))) premss)
wenzelm@20234
  1106
  end;
wenzelm@20209
  1107
wenzelm@20209
  1108
in
wenzelm@20209
  1109
wenzelm@20209
  1110
val add_assms = gen_assms (apsnd #1 o bind_propp);
wenzelm@20209
  1111
val add_assms_i = gen_assms (apsnd #1 o bind_propp_i);
wenzelm@20209
  1112
wenzelm@20209
  1113
end;
wenzelm@20209
  1114
wenzelm@20209
  1115
wenzelm@5819
  1116
wenzelm@8373
  1117
(** cases **)
wenzelm@8373
  1118
wenzelm@16147
  1119
local
wenzelm@16147
  1120
wenzelm@16668
  1121
fun rem_case name = remove (fn (x: string, (y, _)) => x = y) name;
wenzelm@16147
  1122
wenzelm@18476
  1123
fun add_case _ ("", _) cases = cases
wenzelm@18476
  1124
  | add_case _ (name, NONE) cases = rem_case name cases
wenzelm@18476
  1125
  | add_case is_proper (name, SOME c) cases = (name, (c, is_proper)) :: rem_case name cases;
wenzelm@16147
  1126
wenzelm@18678
  1127
fun prep_case name fxs c =
wenzelm@18609
  1128
  let
wenzelm@18609
  1129
    fun replace (opt_x :: xs) ((y, T) :: ys) = (the_default y opt_x, T) :: replace xs ys
wenzelm@18609
  1130
      | replace [] ys = ys
wenzelm@18678
  1131
      | replace (_ :: _) [] = error ("Too many parameters for case " ^ quote name);
wenzelm@18609
  1132
    val RuleCases.Case {fixes, assumes, binds, cases} = c;
wenzelm@18609
  1133
    val fixes' = replace fxs fixes;
wenzelm@18609
  1134
    val binds' = map drop_schematic binds;
wenzelm@18609
  1135
  in
wenzelm@18609
  1136
    if null (fold (Term.add_tvarsT o snd) fixes []) andalso
wenzelm@18609
  1137
      null (fold (fold Term.add_vars o snd) assumes []) then
wenzelm@18609
  1138
        RuleCases.Case {fixes = fixes', assumes = assumes, binds = binds', cases = cases}
wenzelm@18678
  1139
    else error ("Illegal schematic variable(s) in case " ^ quote name)
wenzelm@18609
  1140
  end;
wenzelm@18609
  1141
wenzelm@18672
  1142
fun fix (x, T) ctxt =
wenzelm@18672
  1143
  let
wenzelm@18672
  1144
    val (bind, ctxt') = bind_fixes [x] ctxt;
wenzelm@18672
  1145
    val t = bind (Free (x, T));
wenzelm@20163
  1146
  in (t, ctxt' |> Variable.declare_constraints t) end;
wenzelm@18672
  1147
wenzelm@16147
  1148
in
wenzelm@16147
  1149
wenzelm@18672
  1150
fun add_cases is_proper = map_cases o fold (add_case is_proper);
wenzelm@18609
  1151
wenzelm@18609
  1152
fun case_result c ctxt =
wenzelm@18609
  1153
  let
wenzelm@18609
  1154
    val RuleCases.Case {fixes, ...} = c;
wenzelm@18672
  1155
    val (ts, ctxt') = ctxt |> fold_map fix fixes;
wenzelm@18672
  1156
    val RuleCases.Case {assumes, binds, cases, ...} = RuleCases.apply ts c;
wenzelm@18609
  1157
  in
wenzelm@18609
  1158
    ctxt'
wenzelm@18699
  1159
    |> add_binds_i (map drop_schematic binds)
wenzelm@18609
  1160
    |> add_cases true (map (apsnd SOME) cases)
wenzelm@18609
  1161
    |> pair (assumes, (binds, cases))
wenzelm@18609
  1162
  end;
wenzelm@18609
  1163
wenzelm@18609
  1164
val apply_case = apfst fst oo case_result;
wenzelm@18609
  1165
wenzelm@16540
  1166
fun get_case ctxt name xs =
wenzelm@17184
  1167
  (case AList.lookup (op =) (cases_of ctxt) name of
wenzelm@18678
  1168
    NONE => error ("Unknown case: " ^ quote name)
wenzelm@18678
  1169
  | SOME (c, _) => prep_case name xs c);
wenzelm@8373
  1170
wenzelm@16147
  1171
end;
wenzelm@8373
  1172
wenzelm@8373
  1173
wenzelm@8373
  1174
wenzelm@10810
  1175
(** print context information **)
wenzelm@10810
  1176
wenzelm@20310
  1177
val debug = ref false;
wenzelm@20310
  1178
wenzelm@10810
  1179
val verbose = ref false;
wenzelm@10810
  1180
fun verb f x = if ! verbose then f (x ()) else [];
wenzelm@10810
  1181
wenzelm@10810
  1182
fun setmp_verbose f x = Library.setmp verbose true f x;
wenzelm@10810
  1183
wenzelm@10810
  1184
wenzelm@12072
  1185
(* local syntax *)
wenzelm@12072
  1186
wenzelm@12093
  1187
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@12072
  1188
wenzelm@12072
  1189
wenzelm@21728
  1190
(* abbreviations *)
wenzelm@18971
  1191
wenzelm@21728
  1192
fun pretty_abbrevs show_globals ctxt =
wenzelm@18971
  1193
  let
wenzelm@19033
  1194
    val ((_, globals), (space, consts)) =
wenzelm@19033
  1195
      pairself (#constants o Consts.dest) (#consts (rep_context ctxt));
wenzelm@21803
  1196
    fun add_abbr (_, (_, NONE)) = I
wenzelm@21803
  1197
      | add_abbr (c, (T, SOME (t, _))) =
wenzelm@21728
  1198
          if not show_globals andalso Symtab.defined globals c then I
wenzelm@21728
  1199
          else cons (c, Logic.mk_equals (Const (c, T), t));
wenzelm@21803
  1200
    val abbrevs = NameSpace.extern_table (space, Symtab.make (Symtab.fold add_abbr consts []));
wenzelm@18971
  1201
  in
wenzelm@18971
  1202
    if null abbrevs andalso not (! verbose) then []
wenzelm@21728
  1203
    else [Pretty.big_list "abbreviations:" (map (pretty_term_abbrev ctxt o #2) abbrevs)]
wenzelm@18971
  1204
  end;
wenzelm@18971
  1205
wenzelm@21728
  1206
val print_abbrevs = Pretty.writeln o Pretty.chunks o pretty_abbrevs true;
wenzelm@21728
  1207
wenzelm@18971
  1208
wenzelm@10810
  1209
(* term bindings *)
wenzelm@10810
  1210
wenzelm@16540
  1211
fun pretty_binds ctxt =
wenzelm@10810
  1212
  let
wenzelm@19897
  1213
    val binds = Variable.binds_of ctxt;
wenzelm@21728
  1214
    fun prt_bind (xi, (T, t)) = pretty_term_abbrev ctxt (Logic.mk_equals (Var (xi, T), t));
wenzelm@10810
  1215
  in
wenzelm@15758
  1216
    if Vartab.is_empty binds andalso not (! verbose) then []
wenzelm@15758
  1217
    else [Pretty.big_list "term bindings:" (map prt_bind (Vartab.dest binds))]
wenzelm@10810
  1218
  end;
wenzelm@10810
  1219
wenzelm@10810
  1220
val print_binds = Pretty.writeln o Pretty.chunks o pretty_binds;
wenzelm@10810
  1221
wenzelm@10810
  1222
wenzelm@10810
  1223
(* local theorems *)
wenzelm@10810
  1224
wenzelm@16540
  1225
fun pretty_lthms ctxt =
wenzelm@20012
  1226
  let
wenzelm@20012
  1227
    val props = FactIndex.props (fact_index_of ctxt);
wenzelm@20012
  1228
    val facts =
wenzelm@20012
  1229
      (if null props then I else cons ("unnamed", props))
wenzelm@20012
  1230
      (NameSpace.extern_table (#1 (thms_of ctxt)));
wenzelm@20012
  1231
  in
wenzelm@20012
  1232
    if null facts andalso not (! verbose) then []
wenzelm@20012
  1233
    else [Pretty.big_list "facts:" (map (pretty_fact ctxt) facts)]
wenzelm@20012
  1234
  end;
wenzelm@10810
  1235
wenzelm@12057
  1236
val print_lthms = Pretty.writeln o Pretty.chunks o pretty_lthms;
wenzelm@10810
  1237
wenzelm@10810
  1238
wenzelm@10810
  1239
(* local contexts *)
wenzelm@10810
  1240
wenzelm@16540
  1241
fun pretty_cases ctxt =
wenzelm@10810
  1242
  let
wenzelm@12057
  1243
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1244
wenzelm@10810
  1245
    fun prt_let (xi, t) = Pretty.block
wenzelm@10818
  1246
      [Pretty.quote (prt_term (Var (xi, Term.fastype_of t))), Pretty.str " =", Pretty.brk 1,
wenzelm@10810
  1247
        Pretty.quote (prt_term t)];
wenzelm@10810
  1248
wenzelm@13425
  1249
    fun prt_asm (a, ts) = Pretty.block (Pretty.breaks
wenzelm@13425
  1250
      ((if a = "" then [] else [Pretty.str (a ^ ":")]) @ map (Pretty.quote o prt_term) ts));
wenzelm@13425
  1251
wenzelm@10810
  1252
    fun prt_sect _ _ _ [] = []
wenzelm@10810
  1253
      | prt_sect s sep prt xs = [Pretty.block (Pretty.breaks (Pretty.str s ::
wenzelm@19482
  1254
            flat (Library.separate sep (map (Library.single o prt) xs))))];
wenzelm@10810
  1255
wenzelm@18609
  1256
    fun prt_case (name, (fixes, (asms, (lets, cs)))) = Pretty.block (Pretty.fbreaks
wenzelm@10810
  1257
      (Pretty.str (name ^ ":") ::
wenzelm@11915
  1258
        prt_sect "fix" [] (Pretty.str o fst) fixes @
wenzelm@10810
  1259
        prt_sect "let" [Pretty.str "and"] prt_let
wenzelm@19482
  1260
          (map_filter (fn (xi, SOME t) => SOME (xi, t) | _ => NONE) lets) @
wenzelm@13425
  1261
        (if forall (null o #2) asms then []
wenzelm@18609
  1262
          else prt_sect "assume" [Pretty.str "and"] prt_asm asms) @
wenzelm@18609
  1263
        prt_sect "subcases:" [] (Pretty.str o fst) cs));
wenzelm@16540
  1264
wenzelm@18476
  1265
    fun add_case (_, (_, false)) = I
wenzelm@18609
  1266
      | add_case (name, (c as RuleCases.Case {fixes, ...}, true)) =
wenzelm@18609
  1267
          cons (name, (fixes, #1 (case_result c ctxt)));
wenzelm@18476
  1268
    val cases = fold add_case (cases_of ctxt) [];
wenzelm@10810
  1269
  in
wenzelm@10810
  1270
    if null cases andalso not (! verbose) then []
wenzelm@18476
  1271
    else [Pretty.big_list "cases:" (map prt_case cases)]
wenzelm@10810
  1272
  end;
wenzelm@10810
  1273
wenzelm@10810
  1274
val print_cases = Pretty.writeln o Pretty.chunks o pretty_cases;
wenzelm@10810
  1275
wenzelm@10810
  1276
wenzelm@12057
  1277
(* core context *)
wenzelm@10810
  1278
wenzelm@20367
  1279
val prems_limit = ref ~1;
wenzelm@10810
  1280
wenzelm@18672
  1281
fun pretty_ctxt ctxt =
wenzelm@20310
  1282
  if ! prems_limit < 0 andalso not (! debug) then []
wenzelm@20310
  1283
  else
wenzelm@20310
  1284
    let
wenzelm@20310
  1285
      val prt_term = pretty_term ctxt;
wenzelm@12057
  1286
wenzelm@20310
  1287
      (*structures*)
wenzelm@20310
  1288
      val structs = LocalSyntax.structs_of (syntax_of ctxt);
wenzelm@20310
  1289
      val prt_structs = if null structs then []
wenzelm@20310
  1290
        else [Pretty.block (Pretty.str "structures:" :: Pretty.brk 1 ::
wenzelm@20310
  1291
          Pretty.commas (map Pretty.str structs))];
wenzelm@12093
  1292
wenzelm@20310
  1293
      (*fixes*)
wenzelm@20310
  1294
      fun prt_fix (x, x') =
wenzelm@20310
  1295
        if x = x' then Pretty.str x
wenzelm@20310
  1296
        else Pretty.block [Pretty.str x, Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
wenzelm@20310
  1297
      val fixes =
wenzelm@20310
  1298
        rev (filter_out ((can Name.dest_internal orf member (op =) structs) o #1)
wenzelm@20310
  1299
          (Variable.fixes_of ctxt));
wenzelm@20310
  1300
      val prt_fixes = if null fixes then []
wenzelm@20310
  1301
        else [Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
wenzelm@20310
  1302
          Pretty.commas (map prt_fix fixes))];
wenzelm@12057
  1303
wenzelm@20310
  1304
      (*prems*)
wenzelm@20310
  1305
      val prems = Assumption.prems_of ctxt;
wenzelm@20310
  1306
      val len = length prems;
wenzelm@20367
  1307
      val suppressed = len - ! prems_limit;
wenzelm@20310
  1308
      val prt_prems = if null prems then []
wenzelm@20367
  1309
        else [Pretty.big_list "prems:" ((if suppressed <= 0 then [] else [Pretty.str "..."]) @
wenzelm@20367
  1310
          map (pretty_thm ctxt) (Library.drop (suppressed, prems)))];
wenzelm@20310
  1311
    in prt_structs @ prt_fixes @ prt_prems end;
wenzelm@10810
  1312
wenzelm@10810
  1313
wenzelm@10810
  1314
(* main context *)
wenzelm@10810
  1315
wenzelm@16540
  1316
fun pretty_context ctxt =
wenzelm@10810
  1317
  let
wenzelm@12057
  1318
    val prt_term = pretty_term ctxt;
wenzelm@12057
  1319
    val prt_typ = pretty_typ ctxt;
wenzelm@12057
  1320
    val prt_sort = pretty_sort ctxt;
wenzelm@10810
  1321
wenzelm@10810
  1322
    (*theory*)
wenzelm@12057
  1323
    val pretty_thy = Pretty.block
wenzelm@17384
  1324
      [Pretty.str "theory:", Pretty.brk 1, Context.pretty_thy (theory_of ctxt)];
wenzelm@10810
  1325
wenzelm@10810
  1326
    (*defaults*)
wenzelm@10810
  1327
    fun prt_atom prt prtT (x, X) = Pretty.block
wenzelm@10810
  1328
      [prt x, Pretty.str " ::", Pretty.brk 1, prtT X];
wenzelm@10810
  1329
wenzelm@10810
  1330
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@10810
  1331
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@10810
  1332
wenzelm@10810
  1333
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@10810
  1334
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@10810
  1335
wenzelm@10810
  1336
    val prt_defT = prt_atom prt_var prt_typ;
wenzelm@10810
  1337
    val prt_defS = prt_atom prt_varT prt_sort;
wenzelm@16540
  1338
wenzelm@20163
  1339
    val (types, sorts) = Variable.constraints_of ctxt;
wenzelm@10810
  1340
  in
wenzelm@18609
  1341
    verb single (K pretty_thy) @
wenzelm@18672
  1342
    pretty_ctxt ctxt @
wenzelm@21728
  1343
    verb (pretty_abbrevs false) (K ctxt) @
wenzelm@10810
  1344
    verb pretty_binds (K ctxt) @
wenzelm@12057
  1345
    verb pretty_lthms (K ctxt) @
wenzelm@10810
  1346
    verb pretty_cases (K ctxt) @
wenzelm@18609
  1347
    verb single (fn () => Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
wenzelm@20163
  1348
    verb single (fn () => Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts)))
wenzelm@10810
  1349
  end;
wenzelm@10810
  1350
wenzelm@5819
  1351
end;