(* Title: HOL/Quickcheck_Examples/Quickcheck_Examples.thy Author: Stefan Berghofer, Lukas Bulwahn Copyright 2004 - 2010 TU Muenchen *)
section‹Examples for the 'quickcheck' command›
theory Quickcheck_Examples imports Complex_Main "HOL-Library.Dlist""HOL-Library.DAList_Multiset" begin
text‹ The 'quickcheck' command allows to find counterexamples by evaluating formulae. Currently, there are two different exploration schemes: - random testing: this is incomplete, but explores the search space faster. - exhaustive testing: this is complete, but increasing the depth leads to exponentially many assignments. quickcheck can handle quantifiers on finite universes. ›
declare [[quickcheck_timeout = 3600]]
subsection‹Lists›
theorem"map g (map f xs) = map (g o f) xs" quickcheck[random, expect = no_counterexample] quickcheck[exhaustive, size = 3, expect = no_counterexample] oops
theorem"map g (map f xs) = map (f o g) xs" quickcheck[random, expect = counterexample] quickcheck[exhaustive, expect = counterexample] oops
primrec occurs :: "'a ==> 'a list ==> nat"where "occurs a [] = 0"
| "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
primrec del1 :: "'a ==> 'a list ==> 'a list"where "del1 a [] = []"
| "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
text‹A lemma, you'd think to be true from our experience with delAll› lemma"Suc (occurs a (del1 a xs)) = occurs a xs" 🍋‹Wrong. Precondition needed.› quickcheck[random, expect = counterexample] quickcheck[exhaustive, expect = counterexample] oops
lemma"xs ~= [] ⟶ Suc (occurs a (del1 a xs)) = occurs a xs" quickcheck[random, expect = counterexample] quickcheck[exhaustive, expect = counterexample] 🍋‹Also wrong.› oops
lemma"0 < occurs a xs ⟶ Suc (occurs a (del1 a xs)) = occurs a xs" quickcheck[random, expect = no_counterexample] quickcheck[exhaustive, expect = no_counterexample] by (induct xs) auto
primrec replace :: "'a ==> 'a ==> 'a list ==> 'a list"where "replace a b [] = []"
| "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs)) else (x#(replace a b xs)))"
lemma"occurs a xs = occurs b (replace a b xs)" quickcheck[random, expect = counterexample] quickcheck[exhaustive, expect = counterexample] 🍋‹Wrong. Precondition needed.› oops
lemma"occurs b xs = 0 ∨ a=b ⟶ occurs a xs = occurs b (replace a b xs)" quickcheck[random, expect = no_counterexample] quickcheck[exhaustive, expect = no_counterexample] by (induct xs) simp_all
subsection‹Trees›
datatype 'a tree = Twig | Leaf 'a | Branch "'a tree""'a tree"
primrec leaves :: "'a tree ==> 'a list"where "leaves Twig = []"
| "leaves (Leaf a) = [a]"
| "leaves (Branch l r) = (leaves l) @ (leaves r)"
lemma "i < n - m ==> f (lcm m i) = map f [m.. quickcheck[random, iterations = 10000, finite_types = false] quickcheck[exhaustive, finite_types = false, expect = counterexample] oops
lemma "i < n - m ==> f (lcm m i) = map f [m.. quickcheck[random, iterations = 10000, finite_types = false] quickcheck[exhaustive, finite_types = false, expect = counterexample] oops
locale antisym = fixes R assumes"R x y --> R y x --> x = y"
interpretation equal : antisym "(=)"by standard simp interpretation order_nat : antisym "(<=) :: nat => _ => _"by standard simp
lemma (in antisym) "R x y --> R y z --> R x z" quickcheck[exhaustive, finite_type_size = 2, expect = no_counterexample] quickcheck[exhaustive, expect = counterexample] oops
declare [[quickcheck_locale = "interpret"]]
lemma (in antisym) "R x y --> R y z --> R x z" quickcheck[exhaustive, expect = no_counterexample] oops
declare [[quickcheck_locale = "expand"]]
lemma (in antisym) "R x y --> R y z --> R x z" quickcheck[exhaustive, finite_type_size = 2, expect = no_counterexample] quickcheck[exhaustive, expect = counterexample] oops
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.