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