src/HOL/ex/Quickcheck_Examples.thy
author haftmann
Mon, 21 Jan 2008 08:43:29 +0100
changeset 25929 6bfef23e26be
parent 25891 1bd12187a96e
child 28314 053419cefd3c
permissions -rw-r--r--
avoiding direct references to numeral presentation
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
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    44
text {* An example involving functions inside other data structures *}
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    45
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    46
consts app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a"
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    47
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    48
primrec
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    49
  "app [] x = x"
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    50
  "app (f # fs) x = app fs (f x)"
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    51
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    52
lemma "app (fs @ gs) x = app gs (app fs x)"
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    53
  quickcheck
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    54
  by (induct fs arbitrary: x) simp_all
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    55
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    56
lemma "app (fs @ gs) x = app fs (app gs x)"
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    57
  quickcheck
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    58
  oops
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    59
14592
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    60
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    61
  occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    62
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    63
  "occurs a [] = 0"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    64
  "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
    65
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    66
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    67
  del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    68
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    69
  "del1 a [] = []"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    70
  "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
    71
25891
1bd12187a96e New example involving functions.
berghofe
parents: 17388
diff changeset
    72
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
    73
lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    74
  -- {* Wrong. Precondition needed.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    75
  quickcheck
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 "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    79
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    80
    -- {* Also wrong.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    81
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    82
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    83
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
    84
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    85
  apply (induct_tac xs)  
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    86
  apply auto
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    87
    -- {* Correct! *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    88
  done
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    89
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    90
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    91
  replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    92
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    93
  "replace a b [] = []"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    94
  "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
    95
                            else (x#(replace a b xs)))"
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
lemma "occurs a xs = occurs b (replace a b xs)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    98
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
    99
  -- {* Wrong. Precondition needed.*}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   100
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   101
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   102
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
   103
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   104
  apply (induct_tac xs)  
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   105
  apply auto
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   106
  done
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   107
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
subsection {* Trees *}
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   110
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   111
datatype 'a tree = Twig |  Leaf 'a | Branch "'a tree" "'a tree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   112
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   113
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   114
  leaves :: "'a tree \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   115
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   116
  "leaves Twig = []"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   117
  "leaves (Leaf a) = [a]"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   118
  "leaves (Branch l r) = (leaves l) @ (leaves r)"
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
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   121
  plant :: "'a list \<Rightarrow> 'a tree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   122
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   123
  "plant [] = Twig "
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   124
  "plant (x#xs) = Branch (Leaf x) (plant xs)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   125
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   126
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   127
  mirror :: "'a tree \<Rightarrow> 'a tree"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   128
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   129
  "mirror (Twig) = Twig "
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   130
  "mirror (Leaf a) = Leaf a "
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   131
  "mirror (Branch l r) = Branch (mirror r) (mirror l)"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   132
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   133
theorem "plant (rev (leaves xt)) = mirror xt"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   134
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   135
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   136
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   137
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   138
theorem "plant((leaves xt) @ (leaves yt)) = Branch xt yt"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   139
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   140
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   141
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   142
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   143
datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
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
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   146
  inOrder :: "'a ntree \<Rightarrow> 'a list"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   147
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   148
  "inOrder (Tip a)= [a]"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   149
  "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
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
consts
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   152
  root :: "'a ntree \<Rightarrow> 'a"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   153
primrec
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   154
  "root (Tip a) = a"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   155
  "root (Node f x y) = f"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   156
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   157
theorem "hd(inOrder xt) = root xt"
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   158
  quickcheck
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   159
    --{* Wrong! *} 
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   160
  oops
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   161
dd1a2905ea73 Added theory with examples for quickcheck command.
berghofe
parents:
diff changeset
   162
end