src/HOL/ex/Quickcheck_Examples.thy
author bulwahn
Wed, 30 Mar 2011 09:44:16 +0200
changeset 42159 234ec7011e5d
parent 42087 5e236f6ef04f
child 42434 1914fd5d7c0e
permissions -rw-r--r--
generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/ex/Quickcheck_Examples.thy
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
     2
    Author:     Stefan Berghofer, Lukas Bulwahn
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
     3
    Copyright   2004 - 2010 TU Muenchen
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     4
*)
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     5
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     6
header {* Examples for the 'quickcheck' command *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     7
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
     8
theory Quickcheck_Examples
41231
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
     9
imports Complex_Main
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    10
begin
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    11
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    12
text {*
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    13
The 'quickcheck' command allows to find counterexamples by evaluating
40654
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    14
formulae.
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    15
Currently, there are two different exploration schemes:
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    16
- random testing: this is incomplete, but explores the search space faster.
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    17
- exhaustive testing: this is complete, but increasing the depth leads to
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    18
  exponentially many assignments.
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    19
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    20
quickcheck can handle quantifiers on finite universes.
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
    21
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    22
*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    23
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    24
subsection {* Lists *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    25
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    26
theorem "map g (map f xs) = map (g o f) xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    27
  quickcheck[random, expect = no_counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    28
  quickcheck[exhaustive, size = 3, expect = no_counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    29
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    30
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    31
theorem "map g (map f xs) = map (f o g) xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    32
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    33
  quickcheck[exhaustive, expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    34
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    35
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    36
theorem "rev (xs @ ys) = rev ys @ rev xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    37
  quickcheck[random, expect = no_counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    38
  quickcheck[exhaustive, expect = no_counterexample]
42087
5e236f6ef04f changing timeout behaviour of quickcheck to proceed after command rather than failing; adding a test case for timeout
bulwahn
parents: 41231
diff changeset
    39
  quickcheck[exhaustive, size = 1000, timeout = 0.1]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    40
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    41
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    42
theorem "rev (xs @ ys) = rev xs @ rev ys"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    43
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    44
  quickcheck[exhaustive, expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    45
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    46
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    47
theorem "rev (rev xs) = xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    48
  quickcheck[random, expect = no_counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    49
  quickcheck[exhaustive, expect = no_counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    50
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    51
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    52
theorem "rev xs = xs"
42159
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    53
  quickcheck[tester = random, finite_types = true, report = false, expect = counterexample]
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    54
  quickcheck[tester = random, finite_types = false, report = false, expect = counterexample]
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    55
  quickcheck[tester = random, finite_types = true, report = true, expect = counterexample]
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    56
  quickcheck[tester = random, finite_types = false, report = true, expect = counterexample]
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    57
  quickcheck[tester = exhaustive, finite_types = true, expect = counterexample]
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    58
  quickcheck[tester = exhaustive, finite_types = false, expect = counterexample]
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    59
oops
234ec7011e5d generalizing compilation scheme of quickcheck generators to multiple arguments; changing random and exhaustive tester to use one code invocation for polymorphic instances with multiple cardinalities
bulwahn
parents: 42087
diff changeset
    60
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    61
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    62
text {* An example involving functions inside other data structures *}
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    63
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    64
primrec app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a" where
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    65
  "app [] x = x"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    66
  | "app (f # fs) x = app fs (f x)"
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    67
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    68
lemma "app (fs @ gs) x = app gs (app fs x)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    69
  quickcheck[random, expect = no_counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    70
  quickcheck[exhaustive, size = 4, expect = no_counterexample]
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    71
  by (induct fs arbitrary: x) simp_all
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    72
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    73
lemma "app (fs @ gs) x = app fs (app gs x)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    74
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    75
  quickcheck[exhaustive, expect = counterexample]
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    76
  oops
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    77
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    78
primrec occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    79
  "occurs a [] = 0"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    80
  | "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    81
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    82
primrec del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    83
  "del1 a [] = []"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    84
  | "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    85
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    86
text {* A lemma, you'd think to be true from our experience with delAll *}
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    87
lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    88
  -- {* Wrong. Precondition needed.*}
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    89
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    90
  quickcheck[exhaustive, expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    91
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    92
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    93
lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    94
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
    95
  quickcheck[exhaustive, expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    96
    -- {* Also wrong.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    97
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    98
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    99
lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   100
  quickcheck[random, expect = no_counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   101
  quickcheck[exhaustive, expect = no_counterexample]
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   102
  by (induct xs) auto
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   103
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   104
primrec replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   105
  "replace a b [] = []"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   106
  | "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs)) 
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   107
                            else (x#(replace a b xs)))"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   108
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   109
lemma "occurs a xs = occurs b (replace a b xs)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   110
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   111
  quickcheck[exhaustive, expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   112
  -- {* Wrong. Precondition needed.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   113
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   114
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   115
lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   116
  quickcheck[random, expect = no_counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   117
  quickcheck[exhaustive, expect = no_counterexample]
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   118
  by (induct xs) simp_all
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   119
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   120
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   121
subsection {* Trees *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   122
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   123
datatype 'a tree = Twig |  Leaf 'a | Branch "'a tree" "'a tree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   124
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   125
primrec leaves :: "'a tree \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   126
  "leaves Twig = []"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   127
  | "leaves (Leaf a) = [a]"
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   128
  | "leaves (Branch l r) = (leaves l) @ (leaves r)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   129
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   130
primrec plant :: "'a list \<Rightarrow> 'a tree" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   131
  "plant [] = Twig "
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   132
  | "plant (x#xs) = Branch (Leaf x) (plant xs)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   133
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   134
primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   135
  "mirror (Twig) = Twig "
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   136
  | "mirror (Leaf a) = Leaf a "
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   137
  | "mirror (Branch l r) = Branch (mirror r) (mirror l)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   138
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   139
theorem "plant (rev (leaves xt)) = mirror xt"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   140
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   141
  quickcheck[exhaustive, expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   142
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   143
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   144
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   145
theorem "plant((leaves xt) @ (leaves yt)) = Branch xt yt"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   146
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   147
  quickcheck[exhaustive, expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   148
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   149
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   150
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   151
datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   152
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   153
primrec inOrder :: "'a ntree \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   154
  "inOrder (Tip a)= [a]"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   155
  | "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   156
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   157
primrec root :: "'a ntree \<Rightarrow> 'a" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   158
  "root (Tip a) = a"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   159
  | "root (Node f x y) = f"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   160
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   161
theorem "hd (inOrder xt) = root xt"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   162
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   163
  quickcheck[exhaustive, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   164
  --{* Wrong! *} 
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   165
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   166
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   167
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   168
subsection {* Exhaustive Testing beats Random Testing *}
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   169
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   170
text {* Here are some examples from mutants from the List theory
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   171
where exhaustive testing beats random testing *}
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   172
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   173
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   174
  "[] ~= xs ==> hd xs = last (x # xs)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   175
quickcheck[random]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   176
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   177
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   178
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   179
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   180
  assumes "!!i. [| i < n; i < length xs |] ==> P (xs ! i)" "n < length xs ==> ~ P (xs ! n)"
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   181
  shows "drop n xs = takeWhile P xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   182
quickcheck[random, iterations = 10000, quiet]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   183
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   184
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   185
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   186
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   187
  "i < length (List.transpose (List.transpose xs)) ==> xs ! i = map (%xs. xs ! i) [ys<-xs. i < length ys]"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   188
quickcheck[random, iterations = 10000]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   189
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   190
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   191
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   192
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   193
  "i < n - m ==> f (lcm m i) = map f [m..<n] ! i"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   194
quickcheck[random, iterations = 10000, finite_types = false]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   195
quickcheck[exhaustive, finite_types = false, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   196
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   197
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   198
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   199
  "i < n - m ==> f (lcm m i) = map f [m..<n] ! i"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   200
quickcheck[random, iterations = 10000, finite_types = false]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   201
quickcheck[exhaustive, finite_types = false, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   202
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   203
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   204
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   205
  "ns ! k < length ns ==> k <= listsum ns"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   206
quickcheck[random, iterations = 10000, finite_types = false, quiet]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   207
quickcheck[exhaustive, finite_types = false, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   208
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   209
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   210
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   211
  "[| ys = x # xs1; zs = xs1 @ xs |] ==> ys @ zs = x # xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   212
quickcheck[random, iterations = 10000]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   213
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   214
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   215
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   216
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   217
"i < length xs ==> take (Suc i) xs = [] @ xs ! i # take i xs"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   218
quickcheck[random, iterations = 10000]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   219
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   220
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   221
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   222
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   223
  "i < length xs ==> take (Suc i) xs = (xs ! i # xs) @ take i []"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   224
quickcheck[random, iterations = 10000]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   225
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   226
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   227
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   228
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   229
  "[| sorted (rev (map length xs)); i < length xs |] ==> xs ! i = map (%ys. ys ! i) [ys<-remdups xs. i < length ys]"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   230
quickcheck[random]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   231
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   232
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   233
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   234
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   235
  "[| sorted (rev (map length xs)); i < length xs |] ==> xs ! i = map (%ys. ys ! i) [ys<-List.transpose xs. {..<i} (length ys)]"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   236
quickcheck[random]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   237
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   238
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   239
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   240
lemma
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   241
  "(ys = zs) = (xs @ ys = splice xs zs)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   242
quickcheck[random]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   243
quickcheck[exhaustive, expect = counterexample]
40645
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   244
oops
03ce94672ee6 adding test cases for smallcheck and adding examples where exhaustive testing is more successful
bulwahn
parents: 37929
diff changeset
   245
41231
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   246
subsection {* Examples with quantifiers *}
40654
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   247
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   248
text {*
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   249
  These examples show that we can handle quantifiers.
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   250
*}
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   251
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   252
lemma "(\<exists>x. P x) \<longrightarrow> (\<forall>x. P x)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   253
  quickcheck[random, expect = counterexample]
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   254
  quickcheck[exhaustive, expect = counterexample]
40654
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   255
oops
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   256
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   257
lemma "(\<forall>x. \<exists>y. P x y) \<longrightarrow> (\<exists>y. \<forall>x. P x y)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   258
  quickcheck[random, expect = counterexample]
40654
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   259
  quickcheck[expect = counterexample]
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   260
oops
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   261
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   262
lemma "(\<exists>x. P x) \<longrightarrow> (EX! x. P x)"
40917
c288fd2ead5a adapting quickcheck examples
bulwahn
parents: 40660
diff changeset
   263
  quickcheck[random, expect = counterexample]
40654
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   264
  quickcheck[expect = counterexample]
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   265
oops
a716071ec306 adapting the quickcheck examples
bulwahn
parents: 40645
diff changeset
   266
41231
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   267
subsection {* Examples with numerical types *}
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   268
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   269
text {*
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   270
Quickcheck supports the common types nat, int, rat and real.
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   271
*}
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   272
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   273
lemma
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   274
  "(x :: nat) > 0 ==> y > 0 ==> z > 0 ==> x * x + y * y \<noteq> z * z"
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   275
quickcheck[exhaustive, size = 10, expect = counterexample]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   276
quickcheck[random, size = 10]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   277
oops
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   278
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   279
lemma
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   280
  "(x :: int) > 0 ==> y > 0 ==> z > 0 ==> x * x + y * y \<noteq> z * z"
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   281
quickcheck[exhaustive, size = 10, expect = counterexample]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   282
quickcheck[random, size = 10]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   283
oops
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   284
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   285
lemma
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   286
  "(x :: rat) > 0 ==> y > 0 ==> z > 0 ==> x * x + y * y \<noteq> z * z"
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   287
quickcheck[exhaustive, size = 10, expect = counterexample]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   288
quickcheck[random, size = 10]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   289
oops
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   290
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   291
lemma
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   292
  "(x :: real) > 0 ==> y > 0 ==> z > 0 ==> x * x + y * y \<noteq> z * z"
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   293
quickcheck[exhaustive, size = 10, expect = counterexample]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   294
quickcheck[random, size = 10]
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   295
oops
2e901158675e adding exhaustive tester instances for numeric types: code_numeral, nat, rat and real
bulwahn
parents: 40917
diff changeset
   296
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   297
end