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