(* Title: HOL/Quickcheck_Examples/Quickcheck_Examples.thy Author: Stefan Berghofer, Lukas Bulwahn Copyright 2004 - 2010 TU Muenchen
*)
section \<open>Examples for the 'quickcheck' command\<close>
theory Quickcheck_Examples imports Complex_Main "HOL-Library.Dlist""HOL-Library.DAList_Multiset" begin
text\<open>
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.
\<close>
declare [[quickcheck_timeout = 3600]]
subsection \<open>Lists\<close>
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\<open>A lemma, you'd think to be true from our experience with delAll\<close> lemma"Suc (occurs a (del1 a xs)) = occurs a xs" \<comment> \<open>Wrong. Precondition needed.\<close> 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] \<comment> \<open>Also wrong.\<close> 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] \<comment> \<open>Wrong. Precondition needed.\<close> 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 \<open>Trees\<close>
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)"
primrec plant :: "'a list \ 'a tree" where "plant [] = Twig "
| "plant (x#xs) = Branch (Leaf x) (plant xs)"
primrec mirror :: "'a tree \ 'a tree" where "mirror (Twig) = Twig "
| "mirror (Leaf a) = Leaf a "
| "mirror (Branch l r) = Branch (mirror r) (mirror l)"
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
lemma mult_inj_if_coprime_nat: "inj_on f A \ inj_on g B \<Longrightarrow> inj_on (%(a,b). f a * g b::nat) (A \<times> B)" quickcheck[exhaustive] quickcheck[random] oops
subsection \<open>Examples with quantifiers\<close>
text\<open>
These examples show that we can handle quantifiers. \<close>
lemma"(\x. P x) \ (\x. P x)" quickcheck[random, expect = counterexample] quickcheck[exhaustive, expect = counterexample] oops
lemma"(\x. \y. P x y) \ (\y. \x. P x y)" quickcheck[random, expect = counterexample] quickcheck[expect = counterexample] oops
lemma"(\x. P x) \ (\!x. P x)" quickcheck[random, expect = counterexample] quickcheck[expect = counterexample] oops
subsection \<open>Examples with sets\<close>
lemma "{} = A Un - A" quickcheck[exhaustive, expect = counterexample] oops
lemma "[| bij_betw f A B; bij_betw f C D |] ==> bij_betw f (A Un C) (B Un D)" quickcheck[exhaustive, 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
subsection \<open>Examples with HOL quantifiers\<close>
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 ist noch experimentell.