bulwahn@41905: (* Author: Lukas Bulwahn, TU Muenchen *) bulwahn@41905: bulwahn@43356: header {* Counterexample generator performing narrowing-based testing *} bulwahn@41905: bulwahn@41930: theory Quickcheck_Narrowing bulwahn@43312: imports Quickcheck_Exhaustive wenzelm@46950: keywords "find_unused_assms" :: diag bulwahn@41905: begin bulwahn@41905: bulwahn@41905: subsection {* Counterexample generator *} bulwahn@41905: haftmann@51143: subsubsection {* Code generation setup *} bulwahn@43308: bulwahn@43308: setup {* Code_Target.extend_target ("Haskell_Quickcheck", (Code_Haskell.target, K I)) *} bulwahn@43308: haftmann@52435: code_printing haftmann@52435: type_constructor typerep \ (Haskell_Quickcheck) "Typerep" haftmann@52435: | constant Typerep.Typerep \ (Haskell_Quickcheck) "Typerep" haftmann@52435: | type_constructor integer \ (Haskell_Quickcheck) "Prelude.Int" haftmann@51143: bulwahn@43308: code_reserved Haskell_Quickcheck Typerep bulwahn@41909: bulwahn@42021: bulwahn@41961: subsubsection {* Narrowing's deep representation of types and terms *} bulwahn@41905: bulwahn@46758: datatype narrowing_type = Narrowing_sum_of_products "narrowing_type list list" haftmann@51143: datatype narrowing_term = Narrowing_variable "integer list" narrowing_type | Narrowing_constructor integer "narrowing_term list" bulwahn@46758: datatype 'a narrowing_cons = Narrowing_cons narrowing_type "(narrowing_term list => 'a) list" bulwahn@41905: bulwahn@46758: primrec map_cons :: "('a => 'b) => 'a narrowing_cons => 'b narrowing_cons" bulwahn@43356: where bulwahn@46758: "map_cons f (Narrowing_cons ty cs) = Narrowing_cons ty (map (%c. f o c) cs)" bulwahn@43356: hoelzl@43341: subsubsection {* From narrowing's deep representation of terms to @{theory Code_Evaluation}'s terms *} bulwahn@42980: bulwahn@42980: class partial_term_of = typerep + bulwahn@43047: fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term" bulwahn@43047: bulwahn@43047: lemma partial_term_of_anything: "partial_term_of x nt \ t" bulwahn@43047: by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp) bulwahn@43356: bulwahn@41964: subsubsection {* Auxilary functions for Narrowing *} bulwahn@41905: haftmann@51143: consts nth :: "'a list => integer => 'a" bulwahn@41905: haftmann@52435: code_printing constant nth \ (Haskell_Quickcheck) infixl 9 "!!" bulwahn@41905: bulwahn@41908: consts error :: "char list => 'a" bulwahn@41905: haftmann@52435: code_printing constant error \ (Haskell_Quickcheck) "error" bulwahn@41905: haftmann@51143: consts toEnum :: "integer => char" bulwahn@41908: haftmann@52435: code_printing constant toEnum \ (Haskell_Quickcheck) "Prelude.toEnum" bulwahn@41905: bulwahn@43316: consts marker :: "char" bulwahn@41905: haftmann@52435: code_printing constant marker \ (Haskell_Quickcheck) "''\\0'" bulwahn@43316: bulwahn@41961: subsubsection {* Narrowing's basic operations *} bulwahn@41905: haftmann@51143: type_synonym 'a narrowing = "integer => 'a narrowing_cons" bulwahn@41905: bulwahn@41961: definition empty :: "'a narrowing" bulwahn@41905: where bulwahn@46758: "empty d = Narrowing_cons (Narrowing_sum_of_products []) []" bulwahn@41905: bulwahn@41961: definition cons :: "'a => 'a narrowing" bulwahn@41905: where bulwahn@46758: "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(%_. a)])" bulwahn@41905: bulwahn@43047: fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a" bulwahn@41905: where bulwahn@46758: "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)" bulwahn@46758: | "conv cs (Narrowing_constructor i xs) = (nth cs i) xs" bulwahn@41905: bulwahn@46758: fun non_empty :: "narrowing_type => bool" bulwahn@41905: where bulwahn@46758: "non_empty (Narrowing_sum_of_products ps) = (\ (List.null ps))" bulwahn@41905: bulwahn@41961: definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing" bulwahn@41905: where bulwahn@41905: "apply f a d = bulwahn@46758: (case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs => bulwahn@46758: case a (d - 1) of Narrowing_cons ta cas => bulwahn@41905: let bulwahn@46758: shallow = (d > 0 \ non_empty ta); bulwahn@41905: cs = [(%xs'. (case xs' of [] => undefined | x # xs => cf xs (conv cas x))). shallow, cf <- cfs] bulwahn@46758: in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p <- ps]) cs)" bulwahn@41905: bulwahn@41961: definition sum :: "'a narrowing => 'a narrowing => 'a narrowing" bulwahn@41905: where bulwahn@41905: "sum a b d = bulwahn@46758: (case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca => bulwahn@46758: case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb => bulwahn@46758: Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))" bulwahn@41905: bulwahn@41912: lemma [fundef_cong]: bulwahn@41912: assumes "a d = a' d" "b d = b' d" "d = d'" bulwahn@41912: shows "sum a b d = sum a' b' d'" bulwahn@46758: using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split) bulwahn@41912: bulwahn@41912: lemma [fundef_cong]: haftmann@51143: assumes "f d = f' d" "(\d'. 0 \ d' \ d' < d \ a d' = a' d')" bulwahn@41912: assumes "d = d'" bulwahn@41912: shows "apply f a d = apply f' a' d'" bulwahn@41912: proof - haftmann@51143: note assms haftmann@51143: moreover have "0 < d' \ 0 \ d' - 1" haftmann@51143: by (simp add: less_integer_def less_eq_integer_def) bulwahn@41912: ultimately show ?thesis haftmann@51143: by (auto simp add: apply_def Let_def haftmann@51143: split: narrowing_cons.split narrowing_type.split) bulwahn@41912: qed bulwahn@41912: bulwahn@41961: subsubsection {* Narrowing generator type class *} bulwahn@41905: bulwahn@41961: class narrowing = haftmann@51143: fixes narrowing :: "integer => 'a narrowing_cons" bulwahn@41905: bulwahn@43237: datatype property = Universal narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term" | Existential narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term" | Property bool bulwahn@43237: bulwahn@43237: (* FIXME: hard-wired maximal depth of 100 here *) bulwahn@43315: definition exists :: "('a :: {narrowing, partial_term_of} => property) => property" bulwahn@43237: where haftmann@51143: "exists f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Existential ty (\ t. f (conv cs t)) (partial_term_of (TYPE('a))))" bulwahn@43237: bulwahn@43315: definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property" bulwahn@43237: where haftmann@51143: "all f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Universal ty (\t. f (conv cs t)) (partial_term_of (TYPE('a))))" bulwahn@43237: wenzelm@41943: subsubsection {* class @{text is_testable} *} bulwahn@41905: wenzelm@41943: text {* The class @{text is_testable} ensures that all necessary type instances are generated. *} bulwahn@41905: bulwahn@41905: class is_testable bulwahn@41905: bulwahn@41905: instance bool :: is_testable .. bulwahn@41905: bulwahn@43047: instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable .. bulwahn@41905: bulwahn@41905: definition ensure_testable :: "'a :: is_testable => 'a :: is_testable" bulwahn@41905: where bulwahn@41905: "ensure_testable f = f" bulwahn@41905: bulwahn@41910: bulwahn@42022: subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *} bulwahn@42022: bulwahn@42022: datatype ('a, 'b) ffun = Constant 'b | Update 'a 'b "('a, 'b) ffun" bulwahn@42022: bulwahn@42022: primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b" bulwahn@42022: where bulwahn@42022: "eval_ffun (Constant c) x = c" bulwahn@42022: | "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)" bulwahn@42022: bulwahn@42022: hide_type (open) ffun bulwahn@42022: hide_const (open) Constant Update eval_ffun bulwahn@42022: bulwahn@42024: datatype 'b cfun = Constant 'b bulwahn@42024: bulwahn@42024: primrec eval_cfun :: "'b cfun => 'a => 'b" bulwahn@42024: where bulwahn@42024: "eval_cfun (Constant c) y = c" bulwahn@42024: bulwahn@42024: hide_type (open) cfun huffman@45734: hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun bulwahn@42024: bulwahn@42024: subsubsection {* Setting up the counterexample generator *} bulwahn@43237: wenzelm@48891: ML_file "Tools/Quickcheck/narrowing_generators.ML" bulwahn@42024: bulwahn@42024: setup {* Narrowing_Generators.setup *} bulwahn@42024: bulwahn@45001: definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term" bulwahn@45001: where bulwahn@45001: "narrowing_dummy_partial_term_of = partial_term_of" bulwahn@45001: haftmann@51143: definition narrowing_dummy_narrowing :: "integer => ('a :: narrowing) narrowing_cons" bulwahn@45001: where bulwahn@45001: "narrowing_dummy_narrowing = narrowing" bulwahn@45001: bulwahn@45001: lemma [code]: bulwahn@45001: "ensure_testable f = bulwahn@45001: (let haftmann@51143: x = narrowing_dummy_narrowing :: integer => bool narrowing_cons; bulwahn@45001: y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term; bulwahn@45001: z = (conv :: _ => _ => unit) in f)" bulwahn@45001: unfolding Let_def ensure_testable_def .. bulwahn@45001: bulwahn@46308: subsection {* Narrowing for sets *} bulwahn@46308: bulwahn@46308: instantiation set :: (narrowing) narrowing bulwahn@46308: begin bulwahn@46308: bulwahn@46308: definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing" bulwahn@46308: bulwahn@46308: instance .. bulwahn@46308: bulwahn@46308: end bulwahn@45001: bulwahn@43356: subsection {* Narrowing for integers *} bulwahn@43356: bulwahn@43356: haftmann@51143: definition drawn_from :: "'a list \ 'a narrowing_cons" haftmann@51143: where haftmann@51143: "drawn_from xs = haftmann@51143: Narrowing_cons (Narrowing_sum_of_products (map (\_. []) xs)) (map (\x _. x) xs)" bulwahn@43356: haftmann@51143: function around_zero :: "int \ int list" bulwahn@43356: where bulwahn@43356: "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))" haftmann@51143: by pat_completeness auto bulwahn@43356: termination by (relation "measure nat") auto bulwahn@43356: haftmann@51143: declare around_zero.simps [simp del] bulwahn@43356: bulwahn@43356: lemma length_around_zero: bulwahn@43356: assumes "i >= 0" bulwahn@43356: shows "length (around_zero i) = 2 * nat i + 1" haftmann@51143: proof (induct rule: int_ge_induct [OF assms]) bulwahn@43356: case 1 bulwahn@43356: from 1 show ?case by (simp add: around_zero.simps) bulwahn@43356: next bulwahn@43356: case (2 i) bulwahn@43356: from 2 show ?case haftmann@51143: by (simp add: around_zero.simps [of "i + 1"]) bulwahn@43356: qed bulwahn@43356: bulwahn@43356: instantiation int :: narrowing bulwahn@43356: begin bulwahn@43356: bulwahn@43356: definition haftmann@51143: "narrowing_int d = (let (u :: _ \ _ \ unit) = conv; i = int_of_integer d haftmann@51143: in drawn_from (around_zero i))" bulwahn@43356: bulwahn@43356: instance .. bulwahn@43356: bulwahn@43356: end bulwahn@43356: haftmann@51143: lemma [code, code del]: "partial_term_of (ty :: int itself) t \ undefined" haftmann@51143: by (rule partial_term_of_anything)+ bulwahn@43356: bulwahn@43356: lemma [code]: haftmann@51143: "partial_term_of (ty :: int itself) (Narrowing_variable p t) \ haftmann@51143: Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])" haftmann@51143: "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \ haftmann@51143: (if i mod 2 = 0 haftmann@51143: then Code_Evaluation.term_of (- (int_of_integer i) div 2) haftmann@51143: else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))" haftmann@51143: by (rule partial_term_of_anything)+ haftmann@51143: haftmann@51143: instantiation integer :: narrowing haftmann@51143: begin haftmann@51143: haftmann@51143: definition haftmann@51143: "narrowing_integer d = (let (u :: _ \ _ \ unit) = conv; i = int_of_integer d haftmann@51143: in drawn_from (map integer_of_int (around_zero i)))" haftmann@51143: haftmann@51143: instance .. haftmann@51143: haftmann@51143: end haftmann@51143: haftmann@51143: lemma [code, code del]: "partial_term_of (ty :: integer itself) t \ undefined" haftmann@51143: by (rule partial_term_of_anything)+ haftmann@51143: haftmann@51143: lemma [code]: haftmann@51143: "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \ haftmann@51143: Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])" haftmann@51143: "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \ haftmann@51143: (if i mod 2 = 0 haftmann@51143: then Code_Evaluation.term_of (- i div 2) haftmann@51143: else Code_Evaluation.term_of ((i + 1) div 2))" haftmann@51143: by (rule partial_term_of_anything)+ bulwahn@43356: bulwahn@43356: bulwahn@46589: subsection {* The @{text find_unused_assms} command *} bulwahn@46589: wenzelm@48891: ML_file "Tools/Quickcheck/find_unused_assms.ML" bulwahn@46589: bulwahn@46589: subsection {* Closing up *} bulwahn@46589: haftmann@51143: hide_type narrowing_type narrowing_term narrowing_cons property haftmann@51143: hide_const map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero bulwahn@46758: hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons bulwahn@46758: hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def bulwahn@42022: bulwahn@45001: end haftmann@51143: