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