| author | haftmann | 
| Fri, 24 Feb 2012 09:40:02 +0100 | |
| changeset 46638 | fc315796794e | 
| parent 46635 | cde737f9c911 | 
| child 46664 | 1f6c140f9c72 | 
| permissions | -rw-r--r-- | 
| 41922 | 1  | 
(* Author: Florian Haftmann & Lukas Bulwahn, TU Muenchen *)  | 
| 26265 | 2  | 
|
| 41922 | 3  | 
header {* A simple counterexample generator performing random testing *}
 | 
| 26265 | 4  | 
|
5  | 
theory Quickcheck  | 
|
| 
40650
 
d40b347d5b0b
adding Enum to HOL-Main image and removing it from HOL-Library
 
bulwahn 
parents: 
38857 
diff
changeset
 | 
6  | 
imports Random Code_Evaluation Enum  | 
| 
41928
 
05abcee548a1
adaptions in generators using the common functions
 
bulwahn 
parents: 
41923 
diff
changeset
 | 
7  | 
uses  | 
| 
45718
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
8  | 
  ("Tools/Quickcheck/quickcheck_common.ML")
 | 
| 
41928
 
05abcee548a1
adaptions in generators using the common functions
 
bulwahn 
parents: 
41923 
diff
changeset
 | 
9  | 
  ("Tools/Quickcheck/random_generators.ML")
 | 
| 26265 | 10  | 
begin  | 
11  | 
||
| 37751 | 12  | 
notation fcomp (infixl "\<circ>>" 60)  | 
13  | 
notation scomp (infixl "\<circ>\<rightarrow>" 60)  | 
|
| 31179 | 14  | 
|
| 
45718
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
15  | 
setup {* Code_Target.extend_target ("Quickcheck", (Code_Runtime.target, K I)) *}
 | 
| 
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
16  | 
|
| 
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
17  | 
subsection {* Catching Match exceptions *}
 | 
| 
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
18  | 
|
| 
45801
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
19  | 
axiomatization catch_match :: "'a => 'a => 'a"  | 
| 
45718
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
20  | 
|
| 
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
21  | 
code_const catch_match  | 
| 
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
22  | 
(Quickcheck "(_) handle Match => _")  | 
| 31179 | 23  | 
|
| 26265 | 24  | 
subsection {* The @{text random} class *}
 | 
25  | 
||
| 28335 | 26  | 
class random = typerep +  | 
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
27  | 
  fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
 | 
| 26265 | 28  | 
|
| 
26267
 
ba710daf77a7
added combinator for interpretation of construction of datatype
 
haftmann 
parents: 
26265 
diff
changeset
 | 
29  | 
|
| 31260 | 30  | 
subsection {* Fundamental and numeric types*}
 | 
| 31179 | 31  | 
|
32  | 
instantiation bool :: random  | 
|
33  | 
begin  | 
|
34  | 
||
35  | 
definition  | 
|
| 37751 | 36  | 
"random i = Random.range 2 \<circ>\<rightarrow>  | 
| 32657 | 37  | 
(\<lambda>k. Pair (if k = 0 then Code_Evaluation.valtermify False else Code_Evaluation.valtermify True))"  | 
| 31179 | 38  | 
|
39  | 
instance ..  | 
|
40  | 
||
41  | 
end  | 
|
42  | 
||
43  | 
instantiation itself :: (typerep) random  | 
|
44  | 
begin  | 
|
45  | 
||
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
46  | 
definition random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
 | 
| 32657 | 47  | 
  "random_itself _ = Pair (Code_Evaluation.valtermify TYPE('a))"
 | 
| 31179 | 48  | 
|
49  | 
instance ..  | 
|
50  | 
||
51  | 
end  | 
|
52  | 
||
| 
31483
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
53  | 
instantiation char :: random  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
54  | 
begin  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
55  | 
|
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
56  | 
definition  | 
| 37751 | 57  | 
"random _ = Random.select chars \<circ>\<rightarrow> (\<lambda>c. Pair (c, \<lambda>u. Code_Evaluation.term_of c))"  | 
| 
31483
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
58  | 
|
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
59  | 
instance ..  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
60  | 
|
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
61  | 
end  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
62  | 
|
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
63  | 
instantiation String.literal :: random  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
64  | 
begin  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
65  | 
|
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
66  | 
definition  | 
| 32657 | 67  | 
"random _ = Pair (STR '''', \<lambda>u. Code_Evaluation.term_of (STR ''''))"  | 
| 
31483
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
68  | 
|
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
69  | 
instance ..  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
70  | 
|
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
71  | 
end  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
72  | 
|
| 31179 | 73  | 
instantiation nat :: random  | 
74  | 
begin  | 
|
75  | 
||
| 32657 | 76  | 
definition random_nat :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed" where  | 
| 37751 | 77  | 
"random_nat i = Random.range (i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (  | 
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
78  | 
let n = Code_Numeral.nat_of k  | 
| 32657 | 79  | 
in (n, \<lambda>_. Code_Evaluation.term_of n)))"  | 
| 31194 | 80  | 
|
81  | 
instance ..  | 
|
82  | 
||
83  | 
end  | 
|
84  | 
||
85  | 
instantiation int :: random  | 
|
86  | 
begin  | 
|
87  | 
||
88  | 
definition  | 
|
| 37751 | 89  | 
"random i = Random.range (2 * i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (  | 
| 
31205
 
98370b26c2ce
String.literal replaces message_string, code_numeral replaces (code_)index
 
haftmann 
parents: 
31203 
diff
changeset
 | 
90  | 
let j = (if k \<ge> i then Code_Numeral.int_of (k - i) else - Code_Numeral.int_of (i - k))  | 
| 32657 | 91  | 
in (j, \<lambda>_. Code_Evaluation.term_of j)))"  | 
| 31179 | 92  | 
|
93  | 
instance ..  | 
|
94  | 
||
| 30945 | 95  | 
end  | 
| 31179 | 96  | 
|
| 31260 | 97  | 
|
98  | 
subsection {* Complex generators *}
 | 
|
99  | 
||
| 
31603
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
100  | 
text {* Towards @{typ "'a \<Rightarrow> 'b"} *}
 | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
101  | 
|
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
102  | 
axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
 | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
103  | 
  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
 | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
104  | 
  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
 | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
105  | 
|
| 31622 | 106  | 
definition random_fun_lift :: "(Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
 | 
107  | 
  \<Rightarrow> Random.seed \<Rightarrow> (('a\<Colon>term_of \<Rightarrow> 'b\<Colon>typerep) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
 | 
|
| 32657 | 108  | 
  "random_fun_lift f = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Evaluation.term_of f Random.split_seed"
 | 
| 
31603
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
109  | 
|
| 
38857
 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 
haftmann 
parents: 
37751 
diff
changeset
 | 
110  | 
instantiation "fun" :: ("{equal, term_of}", random) random
 | 
| 
31603
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
111  | 
begin  | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
112  | 
|
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
113  | 
definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
 | 
| 31622 | 114  | 
"random i = random_fun_lift (random i)"  | 
| 
31603
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
115  | 
|
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
116  | 
instance ..  | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
117  | 
|
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
118  | 
end  | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
119  | 
|
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
120  | 
text {* Towards type copies and datatypes *}
 | 
| 
 
fa30cd74d7d6
revised interpretation combinator for datatype constructions
 
haftmann 
parents: 
31483 
diff
changeset
 | 
121  | 
|
| 31260 | 122  | 
definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
 | 
| 37751 | 123  | 
"collapse f = (f \<circ>\<rightarrow> id)"  | 
| 
31223
 
87bde6b5f793
re-added corrected version of type copy quickcheck generator
 
haftmann 
parents: 
31211 
diff
changeset
 | 
124  | 
|
| 31260 | 125  | 
definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where  | 
126  | 
"beyond k l = (if l > k then l else 0)"  | 
|
127  | 
||
| 31267 | 128  | 
lemma beyond_zero:  | 
129  | 
"beyond k 0 = 0"  | 
|
130  | 
by (simp add: beyond_def)  | 
|
131  | 
||
| 46311 | 132  | 
|
133  | 
definition (in term_syntax) [code_unfold]: "valterm_emptyset = Code_Evaluation.valtermify ({} :: ('a :: typerep) set)"
 | 
|
134  | 
definition (in term_syntax) [code_unfold]: "valtermify_insert x s = Code_Evaluation.valtermify insert {\<cdot>} (x :: ('a :: typerep * _)) {\<cdot>} s"
 | 
|
135  | 
||
136  | 
instantiation set :: (random) random  | 
|
137  | 
begin  | 
|
138  | 
||
139  | 
primrec random_aux_set  | 
|
140  | 
where  | 
|
141  | 
"random_aux_set 0 j = collapse (Random.select_weight [(1, Pair valterm_emptyset)])"  | 
|
| 
46547
 
d1dcb91a512e
use qualified constant names instead of suffixes (from Florian Haftmann)
 
huffman 
parents: 
46311 
diff
changeset
 | 
142  | 
| "random_aux_set (Code_Numeral.Suc i) j = collapse (Random.select_weight [(1, Pair valterm_emptyset), (Code_Numeral.Suc i, random j \<circ>\<rightarrow> (%x. random_aux_set i j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"  | 
| 46311 | 143  | 
|
144  | 
lemma [code]:  | 
|
145  | 
"random_aux_set i j = collapse (Random.select_weight [(1, Pair valterm_emptyset), (i, random j \<circ>\<rightarrow> (%x. random_aux_set (i - 1) j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"  | 
|
146  | 
proof (induct i rule: code_numeral.induct)  | 
|
147  | 
print_cases  | 
|
148  | 
case zero  | 
|
149  | 
show ?case by (subst select_weight_drop_zero[symmetric])  | 
|
150  | 
(simp add: filter.simps random_aux_set.simps[simplified])  | 
|
151  | 
next  | 
|
| 
46547
 
d1dcb91a512e
use qualified constant names instead of suffixes (from Florian Haftmann)
 
huffman 
parents: 
46311 
diff
changeset
 | 
152  | 
case (Suc i)  | 
| 46311 | 153  | 
show ?case by (simp only: random_aux_set.simps(2)[of "i"] Suc_code_numeral_minus_one)  | 
154  | 
qed  | 
|
155  | 
||
156  | 
definition random_set  | 
|
157  | 
where  | 
|
158  | 
"random_set i = random_aux_set i i"  | 
|
159  | 
||
160  | 
instance ..  | 
|
161  | 
||
162  | 
end  | 
|
163  | 
||
| 
31483
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
164  | 
lemma random_aux_rec:  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
165  | 
fixes random_aux :: "code_numeral \<Rightarrow> 'a"  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
166  | 
assumes "random_aux 0 = rhs 0"  | 
| 
46547
 
d1dcb91a512e
use qualified constant names instead of suffixes (from Florian Haftmann)
 
huffman 
parents: 
46311 
diff
changeset
 | 
167  | 
and "\<And>k. random_aux (Code_Numeral.Suc k) = rhs (Code_Numeral.Suc k)"  | 
| 
31483
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
168  | 
shows "random_aux k = rhs k"  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
169  | 
using assms by (rule code_numeral.induct)  | 
| 
 
88210717bfc8
added generator for char and trivial generator for String.literal
 
haftmann 
parents: 
31267 
diff
changeset
 | 
170  | 
|
| 
45718
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
171  | 
subsection {* Deriving random generators for datatypes *}
 | 
| 
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
172  | 
|
| 
 
8979b2463fc8
quickcheck random can also find potential counterexamples;
 
bulwahn 
parents: 
45178 
diff
changeset
 | 
173  | 
use "Tools/Quickcheck/quickcheck_common.ML"  | 
| 41922 | 174  | 
use "Tools/Quickcheck/random_generators.ML"  | 
| 
41923
 
f05fc0711bc7
renaming signatures and structures; correcting header
 
bulwahn 
parents: 
41922 
diff
changeset
 | 
175  | 
setup Random_Generators.setup  | 
| 
34968
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
176  | 
|
| 
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
177  | 
|
| 
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
178  | 
subsection {* Code setup *}
 | 
| 
33561
 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 
blanchet 
parents: 
32657 
diff
changeset
 | 
179  | 
|
| 
41935
 
d786a8a3dc47
minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
 
bulwahn 
parents: 
41928 
diff
changeset
 | 
180  | 
code_const random_fun_aux (Quickcheck "Random'_Generators.random'_fun")  | 
| 
34968
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
181  | 
  -- {* With enough criminal energy this can be abused to derive @{prop False};
 | 
| 
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
182  | 
  for this reason we use a distinguished target @{text Quickcheck}
 | 
| 
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
183  | 
not spoiling the regular trusted code generation *}  | 
| 
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
184  | 
|
| 
41935
 
d786a8a3dc47
minor corrections for renaming; moved postprocessing of terms to Quickcheck_Common
 
bulwahn 
parents: 
41928 
diff
changeset
 | 
185  | 
code_reserved Quickcheck Random_Generators  | 
| 
34968
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
186  | 
|
| 37751 | 187  | 
no_notation fcomp (infixl "\<circ>>" 60)  | 
188  | 
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)  | 
|
| 
34968
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
189  | 
|
| 
 
ceeffca32eb0
tuned structure; moved non-related quickcheck.setup to Code_Generator.thy
 
haftmann 
parents: 
33562 
diff
changeset
 | 
190  | 
subsection {* The Random-Predicate Monad *} 
 | 
| 
33250
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
191  | 
|
| 
35880
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
192  | 
fun iter' ::  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
193  | 
  "'a itself => code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
 | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
194  | 
where  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
195  | 
"iter' T nrandom sz seed = (if nrandom = 0 then bot_class.bot else  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
196  | 
let ((x, _), seed') = random sz seed  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
197  | 
in Predicate.Seq (%u. Predicate.Insert x (iter' T (nrandom - 1) sz seed')))"  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
198  | 
|
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
199  | 
definition iter :: "code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
 | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
200  | 
where  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
201  | 
  "iter nrandom sz seed = iter' (TYPE('a)) nrandom sz seed"
 | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
202  | 
|
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
203  | 
lemma [code]:  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
204  | 
"iter nrandom sz seed = (if nrandom = 0 then bot_class.bot else  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
205  | 
let ((x, _), seed') = random sz seed  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
206  | 
in Predicate.Seq (%u. Predicate.Insert x (iter (nrandom - 1) sz seed')))"  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
207  | 
unfolding iter_def iter'.simps[of _ nrandom] ..  | 
| 
 
2623b23e41fc
a new simpler random compilation for the predicate compiler
 
bulwahn 
parents: 
35028 
diff
changeset
 | 
208  | 
|
| 
42163
 
392fd6c4669c
renewing specifications in HOL: replacing types by type_synonym
 
bulwahn 
parents: 
42159 
diff
changeset
 | 
209  | 
type_synonym 'a randompred = "Random.seed \<Rightarrow> ('a Predicate.pred \<times> Random.seed)"
 | 
| 
33250
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
210  | 
|
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
211  | 
definition empty :: "'a randompred"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
212  | 
where "empty = Pair (bot_class.bot)"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
213  | 
|
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
214  | 
definition single :: "'a => 'a randompred"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
215  | 
where "single x = Pair (Predicate.single x)"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
216  | 
|
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
217  | 
definition bind :: "'a randompred \<Rightarrow> ('a \<Rightarrow> 'b randompred) \<Rightarrow> 'b randompred"
 | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
218  | 
where  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
219  | 
"bind R f = (\<lambda>s. let  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
220  | 
(P, s') = R s;  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
221  | 
(s1, s2) = Random.split_seed s'  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
222  | 
in (Predicate.bind P (%a. fst (f a s1)), s2))"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
223  | 
|
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
224  | 
definition union :: "'a randompred \<Rightarrow> 'a randompred \<Rightarrow> 'a randompred"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
225  | 
where  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
226  | 
"union R1 R2 = (\<lambda>s. let  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
227  | 
(P1, s') = R1 s; (P2, s'') = R2 s'  | 
| 44845 | 228  | 
in (sup_class.sup P1 P2, s''))"  | 
| 
33250
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
229  | 
|
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
230  | 
definition if_randompred :: "bool \<Rightarrow> unit randompred"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
231  | 
where  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
232  | 
"if_randompred b = (if b then single () else empty)"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
233  | 
|
| 
36049
 
0ce5b7a5c2fd
adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
 
bulwahn 
parents: 
35880 
diff
changeset
 | 
234  | 
definition iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a randompred"  | 
| 
 
0ce5b7a5c2fd
adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
 
bulwahn 
parents: 
35880 
diff
changeset
 | 
235  | 
where  | 
| 46638 | 236  | 
"iterate_upto f n m = Pair (Code_Numeral.iterate_upto f n m)"  | 
| 
36049
 
0ce5b7a5c2fd
adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
 
bulwahn 
parents: 
35880 
diff
changeset
 | 
237  | 
|
| 
33250
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
238  | 
definition not_randompred :: "unit randompred \<Rightarrow> unit randompred"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
239  | 
where  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
240  | 
"not_randompred P = (\<lambda>s. let  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
241  | 
(P', s') = P s  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
242  | 
in if Predicate.eval P' () then (Orderings.bot, s') else (Predicate.single (), s'))"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
243  | 
|
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
244  | 
definition Random :: "(Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> 'a randompred"
 | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
245  | 
where "Random g = scomp g (Pair o (Predicate.single o fst))"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
246  | 
|
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
247  | 
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a randompred \<Rightarrow> 'b randompred)"
 | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
248  | 
where "map f P = bind P (single o f)"  | 
| 
 
5c2af18a3237
including the predicate compiler in HOL-Main; added RandomPredicate monad to Quickcheck
 
bulwahn 
parents: 
32657 
diff
changeset
 | 
249  | 
|
| 
45801
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
250  | 
hide_fact  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
251  | 
random_bool_def random_bool_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
252  | 
random_itself_def random_itself_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
253  | 
random_char_def random_char_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
254  | 
random_literal_def random_literal_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
255  | 
random_nat_def random_nat_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
256  | 
random_int_def random_int_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
257  | 
random_fun_lift_def random_fun_lift_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
258  | 
random_fun_def random_fun_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
259  | 
collapse_def collapse_def_raw  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
260  | 
beyond_def beyond_def_raw beyond_zero  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
261  | 
random_aux_rec  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
262  | 
|
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
263  | 
hide_const (open) catch_match random collapse beyond random_fun_aux random_fun_lift  | 
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
264  | 
|
| 
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
265  | 
hide_fact (open) iter'.simps iter_def empty_def single_def bind_def union_def if_randompred_def iterate_upto_def not_randompred_def Random_def map_def  | 
| 
36176
 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 
wenzelm 
parents: 
36049 
diff
changeset
 | 
266  | 
hide_type (open) randompred  | 
| 
45801
 
5616fbda86e6
hiding definitional facts in Quickcheck; introducing catch_match more honestly
 
bulwahn 
parents: 
45733 
diff
changeset
 | 
267  | 
hide_const (open) iter' iter empty single bind union if_randompred iterate_upto not_randompred Random map  | 
| 31267 | 268  | 
|
| 31179 | 269  | 
end  |