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