author | wenzelm |
Wed, 20 Apr 2011 16:49:52 +0200 | |
changeset 42429 | 7691cc61720a |
parent 42361 | 23f352990944 |
child 43883 | aacbe67793c3 |
permissions | -rw-r--r-- |
37744 | 1 |
(* Title: HOL/Mutabelle/mutabelle.ML |
34967 | 2 |
Author: Veronika Ortner, TU Muenchen |
34965 | 3 |
|
41408 | 4 |
Mutation of theorems. |
34965 | 5 |
*) |
41408 | 6 |
|
34965 | 7 |
signature MUTABELLE = |
8 |
sig |
|
9 |
val testgen_name : string Unsynchronized.ref |
|
10 |
exception WrongPath of string; |
|
11 |
exception WrongArg of string; |
|
12 |
val freeze : term -> term |
|
13 |
val mutate_exc : term -> string list -> int -> term list |
|
14 |
val mutate_sign : term -> theory -> (string * string) list -> int -> term list |
|
15 |
val mutate_mix : term -> theory -> string list -> |
|
16 |
(string * string) list -> int -> term list |
|
17 |
val qc_test : term list -> (typ * typ) list -> theory -> |
|
18 |
int -> int -> int * Thm.cterm list * int * (Thm.cterm * (string * Thm.cterm) list) list |
|
19 |
val qc_test_file : bool -> term list -> (typ * typ) list |
|
20 |
-> theory -> int -> int -> string -> unit |
|
21 |
val mutqc_file_exc : theory -> string list -> |
|
22 |
int -> Thm.thm -> (typ * typ) list -> int -> int -> string -> unit |
|
23 |
val mutqc_file_sign : theory -> (string * string) list -> |
|
24 |
int -> Thm.thm -> (typ * typ) list -> int -> int -> string -> unit |
|
25 |
val mutqc_file_mix : theory -> string list -> (string * string) list -> |
|
26 |
int -> Thm.thm -> (typ * typ) list -> int -> int -> string -> unit |
|
27 |
val mutqc_file_rec_exc : theory -> string list -> int -> |
|
28 |
Thm.thm list -> (typ * typ) list -> int -> int -> string list -> unit |
|
29 |
val mutqc_file_rec_sign : theory -> (string * string) list -> int -> |
|
30 |
Thm.thm list -> (typ * typ) list -> int -> int -> string list -> unit |
|
31 |
val mutqc_file_rec_mix : theory -> string list -> (string * string) list -> |
|
32 |
int -> Thm.thm list -> (typ * typ) list -> int -> int -> string list -> unit |
|
33 |
val mutqc_thy_exc : theory -> theory -> |
|
34 |
string list -> int -> (typ * typ) list -> int -> int -> string -> unit |
|
35 |
val mutqc_thy_sign : theory -> theory -> (string * string) list -> |
|
36 |
int -> (typ * typ) list -> int -> int -> string -> unit |
|
37 |
val mutqc_thy_mix : theory -> theory -> string list -> (string * string) list -> |
|
38 |
int -> (typ * typ) list -> int -> int -> string -> unit |
|
39 |
val mutqc_file_stat_sign : theory -> (string * string) list -> |
|
40 |
int -> Thm.thm list -> (typ * typ) list -> int -> int -> string -> unit |
|
41 |
val mutqc_file_stat_exc : theory -> string list -> |
|
42 |
int -> Thm.thm list -> (typ * typ) list -> int -> int -> string -> unit |
|
43 |
val mutqc_file_stat_mix : theory -> string list -> (string * string) list -> |
|
44 |
int -> Thm.thm list -> (typ * typ) list -> int -> int -> string -> unit |
|
45 |
val mutqc_thystat_exc : (string -> thm -> bool) -> theory -> theory -> |
|
46 |
string list -> int -> (typ * typ) list -> int -> int -> string -> unit |
|
47 |
val mutqc_thystat_sign : (string -> thm -> bool) -> theory -> theory -> (string * string) list -> |
|
48 |
int -> (typ * typ) list -> int -> int -> string -> unit |
|
49 |
val mutqc_thystat_mix : (string -> thm -> bool) -> theory -> theory -> string list -> |
|
50 |
(string * string) list -> int -> (typ * typ) list -> int -> int -> string -> unit |
|
51 |
val canonize_term: term -> string list -> term |
|
52 |
||
53 |
val all_unconcealed_thms_of : theory -> (string * thm) list |
|
54 |
end; |
|
55 |
||
56 |
structure Mutabelle : MUTABELLE = |
|
57 |
struct |
|
58 |
||
40920 | 59 |
val testgen_name = Unsynchronized.ref "random"; |
34965 | 60 |
|
61 |
fun all_unconcealed_thms_of thy = |
|
62 |
let |
|
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
37863
diff
changeset
|
63 |
val facts = Global_Theory.facts_of thy |
34965 | 64 |
in |
65 |
Facts.fold_static |
|
66 |
(fn (s, ths) => |
|
67 |
if Facts.is_concealed facts s then I else append (map (`(Thm.get_name_hint)) ths)) |
|
68 |
facts [] |
|
69 |
end; |
|
70 |
||
71 |
fun thms_of thy = filter (fn (_, th) => |
|
72 |
Context.theory_name (theory_of_thm th) = Context.theory_name thy) (all_unconcealed_thms_of thy); |
|
73 |
||
74 |
fun consts_of thy = |
|
75 |
let |
|
76 |
val (namespace, const_table) = #constants (Consts.dest (Sign.consts_of thy)) |
|
77 |
val consts = Symtab.dest const_table |
|
78 |
in |
|
42429 | 79 |
map_filter (fn (s, (T, NONE)) => SOME (s, T) | _ => NONE) |
34965 | 80 |
(filter_out (fn (s, _) => Name_Space.is_concealed namespace s) consts) |
81 |
end; |
|
82 |
||
83 |
||
41408 | 84 |
(*possibility to print a given term for debugging purposes*) |
34965 | 85 |
|
37863
7f113caabcf4
discontinued pervasive val theory = Thy_Info.get_theory -- prefer antiquotations in most situations;
wenzelm
parents:
37744
diff
changeset
|
86 |
fun prt x = Pretty.writeln (Syntax.pretty_term_global @{theory Main} x); |
34965 | 87 |
|
88 |
val debug = (Unsynchronized.ref false); |
|
89 |
fun debug_msg mutterm = if (!debug) then (prt mutterm) else (); |
|
90 |
||
91 |
||
92 |
(*thrown in case the specified path doesn't exist in the specified term*) |
|
93 |
||
94 |
exception WrongPath of string; |
|
95 |
||
96 |
||
97 |
(*thrown in case the arguments did not fit to the function*) |
|
98 |
||
99 |
exception WrongArg of string; |
|
100 |
||
101 |
(*Rename the bound variables in a term with the minimal Index min of |
|
102 |
bound variables. Variable (Bound(min)) will be renamed to Bound(0) etc. |
|
103 |
This is needed in course auf evaluation of contexts.*) |
|
104 |
||
105 |
fun rename_bnds curTerm 0 = curTerm |
|
106 |
| rename_bnds (Bound(i)) minInd = |
|
107 |
let |
|
108 |
val erg = if (i-minInd < 0) then 0 else (i - minInd) |
|
109 |
in |
|
110 |
Bound(erg) |
|
111 |
end |
|
112 |
| rename_bnds (Abs(name,t,uTerm)) minInd = |
|
113 |
Abs(name,t,(rename_bnds uTerm minInd)) |
|
114 |
| rename_bnds (fstUTerm $ sndUTerm) minInd = |
|
115 |
(rename_bnds fstUTerm minInd) $ (rename_bnds sndUTerm minInd) |
|
116 |
| rename_bnds elseTerm minInd = elseTerm; |
|
117 |
||
118 |
||
119 |
||
120 |
||
121 |
||
122 |
(*Partition a term in its subterms and create an entry |
|
123 |
(term * type * abscontext * mincontext * path) |
|
124 |
for each term in the return list |
|
125 |
e.g: getSubTermList Abs(y, int, Const(f,int->int) $ Const(x,int) $ Bound(0)) |
|
126 |
will give [(Const(f,int->int),int->int,[int],[],[00]), |
|
127 |
(Const(x,int),int,[int],[],[010]), |
|
128 |
(Bound(0),int,[int],[int],[110]), |
|
129 |
(Const(x,int) $ Bound(0),type,[int],[int],[10]), |
|
130 |
(Const(f,int->int) $ Const(x,int) $ Bound(0),type,[int],[int],[0], |
|
131 |
(Abs (y,int,Const(f,int->int) $ const(x,int) $ Bound(0)),type,[],[],[])] |
|
132 |
*) |
|
133 |
||
134 |
fun getSubTermList (Const(name,t)) abscontext path acc = |
|
135 |
(Const(name,t),t,abscontext,abscontext,path)::acc |
|
136 |
| getSubTermList (Free(name,t)) abscontext path acc = |
|
137 |
(Free(name,t),t,abscontext,abscontext,path)::acc |
|
138 |
| getSubTermList (Var(indname,t)) abscontext path acc = |
|
139 |
(Var(indname,t),t,abscontext,abscontext,path)::acc |
|
140 |
| getSubTermList (Bound(i)) abscontext path acc = |
|
141 |
(Bound(0),nth abscontext i,abscontext, Library.drop i abscontext,path)::acc |
|
142 |
| getSubTermList (Abs(name,t,uTerm)) abscontext path acc = |
|
143 |
let |
|
144 |
val curTerm = Abs(name,t,uTerm) |
|
145 |
val bnos = Term.add_loose_bnos (curTerm,0,[]) |
|
146 |
val minInd = if (bnos = []) then 0 |
|
147 |
else Library.foldl (fn (n,m) => if (n<m) then n else m) (hd bnos,tl bnos) |
|
148 |
val newTerm = rename_bnds curTerm minInd |
|
149 |
val newContext = Library.drop minInd abscontext |
|
150 |
in |
|
151 |
getSubTermList uTerm (t::abscontext) (0::path) |
|
152 |
((newTerm,(fastype_of1 (abscontext, curTerm)),abscontext,newContext,path)::acc) |
|
153 |
end |
|
154 |
| getSubTermList (fstUTerm $ sndUTerm) abscontext path acc = |
|
155 |
let |
|
156 |
val curTerm = (fstUTerm $ sndUTerm) |
|
157 |
val bnos = Term.add_loose_bnos (curTerm, 0, []) |
|
158 |
val minInd = if (bnos = []) then 0 |
|
159 |
else Library.foldl (fn (n,m) => if (n<m) then n else m) (hd bnos,tl bnos) |
|
160 |
val newTerm = rename_bnds curTerm minInd |
|
161 |
val newContext = Library.drop minInd abscontext |
|
162 |
in |
|
163 |
getSubTermList fstUTerm abscontext (0::path) |
|
164 |
(getSubTermList sndUTerm abscontext (1::path) |
|
165 |
((newTerm,(fastype_of1 (abscontext, curTerm)),abscontext,newContext,path)::acc)) |
|
166 |
end; |
|
167 |
||
168 |
||
169 |
||
170 |
||
171 |
||
172 |
(*tests if the given element ist in the given list*) |
|
173 |
||
174 |
fun in_list elem [] = false |
|
175 |
| in_list elem (x::xs) = if (elem = x) then true else in_list elem xs; |
|
176 |
||
177 |
||
178 |
(*Evaluate if the longContext is more special as the shortContext. |
|
179 |
If so, a term with shortContext can be substituted in the place of a |
|
180 |
term with longContext*) |
|
181 |
||
182 |
fun is_morespecial longContext shortContext = |
|
183 |
let |
|
184 |
val revlC = rev longContext |
|
185 |
val revsC = rev shortContext |
|
186 |
fun is_prefix [] longList = true |
|
187 |
| is_prefix shList [] = false |
|
188 |
| is_prefix (x::xs) (y::ys) = if (x=y) then is_prefix xs ys else false |
|
189 |
in |
|
190 |
is_prefix revsC revlC |
|
191 |
end; |
|
192 |
||
193 |
||
194 |
(*takes a (term * type * context * context * path)-tupel and searches in the specified list for |
|
195 |
terms with the same type and appropriate context. Returns a (term * path) list of these terms. |
|
196 |
Used in order to generate a list of type-equal subterms of the original term*) |
|
197 |
||
198 |
fun searchForMutatableSubTerm (sterm,stype,sabsContext,sminContext,spath) [] resultList = |
|
199 |
resultList |
|
200 |
| searchForMutatableSubTerm (sterm,stype,sabsContext,sminContext,spath) |
|
201 |
((hdterm,hdtype,hdabsContext,hdminContext,hdpath)::xs) resultList = |
|
202 |
if ((stype = hdtype) andalso (is_morespecial sabsContext hdminContext) |
|
203 |
andalso (is_morespecial hdabsContext sminContext)) |
|
204 |
then searchForMutatableSubTerm (sterm,stype,sabsContext,sminContext,spath) xs |
|
205 |
((hdterm,hdabsContext,hdminContext,hdpath)::resultList) |
|
206 |
else searchForMutatableSubTerm (sterm,stype,sabsContext,sminContext,spath) xs resultList; |
|
207 |
||
208 |
||
209 |
(*evaluates if the given function is in the passed list of forbidden functions*) |
|
210 |
||
211 |
fun in_list_forb consSig (consNameStr,consType) [] = false |
|
212 |
| in_list_forb consSig (consNameStr,consType) ((forbNameStr,forbTypeStr)::xs) = |
|
213 |
let |
|
214 |
val forbType = Syntax.read_typ_global consSig forbTypeStr |
|
215 |
val typeSignature = #tsig (Sign.rep_sg consSig) |
|
216 |
in |
|
217 |
if ((consNameStr = forbNameStr) |
|
35845
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
wenzelm
parents:
35625
diff
changeset
|
218 |
andalso (Type.typ_instance typeSignature (consType,(Logic.varifyT_global forbType)))) |
34965 | 219 |
then true |
220 |
else in_list_forb consSig (consNameStr,consType) xs |
|
221 |
end; |
|
222 |
||
223 |
||
224 |
||
225 |
(*searches in the given signature Consts with the same type as sterm and |
|
226 |
returns a list of those terms*) |
|
227 |
||
228 |
fun searchForSignatureMutations (sterm,stype) consSig forbidden_funs = |
|
229 |
let |
|
230 |
val sigConsTypeList = consts_of consSig; |
|
231 |
val typeSignature = #tsig (Sign.rep_sg consSig) |
|
232 |
in |
|
233 |
let |
|
234 |
fun recursiveSearch mutatableTermList [] = mutatableTermList |
|
235 |
| recursiveSearch mutatableTermList ((ConsName,ConsType)::xs) = |
|
236 |
if (Type.typ_instance typeSignature (stype,ConsType) |
|
237 |
andalso (not (sterm = Const(ConsName,stype))) |
|
238 |
andalso (not (in_list_forb consSig (ConsName,ConsType) forbidden_funs))) |
|
239 |
then recursiveSearch ((Term.Const(ConsName,stype), [], [], [5])::mutatableTermList) xs |
|
240 |
else recursiveSearch mutatableTermList xs |
|
241 |
in |
|
242 |
recursiveSearch [] sigConsTypeList |
|
243 |
end |
|
244 |
end; |
|
245 |
||
246 |
||
247 |
(*generates a list of terms that can be used instead of the passed subterm in the original term. These terms either have |
|
248 |
the same type and appropriate context and are generated from the list of subterms either - in case of a Const-term they have been found |
|
249 |
in the current signature. |
|
250 |
This function has 3 versions: |
|
251 |
0: no instertion of signature functions, |
|
252 |
only terms in the subTermList with the same type and appropriate context as the passed term are returned |
|
253 |
1: no exchange of subterms, |
|
254 |
only signature functions are inserted at the place of type-aequivalent Conses |
|
255 |
2: mixture of the two other versions. insertion of signature functions and exchange of subterms*) |
|
256 |
||
257 |
fun searchForMutatableTerm 0 (sterm,stype,sabscontext,smincontext,spath) |
|
258 |
subTerms consSig resultList forbidden_funs = |
|
259 |
searchForMutatableSubTerm (sterm,stype,sabscontext,smincontext,spath) subTerms resultList |
|
260 |
| searchForMutatableTerm 1 (Const(constName,constType),stype,sabscontext,smincontext,spath) |
|
261 |
subTerms consSig resultList forbidden_funs = |
|
262 |
searchForSignatureMutations (Const(constName,constType),stype) consSig forbidden_funs |
|
263 |
| searchForMutatableTerm 1 _ _ _ _ _ = [] |
|
264 |
| searchForMutatableTerm 2 (Const(constName,constType),stype,sabscontext,smincontext,spath) |
|
265 |
subTerms consSig resultList forbidden_funs = |
|
266 |
let |
|
267 |
val subtermMutations = searchForMutatableSubTerm |
|
268 |
(Const(constName,constType),stype,sabscontext,smincontext,spath) subTerms resultList |
|
269 |
val signatureMutations = searchForSignatureMutations |
|
270 |
(Const(constName,constType),stype) consSig forbidden_funs |
|
271 |
in |
|
272 |
subtermMutations@signatureMutations |
|
273 |
end |
|
274 |
| searchForMutatableTerm 2 (sterm,stype,sabscontext,smincontext,spath) |
|
275 |
subTerms consSig resultList forbidden_funs = |
|
276 |
searchForMutatableSubTerm (sterm,stype,sabscontext,smincontext,spath) subTerms resultList |
|
277 |
| searchForMutatableTerm i _ _ _ _ _ = |
|
278 |
raise WrongArg("Version " ^ string_of_int i ^ |
|
279 |
" doesn't exist for function searchForMutatableTerm!") ; |
|
280 |
||
281 |
||
282 |
||
283 |
||
284 |
(*evaluates if the two terms with paths passed as arguments can be exchanged, i.e. evaluates if one of the terms is a subterm of the other one*) |
|
285 |
||
286 |
fun areReplacable [] [] = false |
|
287 |
| areReplacable fstPath [] = false |
|
288 |
| areReplacable [] sndPath = false |
|
289 |
| areReplacable (x::xs) (y::ys) = if (x=y) then areReplacable xs ys else true; |
|
290 |
||
291 |
||
292 |
||
293 |
||
294 |
(*substitutes the term at the position of the first list in fstTerm by sndTerm. |
|
295 |
The lists represent paths as generated by createSubTermList*) |
|
296 |
||
297 |
fun substitute [] fstTerm sndTerm = sndTerm |
|
298 |
| substitute (_::xs) (Abs(s,T,subTerm)) sndTerm = Abs(s,T,(substitute xs subTerm sndTerm)) |
|
299 |
| substitute (0::xs) (t $ u) sndTerm = substitute xs t sndTerm $ u |
|
300 |
| substitute (1::xs) (t $ u) sndTerm = t $ substitute xs u sndTerm |
|
301 |
| substitute (_::xs) _ sndTerm = |
|
302 |
raise WrongPath ("The Term could not be found at the specified position"); |
|
303 |
||
304 |
||
305 |
(*get the subterm with the specified path in myTerm*) |
|
306 |
||
307 |
fun getSubTerm myTerm [] = myTerm |
|
308 |
| getSubTerm (Abs(s,T,subTerm)) (0::xs) = getSubTerm subTerm xs |
|
309 |
| getSubTerm (t $ u) (0::xs) = getSubTerm t xs |
|
310 |
| getSubTerm (t $ u) (1::xs) = getSubTerm u xs |
|
311 |
| getSubTerm _ (_::xs) = |
|
312 |
raise WrongPath ("The subterm could not be found at the specified position"); |
|
313 |
||
314 |
||
315 |
(*exchanges two subterms with the given paths in the original Term*) |
|
316 |
||
317 |
fun replace origTerm (fstTerm, fstPath) (sndTerm, sndPath) = |
|
318 |
if (areReplacable (rev fstPath) (rev sndPath)) |
|
319 |
then substitute (rev sndPath) (substitute (rev fstPath) origTerm sndTerm) fstTerm |
|
320 |
else origTerm; |
|
321 |
||
322 |
||
323 |
||
324 |
||
325 |
(*tests if the terms with the given pathes in the origTerm are commutative |
|
326 |
respecting the list of commutative operators (commutatives)*) |
|
327 |
||
328 |
fun areCommutative origTerm fstPath sndPath commutatives = |
|
329 |
if (sndPath = []) |
|
330 |
then false |
|
331 |
else |
|
332 |
let |
|
333 |
val base = (tl sndPath) |
|
334 |
in |
|
335 |
let |
|
336 |
val fstcomm = 1::0::base |
|
337 |
val opcomm = 0::0::base |
|
338 |
in |
|
339 |
if ((fstPath = fstcomm) andalso (is_Const (getSubTerm origTerm (rev opcomm)))) |
|
340 |
then |
|
341 |
let |
|
342 |
val Const(name,_) = (getSubTerm origTerm (rev opcomm)) |
|
343 |
in |
|
344 |
if (in_list name commutatives) |
|
345 |
then true |
|
346 |
else false |
|
347 |
end |
|
348 |
else false |
|
349 |
end |
|
350 |
end; |
|
351 |
||
352 |
||
353 |
(*Canonizes term t with the commutative operators stored in list |
|
354 |
commutatives*) |
|
355 |
||
356 |
fun canonize_term (Const (s, T) $ t $ u) comms = |
|
357 |
let |
|
358 |
val t' = canonize_term t comms; |
|
359 |
val u' = canonize_term u comms; |
|
360 |
in |
|
36692
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents:
36610
diff
changeset
|
361 |
if member (op =) comms s andalso Term_Ord.termless (u', t') |
34965 | 362 |
then Const (s, T) $ u' $ t' |
363 |
else Const (s, T) $ t' $ u' |
|
364 |
end |
|
365 |
| canonize_term (t $ u) comms = canonize_term t comms $ canonize_term u comms |
|
366 |
| canonize_term (Abs (s, T, t)) comms = Abs (s, T, canonize_term t comms) |
|
367 |
| canonize_term t comms = t; |
|
368 |
||
369 |
||
370 |
(*inspect the passed list and mutate origTerm following the elements of the list: |
|
371 |
if the path of the current element is [5] (dummy path), the term has been found in the signature |
|
372 |
and the subterm will be substituted by it |
|
373 |
else the term has been found in the original term and the two subterms have to be exchanged |
|
374 |
The additional parameter commutatives indicates the commutative operators |
|
375 |
in the term whose operands won't be exchanged*) |
|
376 |
||
377 |
fun createMutatedTerms origTerm _ [] commutatives mutatedTerms = mutatedTerms |
|
378 |
| createMutatedTerms origTerm (hdt as (hdTerm,hdabsContext,hdminContext,hdPath)) |
|
379 |
((sndTerm,sndabsContext,sndminContext,sndPath)::xs) commutatives mutatedTerms = |
|
380 |
if (sndPath = [5]) |
|
381 |
then |
|
382 |
let |
|
383 |
val canonized = canonize_term (substitute (rev hdPath) origTerm sndTerm) commutatives |
|
384 |
in |
|
385 |
if (canonized = origTerm) |
|
386 |
then createMutatedTerms origTerm hdt xs commutatives mutatedTerms |
|
387 |
else createMutatedTerms origTerm hdt xs commutatives |
|
388 |
(insert op aconv canonized mutatedTerms) |
|
389 |
end |
|
390 |
else |
|
391 |
if ((areCommutative origTerm hdPath sndPath commutatives) |
|
392 |
orelse (areCommutative origTerm sndPath hdPath commutatives)) |
|
393 |
then createMutatedTerms origTerm hdt xs commutatives mutatedTerms |
|
394 |
else |
|
395 |
let |
|
396 |
val canonized = canonize_term |
|
397 |
(replace origTerm |
|
398 |
(incr_boundvars (length sndabsContext - length hdminContext) hdTerm, |
|
399 |
hdPath) |
|
400 |
(incr_boundvars (length hdabsContext - length sndminContext) sndTerm, |
|
401 |
sndPath)) commutatives |
|
402 |
in |
|
403 |
if (not(canonized = origTerm)) |
|
404 |
then createMutatedTerms origTerm hdt xs commutatives |
|
405 |
(insert op aconv canonized mutatedTerms) |
|
406 |
else createMutatedTerms origTerm hdt xs commutatives mutatedTerms |
|
407 |
end; |
|
408 |
||
409 |
||
410 |
||
411 |
(*mutates origTerm by exchanging subterms. The mutated terms are returned in a term list |
|
412 |
The parameter commutatives consists of a list of commutative operators. The permutation of their |
|
413 |
operands won't be considered as a new term |
|
414 |
!!!Attention!!!: The given origTerm must be canonized. Use function canonize_term!*) |
|
415 |
||
416 |
fun mutate_once option origTerm tsig commutatives forbidden_funs= |
|
417 |
let |
|
418 |
val subTermList = getSubTermList origTerm [] [] [] |
|
419 |
in |
|
420 |
let |
|
421 |
fun replaceRecursively [] mutatedTerms = mutatedTerms |
|
422 |
| replaceRecursively ((hdTerm,hdType,hdabsContext,hdminContext,hdPath)::tail) |
|
423 |
mutatedTerms = |
|
424 |
replaceRecursively tail (union op aconv (createMutatedTerms origTerm |
|
425 |
(hdTerm,hdabsContext,hdminContext,hdPath) |
|
426 |
(searchForMutatableTerm option (hdTerm,hdType,hdabsContext,hdminContext,hdPath) |
|
427 |
tail tsig [] forbidden_funs) |
|
428 |
commutatives []) mutatedTerms) |
|
429 |
in |
|
430 |
replaceRecursively subTermList [] |
|
431 |
end |
|
432 |
end; |
|
433 |
||
434 |
||
435 |
||
436 |
||
437 |
(*helper function in order to apply recursively the mutate_once function on a whole list of terms |
|
438 |
Needed for the mutate function*) |
|
439 |
||
440 |
fun mutate_once_rec option [] tsig commutatives forbidden_funs acc = acc |
|
441 |
| mutate_once_rec option (x::xs) tsig commutatives forbidden_funs acc = |
|
442 |
mutate_once_rec option xs tsig commutatives forbidden_funs |
|
443 |
(union op aconv (mutate_once option x tsig commutatives forbidden_funs) acc); |
|
444 |
||
445 |
||
446 |
||
447 |
(*apply function mutate_once iter times on the given origTerm. *) |
|
448 |
(*call of mutiere with canonized form of origTerm. Prevents us of the computation of |
|
449 |
canonization in the course of insertion of new terms!*) |
|
450 |
||
451 |
fun mutate option origTerm tsig commutatives forbidden_funs 0 = [] |
|
452 |
| mutate option origTerm tsig commutatives forbidden_funs 1 = |
|
453 |
mutate_once option (canonize_term origTerm commutatives) tsig commutatives forbidden_funs |
|
454 |
| mutate option origTerm tsig commutatives forbidden_funs iter = |
|
455 |
mutate_once_rec option (mutate option origTerm tsig commutatives forbidden_funs (iter-1)) |
|
456 |
tsig commutatives forbidden_funs []; |
|
457 |
||
458 |
(*mutate origTerm iter times by only exchanging subterms*) |
|
459 |
||
460 |
fun mutate_exc origTerm commutatives iter = |
|
37863
7f113caabcf4
discontinued pervasive val theory = Thy_Info.get_theory -- prefer antiquotations in most situations;
wenzelm
parents:
37744
diff
changeset
|
461 |
mutate 0 origTerm @{theory Main} commutatives [] iter; |
34965 | 462 |
|
463 |
||
464 |
(*mutate origTerm iter times by only inserting signature functions*) |
|
465 |
||
466 |
fun mutate_sign origTerm tsig forbidden_funs iter = |
|
467 |
mutate 1 origTerm tsig [] forbidden_funs iter; |
|
468 |
||
469 |
||
470 |
(*mutate origTerm iter times by exchange of subterms and insertion of subterms*) |
|
471 |
||
472 |
fun mutate_mix origTerm tsig commutatives forbidden_funs iter = |
|
473 |
mutate 2 origTerm tsig commutatives forbidden_funs iter; |
|
474 |
||
475 |
||
476 |
(*helper function in order to prettily print a list of terms*) |
|
477 |
||
478 |
fun pretty xs = map (fn (id, t) => (id, (HOLogic.mk_number HOLogic.natT |
|
479 |
(HOLogic.dest_nat t)) handle TERM _ => t)) xs; |
|
480 |
||
481 |
||
482 |
(*helper function for the quickcheck invocation. Evaluates the quickcheck_term function on a whole list of terms |
|
483 |
and tries to print the exceptions*) |
|
484 |
||
485 |
fun freeze (t $ u) = freeze t $ freeze u |
|
486 |
| freeze (Abs (s, T, t)) = Abs (s, T, freeze t) |
|
487 |
| freeze (Var ((a, i), T)) = |
|
488 |
Free (if i = 0 then a else a ^ "_" ^ string_of_int i, T) |
|
489 |
| freeze t = t; |
|
490 |
||
491 |
fun inst_type insts (Type (s, Ts)) = Type (s, map (inst_type insts) Ts) |
|
492 |
| inst_type insts T = the_default HOLogic.intT (AList.lookup op = insts T); |
|
493 |
||
35625 | 494 |
fun preprocess thy insts t = Object_Logic.atomize_term thy |
34965 | 495 |
(map_types (inst_type insts) (freeze t)); |
496 |
||
497 |
fun is_executable thy insts th = |
|
40906 | 498 |
((Quickcheck.test_term |
42361 | 499 |
(Proof_Context.init_global thy |
40920 | 500 |
|> Config.put Quickcheck.size 1 |
501 |
|> Config.put Quickcheck.iterations 1 |
|
502 |
|> Config.put Quickcheck.tester (!testgen_name)) |
|
42030 | 503 |
(true, false) (preprocess thy insts (prop_of th), []); |
40248 | 504 |
Output.urgent_message "executable"; true) handle ERROR s => |
505 |
(Output.urgent_message ("not executable: " ^ s); false)); |
|
34965 | 506 |
|
507 |
fun qc_recursive usedthy [] insts sz qciter acc = rev acc |
|
508 |
| qc_recursive usedthy (x::xs) insts sz qciter acc = qc_recursive usedthy xs insts sz qciter |
|
40248 | 509 |
(Output.urgent_message ("qc_recursive: " ^ string_of_int (length xs)); |
42089
904897d0c5bd
adapting mutabelle; exporting more Quickcheck functions
bulwahn
parents:
42030
diff
changeset
|
510 |
((x, pretty (the_default [] (Quickcheck.counterexample_of (Quickcheck.test_term |
40920 | 511 |
((Config.put Quickcheck.size sz #> Config.put Quickcheck.iterations qciter |
512 |
#> Config.put Quickcheck.tester (!testgen_name)) |
|
42361 | 513 |
(Proof_Context.init_global usedthy)) |
42089
904897d0c5bd
adapting mutabelle; exporting more Quickcheck functions
bulwahn
parents:
42030
diff
changeset
|
514 |
(true, false) (preprocess usedthy insts x, []))))) :: acc)) |
40132
7ee65dbffa31
renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents:
39557
diff
changeset
|
515 |
handle ERROR msg => (Output.urgent_message msg; qc_recursive usedthy xs insts sz qciter acc); |
34965 | 516 |
|
517 |
||
518 |
(*quickcheck-test the mutants created by function mutate with type-instantiation insts, |
|
519 |
quickcheck-theory usedthy and qciter quickcheck-iterations *) |
|
520 |
||
521 |
fun qc_test mutated insts usedthy sz qciter = |
|
522 |
let |
|
523 |
val checked = map (apsnd (map (apsnd (cterm_of usedthy)))) |
|
524 |
(qc_recursive usedthy mutated insts sz qciter []); |
|
525 |
fun combine (passednum,passed) (cenum,ces) [] = (passednum,passed,cenum,ces) |
|
526 |
| combine (passednum,passed) (cenum,ces) ((t, []) :: xs) = |
|
527 |
combine (passednum+1,(cterm_of usedthy t)::passed) (cenum,ces) xs |
|
528 |
| combine (passednum,passed) (cenum,ces) ((t, x) :: xs) = |
|
529 |
combine (passednum,passed) |
|
530 |
(cenum+1,(cterm_of usedthy t, x) :: ces) xs |
|
531 |
in |
|
532 |
combine (0,[]) (0,[]) checked |
|
533 |
end; |
|
534 |
||
535 |
||
536 |
(*create a string of a list of terms*) |
|
537 |
||
538 |
fun string_of_ctermlist thy [] acc = acc |
|
539 |
| string_of_ctermlist thy (x::xs) acc = |
|
540 |
string_of_ctermlist thy xs ((Syntax.string_of_term_global thy (term_of x)) ^ "\n" ^ acc); |
|
541 |
||
542 |
(*helper function in order to decompose the counter examples generated by quickcheck*) |
|
543 |
||
544 |
fun decompose_ce thy [] acc = acc |
|
545 |
| decompose_ce thy ((varname,varce)::xs) acc = |
|
546 |
decompose_ce thy xs ("\t" ^ varname ^ " instanciated to " ^ |
|
547 |
(Syntax.string_of_term_global thy (term_of varce)) ^ "\n" ^ acc); |
|
548 |
||
549 |
(*helper function in order to decompose a list of counter examples*) |
|
550 |
||
551 |
fun decompose_celist thy [] acc = acc |
|
552 |
| decompose_celist thy ((mutTerm,varcelist)::xs) acc = decompose_celist thy xs |
|
553 |
("mutated term : " ^ |
|
554 |
(Syntax.string_of_term_global thy (term_of mutTerm)) ^ "\n" ^ |
|
555 |
"counterexample : \n" ^ |
|
556 |
(decompose_ce thy (rev varcelist) "") ^ acc); |
|
557 |
||
558 |
||
559 |
(*quickcheck test the list of mutants mutated by applying the type instantiations |
|
560 |
insts and using the quickcheck-theory usedthy. The results of quickcheck are stored |
|
561 |
in the file with name filename. If app is true, the output will be appended to the file. |
|
562 |
Else it will be overwritten. *) |
|
563 |
||
564 |
fun qc_test_file app mutated insts usedthy sz qciter filename = |
|
565 |
let |
|
566 |
val statisticList = qc_test mutated insts usedthy sz qciter |
|
567 |
val passed = (string_of_int (#1 statisticList)) ^ |
|
568 |
" terms passed the quickchecktest: \n\n" ^ |
|
569 |
(string_of_ctermlist usedthy (rev (#2 statisticList)) "") |
|
570 |
val counterexps = (string_of_int (#3 statisticList)) ^ |
|
571 |
" terms produced a counterexample: \n\n" ^ |
|
572 |
decompose_celist usedthy (rev (#4 statisticList)) "" |
|
573 |
in |
|
574 |
if (app = false) |
|
575 |
then File.write (Path.explode filename) (passed ^ "\n\n" ^ counterexps) |
|
576 |
else File.append (Path.explode filename) (passed ^ "\n\n" ^ counterexps) |
|
577 |
end; |
|
578 |
||
579 |
||
580 |
(*mutate sourceThm with the mutate-version given in option and check the resulting mutants. |
|
581 |
The output will be written to the file with name filename*) |
|
582 |
||
583 |
fun mutqc_file option usedthy commutatives forbidden_funs iter sourceThm insts sz qciter filename = |
|
584 |
let |
|
585 |
val mutated = mutate option (prop_of sourceThm) |
|
586 |
usedthy commutatives forbidden_funs iter |
|
587 |
in |
|
588 |
qc_test_file false mutated insts usedthy sz qciter filename |
|
589 |
end; |
|
590 |
||
591 |
(*exchange version of function mutqc_file*) |
|
592 |
||
593 |
fun mutqc_file_exc usedthy commutatives iter sourceThm insts sz qciter filename = |
|
594 |
mutqc_file 0 usedthy commutatives [] iter sourceThm insts sz qciter filename; |
|
595 |
||
596 |
||
597 |
(*sinature version of function mutqc_file*) |
|
598 |
fun mutqc_file_sign usedthy forbidden_funs iter sourceThm insts sz qciter filename= |
|
599 |
mutqc_file 1 usedthy [] forbidden_funs iter sourceThm insts sz qciter filename; |
|
600 |
||
601 |
(*mixed version of function mutqc_file*) |
|
602 |
||
603 |
fun mutqc_file_mix usedthy commutatives forbidden_funs iter sourceThm insts sz qciter filename = |
|
604 |
mutqc_file 2 usedthy commutatives forbidden_funs iter sourceThm insts sz qciter filename; |
|
605 |
||
606 |
||
607 |
||
608 |
(*apply function mutqc_file on a list of theorems. The output for each theorem |
|
609 |
will be stored in a seperated file whose filename must be indicated in a list*) |
|
610 |
||
611 |
fun mutqc_file_rec option usedthy commutatives forbFuns iter [] insts sz qciter _ = () |
|
612 |
| mutqc_file_rec option usedthy commutatives forbFuns iter sourceThms insts sz qciter [] = |
|
613 |
raise WrongArg ("Not enough files for the output of qc_test_file_rec!") |
|
614 |
| mutqc_file_rec option usedthy commutatives forbFuns iter (x::xs) insts sz qciter (y::ys) = |
|
615 |
(mutqc_file option usedthy commutatives forbFuns iter x insts sz qciter y ; |
|
616 |
mutqc_file_rec option usedthy commutatives forbFuns iter xs insts sz qciter ys); |
|
617 |
||
618 |
||
619 |
(*exchange version of function mutqc_file_rec*) |
|
620 |
||
621 |
fun mutqc_file_rec_exc usedthy commutatives iter sourceThms insts sz qciter files = |
|
622 |
mutqc_file_rec 0 usedthy commutatives [] iter sourceThms insts sz qciter files; |
|
623 |
||
624 |
(*signature version of function mutqc_file_rec*) |
|
625 |
||
626 |
fun mutqc_file_rec_sign usedthy forbidden_funs iter sourceThms insts sz qciter files = |
|
627 |
mutqc_file_rec 1 usedthy [] forbidden_funs iter sourceThms insts sz qciter files; |
|
628 |
||
629 |
(*mixed version of function mutqc_file_rec*) |
|
630 |
||
631 |
fun mutqc_file_rec_mix usedthy commutatives forbidden_funs iter sourceThms insts sz qciter files = |
|
632 |
mutqc_file_rec 2 usedthy commutatives forbidden_funs iter sourceThms insts sz qciter files; |
|
633 |
||
634 |
(*create the appropriate number of spaces in order to properly print a table*) |
|
635 |
||
636 |
fun create_spaces entry spacenum = |
|
637 |
let |
|
41067 | 638 |
val diff = spacenum - (size entry) |
639 |
in |
|
640 |
if (diff > 0) |
|
34965 | 641 |
then implode (replicate diff " ") |
642 |
else "" |
|
643 |
end; |
|
644 |
||
645 |
||
646 |
(*create a statistic of the output of a quickcheck test on the passed thmlist*) |
|
647 |
||
648 |
fun mutqc_file_stat option usedthy commutatives forbidden_funs iter thmlist insts sz qciter filename = |
|
649 |
let |
|
650 |
fun mutthmrec [] = () |
|
651 |
| mutthmrec (x::xs) = |
|
652 |
let |
|
653 |
val mutated = mutate option (prop_of x) usedthy |
|
654 |
commutatives forbidden_funs iter |
|
655 |
val (passednum,_,cenum,_) = qc_test mutated insts usedthy sz qciter |
|
36743
ce2297415b54
prefer Thm.get_name_hint, which is closer to a user-space idea of "theorem name";
wenzelm
parents:
36692
diff
changeset
|
656 |
val thmname = "\ntheorem " ^ Thm.get_name_hint x ^ ":" |
34965 | 657 |
val pnumstring = string_of_int passednum |
658 |
val cenumstring = string_of_int cenum |
|
659 |
in |
|
660 |
(File.append (Path.explode filename) |
|
661 |
(thmname ^ (create_spaces thmname 50) ^ |
|
662 |
pnumstring ^ (create_spaces pnumstring 20) ^ |
|
663 |
cenumstring ^ (create_spaces cenumstring 20) ^ "\n"); |
|
664 |
mutthmrec xs) |
|
665 |
end; |
|
666 |
in |
|
667 |
(File.write (Path.explode filename) |
|
668 |
("\n\ntheorem name" ^ (create_spaces "theorem name" 50) ^ |
|
669 |
"passed mutants" ^ (create_spaces "passed mutants" 20) ^ |
|
670 |
"counter examples\n\n" ); |
|
671 |
mutthmrec thmlist) |
|
672 |
end; |
|
673 |
||
674 |
(*signature version of function mutqc_file_stat*) |
|
675 |
||
676 |
fun mutqc_file_stat_sign usedthy forbidden_funs iter thmlist insts sz qciter filename = |
|
677 |
mutqc_file_stat 1 usedthy [] forbidden_funs iter thmlist insts sz qciter filename; |
|
678 |
||
679 |
(*exchange version of function mutqc_file_stat*) |
|
680 |
||
681 |
fun mutqc_file_stat_exc usedthy commutatives iter thmlist insts sz qciter filename = |
|
682 |
mutqc_file_stat 0 usedthy commutatives [] iter thmlist insts sz qciter filename; |
|
683 |
||
684 |
(*mixed version of function mutqc_file_stat*) |
|
685 |
||
686 |
fun mutqc_file_stat_mix usedthy commutatives forbidden_funs iter thmlist insts sz qciter filename = |
|
687 |
mutqc_file_stat 2 usedthy commutatives forbidden_funs iter thmlist insts sz qciter filename; |
|
688 |
||
689 |
(*mutate and quickcheck-test all the theorems contained in the passed theory. |
|
690 |
The output will be stored in a single file*) |
|
691 |
||
692 |
fun mutqc_thy option mutthy usedthy commutatives forbidden_funs iter insts sz qciter filename = |
|
693 |
let |
|
694 |
val thmlist = filter (is_executable mutthy insts o snd) (thms_of mutthy); |
|
695 |
fun mutthmrec [] = () |
|
696 |
| mutthmrec ((name,thm)::xs) = |
|
697 |
let |
|
698 |
val mutated = mutate option (prop_of thm) |
|
699 |
usedthy commutatives forbidden_funs iter |
|
700 |
in |
|
701 |
(File.append (Path.explode filename) |
|
702 |
("--------------------------\n\nQuickchecktest of theorem " ^ name ^ ":\n\n"); |
|
703 |
qc_test_file true mutated insts usedthy sz qciter filename; |
|
704 |
mutthmrec xs) |
|
705 |
end; |
|
706 |
in |
|
707 |
mutthmrec thmlist |
|
708 |
end; |
|
709 |
||
710 |
(*exchange version of function mutqc_thy*) |
|
711 |
||
712 |
fun mutqc_thy_exc mutthy usedthy commutatives iter insts sz qciter filename = |
|
713 |
mutqc_thy 0 mutthy usedthy commutatives [] iter insts sz qciter filename; |
|
714 |
||
715 |
(*signature version of function mutqc_thy*) |
|
716 |
||
717 |
fun mutqc_thy_sign mutthy usedthy forbidden_funs iter insts sz qciter filename = |
|
718 |
mutqc_thy 1 mutthy usedthy [] forbidden_funs iter insts sz qciter filename; |
|
719 |
||
720 |
(*mixed version of function mutqc_thy*) |
|
721 |
||
722 |
fun mutqc_thy_mix mutthy usedthy commutatives forbidden_funs iter insts sz qciter filename = |
|
723 |
mutqc_thy 2 mutthy usedthy commutatives forbidden_funs iter insts sz qciter filename; |
|
724 |
||
725 |
(*create a statistic representation of the call of function mutqc_thy*) |
|
726 |
||
727 |
fun mutqc_thystat option p mutthy usedthy commutatives forbidden_funs iter insts sz qciter filename = |
|
728 |
let |
|
729 |
val thmlist = filter |
|
40132
7ee65dbffa31
renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents:
39557
diff
changeset
|
730 |
(fn (s, th) => not (p s th) andalso (Output.urgent_message s; is_executable mutthy insts th)) (thms_of mutthy) |
34965 | 731 |
fun mutthmrec [] = () |
732 |
| mutthmrec ((name,thm)::xs) = |
|
733 |
let |
|
40132
7ee65dbffa31
renamed Output.priority to Output.urgent_message to emphasize its special role more clearly;
wenzelm
parents:
39557
diff
changeset
|
734 |
val _ = Output.urgent_message ("mutthmrec: " ^ name); |
34965 | 735 |
val mutated = mutate option (prop_of thm) usedthy |
736 |
commutatives forbidden_funs iter |
|
737 |
val (passednum,_,cenum,_) = qc_test mutated insts usedthy sz qciter |
|
738 |
val thmname = "\ntheorem " ^ name ^ ":" |
|
739 |
val pnumstring = string_of_int passednum |
|
740 |
val cenumstring = string_of_int cenum |
|
741 |
in |
|
742 |
(File.append (Path.explode filename) |
|
743 |
(thmname ^ (create_spaces thmname 50) ^ |
|
744 |
pnumstring ^ (create_spaces pnumstring 20) ^ |
|
745 |
cenumstring ^ (create_spaces cenumstring 20) ^ "\n"); |
|
746 |
mutthmrec xs) |
|
747 |
end; |
|
748 |
in |
|
749 |
(File.write (Path.explode filename) ("Result of the quickcheck-test of theory " ^ |
|
750 |
":\n\ntheorem name" ^ (create_spaces "theorem name" 50) ^ |
|
751 |
"passed mutants" ^ (create_spaces "passed mutants" 20) ^ |
|
752 |
"counter examples\n\n" ); |
|
753 |
mutthmrec thmlist) |
|
754 |
end; |
|
755 |
||
756 |
(*exchange version of function mutqc_thystat*) |
|
41067 | 757 |
|
34965 | 758 |
fun mutqc_thystat_exc p mutthy usedthy commutatives iter insts sz qciter filename = |
759 |
mutqc_thystat 0 p mutthy usedthy commutatives [] iter insts sz qciter filename; |
|
760 |
||
761 |
(*signature version of function mutqc_thystat*) |
|
762 |
||
763 |
fun mutqc_thystat_sign p mutthy usedthy forbidden_funs iter insts sz qciter filename = |
|
764 |
mutqc_thystat 1 p mutthy usedthy [] forbidden_funs iter insts sz qciter filename; |
|
765 |
||
766 |
(*mixed version of function mutqc_thystat*) |
|
767 |
||
768 |
fun mutqc_thystat_mix p mutthy usedthy commutatives forbidden_funs iter insts sz qciter filename = |
|
769 |
mutqc_thystat 2 p mutthy usedthy commutatives forbidden_funs iter insts sz qciter filename; |
|
770 |
||
771 |
end |