src/Pure/Isar/isar_thy.ML
author wenzelm
Sun May 21 14:35:27 2000 +0200 (2000-05-21)
changeset 8897 fb1436ca3b2e
parent 8885 19ab913a6a6a
child 8966 916966f68907
permissions -rw-r--r--
adapted to inner syntax of sorts;
wenzelm@5830
     1
(*  Title:      Pure/Isar/isar_thy.ML
wenzelm@5830
     2
    ID:         $Id$
wenzelm@5830
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8804
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@5830
     5
wenzelm@6371
     6
Pure/Isar derived theory operations.
wenzelm@5830
     7
*)
wenzelm@5830
     8
wenzelm@5830
     9
signature ISAR_THY =
wenzelm@5830
    10
sig
wenzelm@7734
    11
  val add_header: Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@6552
    12
  val add_chapter: Comment.text -> theory -> theory
wenzelm@6552
    13
  val add_section: Comment.text -> theory -> theory
wenzelm@6552
    14
  val add_subsection: Comment.text -> theory -> theory
wenzelm@6552
    15
  val add_subsubsection: Comment.text -> theory -> theory
wenzelm@7172
    16
  val add_text: Comment.text -> theory -> theory
wenzelm@7862
    17
  val add_text_raw: Comment.text -> theory -> theory
wenzelm@7172
    18
  val add_sect: Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@7172
    19
  val add_subsect: Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@7172
    20
  val add_subsubsect: Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@7172
    21
  val add_txt: Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@7862
    22
  val add_txt_raw: Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@8724
    23
  val global_path: Comment.text -> theory -> theory
wenzelm@8724
    24
  val local_path: Comment.text -> theory -> theory
wenzelm@8724
    25
  val hide_space: (string * xstring list) * Comment.text -> theory -> theory
wenzelm@8724
    26
  val hide_space_i: (string * string list) * Comment.text -> theory -> theory
wenzelm@6728
    27
  val add_classes: (bclass * xclass list) list * Comment.text -> theory -> theory
wenzelm@6728
    28
  val add_classes_i: (bclass * class list) list * Comment.text  -> theory -> theory
wenzelm@6728
    29
  val add_classrel: (xclass * xclass) * Comment.text -> theory -> theory
wenzelm@6728
    30
  val add_classrel_i: (class * class) * Comment.text -> theory -> theory
wenzelm@8897
    31
  val add_defsort: string * Comment.text -> theory -> theory
wenzelm@6728
    32
  val add_defsort_i: sort * Comment.text -> theory -> theory
wenzelm@6728
    33
  val add_nonterminals: (bstring * Comment.text) list -> theory -> theory
wenzelm@6728
    34
  val add_tyabbrs: ((bstring * string list * string * mixfix) * Comment.text) list
wenzelm@6728
    35
    -> theory -> theory
wenzelm@6728
    36
  val add_tyabbrs_i: ((bstring * string list * typ * mixfix) * Comment.text) list
wenzelm@6728
    37
    -> theory -> theory
wenzelm@8897
    38
  val add_arities: ((xstring * string list * string) * Comment.text) list -> theory -> theory
wenzelm@6728
    39
  val add_arities_i: ((string * sort list * sort) * Comment.text) list -> theory -> theory
wenzelm@6728
    40
  val add_typedecl: (bstring * string list * mixfix) * Comment.text -> theory -> theory
wenzelm@6728
    41
  val add_consts: ((bstring * string * mixfix) * Comment.text) list -> theory -> theory
wenzelm@6728
    42
  val add_consts_i: ((bstring * typ * mixfix) * Comment.text) list -> theory -> theory
wenzelm@6728
    43
  val add_modesyntax: string * bool -> ((bstring * string * mixfix) * Comment.text) list
wenzelm@6728
    44
    -> theory -> theory
wenzelm@6728
    45
  val add_modesyntax_i: string * bool -> ((bstring * typ * mixfix) * Comment.text) list
wenzelm@6728
    46
    -> theory -> theory
wenzelm@6728
    47
  val add_trrules: ((xstring * string) Syntax.trrule * Comment.text) list -> theory -> theory
wenzelm@6728
    48
  val add_trrules_i: (Syntax.ast Syntax.trrule * Comment.text) list -> theory -> theory
wenzelm@8227
    49
  val add_judgment: (bstring * string * mixfix) * Comment.text -> theory -> theory
wenzelm@8227
    50
  val add_judgment_i: (bstring * typ * mixfix) * Comment.text -> theory -> theory
wenzelm@6728
    51
  val add_axioms: (((bstring * string) * Args.src list) * Comment.text) list -> theory -> theory
wenzelm@6728
    52
  val add_axioms_i: (((bstring * term) * theory attribute list) * Comment.text) list
wenzelm@6728
    53
    -> theory -> theory
wenzelm@6728
    54
  val add_defs: (((bstring * string) * Args.src list) * Comment.text) list -> theory -> theory
wenzelm@6728
    55
  val add_defs_i: (((bstring * term) * theory attribute list) * Comment.text) list
wenzelm@6728
    56
    -> theory -> theory
wenzelm@6728
    57
  val add_constdefs: (((bstring * string * mixfix) * Comment.text) * (string * Comment.text)) list
wenzelm@6728
    58
    -> theory -> theory
wenzelm@6728
    59
  val add_constdefs_i: (((bstring * typ * mixfix) * Comment.text) * (term * Comment.text)) list
wenzelm@6728
    60
    -> theory -> theory
wenzelm@6371
    61
  val apply_theorems: (xstring * Args.src list) list -> theory -> theory * thm list
wenzelm@6371
    62
  val apply_theorems_i: (thm * theory attribute list) list -> theory -> theory * thm list
wenzelm@6728
    63
  val have_theorems: ((bstring * Args.src list) * (xstring * Args.src list) list) * Comment.text
wenzelm@6371
    64
    -> theory -> theory
wenzelm@6728
    65
  val have_theorems_i: ((bstring * theory attribute list) * (thm * theory attribute list) list)
wenzelm@6728
    66
    * Comment.text -> theory -> theory
wenzelm@6728
    67
  val have_lemmas: ((bstring * Args.src list) * (xstring * Args.src list) list) * Comment.text
wenzelm@5915
    68
    -> theory -> theory
wenzelm@6728
    69
  val have_lemmas_i: ((bstring * theory attribute list) * (thm * theory attribute list) list)
wenzelm@6728
    70
    * Comment.text -> theory -> theory
wenzelm@6728
    71
  val have_facts: ((string * Args.src list) * (string * Args.src list) list) * Comment.text
wenzelm@5915
    72
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6728
    73
  val have_facts_i: ((string * Proof.context attribute list) *
wenzelm@6728
    74
    (thm * Proof.context attribute list) list) * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6728
    75
  val from_facts: (string * Args.src list) list * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6728
    76
  val from_facts_i: (thm * Proof.context attribute list) list * Comment.text
wenzelm@6728
    77
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6879
    78
  val with_facts: (string * Args.src list) list * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6879
    79
  val with_facts_i: (thm * Proof.context attribute list) list * Comment.text
wenzelm@6879
    80
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6728
    81
  val chain: Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@7417
    82
  val fix: ((string list * string option) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@7666
    83
  val fix_i: ((string list * typ option) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@8615
    84
  val let_bind: ((string list * string) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@8615
    85
  val let_bind_i: ((term list * term) * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@8450
    86
  val invoke_case: (string * Args.src list) * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@8450
    87
  val invoke_case_i: (string * Proof.context attribute list) * Comment.text
wenzelm@8450
    88
    -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
    89
  val theorem: (bstring * Args.src list * (string * (string list * string list)))
wenzelm@6937
    90
    * Comment.text -> bool -> theory -> ProofHistory.T
wenzelm@6937
    91
  val theorem_i: (bstring * theory attribute list * (term * (term list * term list)))
wenzelm@6937
    92
    * Comment.text -> bool -> theory -> ProofHistory.T
wenzelm@6937
    93
  val lemma: (bstring * Args.src list * (string * (string list * string list)))
wenzelm@6937
    94
    * Comment.text -> bool -> theory -> ProofHistory.T
wenzelm@6937
    95
  val lemma_i: (bstring * theory attribute list * (term * (term list * term list)))
wenzelm@6937
    96
    * Comment.text -> bool -> theory -> ProofHistory.T
wenzelm@7271
    97
  val assume: ((string * Args.src list * (string * (string list * string list)) list)
wenzelm@7271
    98
    * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@7271
    99
  val assume_i: ((string * Proof.context attribute list * (term * (term list * term list)) list)
wenzelm@7271
   100
    * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@7271
   101
  val presume: ((string * Args.src list * (string * (string list * string list)) list)
wenzelm@7271
   102
    * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@7271
   103
  val presume_i: ((string * Proof.context attribute list * (term * (term list * term list)) list)
wenzelm@7271
   104
    * Comment.text) list -> ProofHistory.T -> ProofHistory.T
wenzelm@6952
   105
  val local_def: (string * Args.src list * ((string * string option) * (string * string list)))
wenzelm@6952
   106
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@7666
   107
  val local_def_i: (string * Args.src list * ((string * typ option) * (term * term list)))
wenzelm@6952
   108
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   109
  val show: (string * Args.src list * (string * (string list * string list)))
wenzelm@6937
   110
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   111
  val show_i: (string * Proof.context attribute list * (term * (term list * term list)))
wenzelm@6937
   112
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   113
  val have: (string * Args.src list * (string * (string list * string list)))
wenzelm@6937
   114
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   115
  val have_i: (string * Proof.context attribute list * (term * (term list * term list)))
wenzelm@6937
   116
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   117
  val thus: (string * Args.src list * (string * (string list * string list)))
wenzelm@6937
   118
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   119
  val thus_i: (string * Proof.context attribute list * (term * (term list * term list)))
wenzelm@6937
   120
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   121
  val hence: (string * Args.src list * (string * (string list * string list)))
wenzelm@6937
   122
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@6937
   123
  val hence_i: (string * Proof.context attribute list * (term * (term list * term list)))
wenzelm@6937
   124
    * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@5830
   125
  val begin_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
   126
  val next_block: ProofHistory.T -> ProofHistory.T
wenzelm@5830
   127
  val end_block: ProofHistory.T -> ProofHistory.T
wenzelm@8681
   128
  val defer: int option * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@8681
   129
  val prefer: int * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@8681
   130
  val apply: Method.text * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@8681
   131
  val apply_end: Method.text * Comment.text -> ProofHistory.T -> ProofHistory.T
wenzelm@7002
   132
  val proof: (Comment.interest * (Method.text * Comment.interest) option) * Comment.text
wenzelm@6728
   133
    -> ProofHistory.T -> ProofHistory.T
wenzelm@7002
   134
  val qed: (Method.text * Comment.interest) option * Comment.text
wenzelm@6937
   135
    -> Toplevel.transition -> Toplevel.transition
wenzelm@7002
   136
  val terminal_proof: ((Method.text * Comment.interest) * (Method.text * Comment.interest) option)
wenzelm@7002
   137
    * Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@7002
   138
  val immediate_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@7002
   139
  val default_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@7002
   140
  val skip_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@8210
   141
  val forget_proof: Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@7012
   142
  val get_thmss: (string * Args.src list) list -> Proof.state -> thm list
wenzelm@6879
   143
  val also: ((string * Args.src list) list * Comment.interest) option * Comment.text
wenzelm@6879
   144
    -> Toplevel.transition -> Toplevel.transition
wenzelm@6879
   145
  val also_i: (thm list * Comment.interest) option * Comment.text
wenzelm@6879
   146
    -> Toplevel.transition -> Toplevel.transition
wenzelm@6879
   147
  val finally: ((string * Args.src list) list * Comment.interest) option * Comment.text
wenzelm@6879
   148
    -> Toplevel.transition -> Toplevel.transition
wenzelm@6879
   149
  val finally_i: (thm list * Comment.interest) option * Comment.text
wenzelm@6879
   150
    -> Toplevel.transition -> Toplevel.transition
wenzelm@8562
   151
  val moreover: Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@8588
   152
  val ultimately: Comment.text -> Toplevel.transition -> Toplevel.transition
wenzelm@5830
   153
  val parse_ast_translation: string -> theory -> theory
wenzelm@5830
   154
  val parse_translation: string -> theory -> theory
wenzelm@5830
   155
  val print_translation: string -> theory -> theory
wenzelm@5830
   156
  val typed_print_translation: string -> theory -> theory
wenzelm@5830
   157
  val print_ast_translation: string -> theory -> theory
wenzelm@5830
   158
  val token_translation: string -> theory -> theory
wenzelm@6728
   159
  val add_oracle: (bstring * string) * Comment.text -> theory -> theory
wenzelm@6331
   160
  val begin_theory: string -> string list -> (string * bool) list -> theory
wenzelm@6331
   161
  val end_theory: theory -> theory
wenzelm@7932
   162
  val kill_theory: string -> unit
wenzelm@6246
   163
  val theory: string * string list * (string * bool) list
wenzelm@6246
   164
    -> Toplevel.transition -> Toplevel.transition
wenzelm@7960
   165
  val init_context: ('a -> unit) -> 'a -> Toplevel.transition -> Toplevel.transition
wenzelm@6246
   166
  val context: string -> Toplevel.transition -> Toplevel.transition
wenzelm@5830
   167
end;
wenzelm@5830
   168
wenzelm@5830
   169
structure IsarThy: ISAR_THY =
wenzelm@5830
   170
struct
wenzelm@5830
   171
wenzelm@5830
   172
wenzelm@5830
   173
(** derived theory and proof operations **)
wenzelm@5830
   174
wenzelm@8090
   175
(* markup commands *)
wenzelm@5959
   176
wenzelm@7734
   177
fun add_header comment =
wenzelm@7734
   178
  Toplevel.keep (fn state => if Toplevel.is_toplevel state then () else raise Toplevel.UNDEF);
wenzelm@7734
   179
wenzelm@8090
   180
fun add_text _ x = x;
wenzelm@8079
   181
fun add_text_raw _ x = x;
wenzelm@7633
   182
wenzelm@8090
   183
fun add_heading add present txt thy =
wenzelm@7633
   184
  (Context.setmp (Some thy) present (Comment.string_of txt); add txt thy);
wenzelm@7633
   185
wenzelm@8090
   186
val add_chapter = add_heading add_text Present.section;
wenzelm@8090
   187
val add_section = add_heading add_text Present.section;
wenzelm@8090
   188
val add_subsection = add_heading add_text Present.subsection;
wenzelm@8090
   189
val add_subsubsection = add_heading add_text Present.subsubsection;
wenzelm@5959
   190
wenzelm@8090
   191
fun add_txt (_: Comment.text) = ProofHistory.apply I;
wenzelm@8090
   192
val add_txt_raw = add_txt;
wenzelm@7332
   193
val add_sect = add_txt;
wenzelm@7332
   194
val add_subsect = add_txt;
wenzelm@7332
   195
val add_subsubsect = add_txt;
wenzelm@7172
   196
wenzelm@5959
   197
wenzelm@8724
   198
(* name spaces *)
wenzelm@8724
   199
wenzelm@8724
   200
fun global_path comment_ignore = PureThy.global_path;
wenzelm@8724
   201
fun local_path comment_ignore = PureThy.local_path;
wenzelm@8724
   202
wenzelm@8885
   203
local
wenzelm@8885
   204
wenzelm@8885
   205
val kinds =
wenzelm@8885
   206
 [(Sign.classK, fn sg => fn c => c mem Sign.classes sg),
wenzelm@8885
   207
  (Sign.typeK, fn sg => fn c =>
wenzelm@8885
   208
    let val {tycons, abbrs, ...} = Type.rep_tsig (Sign.tsig_of sg)
wenzelm@8885
   209
    in is_some (Symtab.lookup (tycons, c)) orelse is_some (Symtab.lookup (abbrs, c)) end),
wenzelm@8885
   210
  (Sign.constK, is_some oo Sign.const_type)];
wenzelm@8724
   211
wenzelm@8885
   212
fun gen_hide intern ((kind, xnames), comment_ignore) thy =
wenzelm@8885
   213
  (case assoc (kinds, kind) of
wenzelm@8885
   214
    Some check =>
wenzelm@8885
   215
      let
wenzelm@8885
   216
        val sg = Theory.sign_of thy;
wenzelm@8885
   217
        val names = map (intern sg kind) xnames;
wenzelm@8885
   218
        val bads = filter_out (check sg) names;
wenzelm@8885
   219
      in
wenzelm@8885
   220
        if null bads then Theory.hide_space_i (kind, names) thy
wenzelm@8885
   221
        else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
wenzelm@8885
   222
      end
wenzelm@8885
   223
  | None => error ("Bad name space specification: " ^ quote kind));
wenzelm@8885
   224
wenzelm@8885
   225
in
wenzelm@8885
   226
wenzelm@8885
   227
fun hide_space x = gen_hide Sign.intern x;
wenzelm@8885
   228
fun hide_space_i x = gen_hide (K (K I)) x;
wenzelm@8885
   229
wenzelm@8885
   230
end;
wenzelm@8724
   231
wenzelm@8724
   232
wenzelm@6728
   233
(* signature and syntax *)
wenzelm@6728
   234
wenzelm@6728
   235
val add_classes = Theory.add_classes o Comment.ignore;
wenzelm@6728
   236
val add_classes_i = Theory.add_classes_i o Comment.ignore;
wenzelm@6728
   237
val add_classrel = Theory.add_classrel o single o Comment.ignore;
wenzelm@6728
   238
val add_classrel_i = Theory.add_classrel_i o single o Comment.ignore;
wenzelm@6728
   239
val add_defsort = Theory.add_defsort o Comment.ignore;
wenzelm@6728
   240
val add_defsort_i = Theory.add_defsort_i o Comment.ignore;
wenzelm@6728
   241
val add_nonterminals = Theory.add_nonterminals o map Comment.ignore;
wenzelm@6728
   242
val add_tyabbrs = Theory.add_tyabbrs o map Comment.ignore;
wenzelm@6728
   243
val add_tyabbrs_i = Theory.add_tyabbrs_i o map Comment.ignore;
wenzelm@6728
   244
val add_arities = Theory.add_arities o map Comment.ignore;
wenzelm@6728
   245
val add_arities_i = Theory.add_arities_i o map Comment.ignore;
wenzelm@6728
   246
val add_typedecl = PureThy.add_typedecls o single o Comment.ignore;
wenzelm@6728
   247
val add_consts = Theory.add_consts o map Comment.ignore;
wenzelm@6728
   248
val add_consts_i = Theory.add_consts_i o map Comment.ignore;
wenzelm@6728
   249
fun add_modesyntax mode = Theory.add_modesyntax mode o map Comment.ignore;
wenzelm@6728
   250
fun add_modesyntax_i mode = Theory.add_modesyntax_i mode o map Comment.ignore;
wenzelm@6728
   251
val add_trrules = Theory.add_trrules o map Comment.ignore;
wenzelm@6728
   252
val add_trrules_i = Theory.add_trrules_i o map Comment.ignore;
wenzelm@8227
   253
val add_judgment = AutoBind.add_judgment o Comment.ignore;
wenzelm@8227
   254
val add_judgment_i = AutoBind.add_judgment_i o Comment.ignore;
wenzelm@6728
   255
wenzelm@6728
   256
wenzelm@5830
   257
(* axioms and defs *)
wenzelm@5830
   258
wenzelm@5915
   259
fun add_axms f args thy =
wenzelm@5915
   260
  f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
wenzelm@5915
   261
wenzelm@8428
   262
val add_axioms = add_axms (#1 oo PureThy.add_axioms) o map Comment.ignore;
wenzelm@8428
   263
val add_axioms_i = (#1 oo PureThy.add_axioms_i) o map Comment.ignore;
wenzelm@8428
   264
val add_defs = add_axms (#1 oo PureThy.add_defs) o map Comment.ignore;
wenzelm@8428
   265
val add_defs_i = (#1 oo PureThy.add_defs_i) o map Comment.ignore;
wenzelm@6371
   266
wenzelm@6371
   267
wenzelm@6371
   268
(* constdefs *)
wenzelm@6371
   269
wenzelm@6371
   270
fun gen_add_constdefs consts defs args thy =
wenzelm@6371
   271
  thy
wenzelm@6728
   272
  |> consts (map (Comment.ignore o fst) args)
wenzelm@6728
   273
  |> defs (map (fn (((c, _, mx), _), (s, _)) =>
wenzelm@6728
   274
    (((Thm.def_name (Syntax.const_name c mx), s), []), Comment.none)) args);
wenzelm@6371
   275
wenzelm@6728
   276
fun add_constdefs args = gen_add_constdefs Theory.add_consts add_defs args;
wenzelm@6728
   277
fun add_constdefs_i args = gen_add_constdefs Theory.add_consts_i add_defs_i args;
wenzelm@5915
   278
wenzelm@5915
   279
wenzelm@5915
   280
(* theorems *)
wenzelm@5915
   281
wenzelm@5915
   282
fun gen_have_thmss get attrib f ((name, more_srcs), th_srcs) x =
wenzelm@5915
   283
  f name (map (attrib x) more_srcs)
wenzelm@5915
   284
    (map (fn (s, srcs) => (get x s, map (attrib x) srcs)) th_srcs) x;
wenzelm@5915
   285
wenzelm@7572
   286
fun global_have_thmss kind f (x as ((name, _), _)) thy =
wenzelm@7572
   287
  let val (thy', thms) = gen_have_thmss PureThy.get_thms Attrib.global_attribute f x thy in
wenzelm@7572
   288
    if kind <> "" then Context.setmp (Some thy') (Present.results kind name) thms else ();
wenzelm@7572
   289
    (thy', thms)
wenzelm@7572
   290
  end;
wenzelm@5915
   291
wenzelm@6371
   292
fun local_have_thmss x =
wenzelm@6371
   293
  gen_have_thmss (ProofContext.get_thms o Proof.context_of)
wenzelm@6371
   294
    (Attrib.local_attribute o Proof.theory_of) x;
wenzelm@6371
   295
wenzelm@7012
   296
fun gen_have_thmss_i f ((name, more_atts), th_atts) =
wenzelm@6371
   297
  f name more_atts (map (apfst single) th_atts);
wenzelm@6371
   298
wenzelm@6371
   299
fun have_lemss name atts = PureThy.have_thmss name (atts @ [Drule.tag_lemma]);
wenzelm@5915
   300
wenzelm@5915
   301
wenzelm@7572
   302
fun apply_theorems th_srcs = global_have_thmss "" PureThy.have_thmss (("", []), th_srcs);
wenzelm@7476
   303
fun apply_theorems_i th_srcs = gen_have_thmss_i PureThy.have_thmss (("", []), th_srcs);
wenzelm@7572
   304
val have_theorems = #1 oo (global_have_thmss "theorems" PureThy.have_thmss o Comment.ignore);
wenzelm@7476
   305
val have_theorems_i = #1 oo (gen_have_thmss_i PureThy.have_thmss o Comment.ignore);
wenzelm@7572
   306
val have_lemmas = #1 oo (global_have_thmss "lemmas" have_lemss o Comment.ignore);
wenzelm@7476
   307
val have_lemmas_i = #1 oo (gen_have_thmss_i have_lemss o Comment.ignore);
wenzelm@6879
   308
val have_facts = ProofHistory.apply o local_have_thmss (Proof.have_thmss []) o Comment.ignore;
wenzelm@7012
   309
val have_facts_i = ProofHistory.apply o gen_have_thmss_i (Proof.have_thmss []) o Comment.ignore;
wenzelm@6371
   310
wenzelm@5915
   311
wenzelm@6371
   312
(* forward chaining *)
wenzelm@6371
   313
wenzelm@6879
   314
fun gen_from_facts f = ProofHistory.apply o (Proof.chain oo curry f ("", []));
wenzelm@6879
   315
wenzelm@6879
   316
fun add_thmss name atts ths_atts state =
wenzelm@6879
   317
  Proof.have_thmss (Proof.the_facts state) name atts ths_atts state;
wenzelm@6371
   318
wenzelm@6879
   319
val from_facts = gen_from_facts (local_have_thmss (Proof.have_thmss [])) o Comment.ignore;
wenzelm@7012
   320
val from_facts_i = gen_from_facts (gen_have_thmss_i (Proof.have_thmss [])) o Comment.ignore;
wenzelm@6879
   321
val with_facts = gen_from_facts (local_have_thmss add_thmss) o Comment.ignore;
wenzelm@7012
   322
val with_facts_i = gen_from_facts (gen_have_thmss_i add_thmss) o Comment.ignore;
wenzelm@6879
   323
wenzelm@6728
   324
fun chain comment_ignore = ProofHistory.apply Proof.chain;
wenzelm@5830
   325
wenzelm@5830
   326
wenzelm@5830
   327
(* context *)
wenzelm@5830
   328
wenzelm@7417
   329
val fix = ProofHistory.apply o Proof.fix o map Comment.ignore;
wenzelm@7417
   330
val fix_i = ProofHistory.apply o Proof.fix_i o map Comment.ignore;
wenzelm@8615
   331
val let_bind = ProofHistory.apply o Proof.let_bind o map Comment.ignore;
wenzelm@8615
   332
val let_bind_i = ProofHistory.apply o Proof.let_bind_i o map Comment.ignore;
wenzelm@8450
   333
wenzelm@8450
   334
fun invoke_case ((name, src), comment_ignore) = ProofHistory.apply (fn state =>
wenzelm@8450
   335
  Proof.invoke_case (name, map (Attrib.local_attribute (Proof.theory_of state)) src) state);
wenzelm@8450
   336
val invoke_case_i = ProofHistory.apply o Proof.invoke_case o Comment.ignore;
wenzelm@5830
   337
wenzelm@5830
   338
wenzelm@5830
   339
(* statements *)
wenzelm@5830
   340
wenzelm@7271
   341
local
wenzelm@7271
   342
wenzelm@6728
   343
fun global_statement f (name, src, s) int thy =
wenzelm@6688
   344
  ProofHistory.init (Toplevel.undo_limit int)
wenzelm@6688
   345
    (f name (map (Attrib.global_attribute thy) src) s thy);
wenzelm@5830
   346
wenzelm@6728
   347
fun global_statement_i f (name, atts, t) int thy =
wenzelm@6688
   348
  ProofHistory.init (Toplevel.undo_limit int) (f name atts t thy);
wenzelm@6501
   349
wenzelm@6904
   350
fun local_statement f g (name, src, s) = ProofHistory.apply (fn state =>
wenzelm@6501
   351
  f name (map (Attrib.local_attribute (Proof.theory_of state)) src) s (g state));
wenzelm@6371
   352
wenzelm@6904
   353
fun local_statement_i f g (name, atts, t) = ProofHistory.apply (f name atts t o g);
wenzelm@5830
   354
wenzelm@8090
   355
fun multi_local_attribute state (name, src, s) =
wenzelm@8090
   356
  (name, map (Attrib.local_attribute (Proof.theory_of state)) src, s);
wenzelm@8090
   357
wenzelm@7271
   358
fun multi_statement f args = ProofHistory.apply (fn state =>
wenzelm@7678
   359
  f (map (multi_local_attribute state) args) state);
wenzelm@7271
   360
wenzelm@7271
   361
fun multi_statement_i f args = ProofHistory.apply (f args);
wenzelm@7271
   362
wenzelm@7271
   363
in
wenzelm@7271
   364
wenzelm@6952
   365
val theorem     = global_statement Proof.theorem o Comment.ignore;
wenzelm@6952
   366
val theorem_i   = global_statement_i Proof.theorem_i o Comment.ignore;
wenzelm@6952
   367
val lemma       = global_statement Proof.lemma o Comment.ignore;
wenzelm@6952
   368
val lemma_i     = global_statement_i Proof.lemma_i o Comment.ignore;
wenzelm@7271
   369
val assume      = multi_statement Proof.assume o map Comment.ignore;
wenzelm@7271
   370
val assume_i    = multi_statement_i Proof.assume_i o map Comment.ignore;
wenzelm@7271
   371
val presume     = multi_statement Proof.presume o map Comment.ignore;
wenzelm@7271
   372
val presume_i   = multi_statement_i Proof.presume_i o map Comment.ignore;
wenzelm@6952
   373
val local_def   = local_statement LocalDefs.def I o Comment.ignore;
wenzelm@6952
   374
val local_def_i = local_statement LocalDefs.def_i I o Comment.ignore;
wenzelm@7176
   375
val show        = local_statement (Proof.show Seq.single) I o Comment.ignore;
wenzelm@7176
   376
val show_i      = local_statement_i (Proof.show_i Seq.single) I o Comment.ignore;
wenzelm@7176
   377
val have        = local_statement (Proof.have Seq.single) I o Comment.ignore;
wenzelm@7176
   378
val have_i      = local_statement_i (Proof.have_i Seq.single) I o Comment.ignore;
wenzelm@7176
   379
val thus        = local_statement (Proof.show Seq.single) Proof.chain o Comment.ignore;
wenzelm@7176
   380
val thus_i      = local_statement_i (Proof.show_i Seq.single) Proof.chain o Comment.ignore;
wenzelm@7176
   381
val hence       = local_statement (Proof.have Seq.single) Proof.chain o Comment.ignore;
wenzelm@7176
   382
val hence_i     = local_statement_i (Proof.have_i Seq.single) Proof.chain o Comment.ignore;
wenzelm@5830
   383
wenzelm@7271
   384
end;
wenzelm@7271
   385
wenzelm@5830
   386
wenzelm@5830
   387
(* blocks *)
wenzelm@5830
   388
wenzelm@6904
   389
val begin_block = ProofHistory.apply Proof.begin_block;
wenzelm@5830
   390
val next_block = ProofHistory.apply Proof.next_block;
wenzelm@6937
   391
val end_block = ProofHistory.applys Proof.end_block;
wenzelm@5830
   392
wenzelm@5830
   393
wenzelm@8165
   394
(* shuffle state *)
wenzelm@8165
   395
wenzelm@8236
   396
fun shuffle meth i = Method.refine (Method.Basic (K (meth i))) o Proof.assert_no_chain;
wenzelm@8204
   397
wenzelm@8681
   398
fun defer (i, comment_ignore) = ProofHistory.applys (shuffle Method.defer i);
wenzelm@8681
   399
fun prefer (i, comment_ignore) = ProofHistory.applys (shuffle Method.prefer i);
wenzelm@8165
   400
wenzelm@8165
   401
wenzelm@5830
   402
(* backward steps *)
wenzelm@5830
   403
wenzelm@8881
   404
fun apply (m, comment_ignore) = ProofHistory.applys
wenzelm@8881
   405
  (Seq.map (Proof.goal_facts (K [])) o Method.refine m o Proof.assert_backward);
wenzelm@8881
   406
wenzelm@8881
   407
fun apply_end (m, comment_ignore) = ProofHistory.applys
wenzelm@8881
   408
  (Method.refine_end m o Proof.assert_forward);
wenzelm@6728
   409
wenzelm@7002
   410
val proof = ProofHistory.applys o Method.proof o
wenzelm@7002
   411
  apsome Comment.ignore_interest o Comment.ignore_interest' o Comment.ignore;
wenzelm@6404
   412
wenzelm@6404
   413
wenzelm@6732
   414
(* print result *)
wenzelm@6732
   415
wenzelm@7572
   416
local
wenzelm@7572
   417
wenzelm@6732
   418
fun pretty_result {kind, name, thm} =
wenzelm@7590
   419
  Pretty.block [Pretty.str (kind ^ (if name = "" then "" else " " ^ name) ^ ":"),
wenzelm@7572
   420
    Pretty.fbrk, Proof.pretty_thm thm];
wenzelm@6986
   421
wenzelm@6986
   422
fun pretty_rule thm =
wenzelm@7353
   423
  Pretty.block [Pretty.str "trying to solve goal by rule:", Pretty.fbrk, Proof.pretty_thm thm];
wenzelm@6732
   424
wenzelm@7572
   425
in
wenzelm@7572
   426
wenzelm@6732
   427
val print_result = Pretty.writeln o pretty_result;
wenzelm@6732
   428
wenzelm@6986
   429
fun cond_print_result_rule int =
wenzelm@6986
   430
  if int then (print_result, Pretty.writeln o pretty_rule)
wenzelm@6986
   431
  else (K (), K ());
wenzelm@6986
   432
wenzelm@6986
   433
fun proof'' f = Toplevel.proof' (f o cond_print_result_rule);
wenzelm@6732
   434
wenzelm@7572
   435
end;
wenzelm@7572
   436
wenzelm@6732
   437
wenzelm@6404
   438
(* local endings *)
wenzelm@6404
   439
wenzelm@6728
   440
val local_qed =
wenzelm@6904
   441
  proof'' o (ProofHistory.applys oo Method.local_qed) o apsome Comment.ignore_interest;
wenzelm@6728
   442
wenzelm@6937
   443
fun ignore_interests (x, y) = (Comment.ignore_interest x, apsome Comment.ignore_interest y);
wenzelm@6937
   444
wenzelm@6688
   445
val local_terminal_proof =
wenzelm@6937
   446
  proof'' o (ProofHistory.applys oo Method.local_terminal_proof) o ignore_interests;
wenzelm@6728
   447
wenzelm@6904
   448
val local_immediate_proof = proof'' (ProofHistory.applys o Method.local_immediate_proof);
wenzelm@6904
   449
val local_default_proof = proof'' (ProofHistory.applys o Method.local_default_proof);
wenzelm@6904
   450
val local_skip_proof = proof'' (ProofHistory.applys o SkipProof.local_skip_proof);
wenzelm@6404
   451
wenzelm@6404
   452
wenzelm@6404
   453
(* global endings *)
wenzelm@6404
   454
wenzelm@6404
   455
fun global_result finish = Toplevel.proof_to_theory (fn prf =>
wenzelm@6404
   456
  let
wenzelm@6404
   457
    val state = ProofHistory.current prf;
wenzelm@6404
   458
    val _ = if Proof.at_bottom state then () else raise Toplevel.UNDEF;
wenzelm@7572
   459
    val (thy, res as {kind, name, thm}) = finish state;
wenzelm@7572
   460
  in print_result res; Context.setmp (Some thy) (Present.result kind name) thm; thy end);
wenzelm@6404
   461
wenzelm@6952
   462
val global_qed = global_result o Method.global_qed o apsome Comment.ignore_interest;
wenzelm@6937
   463
val global_terminal_proof = global_result o Method.global_terminal_proof o ignore_interests;
wenzelm@6404
   464
val global_immediate_proof = global_result Method.global_immediate_proof;
wenzelm@6404
   465
val global_default_proof = global_result Method.global_default_proof;
wenzelm@6888
   466
val global_skip_proof = global_result SkipProof.global_skip_proof;
wenzelm@6404
   467
wenzelm@6404
   468
wenzelm@6404
   469
(* common endings *)
wenzelm@6404
   470
wenzelm@7002
   471
fun qed (meth, comment) = local_qed meth o global_qed meth;
wenzelm@7002
   472
fun terminal_proof (meths, comment) = local_terminal_proof meths o global_terminal_proof meths;
wenzelm@7002
   473
fun immediate_proof comment = local_immediate_proof o global_immediate_proof;
wenzelm@7002
   474
fun default_proof comment = local_default_proof o global_default_proof;
wenzelm@7002
   475
fun skip_proof comment = local_skip_proof o global_skip_proof;
wenzelm@5830
   476
wenzelm@8210
   477
fun forget_proof comment = Toplevel.proof_to_theory (Proof.theory_of o ProofHistory.current);
wenzelm@8210
   478
wenzelm@5830
   479
wenzelm@6774
   480
(* calculational proof commands *)
wenzelm@6774
   481
wenzelm@6879
   482
local
wenzelm@6879
   483
wenzelm@7417
   484
fun cond_print_calc int thms =
wenzelm@7417
   485
  if int then Pretty.writeln (Pretty.big_list "calculation:" (map Proof.pretty_thm thms))
wenzelm@6774
   486
  else ();
wenzelm@6774
   487
wenzelm@6774
   488
fun proof''' f = Toplevel.proof' (f o cond_print_calc);
wenzelm@6774
   489
wenzelm@6879
   490
fun gen_calc get f (args, _) prt state =
wenzelm@6879
   491
  f (apsome (fn (srcs, _) => get srcs state) args) prt state;
wenzelm@6879
   492
wenzelm@6879
   493
in
wenzelm@6879
   494
wenzelm@7012
   495
fun get_thmss srcs = Proof.the_facts o local_have_thmss (Proof.have_thmss []) (("", []), srcs); 
wenzelm@7012
   496
fun get_thmss_i thms _ = thms;
wenzelm@7012
   497
wenzelm@6879
   498
fun also x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.also x);
wenzelm@6879
   499
fun also_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.also x);
wenzelm@6879
   500
fun finally x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.finally x);
wenzelm@6879
   501
fun finally_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.finally x);
wenzelm@8562
   502
fun moreover comment = proof''' (ProofHistory.apply o Calculation.moreover);
wenzelm@8588
   503
fun ultimately comment = proof''' (ProofHistory.apply o Calculation.ultimately);
wenzelm@6879
   504
wenzelm@6879
   505
end;
wenzelm@6774
   506
wenzelm@6774
   507
wenzelm@5830
   508
(* translation functions *)
wenzelm@5830
   509
wenzelm@5830
   510
val parse_ast_translation =
wenzelm@8349
   511
  Context.use_let "parse_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   512
    "Theory.add_trfuns (parse_ast_translation, [], [], [])";
wenzelm@5830
   513
wenzelm@5830
   514
val parse_translation =
wenzelm@8349
   515
  Context.use_let "parse_translation: (string * (term list -> term)) list"
wenzelm@5830
   516
    "Theory.add_trfuns ([], parse_translation, [], [])";
wenzelm@5830
   517
wenzelm@5830
   518
val print_translation =
wenzelm@8349
   519
  Context.use_let "print_translation: (string * (term list -> term)) list"
wenzelm@5830
   520
    "Theory.add_trfuns ([], [], print_translation, [])";
wenzelm@5830
   521
wenzelm@5830
   522
val print_ast_translation =
wenzelm@8349
   523
  Context.use_let "print_ast_translation: (string * (Syntax.ast list -> Syntax.ast)) list"
wenzelm@5830
   524
    "Theory.add_trfuns ([], [], [], print_ast_translation)";
wenzelm@5830
   525
wenzelm@5830
   526
val typed_print_translation =
wenzelm@8349
   527
  Context.use_let "typed_print_translation: (string * (bool -> typ -> term list -> term)) list"
wenzelm@5830
   528
    "Theory.add_trfunsT typed_print_translation";
wenzelm@5830
   529
wenzelm@5830
   530
val token_translation =
wenzelm@8349
   531
  Context.use_let "token_translation: (string * string * (string -> string * int)) list"
wenzelm@5830
   532
    "Theory.add_tokentrfuns token_translation";
wenzelm@5830
   533
wenzelm@5830
   534
wenzelm@5830
   535
(* add_oracle *)
wenzelm@5830
   536
wenzelm@6728
   537
fun add_oracle ((name, txt), comment_ignore) =
wenzelm@8349
   538
  Context.use_let
wenzelm@5830
   539
    "oracle: bstring * (Sign.sg * Object.T -> term)"
wenzelm@5830
   540
    "Theory.add_oracle oracle"
wenzelm@5830
   541
    ("(" ^ quote name ^ ", " ^ txt ^ ")");
wenzelm@5830
   542
wenzelm@5830
   543
wenzelm@6688
   544
(* theory init and exit *)
wenzelm@5830
   545
wenzelm@8804
   546
fun gen_begin_theory upd name parents files =
wenzelm@8804
   547
  ThyInfo.begin_theory Present.begin_theory upd name parents (map (apfst Path.unpack) files);
wenzelm@5830
   548
wenzelm@8804
   549
val begin_theory = gen_begin_theory false;
wenzelm@7909
   550
wenzelm@7909
   551
fun end_theory thy =
wenzelm@7932
   552
  if ThyInfo.check_known_thy (PureThy.get_name thy) then ThyInfo.end_theory thy
wenzelm@7909
   553
  else thy;
wenzelm@7909
   554
wenzelm@7932
   555
val kill_theory = ThyInfo.if_known_thy ThyInfo.remove_thy;
wenzelm@6688
   556
wenzelm@7103
   557
wenzelm@8804
   558
fun bg_theory (name, parents, files) int = gen_begin_theory int name parents files;
wenzelm@6331
   559
fun en_theory thy = (end_theory thy; ());
wenzelm@6331
   560
wenzelm@7932
   561
fun theory spec = Toplevel.init_theory (bg_theory spec) en_theory (kill_theory o PureThy.get_name);
wenzelm@6246
   562
wenzelm@6246
   563
wenzelm@6246
   564
(* context switch *)
wenzelm@6246
   565
wenzelm@7960
   566
fun fetch_context f x =
wenzelm@7960
   567
  (case Context.pass None f x of
wenzelm@7960
   568
    ((), None) => raise Toplevel.UNDEF
wenzelm@7960
   569
  | ((), Some thy) => thy);
wenzelm@7960
   570
wenzelm@7960
   571
fun init_context f x = Toplevel.init_theory (fn _ => fetch_context f x) (K ()) (K ());
wenzelm@7953
   572
wenzelm@7953
   573
val context = init_context (ThyInfo.quiet_update_thy true);
wenzelm@6246
   574
wenzelm@5830
   575
wenzelm@5830
   576
end;