10 signature BASIC_THEORY = |
10 signature BASIC_THEORY = |
11 sig |
11 sig |
12 type theory |
12 type theory |
13 exception THEORY of string * theory list |
13 exception THEORY of string * theory list |
14 val rep_theory : theory -> |
14 val rep_theory : theory -> |
15 {sign: Sign.sg, oraopt: (Sign.sg * exn -> term) option, |
15 {sign: Sign.sg, |
16 new_axioms: term Symtab.table, parents: theory list} |
16 new_axioms: term Symtab.table, |
|
17 oracles: ((Sign.sg * exn -> term) * stamp) Symtab.table, |
|
18 parents: theory list} |
17 val sign_of : theory -> Sign.sg |
19 val sign_of : theory -> Sign.sg |
18 val syn_of : theory -> Syntax.syntax |
20 val syn_of : theory -> Syntax.syntax |
19 val stamps_of_thy : theory -> string ref list |
21 val stamps_of_thy : theory -> string ref list |
20 val parents_of : theory -> theory list |
22 val parents_of : theory -> theory list |
21 val subthy : theory * theory -> bool |
23 val subthy : theory * theory -> bool |
63 (string * string * (string -> string * int)) list -> theory -> theory |
67 (string * string * (string -> string * int)) list -> theory -> theory |
64 val add_trrules : (string * string)Syntax.trrule list -> theory -> theory |
68 val add_trrules : (string * string)Syntax.trrule list -> theory -> theory |
65 val add_trrules_i : Syntax.ast Syntax.trrule list -> theory -> theory |
69 val add_trrules_i : Syntax.ast Syntax.trrule list -> theory -> theory |
66 val add_axioms : (xstring * string) list -> theory -> theory |
70 val add_axioms : (xstring * string) list -> theory -> theory |
67 val add_axioms_i : (xstring * term) list -> theory -> theory |
71 val add_axioms_i : (xstring * term) list -> theory -> theory |
|
72 val add_oracle : string * (Sign.sg * exn -> term) -> theory -> theory |
68 val add_defs : (xstring * string) list -> theory -> theory |
73 val add_defs : (xstring * string) list -> theory -> theory |
69 val add_defs_i : (xstring * term) list -> theory -> theory |
74 val add_defs_i : (xstring * term) list -> theory -> theory |
70 val add_path : string -> theory -> theory |
75 val add_path : string -> theory -> theory |
71 val add_space : string * xstring list -> theory -> theory |
76 val add_space : string * string list -> theory -> theory |
72 val add_name : string -> theory -> theory |
77 val add_name : string -> theory -> theory |
73 |
|
74 val set_oracle : (Sign.sg * exn -> term) -> theory -> theory |
|
75 |
|
76 val merge_thy_list : bool -> theory list -> theory |
78 val merge_thy_list : bool -> theory list -> theory |
77 end; |
79 end; |
78 |
80 |
79 |
81 |
80 structure Theory: THEORY = |
82 structure Theory: THEORY = |
110 val eq_thy = Sign.eq_sg o pairself sign_of; |
112 val eq_thy = Sign.eq_sg o pairself sign_of; |
111 |
113 |
112 |
114 |
113 (* the Pure theories *) |
115 (* the Pure theories *) |
114 |
116 |
115 val proto_pure_thy = |
117 fun make_thy sign parents = |
116 Theory {sign = Sign.proto_pure, oraopt = None, |
118 Theory {sign = sign, new_axioms = Symtab.null, |
117 new_axioms = Symtab.null, parents = []}; |
119 oracles = Symtab.null, parents = parents}; |
118 |
120 |
119 val pure_thy = |
121 val proto_pure_thy = make_thy Sign.proto_pure []; |
120 Theory {sign = Sign.pure, oraopt = None, |
122 val pure_thy = make_thy Sign.pure [proto_pure_thy]; |
121 new_axioms = Symtab.null, parents = []}; |
123 val cpure_thy = make_thy Sign.cpure [proto_pure_thy]; |
122 |
|
123 val cpure_thy = |
|
124 Theory {sign = Sign.cpure, oraopt = None, |
|
125 new_axioms = Symtab.null, parents = []}; |
|
126 |
124 |
127 |
125 |
128 |
126 |
129 (** extend theory **) |
127 (** extend theory **) |
|
128 |
|
129 (*name space kinds*) |
|
130 val thmK = "thm"; |
|
131 val oracleK = "oracle"; |
|
132 |
|
133 |
|
134 (* extend logical part of a theory *) |
130 |
135 |
131 fun err_dup_axms names = |
136 fun err_dup_axms names = |
132 error ("Duplicate axiom name(s) " ^ commas_quote names); |
137 error ("Duplicate axiom name(s) " ^ commas_quote names); |
133 |
138 |
134 fun ext_thy (thy as Theory {sign, oraopt, new_axioms, parents}) |
139 fun err_dup_oras names = |
135 sign1 new_axms = |
140 error ("Duplicate oracles " ^ commas_quote names); |
136 let |
141 |
|
142 |
|
143 fun ext_thy thy sign' new_axms new_oras = |
|
144 let |
|
145 val Theory {sign, new_axioms, oracles, parents} = thy; |
137 val draft = Sign.is_draft sign; |
146 val draft = Sign.is_draft sign; |
138 val new_axioms1 = |
147 val new_axioms' = |
139 Symtab.extend_new (if draft then new_axioms else Symtab.null, new_axms) |
148 Symtab.extend_new (if draft then new_axioms else Symtab.null, new_axms) |
140 handle Symtab.DUPS names => err_dup_axms names; |
149 handle Symtab.DUPS names => err_dup_axms names; |
141 val parents1 = if draft then parents else [thy]; |
150 val oracles' = |
142 in |
151 Symtab.extend_new (oracles, new_oras) |
143 Theory {sign = sign1, oraopt = oraopt, |
152 handle Symtab.DUPS names => err_dup_oras names; |
144 new_axioms = new_axioms1, parents = parents1} |
153 val parents' = if draft then parents else [thy]; |
|
154 in |
|
155 Theory {sign = sign', new_axioms = new_axioms', |
|
156 oracles = oracles', parents = parents'} |
145 end; |
157 end; |
146 |
158 |
147 |
159 |
148 (* extend signature of a theory *) |
160 (* extend signature of a theory *) |
149 |
161 |
150 fun ext_sg extfun decls (thy as Theory {sign, ...}) = |
162 fun ext_sg extfun decls (thy as Theory {sign, ...}) = |
151 ext_thy thy (extfun decls sign) []; |
163 ext_thy thy (extfun decls sign) [] []; |
152 |
164 |
153 val add_classes = ext_sg Sign.add_classes; |
165 val add_classes = ext_sg Sign.add_classes; |
154 val add_classes_i = ext_sg Sign.add_classes_i; |
166 val add_classes_i = ext_sg Sign.add_classes_i; |
155 val add_classrel = ext_sg Sign.add_classrel; |
167 val add_classrel = ext_sg Sign.add_classrel; |
156 val add_classrel_i = ext_sg Sign.add_classrel_i; |
168 val add_classrel_i = ext_sg Sign.add_classrel_i; |
197 end |
212 end |
198 handle ERROR => err_in_axm name; |
213 handle ERROR => err_in_axm name; |
199 |
214 |
200 (*Some duplication of code with read_def_cterm*) |
215 (*Some duplication of code with read_def_cterm*) |
201 fun read_axm sg (name, str) = |
216 fun read_axm sg (name, str) = |
202 let val ts = Syntax.read (#syn (Sign.rep_sg sg)) propT str; |
217 let |
203 val (_, t, _) = |
218 val ts = Syntax.read (#syn (Sign.rep_sg sg)) propT str; |
204 Sign.infer_types sg (K None) (K None) [] true (ts,propT); |
219 val (_, t, _) = |
|
220 Sign.infer_types sg (K None) (K None) [] true (ts, propT); |
205 in cert_axm sg (name,t) end |
221 in cert_axm sg (name,t) end |
206 handle ERROR => err_in_axm name; |
222 handle ERROR => err_in_axm name; |
207 |
223 |
208 fun inferT_axm sg (name, pre_tm) = |
224 fun inferT_axm sg (name, pre_tm) = |
209 let val t = #2(Sign.infer_types sg (K None) (K None) [] true |
225 let |
210 ([pre_tm], propT)) |
226 val (_, t, _) = |
211 in (name, no_vars t) end |
227 Sign.infer_types sg (K None) (K None) [] true ([pre_tm], propT); |
|
228 in (name, no_vars t) end |
212 handle ERROR => err_in_axm name; |
229 handle ERROR => err_in_axm name; |
213 |
230 |
214 |
231 |
215 (* extend axioms of a theory *) |
232 (* extend axioms of a theory *) |
216 |
233 |
217 fun ext_axms prep_axm axms (thy as Theory {sign, ...}) = |
234 fun ext_axms prep_axm raw_axms (thy as Theory {sign, ...}) = |
218 let |
235 let |
219 val sign1 = Sign.make_draft sign; |
236 val raw_axms' = map (apfst (Sign.full_name sign)) raw_axms; |
220 val axioms = map (apsnd (Term.compress_term o Logic.varify) o |
237 val axioms = |
221 prep_axm sign) |
238 map (apsnd (Term.compress_term o Logic.varify) o prep_axm sign) raw_axms'; |
222 axms; |
239 val sign' = Sign.add_space (thmK, map fst axioms) sign; |
223 in |
240 in |
224 ext_thy thy sign1 axioms |
241 ext_thy thy sign' axioms [] |
225 end; |
242 end; |
226 |
243 |
227 val add_axioms = ext_axms read_axm; |
244 val add_axioms = ext_axms read_axm; |
228 val add_axioms_i = ext_axms cert_axm; |
245 val add_axioms_i = ext_axms cert_axm; |
|
246 |
|
247 |
|
248 (* add oracle **) |
|
249 |
|
250 fun add_oracle (raw_name, oracle) (thy as Theory {sign, ...}) = |
|
251 let |
|
252 val name = Sign.full_name sign raw_name; |
|
253 val sign' = Sign.add_space (oracleK, [name]) sign; |
|
254 in |
|
255 ext_thy thy sign' [] [(name, (oracle, stamp ()))] |
|
256 end; |
229 |
257 |
230 |
258 |
231 |
259 |
232 (** add constant definitions **) |
260 (** add constant definitions **) |
233 |
261 |
307 end; |
335 end; |
308 |
336 |
309 |
337 |
310 (* check_defn *) |
338 (* check_defn *) |
311 |
339 |
312 fun err_in_defn name msg = |
340 fun err_in_defn sg name msg = |
313 (writeln msg; error ("The error(s) above occurred in definition " ^ quote name)); |
341 (writeln msg; error ("The error(s) above occurred in definition " ^ |
|
342 quote (Sign.full_name sg name))); |
314 |
343 |
315 fun check_defn sign (axms, (name, tm)) = |
344 fun check_defn sign (axms, (name, tm)) = |
316 let |
345 let |
317 fun show_const (c, ty) = quote (Pretty.string_of (Pretty.block |
346 fun show_const (c, ty) = quote (Pretty.string_of (Pretty.block |
318 [Pretty.str (c ^ " ::"), Pretty.brk 1, Sign.pretty_typ sign ty])); |
347 [Pretty.str (c ^ " ::"), Pretty.brk 1, Sign.pretty_typ sign ty])); |
319 |
348 |
320 fun show_defn c (dfn, ty') = show_const (c, ty') ^ " in " ^ dfn; |
349 fun show_defn c (dfn, ty') = show_const (c, ty') ^ " in " ^ dfn; |
321 fun show_defns c = cat_lines o map (show_defn c); |
350 fun show_defns c = cat_lines o map (show_defn c); |
322 |
351 |
323 val (c, ty) = dest_defn tm |
352 val (c, ty) = dest_defn tm |
324 handle TERM (msg, _) => err_in_defn name msg; |
353 handle TERM (msg, _) => err_in_defn sign name msg; |
325 val defns = clash_defns (c, ty) axms; |
354 val defns = clash_defns (c, ty) axms; |
326 in |
355 in |
327 if not (null defns) then |
356 if not (null defns) then |
328 err_in_defn name ("Definition of " ^ show_const (c, ty) ^ |
357 err_in_defn sign name ("Definition of " ^ show_const (c, ty) ^ |
329 "\nclashes with " ^ show_defns c defns) |
358 "\nclashes with " ^ show_defns c defns) |
330 else (name, tm) :: axms |
359 else (name, tm) :: axms |
331 end; |
360 end; |
332 |
361 |
333 |
362 |
345 val add_defs_i = ext_defns cert_axm; |
374 val add_defs_i = ext_defns cert_axm; |
346 val add_defs = ext_defns read_axm; |
375 val add_defs = ext_defns read_axm; |
347 |
376 |
348 |
377 |
349 |
378 |
350 (** Set oracle of theory **) |
|
351 |
|
352 (* FIXME support more than one oracle (!?) *) |
|
353 |
|
354 fun set_oracle oracle |
|
355 (thy as Theory {sign, oraopt = None, new_axioms, parents}) = |
|
356 if Sign.is_draft sign then |
|
357 Theory {sign = sign, |
|
358 oraopt = Some oracle, |
|
359 new_axioms = new_axioms, |
|
360 parents = parents} |
|
361 else raise THEORY ("Can only set oracle of a draft", [thy]) |
|
362 | set_oracle _ thy = raise THEORY ("Oracle already set", [thy]); |
|
363 |
|
364 |
|
365 |
|
366 (** merge theories **) |
379 (** merge theories **) |
367 |
380 |
368 fun merge_thy_list mk_draft thys = |
381 fun merge_thy_list mk_draft thys = |
369 let |
382 let |
370 fun is_union thy = forall (fn t => subthy (t, thy)) thys; |
383 fun is_union thy = forall (fn t => subthy (t, thy)) thys; |
371 val is_draft = Sign.is_draft o sign_of; |
384 val is_draft = Sign.is_draft o sign_of; |
372 |
385 |
373 fun add_sign (sg, Theory {sign, ...}) = |
386 fun add_sign (sg, Theory {sign, ...}) = |
374 Sign.merge (sg, sign) handle TERM (msg, _) => error msg; |
387 Sign.merge (sg, sign) handle TERM (msg, _) => error msg; |
375 in |
388 |
376 case (find_first is_union thys, exists is_draft thys) of |
389 fun oracles_of (Theory {oracles, ...}) = Symtab.dest oracles; |
|
390 fun eq_ora ((_, (_, s1: stamp)), (_, (_, s2))) = s1 = s2; |
|
391 val all_oracles = |
|
392 Symtab.make (gen_distinct eq_ora (flat (map oracles_of thys))) |
|
393 handle Symtab.DUPS names => err_dup_oras names; |
|
394 in |
|
395 (case (find_first is_union thys, exists is_draft thys) of |
377 (Some thy, _) => thy |
396 (Some thy, _) => thy |
378 | (None, true) => raise THEORY ("Illegal merge of draft theories", thys) |
397 | (None, true) => raise THEORY ("Illegal merge of draft theories", thys) |
379 | (None, false) => Theory { |
398 | (None, false) => Theory { |
380 sign = |
399 sign = |
381 (if mk_draft then Sign.make_draft else I) |
400 (if mk_draft then Sign.make_draft else I) |
382 (foldl add_sign (Sign.proto_pure, thys)), |
401 (foldl add_sign (Sign.proto_pure, thys)), |
383 oraopt = None, |
|
384 new_axioms = Symtab.null, |
402 new_axioms = Symtab.null, |
385 parents = thys} |
403 oracles = all_oracles, |
|
404 parents = thys}) |
386 end; |
405 end; |
387 |
406 |
388 fun merge_theories (thy1, thy2) = merge_thy_list false [thy1, thy2]; |
407 fun merge_theories (thy1, thy2) = merge_thy_list false [thy1, thy2]; |
389 |
408 |
390 |
409 |