author | wenzelm |
Thu, 18 Aug 2005 11:17:43 +0200 | |
changeset 17108 | 3962f74bbb8e |
parent 17034 | b4d9b87c102e |
child 17139 | 165c97f9bb63 |
permissions | -rw-r--r-- |
5830 | 1 |
(* Title: Pure/Isar/isar_thy.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
6371 | 5 |
Pure/Isar derived theory operations. |
5830 | 6 |
*) |
7 |
||
8 |
signature ISAR_THY = |
|
9 |
sig |
|
16372 | 10 |
val hide_names: string * xstring list -> theory -> theory |
11 |
val hide_names_i: string * string list -> theory -> theory |
|
15703 | 12 |
val add_axioms: ((bstring * string) * Attrib.src list) list -> theory -> theory |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
13 |
val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory |
15703 | 14 |
val add_defs: bool * ((bstring * string) * Attrib.src list) list -> theory -> theory |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
15 |
val add_defs_i: bool * ((bstring * term) * theory attribute list) list -> theory -> theory |
15703 | 16 |
val theorems: string -> |
17 |
((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list |
|
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
18 |
-> theory -> theory * (string * thm list) list |
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
19 |
val theorems_i: string -> |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
20 |
((bstring * theory attribute list) * (thm list * theory attribute list) list) list |
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
21 |
-> theory -> theory * (string * thm list) list |
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
22 |
val locale_theorems: string -> xstring -> |
15703 | 23 |
((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list |
17108 | 24 |
-> theory -> (theory * Proof.context) * (bstring * thm list) list |
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
25 |
val locale_theorems_i: string -> string -> |
15703 | 26 |
((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list -> |
17108 | 27 |
theory -> (theory * Proof.context) * (string * thm list) list |
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
28 |
val smart_theorems: string -> xstring option -> |
15703 | 29 |
((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list |
17108 | 30 |
-> theory -> (theory * Proof.context) * (string * thm list) list |
15703 | 31 |
val declare_theorems: xstring option -> |
17108 | 32 |
(thmref * Attrib.src list) list -> theory -> theory * Proof.context |
15703 | 33 |
val apply_theorems: (thmref * Attrib.src list) list -> theory -> theory * thm list |
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
34 |
val apply_theorems_i: (thm list * theory attribute list) list -> theory -> theory * thm list |
15703 | 35 |
val theorem: string -> (bstring * Attrib.src list) * (string * (string list * string list)) |
12141 | 36 |
-> bool -> theory -> ProofHistory.T |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
37 |
val theorem_i: string -> (bstring * theory attribute list) * |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
38 |
(term * (term list * term list)) -> bool -> theory -> ProofHistory.T |
17034 | 39 |
val multi_theorem: string -> (thm list * thm list -> theory -> theory) -> |
15696 | 40 |
(cterm list -> Proof.context -> Proof.context -> thm -> thm) -> |
15703 | 41 |
bstring * Attrib.src list -> Locale.element Locale.elem_expr list -> |
42 |
((bstring * Attrib.src list) * (string * (string list * string list)) list) list |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
43 |
-> bool -> theory -> ProofHistory.T |
17034 | 44 |
val multi_theorem_i: string -> (thm list * thm list -> theory -> theory) -> |
45 |
(cterm list -> Proof.context -> Proof.context -> thm -> thm) -> |
|
46 |
bstring * theory attribute list -> |
|
47 |
Locale.element_i Locale.elem_expr list -> |
|
48 |
((bstring * theory attribute list) * (term * (term list * term list)) list) list -> |
|
49 |
bool -> theory -> ProofHistory.T |
|
50 |
val locale_multi_theorem: |
|
51 |
string -> (thm list * thm list -> theory -> theory) -> |
|
15696 | 52 |
(cterm list -> Proof.context -> Proof.context -> thm -> thm) -> |
17034 | 53 |
xstring -> |
54 |
bstring * Attrib.src list -> Locale.element Locale.elem_expr list -> |
|
55 |
((bstring * Attrib.src list) * (string * (string list * string list)) list) list -> |
|
56 |
bool -> theory -> ProofHistory.T |
|
57 |
val locale_multi_theorem_i: |
|
58 |
string -> (thm list * thm list -> theory -> theory) -> |
|
59 |
(cterm list -> Proof.context -> Proof.context -> thm -> thm) -> |
|
60 |
string -> |
|
61 |
bstring * Attrib.src list -> Locale.element_i Locale.elem_expr list -> |
|
62 |
((bstring * Attrib.src list) * (term * (term list * term list)) list) list -> |
|
63 |
bool -> theory -> ProofHistory.T |
|
15531 | 64 |
val smart_multi_theorem: string -> xstring option |
15703 | 65 |
-> bstring * Attrib.src list -> Locale.element Locale.elem_expr list |
66 |
-> ((bstring * Attrib.src list) * (string * (string list * string list)) list) list |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
67 |
-> bool -> theory -> ProofHistory.T |
15703 | 68 |
val have: ((string * Attrib.src list) * |
69 |
(string * (string list * string list)) list) list -> ProofHistory.T -> ProofHistory.T |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
70 |
val have_i: ((string * Proof.context attribute list) * |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
71 |
(term * (term list * term list)) list) list -> ProofHistory.T -> ProofHistory.T |
15703 | 72 |
val hence: ((string * Attrib.src list) * |
73 |
(string * (string list * string list)) list) list -> ProofHistory.T -> ProofHistory.T |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
74 |
val hence_i: ((string * Proof.context attribute list) * |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
75 |
(term * (term list * term list)) list) list -> ProofHistory.T -> ProofHistory.T |
15703 | 76 |
val show: ((string * Attrib.src list) * |
77 |
(string * (string list * string list)) list) list |
|
12141 | 78 |
-> bool -> ProofHistory.T -> ProofHistory.T |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
79 |
val show_i: ((string * Proof.context attribute list) * |
15703 | 80 |
(term * (term list * term list)) list) list |
81 |
-> bool -> ProofHistory.T -> ProofHistory.T |
|
82 |
val thus: ((string * Attrib.src list) * |
|
83 |
(string * (string list * string list)) list) list |
|
12141 | 84 |
-> bool -> ProofHistory.T -> ProofHistory.T |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
85 |
val thus_i: ((string * Proof.context attribute list) |
15703 | 86 |
* (term * (term list * term list)) list) list |
87 |
-> bool -> ProofHistory.T -> ProofHistory.T |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
88 |
val obtain: (string list * string option) list |
15703 | 89 |
* ((string * Attrib.src list) * (string * (string list * string list)) list) list |
12969 | 90 |
-> Toplevel.transition -> Toplevel.transition |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
91 |
val obtain_i: (string list * typ option) list |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
92 |
* ((string * Proof.context attribute list) * (term * (term list * term list)) list) list |
12969 | 93 |
-> Toplevel.transition -> Toplevel.transition |
16605
4590c1f79050
added print_mode three_buffersN and corresponding cond_print;
wenzelm
parents:
16529
diff
changeset
|
94 |
val three_buffersN: string |
4590c1f79050
added print_mode three_buffersN and corresponding cond_print;
wenzelm
parents:
16529
diff
changeset
|
95 |
val cond_print: Toplevel.transition -> Toplevel.transition |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
96 |
val qed: Method.text option -> Toplevel.transition -> Toplevel.transition |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
97 |
val terminal_proof: Method.text * Method.text option |
6937 | 98 |
-> Toplevel.transition -> Toplevel.transition |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
99 |
val default_proof: Toplevel.transition -> Toplevel.transition |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
100 |
val immediate_proof: Toplevel.transition -> Toplevel.transition |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
101 |
val done_proof: Toplevel.transition -> Toplevel.transition |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
102 |
val skip_proof: Toplevel.transition -> Toplevel.transition |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
103 |
val forget_proof: Toplevel.transition -> Toplevel.transition |
15703 | 104 |
val get_thmss: (thmref * Attrib.src list) list -> Proof.state -> thm list |
105 |
val also: (thmref * Attrib.src list) list option -> Toplevel.transition -> Toplevel.transition |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
106 |
val also_i: thm list option -> Toplevel.transition -> Toplevel.transition |
15703 | 107 |
val finally: (thmref * Attrib.src list) list option -> Toplevel.transition -> Toplevel.transition |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
108 |
val finally_i: thm list option -> Toplevel.transition -> Toplevel.transition |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
109 |
val moreover: Toplevel.transition -> Toplevel.transition |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
110 |
val ultimately: Toplevel.transition -> Toplevel.transition |
15801 | 111 |
val generic_setup: string -> theory -> theory |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
112 |
val method_setup: bstring * string * string -> theory -> theory |
16849 | 113 |
val add_oracle: bstring * string * string -> theory -> theory |
15703 | 114 |
val register_globally: |
115 |
((string * Attrib.src list) * Locale.expr) * string option list -> |
|
116 |
bool -> theory -> ProofHistory.T |
|
16736
1e792b32abef
Preparations for interpretation of locales in locales.
ballarin
parents:
16605
diff
changeset
|
117 |
val register_in_locale: |
1e792b32abef
Preparations for interpretation of locales in locales.
ballarin
parents:
16605
diff
changeset
|
118 |
string * Locale.expr -> bool -> theory -> ProofHistory.T |
15703 | 119 |
val register_locally: |
120 |
((string * Attrib.src list) * Locale.expr) * string option list -> |
|
121 |
ProofHistory.T -> ProofHistory.T |
|
6331 | 122 |
val begin_theory: string -> string list -> (string * bool) list -> theory |
123 |
val end_theory: theory -> theory |
|
7932 | 124 |
val kill_theory: string -> unit |
6246 | 125 |
val theory: string * string list * (string * bool) list |
126 |
-> Toplevel.transition -> Toplevel.transition |
|
7960 | 127 |
val init_context: ('a -> unit) -> 'a -> Toplevel.transition -> Toplevel.transition |
6246 | 128 |
val context: string -> Toplevel.transition -> Toplevel.transition |
5830 | 129 |
end; |
130 |
||
131 |
structure IsarThy: ISAR_THY = |
|
132 |
struct |
|
133 |
||
134 |
(** derived theory and proof operations **) |
|
135 |
||
8724 | 136 |
(* name spaces *) |
137 |
||
8885 | 138 |
local |
139 |
||
140 |
val kinds = |
|
16372 | 141 |
[("class", (Sign.intern_class, can o Sign.certify_class, Theory.hide_classes_i)), |
142 |
("type", (Sign.intern_type, Sign.declared_tyname, Theory.hide_types_i)), |
|
143 |
("const", (Sign.intern_const, Sign.declared_const, Theory.hide_consts_i))]; |
|
8724 | 144 |
|
16372 | 145 |
fun gen_hide int (kind, xnames) thy = |
146 |
(case assoc_string (kinds, kind) of |
|
147 |
SOME (intern, check, hide) => |
|
8885 | 148 |
let |
16425
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
149 |
val names = if int then map (intern thy) xnames else xnames; |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
150 |
val bads = filter_out (check thy) names; |
8885 | 151 |
in |
16372 | 152 |
if null bads then hide true names thy |
8885 | 153 |
else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads) |
154 |
end |
|
15531 | 155 |
| NONE => error ("Bad name space specification: " ^ quote kind)); |
8885 | 156 |
|
157 |
in |
|
158 |
||
16372 | 159 |
val hide_names = gen_hide true; |
160 |
val hide_names_i = gen_hide false; |
|
8885 | 161 |
|
162 |
end; |
|
8724 | 163 |
|
164 |
||
5830 | 165 |
(* axioms and defs *) |
166 |
||
5915 | 167 |
fun add_axms f args thy = |
168 |
f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy; |
|
169 |
||
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
170 |
val add_axioms = add_axms (#1 oo PureThy.add_axioms); |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
171 |
val add_axioms_i = #1 oo PureThy.add_axioms_i; |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
172 |
fun add_defs (x, args) = add_axms (#1 oo PureThy.add_defs x) args; |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
173 |
fun add_defs_i (x, args) = (#1 oo PureThy.add_defs_i x) args; |
6371 | 174 |
|
175 |
||
5915 | 176 |
(* theorems *) |
177 |
||
12705 | 178 |
local |
179 |
||
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
180 |
fun present_thmss kind (thy, named_thmss) = |
17108 | 181 |
conditional (kind <> "" andalso kind <> Drule.internalK) (fn () => |
15531 | 182 |
Context.setmp (SOME thy) (Present.results (kind ^ "s")) named_thmss); |
17108 | 183 |
|
184 |
fun present_thmss' kind ((thy, _), named_thmss) = present_thmss kind (thy, named_thmss); |
|
185 |
||
186 |
fun global_attribs thy = Attrib.map_facts (Attrib.global_attribute thy); |
|
5915 | 187 |
|
12705 | 188 |
in |
189 |
||
17108 | 190 |
fun theorems_i k = tap (present_thmss k) oo PureThy.note_thmss_i (Drule.kind k); |
191 |
fun locale_theorems_i k loc = tap (present_thmss' k) oo Locale.note_thmss_i k loc; |
|
5915 | 192 |
|
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
193 |
fun theorems k args thy = thy |
14564 | 194 |
|> PureThy.note_thmss (Drule.kind k) (global_attribs thy args) |
17108 | 195 |
|> tap (present_thmss k); |
12713
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
196 |
|
c9e3e34dbc45
clarified/added theorems(_i) vs. locale_theorems(_i);
wenzelm
parents:
12705
diff
changeset
|
197 |
fun locale_theorems k loc args thy = thy |
15703 | 198 |
|> Locale.note_thmss k loc args |
17108 | 199 |
|> tap (present_thmss' k); |
6371 | 200 |
|
17108 | 201 |
fun smart_theorems k NONE args thy = |
202 |
let val (thy', res) = |
|
203 |
thy |> PureThy.note_thmss (Drule.kind k) (global_attribs thy args) |
|
204 |
in |
|
205 |
present_thmss k (thy', res); |
|
206 |
((thy', ProofContext.init thy'), res) |
|
207 |
end |
|
208 |
| smart_theorems k (SOME loc) args thy = thy |
|
209 |
|> Locale.note_thmss k loc args |
|
210 |
|> tap (present_thmss' k); |
|
9590 | 211 |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
212 |
fun declare_theorems opt_loc args = #1 o smart_theorems "" opt_loc [(("", []), args)]; |
9590 | 213 |
|
15570 | 214 |
fun apply_theorems args = apsnd (List.concat o map snd) o theorems "" [(("", []), args)]; |
215 |
fun apply_theorems_i args = apsnd (List.concat o map snd) o theorems_i "" [(("", []), args)]; |
|
6371 | 216 |
|
12705 | 217 |
end; |
218 |
||
5915 | 219 |
|
10935 | 220 |
(* local results *) |
221 |
||
222 |
local |
|
223 |
||
12244 | 224 |
fun prt_facts ctxt = |
15570 | 225 |
List.concat o (separate [Pretty.fbrk, Pretty.str "and "]) o |
13276 | 226 |
map (single o ProofContext.pretty_fact ctxt); |
12244 | 227 |
|
228 |
fun pretty_results ctxt ((kind, ""), facts) = |
|
229 |
Pretty.block (Pretty.str kind :: Pretty.brk 1 :: prt_facts ctxt facts) |
|
16425
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
230 |
| pretty_results ctxt ((kind, name), [fact]) = Pretty.blk (1, |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
231 |
[Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, ProofContext.pretty_fact ctxt fact]) |
12244 | 232 |
| pretty_results ctxt ((kind, name), facts) = Pretty.blk (1, |
233 |
[Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk, |
|
234 |
Pretty.blk (1, Pretty.str "(" :: prt_facts ctxt facts @ [Pretty.str ")"])]); |
|
10935 | 235 |
|
12055 | 236 |
fun pretty_rule s ctxt thm = |
10935 | 237 |
Pretty.block [Pretty.str (s ^ " to solve goal by exported rule:"), |
12055 | 238 |
Pretty.fbrk, ProofContext.pretty_thm ctxt thm]; |
10935 | 239 |
|
240 |
in |
|
241 |
||
12244 | 242 |
val print_result = Pretty.writeln oo pretty_results; |
243 |
fun print_result' ctxt (k, res) = print_result ctxt ((k, ""), res); |
|
10935 | 244 |
|
16529 | 245 |
fun cond_print_result_rule true = |
246 |
(print_result', priority oo (Pretty.string_of oo pretty_rule "Successful attempt")) |
|
247 |
| cond_print_result_rule false = (K (K ()), K (K ())); |
|
10935 | 248 |
|
249 |
fun proof'' f = Toplevel.proof' (f o cond_print_result_rule); |
|
250 |
||
251 |
fun check_goal false state = state |
|
252 |
| check_goal true state = |
|
253 |
let |
|
15531 | 254 |
val rule = ref (NONE: thm option); |
13529 | 255 |
fun fail exn = |
10935 | 256 |
(["Problem! Local statement will fail to solve any pending goal"] @ |
15531 | 257 |
(case exn of NONE => [] | SOME e => [Toplevel.exn_message e]) @ |
258 |
(case ! rule of NONE => [] | SOME thm => |
|
12055 | 259 |
[Pretty.string_of (pretty_rule "Failed attempt" (Proof.context_of state) thm)])) |
13529 | 260 |
|> cat_lines |> error; |
10935 | 261 |
val check = |
12055 | 262 |
(fn () => Seq.pull (SkipProof.local_skip_proof (K (K ()), |
15531 | 263 |
fn _ => fn thm => rule := SOME thm) true state)) |
14836 | 264 |
|> setmp proofs 0 |
265 |
|> transform_error; |
|
15531 | 266 |
val result = (check (), NONE) handle Interrupt => raise Interrupt | e => (NONE, SOME e); |
267 |
in (case result of (SOME _, NONE) => () | (_, exn) => fail exn); state end; |
|
12244 | 268 |
|
10935 | 269 |
end; |
270 |
||
271 |
||
5830 | 272 |
(* statements *) |
273 |
||
7271 | 274 |
local |
275 |
||
17108 | 276 |
fun global_statement f args int thy = ProofHistory.init (Toplevel.undo_limit int) (f args thy); |
277 |
fun local_statement' f g args int = ProofHistory.apply (f args int o g); |
|
12141 | 278 |
fun local_statement f g args = local_statement' (K o f) g args (); |
7271 | 279 |
|
12701
77ac6d8451ea
export multi_theorem(_i), locale_multi_theorem(_i);
wenzelm
parents:
12697
diff
changeset
|
280 |
in |
77ac6d8451ea
export multi_theorem(_i), locale_multi_theorem(_i);
wenzelm
parents:
12697
diff
changeset
|
281 |
|
15703 | 282 |
fun multi_theorem k after_qed export a elems concl int thy = |
17108 | 283 |
global_statement (Proof.multi_theorem k after_qed export NONE a |
15703 | 284 |
(map (Locale.intern_attrib_elem_expr thy) elems)) concl int thy; |
12697 | 285 |
|
15703 | 286 |
fun multi_theorem_i k after_qed export a = |
17108 | 287 |
global_statement o Proof.multi_theorem_i k after_qed export NONE a; |
12697 | 288 |
|
17034 | 289 |
fun locale_multi_theorem k after_qed export locale (name, atts) elems concl int thy = |
290 |
global_statement (Proof.multi_theorem k after_qed export |
|
15703 | 291 |
(SOME (locale, |
16425
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
292 |
(map (Attrib.intern_src thy) atts, |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
293 |
map (map (Attrib.intern_src thy) o snd o fst) concl))) |
15703 | 294 |
(name, []) |
295 |
(map (Locale.intern_attrib_elem_expr thy) elems)) |
|
296 |
(map (apfst (apsnd (K []))) concl) int thy; |
|
12697 | 297 |
|
17034 | 298 |
fun locale_multi_theorem_i k after_qed export locale (name, atts) elems concl = |
17108 | 299 |
global_statement (Proof.multi_theorem_i k after_qed export |
15696 | 300 |
(SOME (locale, (atts, map (snd o fst) concl))) |
12957 | 301 |
(name, []) elems) (map (apfst (apsnd (K []))) concl); |
12697 | 302 |
|
15703 | 303 |
fun theorem k (a, t) = |
17034 | 304 |
multi_theorem k (K I) ProofContext.export_standard a [] [(("", []), [t])]; |
15703 | 305 |
|
306 |
fun theorem_i k (a, t) = |
|
17034 | 307 |
multi_theorem_i k (K I) ProofContext.export_standard a [] [(("", []), [t])]; |
11742 | 308 |
|
15696 | 309 |
fun smart_multi_theorem k NONE a elems = |
17034 | 310 |
multi_theorem k (K I) ProofContext.export_standard a elems |
15696 | 311 |
| smart_multi_theorem k (SOME locale) a elems = |
17034 | 312 |
locale_multi_theorem k (K I) ProofContext.export_standard locale a elems; |
11742 | 313 |
|
17034 | 314 |
val have = local_statement (Proof.have (K Seq.single) true) I; |
17108 | 315 |
val have_i = local_statement (Proof.have_i (K Seq.single) true) I; |
17034 | 316 |
val hence = local_statement (Proof.have (K Seq.single) true) Proof.chain; |
17108 | 317 |
val hence_i = local_statement (Proof.have_i (K Seq.single) true) Proof.chain; |
17034 | 318 |
val show = local_statement' (Proof.show check_goal (K Seq.single) true) I; |
17108 | 319 |
val show_i = local_statement' (Proof.show_i check_goal (K Seq.single) true) I; |
17034 | 320 |
val thus = local_statement' (Proof.show check_goal (K Seq.single) true) Proof.chain; |
17108 | 321 |
val thus_i = local_statement' (Proof.show_i check_goal (K Seq.single) true) Proof.chain; |
12141 | 322 |
|
17108 | 323 |
fun obtain (xs, asms) = proof'' (ProofHistory.applys o Obtain.obtain xs asms); |
12969 | 324 |
fun obtain_i (xs, asms) = proof'' (ProofHistory.applys o Obtain.obtain_i xs asms); |
11890 | 325 |
|
7271 | 326 |
end; |
327 |
||
5830 | 328 |
|
6404 | 329 |
(* local endings *) |
330 |
||
16605
4590c1f79050
added print_mode three_buffersN and corresponding cond_print;
wenzelm
parents:
16529
diff
changeset
|
331 |
val three_buffersN = "three_buffers"; |
4590c1f79050
added print_mode three_buffersN and corresponding cond_print;
wenzelm
parents:
16529
diff
changeset
|
332 |
val cond_print = Toplevel.print' three_buffersN; |
4590c1f79050
added print_mode three_buffersN and corresponding cond_print;
wenzelm
parents:
16529
diff
changeset
|
333 |
|
17108 | 334 |
val local_qed = proof'' o (ProofHistory.applys oo Proof.local_qed true); |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
15206
diff
changeset
|
335 |
val skip_local_qed = |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
15206
diff
changeset
|
336 |
Toplevel.skip_proof (History.apply (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF)); |
17108 | 337 |
val local_terminal_proof = proof'' o (ProofHistory.applys oo Proof.local_terminal_proof); |
338 |
val local_default_proof = proof'' (ProofHistory.applys o Proof.local_default_proof); |
|
339 |
val local_immediate_proof = proof'' (ProofHistory.applys o Proof.local_immediate_proof); |
|
340 |
val local_done_proof = proof'' (ProofHistory.applys o Proof.local_done_proof); |
|
12318
72348ff7d4a0
skip_proof: do not require quick_and_dirty in interactive mode;
wenzelm
parents:
12271
diff
changeset
|
341 |
val local_skip_proof = Toplevel.proof' (fn int => |
72348ff7d4a0
skip_proof: do not require quick_and_dirty in interactive mode;
wenzelm
parents:
12271
diff
changeset
|
342 |
ProofHistory.applys (SkipProof.local_skip_proof (cond_print_result_rule int) int)); |
6404 | 343 |
|
344 |
||
345 |
(* global endings *) |
|
346 |
||
16143
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
347 |
fun name_results "" res = res |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
348 |
| name_results name res = |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
349 |
let |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
350 |
val n = length (List.concat (map #2 res)); |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
351 |
fun name_res (i, (a, ths)) = |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
352 |
let |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
353 |
val m = length ths; |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
354 |
val j = i + m; |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
355 |
in |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
356 |
if a <> "" then (j, (a, ths)) |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
357 |
else if m = n then (j, (name, ths)) |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
358 |
else if m = 1 then |
16498 | 359 |
(j, (PureThy.string_of_thmref (NameSelection (name, [Single i])), ths)) |
360 |
else (j, (PureThy.string_of_thmref (NameSelection (name, [FromTo (i, j - 1)])), ths)) |
|
16143
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
361 |
end; |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
362 |
in #2 (foldl_map name_res (1, res)) end; |
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
363 |
|
17108 | 364 |
fun global_result finish = Toplevel.proof_to_theory_context (fn int => fn prf => |
6404 | 365 |
let |
366 |
val state = ProofHistory.current prf; |
|
367 |
val _ = if Proof.at_bottom state then () else raise Toplevel.UNDEF; |
|
17108 | 368 |
val ((thy, ctxt), ((kind, name), res)) = finish int state; |
11742 | 369 |
in |
370 |
if kind = "" orelse kind = Drule.internalK then () |
|
12244 | 371 |
else (print_result (Proof.context_of state) ((kind, name), res); |
16143
ee6f7e6fc196
improved naming of complex theorems in presentation;
wenzelm
parents:
15979
diff
changeset
|
372 |
Context.setmp (SOME thy) (Present.results kind) (name_results name res)); |
17108 | 373 |
(thy, ctxt) |
11742 | 374 |
end); |
6404 | 375 |
|
17108 | 376 |
fun global_qed m = global_result (K (Proof.global_qed true m)); |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
15206
diff
changeset
|
377 |
val skip_global_qed = Toplevel.skip_proof_to_theory (equal 1 o History.current); |
17108 | 378 |
fun global_terminal_proof m = global_result (K (Proof.global_terminal_proof m)); |
379 |
val global_default_proof = global_result (K Proof.global_default_proof); |
|
380 |
val global_immediate_proof = global_result (K Proof.global_immediate_proof); |
|
6888
d0c68ebdabc5
skip_proof feature 'sorry' (for quick_and_dirty mode only);
wenzelm
parents:
6885
diff
changeset
|
381 |
val global_skip_proof = global_result SkipProof.global_skip_proof; |
17108 | 382 |
val global_done_proof = global_result (K Proof.global_done_proof); |
6404 | 383 |
|
384 |
||
385 |
(* common endings *) |
|
386 |
||
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
15206
diff
changeset
|
387 |
fun qed meth = local_qed meth o global_qed meth o skip_local_qed o skip_global_qed; |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
388 |
fun terminal_proof meths = local_terminal_proof meths o global_terminal_proof meths; |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
389 |
val default_proof = local_default_proof o global_default_proof; |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
390 |
val immediate_proof = local_immediate_proof o global_immediate_proof; |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
391 |
val done_proof = local_done_proof o global_done_proof; |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
392 |
val skip_proof = local_skip_proof o global_skip_proof; |
15237
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
15206
diff
changeset
|
393 |
val forget_proof = Toplevel.proof_to_theory (Proof.theory_of o ProofHistory.current) o |
250e9be7a09d
Some changes to allow skipping of proof scripts.
berghofe
parents:
15206
diff
changeset
|
394 |
Toplevel.skip_proof_to_theory (K true); |
8210 | 395 |
|
5830 | 396 |
|
6774 | 397 |
(* calculational proof commands *) |
398 |
||
6879 | 399 |
local |
400 |
||
12055 | 401 |
fun cond_print_calc int ctxt thms = |
402 |
if int then |
|
403 |
Pretty.writeln (Pretty.big_list "calculation:" (map (ProofContext.pretty_thm ctxt) thms)) |
|
6774 | 404 |
else (); |
405 |
||
406 |
fun proof''' f = Toplevel.proof' (f o cond_print_calc); |
|
407 |
||
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
408 |
fun gen_calc get f args prt state = |
15570 | 409 |
f (Option.map (fn srcs => get srcs state) args) prt state; |
6879 | 410 |
|
411 |
in |
|
412 |
||
17108 | 413 |
fun get_thmss srcs = Proof.the_facts o Proof.note_thmss [(("", []), srcs)]; |
7012 | 414 |
fun get_thmss_i thms _ = thms; |
415 |
||
6879 | 416 |
fun also x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.also x); |
417 |
fun also_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.also x); |
|
418 |
fun finally x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.finally x); |
|
419 |
fun finally_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.finally x); |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
420 |
val moreover = proof''' (ProofHistory.apply o Calculation.moreover); |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
421 |
val ultimately = proof''' (ProofHistory.apply o Calculation.ultimately); |
6879 | 422 |
|
423 |
end; |
|
6774 | 424 |
|
425 |
||
15801 | 426 |
(* generic_setup *) |
427 |
||
428 |
val generic_setup = |
|
429 |
Context.use_let "val setup: (theory -> theory) list" "Library.apply setup"; |
|
430 |
||
431 |
||
432 |
(* method_setup *) |
|
9193
4f4936582889
have_theorems etc.: handle multiple lists of arguments;
wenzelm
parents:
9032
diff
changeset
|
433 |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12848
diff
changeset
|
434 |
fun method_setup (name, txt, cmt) = |
9193
4f4936582889
have_theorems etc.: handle multiple lists of arguments;
wenzelm
parents:
9032
diff
changeset
|
435 |
Context.use_let |
16498 | 436 |
"val thm = PureThy.get_thm_closure (Context.the_context ()) o PureThy.Name;\n\ |
437 |
\val thms = PureThy.get_thms_closure (Context.the_context ()) o PureThy.Name;\n\ |
|
15703 | 438 |
\val method: bstring * (Method.src -> Proof.context -> Proof.method) * string" |
15829 | 439 |
"Method.add_method method" |
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
14564
diff
changeset
|
440 |
("(" ^ Library.quote name ^ ", " ^ txt ^ ", " ^ Library.quote cmt ^ ")"); |
9193
4f4936582889
have_theorems etc.: handle multiple lists of arguments;
wenzelm
parents:
9032
diff
changeset
|
441 |
|
4f4936582889
have_theorems etc.: handle multiple lists of arguments;
wenzelm
parents:
9032
diff
changeset
|
442 |
|
5830 | 443 |
(* add_oracle *) |
444 |
||
16849 | 445 |
fun add_oracle (name, T, oracle) = |
446 |
let val txt = |
|
447 |
"local\n\ |
|
448 |
\ type T = " ^ T ^ ";\n\ |
|
449 |
\ val oracle: theory -> T -> term = " ^ oracle ^ ";\n\ |
|
450 |
\ val name = " ^ quote name ^ ";\n\ |
|
451 |
\ exception Arg of T;\n\ |
|
452 |
\ val _ = Context.>> (Theory.add_oracle (name, fn (thy, Arg x) => oracle thy x));\n\ |
|
453 |
\ val thy = Context.the_context ();\n\ |
|
454 |
\ val invoke_" ^ name ^ " = Thm.invoke_oracle_i thy (Sign.full_name thy name);\n\ |
|
455 |
\in\n\ |
|
456 |
\ fun " ^ name ^ " thy x = invoke_" ^ name ^ " (thy, Arg x);\n\ |
|
457 |
\end;\n"; |
|
458 |
in Context.use_mltext_theory txt false end; |
|
5830 | 459 |
|
460 |
||
15703 | 461 |
(* registration of locale interpretations *) |
462 |
||
463 |
fun register_globally (((prfx, atts), expr), insts) int thy = |
|
464 |
let |
|
17034 | 465 |
val (propss, after_qed) = |
15703 | 466 |
Locale.prep_global_registration (prfx, atts) expr insts thy; |
467 |
in |
|
17034 | 468 |
multi_theorem_i Drule.internalK (fst #> after_qed) ProofContext.export_plain |
15703 | 469 |
("", []) [] |
17034 | 470 |
(map (fn ((n, _), props) => ((NameSpace.base n, []), |
471 |
map (fn prop => (prop, ([], []))) props)) propss) int thy |
|
16736
1e792b32abef
Preparations for interpretation of locales in locales.
ballarin
parents:
16605
diff
changeset
|
472 |
end; |
1e792b32abef
Preparations for interpretation of locales in locales.
ballarin
parents:
16605
diff
changeset
|
473 |
|
15703 | 474 |
fun register_locally (((prfx, atts), expr), insts) = |
475 |
ProofHistory.apply (fn state => |
|
476 |
let |
|
477 |
val ctxt = Proof.context_of state; |
|
17034 | 478 |
val (propss, after_qed) = |
15703 | 479 |
Locale.prep_local_registration (prfx, atts) expr insts ctxt; |
480 |
in |
|
481 |
state |
|
17034 | 482 |
|> Proof.map_context (fn _ => ctxt) |
483 |
|> Proof.have_i (fn result => Seq.single o Proof.map_context (after_qed result)) true |
|
484 |
(map (fn ((n, _), props) => ((NameSpace.base n, []), |
|
15703 | 485 |
map (fn prop => (prop, ([], []))) props)) propss) |
486 |
end); |
|
487 |
||
17034 | 488 |
fun register_in_locale (target, expr) int thy = |
489 |
let |
|
490 |
val target = Locale.intern thy target; |
|
491 |
val (propss, after_qed) = |
|
492 |
Locale.prep_registration_in_locale target expr thy; |
|
493 |
in |
|
494 |
locale_multi_theorem_i Drule.internalK (fst #> after_qed) |
|
495 |
ProofContext.export_plain |
|
496 |
target ("",[]) [] |
|
497 |
(map (fn ((n, _), props) => ((NameSpace.base n, []), |
|
498 |
map (fn prop => (prop, ([], []))) props)) propss) int thy |
|
499 |
end; |
|
500 |
||
12062 | 501 |
|
6688 | 502 |
(* theory init and exit *) |
5830 | 503 |
|
8804 | 504 |
fun gen_begin_theory upd name parents files = |
16425
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
505 |
let |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
506 |
val ml_filename = Path.pack (ThyLoad.ml_path name); |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
507 |
(* FIXME unreliable test -- better make ThyInfo manage dependencies properly *) |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
508 |
val _ = conditional (exists (equal ml_filename o #1) files) (fn () => |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
509 |
error ((quote ml_filename) ^ " not allowed in files section for theory " ^ name)); |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
510 |
in |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
511 |
ThyInfo.begin_theory Present.begin_theory upd name parents |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
512 |
(map (apfst Path.unpack) files) |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
513 |
end; |
5830 | 514 |
|
8804 | 515 |
val begin_theory = gen_begin_theory false; |
7909 | 516 |
|
517 |
fun end_theory thy = |
|
16425
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
518 |
if ThyInfo.check_known_thy (Context.theory_name thy) then ThyInfo.end_theory thy |
7909 | 519 |
else thy; |
520 |
||
7932 | 521 |
val kill_theory = ThyInfo.if_known_thy ThyInfo.remove_thy; |
6688 | 522 |
|
7103 | 523 |
|
8804 | 524 |
fun bg_theory (name, parents, files) int = gen_begin_theory int name parents files; |
6331 | 525 |
fun en_theory thy = (end_theory thy; ()); |
526 |
||
16425
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
527 |
fun theory spec = |
2427be27cc60
accomodate identification of type Sign.sg and theory;
wenzelm
parents:
16372
diff
changeset
|
528 |
Toplevel.init_theory (bg_theory spec) en_theory (kill_theory o Context.theory_name); |
6246 | 529 |
|
530 |
||
531 |
(* context switch *) |
|
532 |
||
7960 | 533 |
fun fetch_context f x = |
15531 | 534 |
(case Context.pass NONE f x of |
535 |
((), NONE) => raise Toplevel.UNDEF |
|
536 |
| ((), SOME thy) => thy); |
|
7960 | 537 |
|
538 |
fun init_context f x = Toplevel.init_theory (fn _ => fetch_context f x) (K ()) (K ()); |
|
7953 | 539 |
|
540 |
val context = init_context (ThyInfo.quiet_update_thy true); |
|
6246 | 541 |
|
5830 | 542 |
end; |