68 *} |
68 *} |
69 |
69 |
70 |
70 |
71 subsection {* Fundamental types*} |
71 subsection {* Fundamental types*} |
72 |
72 |
73 definition (in term_syntax) |
|
74 "termify_bool b = (if b then termify True else termify False)" |
|
75 |
|
76 instantiation bool :: random |
73 instantiation bool :: random |
77 begin |
74 begin |
78 |
75 |
79 definition |
76 definition |
80 "random i = Random.range i o\<rightarrow> (\<lambda>k. Pair (termify_bool (k div 2 = 0)))" |
77 "random i = Random.range i o\<rightarrow> |
81 |
78 (\<lambda>k. Pair (if (k div 2 = 0) then Code_Eval.valtermify True else Code_Eval.valtermify False))" |
82 instance .. |
79 |
83 |
80 instance .. |
84 end |
81 |
85 |
82 end |
86 definition (in term_syntax) |
|
87 "termify_itself TYPE('a\<Colon>typerep) = termify TYPE('a)" |
|
88 |
83 |
89 instantiation itself :: (typerep) random |
84 instantiation itself :: (typerep) random |
90 begin |
85 begin |
91 |
86 |
92 definition random_itself :: "index \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where |
87 definition random_itself :: "index \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where |
93 "random_itself _ = Pair (termify_itself TYPE('a))" |
88 "random_itself _ = Pair (Code_Eval.valtermify TYPE('a))" |
94 |
89 |
95 instance .. |
90 instance .. |
96 |
91 |
97 end |
92 end |
98 |
93 |
154 code_reserved Quickcheck Random_Engine |
149 code_reserved Quickcheck Random_Engine |
155 |
150 |
156 |
151 |
157 subsection {* Numeric types *} |
152 subsection {* Numeric types *} |
158 |
153 |
159 function (in term_syntax) termify_numeral :: "index \<Rightarrow> int \<times> (unit \<Rightarrow> term)" where |
|
160 "termify_numeral k = (if k = 0 then termify Int.Pls |
|
161 else (if k mod 2 = 0 then termify Int.Bit0 else termify Int.Bit1) <\<cdot>> termify_numeral (k div 2))" |
|
162 by pat_completeness auto |
|
163 |
|
164 declare (in term_syntax) termify_numeral.psimps [simp del] |
|
165 |
|
166 termination termify_numeral by (relation "measure Code_Index.nat_of") |
|
167 (simp_all add: index) |
|
168 |
|
169 definition (in term_syntax) termify_int_number :: "index \<Rightarrow> int \<times> (unit \<Rightarrow> term)" where |
|
170 "termify_int_number k = termify number_of <\<cdot>> termify_numeral k" |
|
171 |
|
172 definition (in term_syntax) termify_nat_number :: "index \<Rightarrow> nat \<times> (unit \<Rightarrow> term)" where |
|
173 "termify_nat_number k = (nat \<circ> number_of, snd (termify (number_of :: int \<Rightarrow> nat))) <\<cdot>> termify_numeral k" |
|
174 |
|
175 declare termify_nat_number_def [simplified snd_conv, code] |
|
176 |
|
177 instantiation nat :: random |
154 instantiation nat :: random |
178 begin |
155 begin |
179 |
156 |
180 definition random_nat :: "index \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where |
157 definition random_nat :: "index \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Eval.term)) \<times> Random.seed" where |
181 "random_nat i = Random.range (i + 1) o\<rightarrow> (\<lambda>k. Pair (termify_nat_number k))" |
158 "random_nat i = Random.range (i + 1) o\<rightarrow> (\<lambda>k. Pair ( |
182 |
159 let n = Code_Index.nat_of k |
183 instance .. |
160 in (n, \<lambda>_. Code_Eval.term_of n)))" |
184 |
161 |
185 end |
162 instance .. |
186 |
163 |
187 definition (in term_syntax) term_uminus :: "int \<times> (unit \<Rightarrow> term) \<Rightarrow> int \<times> (unit \<Rightarrow> term)" where |
164 end |
188 [code inline]: "term_uminus k = termify uminus <\<cdot>> k" |
|
189 |
165 |
190 instantiation int :: random |
166 instantiation int :: random |
191 begin |
167 begin |
192 |
168 |
193 definition |
169 definition |
194 "random i = Random.range (2 * i + 1) o\<rightarrow> (\<lambda>k. Pair (if k \<ge> i |
170 "random i = Random.range (2 * i + 1) o\<rightarrow> (\<lambda>k. Pair ( |
195 then let j = k - i in termify_int_number j |
171 let j = (if k \<ge> i then Code_Index.int_of (k - i) else - Code_Index.int_of (i - k)) |
196 else let j = i - k in term_uminus (termify_int_number j)))" |
172 in (j, \<lambda>_. Code_Eval.term_of j)))" |
197 |
173 |
198 instance .. |
174 instance .. |
199 |
175 |
200 end |
176 end |
201 |
177 |
202 definition (in term_syntax) term_fract :: "int \<times> (unit \<Rightarrow> term) \<Rightarrow> int \<times> (unit \<Rightarrow> term) \<Rightarrow> rat \<times> (unit \<Rightarrow> term)" where |
178 definition (in term_syntax) |
203 [code inline]: "term_fract k l = termify Fract <\<cdot>> k <\<cdot>> l" |
179 valterm_fract :: "int \<times> (unit \<Rightarrow> Code_Eval.term) \<Rightarrow> int \<times> (unit \<Rightarrow> Code_Eval.term) \<Rightarrow> rat \<times> (unit \<Rightarrow> Code_Eval.term)" where |
|
180 [code inline]: "valterm_fract k l = Code_Eval.valtermify Fract {\<cdot>} k {\<cdot>} l" |
204 |
181 |
205 instantiation rat :: random |
182 instantiation rat :: random |
206 begin |
183 begin |
207 |
184 |
208 definition |
185 definition |
209 "random i = random i o\<rightarrow> (\<lambda>num. Random.range (i + 1) o\<rightarrow> (\<lambda>denom. Pair (term_fract num (termify_int_number denom))))" |
186 "random i = random i o\<rightarrow> (\<lambda>num. Random.range i o\<rightarrow> (\<lambda>denom. Pair ( |
210 |
187 let j = Code_Index.int_of (denom + 1) |
211 instance .. |
188 in valterm_fract num (j, \<lambda>u. Code_Eval.term_of j))))" |
212 |
189 |
213 end |
190 instance .. |
214 |
191 |
215 definition (in term_syntax) term_ratreal :: "rat \<times> (unit \<Rightarrow> term) \<Rightarrow> real \<times> (unit \<Rightarrow> term)" where |
192 end |
216 [code inline]: "term_ratreal k = termify Ratreal <\<cdot>> k" |
193 |
|
194 definition (in term_syntax) |
|
195 valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Eval.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Eval.term)" where |
|
196 [code inline]: "valterm_ratreal k = Code_Eval.valtermify Ratreal {\<cdot>} k" |
217 |
197 |
218 instantiation real :: random |
198 instantiation real :: random |
219 begin |
199 begin |
220 |
200 |
221 definition |
201 definition |
222 "random i = random i o\<rightarrow> (\<lambda>r. Pair (term_ratreal r))" |
202 "random i = random i o\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))" |
223 |
203 |
224 instance .. |
204 instance .. |
225 |
205 |
226 end |
206 end |
227 |
207 |