src/HOL/ex/Quickcheck_Examples.thy
author haftmann
Thu, 26 Aug 2010 20:51:17 +0200
changeset 38786 e46e7a9cb622
parent 37929 22e0797857e6
child 40645 03ce94672ee6
permissions -rw-r--r--
formerly unnamed infix impliciation now named HOL.implies
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
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     2
    Author:     Stefan Berghofer
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     3
    Copyright   2004 TU Muenchen
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
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
     9
imports Main
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
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    14
formulae under an assignment of free variables to random values.
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    15
In contrast to 'refute', it can deal with inductive datatypes,
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    16
but cannot handle quantifiers.
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    17
*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    18
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    19
subsection {* Lists *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    20
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    21
theorem "map g (map f xs) = map (g o f) xs"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    22
  quickcheck[expect = no_counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    23
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    24
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    25
theorem "map g (map f xs) = map (f o g) xs"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    26
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    27
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    28
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    29
theorem "rev (xs @ ys) = rev ys @ rev xs"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    30
  quickcheck[expect = no_counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    31
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    32
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    33
theorem "rev (xs @ ys) = rev xs @ rev ys"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    34
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    35
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    36
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    37
theorem "rev (rev xs) = xs"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    38
  quickcheck[expect = no_counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    39
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    40
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    41
theorem "rev xs = xs"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    42
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    43
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    44
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    45
text {* An example involving functions inside other data structures *}
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    46
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    47
primrec app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a" where
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    48
  "app [] x = x"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    49
  | "app (f # fs) x = app fs (f x)"
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    50
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    51
lemma "app (fs @ gs) x = app gs (app fs x)"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    52
  quickcheck[expect = no_counterexample]
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    53
  by (induct fs arbitrary: x) simp_all
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    54
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    55
lemma "app (fs @ gs) x = app fs (app gs x)"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    56
  quickcheck[expect = counterexample]
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    57
  oops
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    58
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    59
primrec occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    60
  "occurs a [] = 0"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    61
  | "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
    62
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    63
primrec del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    64
  "del1 a [] = []"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    65
  | "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
    66
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    67
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
    68
lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    69
  -- {* Wrong. Precondition needed.*}
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    70
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    71
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    72
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    73
lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    74
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    75
    -- {* Also wrong.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    76
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    77
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    78
lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    79
  quickcheck[expect = no_counterexample]
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    80
  by (induct xs) auto
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 replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    83
  "replace a b [] = []"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    84
  | "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
    85
                            else (x#(replace a b xs)))"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    86
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    87
lemma "occurs a xs = occurs b (replace a b xs)"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    88
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    89
  -- {* Wrong. Precondition needed.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    90
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    91
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    92
lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
    93
  quickcheck[expect = no_counterexample]
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
    94
  by (induct xs) simp_all
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    95
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    96
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    97
subsection {* Trees *}
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
datatype 'a tree = Twig |  Leaf 'a | Branch "'a tree" "'a tree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   100
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   101
primrec leaves :: "'a tree \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   102
  "leaves Twig = []"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   103
  | "leaves (Leaf a) = [a]"
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   104
  | "leaves (Branch l r) = (leaves l) @ (leaves r)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   105
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   106
primrec plant :: "'a list \<Rightarrow> 'a tree" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   107
  "plant [] = Twig "
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   108
  | "plant (x#xs) = Branch (Leaf x) (plant xs)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   109
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   110
primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   111
  "mirror (Twig) = Twig "
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   112
  | "mirror (Leaf a) = Leaf a "
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   113
  | "mirror (Branch l r) = Branch (mirror r) (mirror l)"
14592
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
theorem "plant (rev (leaves xt)) = mirror xt"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
   116
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   117
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   118
  oops
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
theorem "plant((leaves xt) @ (leaves yt)) = Branch xt yt"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
   121
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   122
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   123
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   124
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   125
datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   126
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   127
primrec inOrder :: "'a ntree \<Rightarrow> 'a list" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   128
  "inOrder (Tip a)= [a]"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   129
  | "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   130
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   131
primrec root :: "'a ntree \<Rightarrow> 'a" where
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   132
  "root (Tip a) = a"
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   133
  | "root (Node f x y) = f"
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   134
28314
053419cefd3c TEMPORARY: make batch run happy
haftmann
parents: 25891
diff changeset
   135
theorem "hd (inOrder xt) = root xt"
37929
22e0797857e6 adding checking of expected result for the tool quickcheck; annotated a few quickcheck examples
bulwahn
parents: 37914
diff changeset
   136
  quickcheck[expect = counterexample]
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   137
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   138
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   139
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   140
end