14516
|
1 |
signature ProofKernel =
|
|
2 |
sig
|
|
3 |
type hol_type
|
|
4 |
type tag
|
|
5 |
type term
|
|
6 |
type thm
|
|
7 |
type ('a,'b) subst
|
|
8 |
|
|
9 |
type proof_info
|
|
10 |
datatype proof = Proof of proof_info * proof_content
|
|
11 |
and proof_content
|
|
12 |
= PRefl of term
|
|
13 |
| PInstT of proof * (hol_type,hol_type) subst
|
|
14 |
| PSubst of proof list * term * proof
|
|
15 |
| PAbs of proof * term
|
|
16 |
| PDisch of proof * term
|
|
17 |
| PMp of proof * proof
|
|
18 |
| PHyp of term
|
|
19 |
| PAxm of string * term
|
|
20 |
| PDef of string * string * term
|
|
21 |
| PTmSpec of string * string list * proof
|
|
22 |
| PTyDef of string * string * proof
|
|
23 |
| PTyIntro of string * string * string * string * term * term * proof
|
|
24 |
| POracle of tag * term list * term
|
|
25 |
| PDisk
|
|
26 |
| PSpec of proof * term
|
|
27 |
| PInst of proof * (term,term) subst
|
|
28 |
| PGen of proof * term
|
|
29 |
| PGenAbs of proof * term option * term list
|
|
30 |
| PImpAS of proof * proof
|
|
31 |
| PSym of proof
|
|
32 |
| PTrans of proof * proof
|
|
33 |
| PComb of proof * proof
|
|
34 |
| PEqMp of proof * proof
|
|
35 |
| PEqImp of proof
|
|
36 |
| PExists of proof * term * term
|
|
37 |
| PChoose of term * proof * proof
|
|
38 |
| PConj of proof * proof
|
|
39 |
| PConjunct1 of proof
|
|
40 |
| PConjunct2 of proof
|
|
41 |
| PDisj1 of proof * term
|
|
42 |
| PDisj2 of proof * term
|
|
43 |
| PDisjCases of proof * proof * proof
|
|
44 |
| PNotI of proof
|
|
45 |
| PNotE of proof
|
|
46 |
| PContr of proof * term
|
|
47 |
|
|
48 |
exception PK of string * string
|
|
49 |
|
|
50 |
val get_proof_dir: string -> theory -> string
|
|
51 |
val debug : bool ref
|
|
52 |
val disk_info_of : proof -> (string * string) option
|
|
53 |
val set_disk_info_of : proof -> string -> string -> unit
|
|
54 |
val mk_proof : proof_content -> proof
|
|
55 |
val content_of : proof -> proof_content
|
|
56 |
val import_proof : string -> string -> theory -> (theory -> term) option * (theory -> proof)
|
|
57 |
|
|
58 |
val rewrite_hol4_term: Term.term -> theory -> Thm.thm
|
|
59 |
|
|
60 |
val type_of : term -> hol_type
|
|
61 |
|
|
62 |
val get_thm : string -> string -> theory -> (theory * thm option)
|
|
63 |
val get_def : string -> string -> term -> theory -> (theory * thm option)
|
|
64 |
val get_axiom: string -> string -> theory -> (theory * thm option)
|
|
65 |
|
|
66 |
val store_thm : string -> string -> thm -> theory -> theory * thm
|
|
67 |
|
|
68 |
val to_isa_thm : thm -> (term * term) list * Thm.thm
|
|
69 |
val to_isa_term: term -> Term.term
|
|
70 |
|
|
71 |
val REFL : term -> theory -> theory * thm
|
|
72 |
val ASSUME : term -> theory -> theory * thm
|
|
73 |
val INST_TYPE : (hol_type,hol_type) subst -> thm -> theory -> theory * thm
|
|
74 |
val INST : (term,term)subst -> thm -> theory -> theory * thm
|
|
75 |
val EQ_MP : thm -> thm -> theory -> theory * thm
|
|
76 |
val EQ_IMP_RULE : thm -> theory -> theory * thm
|
|
77 |
val SUBST : thm list -> term -> thm -> theory -> theory * thm
|
|
78 |
val DISJ_CASES : thm -> thm -> thm -> theory -> theory * thm
|
|
79 |
val DISJ1: thm -> term -> theory -> theory * thm
|
|
80 |
val DISJ2: term -> thm -> theory -> theory * thm
|
|
81 |
val IMP_ANTISYM: thm -> thm -> theory -> theory * thm
|
|
82 |
val SYM : thm -> theory -> theory * thm
|
|
83 |
val MP : thm -> thm -> theory -> theory * thm
|
|
84 |
val GEN : term -> thm -> theory -> theory * thm
|
|
85 |
val CHOOSE : term -> thm -> thm -> theory -> theory * thm
|
|
86 |
val EXISTS : term -> term -> thm -> theory -> theory * thm
|
|
87 |
val ABS : term -> thm -> theory -> theory * thm
|
|
88 |
val GEN_ABS : term option -> term list -> thm -> theory -> theory * thm
|
|
89 |
val TRANS : thm -> thm -> theory -> theory * thm
|
|
90 |
val CCONTR : term -> thm -> theory -> theory * thm
|
|
91 |
val CONJ : thm -> thm -> theory -> theory * thm
|
|
92 |
val CONJUNCT1: thm -> theory -> theory * thm
|
|
93 |
val CONJUNCT2: thm -> theory -> theory * thm
|
|
94 |
val NOT_INTRO: thm -> theory -> theory * thm
|
|
95 |
val NOT_ELIM : thm -> theory -> theory * thm
|
|
96 |
val SPEC : term -> thm -> theory -> theory * thm
|
|
97 |
val COMB : thm -> thm -> theory -> theory * thm
|
|
98 |
val DISCH: term -> thm -> theory -> theory * thm
|
|
99 |
|
|
100 |
val type_introduction: string -> string -> string -> string -> string -> term * term -> thm -> theory -> theory * thm
|
|
101 |
|
|
102 |
val new_definition : string -> string -> term -> theory -> theory * thm
|
|
103 |
val new_specification : string -> string -> string list -> thm -> theory -> theory * thm
|
|
104 |
val new_type_definition : string -> string -> string -> thm -> theory -> theory * thm
|
|
105 |
val new_axiom : string -> term -> theory -> theory * thm
|
|
106 |
|
|
107 |
end
|
|
108 |
|
|
109 |
structure ProofKernel :> ProofKernel =
|
|
110 |
struct
|
|
111 |
type hol_type = Term.typ
|
|
112 |
type term = Term.term
|
|
113 |
datatype tag = Tag of string list
|
|
114 |
type ('a,'b) subst = ('a * 'b) list
|
|
115 |
datatype thm = HOLThm of (Term.term * Term.term) list * Thm.thm
|
|
116 |
|
|
117 |
datatype proof_info
|
|
118 |
= Info of {disk_info: (string * string) option ref}
|
|
119 |
|
|
120 |
datatype proof = Proof of proof_info * proof_content
|
|
121 |
and proof_content
|
|
122 |
= PRefl of term
|
|
123 |
| PInstT of proof * (hol_type,hol_type) subst
|
|
124 |
| PSubst of proof list * term * proof
|
|
125 |
| PAbs of proof * term
|
|
126 |
| PDisch of proof * term
|
|
127 |
| PMp of proof * proof
|
|
128 |
| PHyp of term
|
|
129 |
| PAxm of string * term
|
|
130 |
| PDef of string * string * term
|
|
131 |
| PTmSpec of string * string list * proof
|
|
132 |
| PTyDef of string * string * proof
|
|
133 |
| PTyIntro of string * string * string * string * term * term * proof
|
|
134 |
| POracle of tag * term list * term
|
|
135 |
| PDisk
|
|
136 |
| PSpec of proof * term
|
|
137 |
| PInst of proof * (term,term) subst
|
|
138 |
| PGen of proof * term
|
|
139 |
| PGenAbs of proof * term option * term list
|
|
140 |
| PImpAS of proof * proof
|
|
141 |
| PSym of proof
|
|
142 |
| PTrans of proof * proof
|
|
143 |
| PComb of proof * proof
|
|
144 |
| PEqMp of proof * proof
|
|
145 |
| PEqImp of proof
|
|
146 |
| PExists of proof * term * term
|
|
147 |
| PChoose of term * proof * proof
|
|
148 |
| PConj of proof * proof
|
|
149 |
| PConjunct1 of proof
|
|
150 |
| PConjunct2 of proof
|
|
151 |
| PDisj1 of proof * term
|
|
152 |
| PDisj2 of proof * term
|
|
153 |
| PDisjCases of proof * proof * proof
|
|
154 |
| PNotI of proof
|
|
155 |
| PNotE of proof
|
|
156 |
| PContr of proof * term
|
|
157 |
|
|
158 |
exception PK of string * string
|
|
159 |
fun ERR f mesg = PK (f,mesg)
|
|
160 |
|
|
161 |
fun print_exn e =
|
|
162 |
case e of
|
|
163 |
PK (m,s) => (writeln ("PK (" ^ m ^ "): " ^ s); raise e)
|
|
164 |
| _ => Goals.print_exn e
|
|
165 |
|
|
166 |
(* Compatibility. *)
|
|
167 |
|
|
168 |
fun quote s = "\"" ^ s ^ "\""
|
|
169 |
|
|
170 |
fun alphanum str = case String.explode str of
|
|
171 |
first::rest => Char.isAlpha first andalso forall (fn c => Char.isAlphaNum c orelse c = #"_") rest
|
|
172 |
| _ => error "ProofKernel.alphanum: Empty constname??"
|
|
173 |
|
|
174 |
fun mk_syn c = if alphanum c then NoSyn else Syntax.literal c
|
|
175 |
|
|
176 |
val keywords = ["open"]
|
|
177 |
fun quotename c = if alphanum c andalso not (c mem keywords) then c else quote c
|
|
178 |
|
|
179 |
fun smart_string_of_cterm ct =
|
|
180 |
let
|
|
181 |
val {sign,t,T,...} = rep_cterm ct
|
|
182 |
(* Hack to avoid parse errors with Trueprop *)
|
|
183 |
val ct = (cterm_of sign (HOLogic.dest_Trueprop t)
|
|
184 |
handle TERM _ => ct)
|
|
185 |
fun match cu = t aconv (term_of cu)
|
|
186 |
fun G 0 = I
|
|
187 |
| G 1 = Library.setmp show_types true
|
|
188 |
| G 2 = Library.setmp show_all_types true
|
|
189 |
| G _ = error ("ProofKernel.smart_string_of_cterm internal error: " ^ (G 2 string_of_cterm ct))
|
|
190 |
fun F sh_br n =
|
|
191 |
let
|
|
192 |
val str = Library.setmp show_brackets sh_br (G n string_of_cterm) ct
|
|
193 |
val cu = transform_error (read_cterm sign) (str,T)
|
|
194 |
in
|
|
195 |
if match cu
|
|
196 |
then quote str
|
|
197 |
else F false (n+1)
|
|
198 |
end
|
|
199 |
handle ERROR_MESSAGE mesg =>
|
|
200 |
if String.isPrefix "Ambiguous" mesg andalso
|
|
201 |
not sh_br
|
|
202 |
then F true n
|
|
203 |
else F false (n+1)
|
|
204 |
in
|
|
205 |
transform_error (Library.setmp Syntax.ambiguity_is_error true (F false)) 0
|
|
206 |
end
|
|
207 |
handle ERROR_MESSAGE mesg =>
|
|
208 |
(writeln "Exception in smart_string_of_cterm!";
|
|
209 |
writeln mesg;
|
|
210 |
quote (string_of_cterm ct))
|
|
211 |
|
|
212 |
val smart_string_of_thm = smart_string_of_cterm o cprop_of
|
|
213 |
|
|
214 |
fun prth th = writeln ((Library.setmp print_mode [] string_of_thm) th)
|
|
215 |
fun prc ct = writeln ((Library.setmp print_mode [] string_of_cterm) ct)
|
|
216 |
val prin = Library.setmp print_mode [] prin
|
|
217 |
fun pth (HOLThm(ren,thm)) =
|
|
218 |
let
|
|
219 |
val _ = writeln "Renaming:"
|
|
220 |
val _ = app (fn(v,w) => (prin v; writeln " -->"; prin w)) ren
|
|
221 |
val _ = prth thm
|
|
222 |
in
|
|
223 |
()
|
|
224 |
end
|
|
225 |
|
|
226 |
fun disk_info_of (Proof(Info{disk_info,...},_)) = !disk_info
|
|
227 |
fun mk_proof p = Proof(Info{disk_info = ref None},p)
|
|
228 |
fun content_of (Proof(_,p)) = p
|
|
229 |
|
|
230 |
fun set_disk_info_of (Proof(Info{disk_info,...},_)) thyname thmname =
|
|
231 |
disk_info := Some(thyname,thmname)
|
|
232 |
|
|
233 |
structure Lib =
|
|
234 |
struct
|
|
235 |
fun wrap b e s = String.concat[b,s,e]
|
|
236 |
|
|
237 |
fun assoc x =
|
|
238 |
let
|
|
239 |
fun F [] = raise PK("Lib.assoc","Not found")
|
|
240 |
| F ((x',y)::rest) = if x = x'
|
|
241 |
then y
|
|
242 |
else F rest
|
|
243 |
in
|
|
244 |
F
|
|
245 |
end
|
|
246 |
fun i mem L =
|
|
247 |
let fun itr [] = false
|
|
248 |
| itr (a::rst) = i=a orelse itr rst
|
|
249 |
in itr L end;
|
|
250 |
|
|
251 |
fun insert i L = if i mem L then L else i::L
|
|
252 |
|
|
253 |
fun mk_set [] = []
|
|
254 |
| mk_set (a::rst) = insert a (mk_set rst)
|
|
255 |
|
|
256 |
fun [] union S = S
|
|
257 |
| S union [] = S
|
|
258 |
| (a::rst) union S2 = rst union (insert a S2)
|
|
259 |
|
|
260 |
fun implode_subst [] = []
|
|
261 |
| implode_subst (x::r::rest) = ((x,r)::(implode_subst rest))
|
|
262 |
| implode_subst _ = raise ERR "implode_subst" "malformed substitution list"
|
|
263 |
|
|
264 |
fun apboth f (x,y) = (f x,f y)
|
|
265 |
end
|
|
266 |
open Lib
|
|
267 |
|
|
268 |
structure Tag =
|
|
269 |
struct
|
|
270 |
val empty_tag = Tag []
|
|
271 |
fun read name = Tag [name]
|
|
272 |
fun merge (Tag tag1) (Tag tag2) = Tag (Lib.union(tag1,tag2))
|
|
273 |
end
|
|
274 |
|
|
275 |
(* Acutal code. *)
|
|
276 |
|
|
277 |
fun get_segment thyname l = (Lib.assoc "s" l
|
|
278 |
handle PK _ => thyname)
|
|
279 |
val get_name = Lib.assoc "n"
|
|
280 |
|
|
281 |
local
|
|
282 |
open LazyScan
|
|
283 |
infix 7 |-- --|
|
|
284 |
infix 5 :-- -- ^^
|
|
285 |
infix 3 >>
|
|
286 |
infix 0 ||
|
|
287 |
in
|
|
288 |
exception XML of string
|
|
289 |
|
|
290 |
datatype xml = Elem of string * (string * string) list * xml list
|
|
291 |
datatype XMLtype = XMLty of xml | FullType of hol_type
|
|
292 |
datatype XMLterm = XMLtm of xml | FullTerm of term
|
|
293 |
|
|
294 |
fun pair x y = (x,y)
|
|
295 |
|
|
296 |
fun scan_id toks =
|
|
297 |
let
|
|
298 |
val (x,toks2) = one Char.isAlpha toks
|
|
299 |
val (xs,toks3) = any Char.isAlphaNum toks2
|
|
300 |
in
|
|
301 |
(String.implode (x::xs),toks3)
|
|
302 |
end
|
|
303 |
|
|
304 |
fun scan_string str c =
|
|
305 |
let
|
|
306 |
fun F [] toks = (c,toks)
|
|
307 |
| F (c::cs) toks =
|
|
308 |
case LazySeq.getItem toks of
|
|
309 |
Some(c',toks') =>
|
|
310 |
if c = c'
|
|
311 |
then F cs toks'
|
|
312 |
else raise SyntaxError
|
|
313 |
| None => raise SyntaxError
|
|
314 |
in
|
|
315 |
F (String.explode str)
|
|
316 |
end
|
|
317 |
|
|
318 |
local
|
|
319 |
val scan_entity =
|
|
320 |
(scan_string "amp;" #"&")
|
|
321 |
|| scan_string "quot;" #"\""
|
|
322 |
|| scan_string "gt;" #">"
|
|
323 |
|| scan_string "lt;" #"<"
|
|
324 |
in
|
|
325 |
fun scan_nonquote toks =
|
|
326 |
case LazySeq.getItem toks of
|
|
327 |
Some (c,toks') =>
|
|
328 |
(case c of
|
|
329 |
#"\"" => raise SyntaxError
|
|
330 |
| #"&" => scan_entity toks'
|
|
331 |
| c => (c,toks'))
|
|
332 |
| None => raise SyntaxError
|
|
333 |
end
|
|
334 |
|
|
335 |
val scan_string = $$ #"\"" |-- repeat scan_nonquote --| $$ #"\"" >>
|
|
336 |
String.implode
|
|
337 |
|
|
338 |
val scan_attribute = scan_id -- $$ #"=" |-- scan_string
|
|
339 |
|
|
340 |
val scan_start_of_tag = $$ #"<" |-- scan_id --
|
|
341 |
repeat ($$ #" " |-- scan_attribute)
|
|
342 |
|
|
343 |
val scan_end_of_tag = $$ #"/" |-- $$ #">" |-- succeed []
|
|
344 |
val scan_end_tag = $$ #"<" |-- $$ #"/" |-- scan_id --| $$ #">"
|
|
345 |
|
|
346 |
fun scan_children id = $$ #">" |-- repeat scan_tag -- scan_end_tag >>
|
|
347 |
(fn (chldr,id') => if id = id'
|
|
348 |
then chldr
|
|
349 |
else raise XML "Tag mismatch")
|
|
350 |
and scan_tag toks =
|
|
351 |
let
|
|
352 |
val ((id,atts),toks2) = scan_start_of_tag toks
|
|
353 |
val (chldr,toks3) = (scan_children id || scan_end_of_tag) toks2
|
|
354 |
in
|
|
355 |
(Elem (id,atts,chldr),toks3)
|
|
356 |
end
|
|
357 |
end
|
|
358 |
|
|
359 |
val type_of = Term.type_of
|
|
360 |
|
|
361 |
val boolT = Type("bool",[])
|
|
362 |
val propT = Type("prop",[])
|
|
363 |
|
|
364 |
fun mk_defeq name rhs thy =
|
|
365 |
let
|
|
366 |
val ty = type_of rhs
|
|
367 |
in
|
|
368 |
Logic.mk_equals (Const(Sign.intern_const (sign_of thy) name,ty),rhs)
|
|
369 |
end
|
|
370 |
|
|
371 |
fun mk_teq name rhs thy =
|
|
372 |
let
|
|
373 |
val ty = type_of rhs
|
|
374 |
in
|
|
375 |
HOLogic.mk_eq (Const(Sign.intern_const (sign_of thy) name,ty),rhs)
|
|
376 |
end
|
|
377 |
|
|
378 |
fun intern_const_name thyname const thy =
|
|
379 |
case get_hol4_const_mapping thyname const thy of
|
|
380 |
Some (_,cname,_) => cname
|
|
381 |
| None => (case get_hol4_const_renaming thyname const thy of
|
|
382 |
Some cname => Sign.intern_const (sign_of thy) (thyname ^ "." ^ cname)
|
|
383 |
| None => Sign.intern_const (sign_of thy) (thyname ^ "." ^ const))
|
|
384 |
|
|
385 |
fun intern_type_name thyname const thy =
|
|
386 |
case get_hol4_type_mapping thyname const thy of
|
|
387 |
Some (_,cname) => cname
|
|
388 |
| None => Sign.intern_const (sign_of thy) (thyname ^ "." ^ const)
|
|
389 |
|
|
390 |
fun mk_vartype name = TFree(name,["HOL.type"])
|
|
391 |
fun mk_thy_type thy Thy Tyop Args = Type(intern_type_name Thy Tyop thy,Args)
|
|
392 |
|
|
393 |
val mk_var = Free
|
|
394 |
|
|
395 |
fun dom_rng (Type("fun",[dom,rng])) = (dom,rng)
|
|
396 |
| dom_rng _ = raise ERR "dom_rng" "Not a functional type"
|
|
397 |
|
|
398 |
fun mk_thy_const thy Thy Name Ty = Const(intern_const_name Thy Name thy,Ty)
|
|
399 |
|
|
400 |
local
|
|
401 |
fun get_type sg thyname name =
|
|
402 |
case Sign.const_type sg name of
|
|
403 |
Some ty => ty
|
|
404 |
| None => raise ERR "get_type" (name ^ ": No such constant")
|
|
405 |
in
|
|
406 |
fun prim_mk_const thy Thy Name =
|
|
407 |
let
|
|
408 |
val name = intern_const_name Thy Name thy
|
|
409 |
val cmaps = HOL4ConstMaps.get thy
|
|
410 |
in
|
|
411 |
case StringPair.lookup(cmaps,(Thy,Name)) of
|
|
412 |
Some(_,_,Some ty) => Const(name,ty)
|
|
413 |
| _ => Const(name,get_type (sign_of thy) Thy name)
|
|
414 |
end
|
|
415 |
end
|
|
416 |
|
|
417 |
fun mk_comb(f,a) = f $ a
|
|
418 |
fun mk_abs(x,a) = Term.lambda x a
|
|
419 |
|
|
420 |
(* Needed for HOL Light *)
|
|
421 |
fun protect_tyvarname s =
|
|
422 |
let
|
|
423 |
fun no_quest s =
|
|
424 |
if Char.contains s #"?"
|
|
425 |
then String.translate (fn #"?" => "q_" | c => Char.toString c) s
|
|
426 |
else s
|
|
427 |
fun beg_prime s =
|
|
428 |
if String.isPrefix "'" s
|
|
429 |
then s
|
|
430 |
else "'" ^ s
|
|
431 |
in
|
|
432 |
s |> no_quest |> beg_prime
|
|
433 |
end
|
|
434 |
fun protect_varname s =
|
|
435 |
let
|
|
436 |
fun no_beg_underscore s =
|
|
437 |
if String.isPrefix "_" s
|
|
438 |
then "dummy" ^ s
|
|
439 |
else s
|
|
440 |
in
|
|
441 |
s |> no_beg_underscore
|
|
442 |
end
|
|
443 |
|
|
444 |
structure TypeNet =
|
|
445 |
struct
|
|
446 |
fun get_type_from_index thy thyname types is =
|
|
447 |
case Int.fromString is of
|
|
448 |
SOME i => (case Array.sub(types,i) of
|
|
449 |
FullType ty => ty
|
|
450 |
| XMLty xty =>
|
|
451 |
let
|
|
452 |
val ty = get_type_from_xml thy thyname types xty
|
|
453 |
val _ = Array.update(types,i,FullType ty)
|
|
454 |
in
|
|
455 |
ty
|
|
456 |
end)
|
|
457 |
| NONE => raise ERR "get_type_from_index" "Bad index"
|
|
458 |
and get_type_from_xml thy thyname types =
|
|
459 |
let
|
|
460 |
fun gtfx (Elem("tyi",[("i",iS)],[])) =
|
|
461 |
get_type_from_index thy thyname types iS
|
|
462 |
| gtfx (Elem("tyc",atts,[])) =
|
|
463 |
mk_thy_type thy
|
|
464 |
(get_segment thyname atts)
|
|
465 |
(get_name atts)
|
|
466 |
[]
|
|
467 |
| gtfx (Elem("tyv",[("n",s)],[])) = mk_vartype (protect_tyvarname s)
|
|
468 |
| gtfx (Elem("tya",[],(Elem("tyc",atts,[]))::tys)) =
|
|
469 |
mk_thy_type thy
|
|
470 |
(get_segment thyname atts)
|
|
471 |
(get_name atts)
|
|
472 |
(map gtfx tys)
|
|
473 |
| gtfx _ = raise ERR "get_type" "Bad type"
|
|
474 |
in
|
|
475 |
gtfx
|
|
476 |
end
|
|
477 |
|
|
478 |
fun input_types thyname (Elem("tylist",[("i",i)],xtys)) =
|
|
479 |
let
|
|
480 |
val types = Array.array(valOf (Int.fromString i),XMLty (Elem("",[],[])))
|
|
481 |
fun IT _ [] = ()
|
|
482 |
| IT n (xty::xtys) =
|
|
483 |
(Array.update(types,n,XMLty xty);
|
|
484 |
IT (n+1) xtys)
|
|
485 |
val _ = IT 0 xtys
|
|
486 |
in
|
|
487 |
types
|
|
488 |
end
|
|
489 |
| input_types _ _ = raise ERR "input_types" "Bad type list"
|
|
490 |
end
|
|
491 |
|
|
492 |
structure TermNet =
|
|
493 |
struct
|
|
494 |
fun get_term_from_index thy thyname types terms is =
|
|
495 |
case Int.fromString is of
|
|
496 |
SOME i => (case Array.sub(terms,i) of
|
|
497 |
FullTerm tm => tm
|
|
498 |
| XMLtm xtm =>
|
|
499 |
let
|
|
500 |
val tm = get_term_from_xml thy thyname types terms xtm
|
|
501 |
val _ = Array.update(terms,i,FullTerm tm)
|
|
502 |
in
|
|
503 |
tm
|
|
504 |
end)
|
|
505 |
| NONE => raise ERR "get_term_from_index" "Bad index"
|
|
506 |
and get_term_from_xml thy thyname types terms =
|
|
507 |
let
|
|
508 |
fun get_type [] = None
|
|
509 |
| get_type [ty] = Some (TypeNet.get_type_from_xml thy thyname types ty)
|
|
510 |
| get_type _ = raise ERR "get_term" "Bad type"
|
|
511 |
|
|
512 |
fun gtfx (Elem("tmv",[("n",name),("t",tyi)],[])) =
|
|
513 |
mk_var(protect_varname name,TypeNet.get_type_from_index thy thyname types tyi)
|
|
514 |
| gtfx (Elem("tmc",atts,[])) =
|
|
515 |
let
|
|
516 |
val segment = get_segment thyname atts
|
|
517 |
val name = get_name atts
|
|
518 |
in
|
|
519 |
mk_thy_const thy segment name (TypeNet.get_type_from_index thy thyname types (Lib.assoc "t" atts))
|
|
520 |
handle PK _ => prim_mk_const thy segment name
|
|
521 |
end
|
|
522 |
| gtfx (Elem("tma",[("f",tmf),("a",tma)],[])) =
|
|
523 |
let
|
|
524 |
val f = get_term_from_index thy thyname types terms tmf
|
|
525 |
val a = get_term_from_index thy thyname types terms tma
|
|
526 |
in
|
|
527 |
mk_comb(f,a)
|
|
528 |
end
|
|
529 |
| gtfx (Elem("tml",[("x",tmx),("a",tma)],[])) =
|
|
530 |
let
|
|
531 |
val x = get_term_from_index thy thyname types terms tmx
|
|
532 |
val a = get_term_from_index thy thyname types terms tma
|
|
533 |
in
|
|
534 |
mk_abs(x,a)
|
|
535 |
end
|
|
536 |
| gtfx (Elem("tmi",[("i",iS)],[])) =
|
|
537 |
get_term_from_index thy thyname types terms iS
|
|
538 |
| gtfx (Elem(tag,_,_)) =
|
|
539 |
raise ERR "get_term" ("Not a term: "^tag)
|
|
540 |
in
|
|
541 |
gtfx
|
|
542 |
end
|
|
543 |
|
|
544 |
fun input_terms thyname types (Elem("tmlist",[("i",i)],xtms)) =
|
|
545 |
let
|
|
546 |
val terms = Array.array(valOf(Int.fromString i),XMLtm (Elem("",[],[])))
|
|
547 |
|
|
548 |
fun IT _ [] = ()
|
|
549 |
| IT n (xtm::xtms) =
|
|
550 |
(Array.update(terms,n,XMLtm xtm);
|
|
551 |
IT (n+1) xtms)
|
|
552 |
val _ = IT 0 xtms
|
|
553 |
in
|
|
554 |
terms
|
|
555 |
end
|
|
556 |
| input_terms _ _ _ = raise ERR "input_terms" "Bad term list"
|
|
557 |
end
|
|
558 |
|
|
559 |
fun get_proof_dir (thyname:string) thy =
|
|
560 |
let
|
|
561 |
val import_segment =
|
|
562 |
case get_segment2 thyname thy of
|
|
563 |
Some seg => seg
|
|
564 |
| None => get_import_segment thy
|
|
565 |
val defpath = [(getenv "ISABELLE_HOME_USER") ^ "/proofs"]
|
|
566 |
val path =
|
|
567 |
case getenv "PROOF_DIRS" of
|
|
568 |
"" => defpath
|
|
569 |
| path => (space_explode ":" path) @ defpath
|
|
570 |
fun find [] = error ("Unable to find import segment " ^ import_segment)
|
|
571 |
| find (p::ps) = (let
|
|
572 |
val dir = p ^ "/" ^ import_segment
|
|
573 |
in
|
|
574 |
if OS.FileSys.isDir dir
|
|
575 |
then dir
|
|
576 |
else find ps
|
|
577 |
end) handle OS.SysErr _ => find ps
|
|
578 |
in
|
|
579 |
find path ^ "/" ^ thyname
|
|
580 |
end
|
|
581 |
|
|
582 |
fun proof_file_name thyname thmname thy =
|
|
583 |
let
|
|
584 |
val path = get_proof_dir thyname thy
|
|
585 |
val _ = OS.FileSys.mkDir path handle OS.SysErr _ => ()
|
|
586 |
in
|
|
587 |
String.concat[path,"/",thmname,".prf"]
|
|
588 |
end
|
|
589 |
|
|
590 |
fun xml_to_proof thyname types terms prf thy =
|
|
591 |
let
|
|
592 |
val xml_to_hol_type = TypeNet.get_type_from_xml thy thyname types
|
|
593 |
val xml_to_term = TermNet.get_term_from_xml thy thyname types terms
|
|
594 |
|
|
595 |
fun index_to_term is =
|
|
596 |
TermNet.get_term_from_index thy thyname types terms is
|
|
597 |
|
|
598 |
fun x2p (Elem("prefl",[("i",is)],[])) = mk_proof (PRefl (index_to_term is))
|
|
599 |
| x2p (Elem("pinstt",[],p::lambda)) =
|
|
600 |
let
|
|
601 |
val p = x2p p
|
|
602 |
val lambda = implode_subst (map xml_to_hol_type lambda)
|
|
603 |
in
|
|
604 |
mk_proof (PInstT(p,lambda))
|
|
605 |
end
|
|
606 |
| x2p (Elem("psubst",[("i",is)],prf::prfs)) =
|
|
607 |
let
|
|
608 |
val tm = index_to_term is
|
|
609 |
val prf = x2p prf
|
|
610 |
val prfs = map x2p prfs
|
|
611 |
in
|
|
612 |
mk_proof (PSubst(prfs,tm,prf))
|
|
613 |
end
|
|
614 |
| x2p (Elem("pabs",[("i",is)],[prf])) =
|
|
615 |
let
|
|
616 |
val p = x2p prf
|
|
617 |
val t = index_to_term is
|
|
618 |
in
|
|
619 |
mk_proof (PAbs (p,t))
|
|
620 |
end
|
|
621 |
| x2p (Elem("pdisch",[("i",is)],[prf])) =
|
|
622 |
let
|
|
623 |
val p = x2p prf
|
|
624 |
val t = index_to_term is
|
|
625 |
in
|
|
626 |
mk_proof (PDisch (p,t))
|
|
627 |
end
|
|
628 |
| x2p (Elem("pmp",[],[prf1,prf2])) =
|
|
629 |
let
|
|
630 |
val p1 = x2p prf1
|
|
631 |
val p2 = x2p prf2
|
|
632 |
in
|
|
633 |
mk_proof (PMp(p1,p2))
|
|
634 |
end
|
|
635 |
| x2p (Elem("phyp",[("i",is)],[])) = mk_proof (PHyp (index_to_term is))
|
|
636 |
| x2p (Elem("paxiom",[("n",n),("i",is)],[])) =
|
|
637 |
mk_proof (PAxm(n,index_to_term is))
|
|
638 |
| x2p (Elem("pfact",atts,[])) =
|
|
639 |
let
|
|
640 |
val thyname = get_segment thyname atts
|
|
641 |
val thmname = get_name atts
|
|
642 |
val p = mk_proof PDisk
|
|
643 |
val _ = set_disk_info_of p thyname thmname
|
|
644 |
in
|
|
645 |
p
|
|
646 |
end
|
|
647 |
| x2p (Elem("pdef",[("s",seg),("n",name),("i",is)],[])) =
|
|
648 |
mk_proof (PDef(seg,name,index_to_term is))
|
|
649 |
| x2p (Elem("ptmspec",[("s",seg)],p::names)) =
|
|
650 |
let
|
|
651 |
val names = map (fn Elem("name",[("n",name)],[]) => name
|
|
652 |
| _ => raise ERR "x2p" "Bad proof (ptmspec)") names
|
|
653 |
in
|
|
654 |
mk_proof (PTmSpec(seg,names,x2p p))
|
|
655 |
end
|
|
656 |
| x2p (Elem("ptyintro",[("s",seg),("n",name),("a",abs_name),("r",rep_name)],[xP,xt,p])) =
|
|
657 |
let
|
|
658 |
val P = xml_to_term xP
|
|
659 |
val t = xml_to_term xt
|
|
660 |
in
|
|
661 |
mk_proof (PTyIntro(seg,name,abs_name,rep_name,P,t,x2p p))
|
|
662 |
end
|
|
663 |
| x2p (Elem("ptydef",[("s",seg),("n",name)],[p])) =
|
|
664 |
mk_proof (PTyDef(seg,name,x2p p))
|
|
665 |
| x2p (xml as Elem("poracle",[],chldr)) =
|
|
666 |
let
|
|
667 |
val (oracles,terms) = Library.partition (fn (Elem("oracle",_,_)) => true | _ => false) chldr
|
|
668 |
val ors = map (fn (Elem("oracle",[("n",name)],[])) => name | xml => raise ERR "x2p" "bad oracle") oracles
|
|
669 |
val (c,asl) = case terms of
|
|
670 |
[] => raise ERR "x2p" "Bad oracle description"
|
|
671 |
| (hd::tl) => (hd,tl)
|
|
672 |
val tg = foldr (fn (oracle,tg) => Tag.merge (Tag.read oracle) tg) (ors,Tag.empty_tag)
|
|
673 |
in
|
|
674 |
mk_proof (POracle(tg,map xml_to_term asl,xml_to_term c))
|
|
675 |
end
|
|
676 |
| x2p (Elem("pspec",[("i",is)],[prf])) =
|
|
677 |
let
|
|
678 |
val p = x2p prf
|
|
679 |
val tm = index_to_term is
|
|
680 |
in
|
|
681 |
mk_proof (PSpec(p,tm))
|
|
682 |
end
|
|
683 |
| x2p (Elem("pinst",[],p::theta)) =
|
|
684 |
let
|
|
685 |
val p = x2p p
|
|
686 |
val theta = implode_subst (map xml_to_term theta)
|
|
687 |
in
|
|
688 |
mk_proof (PInst(p,theta))
|
|
689 |
end
|
|
690 |
| x2p (Elem("pgen",[("i",is)],[prf])) =
|
|
691 |
let
|
|
692 |
val p = x2p prf
|
|
693 |
val tm = index_to_term is
|
|
694 |
in
|
|
695 |
mk_proof (PGen(p,tm))
|
|
696 |
end
|
|
697 |
| x2p (Elem("pgenabs",[],prf::tms)) =
|
|
698 |
let
|
|
699 |
val p = x2p prf
|
|
700 |
val tml = map xml_to_term tms
|
|
701 |
in
|
|
702 |
mk_proof (PGenAbs(p,None,tml))
|
|
703 |
end
|
|
704 |
| x2p (Elem("pgenabs",[("i",is)],prf::tms)) =
|
|
705 |
let
|
|
706 |
val p = x2p prf
|
|
707 |
val tml = map xml_to_term tms
|
|
708 |
in
|
|
709 |
mk_proof (PGenAbs(p,Some (index_to_term is),tml))
|
|
710 |
end
|
|
711 |
| x2p (Elem("pimpas",[],[prf1,prf2])) =
|
|
712 |
let
|
|
713 |
val p1 = x2p prf1
|
|
714 |
val p2 = x2p prf2
|
|
715 |
in
|
|
716 |
mk_proof (PImpAS(p1,p2))
|
|
717 |
end
|
|
718 |
| x2p (Elem("psym",[],[prf])) =
|
|
719 |
let
|
|
720 |
val p = x2p prf
|
|
721 |
in
|
|
722 |
mk_proof (PSym p)
|
|
723 |
end
|
|
724 |
| x2p (Elem("ptrans",[],[prf1,prf2])) =
|
|
725 |
let
|
|
726 |
val p1 = x2p prf1
|
|
727 |
val p2 = x2p prf2
|
|
728 |
in
|
|
729 |
mk_proof (PTrans(p1,p2))
|
|
730 |
end
|
|
731 |
| x2p (Elem("pcomb",[],[prf1,prf2])) =
|
|
732 |
let
|
|
733 |
val p1 = x2p prf1
|
|
734 |
val p2 = x2p prf2
|
|
735 |
in
|
|
736 |
mk_proof (PComb(p1,p2))
|
|
737 |
end
|
|
738 |
| x2p (Elem("peqmp",[],[prf1,prf2])) =
|
|
739 |
let
|
|
740 |
val p1 = x2p prf1
|
|
741 |
val p2 = x2p prf2
|
|
742 |
in
|
|
743 |
mk_proof (PEqMp(p1,p2))
|
|
744 |
end
|
|
745 |
| x2p (Elem("peqimp",[],[prf])) =
|
|
746 |
let
|
|
747 |
val p = x2p prf
|
|
748 |
in
|
|
749 |
mk_proof (PEqImp p)
|
|
750 |
end
|
|
751 |
| x2p (Elem("pexists",[("e",ise),("w",isw)],[prf])) =
|
|
752 |
let
|
|
753 |
val p = x2p prf
|
|
754 |
val ex = index_to_term ise
|
|
755 |
val w = index_to_term isw
|
|
756 |
in
|
|
757 |
mk_proof (PExists(p,ex,w))
|
|
758 |
end
|
|
759 |
| x2p (Elem("pchoose",[("i",is)],[prf1,prf2])) =
|
|
760 |
let
|
|
761 |
val v = index_to_term is
|
|
762 |
val p1 = x2p prf1
|
|
763 |
val p2 = x2p prf2
|
|
764 |
in
|
|
765 |
mk_proof (PChoose(v,p1,p2))
|
|
766 |
end
|
|
767 |
| x2p (Elem("pconj",[],[prf1,prf2])) =
|
|
768 |
let
|
|
769 |
val p1 = x2p prf1
|
|
770 |
val p2 = x2p prf2
|
|
771 |
in
|
|
772 |
mk_proof (PConj(p1,p2))
|
|
773 |
end
|
|
774 |
| x2p (Elem("pconjunct1",[],[prf])) =
|
|
775 |
let
|
|
776 |
val p = x2p prf
|
|
777 |
in
|
|
778 |
mk_proof (PConjunct1 p)
|
|
779 |
end
|
|
780 |
| x2p (Elem("pconjunct2",[],[prf])) =
|
|
781 |
let
|
|
782 |
val p = x2p prf
|
|
783 |
in
|
|
784 |
mk_proof (PConjunct2 p)
|
|
785 |
end
|
|
786 |
| x2p (Elem("pdisj1",[("i",is)],[prf])) =
|
|
787 |
let
|
|
788 |
val p = x2p prf
|
|
789 |
val t = index_to_term is
|
|
790 |
in
|
|
791 |
mk_proof (PDisj1 (p,t))
|
|
792 |
end
|
|
793 |
| x2p (Elem("pdisj2",[("i",is)],[prf])) =
|
|
794 |
let
|
|
795 |
val p = x2p prf
|
|
796 |
val t = index_to_term is
|
|
797 |
in
|
|
798 |
mk_proof (PDisj2 (p,t))
|
|
799 |
end
|
|
800 |
| x2p (Elem("pdisjcases",[],[prf1,prf2,prf3])) =
|
|
801 |
let
|
|
802 |
val p1 = x2p prf1
|
|
803 |
val p2 = x2p prf2
|
|
804 |
val p3 = x2p prf3
|
|
805 |
in
|
|
806 |
mk_proof (PDisjCases(p1,p2,p3))
|
|
807 |
end
|
|
808 |
| x2p (Elem("pnoti",[],[prf])) =
|
|
809 |
let
|
|
810 |
val p = x2p prf
|
|
811 |
in
|
|
812 |
mk_proof (PNotI p)
|
|
813 |
end
|
|
814 |
| x2p (Elem("pnote",[],[prf])) =
|
|
815 |
let
|
|
816 |
val p = x2p prf
|
|
817 |
in
|
|
818 |
mk_proof (PNotE p)
|
|
819 |
end
|
|
820 |
| x2p (Elem("pcontr",[("i",is)],[prf])) =
|
|
821 |
let
|
|
822 |
val p = x2p prf
|
|
823 |
val t = index_to_term is
|
|
824 |
in
|
|
825 |
mk_proof (PContr (p,t))
|
|
826 |
end
|
|
827 |
| x2p xml = raise ERR "x2p" "Bad proof"
|
|
828 |
in
|
|
829 |
x2p prf
|
|
830 |
end
|
|
831 |
|
|
832 |
fun import_proof thyname thmname thy =
|
|
833 |
let
|
|
834 |
val is = TextIO.openIn(proof_file_name thyname thmname thy)
|
|
835 |
val (proof_xml,_) = scan_tag (LazySeq.of_instream is)
|
|
836 |
val _ = TextIO.closeIn is
|
|
837 |
in
|
|
838 |
case proof_xml of
|
|
839 |
Elem("proof",[],xtypes::xterms::prf::rest) =>
|
|
840 |
let
|
|
841 |
val types = TypeNet.input_types thyname xtypes
|
|
842 |
val terms = TermNet.input_terms thyname types xterms
|
|
843 |
in
|
|
844 |
(case rest of
|
|
845 |
[] => None
|
|
846 |
| [xtm] => Some (fn thy => TermNet.get_term_from_xml thy thyname types terms xtm)
|
|
847 |
| _ => raise ERR "import_proof" "Bad argument list",
|
|
848 |
xml_to_proof thyname types terms prf)
|
|
849 |
end
|
|
850 |
| _ => raise ERR "import_proof" "Bad proof"
|
|
851 |
end
|
|
852 |
|
|
853 |
fun uniq_compose m th i st =
|
|
854 |
let
|
|
855 |
val res = bicompose false (false,th,m) i st
|
|
856 |
in
|
|
857 |
case Seq.pull res of
|
|
858 |
Some (th,rest) => (case Seq.pull rest of
|
|
859 |
Some _ => raise ERR "uniq_compose" "Not unique!"
|
|
860 |
| None => th)
|
|
861 |
| None => raise ERR "uniq_compose" "No result"
|
|
862 |
end
|
|
863 |
|
|
864 |
val reflexivity_thm = thm "refl"
|
|
865 |
val substitution_thm = thm "subst"
|
|
866 |
val mp_thm = thm "mp"
|
|
867 |
val imp_antisym_thm = thm "light_imp_as"
|
|
868 |
val disch_thm = thm "impI"
|
|
869 |
val ccontr_thm = thm "ccontr"
|
|
870 |
|
|
871 |
val meta_eq_to_obj_eq_thm = thm "meta_eq_to_obj_eq"
|
|
872 |
|
|
873 |
val gen_thm = thm "HOLallI"
|
|
874 |
val choose_thm = thm "exE"
|
|
875 |
val exists_thm = thm "exI"
|
|
876 |
val conj_thm = thm "conjI"
|
|
877 |
val conjunct1_thm = thm "conjunct1"
|
|
878 |
val conjunct2_thm = thm "conjunct2"
|
|
879 |
val spec_thm = thm "spec"
|
|
880 |
val disj_cases_thm = thm "disjE"
|
|
881 |
val disj1_thm = thm "disjI1"
|
|
882 |
val disj2_thm = thm "disjI2"
|
|
883 |
|
|
884 |
local
|
|
885 |
val th = thm "not_def"
|
|
886 |
val sg = sign_of_thm th
|
|
887 |
val pp = reflexive (cterm_of sg (Const("Trueprop",boolT-->propT)))
|
|
888 |
in
|
|
889 |
val not_elim_thm = combination pp th
|
|
890 |
end
|
|
891 |
|
|
892 |
val not_intro_thm = symmetric not_elim_thm
|
|
893 |
val abs_thm = thm "ext"
|
|
894 |
val trans_thm = thm "trans"
|
|
895 |
val symmetry_thm = thm "sym"
|
|
896 |
val transitivity_thm = thm "trans"
|
|
897 |
val eqmp_thm = thm "iffD1"
|
|
898 |
val eqimp_thm = thm "HOL4Setup.eq_imp"
|
|
899 |
val comb_thm = thm "cong"
|
|
900 |
|
|
901 |
(* Beta-eta normalizes a theorem (only the conclusion, not the *
|
|
902 |
hypotheses!) *)
|
|
903 |
|
|
904 |
fun beta_eta_thm th =
|
|
905 |
let
|
|
906 |
val th1 = Thm.equal_elim (Thm.beta_conversion true (cprop_of th)) th
|
|
907 |
val th2 = Thm.equal_elim (Thm.eta_conversion (cprop_of th1)) th1
|
|
908 |
in
|
|
909 |
th2
|
|
910 |
end
|
|
911 |
|
|
912 |
fun implies_elim_all th =
|
|
913 |
foldl (fn (th,p) => implies_elim th (assume p)) (th,cprems_of th)
|
|
914 |
|
|
915 |
fun norm_hyps th =
|
|
916 |
th |> beta_eta_thm
|
|
917 |
|> implies_elim_all
|
|
918 |
|> implies_intr_hyps
|
|
919 |
|
|
920 |
fun mk_GEN v th sg =
|
|
921 |
let
|
|
922 |
val c = HOLogic.dest_Trueprop (concl_of th)
|
|
923 |
val cv = cterm_of sg v
|
|
924 |
val lc = Term.lambda v c
|
|
925 |
val clc = Thm.cterm_of sg lc
|
|
926 |
val cvty = ctyp_of_term cv
|
|
927 |
val th1 = implies_elim_all th
|
|
928 |
val th2 = beta_eta_thm (forall_intr cv th1)
|
|
929 |
val th3 = th2 COMP (beta_eta_thm (Drule.instantiate' [Some cvty] [Some clc] gen_thm))
|
|
930 |
val c = prop_of th3
|
|
931 |
val vname = fst(dest_Free v)
|
|
932 |
val (cold,cnew) = case c of
|
|
933 |
tpc $ (Const("All",allT) $ Abs(oldname,ty,body)) =>
|
|
934 |
(Abs(oldname,dummyT,Bound 0),Abs(vname,dummyT,Bound 0))
|
|
935 |
| tpc $ (Const("All",allT) $ rest) => (tpc,tpc)
|
|
936 |
| _ => raise ERR "mk_GEN" "Unknown conclusion"
|
|
937 |
val th4 = Thm.rename_boundvars cold cnew th3
|
|
938 |
val res = implies_intr_hyps th4
|
|
939 |
in
|
|
940 |
res
|
|
941 |
end
|
|
942 |
|
|
943 |
(* rotate left k places, leaving the first j and last l premises alone
|
|
944 |
*)
|
|
945 |
|
|
946 |
fun permute_prems j k 0 th = Thm.permute_prems j k th
|
|
947 |
| permute_prems j k l th =
|
|
948 |
th |> Thm.permute_prems 0 (~l)
|
|
949 |
|> Thm.permute_prems (j+l) k
|
|
950 |
|> Thm.permute_prems 0 l
|
|
951 |
|
|
952 |
fun rearrange sg tm th =
|
|
953 |
let
|
|
954 |
val tm' = Pattern.beta_eta_contract tm
|
|
955 |
fun find [] n = permute_prems 0 1 0 (implies_intr (Thm.cterm_of sg tm) th)
|
|
956 |
| find (p::ps) n = if tm' aconv (Pattern.beta_eta_contract p)
|
|
957 |
then permute_prems n 1 0 th
|
|
958 |
else find ps (n+1)
|
|
959 |
in
|
|
960 |
find (prems_of th) 0
|
|
961 |
end
|
|
962 |
|
|
963 |
fun zip (x::xs) (y::ys) = (x,y)::(zip xs ys)
|
|
964 |
| zip [] [] = []
|
|
965 |
| zip _ _ = raise ERR "zip" "arguments not of same length"
|
|
966 |
|
|
967 |
fun mk_INST dom rng th =
|
|
968 |
th |> forall_intr_list dom
|
|
969 |
|> forall_elim_list rng
|
|
970 |
|
|
971 |
fun apply_tyinst_typ tyinst =
|
|
972 |
let
|
|
973 |
fun G (ty as TFree _) =
|
|
974 |
(case try (Lib.assoc ty) tyinst of
|
|
975 |
Some ty' => ty'
|
|
976 |
| None => ty)
|
|
977 |
| G (Type(tyname,tys)) = Type(tyname,map G tys)
|
|
978 |
| G (TVar _) = raise ERR "apply_tyinst_typ" "Scematic variable found"
|
|
979 |
in
|
|
980 |
G
|
|
981 |
end
|
|
982 |
|
|
983 |
fun apply_tyinst_term tyinst =
|
|
984 |
let
|
|
985 |
val G = apply_tyinst_typ tyinst
|
|
986 |
fun F (tm as Bound _) = tm
|
|
987 |
| F (tm as Free(vname,ty)) = Free(vname,G ty)
|
|
988 |
| F (tm as Const(vname,ty)) = Const(vname,G ty)
|
|
989 |
| F (tm1 $ tm2) = (F tm1) $ (F tm2)
|
|
990 |
| F (Abs(vname,ty,body)) = Abs(vname,G ty,F body)
|
|
991 |
| F (Var _) = raise ERR "apply_tyinst_term" "Schematic variable found"
|
|
992 |
in
|
|
993 |
F
|
|
994 |
end
|
|
995 |
|
|
996 |
fun apply_inst_term tminst =
|
|
997 |
let
|
|
998 |
fun F (tm as Bound _) = tm
|
|
999 |
| F (tm as Free _) =
|
|
1000 |
(case try (Lib.assoc tm) tminst of
|
|
1001 |
Some tm' => tm'
|
|
1002 |
| None => tm)
|
|
1003 |
| F (tm as Const _) = tm
|
|
1004 |
| F (tm1 $ tm2) = (F tm1) $ (F tm2)
|
|
1005 |
| F (Abs(vname,ty,body)) = Abs(vname,ty,F body)
|
|
1006 |
| F (Var _) = raise ERR "apply_inst_term" "Schematic variable found"
|
|
1007 |
in
|
|
1008 |
F
|
|
1009 |
end
|
|
1010 |
|
|
1011 |
val collect_vars =
|
|
1012 |
let
|
|
1013 |
fun F vars (Bound _) = vars
|
|
1014 |
| F vars (tm as Free _) =
|
|
1015 |
if tm mem vars
|
|
1016 |
then vars
|
|
1017 |
else (tm::vars)
|
|
1018 |
| F vars (Const _) = vars
|
|
1019 |
| F vars (tm1 $ tm2) = F (F vars tm1) tm2
|
|
1020 |
| F vars (Abs(_,_,body)) = F vars body
|
|
1021 |
| F vars (Var _) = raise ERR "collect_vars" "Schematic variable found"
|
|
1022 |
in
|
|
1023 |
F []
|
|
1024 |
end
|
|
1025 |
|
|
1026 |
(* Code for disambiguating variablenames (wrt. types) *)
|
|
1027 |
|
|
1028 |
val disamb_info_empty = {vars=[],rens=[]}
|
|
1029 |
|
|
1030 |
fun rens_of {vars,rens} = rens
|
|
1031 |
|
|
1032 |
fun name_of_var (Free(vname,_)) = vname
|
|
1033 |
| name_of_var _ = raise ERR "name_of_var" "Not a variable"
|
|
1034 |
|
|
1035 |
fun disamb_helper {vars,rens} tm =
|
|
1036 |
let
|
|
1037 |
val varstm = collect_vars tm
|
|
1038 |
fun process (v as Free(vname,ty),(vars,rens,inst)) =
|
|
1039 |
if v mem vars
|
|
1040 |
then (vars,rens,inst)
|
|
1041 |
else (case try (Lib.assoc v) rens of
|
|
1042 |
Some vnew => (vars,rens,(v,vnew)::inst)
|
|
1043 |
| None => if exists (fn Free(vname',_) => vname = vname' | _ => raise ERR "disamb_helper" "Bad varlist") vars
|
|
1044 |
then
|
|
1045 |
let
|
|
1046 |
val tmnames = map name_of_var varstm
|
|
1047 |
val varnames = map name_of_var vars
|
|
1048 |
val (dom,rng) = ListPair.unzip rens
|
|
1049 |
val rensnames = (map name_of_var dom) @ (map name_of_var rng)
|
|
1050 |
val instnames = map name_of_var (snd (ListPair.unzip inst))
|
|
1051 |
val allnames = tmnames @ varnames @ rensnames @ instnames
|
|
1052 |
val vnewname = Term.variant allnames vname
|
|
1053 |
val vnew = Free(vnewname,ty)
|
|
1054 |
in
|
|
1055 |
(vars,(v,vnew)::rens,(v,vnew)::inst)
|
|
1056 |
end
|
|
1057 |
else (v::vars,rens,inst))
|
|
1058 |
| process _ = raise ERR "disamb_helper" "Internal error"
|
|
1059 |
|
|
1060 |
val (vars',rens',inst) =
|
|
1061 |
foldr process (varstm,(vars,rens,[]))
|
|
1062 |
in
|
|
1063 |
({vars=vars',rens=rens'},inst)
|
|
1064 |
end
|
|
1065 |
|
|
1066 |
fun disamb_term_from info tm =
|
|
1067 |
let
|
|
1068 |
val (info',inst) = disamb_helper info tm
|
|
1069 |
in
|
|
1070 |
(info',apply_inst_term inst tm)
|
|
1071 |
end
|
|
1072 |
|
|
1073 |
fun swap (x,y) = (y,x)
|
|
1074 |
|
|
1075 |
fun has_ren (HOLThm([],_)) = false
|
|
1076 |
| has_ren _ = true
|
|
1077 |
|
|
1078 |
fun prinfo {vars,rens} = (writeln "Vars:";
|
|
1079 |
app prin vars;
|
|
1080 |
writeln "Renaming:";
|
|
1081 |
app (fn(x,y)=>(prin x; writeln " -->"; prin y)) rens)
|
|
1082 |
|
|
1083 |
fun disamb_thm_from info (hth as HOLThm(rens,thm)) =
|
|
1084 |
let
|
|
1085 |
val inv_rens = map swap rens
|
|
1086 |
val orig_thm = apply_inst_term inv_rens (prop_of thm)
|
|
1087 |
val (info',inst) = disamb_helper info orig_thm
|
|
1088 |
val rens' = map (apsnd (apply_inst_term inst)) inv_rens
|
|
1089 |
val (dom,rng) = ListPair.unzip (rens' @ inst)
|
|
1090 |
val sg = sign_of_thm thm
|
|
1091 |
val thm' = mk_INST (map (cterm_of sg) dom) (map (cterm_of sg) rng) thm
|
|
1092 |
in
|
|
1093 |
(info',thm')
|
|
1094 |
end
|
|
1095 |
|
|
1096 |
fun disamb_terms_from info tms =
|
|
1097 |
foldr (fn (tm,(info,tms)) =>
|
|
1098 |
let
|
|
1099 |
val (info',tm') = disamb_term_from info tm
|
|
1100 |
in
|
|
1101 |
(info',tm'::tms)
|
|
1102 |
end)
|
|
1103 |
(tms,(info,[]))
|
|
1104 |
|
|
1105 |
fun disamb_thms_from info hthms =
|
|
1106 |
foldr (fn (hthm,(info,thms)) =>
|
|
1107 |
let
|
|
1108 |
val (info',tm') = disamb_thm_from info hthm
|
|
1109 |
in
|
|
1110 |
(info',tm'::thms)
|
|
1111 |
end)
|
|
1112 |
(hthms,(info,[]))
|
|
1113 |
|
|
1114 |
fun disamb_term tm = disamb_term_from disamb_info_empty tm
|
|
1115 |
fun disamb_terms tms = disamb_terms_from disamb_info_empty tms
|
|
1116 |
fun disamb_thm thm = disamb_thm_from disamb_info_empty thm
|
|
1117 |
fun disamb_thms thms = disamb_thms_from disamb_info_empty thms
|
|
1118 |
|
|
1119 |
fun norm_hthm sg (hth as HOLThm([],_)) = hth
|
|
1120 |
| norm_hthm sg (hth as HOLThm(rens,th)) =
|
|
1121 |
let
|
|
1122 |
val vars = collect_vars (prop_of th)
|
|
1123 |
val (rens',inst,_) =
|
|
1124 |
foldr (fn((ren as (vold as Free(vname,_),vnew)),
|
|
1125 |
(rens,inst,vars)) =>
|
|
1126 |
(case Library.find_first (fn Free(v,_) => v = vname | _ => false) vars of
|
|
1127 |
Some v' => if v' = vold
|
|
1128 |
then (rens,(vnew,vold)::inst,vold::vars)
|
|
1129 |
else (ren::rens,(vold,vnew)::inst,vnew::vars)
|
|
1130 |
| None => (rens,(vnew,vold)::inst,vold::vars))
|
|
1131 |
| _ => raise ERR "norm_hthm" "Internal error")
|
|
1132 |
(rens,([],[],vars))
|
|
1133 |
val (dom,rng) = ListPair.unzip inst
|
|
1134 |
val th' = mk_INST (map (cterm_of sg) dom) (map (cterm_of sg) rng) th
|
|
1135 |
val nvars = collect_vars (prop_of th')
|
|
1136 |
val rens' = filter (fn(_,v) => v mem nvars) rens
|
|
1137 |
val res = HOLThm(rens',th')
|
|
1138 |
in
|
|
1139 |
res
|
|
1140 |
end
|
|
1141 |
|
|
1142 |
(* End of disambiguating code *)
|
|
1143 |
|
|
1144 |
val debug = ref false
|
|
1145 |
|
|
1146 |
fun if_debug f x = if !debug then f x else ()
|
|
1147 |
val message = if_debug writeln
|
|
1148 |
|
|
1149 |
val conjE_helper = Thm.permute_prems 0 1 conjE
|
|
1150 |
|
|
1151 |
fun get_hol4_thm thyname thmname thy =
|
|
1152 |
case get_hol4_theorem thyname thmname thy of
|
|
1153 |
Some hth => Some (HOLThm hth)
|
|
1154 |
| None =>
|
|
1155 |
let
|
|
1156 |
val pending = HOL4Pending.get thy
|
|
1157 |
in
|
|
1158 |
case StringPair.lookup (pending,(thyname,thmname)) of
|
|
1159 |
Some hth => Some (HOLThm hth)
|
|
1160 |
| None => None
|
|
1161 |
end
|
|
1162 |
|
|
1163 |
fun non_trivial_term_consts tm =
|
|
1164 |
filter (fn c => not (c = "Trueprop" orelse
|
|
1165 |
c = "All" orelse
|
|
1166 |
c = "op -->" orelse
|
|
1167 |
c = "op &" orelse
|
|
1168 |
c = "op =")) (Term.term_consts tm)
|
|
1169 |
|
|
1170 |
fun match_consts t (* th *) =
|
|
1171 |
let
|
|
1172 |
fun add_consts (Const (c, _), cs) =
|
|
1173 |
(case c of
|
|
1174 |
"op =" => "==" ins_string cs
|
|
1175 |
| "op -->" => "==>" ins_string cs
|
|
1176 |
| "All" => cs
|
|
1177 |
| "all" => cs
|
|
1178 |
| "op &" => cs
|
|
1179 |
| "Trueprop" => cs
|
|
1180 |
| _ => c ins_string cs)
|
|
1181 |
| add_consts (t $ u, cs) = add_consts (t, add_consts (u, cs))
|
|
1182 |
| add_consts (Abs (_, _, t), cs) = add_consts (t, cs)
|
|
1183 |
| add_consts (_, cs) = cs
|
|
1184 |
val t_consts = add_consts(t,[])
|
|
1185 |
in
|
|
1186 |
fn th => eq_set(t_consts,add_consts(prop_of th,[]))
|
|
1187 |
end
|
|
1188 |
|
|
1189 |
fun split_name str =
|
|
1190 |
let
|
|
1191 |
val sub = Substring.all str
|
|
1192 |
val (f,idx) = apsnd Substring.string (Substring.splitr Char.isDigit sub)
|
|
1193 |
val (newstr,u) = apboth Substring.string (Substring.splitr (fn c => c = #"_") f)
|
|
1194 |
in
|
|
1195 |
if not (idx = "") andalso u = "_"
|
|
1196 |
then Some (newstr,valOf(Int.fromString idx))
|
|
1197 |
else None
|
|
1198 |
end
|
|
1199 |
handle _ => None
|
|
1200 |
|
|
1201 |
fun rewrite_hol4_term t thy =
|
|
1202 |
let
|
|
1203 |
val sg = sign_of thy
|
|
1204 |
|
|
1205 |
val hol4rews1 = map (transfer_sg sg) (HOL4Rewrites.get thy)
|
|
1206 |
val hol4ss = empty_ss setmksimps single addsimps hol4rews1
|
|
1207 |
in
|
|
1208 |
transfer_sg sg (Simplifier.full_rewrite hol4ss (cterm_of sg t))
|
|
1209 |
end
|
|
1210 |
|
|
1211 |
|
|
1212 |
fun get_isabelle_thm thyname thmname hol4conc thy =
|
|
1213 |
let
|
|
1214 |
val sg = sign_of thy
|
|
1215 |
|
|
1216 |
val (info,hol4conc') = disamb_term hol4conc
|
|
1217 |
val i2h_conc = symmetric (rewrite_hol4_term (HOLogic.mk_Trueprop hol4conc') thy)
|
|
1218 |
val isaconc =
|
|
1219 |
case concl_of i2h_conc of
|
|
1220 |
Const("==",_) $ lhs $ _ => lhs
|
|
1221 |
| _ => error "get_isabelle_thm" "Bad rewrite rule"
|
|
1222 |
val _ = (message "Original conclusion:";
|
|
1223 |
if_debug prin hol4conc';
|
|
1224 |
message "Modified conclusion:";
|
|
1225 |
if_debug prin isaconc)
|
|
1226 |
|
|
1227 |
fun mk_res th = HOLThm(rens_of info,equal_elim i2h_conc th)
|
|
1228 |
in
|
|
1229 |
case get_hol4_mapping thyname thmname thy of
|
|
1230 |
Some (Some thmname) =>
|
|
1231 |
let
|
|
1232 |
val _ = message ("Looking for " ^ thmname)
|
|
1233 |
val th1 = (Some (transform_error (PureThy.get_thm thy) thmname)
|
|
1234 |
handle ERROR_MESSAGE _ =>
|
|
1235 |
(case split_name thmname of
|
|
1236 |
Some (listname,idx) => (Some (nth_elem(idx-1,PureThy.get_thms thy listname))
|
|
1237 |
handle _ => None)
|
|
1238 |
| None => None))
|
|
1239 |
in
|
|
1240 |
case th1 of
|
|
1241 |
Some th2 =>
|
|
1242 |
(case Shuffler.set_prop thy isaconc [(thmname,th2)] of
|
|
1243 |
Some (_,th) => (message "YES";(thy, Some (mk_res th)))
|
|
1244 |
| None => (message "NO2";error "get_isabelle_thm" "Bad mapping"))
|
|
1245 |
| None => (message "NO1";error "get_isabelle_thm" "Bad mapping")
|
|
1246 |
end
|
|
1247 |
| Some None => error ("Trying to access ignored theorem " ^ thmname)
|
|
1248 |
| None =>
|
|
1249 |
let
|
|
1250 |
val _ = (message "Looking for conclusion:";
|
|
1251 |
if_debug prin isaconc)
|
|
1252 |
val cs = non_trivial_term_consts isaconc
|
|
1253 |
val _ = (message "Looking for consts:";
|
|
1254 |
message (String.concat cs))
|
|
1255 |
val pot_thms = Shuffler.find_potential thy isaconc
|
|
1256 |
val _ = message ((Int.toString (length pot_thms)) ^ " potential theorems")
|
|
1257 |
in
|
|
1258 |
case Shuffler.set_prop thy isaconc pot_thms of
|
|
1259 |
Some (isaname,th) =>
|
|
1260 |
let
|
|
1261 |
val hth as HOLThm args = mk_res th
|
|
1262 |
val thy' = thy |> add_hol4_theorem thyname thmname args
|
|
1263 |
|> add_hol4_mapping thyname thmname isaname
|
|
1264 |
in
|
|
1265 |
(thy',Some hth)
|
|
1266 |
end
|
|
1267 |
| None => (thy,None)
|
|
1268 |
end
|
|
1269 |
end
|
|
1270 |
handle _ => (thy,None)
|
|
1271 |
|
|
1272 |
fun get_thm thyname thmname thy =
|
|
1273 |
case get_hol4_thm thyname thmname thy of
|
|
1274 |
Some hth => (thy,Some hth)
|
|
1275 |
| None => ((case fst (import_proof thyname thmname thy) of
|
|
1276 |
Some f => get_isabelle_thm thyname thmname (f thy) thy
|
|
1277 |
| None => (thy,None))
|
|
1278 |
handle e as IO.Io _ => (thy,None)
|
|
1279 |
| e as PK _ => (thy,None))
|
|
1280 |
|
|
1281 |
fun rename_const thyname thy name =
|
|
1282 |
case get_hol4_const_renaming thyname name thy of
|
|
1283 |
Some cname => cname
|
|
1284 |
| None => name
|
|
1285 |
|
|
1286 |
fun get_def thyname constname rhs thy =
|
|
1287 |
let
|
|
1288 |
val constname = rename_const thyname thy constname
|
|
1289 |
val (thmname,thy') = get_defname thyname constname thy
|
|
1290 |
val _ = message ("Looking for definition " ^ thyname ^ "." ^ thmname)
|
|
1291 |
in
|
|
1292 |
get_isabelle_thm thyname thmname (mk_teq (thyname ^ "." ^ constname) rhs thy') thy'
|
|
1293 |
end
|
|
1294 |
|
|
1295 |
fun get_axiom thyname axname thy =
|
|
1296 |
case get_thm thyname axname thy of
|
|
1297 |
arg as (_,Some _) => arg
|
|
1298 |
| _ => raise ERR "get_axiom" ("Trying to retrieve axiom (" ^ axname ^ ")")
|
|
1299 |
|
|
1300 |
fun intern_store_thm gen_output thyname thmname hth thy =
|
|
1301 |
let
|
|
1302 |
val sg = sign_of thy
|
|
1303 |
val (hth' as HOLThm (args as (_,th))) = norm_hthm sg hth
|
|
1304 |
val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating")
|
|
1305 |
else ()
|
|
1306 |
val rew = rewrite_hol4_term (concl_of th) thy
|
|
1307 |
val th = equal_elim rew th
|
|
1308 |
val thy' = add_hol4_pending thyname thmname args thy
|
|
1309 |
val thy2 = if gen_output
|
|
1310 |
then add_dump ("lemma " ^ thmname ^ ": " ^ (smart_string_of_thm th) ^ "\n by (import " ^ thyname ^ " " ^ thmname ^ ")") thy'
|
|
1311 |
else thy'
|
|
1312 |
in
|
|
1313 |
(thy2,hth')
|
|
1314 |
end
|
|
1315 |
|
|
1316 |
val store_thm = intern_store_thm true
|
|
1317 |
|
|
1318 |
fun mk_REFL ctm =
|
|
1319 |
let
|
|
1320 |
val cty = Thm.ctyp_of_term ctm
|
|
1321 |
in
|
|
1322 |
Drule.instantiate' [Some cty] [Some ctm] reflexivity_thm
|
|
1323 |
end
|
|
1324 |
|
|
1325 |
fun REFL tm thy =
|
|
1326 |
let
|
|
1327 |
val _ = message "REFL:"
|
|
1328 |
val (info,tm') = disamb_term tm
|
|
1329 |
val sg = sign_of thy
|
|
1330 |
val ctm = Thm.cterm_of sg tm'
|
|
1331 |
val res = HOLThm(rens_of info,mk_REFL ctm)
|
|
1332 |
val _ = if_debug pth res
|
|
1333 |
in
|
|
1334 |
(thy,res)
|
|
1335 |
end
|
|
1336 |
|
|
1337 |
fun ASSUME tm thy =
|
|
1338 |
let
|
|
1339 |
val _ = message "ASSUME:"
|
|
1340 |
val (info,tm') = disamb_term tm
|
|
1341 |
val sg = sign_of thy
|
|
1342 |
val ctm = Thm.cterm_of sg (HOLogic.mk_Trueprop tm')
|
|
1343 |
val th = Thm.trivial ctm
|
|
1344 |
val res = HOLThm(rens_of info,th)
|
|
1345 |
val _ = if_debug pth res
|
|
1346 |
in
|
|
1347 |
(thy,res)
|
|
1348 |
end
|
|
1349 |
|
|
1350 |
fun INST_TYPE lambda (hth as HOLThm(rens,th)) thy =
|
|
1351 |
let
|
|
1352 |
val _ = message "INST_TYPE:"
|
|
1353 |
val _ = if_debug pth hth
|
|
1354 |
val sg = sign_of thy
|
|
1355 |
val tys_before = add_term_tfrees (prop_of th,[])
|
|
1356 |
val th1 = varifyT th
|
|
1357 |
val tys_after = add_term_tvars (prop_of th1,[])
|
|
1358 |
val tyinst = map (fn (bef,(i,_)) =>
|
|
1359 |
(case try (Lib.assoc (TFree bef)) lambda of
|
|
1360 |
Some ty => (i,ctyp_of sg ty)
|
|
1361 |
| None => (i,ctyp_of sg (TFree bef))
|
|
1362 |
))
|
|
1363 |
(zip tys_before tys_after)
|
|
1364 |
val res = Drule.instantiate (tyinst,[]) th1
|
|
1365 |
val appty = apboth (apply_tyinst_term lambda)
|
|
1366 |
val hth = HOLThm(map appty rens,res)
|
|
1367 |
val res = norm_hthm sg hth
|
|
1368 |
val _ = message "RESULT:"
|
|
1369 |
val _ = if_debug pth res
|
|
1370 |
in
|
|
1371 |
(thy,res)
|
|
1372 |
end
|
|
1373 |
|
|
1374 |
fun INST sigma hth thy =
|
|
1375 |
let
|
|
1376 |
val _ = message "INST:"
|
|
1377 |
val _ = if_debug (app (fn (x,y) => (prin x; prin y))) sigma
|
|
1378 |
val _ = if_debug pth hth
|
|
1379 |
val sg = sign_of thy
|
|
1380 |
val (sdom,srng) = ListPair.unzip sigma
|
|
1381 |
val (info,th) = disamb_thm hth
|
|
1382 |
val (info',srng') = disamb_terms_from info srng
|
|
1383 |
val rens = rens_of info'
|
|
1384 |
val sdom' = map (apply_inst_term rens) sdom
|
|
1385 |
val th1 = mk_INST (map (cterm_of sg) sdom') (map (cterm_of sg) srng') th
|
|
1386 |
val res = HOLThm(rens,th1)
|
|
1387 |
val _ = message "RESULT:"
|
|
1388 |
val _ = if_debug pth res
|
|
1389 |
in
|
|
1390 |
(thy,res)
|
|
1391 |
end
|
|
1392 |
|
|
1393 |
fun EQ_IMP_RULE (hth as HOLThm(rens,th)) thy =
|
|
1394 |
let
|
|
1395 |
val _ = message "EQ_IMP_RULE:"
|
|
1396 |
val _ = if_debug pth hth
|
|
1397 |
val res = HOLThm(rens,th RS eqimp_thm)
|
|
1398 |
val _ = message "RESULT:"
|
|
1399 |
val _ = if_debug pth res
|
|
1400 |
in
|
|
1401 |
(thy,res)
|
|
1402 |
end
|
|
1403 |
|
|
1404 |
fun mk_EQ_MP th1 th2 = [beta_eta_thm th1,beta_eta_thm th2] MRS eqmp_thm
|
|
1405 |
|
|
1406 |
fun EQ_MP hth1 hth2 thy =
|
|
1407 |
let
|
|
1408 |
val _ = message "EQ_MP:"
|
|
1409 |
val _ = if_debug pth hth1
|
|
1410 |
val _ = if_debug pth hth2
|
|
1411 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2]
|
|
1412 |
val res = HOLThm(rens_of info,mk_EQ_MP th1 th2)
|
|
1413 |
val _ = message "RESULT:"
|
|
1414 |
val _ = if_debug pth res
|
|
1415 |
in
|
|
1416 |
(thy,res)
|
|
1417 |
end
|
|
1418 |
|
|
1419 |
fun mk_COMB th1 th2 sg =
|
|
1420 |
let
|
|
1421 |
val (f,g) = case concl_of th1 of
|
|
1422 |
_ $ (Const("op =",_) $ f $ g) => (f,g)
|
|
1423 |
| _ => raise ERR "mk_COMB" "First theorem not an equality"
|
|
1424 |
val (x,y) = case concl_of th2 of
|
|
1425 |
_ $ (Const("op =",_) $ x $ y) => (x,y)
|
|
1426 |
| _ => raise ERR "mk_COMB" "Second theorem not an equality"
|
|
1427 |
val fty = type_of f
|
|
1428 |
val (fd,fr) = dom_rng fty
|
|
1429 |
val comb_thm' = Drule.instantiate'
|
|
1430 |
[Some (ctyp_of sg fd),Some (ctyp_of sg fr)]
|
|
1431 |
[Some (cterm_of sg f),Some (cterm_of sg g),
|
|
1432 |
Some (cterm_of sg x),Some (cterm_of sg y)] comb_thm
|
|
1433 |
in
|
|
1434 |
[th1,th2] MRS comb_thm'
|
|
1435 |
end
|
|
1436 |
|
|
1437 |
fun SUBST rews ctxt hth thy =
|
|
1438 |
let
|
|
1439 |
val _ = message "SUBST:"
|
|
1440 |
val _ = if_debug (app pth) rews
|
|
1441 |
val _ = if_debug prin ctxt
|
|
1442 |
val _ = if_debug pth hth
|
|
1443 |
val (info,th) = disamb_thm hth
|
|
1444 |
val (info1,ctxt') = disamb_term_from info ctxt
|
|
1445 |
val (info2,rews') = disamb_thms_from info1 rews
|
|
1446 |
|
|
1447 |
val sg = sign_of thy
|
|
1448 |
val cctxt = cterm_of sg ctxt'
|
|
1449 |
fun subst th [] = th
|
|
1450 |
| subst th (rew::rews) = subst (mk_COMB th rew sg) rews
|
|
1451 |
val res = HOLThm(rens_of info2,mk_EQ_MP (subst (mk_REFL cctxt) rews') th)
|
|
1452 |
val _ = message "RESULT:"
|
|
1453 |
val _ = if_debug pth res
|
|
1454 |
in
|
|
1455 |
(thy,res)
|
|
1456 |
end
|
|
1457 |
|
|
1458 |
fun DISJ_CASES hth hth1 hth2 thy =
|
|
1459 |
let
|
|
1460 |
val _ = message "DISJ_CASES:"
|
|
1461 |
val _ = if_debug (app pth) [hth,hth1,hth2]
|
|
1462 |
val (info,th) = disamb_thm hth
|
|
1463 |
val (info1,th1) = disamb_thm_from info hth1
|
|
1464 |
val (info2,th2) = disamb_thm_from info1 hth2
|
|
1465 |
val sg = sign_of thy
|
|
1466 |
val th1 = norm_hyps th1
|
|
1467 |
val th2 = norm_hyps th2
|
|
1468 |
val (l,r) = case concl_of th of
|
|
1469 |
_ $ (Const("op |",_) $ l $ r) => (l,r)
|
|
1470 |
| _ => raise ERR "DISJ_CASES" "Conclusion not a disjunction"
|
|
1471 |
val th1' = rearrange sg (HOLogic.mk_Trueprop l) th1
|
|
1472 |
val th2' = rearrange sg (HOLogic.mk_Trueprop r) th2
|
|
1473 |
val res1 = th RS disj_cases_thm
|
|
1474 |
val res2 = uniq_compose ((nprems_of th1')-1) th1' ((nprems_of th)+1) res1
|
|
1475 |
val res3 = uniq_compose ((nprems_of th2')-1) th2' (nprems_of res2) res2
|
|
1476 |
val res = HOLThm(rens_of info2,res3)
|
|
1477 |
val _ = message "RESULT:"
|
|
1478 |
val _ = if_debug pth res
|
|
1479 |
in
|
|
1480 |
(thy,res)
|
|
1481 |
end
|
|
1482 |
|
|
1483 |
fun DISJ1 hth tm thy =
|
|
1484 |
let
|
|
1485 |
val _ = message "DISJ1:"
|
|
1486 |
val _ = if_debug pth hth
|
|
1487 |
val _ = if_debug prin tm
|
|
1488 |
val (info,th) = disamb_thm hth
|
|
1489 |
val (info',tm') = disamb_term_from info tm
|
|
1490 |
val sg = sign_of thy
|
|
1491 |
val ct = Thm.cterm_of sg tm'
|
|
1492 |
val disj1_thm' = Drule.instantiate' [] [None,Some ct] disj1_thm
|
|
1493 |
val res = HOLThm(rens_of info',th RS disj1_thm')
|
|
1494 |
val _ = message "RESULT:"
|
|
1495 |
val _ = if_debug pth res
|
|
1496 |
in
|
|
1497 |
(thy,res)
|
|
1498 |
end
|
|
1499 |
|
|
1500 |
fun DISJ2 tm hth thy =
|
|
1501 |
let
|
|
1502 |
val _ = message "DISJ1:"
|
|
1503 |
val _ = if_debug prin tm
|
|
1504 |
val _ = if_debug pth hth
|
|
1505 |
val (info,th) = disamb_thm hth
|
|
1506 |
val (info',tm') = disamb_term_from info tm
|
|
1507 |
val sg = sign_of thy
|
|
1508 |
val ct = Thm.cterm_of sg tm'
|
|
1509 |
val disj2_thm' = Drule.instantiate' [] [None,Some ct] disj2_thm
|
|
1510 |
val res = HOLThm(rens_of info',th RS disj2_thm')
|
|
1511 |
val _ = message "RESULT:"
|
|
1512 |
val _ = if_debug pth res
|
|
1513 |
in
|
|
1514 |
(thy,res)
|
|
1515 |
end
|
|
1516 |
|
|
1517 |
fun IMP_ANTISYM hth1 hth2 thy =
|
|
1518 |
let
|
|
1519 |
val _ = message "IMP_ANTISYM:"
|
|
1520 |
val _ = if_debug pth hth1
|
|
1521 |
val _ = if_debug pth hth2
|
|
1522 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2]
|
|
1523 |
val th = [beta_eta_thm th1,beta_eta_thm th2] MRS imp_antisym_thm
|
|
1524 |
val res = HOLThm(rens_of info,th)
|
|
1525 |
val _ = message "RESULT:"
|
|
1526 |
val _ = if_debug pth res
|
|
1527 |
in
|
|
1528 |
(thy,res)
|
|
1529 |
end
|
|
1530 |
|
|
1531 |
fun SYM (hth as HOLThm(rens,th)) thy =
|
|
1532 |
let
|
|
1533 |
val _ = message "SYM:"
|
|
1534 |
val _ = if_debug pth hth
|
|
1535 |
val th = th RS symmetry_thm
|
|
1536 |
val res = HOLThm(rens,th)
|
|
1537 |
val _ = message "RESULT:"
|
|
1538 |
val _ = if_debug pth res
|
|
1539 |
in
|
|
1540 |
(thy,res)
|
|
1541 |
end
|
|
1542 |
|
|
1543 |
fun MP hth1 hth2 thy =
|
|
1544 |
let
|
|
1545 |
val _ = message "MP:"
|
|
1546 |
val _ = if_debug pth hth1
|
|
1547 |
val _ = if_debug pth hth2
|
|
1548 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2]
|
|
1549 |
val th = [beta_eta_thm th1,beta_eta_thm th2] MRS mp_thm
|
|
1550 |
val res = HOLThm(rens_of info,th)
|
|
1551 |
val _ = message "RESULT:"
|
|
1552 |
val _ = if_debug pth res
|
|
1553 |
in
|
|
1554 |
(thy,res)
|
|
1555 |
end
|
|
1556 |
|
|
1557 |
fun CONJ hth1 hth2 thy =
|
|
1558 |
let
|
|
1559 |
val _ = message "CONJ:"
|
|
1560 |
val _ = if_debug pth hth1
|
|
1561 |
val _ = if_debug pth hth2
|
|
1562 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2]
|
|
1563 |
val th = [th1,th2] MRS conj_thm
|
|
1564 |
val res = HOLThm(rens_of info,th)
|
|
1565 |
val _ = message "RESULT:"
|
|
1566 |
val _ = if_debug pth res
|
|
1567 |
in
|
|
1568 |
(thy,res)
|
|
1569 |
end
|
|
1570 |
|
|
1571 |
fun CONJUNCT1 (hth as HOLThm(rens,th)) thy =
|
|
1572 |
let
|
|
1573 |
val _ = message "CONJUNCT1:"
|
|
1574 |
val _ = if_debug pth hth
|
|
1575 |
val res = HOLThm(rens,th RS conjunct1_thm)
|
|
1576 |
val _ = message "RESULT:"
|
|
1577 |
val _ = if_debug pth res
|
|
1578 |
in
|
|
1579 |
(thy,res)
|
|
1580 |
end
|
|
1581 |
|
|
1582 |
fun CONJUNCT2 (hth as HOLThm(rens,th)) thy =
|
|
1583 |
let
|
|
1584 |
val _ = message "CONJUNCT1:"
|
|
1585 |
val _ = if_debug pth hth
|
|
1586 |
val res = HOLThm(rens,th RS conjunct2_thm)
|
|
1587 |
val _ = message "RESULT:"
|
|
1588 |
val _ = if_debug pth res
|
|
1589 |
in
|
|
1590 |
(thy,res)
|
|
1591 |
end
|
|
1592 |
|
|
1593 |
fun EXISTS ex wit hth thy =
|
|
1594 |
let
|
|
1595 |
val _ = message "EXISTS:"
|
|
1596 |
val _ = if_debug prin ex
|
|
1597 |
val _ = if_debug prin wit
|
|
1598 |
val _ = if_debug pth hth
|
|
1599 |
val (info,th) = disamb_thm hth
|
|
1600 |
val (info',[ex',wit']) = disamb_terms_from info [ex,wit]
|
|
1601 |
val sg = sign_of thy
|
|
1602 |
val cwit = cterm_of sg wit'
|
|
1603 |
val cty = ctyp_of_term cwit
|
|
1604 |
val a = case ex' of
|
|
1605 |
(Const("Ex",_) $ a) => a
|
|
1606 |
| _ => raise ERR "EXISTS" "Argument not existential"
|
|
1607 |
val ca = cterm_of sg a
|
|
1608 |
val exists_thm' = beta_eta_thm (Drule.instantiate' [Some cty] [Some ca,Some cwit] exists_thm)
|
|
1609 |
val th1 = beta_eta_thm th
|
|
1610 |
val th2 = implies_elim_all th1
|
|
1611 |
val th3 = th2 COMP exists_thm'
|
|
1612 |
val th = implies_intr_hyps th3
|
|
1613 |
val res = HOLThm(rens_of info',th)
|
|
1614 |
val _ = message "RESULT:"
|
|
1615 |
val _ = if_debug pth res
|
|
1616 |
in
|
|
1617 |
(thy,res)
|
|
1618 |
end
|
|
1619 |
|
|
1620 |
fun CHOOSE v hth1 hth2 thy =
|
|
1621 |
let
|
|
1622 |
val _ = message "CHOOSE:"
|
|
1623 |
val _ = if_debug prin v
|
|
1624 |
val _ = if_debug pth hth1
|
|
1625 |
val _ = if_debug pth hth2
|
|
1626 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2]
|
|
1627 |
val (info',v') = disamb_term_from info v
|
|
1628 |
fun strip 0 _ th = th
|
|
1629 |
| strip n (p::ps) th =
|
|
1630 |
strip (n-1) ps (implies_elim th (assume p))
|
|
1631 |
| strip _ _ _ = raise ERR "CHOOSE" "strip error"
|
|
1632 |
val sg = sign_of thy
|
|
1633 |
val cv = cterm_of sg v'
|
|
1634 |
val th2 = norm_hyps th2
|
|
1635 |
val cvty = ctyp_of_term cv
|
|
1636 |
val _$c = concl_of th2
|
|
1637 |
val cc = cterm_of sg c
|
|
1638 |
val a = case concl_of th1 of
|
|
1639 |
_ $ (Const("Ex",_) $ a) => a
|
|
1640 |
| _ => raise ERR "CHOOSE" "Conclusion not existential"
|
|
1641 |
val ca = cterm_of (sign_of_thm th1) a
|
|
1642 |
val choose_thm' = beta_eta_thm (Drule.instantiate' [Some cvty] [Some ca,Some cc] choose_thm)
|
|
1643 |
val th21 = rearrange sg (HOLogic.mk_Trueprop (a $ v')) th2
|
|
1644 |
val th22 = strip ((nprems_of th21)-1) (cprems_of th21) th21
|
|
1645 |
val th23 = beta_eta_thm (forall_intr cv th22)
|
|
1646 |
val th11 = implies_elim_all (beta_eta_thm th1)
|
|
1647 |
val th' = th23 COMP (th11 COMP choose_thm')
|
|
1648 |
val th = implies_intr_hyps th'
|
|
1649 |
val res = HOLThm(rens_of info',th)
|
|
1650 |
val _ = message "RESULT:"
|
|
1651 |
val _ = if_debug pth res
|
|
1652 |
in
|
|
1653 |
(thy,res)
|
|
1654 |
end
|
|
1655 |
|
|
1656 |
fun GEN v hth thy =
|
|
1657 |
let
|
|
1658 |
val _ = message "GEN:"
|
|
1659 |
val _ = if_debug prin v
|
|
1660 |
val _ = if_debug pth hth
|
|
1661 |
val (info,th) = disamb_thm hth
|
|
1662 |
val (info',v') = disamb_term_from info v
|
|
1663 |
val res = HOLThm(rens_of info',mk_GEN v' th (sign_of thy))
|
|
1664 |
val _ = message "RESULT:"
|
|
1665 |
val _ = if_debug pth res
|
|
1666 |
in
|
|
1667 |
(thy,res)
|
|
1668 |
end
|
|
1669 |
|
|
1670 |
fun SPEC tm hth thy =
|
|
1671 |
let
|
|
1672 |
val _ = message "SPEC:"
|
|
1673 |
val _ = if_debug prin tm
|
|
1674 |
val _ = if_debug pth hth
|
|
1675 |
val (info,th) = disamb_thm hth
|
|
1676 |
val (info',tm') = disamb_term_from info tm
|
|
1677 |
val sg = sign_of thy
|
|
1678 |
val ctm = Thm.cterm_of sg tm'
|
|
1679 |
val cty = Thm.ctyp_of_term ctm
|
|
1680 |
val spec' = Drule.instantiate' [Some cty] [None,Some ctm] spec_thm
|
|
1681 |
val th = th RS spec'
|
|
1682 |
val res = HOLThm(rens_of info',th)
|
|
1683 |
val _ = message "RESULT:"
|
|
1684 |
val _ = if_debug pth res
|
|
1685 |
in
|
|
1686 |
(thy,res)
|
|
1687 |
end
|
|
1688 |
|
|
1689 |
fun COMB hth1 hth2 thy =
|
|
1690 |
let
|
|
1691 |
val _ = message "COMB:"
|
|
1692 |
val _ = if_debug pth hth1
|
|
1693 |
val _ = if_debug pth hth2
|
|
1694 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2]
|
|
1695 |
val sg = sign_of thy
|
|
1696 |
val res = HOLThm(rens_of info,mk_COMB th1 th2 sg)
|
|
1697 |
val _ = message "RESULT:"
|
|
1698 |
val _ = if_debug pth res
|
|
1699 |
in
|
|
1700 |
(thy,res)
|
|
1701 |
end
|
|
1702 |
|
|
1703 |
fun TRANS hth1 hth2 thy =
|
|
1704 |
let
|
|
1705 |
val _ = message "TRANS:"
|
|
1706 |
val _ = if_debug pth hth1
|
|
1707 |
val _ = if_debug pth hth2
|
|
1708 |
val (info,[th1,th2]) = disamb_thms [hth1,hth2]
|
|
1709 |
val th = [th1,th2] MRS trans_thm
|
|
1710 |
val res = HOLThm(rens_of info,th)
|
|
1711 |
val _ = message "RESULT:"
|
|
1712 |
val _ = if_debug pth res
|
|
1713 |
in
|
|
1714 |
(thy,res)
|
|
1715 |
end
|
|
1716 |
|
|
1717 |
|
|
1718 |
fun CCONTR tm hth thy =
|
|
1719 |
let
|
|
1720 |
val _ = message "SPEC:"
|
|
1721 |
val _ = if_debug prin tm
|
|
1722 |
val _ = if_debug pth hth
|
|
1723 |
val (info,th) = disamb_thm hth
|
|
1724 |
val (info',tm') = disamb_term_from info tm
|
|
1725 |
val th = norm_hyps th
|
|
1726 |
val sg = sign_of thy
|
|
1727 |
val ct = cterm_of sg tm'
|
|
1728 |
val th1 = rearrange sg (HOLogic.mk_Trueprop (Const("Not",boolT-->boolT) $ tm')) th
|
|
1729 |
val ccontr_thm' = Drule.instantiate' [] [Some ct] ccontr_thm
|
|
1730 |
val res1 = uniq_compose ((nprems_of th1) - 1) th1 1 ccontr_thm'
|
|
1731 |
val res = HOLThm(rens_of info',res1)
|
|
1732 |
val _ = message "RESULT:"
|
|
1733 |
val _ = if_debug pth res
|
|
1734 |
in
|
|
1735 |
(thy,res)
|
|
1736 |
end
|
|
1737 |
|
|
1738 |
fun mk_ABS v th sg =
|
|
1739 |
let
|
|
1740 |
val cv = cterm_of sg v
|
|
1741 |
val th1 = implies_elim_all (beta_eta_thm th)
|
|
1742 |
val (f,g) = case concl_of th1 of
|
|
1743 |
_ $ (Const("op =",_) $ f $ g) => (Term.lambda v f,Term.lambda v g)
|
|
1744 |
| _ => raise ERR "mk_ABS" "Bad conclusion"
|
|
1745 |
val (fd,fr) = dom_rng (type_of f)
|
|
1746 |
val abs_thm' = Drule.instantiate' [Some (ctyp_of sg fd), Some (ctyp_of sg fr)] [Some (cterm_of sg f), Some (cterm_of sg g)] abs_thm
|
|
1747 |
val th2 = forall_intr cv th1
|
|
1748 |
val th3 = th2 COMP abs_thm'
|
|
1749 |
val res = implies_intr_hyps th3
|
|
1750 |
in
|
|
1751 |
res
|
|
1752 |
end
|
|
1753 |
|
|
1754 |
fun ABS v hth thy =
|
|
1755 |
let
|
|
1756 |
val _ = message "ABS:"
|
|
1757 |
val _ = if_debug prin v
|
|
1758 |
val _ = if_debug pth hth
|
|
1759 |
val (info,th) = disamb_thm hth
|
|
1760 |
val (info',v') = disamb_term_from info v
|
|
1761 |
val sg = sign_of thy
|
|
1762 |
val res = HOLThm(rens_of info',mk_ABS v' th sg)
|
|
1763 |
val _ = message "RESULT:"
|
|
1764 |
val _ = if_debug pth res
|
|
1765 |
in
|
|
1766 |
(thy,res)
|
|
1767 |
end
|
|
1768 |
|
|
1769 |
fun GEN_ABS copt vlist hth thy =
|
|
1770 |
let
|
|
1771 |
val _ = message "GEN_ABS:"
|
|
1772 |
val _ = case copt of
|
|
1773 |
Some c => if_debug prin c
|
|
1774 |
| None => ()
|
|
1775 |
val _ = if_debug (app prin) vlist
|
|
1776 |
val _ = if_debug pth hth
|
|
1777 |
val (info,th) = disamb_thm hth
|
|
1778 |
val (info',vlist') = disamb_terms_from info vlist
|
|
1779 |
val sg = sign_of thy
|
|
1780 |
val th1 =
|
|
1781 |
case copt of
|
|
1782 |
Some (c as Const(cname,cty)) =>
|
|
1783 |
let
|
|
1784 |
fun inst_type ty1 ty2 (TVar _) = raise ERR "GEN_ABS" "Type variable found!"
|
|
1785 |
| inst_type ty1 ty2 (ty as TFree _) = if ty1 = ty
|
|
1786 |
then ty2
|
|
1787 |
else ty
|
|
1788 |
| inst_type ty1 ty2 (ty as Type(name,tys)) =
|
|
1789 |
Type(name,map (inst_type ty1 ty2) tys)
|
|
1790 |
in
|
|
1791 |
foldr (fn (v,th) =>
|
|
1792 |
let
|
|
1793 |
val cdom = fst (dom_rng (fst (dom_rng cty)))
|
|
1794 |
val vty = type_of v
|
|
1795 |
val newcty = inst_type cdom vty cty
|
|
1796 |
val cc = cterm_of sg (Const(cname,newcty))
|
|
1797 |
in
|
|
1798 |
mk_COMB (mk_REFL cc) (mk_ABS v th sg) sg
|
|
1799 |
end) (vlist',th)
|
|
1800 |
end
|
|
1801 |
| Some _ => raise ERR "GEN_ABS" "Bad constant"
|
|
1802 |
| None =>
|
|
1803 |
foldr (fn (v,th) => mk_ABS v th sg) (vlist',th)
|
|
1804 |
val res = HOLThm(rens_of info',th1)
|
|
1805 |
val _ = message "RESULT:"
|
|
1806 |
val _ = if_debug pth res
|
|
1807 |
in
|
|
1808 |
(thy,res)
|
|
1809 |
end
|
|
1810 |
|
|
1811 |
fun NOT_INTRO (hth as HOLThm(rens,th)) thy =
|
|
1812 |
let
|
|
1813 |
val _ = message "NOT_INTRO:"
|
|
1814 |
val _ = if_debug pth hth
|
|
1815 |
val sg = sign_of thy
|
|
1816 |
val th1 = implies_elim_all (beta_eta_thm th)
|
|
1817 |
val a = case concl_of th1 of
|
|
1818 |
_ $ (Const("op -->",_) $ a $ Const("False",_)) => a
|
|
1819 |
| _ => raise ERR "NOT_INTRO" "Conclusion of bad form"
|
|
1820 |
val ca = cterm_of sg a
|
|
1821 |
val th2 = equal_elim (Drule.instantiate' [] [Some ca] not_intro_thm) th1
|
|
1822 |
val res = HOLThm(rens,implies_intr_hyps th2)
|
|
1823 |
val _ = message "RESULT:"
|
|
1824 |
val _ = if_debug pth res
|
|
1825 |
in
|
|
1826 |
(thy,res)
|
|
1827 |
end
|
|
1828 |
|
|
1829 |
fun NOT_ELIM (hth as HOLThm(rens,th)) thy =
|
|
1830 |
let
|
|
1831 |
val _ = message "NOT_INTRO:"
|
|
1832 |
val _ = if_debug pth hth
|
|
1833 |
val sg = sign_of thy
|
|
1834 |
val th1 = implies_elim_all (beta_eta_thm th)
|
|
1835 |
val a = case concl_of th1 of
|
|
1836 |
_ $ (Const("Not",_) $ a) => a
|
|
1837 |
| _ => raise ERR "NOT_ELIM" "Conclusion of bad form"
|
|
1838 |
val ca = cterm_of sg a
|
|
1839 |
val th2 = equal_elim (Drule.instantiate' [] [Some ca] not_elim_thm) th1
|
|
1840 |
val res = HOLThm(rens,implies_intr_hyps th2)
|
|
1841 |
val _ = message "RESULT:"
|
|
1842 |
val _ = if_debug pth res
|
|
1843 |
in
|
|
1844 |
(thy,res)
|
|
1845 |
end
|
|
1846 |
|
|
1847 |
fun DISCH tm hth thy =
|
|
1848 |
let
|
|
1849 |
val _ = message "DISCH:"
|
|
1850 |
val _ = if_debug prin tm
|
|
1851 |
val _ = if_debug pth hth
|
|
1852 |
val (info,th) = disamb_thm hth
|
|
1853 |
val (info',tm') = disamb_term_from info tm
|
|
1854 |
val prems = prems_of th
|
|
1855 |
val sg = sign_of thy
|
|
1856 |
val th1 = beta_eta_thm th
|
|
1857 |
val th2 = implies_elim_all th1
|
|
1858 |
val th3 = implies_intr (cterm_of sg (HOLogic.mk_Trueprop tm')) th2
|
|
1859 |
val th4 = th3 COMP disch_thm
|
|
1860 |
val res = HOLThm(rens_of info',implies_intr_hyps th4)
|
|
1861 |
val _ = message "RESULT:"
|
|
1862 |
val _ = if_debug pth res
|
|
1863 |
in
|
|
1864 |
(thy,res)
|
|
1865 |
end
|
|
1866 |
|
|
1867 |
val spaces = String.concat o separate " "
|
|
1868 |
|
|
1869 |
fun new_definition thyname constname rhs thy =
|
|
1870 |
let
|
|
1871 |
val constname = rename_const thyname thy constname
|
|
1872 |
val _ = warning ("Introducing constant " ^ constname)
|
|
1873 |
val (thmname,thy) = get_defname thyname constname thy
|
|
1874 |
val (info,rhs') = disamb_term rhs
|
|
1875 |
val ctype = type_of rhs'
|
|
1876 |
val csyn = mk_syn constname
|
|
1877 |
val thy1 = case HOL4DefThy.get thy of
|
|
1878 |
Replaying _ => thy
|
|
1879 |
| _ => Theory.add_consts_i [(constname,ctype,csyn)] thy
|
|
1880 |
val eq = mk_defeq constname rhs' thy1
|
|
1881 |
val (thy2,thms) = PureThy.add_defs_i false [((thmname,eq),[])] thy1
|
|
1882 |
val def_thm = hd thms
|
|
1883 |
val thm' = def_thm RS meta_eq_to_obj_eq_thm
|
|
1884 |
val (thy',th) = (thy2, thm')
|
|
1885 |
val fullcname = Sign.intern_const (sign_of thy') constname
|
|
1886 |
val thy'' = add_hol4_const_mapping thyname constname true fullcname thy'
|
|
1887 |
val (linfo,tm24) = disamb_term (mk_teq constname rhs' thy'')
|
|
1888 |
val sg = sign_of thy''
|
|
1889 |
val rew = rewrite_hol4_term eq thy''
|
|
1890 |
val crhs = cterm_of sg (#2 (Logic.dest_equals (prop_of rew)))
|
|
1891 |
val thy22 = if (def_name constname) = thmname
|
|
1892 |
then
|
|
1893 |
add_dump ("constdefs\n " ^ (quotename constname) ^ " :: \"" ^ (string_of_ctyp (ctyp_of sg ctype)) ^ "\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n " ^ (smart_string_of_cterm crhs)) thy''
|
|
1894 |
else
|
|
1895 |
add_dump ("consts\n " ^ (quotename constname) ^ " :: \"" ^ (string_of_ctyp (ctyp_of sg ctype)) ^
|
|
1896 |
"\" " ^ (Syntax.string_of_mixfix csyn) ^ "\n\ndefs\n " ^ (quotename thmname) ^ ": " ^ (smart_string_of_cterm crhs))
|
|
1897 |
thy''
|
|
1898 |
|
|
1899 |
val hth = case Shuffler.set_prop thy22 (HOLogic.mk_Trueprop tm24) [("",th)] of
|
|
1900 |
Some (_,res) => HOLThm(rens_of linfo,res)
|
|
1901 |
| None => raise ERR "new_definition" "Bad conclusion"
|
|
1902 |
val fullname = Sign.full_name sg thmname
|
|
1903 |
val thy22' = case opt_get_output_thy thy22 of
|
|
1904 |
"" => add_hol4_mapping thyname thmname fullname thy22
|
|
1905 |
| output_thy =>
|
|
1906 |
let
|
|
1907 |
val moved_thmname = output_thy ^ "." ^ thyname ^ "." ^ thmname
|
|
1908 |
in
|
|
1909 |
thy22 |> add_hol4_move fullname moved_thmname
|
|
1910 |
|> add_hol4_mapping thyname thmname moved_thmname
|
|
1911 |
end
|
|
1912 |
val _ = message "new_definition:"
|
|
1913 |
val _ = if_debug pth hth
|
|
1914 |
in
|
|
1915 |
(thy22',hth)
|
|
1916 |
end
|
|
1917 |
handle e => (message "exception in new_definition"; print_exn e)
|
|
1918 |
|
|
1919 |
val commafy = String.concat o separate ", "
|
|
1920 |
|
|
1921 |
local
|
|
1922 |
val helper = thm "termspec_help"
|
|
1923 |
in
|
|
1924 |
fun new_specification thyname thmname names hth thy =
|
|
1925 |
case HOL4DefThy.get thy of
|
|
1926 |
Replaying _ => (thy,hth)
|
|
1927 |
| _ =>
|
|
1928 |
let
|
|
1929 |
val _ = message "NEW_SPEC:"
|
|
1930 |
val _ = if_debug pth hth
|
|
1931 |
val names = map (rename_const thyname thy) names
|
|
1932 |
val _ = warning ("Introducing constants " ^ (commafy names))
|
|
1933 |
val (HOLThm(rens,th)) = norm_hthm (sign_of thy) hth
|
|
1934 |
val thy1 = case HOL4DefThy.get thy of
|
|
1935 |
Replaying _ => thy
|
|
1936 |
| _ =>
|
|
1937 |
let
|
|
1938 |
fun dest_eta_abs (Abs(x,xT,body)) = (x,xT,body)
|
|
1939 |
| dest_eta_abs body =
|
|
1940 |
let
|
|
1941 |
val (dT,rT) = dom_rng (type_of body)
|
|
1942 |
in
|
|
1943 |
("x",dT,body $ Bound 0)
|
|
1944 |
end
|
|
1945 |
handle TYPE _ => raise ERR "new_specification" "not an abstraction type"
|
|
1946 |
fun dest_exists (Const("Ex",_) $ abody) =
|
|
1947 |
dest_eta_abs abody
|
|
1948 |
| dest_exists tm =
|
|
1949 |
raise ERR "new_specification" "Bad existential formula"
|
|
1950 |
|
|
1951 |
val (consts,_) = foldl (fn ((cs,ex),cname) =>
|
|
1952 |
let
|
|
1953 |
val (_,cT,p) = dest_exists ex
|
|
1954 |
in
|
|
1955 |
((cname,cT,mk_syn cname)::cs,p)
|
|
1956 |
end) (([],HOLogic.dest_Trueprop (concl_of th)),names)
|
|
1957 |
val sg = sign_of thy
|
|
1958 |
val str = foldl (fn (acc,(c,T,csyn)) =>
|
|
1959 |
acc ^ "\n " ^ (quotename c) ^ " :: \"" ^ (string_of_ctyp (ctyp_of sg T)) ^ "\" " ^ (Syntax.string_of_mixfix csyn)) ("consts",consts)
|
|
1960 |
val thy' = add_dump str thy
|
|
1961 |
in
|
|
1962 |
Theory.add_consts_i consts thy'
|
|
1963 |
end
|
|
1964 |
|
|
1965 |
val thy1 = foldr (fn(name,thy)=>
|
|
1966 |
snd (get_defname thyname name thy)) (names,thy1)
|
|
1967 |
fun new_name name = fst (get_defname thyname name thy1)
|
|
1968 |
val (thy',res) = SpecificationPackage.add_specification_i None
|
|
1969 |
(map (fn name => (new_name name,name,false)) names)
|
|
1970 |
(thy1,th)
|
|
1971 |
val res' = Drule.freeze_all res
|
|
1972 |
val hth = HOLThm(rens,res')
|
|
1973 |
val rew = rewrite_hol4_term (concl_of res') thy'
|
|
1974 |
val th = equal_elim rew res'
|
|
1975 |
fun handle_const (name,thy) =
|
|
1976 |
let
|
|
1977 |
val defname = def_name name
|
|
1978 |
val (newname,thy') = get_defname thyname name thy
|
|
1979 |
in
|
|
1980 |
(if defname = newname
|
|
1981 |
then quotename name
|
|
1982 |
else (quotename newname) ^ ": " ^ (quotename name),thy')
|
|
1983 |
end
|
|
1984 |
val (new_names,thy') = foldr (fn(name,(names,thy)) =>
|
|
1985 |
let
|
|
1986 |
val (name',thy') = handle_const (name,thy)
|
|
1987 |
in
|
|
1988 |
(name'::names,thy')
|
|
1989 |
end) (names,([],thy'))
|
|
1990 |
val thy'' = add_dump ("specification (" ^ (spaces new_names) ^ ") " ^ thmname ^ ": " ^ (smart_string_of_thm th) ^
|
|
1991 |
"\n by (import " ^ thyname ^ " " ^ thmname ^ ")")
|
|
1992 |
thy'
|
|
1993 |
val _ = message "RESULT:"
|
|
1994 |
val _ = if_debug pth hth
|
|
1995 |
in
|
|
1996 |
intern_store_thm false thyname thmname hth thy''
|
|
1997 |
end
|
|
1998 |
handle e => (message "exception in new_specification"; print_exn e)
|
|
1999 |
|
|
2000 |
end
|
|
2001 |
|
|
2002 |
fun new_axiom name tm thy = raise ERR "new_axiom" ("Oh, no you don't! (" ^ name ^ ")")
|
|
2003 |
|
|
2004 |
fun to_isa_thm (hth as HOLThm(_,th)) =
|
|
2005 |
let
|
|
2006 |
val (HOLThm args) = norm_hthm (sign_of_thm th) hth
|
|
2007 |
in
|
|
2008 |
apsnd strip_shyps args
|
|
2009 |
end
|
|
2010 |
|
|
2011 |
fun to_isa_term tm = tm
|
|
2012 |
|
|
2013 |
local
|
|
2014 |
val light_nonempty = thm "light_ex_imp_nonempty"
|
|
2015 |
val ex_imp_nonempty = thm "ex_imp_nonempty"
|
|
2016 |
val typedef_hol2hol4 = thm "typedef_hol2hol4"
|
|
2017 |
val typedef_hol2hollight = thm "typedef_hol2hollight"
|
|
2018 |
in
|
|
2019 |
fun new_type_definition thyname thmname tycname hth thy =
|
|
2020 |
case HOL4DefThy.get thy of
|
|
2021 |
Replaying _ => (thy,hth)
|
|
2022 |
| _ =>
|
|
2023 |
let
|
|
2024 |
val _ = message "TYPE_DEF:"
|
|
2025 |
val _ = if_debug pth hth
|
|
2026 |
val _ = warning ("Introducing type " ^ tycname)
|
|
2027 |
val (HOLThm(rens,td_th)) = norm_hthm (sign_of thy) hth
|
|
2028 |
val th2 = beta_eta_thm (td_th RS ex_imp_nonempty)
|
|
2029 |
val c = case concl_of th2 of
|
|
2030 |
_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c
|
|
2031 |
| _ => raise ERR "new_type_definition" "Bad type definition theorem"
|
|
2032 |
val tfrees = term_tfrees c
|
|
2033 |
val tnames = map fst tfrees
|
|
2034 |
val tsyn = mk_syn tycname
|
|
2035 |
val typ = (tycname,tnames,tsyn)
|
|
2036 |
val (thy',typedef_info) = TypedefPackage.add_typedef_i false (Some thmname) typ c None (rtac th2 1) thy
|
|
2037 |
|
|
2038 |
val th3 = (#type_definition typedef_info) RS typedef_hol2hol4
|
|
2039 |
|
|
2040 |
val fulltyname = Sign.intern_tycon (sign_of thy') tycname
|
|
2041 |
val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
|
|
2042 |
|
|
2043 |
val sg = sign_of thy''
|
|
2044 |
val (hth' as HOLThm args) = norm_hthm sg (HOLThm(rens,th3))
|
|
2045 |
val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating")
|
|
2046 |
else ()
|
|
2047 |
val thy4 = add_hol4_pending thyname thmname args thy''
|
|
2048 |
|
|
2049 |
val sg = sign_of thy4
|
|
2050 |
val rew = rewrite_hol4_term (concl_of td_th) thy4
|
|
2051 |
val th = equal_elim rew (transfer_sg sg td_th)
|
|
2052 |
val c = case HOLogic.dest_Trueprop (prop_of th) of
|
|
2053 |
Const("Ex",exT) $ P =>
|
|
2054 |
let
|
|
2055 |
val PT = domain_type exT
|
|
2056 |
in
|
|
2057 |
Const("Collect",PT-->HOLogic.mk_setT (domain_type PT)) $ P
|
|
2058 |
end
|
|
2059 |
| _ => error "Internal error in ProofKernel.new_typedefinition"
|
|
2060 |
val tnames_string = if null tnames
|
|
2061 |
then ""
|
|
2062 |
else "(" ^ (commafy tnames) ^ ") "
|
|
2063 |
val proc_prop = if null tnames
|
|
2064 |
then smart_string_of_cterm
|
|
2065 |
else Library.setmp show_all_types true smart_string_of_cterm
|
|
2066 |
val thy5 = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ " = " ^ (proc_prop (cterm_of sg c)) ^ " " ^ (Syntax.string_of_mixfix tsyn) ^ "\n by (rule typedef_helper,import " ^ thyname ^ " " ^ thmname ^ ")") thy4
|
|
2067 |
val thy6 = add_dump ("lemmas " ^ thmname ^ " = typedef_hol2hol4 [OF type_definition_" ^ tycname ^ "]")
|
|
2068 |
thy5
|
|
2069 |
val _ = message "RESULT:"
|
|
2070 |
val _ = if_debug pth hth'
|
|
2071 |
in
|
|
2072 |
(thy6,hth')
|
|
2073 |
end
|
|
2074 |
handle e => (message "exception in new_type_definition"; print_exn e)
|
|
2075 |
|
|
2076 |
fun type_introduction thyname thmname tycname abs_name rep_name (P,t) hth thy =
|
|
2077 |
case HOL4DefThy.get thy of
|
|
2078 |
Replaying _ => (thy,hth)
|
|
2079 |
| _ =>
|
|
2080 |
let
|
|
2081 |
val _ = message "TYPE_INTRO:"
|
|
2082 |
val _ = if_debug pth hth
|
|
2083 |
val _ = warning ("Introducing type " ^ tycname ^ " (with morphisms " ^ abs_name ^ " and " ^ rep_name ^ ")")
|
|
2084 |
val (HOLThm(rens,td_th)) = norm_hthm (sign_of thy) hth
|
|
2085 |
val sg = sign_of thy
|
|
2086 |
val tT = type_of t
|
|
2087 |
val light_nonempty' =
|
|
2088 |
Drule.instantiate' [Some (ctyp_of sg tT)]
|
|
2089 |
[Some (cterm_of sg P),
|
|
2090 |
Some (cterm_of sg t)] light_nonempty
|
|
2091 |
val th2 = beta_eta_thm (td_th RS (beta_eta_thm light_nonempty'))
|
|
2092 |
val c = case concl_of th2 of
|
|
2093 |
_ $ (Const("Ex",_) $ Abs(_,_,Const("op :",_) $ _ $ c)) => c
|
|
2094 |
| _ => raise ERR "type_introduction" "Bad type definition theorem"
|
|
2095 |
val tfrees = term_tfrees c
|
|
2096 |
val tnames = map fst tfrees
|
|
2097 |
val tsyn = mk_syn tycname
|
|
2098 |
val typ = (tycname,tnames,tsyn)
|
|
2099 |
val (thy',typedef_info) = TypedefPackage.add_typedef_i false (Some thmname) typ c (Some(rep_name,abs_name)) (rtac th2 1) thy
|
|
2100 |
|
|
2101 |
val th3 = (#type_definition typedef_info) RS typedef_hol2hollight
|
|
2102 |
|
|
2103 |
val th4 = Drule.freeze_all th3
|
|
2104 |
val fulltyname = Sign.intern_tycon (sign_of thy') tycname
|
|
2105 |
val thy'' = add_hol4_type_mapping thyname tycname true fulltyname thy'
|
|
2106 |
|
|
2107 |
val sg = sign_of thy''
|
|
2108 |
val (hth' as HOLThm args) = norm_hthm sg (HOLThm(rens,th4))
|
|
2109 |
val _ = if #maxidx (rep_thm th4) <> ~1
|
|
2110 |
then (Library.setmp show_types true pth hth' ; error "SCHEME!")
|
|
2111 |
else ()
|
|
2112 |
val _ = if has_ren hth' then warning ("Theorem " ^ thmname ^ " needs variable-disambiguating")
|
|
2113 |
else ()
|
|
2114 |
val thy4 = add_hol4_pending thyname thmname args thy''
|
|
2115 |
|
|
2116 |
val sg = sign_of thy4
|
|
2117 |
val P' = #2 (Logic.dest_equals (concl_of (rewrite_hol4_term P thy4)))
|
|
2118 |
val c =
|
|
2119 |
let
|
|
2120 |
val PT = type_of P'
|
|
2121 |
in
|
|
2122 |
Const("Collect",PT-->HOLogic.mk_setT (domain_type PT)) $ P'
|
|
2123 |
end
|
|
2124 |
|
|
2125 |
val tnames_string = if null tnames
|
|
2126 |
then ""
|
|
2127 |
else "(" ^ (commafy tnames) ^ ") "
|
|
2128 |
val proc_prop = if null tnames
|
|
2129 |
then smart_string_of_cterm
|
|
2130 |
else Library.setmp show_all_types true smart_string_of_cterm
|
|
2131 |
val thy5 = add_dump ("typedef (open) " ^ tnames_string ^ (quotename tycname) ^ " = " ^ (proc_prop (cterm_of sg c)) ^ " " ^ (Syntax.string_of_mixfix tsyn) ^ "\n by (rule light_ex_imp_nonempty,import " ^ thyname ^ " " ^ thmname ^ ")") thy4
|
|
2132 |
val thy6 = add_dump ("lemmas " ^ thmname ^ " = typedef_hol2hollight [OF type_definition_" ^ tycname ^ "]")
|
|
2133 |
thy5
|
|
2134 |
val _ = message "RESULT:"
|
|
2135 |
val _ = if_debug pth hth'
|
|
2136 |
in
|
|
2137 |
(thy6,hth')
|
|
2138 |
end
|
|
2139 |
handle e => (message "exception in type_introduction"; print_exn e)
|
|
2140 |
end
|
|
2141 |
|
|
2142 |
end
|