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