| 14592 |      1 | (*  Title:      HOL/ex/Quickcheck_Examples.thy
 | 
|  |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Stefan Berghofer
 | 
|  |      4 |     Copyright   2004 TU Muenchen
 | 
|  |      5 | *)
 | 
|  |      6 | 
 | 
|  |      7 | header {* Examples for the 'quickcheck' command *}
 | 
|  |      8 | 
 | 
| 28314 |      9 | theory Quickcheck_Examples
 | 
|  |     10 | imports Main
 | 
|  |     11 | begin
 | 
| 14592 |     12 | 
 | 
|  |     13 | text {*
 | 
|  |     14 | The 'quickcheck' command allows to find counterexamples by evaluating
 | 
|  |     15 | formulae under an assignment of free variables to random values.
 | 
|  |     16 | In contrast to 'refute', it can deal with inductive datatypes,
 | 
|  |     17 | but cannot handle quantifiers.
 | 
|  |     18 | *}
 | 
|  |     19 | 
 | 
|  |     20 | subsection {* Lists *}
 | 
|  |     21 | 
 | 
|  |     22 | theorem "map g (map f xs) = map (g o f) xs"
 | 
| 28336 |     23 |   quickcheck
 | 
| 14592 |     24 |   oops
 | 
|  |     25 | 
 | 
|  |     26 | theorem "map g (map f xs) = map (f o g) xs"
 | 
| 28336 |     27 |   quickcheck
 | 
| 14592 |     28 |   oops
 | 
|  |     29 | 
 | 
|  |     30 | theorem "rev (xs @ ys) = rev ys @ rev xs"
 | 
| 28336 |     31 |   quickcheck
 | 
| 14592 |     32 |   oops
 | 
|  |     33 | 
 | 
|  |     34 | theorem "rev (xs @ ys) = rev xs @ rev ys"
 | 
| 28336 |     35 |   quickcheck
 | 
| 14592 |     36 |   oops
 | 
|  |     37 | 
 | 
|  |     38 | theorem "rev (rev xs) = xs"
 | 
| 28336 |     39 |   quickcheck
 | 
| 14592 |     40 |   oops
 | 
|  |     41 | 
 | 
|  |     42 | theorem "rev xs = xs"
 | 
| 28336 |     43 |   quickcheck
 | 
| 14592 |     44 |   oops
 | 
|  |     45 | 
 | 
| 25891 |     46 | text {* An example involving functions inside other data structures *}
 | 
|  |     47 | 
 | 
| 28314 |     48 | primrec app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a" where
 | 
| 25891 |     49 |   "app [] x = x"
 | 
| 28314 |     50 |   | "app (f # fs) x = app fs (f x)"
 | 
| 25891 |     51 | 
 | 
|  |     52 | lemma "app (fs @ gs) x = app gs (app fs x)"
 | 
| 28336 |     53 |   quickcheck
 | 
| 25891 |     54 |   by (induct fs arbitrary: x) simp_all
 | 
|  |     55 | 
 | 
|  |     56 | lemma "app (fs @ gs) x = app fs (app gs x)"
 | 
| 28336 |     57 |   quickcheck
 | 
| 25891 |     58 |   oops
 | 
|  |     59 | 
 | 
| 28314 |     60 | primrec occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
 | 
| 14592 |     61 |   "occurs a [] = 0"
 | 
| 28314 |     62 |   | "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
 | 
| 14592 |     63 | 
 | 
| 28314 |     64 | primrec del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | 
| 14592 |     65 |   "del1 a [] = []"
 | 
| 28314 |     66 |   | "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
 | 
| 14592 |     67 | 
 | 
| 25891 |     68 | text {* A lemma, you'd think to be true from our experience with delAll *}
 | 
| 14592 |     69 | lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
 | 
|  |     70 |   -- {* Wrong. Precondition needed.*}
 | 
| 28336 |     71 |   quickcheck
 | 
| 14592 |     72 |   oops
 | 
|  |     73 | 
 | 
|  |     74 | lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
 | 
| 28336 |     75 |   quickcheck
 | 
| 14592 |     76 |     -- {* Also wrong.*}
 | 
|  |     77 |   oops
 | 
|  |     78 | 
 | 
|  |     79 | lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
 | 
| 28336 |     80 |   quickcheck
 | 
| 28314 |     81 |   by (induct xs) auto
 | 
| 14592 |     82 | 
 | 
| 28314 |     83 | primrec replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
 | 
| 14592 |     84 |   "replace a b [] = []"
 | 
| 28314 |     85 |   | "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs)) 
 | 
| 14592 |     86 |                             else (x#(replace a b xs)))"
 | 
|  |     87 | 
 | 
|  |     88 | lemma "occurs a xs = occurs b (replace a b xs)"
 | 
| 28336 |     89 |   quickcheck
 | 
| 14592 |     90 |   -- {* Wrong. Precondition needed.*}
 | 
|  |     91 |   oops
 | 
|  |     92 | 
 | 
|  |     93 | lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
 | 
| 28336 |     94 |   quickcheck
 | 
| 28314 |     95 |   by (induct xs) simp_all
 | 
| 14592 |     96 | 
 | 
|  |     97 | 
 | 
|  |     98 | subsection {* Trees *}
 | 
|  |     99 | 
 | 
|  |    100 | datatype 'a tree = Twig |  Leaf 'a | Branch "'a tree" "'a tree"
 | 
|  |    101 | 
 | 
| 28314 |    102 | primrec leaves :: "'a tree \<Rightarrow> 'a list" where
 | 
| 14592 |    103 |   "leaves Twig = []"
 | 
| 28314 |    104 |   | "leaves (Leaf a) = [a]"
 | 
|  |    105 |   | "leaves (Branch l r) = (leaves l) @ (leaves r)"
 | 
| 14592 |    106 | 
 | 
| 28314 |    107 | primrec plant :: "'a list \<Rightarrow> 'a tree" where
 | 
| 14592 |    108 |   "plant [] = Twig "
 | 
| 28314 |    109 |   | "plant (x#xs) = Branch (Leaf x) (plant xs)"
 | 
| 14592 |    110 | 
 | 
| 28314 |    111 | primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
 | 
| 14592 |    112 |   "mirror (Twig) = Twig "
 | 
| 28314 |    113 |   | "mirror (Leaf a) = Leaf a "
 | 
|  |    114 |   | "mirror (Branch l r) = Branch (mirror r) (mirror l)"
 | 
| 14592 |    115 | 
 | 
|  |    116 | theorem "plant (rev (leaves xt)) = mirror xt"
 | 
| 28336 |    117 |   quickcheck
 | 
| 14592 |    118 |     --{* Wrong! *} 
 | 
|  |    119 |   oops
 | 
|  |    120 | 
 | 
|  |    121 | theorem "plant((leaves xt) @ (leaves yt)) = Branch xt yt"
 | 
| 28336 |    122 |   quickcheck
 | 
| 14592 |    123 |     --{* Wrong! *} 
 | 
|  |    124 |   oops
 | 
|  |    125 | 
 | 
|  |    126 | datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
 | 
|  |    127 | 
 | 
| 28314 |    128 | primrec inOrder :: "'a ntree \<Rightarrow> 'a list" where
 | 
| 14592 |    129 |   "inOrder (Tip a)= [a]"
 | 
| 28314 |    130 |   | "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
 | 
| 14592 |    131 | 
 | 
| 28314 |    132 | primrec root :: "'a ntree \<Rightarrow> 'a" where
 | 
| 14592 |    133 |   "root (Tip a) = a"
 | 
| 28314 |    134 |   | "root (Node f x y) = f"
 | 
| 14592 |    135 | 
 | 
| 28314 |    136 | theorem "hd (inOrder xt) = root xt"
 | 
| 28336 |    137 |   quickcheck
 | 
| 14592 |    138 |     --{* Wrong! *} 
 | 
|  |    139 |   oops
 | 
|  |    140 | 
 | 
|  |    141 | end
 |