author | wenzelm |
Tue, 05 Apr 2011 14:25:18 +0200 | |
changeset 42224 | 578a51fae383 |
parent 42184 | 1d4fae76ba5e |
child 43239 | 42f82fda796b |
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 |
41937
a369f8ba5425
adapting example file to renaming of the quickcheck tester
bulwahn
parents:
41934
diff
changeset
|
9 |
imports "~~/src/HOL/Library/Quickcheck_Narrowing" |
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 *} |
42021
52551c0a3374
extending code_int type more; adding narrowing instance for type int; added test case for int instance
bulwahn
parents:
42020
diff
changeset
|
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 |
42021
52551c0a3374
extending code_int type more; adding narrowing instance for type int; added test case for int instance
bulwahn
parents:
42020
diff
changeset
|
18 |
*) |
41907 | 19 |
(* |
42020
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
20 |
lemma |
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
21 |
"x = y" |
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
22 |
quickcheck[tester = narrowing, finite_types = true, expect = counterexample] |
41907 | 23 |
oops |
24 |
*) |
|
25 |
||
42020
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
26 |
subsection {* Simple list examples *} |
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
27 |
|
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
28 |
lemma "rev xs = xs" |
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
29 |
quickcheck[tester = narrowing, finite_types = false, default_type = nat, expect = counterexample] |
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
30 |
oops |
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
31 |
|
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
32 |
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
|
33 |
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
|
34 |
oops |
42184
1d4fae76ba5e
adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents:
42024
diff
changeset
|
35 |
(* |
42021
52551c0a3374
extending code_int type more; adding narrowing instance for type int; added test case for int instance
bulwahn
parents:
42020
diff
changeset
|
36 |
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
|
37 |
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
|
38 |
oops |
42184
1d4fae76ba5e
adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents:
42024
diff
changeset
|
39 |
*) |
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
|
40 |
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
|
41 |
|
1bd4b6d1c482
adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents:
42021
diff
changeset
|
42 |
declare [[quickcheck_finite_functions]] |
42184
1d4fae76ba5e
adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents:
42024
diff
changeset
|
43 |
(* |
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
|
44 |
lemma "map f xs = map g xs" |
42024
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
45 |
quickcheck[tester = narrowing, finite_types = true, expect = counterexample] |
42020
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
46 |
oops |
2da02764d523
translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents:
41963
diff
changeset
|
47 |
|
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
|
48 |
lemma "map f xs = map f ys ==> xs = ys" |
42024
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
49 |
quickcheck[tester = narrowing, finite_types = true, expect = counterexample] |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
50 |
oops |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
51 |
|
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
52 |
lemma |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
53 |
"list_all2 P (rev xs) (rev ys) = list_all2 P xs (rev ys)" |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
54 |
quickcheck[tester = narrowing, expect = counterexample] |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
55 |
oops |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
56 |
|
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
57 |
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
|
58 |
quickcheck[tester = narrowing, finite_types = true, expect = counterexample] |
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
|
59 |
oops |
42184
1d4fae76ba5e
adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents:
42024
diff
changeset
|
60 |
*) |
42024
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
oops |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
64 |
|
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
65 |
(* requires some work... |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
66 |
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
|
67 |
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
|
68 |
oops |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
69 |
*) |
51df23535105
handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents:
42023
diff
changeset
|
70 |
|
41907 | 71 |
subsection {* AVL Trees *} |
72 |
||
73 |
datatype 'a tree = ET | MKT 'a "'a tree" "'a tree" nat |
|
74 |
||
75 |
primrec set_of :: "'a tree \<Rightarrow> 'a set" |
|
76 |
where |
|
77 |
"set_of ET = {}" | |
|
78 |
"set_of (MKT n l r h) = insert n (set_of l \<union> set_of r)" |
|
79 |
||
80 |
primrec height :: "'a tree \<Rightarrow> nat" |
|
81 |
where |
|
82 |
"height ET = 0" | |
|
83 |
"height (MKT x l r h) = max (height l) (height r) + 1" |
|
84 |
||
85 |
primrec avl :: "'a tree \<Rightarrow> bool" |
|
86 |
where |
|
87 |
"avl ET = True" | |
|
88 |
"avl (MKT x l r h) = |
|
89 |
((height l = height r \<or> height l = 1+height r \<or> height r = 1+height l) \<and> |
|
90 |
h = max (height l) (height r) + 1 \<and> avl l \<and> avl r)" |
|
91 |
||
92 |
primrec is_ord :: "('a::order) tree \<Rightarrow> bool" |
|
93 |
where |
|
94 |
"is_ord ET = True" | |
|
95 |
"is_ord (MKT n l r h) = |
|
96 |
((\<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)" |
|
97 |
||
98 |
primrec is_in :: "('a::order) \<Rightarrow> 'a tree \<Rightarrow> bool" |
|
99 |
where |
|
100 |
"is_in k ET = False" | |
|
101 |
"is_in k (MKT n l r h) = (if k = n then True else |
|
102 |
if k < n then (is_in k l) |
|
103 |
else (is_in k r))" |
|
104 |
||
105 |
primrec ht :: "'a tree \<Rightarrow> nat" |
|
106 |
where |
|
107 |
"ht ET = 0" | |
|
108 |
"ht (MKT x l r h) = h" |
|
109 |
||
110 |
definition |
|
111 |
mkt :: "'a \<Rightarrow> 'a tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where |
|
112 |
"mkt x l r = MKT x l r (max (ht l) (ht r) + 1)" |
|
113 |
||
114 |
(* replaced MKT lrn lrl lrr by MKT lrr lrl *) |
|
115 |
fun l_bal where |
|
116 |
"l_bal(n, MKT ln ll lr h, r) = |
|
117 |
(if ht ll < ht lr |
|
118 |
then case lr of ET \<Rightarrow> ET (* impossible *) |
|
119 |
| MKT lrn lrr lrl lrh \<Rightarrow> |
|
120 |
mkt lrn (mkt ln ll lrl) (mkt n lrr r) |
|
121 |
else mkt ln ll (mkt n lr r))" |
|
122 |
||
123 |
fun r_bal where |
|
124 |
"r_bal(n, l, MKT rn rl rr h) = |
|
125 |
(if ht rl > ht rr |
|
126 |
then case rl of ET \<Rightarrow> ET (* impossible *) |
|
127 |
| MKT rln rll rlr h \<Rightarrow> mkt rln (mkt n l rll) (mkt rn rlr rr) |
|
128 |
else mkt rn (mkt n l rl) rr)" |
|
129 |
||
130 |
primrec insrt :: "'a::order \<Rightarrow> 'a tree \<Rightarrow> 'a tree" |
|
131 |
where |
|
132 |
"insrt x ET = MKT x ET ET 1" | |
|
133 |
"insrt x (MKT n l r h) = |
|
134 |
(if x=n |
|
135 |
then MKT n l r h |
|
136 |
else if x<n |
|
137 |
then let l' = insrt x l; hl' = ht l'; hr = ht r |
|
138 |
in if hl' = 2+hr then l_bal(n,l',r) |
|
139 |
else MKT n l' r (1 + max hl' hr) |
|
140 |
else let r' = insrt x r; hl = ht l; hr' = ht r' |
|
141 |
in if hr' = 2+hl then r_bal(n,l,r') |
|
142 |
else MKT n l r' (1 + max hl hr'))" |
|
143 |
||
144 |
||
145 |
subsubsection {* Necessary setup for code generation *} |
|
146 |
||
147 |
primrec set_of' |
|
148 |
where |
|
149 |
"set_of' ET = []" |
|
150 |
| "set_of' (MKT n l r h) = n # (set_of' l @ set_of' r)" |
|
151 |
||
152 |
lemma set_of': |
|
153 |
"set (set_of' t) = set_of t" |
|
154 |
by (induct t) auto |
|
155 |
||
156 |
lemma is_ord_mkt: |
|
157 |
"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)" |
|
158 |
by (simp add: set_of') |
|
159 |
||
160 |
declare is_ord.simps(1)[code] is_ord_mkt[code] |
|
161 |
||
162 |
subsubsection {* Invalid Lemma due to typo in lbal *} |
|
163 |
||
164 |
lemma is_ord_l_bal: |
|
165 |
"\<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
|
166 |
quickcheck[tester = narrowing, finite_types = false, default_type = nat, size = 6, timeout = 1000, expect = counterexample] |
41907 | 167 |
oops |
168 |
||
169 |
||
170 |
end |