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