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