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