1(* Author: Lukas Bulwahn, TU Muenchen *)
2
3section \<open>Counterexample generator performing narrowing-based testing\<close>
4
5theory Quickcheck_Narrowing
6imports Quickcheck_Random
7keywords "find_unused_assms" :: diag
8begin
9
10subsection \<open>Counterexample generator\<close>
11
12subsubsection \<open>Code generation setup\<close>
13
14setup \<open>Code_Target.add_derived_target ("Haskell_Quickcheck", [(Code_Haskell.target, I)])\<close>
15
16code_printing
17  code_module Typerep \<rightharpoonup> (Haskell_Quickcheck) \<open>
18data Typerep = Typerep String [Typerep]
19\<close>
20| type_constructor typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep.Typerep"
21| constant Typerep.Typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep.Typerep"
22| type_constructor integer \<rightharpoonup> (Haskell_Quickcheck) "Prelude.Int"
23
24code_reserved Haskell_Quickcheck Typerep
25
26code_printing
27  constant "0::integer" \<rightharpoonup>
28    (Haskell_Quickcheck) "!(0/ ::/ Prelude.Int)"
29
30setup \<open>
31  let
32    val target = "Haskell_Quickcheck";
33    fun print _ = Code_Haskell.print_numeral "Prelude.Int";
34  in
35    Numeral.add_code @{const_name Code_Numeral.Pos} I print target
36    #> Numeral.add_code @{const_name Code_Numeral.Neg} (~) print target
37  end
38\<close>
39
40
41subsubsection \<open>Narrowing's deep representation of types and terms\<close>
42
43datatype (plugins only: code extraction) narrowing_type =
44  Narrowing_sum_of_products "narrowing_type list list"
45
46datatype (plugins only: code extraction) narrowing_term =
47  Narrowing_variable "integer list" narrowing_type
48| Narrowing_constructor integer "narrowing_term list"
49
50datatype (plugins only: code extraction) (dead 'a) narrowing_cons =
51  Narrowing_cons narrowing_type "(narrowing_term list \<Rightarrow> 'a) list"
52
53primrec map_cons :: "('a => 'b) => 'a narrowing_cons => 'b narrowing_cons"
54where
55  "map_cons f (Narrowing_cons ty cs) = Narrowing_cons ty (map (\<lambda>c. f \<circ> c) cs)"
56
57subsubsection \<open>From narrowing's deep representation of terms to @{theory HOL.Code_Evaluation}'s terms\<close>
58
59class partial_term_of = typerep +
60  fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term"
61
62lemma partial_term_of_anything: "partial_term_of x nt \<equiv> t"
63  by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp)
64 
65subsubsection \<open>Auxilary functions for Narrowing\<close>
66
67consts nth :: "'a list => integer => 'a"
68
69code_printing constant nth \<rightharpoonup> (Haskell_Quickcheck) infixl 9 "!!"
70
71consts error :: "char list => 'a"
72
73code_printing constant error \<rightharpoonup> (Haskell_Quickcheck) "error"
74
75consts toEnum :: "integer => char"
76
77code_printing constant toEnum \<rightharpoonup> (Haskell_Quickcheck) "Prelude.toEnum"
78
79consts marker :: "char"
80
81code_printing constant marker \<rightharpoonup> (Haskell_Quickcheck) "''\\0'"
82
83subsubsection \<open>Narrowing's basic operations\<close>
84
85type_synonym 'a narrowing = "integer => 'a narrowing_cons"
86
87definition cons :: "'a => 'a narrowing"
88where
89  "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(\<lambda>_. a)])"
90
91fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a"
92where
93  "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)"
94| "conv cs (Narrowing_constructor i xs) = (nth cs i) xs"
95
96fun non_empty :: "narrowing_type => bool"
97where
98  "non_empty (Narrowing_sum_of_products ps) = (\<not> (List.null ps))"
99
100definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
101where
102  "apply f a d = (if d > 0 then
103     (case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs \<Rightarrow>
104       case a (d - 1) of Narrowing_cons ta cas \<Rightarrow>
105       let
106         shallow = non_empty ta;
107         cs = [(\<lambda>(x # xs) \<Rightarrow> cf xs (conv cas x)). shallow, cf \<leftarrow> cfs]
108       in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p \<leftarrow> ps]) cs)
109     else Narrowing_cons (Narrowing_sum_of_products []) [])"
110
111definition sum :: "'a narrowing => 'a narrowing => 'a narrowing"
112where
113  "sum a b d =
114    (case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca \<Rightarrow>
115      case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb \<Rightarrow>
116      Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))"
117
118lemma [fundef_cong]:
119  assumes "a d = a' d" "b d = b' d" "d = d'"
120  shows "sum a b d = sum a' b' d'"
121using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split)
122
123lemma [fundef_cong]:
124  assumes "f d = f' d" "(\<And>d'. 0 \<le> d' \<and> d' < d \<Longrightarrow> a d' = a' d')"
125  assumes "d = d'"
126  shows "apply f a d = apply f' a' d'"
127proof -
128  note assms
129  moreover have "0 < d' \<Longrightarrow> 0 \<le> d' - 1"
130    by (simp add: less_integer_def less_eq_integer_def)
131  ultimately show ?thesis
132    by (auto simp add: apply_def Let_def
133      split: narrowing_cons.split narrowing_type.split)
134qed
135
136subsubsection \<open>Narrowing generator type class\<close>
137
138class narrowing =
139  fixes narrowing :: "integer => 'a narrowing_cons"
140
141datatype (plugins only: code extraction) property =
142  Universal narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term"
143| Existential narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term"
144| Property bool
145
146(* FIXME: hard-wired maximal depth of 100 here *)
147definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
148where
149  "exists f = (case narrowing (100 :: integer) of Narrowing_cons ty cs \<Rightarrow> Existential ty (\<lambda> t. f (conv cs t)) (partial_term_of (TYPE('a))))"
150
151definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
152where
153  "all f = (case narrowing (100 :: integer) of Narrowing_cons ty cs \<Rightarrow> Universal ty (\<lambda>t. f (conv cs t)) (partial_term_of (TYPE('a))))"
154
155subsubsection \<open>class \<open>is_testable\<close>\<close>
156
157text \<open>The class \<open>is_testable\<close> ensures that all necessary type instances are generated.\<close>
158
159class is_testable
160
161instance bool :: is_testable ..
162
163instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
164
165definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
166where
167  "ensure_testable f = f"
168
169
170subsubsection \<open>Defining a simple datatype to represent functions in an incomplete and redundant way\<close>
171
172datatype (plugins only: code quickcheck_narrowing extraction) (dead 'a, dead 'b) ffun =
173  Constant 'b
174| Update 'a 'b "('a, 'b) ffun"
175
176primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
177where
178  "eval_ffun (Constant c) x = c"
179| "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
180
181hide_type (open) ffun
182hide_const (open) Constant Update eval_ffun
183
184datatype (plugins only: code quickcheck_narrowing extraction) (dead 'b) cfun = Constant 'b
185
186primrec eval_cfun :: "'b cfun => 'a => 'b"
187where
188  "eval_cfun (Constant c) y = c"
189
190hide_type (open) cfun
191hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun
192
193subsubsection \<open>Setting up the counterexample generator\<close>
194
195external_file "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs"
196external_file "~~/src/HOL/Tools/Quickcheck/PNF_Narrowing_Engine.hs"
197ML_file "Tools/Quickcheck/narrowing_generators.ML"
198
199definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
200where
201  "narrowing_dummy_partial_term_of = partial_term_of"
202
203definition narrowing_dummy_narrowing :: "integer => ('a :: narrowing) narrowing_cons"
204where
205  "narrowing_dummy_narrowing = narrowing"
206
207lemma [code]:
208  "ensure_testable f =
209    (let
210      x = narrowing_dummy_narrowing :: integer => bool narrowing_cons;
211      y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term;
212      z = (conv :: _ => _ => unit)  in f)"
213unfolding Let_def ensure_testable_def ..
214
215subsection \<open>Narrowing for sets\<close>
216
217instantiation set :: (narrowing) narrowing
218begin
219
220definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing"
221
222instance ..
223
224end
225  
226subsection \<open>Narrowing for integers\<close>
227
228
229definition drawn_from :: "'a list \<Rightarrow> 'a narrowing_cons"
230where
231  "drawn_from xs =
232    Narrowing_cons (Narrowing_sum_of_products (map (\<lambda>_. []) xs)) (map (\<lambda>x _. x) xs)"
233
234function around_zero :: "int \<Rightarrow> int list"
235where
236  "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))"
237  by pat_completeness auto
238termination by (relation "measure nat") auto
239
240declare around_zero.simps [simp del]
241
242lemma length_around_zero:
243  assumes "i >= 0" 
244  shows "length (around_zero i) = 2 * nat i + 1"
245proof (induct rule: int_ge_induct [OF assms])
246  case 1
247  from 1 show ?case by (simp add: around_zero.simps)
248next
249  case (2 i)
250  from 2 show ?case
251    by (simp add: around_zero.simps [of "i + 1"])
252qed
253
254instantiation int :: narrowing
255begin
256
257definition
258  "narrowing_int d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
259    in drawn_from (around_zero i))"
260
261instance ..
262
263end
264
265declare [[code drop: "partial_term_of :: int itself \<Rightarrow> _"]]
266
267lemma [code]:
268  "partial_term_of (ty :: int itself) (Narrowing_variable p t) \<equiv>
269    Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])"
270  "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \<equiv>
271    (if i mod 2 = 0
272     then Code_Evaluation.term_of (- (int_of_integer i) div 2)
273     else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))"
274  by (rule partial_term_of_anything)+
275
276instantiation integer :: narrowing
277begin
278
279definition
280  "narrowing_integer d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
281    in drawn_from (map integer_of_int (around_zero i)))"
282
283instance ..
284
285end
286
287declare [[code drop: "partial_term_of :: integer itself \<Rightarrow> _"]]  
288
289lemma [code]:
290  "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \<equiv>
291    Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])"
292  "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \<equiv>
293    (if i mod 2 = 0
294     then Code_Evaluation.term_of (- i div 2)
295     else Code_Evaluation.term_of ((i + 1) div 2))"
296  by (rule partial_term_of_anything)+
297
298code_printing constant "Code_Evaluation.term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Haskell_Quickcheck) 
299  "(let { t = Typerep.Typerep \"Code'_Numeral.integer\" [];
300     mkFunT s t = Typerep.Typerep \"fun\" [s, t];
301     numT = Typerep.Typerep \"Num.num\" [];
302     mkBit 0 = Generated'_Code.Const \"Num.num.Bit0\" (mkFunT numT numT);
303     mkBit 1 = Generated'_Code.Const \"Num.num.Bit1\" (mkFunT numT numT);
304     mkNumeral 1 = Generated'_Code.Const \"Num.num.One\" numT;
305     mkNumeral i = let { q = i `Prelude.div` 2; r = i `Prelude.mod` 2 }
306       in Generated'_Code.App (mkBit r) (mkNumeral q);
307     mkNumber 0 = Generated'_Code.Const \"Groups.zero'_class.zero\" t;
308     mkNumber 1 = Generated'_Code.Const \"Groups.one'_class.one\" t;
309     mkNumber i = if i > 0 then
310         Generated'_Code.App
311           (Generated'_Code.Const \"Num.numeral'_class.numeral\"
312              (mkFunT numT t))
313           (mkNumeral i)
314       else
315         Generated'_Code.App
316           (Generated'_Code.Const \"Groups.uminus'_class.uminus\" (mkFunT t t))
317           (mkNumber (- i)); } in mkNumber)"
318
319subsection \<open>The \<open>find_unused_assms\<close> command\<close>
320
321ML_file "Tools/Quickcheck/find_unused_assms.ML"
322
323subsection \<open>Closing up\<close>
324
325hide_type narrowing_type narrowing_term narrowing_cons property
326hide_const map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero
327hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons
328hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def
329
330end
331