src/HOL/ex/Quickcheck_Examples.thy
author haftmann
Fri, 29 Dec 2006 12:11:00 +0100
changeset 21924 fe474e69e603
parent 17388 495c799df31d
child 25891 1bd12187a96e
permissions -rw-r--r--
simplified class_package
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
    ID:         $Id$
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     4
    Copyright   2004 TU Muenchen
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
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     7
header {* Examples for the 'quickcheck' command *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
     8
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 14592
diff changeset
     9
theory Quickcheck_Examples imports Main begin
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    10
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    11
text {*
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    12
The 'quickcheck' command allows to find counterexamples by evaluating
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    13
formulae under an assignment of free variables to random values.
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    14
In contrast to 'refute', it can deal with inductive datatypes,
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    15
but cannot handle quantifiers.
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    16
*}
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
subsection {* Lists *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    19
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    20
theorem "map g (map f xs) = map (g o f) xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    21
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    22
  oops
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
theorem "map g (map f xs) = map (f o g) xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    25
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    26
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    27
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    28
theorem "rev (xs @ ys) = rev ys @ rev xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    29
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    30
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    31
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    32
theorem "rev (xs @ ys) = rev xs @ rev ys"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    33
  quickcheck
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 (rev xs) = xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    37
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    38
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    39
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    40
theorem "rev xs = xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    41
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    42
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    43
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    44
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    45
  occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    46
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    47
  "occurs a [] = 0"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    48
  "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    49
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    50
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    51
  del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    52
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    53
  "del1 a [] = []"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    54
  "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    55
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    56
(* A lemma, you'd think to be true from our experience with delAll*)
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    57
lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    58
  -- {* Wrong. Precondition needed.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    59
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    60
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    61
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    62
lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    63
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    64
    -- {* Also wrong.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    65
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    66
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    67
lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    68
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    69
  apply (induct_tac xs)  
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    70
  apply auto
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    71
    -- {* Correct! *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    72
  done
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    73
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    74
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    75
  replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    76
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    77
  "replace a b [] = []"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    78
  "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs)) 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    79
                            else (x#(replace a b xs)))"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    80
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    81
lemma "occurs a xs = occurs b (replace a b xs)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    82
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    83
  -- {* Wrong. Precondition needed.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    84
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    85
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    86
lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    87
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    88
  apply (induct_tac xs)  
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    89
  apply auto
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    90
  done
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
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    93
subsection {* Trees *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    94
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    95
datatype 'a tree = Twig |  Leaf 'a | Branch "'a tree" "'a tree"
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
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    98
  leaves :: "'a tree \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    99
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   100
  "leaves Twig = []"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   101
  "leaves (Leaf a) = [a]"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   102
  "leaves (Branch l r) = (leaves l) @ (leaves r)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   103
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   104
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   105
  plant :: "'a list \<Rightarrow> 'a tree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   106
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   107
  "plant [] = Twig "
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   108
  "plant (x#xs) = Branch (Leaf x) (plant xs)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   109
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   110
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   111
  mirror :: "'a tree \<Rightarrow> 'a tree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   112
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   113
  "mirror (Twig) = Twig "
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   114
  "mirror (Leaf a) = Leaf a "
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   115
  "mirror (Branch l r) = Branch (mirror r) (mirror l)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   116
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   117
theorem "plant (rev (leaves xt)) = mirror xt"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   118
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   119
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   120
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   121
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   122
theorem "plant((leaves xt) @ (leaves yt)) = Branch xt yt"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   123
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   124
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   125
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   126
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   127
datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   128
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   129
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   130
  inOrder :: "'a ntree \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   131
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   132
  "inOrder (Tip a)= [a]"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   133
  "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   134
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   135
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   136
  root :: "'a ntree \<Rightarrow> 'a"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   137
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   138
  "root (Tip a) = a"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   139
  "root (Node f x y) = f"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   140
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   141
theorem "hd(inOrder xt) = root xt"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   142
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   143
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   144
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   145
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   146
end