| author | bulwahn | 
| Thu, 10 Nov 2011 17:26:17 +0100 | |
| changeset 45441 | fb4ac1dd4fde | 
| parent 45082 | 54c4e12bb5e0 | 
| child 45658 | c2c647a4c237 | 
| permissions | -rw-r--r-- | 
| 
41932
 
e8f113ce8a94
adapting Quickcheck_Narrowing and example file to new names
 
bulwahn 
parents: 
41931 
diff
changeset
 | 
1  | 
(* Title: HOL/ex/Quickcheck_Narrowing_Examples.thy  | 
| 41907 | 2  | 
Author: Lukas Bulwahn  | 
3  | 
Copyright 2011 TU Muenchen  | 
|
4  | 
*)  | 
|
5  | 
||
| 
41932
 
e8f113ce8a94
adapting Quickcheck_Narrowing and example file to new names
 
bulwahn 
parents: 
41931 
diff
changeset
 | 
6  | 
header {* Examples for narrowing-based testing  *}
 | 
| 41907 | 7  | 
|
| 
41932
 
e8f113ce8a94
adapting Quickcheck_Narrowing and example file to new names
 
bulwahn 
parents: 
41931 
diff
changeset
 | 
8  | 
theory Quickcheck_Narrowing_Examples  | 
| 43318 | 9  | 
imports Main  | 
| 41907 | 10  | 
begin  | 
11  | 
||
| 
42020
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
12  | 
subsection {* Minimalistic examples *}
 | 
| 43356 | 13  | 
|
| 
42020
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
14  | 
lemma  | 
| 
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
15  | 
"x = y"  | 
| 
42021
 
52551c0a3374
extending code_int type more; adding narrowing instance for type int; added test case for int instance
 
bulwahn 
parents: 
42020 
diff
changeset
 | 
16  | 
quickcheck[tester = narrowing, finite_types = false, default_type = int, expect = counterexample]  | 
| 41907 | 17  | 
oops  | 
| 43356 | 18  | 
|
| 
42020
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
19  | 
lemma  | 
| 
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
20  | 
"x = y"  | 
| 43356 | 21  | 
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]  | 
| 41907 | 22  | 
oops  | 
23  | 
||
| 43239 | 24  | 
subsection {* Simple examples with existentials *}
 | 
25  | 
||
26  | 
lemma  | 
|
27  | 
"\<exists> y :: nat. \<forall> x. x = y"  | 
|
28  | 
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]  | 
|
29  | 
oops  | 
|
| 43356 | 30  | 
(*  | 
31  | 
lemma  | 
|
32  | 
"\<exists> y :: int. \<forall> x. x = y"  | 
|
33  | 
quickcheck[tester = narrowing, size = 2]  | 
|
34  | 
oops  | 
|
35  | 
*)  | 
|
| 43239 | 36  | 
lemma  | 
37  | 
"x > 1 --> (\<exists>y :: nat. x < y & y <= 1)"  | 
|
38  | 
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]  | 
|
39  | 
oops  | 
|
40  | 
||
41  | 
lemma  | 
|
42  | 
"x > 2 --> (\<exists>y :: nat. x < y & y <= 2)"  | 
|
43  | 
quickcheck[tester = narrowing, finite_types = false, size = 10]  | 
|
44  | 
oops  | 
|
45  | 
||
46  | 
lemma  | 
|
47  | 
"\<forall> x. \<exists> y :: nat. x > 3 --> (y < x & y > 3)"  | 
|
48  | 
quickcheck[tester = narrowing, finite_types = false, size = 7]  | 
|
49  | 
oops  | 
|
50  | 
||
51  | 
text {* A false conjecture derived from an partial copy-n-paste of @{thm not_distinct_decomp} *}
 | 
|
52  | 
lemma  | 
|
53  | 
"~ distinct ws ==> EX xs ys zs y. ws = xs @ [y] @ ys @ [y]"  | 
|
54  | 
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]  | 
|
55  | 
oops  | 
|
56  | 
||
57  | 
text {* A false conjecture derived from theorems @{thm split_list_first} and @{thm split_list_last} *}  
 | 
|
58  | 
lemma  | 
|
59  | 
"x : set xs ==> EX ys zs. xs = ys @ x # zs & x ~: set zs & x ~: set ys"  | 
|
60  | 
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]  | 
|
61  | 
oops  | 
|
62  | 
||
63  | 
text {* A false conjecture derived from @{thm map_eq_Cons_conv} with a typo *}
 | 
|
64  | 
lemma  | 
|
65  | 
"(map f xs = y # ys) = (EX z zs. xs = z' # zs & f z = y & map f zs = ys)"  | 
|
66  | 
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]  | 
|
67  | 
oops  | 
|
68  | 
||
| 
45441
 
fb4ac1dd4fde
adding some test cases for preprocessing and narrowing
 
bulwahn 
parents: 
45082 
diff
changeset
 | 
69  | 
lemma "a # xs = ys @ [a] ==> EX zs. xs = zs @ [a] & ys = a#zs"  | 
| 
 
fb4ac1dd4fde
adding some test cases for preprocessing and narrowing
 
bulwahn 
parents: 
45082 
diff
changeset
 | 
70  | 
quickcheck[narrowing, expect = counterexample]  | 
| 
 
fb4ac1dd4fde
adding some test cases for preprocessing and narrowing
 
bulwahn 
parents: 
45082 
diff
changeset
 | 
71  | 
quickcheck[exhaustive, random, expect = no_counterexample]  | 
| 
 
fb4ac1dd4fde
adding some test cases for preprocessing and narrowing
 
bulwahn 
parents: 
45082 
diff
changeset
 | 
72  | 
oops  | 
| 
 
fb4ac1dd4fde
adding some test cases for preprocessing and narrowing
 
bulwahn 
parents: 
45082 
diff
changeset
 | 
73  | 
|
| 
42020
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
74  | 
subsection {* Simple list examples *}
 | 
| 
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
75  | 
|
| 
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
76  | 
lemma "rev xs = xs"  | 
| 43239 | 77  | 
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample]  | 
| 
42020
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
78  | 
oops  | 
| 
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
79  | 
|
| 43239 | 80  | 
(* FIXME: integer has strange representation! *)  | 
| 
42020
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
81  | 
lemma "rev xs = xs"  | 
| 43239 | 82  | 
quickcheck[tester = narrowing, finite_types = false, default_type = int, expect = counterexample]  | 
| 
42021
 
52551c0a3374
extending code_int type more; adding narrowing instance for type int; added test case for int instance
 
bulwahn 
parents: 
42020 
diff
changeset
 | 
83  | 
oops  | 
| 
42184
 
1d4fae76ba5e
adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
 
bulwahn 
parents: 
42024 
diff
changeset
 | 
84  | 
(*  | 
| 
42021
 
52551c0a3374
extending code_int type more; adding narrowing instance for type int; added test case for int instance
 
bulwahn 
parents: 
42020 
diff
changeset
 | 
85  | 
lemma "rev xs = xs"  | 
| 
42023
 
1bd4b6d1c482
adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
 
bulwahn 
parents: 
42021 
diff
changeset
 | 
86  | 
quickcheck[tester = narrowing, finite_types = true, expect = counterexample]  | 
| 
 
1bd4b6d1c482
adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
 
bulwahn 
parents: 
42021 
diff
changeset
 | 
87  | 
oops  | 
| 
42184
 
1d4fae76ba5e
adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
 
bulwahn 
parents: 
42024 
diff
changeset
 | 
88  | 
*)  | 
| 
42023
 
1bd4b6d1c482
adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
 
bulwahn 
parents: 
42021 
diff
changeset
 | 
89  | 
subsection {* Simple examples with functions *}
 | 
| 
 
1bd4b6d1c482
adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
 
bulwahn 
parents: 
42021 
diff
changeset
 | 
90  | 
|
| 
 
1bd4b6d1c482
adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
 
bulwahn 
parents: 
42021 
diff
changeset
 | 
91  | 
lemma "map f xs = map g xs"  | 
| 43239 | 92  | 
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]  | 
| 
42020
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
93  | 
oops  | 
| 
 
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
 
bulwahn 
parents: 
41963 
diff
changeset
 | 
94  | 
|
| 
42023
 
1bd4b6d1c482
adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
 
bulwahn 
parents: 
42021 
diff
changeset
 | 
95  | 
lemma "map f xs = map f ys ==> xs = ys"  | 
| 43239 | 96  | 
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]  | 
| 
42024
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
97  | 
oops  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
98  | 
|
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
99  | 
lemma  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
100  | 
"list_all2 P (rev xs) (rev ys) = list_all2 P xs (rev ys)"  | 
| 43239 | 101  | 
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]  | 
| 
42024
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
102  | 
oops  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
103  | 
|
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
104  | 
lemma "map f xs = F f xs"  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
105  | 
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
106  | 
oops  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
107  | 
|
| 43239 | 108  | 
lemma "map f xs = F f xs"  | 
109  | 
quickcheck[tester = narrowing, finite_types = false, expect = counterexample]  | 
|
110  | 
oops  | 
|
111  | 
||
112  | 
(* requires some work...*)  | 
|
113  | 
(*  | 
|
| 
42024
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
114  | 
lemma "R O S = S O R"  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
115  | 
quickcheck[tester = narrowing, size = 2]  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
116  | 
oops  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
117  | 
*)  | 
| 
 
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
 
bulwahn 
parents: 
42023 
diff
changeset
 | 
118  | 
|
| 
45082
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
119  | 
subsection {* Simple examples with inductive predicates *}
 | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
120  | 
|
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
121  | 
inductive even where  | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
122  | 
"even 0" |  | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
123  | 
"even n ==> even (Suc (Suc n))"  | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
124  | 
|
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
125  | 
code_pred even .  | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
126  | 
|
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
127  | 
lemma "even (n - 2) ==> even n"  | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
128  | 
quickcheck[narrowing, expect = counterexample]  | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
129  | 
oops  | 
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
130  | 
|
| 
 
54c4e12bb5e0
adding an example with inductive predicates to quickcheck narrowing examples
 
bulwahn 
parents: 
43356 
diff
changeset
 | 
131  | 
|
| 41907 | 132  | 
subsection {* AVL Trees *}
 | 
133  | 
||
134  | 
datatype 'a tree = ET | MKT 'a "'a tree" "'a tree" nat  | 
|
135  | 
||
136  | 
primrec set_of :: "'a tree \<Rightarrow> 'a set"  | 
|
137  | 
where  | 
|
138  | 
"set_of ET = {}" |
 | 
|
139  | 
"set_of (MKT n l r h) = insert n (set_of l \<union> set_of r)"  | 
|
140  | 
||
141  | 
primrec height :: "'a tree \<Rightarrow> nat"  | 
|
142  | 
where  | 
|
143  | 
"height ET = 0" |  | 
|
144  | 
"height (MKT x l r h) = max (height l) (height r) + 1"  | 
|
145  | 
||
146  | 
primrec avl :: "'a tree \<Rightarrow> bool"  | 
|
147  | 
where  | 
|
148  | 
"avl ET = True" |  | 
|
149  | 
"avl (MKT x l r h) =  | 
|
150  | 
((height l = height r \<or> height l = 1+height r \<or> height r = 1+height l) \<and>  | 
|
151  | 
h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)"  | 
|
152  | 
||
153  | 
primrec is_ord :: "('a::order) tree \<Rightarrow> bool"
 | 
|
154  | 
where  | 
|
155  | 
"is_ord ET = True" |  | 
|
156  | 
"is_ord (MKT n l r h) =  | 
|
157  | 
((\<forall>n' \<in> set_of l. n' < n) \<and> (\<forall>n' \<in> set_of r. n < n') \<and> is_ord l \<and> is_ord r)"  | 
|
158  | 
||
159  | 
primrec is_in :: "('a::order) \<Rightarrow> 'a tree \<Rightarrow> bool"
 | 
|
160  | 
where  | 
|
161  | 
"is_in k ET = False" |  | 
|
162  | 
"is_in k (MKT n l r h) = (if k = n then True else  | 
|
163  | 
if k < n then (is_in k l)  | 
|
164  | 
else (is_in k r))"  | 
|
165  | 
||
166  | 
primrec ht :: "'a tree \<Rightarrow> nat"  | 
|
167  | 
where  | 
|
168  | 
"ht ET = 0" |  | 
|
169  | 
"ht (MKT x l r h) = h"  | 
|
170  | 
||
171  | 
definition  | 
|
172  | 
mkt :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where  | 
|
173  | 
"mkt x l r = MKT x l r (max (ht l) (ht r) + 1)"  | 
|
174  | 
||
175  | 
(* replaced MKT lrn lrl lrr by MKT lrr lrl *)  | 
|
176  | 
fun l_bal where  | 
|
177  | 
"l_bal(n, MKT ln ll lr h, r) =  | 
|
178  | 
(if ht ll < ht lr  | 
|
179  | 
then case lr of ET \<Rightarrow> ET (* impossible *)  | 
|
180  | 
| MKT lrn lrr lrl lrh \<Rightarrow>  | 
|
181  | 
mkt lrn (mkt ln ll lrl) (mkt n lrr r)  | 
|
182  | 
else mkt ln ll (mkt n lr r))"  | 
|
183  | 
||
184  | 
fun r_bal where  | 
|
185  | 
"r_bal(n, l, MKT rn rl rr h) =  | 
|
186  | 
(if ht rl > ht rr  | 
|
187  | 
then case rl of ET \<Rightarrow> ET (* impossible *)  | 
|
188  | 
| MKT rln rll rlr h \<Rightarrow> mkt rln (mkt n l rll) (mkt rn rlr rr)  | 
|
189  | 
else mkt rn (mkt n l rl) rr)"  | 
|
190  | 
||
191  | 
primrec insrt :: "'a::order \<Rightarrow> 'a tree \<Rightarrow> 'a tree"  | 
|
192  | 
where  | 
|
193  | 
"insrt x ET = MKT x ET ET 1" |  | 
|
194  | 
"insrt x (MKT n l r h) =  | 
|
195  | 
(if x=n  | 
|
196  | 
then MKT n l r h  | 
|
197  | 
else if x<n  | 
|
198  | 
then let l' = insrt x l; hl' = ht l'; hr = ht r  | 
|
199  | 
in if hl' = 2+hr then l_bal(n,l',r)  | 
|
200  | 
else MKT n l' r (1 + max hl' hr)  | 
|
201  | 
else let r' = insrt x r; hl = ht l; hr' = ht r'  | 
|
202  | 
in if hr' = 2+hl then r_bal(n,l,r')  | 
|
203  | 
else MKT n l r' (1 + max hl hr'))"  | 
|
204  | 
||
205  | 
||
206  | 
subsubsection {* Necessary setup for code generation *}
 | 
|
207  | 
||
208  | 
primrec set_of'  | 
|
209  | 
where  | 
|
210  | 
"set_of' ET = []"  | 
|
211  | 
| "set_of' (MKT n l r h) = n # (set_of' l @ set_of' r)"  | 
|
212  | 
||
213  | 
lemma set_of':  | 
|
214  | 
"set (set_of' t) = set_of t"  | 
|
215  | 
by (induct t) auto  | 
|
216  | 
||
217  | 
lemma is_ord_mkt:  | 
|
218  | 
"is_ord (MKT n l r h) = ((ALL n': set (set_of' l). n' < n) & (ALL n': set (set_of' r). n < n') & is_ord l & is_ord r)"  | 
|
219  | 
by (simp add: set_of')  | 
|
220  | 
||
221  | 
declare is_ord.simps(1)[code] is_ord_mkt[code]  | 
|
222  | 
||
223  | 
subsubsection {* Invalid Lemma due to typo in lbal *}
 | 
|
224  | 
||
225  | 
lemma is_ord_l_bal:  | 
|
226  | 
"\<lbrakk> is_ord(MKT (x :: nat) l r h); height l = height r + 2 \<rbrakk> \<Longrightarrow> is_ord(l_bal(x,l,r))"  | 
|
| 
42184
 
1d4fae76ba5e
adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
 
bulwahn 
parents: 
42024 
diff
changeset
 | 
227  | 
quickcheck[tester = narrowing, finite_types = false, default_type = nat, size = 6, timeout = 1000, expect = counterexample]  | 
| 41907 | 228  | 
oops  | 
229  | 
||
230  | 
||
231  | 
end  |