author  bulwahn 
Fri, 11 Mar 2011 15:21:13 +0100  
changeset 41930  1e008cc4883a 
parent 41929  c3c8b14f480a 
child 41943  12f24ad566ea 
permissions  rwrr 
41905  1 
(* Author: Lukas Bulwahn, TU Muenchen *) 
2 

41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41929
diff
changeset

3 
header {* Counterexample generator preforming narrowingbased testing *} 
41905  4 

41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41929
diff
changeset

5 
theory Quickcheck_Narrowing 
41905  6 
imports Main "~~/src/HOL/Library/Code_Char" 
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41929
diff
changeset

7 
uses ("~~/src/HOL/Tools/Quickcheck/narrowing_generators.ML") 
41905  8 
begin 
9 

10 
subsection {* Counterexample generator *} 

11 

41909
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

12 
subsubsection {* Code generation setup *} 
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

13 

383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

14 
code_type typerep 
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

15 
("Haskell" "Typerep") 
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

16 

383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

17 
code_const Typerep.Typerep 
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

18 
("Haskell" "Typerep") 
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

19 

383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

20 
code_reserved Haskell Typerep 
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset

21 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

22 
subsubsection {* Type code_int for Haskell's Int type *} 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

23 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

24 
typedef (open) code_int = "UNIV \<Colon> int set" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

25 
morphisms int_of of_int by rule 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

26 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

27 
lemma int_of_inject [simp]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

28 
"int_of k = int_of l \<longleftrightarrow> k = l" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

29 
by (rule int_of_inject) 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

30 

41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

31 
definition nat_of :: "code_int => nat" 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

32 
where 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

33 
"nat_of i = nat (int_of i)" 
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

34 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

35 
instantiation code_int :: "{zero, one, minus, linorder}" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

36 
begin 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

37 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

38 
definition [simp, code del]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

39 
"0 = of_int 0" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

40 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

41 
definition [simp, code del]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

42 
"1 = of_int 1" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

43 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

44 
definition [simp, code del]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

45 
"n  m = of_int (int_of n  int_of m)" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

46 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

47 
definition [simp, code del]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

48 
"n \<le> m \<longleftrightarrow> int_of n \<le> int_of m" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

49 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

50 
definition [simp, code del]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

51 
"n < m \<longleftrightarrow> int_of n < int_of m" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

52 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

53 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

54 
instance proof qed (auto) 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

55 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

56 
end 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

57 
(* 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

58 
lemma zero_code_int_code [code, code_unfold]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

59 
"(0\<Colon>code_int) = Numeral0" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

60 
by (simp add: number_of_code_numeral_def Pls_def) 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

61 
lemma [code_post]: "Numeral0 = (0\<Colon>code_numeral)" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

62 
using zero_code_numeral_code .. 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

63 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

64 
lemma one_code_numeral_code [code, code_unfold]: 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

65 
"(1\<Colon>code_int) = Numeral1" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

66 
by (simp add: number_of_code_numeral_def Pls_def Bit1_def) 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

67 
lemma [code_post]: "Numeral1 = (1\<Colon>code_int)" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

68 
using one_code_numeral_code .. 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

69 
*) 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

70 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

71 
code_const "0 \<Colon> code_int" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

72 
(Haskell "0") 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

73 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

74 
code_const "1 \<Colon> code_int" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

75 
(Haskell "1") 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

76 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

77 
code_const "minus \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> code_int" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

78 
(Haskell "(_/ / _)") 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

79 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

80 
code_const "op \<le> \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

81 
(Haskell infix 4 "<=") 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

82 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

83 
code_const "op < \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

84 
(Haskell infix 4 "<") 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

85 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

86 
code_type code_int 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

87 
(Haskell "Int") 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

88 

41905  89 
subsubsection {* LSC's deep representation of types of terms *} 
90 

91 
datatype type = SumOfProd "type list list" 

92 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

93 
datatype "term" = Var "code_int list" type  Ctr code_int "term list" 
41905  94 

95 
datatype 'a cons = C type "(term list => 'a) list" 

96 

97 
subsubsection {* auxilary functions for LSC *} 

98 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

99 
consts nth :: "'a list => code_int => 'a" 
41905  100 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

101 
code_const nth ("Haskell" infixl 9 "!!") 
41905  102 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

103 
consts error :: "char list => 'a" 
41905  104 

105 
code_const error ("Haskell" "error") 

106 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

107 
consts toEnum :: "code_int => char" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

108 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

109 
code_const toEnum ("Haskell" "toEnum") 
41905  110 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

111 
consts map_index :: "(code_int * 'a => 'b) => 'a list => 'b list" 
41905  112 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

113 
consts split_At :: "code_int => 'a list => 'a list * 'a list" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

114 

41905  115 
subsubsection {* LSC's basic operations *} 
116 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

117 
type_synonym 'a series = "code_int => 'a cons" 
41905  118 

119 
definition empty :: "'a series" 

120 
where 

121 
"empty d = C (SumOfProd []) []" 

122 

123 
definition cons :: "'a => 'a series" 

124 
where 

125 
"cons a d = (C (SumOfProd [[]]) [(%_. a)])" 

126 

127 
fun conv :: "(term list => 'a) list => term => 'a" 

128 
where 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

129 
"conv cs (Var p _) = error (Char Nibble0 Nibble0 # map toEnum p)" 
41905  130 
 "conv cs (Ctr i xs) = (nth cs i) xs" 
131 

132 
fun nonEmpty :: "type => bool" 

133 
where 

134 
"nonEmpty (SumOfProd ps) = (\<not> (List.null ps))" 

135 

136 
definition "apply" :: "('a => 'b) series => 'a series => 'b series" 

137 
where 

138 
"apply f a d = 

139 
(case f d of C (SumOfProd ps) cfs => 

140 
case a (d  1) of C ta cas => 

141 
let 

142 
shallow = (d > 0 \<and> nonEmpty ta); 

143 
cs = [(%xs'. (case xs' of [] => undefined  x # xs => cf xs (conv cas x))). shallow, cf < cfs] 

144 
in C (SumOfProd [ta # p. shallow, p < ps]) cs)" 

145 

146 
definition sum :: "'a series => 'a series => 'a series" 

147 
where 

148 
"sum a b d = 

149 
(case a d of C (SumOfProd ssa) ca => 

150 
case b d of C (SumOfProd ssb) cb => 

151 
C (SumOfProd (ssa @ ssb)) (ca @ cb))" 

152 

41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

153 
lemma [fundef_cong]: 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

154 
assumes "a d = a' d" "b d = b' d" "d = d'" 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

155 
shows "sum a b d = sum a' b' d'" 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

156 
using assms unfolding sum_def by (auto split: cons.split type.split) 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

157 

1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

158 
lemma [fundef_cong]: 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

159 
assumes "f d = f' d" "(\<And>d'. 0 <= d' & d' < d ==> a d' = a' d')" 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

160 
assumes "d = d'" 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

161 
shows "apply f a d = apply f' a' d'" 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

162 
proof  
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

163 
note assms moreover 
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41929
diff
changeset

164 
have "int_of (of_int 0) < int_of d' ==> int_of (of_int 0) <= int_of (of_int (int_of d'  int_of (of_int 1)))" 
41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

165 
by (simp add: of_int_inverse) 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

166 
moreover 
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41929
diff
changeset

167 
have "int_of (of_int (int_of d'  int_of (of_int 1))) < int_of d'" 
41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

168 
by (simp add: of_int_inverse) 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

169 
ultimately show ?thesis 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

170 
unfolding apply_def by (auto split: cons.split type.split simp add: Let_def) 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

171 
qed 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

172 

41905  173 
definition cons0 :: "'a => 'a series" 
174 
where 

175 
"cons0 f = cons f" 

176 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

177 
type_synonym pos = "code_int list" 
41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

178 
(* 
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

179 
subsubsection {* Term refinement *} 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

180 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

181 
definition new :: "pos => type list list => term list" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

182 
where 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

183 
"new p ps = map_index (%(c, ts). Ctr c (map_index (%(i, t). Var (p @ [i]) t) ts)) ps" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

184 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

185 
fun refine :: "term => pos => term list" and refineList :: "term list => pos => (term list) list" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

186 
where 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

187 
"refine (Var p (SumOfProd ss)) [] = new p ss" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

188 
 "refine (Ctr c xs) p = map (Ctr c) (refineList xs p)" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

189 
 "refineList xs (i # is) = (let (ls, xrs) = split_At i xs in (case xrs of x#rs => [ls @ y # rs. y < refine x is]))" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

190 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

191 
text {* Find total instantiations of a partial value *} 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

192 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

193 
function total :: "term => term list" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

194 
where 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

195 
"total (Ctr c xs) = [Ctr c ys. ys < map total xs]" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

196 
 "total (Var p (SumOfProd ss)) = [y. x < new p ss, y < total x]" 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

197 
by pat_completeness auto 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

198 

3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

199 
termination sorry 
41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

200 
*) 
41905  201 
subsubsection {* LSC's type class for enumeration *} 
202 

203 
class serial = 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

204 
fixes series :: "code_int => 'a cons" 
41905  205 

206 
definition cons1 :: "('a::serial => 'b) => 'b series" 

207 
where 

208 
"cons1 f = apply (cons f) series" 

209 

210 
definition cons2 :: "('a :: serial => 'b :: serial => 'c) => 'c series" 

211 
where 

212 
"cons2 f = apply (apply (cons f) series) series" 

213 

214 
instantiation unit :: serial 

215 
begin 

216 

217 
definition 

218 
"series = cons0 ()" 

219 

220 
instance .. 

221 

222 
end 

223 

224 
instantiation bool :: serial 

225 
begin 

226 

227 
definition 

228 
"series = sum (cons0 True) (cons0 False)" 

229 

230 
instance .. 

231 

232 
end 

233 

234 
instantiation option :: (serial) serial 

235 
begin 

236 

237 
definition 

238 
"series = sum (cons0 None) (cons1 Some)" 

239 

240 
instance .. 

241 

242 
end 

243 

244 
instantiation sum :: (serial, serial) serial 

245 
begin 

246 

247 
definition 

248 
"series = sum (cons1 Inl) (cons1 Inr)" 

249 

250 
instance .. 

251 

252 
end 

253 

254 
instantiation list :: (serial) serial 

255 
begin 

256 

257 
function series_list :: "'a list series" 

258 
where 

259 
"series_list d = sum (cons []) (apply (apply (cons Cons) series) series_list) d" 

260 
by pat_completeness auto 

261 

41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

262 
termination proof (relation "measure nat_of") 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

263 
qed (auto simp add: of_int_inverse nat_of_def) 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

264 

41905  265 
instance .. 
266 

267 
end 

268 

269 
instantiation nat :: serial 

270 
begin 

271 

272 
function series_nat :: "nat series" 

273 
where 

274 
"series_nat d = sum (cons 0) (apply (cons Suc) series_nat) d" 

275 
by pat_completeness auto 

276 

41912
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

277 
termination proof (relation "measure nat_of") 
1848775589e5
adding termination proofs to series functions in LSC; commenting out momentarily unused term refinement functions in LSC
bulwahn
parents:
41910
diff
changeset

278 
qed (auto simp add: of_int_inverse nat_of_def) 
41905  279 

280 
instance .. 

281 

282 
end 

283 

284 
instantiation Enum.finite_1 :: serial 

285 
begin 

286 

287 
definition series_finite_1 :: "Enum.finite_1 series" 

288 
where 

289 
"series_finite_1 = cons (Enum.finite_1.a\<^isub>1 :: Enum.finite_1)" 

290 

291 
instance .. 

292 

293 
end 

294 

295 
instantiation Enum.finite_2 :: serial 

296 
begin 

297 

298 
definition series_finite_2 :: "Enum.finite_2 series" 

299 
where 

300 
"series_finite_2 = sum (cons (Enum.finite_2.a\<^isub>1 :: Enum.finite_2)) (cons (Enum.finite_2.a\<^isub>2 :: Enum.finite_2))" 

301 

302 
instance .. 

303 

304 
end 

305 

306 
instantiation Enum.finite_3 :: serial 

307 
begin 

308 

309 
definition series_finite_3 :: "Enum.finite_3 series" 

310 
where 

311 
"series_finite_3 = sum (cons (Enum.finite_3.a\<^isub>1 :: Enum.finite_3)) (sum (cons (Enum.finite_3.a\<^isub>2 :: Enum.finite_3)) (cons (Enum.finite_3.a\<^isub>3 :: Enum.finite_3)))" 

312 

313 
instance .. 

314 

315 
end 

316 

41910
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

317 
instantiation Enum.finite_4 :: serial 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

318 
begin 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

319 

709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

320 
definition series_finite_4 :: "Enum.finite_4 series" 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

321 
where 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

322 
"series_finite_4 = sum (cons Enum.finite_4.a\<^isub>1) (sum (cons Enum.finite_4.a\<^isub>2) (sum (cons Enum.finite_4.a\<^isub>3) (cons Enum.finite_4.a\<^isub>4)))" 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

323 

709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

324 
instance .. 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

325 

709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

326 
end 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

327 

41905  328 
subsubsection {* class is_testable *} 
329 

330 
text {* The class is_testable ensures that all necessary type instances are generated. *} 

331 

332 
class is_testable 

333 

334 
instance bool :: is_testable .. 

335 

336 
instance "fun" :: ("{term_of, serial}", is_testable) is_testable .. 

337 

338 
definition ensure_testable :: "'a :: is_testable => 'a :: is_testable" 

339 
where 

340 
"ensure_testable f = f" 

341 

41910
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

342 
declare simp_thms(17,19)[code del] 
709c04e7b703
adding serial instance of finite_4 in lazysmallcheck; changing code equations for implies
bulwahn
parents:
41909
diff
changeset

343 

41905  344 
subsubsection {* Setting up the counterexample generator *} 
345 

41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41929
diff
changeset

346 
use "~~/src/HOL/Tools/Quickcheck/narrowing_generators.ML" 
41905  347 

41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41929
diff
changeset

348 
setup {* Narrowing_Generators.setup *} 
41905  349 

41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

350 
hide_const (open) empty 
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset

351 

41905  352 
end 