src/Pure/Isar/isar_thy.ML
author ballarin
Thu Mar 24 17:03:37 2005 +0100 (2005-03-24 ago)
changeset 15624 484178635bd8
parent 15598 4ab52355bb53
child 15696 1da4ce092c0b
permissions -rw-r--r--
Further work on interpretation commands. New command `interpret' for
interpretation in proof contexts.
wenzelm@5830
     1
(*  Title:      Pure/Isar/isar_thy.ML
wenzelm@5830
     2
    ID:         $Id$
wenzelm@5830
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5830
     4
wenzelm@6371
     5
Pure/Isar derived theory operations.
wenzelm@5830
     6
*)
wenzelm@5830
     7
wenzelm@5830
     8
signature ISAR_THY =
wenzelm@5830
     9
sig
wenzelm@12876
    10
  val hide_space: string * xstring list -> theory -> theory
wenzelm@12876
    11
  val hide_space_i: string * string list -> theory -> theory
wenzelm@12876
    12
  val add_axioms: ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@12876
    13
  val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory
wenzelm@12876
    14
  val add_defs: bool * ((bstring * string) * Args.src list) list -> theory -> theory
wenzelm@12876
    15
  val add_defs_i: bool * ((bstring * term) * theory attribute list) list -> theory -> theory
berghofe@15456
    16
  val theorems: string -> ((bstring * Args.src list) * (thmref * Args.src list) list) list
wenzelm@12713
    17
    -> theory -> theory * (string * thm list) list
wenzelm@12713
    18
  val theorems_i: string ->
wenzelm@12876
    19
    ((bstring * theory attribute list) * (thm list * theory attribute list) list) list
wenzelm@12713
    20
    -> theory -> theory * (string * thm list) list
wenzelm@12713
    21
  val locale_theorems: string -> xstring ->
berghofe@15456
    22
    ((bstring * Args.src list) * (thmref * Args.src list) list) list
wenzelm@12713
    23
    -> theory -> theory * (bstring * thm list) list
wenzelm@12713
    24
  val locale_theorems_i: string -> string ->
ballarin@15596
    25
    ((bstring * Locale.multi_attribute list)
ballarin@15596
    26
      * (thm list * Locale.multi_attribute list) list) list
wenzelm@12713
    27
    -> theory -> theory * (string * thm list) list
wenzelm@12713
    28
  val smart_theorems: string -> xstring option ->
berghofe@15456
    29
    ((bstring * Args.src list) * (thmref * Args.src list) list) list
wenzelm@12713
    30
    -> theory -> theory * (string * thm list) list
berghofe@15456
    31
  val declare_theorems: xstring option -> (thmref * Args.src list) list -> theory -> theory
berghofe@15456
    32
  val apply_theorems: (thmref * Args.src list) list -> theory -> theory * thm list
wenzelm@12713
    33
  val apply_theorems_i: (thm list * theory attribute list) list -> theory -> theory * thm list
berghofe@15456
    34
  val have_facts: ((string * Args.src list) * (thmref * Args.src list) list) list
wenzelm@10464
    35
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    36
  val have_facts_i: ((string * Proof.context attribute list) *
wenzelm@12876
    37
    (thm * Proof.context attribute list) list) list -> ProofHistory.T -> ProofHistory.T
berghofe@15456
    38
  val from_facts: (thmref * Args.src list) list list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    39
  val from_facts_i: (thm * Proof.context attribute list) list list
wenzelm@6728
    40
    -> ProofHistory.T -> ProofHistory.T
berghofe@15456
    41
  val with_facts: (thmref * Args.src list) list list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    42
  val with_facts_i: (thm * Proof.context attribute list) list list
wenzelm@6879
    43
    -> ProofHistory.T -> ProofHistory.T
berghofe@15456
    44
  val using_facts: (thmref * Args.src list) list list -> ProofHistory.T -> ProofHistory.T
wenzelm@12929
    45
  val using_facts_i: (thm * Proof.context attribute list) list list
wenzelm@12929
    46
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    47
  val chain: ProofHistory.T -> ProofHistory.T
ballarin@14528
    48
  val instantiate_locale: (string * Args.src list) * string -> ProofHistory.T
ballarin@14528
    49
    -> ProofHistory.T
wenzelm@12876
    50
  val fix: (string list * string option) list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    51
  val fix_i: (string list * typ option) list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    52
  val let_bind: (string list * string) list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    53
  val let_bind_i: (term list * term) list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    54
  val invoke_case: string * string option list * Args.src list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    55
  val invoke_case_i: string * string option list * Proof.context attribute list
wenzelm@11793
    56
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    57
  val theorem: string -> (bstring * Args.src list) * (string * (string list * string list))
wenzelm@12141
    58
    -> bool -> theory -> ProofHistory.T
wenzelm@12876
    59
  val theorem_i: string -> (bstring * theory attribute list) *
wenzelm@12876
    60
    (term * (term list * term list)) -> bool -> theory -> ProofHistory.T
ballarin@15596
    61
  val multi_theorem: string -> (theory -> theory) -> bstring * Args.src list
ballarin@15206
    62
    -> Args.src Locale.element Locale.elem_expr list
wenzelm@12957
    63
    -> ((bstring * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12876
    64
    -> bool -> theory -> ProofHistory.T
ballarin@15596
    65
  val multi_theorem_i: string -> (theory -> theory) -> bstring * theory attribute list
ballarin@15596
    66
    -> Locale.multi_attribute Locale.element_i Locale.elem_expr list
wenzelm@12957
    67
    -> ((bstring * theory attribute list) * (term * (term list * term list)) list) list
wenzelm@12876
    68
    -> bool -> theory -> ProofHistory.T
wenzelm@12957
    69
  val locale_multi_theorem: string -> xstring
ballarin@15206
    70
    -> bstring * Args.src list -> Args.src Locale.element Locale.elem_expr list
wenzelm@12957
    71
    -> ((bstring * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12876
    72
    -> bool -> theory -> ProofHistory.T
ballarin@15596
    73
  val locale_multi_theorem_i: string -> string -> bstring * Locale.multi_attribute list
ballarin@15596
    74
    -> Locale.multi_attribute Locale.element_i Locale.elem_expr list
ballarin@15596
    75
    -> ((bstring * Locale.multi_attribute list) * (term * (term list * term list)) list) list
wenzelm@12876
    76
    -> bool -> theory -> ProofHistory.T
skalberg@15531
    77
  val smart_multi_theorem: string -> xstring option
ballarin@15206
    78
    -> bstring * Args.src list -> Args.src Locale.element Locale.elem_expr list
wenzelm@12957
    79
    -> ((bstring * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12876
    80
    -> bool -> theory -> ProofHistory.T
wenzelm@12876
    81
  val assume: ((string * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12141
    82
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    83
  val assume_i:
wenzelm@12876
    84
    ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
wenzelm@12876
    85
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    86
  val presume: ((string * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12876
    87
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    88
  val presume_i:
wenzelm@12876
    89
    ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
wenzelm@12141
    90
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    91
  val have: ((string * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12141
    92
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    93
  val have_i: ((string * Proof.context attribute list) *
wenzelm@12876
    94
    (term * (term list * term list)) list) list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    95
  val hence: ((string * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12141
    96
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    97
  val hence_i: ((string * Proof.context attribute list) *
wenzelm@12876
    98
    (term * (term list * term list)) list) list -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
    99
  val show: ((string * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12141
   100
    -> bool -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   101
  val show_i: ((string * Proof.context attribute list) *
wenzelm@12876
   102
    (term * (term list * term list)) list) list -> bool -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   103
  val thus: ((string * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12141
   104
    -> bool -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   105
  val thus_i: ((string * Proof.context attribute list)
wenzelm@12876
   106
    * (term * (term list * term list)) list) list -> bool -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   107
  val local_def: (string * Args.src list) * (string * (string * string list))
wenzelm@12876
   108
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   109
  val local_def_i: (string * Args.src list) * (string * (term * term list))
wenzelm@12876
   110
    -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   111
  val obtain: (string list * string option) list
wenzelm@12876
   112
    * ((string * Args.src list) * (string * (string list * string list)) list) list
wenzelm@12969
   113
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
   114
  val obtain_i: (string list * typ option) list
wenzelm@12876
   115
    * ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
wenzelm@12969
   116
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
   117
  val begin_block: ProofHistory.T -> ProofHistory.T
wenzelm@12876
   118
  val next_block: ProofHistory.T -> ProofHistory.T
wenzelm@12876
   119
  val end_block: ProofHistory.T -> ProofHistory.T
wenzelm@12876
   120
  val defer: int option -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   121
  val prefer: int -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   122
  val apply: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   123
  val apply_end: Method.text -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   124
  val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
wenzelm@12876
   125
  val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
   126
  val terminal_proof: Method.text * Method.text option
wenzelm@6937
   127
    -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
   128
  val default_proof: Toplevel.transition -> Toplevel.transition
wenzelm@12876
   129
  val immediate_proof: Toplevel.transition -> Toplevel.transition
wenzelm@12876
   130
  val done_proof: Toplevel.transition -> Toplevel.transition
wenzelm@12876
   131
  val skip_proof: Toplevel.transition -> Toplevel.transition
wenzelm@12876
   132
  val forget_proof: Toplevel.transition -> Toplevel.transition
berghofe@15456
   133
  val get_thmss: (thmref * Args.src list) list -> Proof.state -> thm list
berghofe@15456
   134
  val also: (thmref * Args.src list) list option -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
   135
  val also_i: thm list option -> Toplevel.transition -> Toplevel.transition
berghofe@15456
   136
  val finally: (thmref * Args.src list) list option -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
   137
  val finally_i: thm list option -> Toplevel.transition -> Toplevel.transition
wenzelm@12876
   138
  val moreover: Toplevel.transition -> Toplevel.transition
wenzelm@12876
   139
  val ultimately: Toplevel.transition -> Toplevel.transition
wenzelm@14649
   140
  val parse_ast_translation: bool * string -> theory -> theory
wenzelm@14649
   141
  val parse_translation: bool * string -> theory -> theory
wenzelm@14649
   142
  val print_translation: bool * string -> theory -> theory
wenzelm@14649
   143
  val typed_print_translation: bool * string -> theory -> theory
wenzelm@14649
   144
  val print_ast_translation: bool * string -> theory -> theory
wenzelm@12876
   145
  val token_translation: string -> theory -> theory
wenzelm@12876
   146
  val method_setup: bstring * string * string -> theory -> theory
wenzelm@12876
   147
  val add_oracle: bstring * string -> theory -> theory
wenzelm@13393
   148
  val add_locale: bool * bstring * Locale.expr * Args.src Locale.element list -> theory -> theory
wenzelm@6331
   149
  val begin_theory: string -> string list -> (string * bool) list -> theory
wenzelm@6331
   150
  val end_theory: theory -> theory
wenzelm@7932
   151
  val kill_theory: string -> unit
wenzelm@6246
   152
  val theory: string * string list * (string * bool) list
wenzelm@6246
   153
    -> Toplevel.transition -> Toplevel.transition
wenzelm@7960
   154
  val init_context: ('a -> unit) -> 'a -> Toplevel.transition -> Toplevel.transition
wenzelm@6246
   155
  val context: string -> Toplevel.transition -> Toplevel.transition
ballarin@15596
   156
ballarin@15596
   157
  val register_globally:
ballarin@15596
   158
    ((string * Args.src list) * Locale.expr) * string option list ->
ballarin@15596
   159
    bool -> theory -> ProofHistory.T
ballarin@15624
   160
  val register_locally:
ballarin@15624
   161
    ((string * Args.src list) * Locale.expr) * string option list ->
ballarin@15624
   162
    ProofHistory.T -> ProofHistory.T
ballarin@15596
   163
wenzelm@5830
   164
end;
wenzelm@5830
   165
wenzelm@5830
   166
structure IsarThy: ISAR_THY =
wenzelm@5830
   167
struct
wenzelm@5830
   168
wenzelm@5830
   169
(** derived theory and proof operations **)
wenzelm@5830
   170
wenzelm@8724
   171
(* name spaces *)
wenzelm@8724
   172
wenzelm@8885
   173
local
wenzelm@8885
   174
wenzelm@8885
   175
val kinds =
wenzelm@10449
   176
 [(Sign.classK, can o Sign.certify_class),
wenzelm@14778
   177
  (Sign.typeK, can o Sign.certify_tyname),
wenzelm@10449
   178
  (Sign.constK, can o Sign.certify_const)];
wenzelm@8724
   179
wenzelm@12876
   180
fun gen_hide intern (kind, xnames) thy =
wenzelm@8885
   181
  (case assoc (kinds, kind) of
skalberg@15531
   182
    SOME check =>
wenzelm@8885
   183
      let
wenzelm@8885
   184
        val sg = Theory.sign_of thy;
wenzelm@8885
   185
        val names = map (intern sg kind) xnames;
wenzelm@8885
   186
        val bads = filter_out (check sg) names;
wenzelm@8885
   187
      in
wenzelm@12589
   188
        if null bads then Theory.hide_space_i true (kind, names) thy
wenzelm@8885
   189
        else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
wenzelm@8885
   190
      end
skalberg@15531
   191
  | NONE => error ("Bad name space specification: " ^ quote kind));
wenzelm@8885
   192
wenzelm@8885
   193
in
wenzelm@8885
   194
wenzelm@8885
   195
fun hide_space x = gen_hide Sign.intern x;
wenzelm@8885
   196
fun hide_space_i x = gen_hide (K (K I)) x;
wenzelm@8885
   197
wenzelm@8885
   198
end;
wenzelm@8724
   199
wenzelm@8724
   200
wenzelm@5830
   201
(* axioms and defs *)
wenzelm@5830
   202
wenzelm@5915
   203
fun add_axms f args thy =
wenzelm@5915
   204
  f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
wenzelm@5915
   205
wenzelm@12876
   206
val add_axioms = add_axms (#1 oo PureThy.add_axioms);
wenzelm@12876
   207
val add_axioms_i = #1 oo PureThy.add_axioms_i;
wenzelm@12876
   208
fun add_defs (x, args) = add_axms (#1 oo PureThy.add_defs x) args;
wenzelm@12876
   209
fun add_defs_i (x, args) = (#1 oo PureThy.add_defs_i x) args;
wenzelm@6371
   210
wenzelm@6371
   211
wenzelm@12713
   212
(* attributes *)
wenzelm@12713
   213
wenzelm@12713
   214
local
wenzelm@12713
   215
wenzelm@12713
   216
fun prep_attribs f = map (fn ((name, more_srcs), th_srcs) =>
wenzelm@12713
   217
  ((name, map f more_srcs), map (apsnd (map f)) th_srcs));
wenzelm@12713
   218
wenzelm@12713
   219
in
wenzelm@12713
   220
wenzelm@12713
   221
fun global_attribs thy = prep_attribs (Attrib.global_attribute thy);
wenzelm@12713
   222
fun local_attribs thy = prep_attribs (Attrib.local_attribute thy);
ballarin@15596
   223
fun multi_attribs thy = prep_attribs (Attrib.multi_attribute thy);
wenzelm@12713
   224
wenzelm@12713
   225
end;
wenzelm@12713
   226
wenzelm@12713
   227
wenzelm@5915
   228
(* theorems *)
wenzelm@5915
   229
wenzelm@12705
   230
local
wenzelm@12705
   231
wenzelm@12713
   232
fun present_thmss kind (thy, named_thmss) =
wenzelm@13529
   233
 (conditional (kind <> "" andalso kind <> Drule.internalK) (fn () =>
skalberg@15531
   234
    Context.setmp (SOME thy) (Present.results (kind ^ "s")) named_thmss);
wenzelm@13529
   235
  (thy, named_thmss));
wenzelm@5915
   236
wenzelm@12705
   237
in
wenzelm@12705
   238
wenzelm@14564
   239
fun theorems_i k = present_thmss k oo PureThy.note_thmss_i (Drule.kind k);
wenzelm@14564
   240
fun locale_theorems_i k loc = present_thmss k oo Locale.note_thmss_i k loc;
wenzelm@5915
   241
wenzelm@12713
   242
fun theorems k args thy = thy
wenzelm@14564
   243
  |> PureThy.note_thmss (Drule.kind k) (global_attribs thy args)
wenzelm@12713
   244
  |> present_thmss k;
wenzelm@12713
   245
wenzelm@12713
   246
fun locale_theorems k loc args thy = thy
ballarin@15596
   247
  |> Locale.note_thmss k loc (multi_attribs thy args)
wenzelm@12713
   248
  |> present_thmss k;
wenzelm@6371
   249
wenzelm@12713
   250
fun smart_theorems k opt_loc args thy = thy
wenzelm@12713
   251
  |> (case opt_loc of
skalberg@15531
   252
    NONE => PureThy.note_thmss (Drule.kind k) (global_attribs thy args)
ballarin@15596
   253
  | SOME loc => Locale.note_thmss k loc (multi_attribs thy args))
wenzelm@12713
   254
  |> present_thmss k;
wenzelm@9590
   255
wenzelm@12876
   256
fun declare_theorems opt_loc args = #1 o smart_theorems "" opt_loc [(("", []), args)];
wenzelm@9590
   257
skalberg@15570
   258
fun apply_theorems args = apsnd (List.concat o map snd) o theorems "" [(("", []), args)];
skalberg@15570
   259
fun apply_theorems_i args = apsnd (List.concat o map snd) o theorems_i "" [(("", []), args)];
wenzelm@6371
   260
wenzelm@12705
   261
end;
wenzelm@12705
   262
wenzelm@5915
   263
wenzelm@12713
   264
(* facts and forward chaining *)
wenzelm@6371
   265
wenzelm@12713
   266
fun local_thmss f args state = f (local_attribs (Proof.theory_of state) args) state;
wenzelm@12713
   267
fun local_thmss_i f args = f (map (fn ((name, more_atts), th_atts) =>
wenzelm@12713
   268
  ((name, more_atts), map (apfst single) th_atts)) args);
wenzelm@6371
   269
wenzelm@12713
   270
fun chain_thmss f args = ProofHistory.apply (Proof.chain o f (map (pair ("", [])) args));
wenzelm@6879
   271
wenzelm@14564
   272
val have_facts = ProofHistory.apply o local_thmss Proof.note_thmss;
wenzelm@14564
   273
val have_facts_i = ProofHistory.apply o local_thmss_i Proof.note_thmss_i;
wenzelm@14564
   274
val from_facts = chain_thmss (local_thmss Proof.note_thmss);
wenzelm@14564
   275
val from_facts_i = chain_thmss (local_thmss_i Proof.note_thmss_i);
wenzelm@12876
   276
val with_facts = chain_thmss (local_thmss Proof.with_thmss);
wenzelm@12876
   277
val with_facts_i = chain_thmss (local_thmss_i Proof.with_thmss_i);
wenzelm@12929
   278
wenzelm@12929
   279
fun using_facts args = ProofHistory.apply (fn state =>
wenzelm@12929
   280
  Proof.using_thmss (map (map (apsnd (map
wenzelm@12929
   281
    (Attrib.local_attribute (Proof.theory_of state))))) args) state);
wenzelm@12929
   282
wenzelm@12929
   283
val using_facts_i = ProofHistory.apply o Proof.using_thmss_i o map (map (apfst single));
wenzelm@12929
   284
wenzelm@12876
   285
val chain = ProofHistory.apply Proof.chain;
wenzelm@5830
   286
wenzelm@14649
   287
ballarin@14508
   288
(* locale instantiation *)
ballarin@14508
   289
wenzelm@14649
   290
fun instantiate_locale ((name, atts), loc) =
ballarin@14528
   291
  ProofHistory.apply (fn state =>
wenzelm@14649
   292
    Proof.instantiate_locale (loc,
wenzelm@14649
   293
        (name, map (Attrib.local_attribute (Proof.theory_of state)) atts)) state);
wenzelm@14649
   294
wenzelm@5830
   295
wenzelm@5830
   296
(* context *)
wenzelm@5830
   297
wenzelm@12876
   298
val fix = ProofHistory.apply o Proof.fix;
wenzelm@12876
   299
val fix_i = ProofHistory.apply o Proof.fix_i;
wenzelm@12876
   300
val let_bind = ProofHistory.apply o Proof.let_bind;
wenzelm@12876
   301
val let_bind_i = ProofHistory.apply o Proof.let_bind_i;
wenzelm@8450
   302
wenzelm@12876
   303
fun invoke_case (name, xs, src) = ProofHistory.apply (fn state =>
wenzelm@14649
   304
  Proof.invoke_case (name, xs,
wenzelm@14649
   305
    map (Attrib.local_attribute (Proof.theory_of state)) src) state);
wenzelm@14649
   306
wenzelm@12876
   307
val invoke_case_i = ProofHistory.apply o Proof.invoke_case;
wenzelm@5830
   308
wenzelm@5830
   309
wenzelm@10935
   310
(* local results *)
wenzelm@10935
   311
wenzelm@10935
   312
local
wenzelm@10935
   313
wenzelm@12244
   314
fun prt_facts ctxt =
skalberg@15570
   315
  List.concat o (separate [Pretty.fbrk, Pretty.str "and "]) o
wenzelm@13276
   316
    map (single o ProofContext.pretty_fact ctxt);
wenzelm@12244
   317
wenzelm@12244
   318
fun pretty_results ctxt ((kind, ""), facts) =
wenzelm@12244
   319
      Pretty.block (Pretty.str kind :: Pretty.brk 1 :: prt_facts ctxt facts)
wenzelm@12244
   320
  | pretty_results ctxt ((kind, name), facts) = Pretty.blk (1,
wenzelm@12244
   321
      [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk,
wenzelm@12244
   322
        Pretty.blk (1, Pretty.str "(" :: prt_facts ctxt facts @ [Pretty.str ")"])]);
wenzelm@10935
   323
wenzelm@12055
   324
fun pretty_rule s ctxt thm =
wenzelm@10935
   325
  Pretty.block [Pretty.str (s ^ " to solve goal by exported rule:"),
wenzelm@12055
   326
    Pretty.fbrk, ProofContext.pretty_thm ctxt thm];
wenzelm@10935
   327
wenzelm@10935
   328
in
wenzelm@10935
   329
wenzelm@12244
   330
val print_result = Pretty.writeln oo pretty_results;
wenzelm@12244
   331
fun print_result' ctxt (k, res) = print_result ctxt ((k, ""), res);
wenzelm@10935
   332
nipkow@13686
   333
fun cond_print_result_rule int = if int
nipkow@13686
   334
  then (print_result',
nipkow@13686
   335
        priority oo (Pretty.string_of oo pretty_rule "Successful attempt"))
wenzelm@12055
   336
  else (K (K ()), K (K ()));
wenzelm@10935
   337
wenzelm@10935
   338
fun proof'' f = Toplevel.proof' (f o cond_print_result_rule);
wenzelm@10935
   339
wenzelm@10935
   340
fun check_goal false state = state
wenzelm@10935
   341
  | check_goal true state =
wenzelm@10935
   342
      let
skalberg@15531
   343
        val rule = ref (NONE: thm option);
wenzelm@13529
   344
        fun fail exn =
wenzelm@10935
   345
          (["Problem! Local statement will fail to solve any pending goal"] @
skalberg@15531
   346
          (case exn of NONE => [] | SOME e => [Toplevel.exn_message e]) @
skalberg@15531
   347
          (case ! rule of NONE => [] | SOME thm =>
wenzelm@12055
   348
            [Pretty.string_of (pretty_rule "Failed attempt" (Proof.context_of state) thm)]))
wenzelm@13529
   349
          |> cat_lines |> error;
wenzelm@10935
   350
        val check =
wenzelm@12055
   351
          (fn () => Seq.pull (SkipProof.local_skip_proof (K (K ()),
skalberg@15531
   352
            fn _ => fn thm => rule := SOME thm) true state))
wenzelm@14836
   353
          |> setmp proofs 0
wenzelm@14836
   354
          |> transform_error;
skalberg@15531
   355
        val result = (check (), NONE) handle Interrupt => raise Interrupt | e => (NONE, SOME e);
skalberg@15531
   356
      in (case result of (SOME _, NONE) => () | (_, exn) => fail exn); state end;
wenzelm@12244
   357
wenzelm@10935
   358
end;
wenzelm@10935
   359
wenzelm@10935
   360
wenzelm@5830
   361
(* statements *)
wenzelm@5830
   362
wenzelm@7271
   363
local
wenzelm@7271
   364
wenzelm@12141
   365
fun global_attributes thy ((name, src), s) = ((name, map (Attrib.global_attribute thy) src), s);
wenzelm@12697
   366
fun local_attributes thy ((name, src), s) = ((name, map (Attrib.local_attribute thy) src), s);
wenzelm@12697
   367
fun local_attributes' state = local_attributes (Proof.theory_of state);
wenzelm@6371
   368
wenzelm@12141
   369
fun global_statement f args int thy =
wenzelm@12141
   370
  ProofHistory.init (Toplevel.undo_limit int) (f (map (global_attributes thy) args) thy);
wenzelm@12141
   371
fun global_statement_i f args int thy = ProofHistory.init (Toplevel.undo_limit int) (f args thy);
wenzelm@10935
   372
wenzelm@12141
   373
fun local_statement' f g args int = ProofHistory.apply (fn state =>
wenzelm@12697
   374
  f (map (local_attributes' state) args) int (g state));
wenzelm@12141
   375
fun local_statement_i' f g args int = ProofHistory.apply (f args int o g);
wenzelm@12141
   376
fun local_statement f g args = local_statement' (K o f) g args ();
wenzelm@12141
   377
fun local_statement_i f g args = local_statement_i' (K o f) g args ();
wenzelm@7271
   378
wenzelm@12701
   379
in
wenzelm@12701
   380
ballarin@15596
   381
fun multi_theorem k thy_mod a elems concl int thy =
ballarin@15596
   382
  global_statement (Proof.multi_theorem k thy_mod NONE (apsnd (map (Attrib.global_attribute thy)) a)
ballarin@15596
   383
    (map (Locale.map_attrib_elem_or_expr (Attrib.multi_attribute thy)) elems)) concl int thy;
wenzelm@12697
   384
ballarin@15596
   385
fun multi_theorem_i k thy_mod a = global_statement_i o Proof.multi_theorem_i k thy_mod NONE a;
wenzelm@12697
   386
wenzelm@12957
   387
fun locale_multi_theorem k locale (name, atts) elems concl int thy =
ballarin@15596
   388
  global_statement (Proof.multi_theorem k I
ballarin@15596
   389
      (SOME (locale, (map (Attrib.multi_attribute thy) atts,
ballarin@15596
   390
          map (map (Attrib.multi_attribute thy) o snd o fst) concl)))
ballarin@15596
   391
      (name, []) (map (Locale.map_attrib_elem_or_expr (Attrib.multi_attribute thy)) elems))
wenzelm@12957
   392
      (map (apfst (apsnd (K []))) concl) int thy;
wenzelm@12697
   393
wenzelm@12957
   394
fun locale_multi_theorem_i k locale (name, atts) elems concl =
ballarin@15596
   395
  global_statement_i (Proof.multi_theorem_i k I (SOME (locale, (atts, map (snd o fst) concl)))
wenzelm@12957
   396
      (name, []) elems) (map (apfst (apsnd (K []))) concl);
wenzelm@12697
   397
ballarin@15596
   398
fun theorem k (a, t) = multi_theorem k I a [] [(("", []), [t])];
ballarin@15596
   399
fun theorem_i k (a, t) = multi_theorem_i k I a [] [(("", []), [t])];
wenzelm@11742
   400
ballarin@15596
   401
fun smart_multi_theorem k NONE a elems = multi_theorem k I a elems
skalberg@15531
   402
  | smart_multi_theorem k (SOME locale) a elems = locale_multi_theorem k locale a elems;
wenzelm@11742
   403
wenzelm@12876
   404
val assume      = local_statement Proof.assume I;
wenzelm@12876
   405
val assume_i    = local_statement_i Proof.assume_i I;
wenzelm@12876
   406
val presume     = local_statement Proof.presume I;
wenzelm@12876
   407
val presume_i   = local_statement_i Proof.presume_i I;
wenzelm@12969
   408
val have        = local_statement (Proof.have Seq.single true) I;
wenzelm@12969
   409
val have_i      = local_statement_i (Proof.have_i Seq.single true) I;
wenzelm@12969
   410
val hence       = local_statement (Proof.have Seq.single true) Proof.chain;
wenzelm@12969
   411
val hence_i     = local_statement_i (Proof.have_i Seq.single true) Proof.chain;
wenzelm@12969
   412
val show        = local_statement' (Proof.show check_goal Seq.single true) I;
wenzelm@12969
   413
val show_i      = local_statement_i' (Proof.show_i check_goal Seq.single true) I;
wenzelm@12969
   414
val thus        = local_statement' (Proof.show check_goal Seq.single true) Proof.chain;
wenzelm@12969
   415
val thus_i      = local_statement_i' (Proof.show_i check_goal Seq.single true) Proof.chain;
wenzelm@12141
   416
wenzelm@12141
   417
fun gen_def f ((name, srcs), def) = ProofHistory.apply (fn state =>
wenzelm@12141
   418
  f name (map (Attrib.local_attribute (Proof.theory_of state)) srcs) def state);
wenzelm@12141
   419
wenzelm@12876
   420
val local_def = gen_def Proof.def;
wenzelm@12876
   421
val local_def_i = gen_def Proof.def_i;
wenzelm@5830
   422
wenzelm@12969
   423
fun obtain (xs, asms) = proof'' (ProofHistory.applys o (fn print => fn state =>
wenzelm@12969
   424
  Obtain.obtain xs (map (local_attributes' state) asms) print state));
wenzelm@11890
   425
wenzelm@12969
   426
fun obtain_i (xs, asms) = proof'' (ProofHistory.applys o Obtain.obtain_i xs asms);
wenzelm@11890
   427
wenzelm@7271
   428
end;
wenzelm@7271
   429
wenzelm@5830
   430
wenzelm@5830
   431
(* blocks *)
wenzelm@5830
   432
wenzelm@12876
   433
val begin_block = ProofHistory.apply Proof.begin_block;
wenzelm@12876
   434
val next_block = ProofHistory.apply Proof.next_block;
wenzelm@12876
   435
val end_block = ProofHistory.applys Proof.end_block;
wenzelm@5830
   436
wenzelm@5830
   437
wenzelm@8165
   438
(* shuffle state *)
wenzelm@8165
   439
wenzelm@8236
   440
fun shuffle meth i = Method.refine (Method.Basic (K (meth i))) o Proof.assert_no_chain;
wenzelm@8204
   441
wenzelm@12876
   442
fun defer i = ProofHistory.applys (shuffle Method.defer i);
wenzelm@12876
   443
fun prefer i = ProofHistory.applys (shuffle Method.prefer i);
wenzelm@8165
   444
wenzelm@8165
   445
wenzelm@5830
   446
(* backward steps *)
wenzelm@5830
   447
wenzelm@12876
   448
fun apply m = ProofHistory.applys
wenzelm@8881
   449
  (Seq.map (Proof.goal_facts (K [])) o Method.refine m o Proof.assert_backward);
wenzelm@12876
   450
fun apply_end m = ProofHistory.applys (Method.refine_end m o Proof.assert_forward);
wenzelm@12876
   451
val proof = ProofHistory.applys o Method.proof;
wenzelm@6404
   452
wenzelm@6404
   453
wenzelm@6404
   454
(* local endings *)
wenzelm@6404
   455
wenzelm@12876
   456
val local_qed = proof'' o (ProofHistory.applys oo Method.local_qed true);
berghofe@15237
   457
val skip_local_qed =
berghofe@15237
   458
  Toplevel.skip_proof (History.apply (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF));
wenzelm@12876
   459
val local_terminal_proof = proof'' o (ProofHistory.applys oo Method.local_terminal_proof);
wenzelm@8966
   460
val local_default_proof = proof'' (ProofHistory.applys o Method.local_default_proof);
wenzelm@6904
   461
val local_immediate_proof = proof'' (ProofHistory.applys o Method.local_immediate_proof);
wenzelm@8966
   462
val local_done_proof = proof'' (ProofHistory.applys o Method.local_done_proof);
wenzelm@12318
   463
val local_skip_proof = Toplevel.proof' (fn int =>
wenzelm@12318
   464
  ProofHistory.applys (SkipProof.local_skip_proof (cond_print_result_rule int) int));
wenzelm@6404
   465
wenzelm@6404
   466
wenzelm@6404
   467
(* global endings *)
wenzelm@6404
   468
wenzelm@12318
   469
fun global_result finish = Toplevel.proof_to_theory' (fn int => fn prf =>
wenzelm@6404
   470
  let
wenzelm@6404
   471
    val state = ProofHistory.current prf;
wenzelm@6404
   472
    val _ = if Proof.at_bottom state then () else raise Toplevel.UNDEF;
wenzelm@12318
   473
    val (thy, ((kind, name), res)) = finish int state;
wenzelm@11742
   474
  in
wenzelm@11742
   475
    if kind = "" orelse kind = Drule.internalK then ()
wenzelm@12244
   476
    else (print_result (Proof.context_of state) ((kind, name), res);
skalberg@15531
   477
      Context.setmp (SOME thy) (Present.results kind) res);
wenzelm@11742
   478
    thy
wenzelm@11742
   479
  end);
wenzelm@6404
   480
wenzelm@12876
   481
fun global_qed m = global_result (K (Method.global_qed true m));
berghofe@15237
   482
val skip_global_qed = Toplevel.skip_proof_to_theory (equal 1 o History.current);
wenzelm@12876
   483
fun global_terminal_proof m = global_result (K (Method.global_terminal_proof m));
wenzelm@12318
   484
val global_default_proof = global_result (K Method.global_default_proof);
wenzelm@12318
   485
val global_immediate_proof = global_result (K Method.global_immediate_proof);
wenzelm@6888
   486
val global_skip_proof = global_result SkipProof.global_skip_proof;
wenzelm@12318
   487
val global_done_proof = global_result (K Method.global_done_proof);
wenzelm@6404
   488
wenzelm@6404
   489
wenzelm@6404
   490
(* common endings *)
wenzelm@6404
   491
berghofe@15237
   492
fun qed meth = local_qed meth o global_qed meth o skip_local_qed o skip_global_qed;
wenzelm@12876
   493
fun terminal_proof meths = local_terminal_proof meths o global_terminal_proof meths;
wenzelm@12876
   494
val default_proof = local_default_proof o global_default_proof;
wenzelm@12876
   495
val immediate_proof = local_immediate_proof o global_immediate_proof;
wenzelm@12876
   496
val done_proof = local_done_proof o global_done_proof;
wenzelm@12876
   497
val skip_proof = local_skip_proof o global_skip_proof;
berghofe@15237
   498
val forget_proof = Toplevel.proof_to_theory (Proof.theory_of o ProofHistory.current) o
berghofe@15237
   499
  Toplevel.skip_proof_to_theory (K true);
wenzelm@8210
   500
wenzelm@5830
   501
wenzelm@6774
   502
(* calculational proof commands *)
wenzelm@6774
   503
wenzelm@6879
   504
local
wenzelm@6879
   505
wenzelm@12055
   506
fun cond_print_calc int ctxt thms =
wenzelm@12055
   507
  if int then
wenzelm@12055
   508
    Pretty.writeln (Pretty.big_list "calculation:" (map (ProofContext.pretty_thm ctxt) thms))
wenzelm@6774
   509
  else ();
wenzelm@6774
   510
wenzelm@6774
   511
fun proof''' f = Toplevel.proof' (f o cond_print_calc);
wenzelm@6774
   512
wenzelm@12876
   513
fun gen_calc get f args prt state =
skalberg@15570
   514
  f (Option.map (fn srcs => get srcs state) args) prt state;
wenzelm@6879
   515
wenzelm@6879
   516
in
wenzelm@6879
   517
wenzelm@14564
   518
fun get_thmss srcs = Proof.the_facts o local_thmss Proof.note_thmss [(("", []), srcs)];
wenzelm@7012
   519
fun get_thmss_i thms _ = thms;
wenzelm@7012
   520
wenzelm@6879
   521
fun also x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.also x);
wenzelm@6879
   522
fun also_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.also x);
wenzelm@6879
   523
fun finally x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.finally x);
wenzelm@6879
   524
fun finally_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.finally x);
wenzelm@12876
   525
val moreover = proof''' (ProofHistory.apply o Calculation.moreover);
wenzelm@12876
   526
val ultimately = proof''' (ProofHistory.apply o Calculation.ultimately);
wenzelm@6879
   527
wenzelm@6879
   528
end;
wenzelm@6774
   529
wenzelm@6774
   530
wenzelm@5830
   531
(* translation functions *)
wenzelm@5830
   532
wenzelm@14649
   533
fun advancedT false = ""
wenzelm@14649
   534
  | advancedT true = "Sign.sg -> ";
wenzelm@5830
   535
wenzelm@14649
   536
fun advancedN false = ""
wenzelm@14649
   537
  | advancedN true = "advanced_";
wenzelm@14649
   538
wenzelm@14649
   539
fun parse_ast_translation (a, txt) =
wenzelm@14649
   540
  txt |> Context.use_let ("val parse_ast_translation: (string * (" ^ advancedT a ^
wenzelm@14649
   541
      "Syntax.ast list -> Syntax.ast)) list")
wenzelm@14649
   542
    ("Theory.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], [])");
wenzelm@5830
   543
wenzelm@14649
   544
fun parse_translation (a, txt) =
wenzelm@14649
   545
  txt |> Context.use_let ("val parse_translation: (string * (" ^ advancedT a ^
wenzelm@14649
   546
      "term list -> term)) list")
wenzelm@14649
   547
    ("Theory.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], [])");
wenzelm@14649
   548
wenzelm@14649
   549
fun print_translation (a, txt) =
wenzelm@14649
   550
  txt |> Context.use_let ("val print_translation: (string * (" ^ advancedT a ^
wenzelm@14649
   551
      "term list -> term)) list")
wenzelm@14649
   552
    ("Theory.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, [])");
wenzelm@5830
   553
wenzelm@14649
   554
fun print_ast_translation (a, txt) =
wenzelm@14649
   555
  txt |> Context.use_let ("val print_ast_translation: (string * (" ^ advancedT a ^
wenzelm@14649
   556
      "Syntax.ast list -> Syntax.ast)) list")
wenzelm@14649
   557
    ("Theory.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation)");
wenzelm@5830
   558
wenzelm@14649
   559
fun typed_print_translation (a, txt) =
wenzelm@14649
   560
  txt |> Context.use_let ("val typed_print_translation: (string * (" ^ advancedT a ^
wenzelm@14649
   561
      "bool -> typ -> term list -> term)) list")
wenzelm@14649
   562
    ("Theory.add_" ^ advancedN a ^ "trfunsT typed_print_translation");
wenzelm@5830
   563
wenzelm@5830
   564
val token_translation =
berghofe@11717
   565
  Context.use_let "val token_translation: (string * string * (string -> string * real)) list"
wenzelm@12876
   566
    "Theory.add_tokentrfuns token_translation";
wenzelm@5830
   567
wenzelm@5830
   568
wenzelm@9193
   569
(* add method *)
wenzelm@9193
   570
wenzelm@12876
   571
fun method_setup (name, txt, cmt) =
wenzelm@9193
   572
  Context.use_let
wenzelm@9810
   573
    "val thm = PureThy.get_thm_closure (Context.the_context ());\n\
wenzelm@9810
   574
    \val thms = PureThy.get_thms_closure (Context.the_context ());\n\
wenzelm@9810
   575
    \val method: bstring * (Args.src -> Proof.context -> Proof.method) * string"
wenzelm@9193
   576
    "PureIsar.Method.add_method method"
berghofe@14598
   577
    ("(" ^ Library.quote name ^ ", " ^ txt ^ ", " ^ Library.quote cmt ^ ")");
wenzelm@9193
   578
wenzelm@9193
   579
wenzelm@5830
   580
(* add_oracle *)
wenzelm@5830
   581
wenzelm@12876
   582
fun add_oracle (name, txt) =
wenzelm@8349
   583
  Context.use_let
wenzelm@9590
   584
    "val oracle: bstring * (Sign.sg * Object.T -> term)"
wenzelm@5830
   585
    "Theory.add_oracle oracle"
berghofe@14598
   586
    ("(" ^ Library.quote name ^ ", " ^ txt ^ ")");
wenzelm@5830
   587
wenzelm@5830
   588
wenzelm@12062
   589
(* add_locale *)
wenzelm@12062
   590
wenzelm@13393
   591
fun add_locale (do_pred, name, import, body) thy =
wenzelm@13393
   592
  Locale.add_locale do_pred name import
ballarin@15596
   593
    (map (Locale.map_attrib_element (Attrib.multi_attribute thy)) body) thy;
wenzelm@12062
   594
wenzelm@6688
   595
(* theory init and exit *)
wenzelm@5830
   596
wenzelm@8804
   597
fun gen_begin_theory upd name parents files =
skalberg@14503
   598
  let val ml_filename = Path.pack (ThyLoad.ml_path name);
skalberg@14503
   599
      val () = if exists (equal ml_filename o #1) files
skalberg@14503
   600
               then error ((quote ml_filename) ^ " not allowed in files section for theory " ^ name)
skalberg@14503
   601
               else ()
skalberg@14503
   602
  in ThyInfo.begin_theory Present.begin_theory upd name parents (map (apfst Path.unpack) files) end;
wenzelm@5830
   603
wenzelm@8804
   604
val begin_theory = gen_begin_theory false;
wenzelm@7909
   605
wenzelm@7909
   606
fun end_theory thy =
wenzelm@7932
   607
  if ThyInfo.check_known_thy (PureThy.get_name thy) then ThyInfo.end_theory thy
wenzelm@7909
   608
  else thy;
wenzelm@7909
   609
wenzelm@7932
   610
val kill_theory = ThyInfo.if_known_thy ThyInfo.remove_thy;
wenzelm@6688
   611
wenzelm@7103
   612
wenzelm@8804
   613
fun bg_theory (name, parents, files) int = gen_begin_theory int name parents files;
wenzelm@6331
   614
fun en_theory thy = (end_theory thy; ());
wenzelm@6331
   615
wenzelm@7932
   616
fun theory spec = Toplevel.init_theory (bg_theory spec) en_theory (kill_theory o PureThy.get_name);
wenzelm@6246
   617
wenzelm@6246
   618
wenzelm@6246
   619
(* context switch *)
wenzelm@6246
   620
wenzelm@7960
   621
fun fetch_context f x =
skalberg@15531
   622
  (case Context.pass NONE f x of
skalberg@15531
   623
    ((), NONE) => raise Toplevel.UNDEF
skalberg@15531
   624
  | ((), SOME thy) => thy);
wenzelm@7960
   625
wenzelm@7960
   626
fun init_context f x = Toplevel.init_theory (fn _ => fetch_context f x) (K ()) (K ());
wenzelm@7953
   627
wenzelm@7953
   628
val context = init_context (ThyInfo.quiet_update_thy true);
wenzelm@6246
   629
wenzelm@5830
   630
ballarin@15624
   631
(* registration of locale interpretation *)
ballarin@15596
   632
ballarin@15624
   633
fun register_globally (((prfx, atts), expr), insts) b thy =
ballarin@15596
   634
  let
ballarin@15624
   635
    val (thy', propss, activate) = Locale.prep_global_registration
ballarin@15598
   636
          (prfx, map (Attrib.global_attribute thy) atts) expr insts thy;
ballarin@15598
   637
    fun witness id (thy, thm) = let
ballarin@15596
   638
        val thm' = Drule.freeze_all thm;
ballarin@15596
   639
      in
ballarin@15624
   640
        (Locale.add_global_witness id thm' thy, thm')
ballarin@15596
   641
      end;
ballarin@15596
   642
  in
ballarin@15596
   643
    multi_theorem_i Drule.internalK activate ("", []) [] 
ballarin@15598
   644
      (map (fn (id as (n, _), props) => ((NameSpace.base n, [witness id]), 
ballarin@15596
   645
        map (fn prop => (prop, ([], []))) props)) propss) b thy'
ballarin@15596
   646
  end;
ballarin@15596
   647
ballarin@15624
   648
fun register_locally (((prfx, atts), expr), insts) =
ballarin@15624
   649
  ProofHistory.apply (fn state =>
ballarin@15624
   650
    let
ballarin@15624
   651
      val ctxt = Proof.context_of state;
ballarin@15624
   652
      val (ctxt', propss, activate) = Locale.prep_local_registration
ballarin@15624
   653
            (prfx, map (Attrib.local_attribute' ctxt) atts) expr insts ctxt;
ballarin@15624
   654
      fun witness id (ctxt, thm) = (Locale.add_local_witness id thm ctxt, thm);
ballarin@15624
   655
    in state
ballarin@15624
   656
      |> Proof.map_context (fn _ => ctxt')
ballarin@15624
   657
      |> Proof.interpret_i activate Seq.single true
ballarin@15624
   658
           (map (fn (id as (n, _), props) => ((NameSpace.base n, [witness id]),
ballarin@15624
   659
             map (fn prop => (prop, ([], []))) props)) propss)
ballarin@15624
   660
    end);
ballarin@15596
   661
wenzelm@5830
   662
end;