author  haftmann 
Fri, 17 Apr 2009 14:29:54 +0200  
changeset 30945  0418e9bffbba 
parent 29823  0ab754d13ccd 
child 30970  3fe2e418a071 
permissions  rwrr 
29132  1 
(* Author: Florian Haftmann, TU Muenchen *) 
26265  2 

3 
header {* A simple counterexample generator *} 

4 

5 
theory Quickcheck 

29132  6 
imports Random Code_Eval Map 
26265  7 
begin 
8 

9 
subsection {* The @{text random} class *} 

10 

28335  11 
class random = typerep + 
26325  12 
fixes random :: "index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed" 
26265  13 

26267
ba710daf77a7
added combinator for interpretation of construction of datatype
haftmann
parents:
26265
diff
changeset

14 
text {* Type @{typ "'a itself"} *} 
26265  15 

28335  16 
instantiation itself :: ("{type, typerep}") random 
26265  17 
begin 
18 

19 
definition 

29823
0ab754d13ccd
session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents:
29808
diff
changeset

20 
"random _ = Pair (TYPE('a), \<lambda>u. Code_Eval.Const (STR ''TYPE'') TYPEREP('a))" 
26265  21 

22 
instance .. 

23 

24 
end 

25 

26267
ba710daf77a7
added combinator for interpretation of construction of datatype
haftmann
parents:
26265
diff
changeset

26 

29808
b8b9d529663b
split of already properly working part of Quickcheck infrastructure
haftmann
parents:
29579
diff
changeset

27 
subsection {* Quickcheck generator *} 
29132  28 

29 
ML {* 

30 
structure StateMonad = 

31 
struct 

32 

33 
fun liftT T sT = sT > HOLogic.mk_prodT (T, sT); 

34 
fun liftT' sT = sT > sT; 

35 

29808
b8b9d529663b
split of already properly working part of Quickcheck infrastructure
haftmann
parents:
29579
diff
changeset

36 
fun return T sT x = Const (@{const_name Pair}, T > liftT T sT) $ x; 
29132  37 

38 
fun scomp T1 T2 sT f g = Const (@{const_name scomp}, 

39 
liftT T1 sT > (T1 > liftT T2 sT) > liftT T2 sT) $ f $ g; 

40 

41 
end; 

42 

26265  43 
structure Quickcheck = 
44 
struct 

45 

28309  46 
open Quickcheck; 
47 

26265  48 
val eval_ref : (unit > int > int * int > term list option * (int * int)) option ref = ref NONE; 
49 

30945  50 
val target = "Quickcheck"; 
51 

26325  52 
fun mk_generator_expr thy prop tys = 
26265  53 
let 
26325  54 
val bound_max = length tys  1; 
55 
val bounds = map_index (fn (i, ty) => 

56 
(2 * (bound_max  i) + 1, 2 * (bound_max  i), 2 * i, ty)) tys; 

57 
val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds); 

58 
val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds); 

26265  59 
val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"} 
26325  60 
$ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms); 
26265  61 
val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"}; 
26325  62 
fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"}); 
63 
fun mk_split ty = Sign.mk_const thy 

64 
(@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT @{typ "term list option"} @{typ seed}]); 

26589  65 
fun mk_scomp_split ty t t' = 
66 
StateMonad.scomp (mk_termtyp ty) @{typ "term list option"} @{typ seed} t (*FIXME*) 

26325  67 
(mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t'))); 
26589  68 
fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty 
26325  69 
(Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i) 
26265  70 
val t = fold_rev mk_bindclause bounds (return $ check); 
71 
in Abs ("n", @{typ index}, t) end; 

72 

28309  73 
fun compile_generator_expr thy t = 
26265  74 
let 
28309  75 
val tys = (map snd o fst o strip_abs) t; 
76 
val t' = mk_generator_expr thy t tys; 

30945  77 
val f = Code_ML.eval (SOME target) ("Quickcheck.eval_ref", eval_ref) thy t' []; 
28309  78 
in f #> Random_Engine.run #> (Option.map o map) (Code.postprocess_term thy) end; 
26265  79 

80 
end 

81 
*} 

82 

28309  83 
setup {* 
30945  84 
Code_Target.extend_target (Quickcheck.target, (Code_ML.target_Eval, K I)) 
85 
#> Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of) 

28309  86 
*} 
87 

30945  88 

89 
subsection {* Type @{typ "'a \<Rightarrow> 'b"} *} 

90 

91 
ML {* 

92 
structure Random_Engine = 

93 
struct 

94 

95 
open Random_Engine; 

96 

97 
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a > 'a > bool) (term_of : 'a > term) 

98 
(random : Random_Engine.seed > ('b * (unit > term)) * Random_Engine.seed) 

99 
(random_split : Random_Engine.seed > Random_Engine.seed * Random_Engine.seed) 

100 
(seed : Random_Engine.seed) = 

101 
let 

102 
val (seed', seed'') = random_split seed; 

103 
val state = ref (seed', [], Const (@{const_name undefined}, T1 > T2)); 

104 
val fun_upd = Const (@{const_name fun_upd}, 

105 
(T1 > T2) > T1 > T2 > T1 > T2); 

106 
fun random_fun' x = 

107 
let 

108 
val (seed, fun_map, f_t) = ! state; 

109 
in case AList.lookup (uncurry eq) fun_map x 

110 
of SOME y => y 

111 
 NONE => let 

112 
val t1 = term_of x; 

113 
val ((y, t2), seed') = random seed; 

114 
val fun_map' = (x, y) :: fun_map; 

115 
val f_t' = fun_upd $ f_t $ t1 $ t2 (); 

116 
val _ = state := (seed', fun_map', f_t'); 

117 
in y end 

118 
end; 

119 
fun term_fun' () = #3 (! state); 

120 
in ((random_fun', term_fun'), seed'') end; 

121 

26265  122 
end 
30945  123 
*} 
124 

125 
axiomatization 

126 
random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term) 

127 
\<Rightarrow> (seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> seed) \<Rightarrow> (seed \<Rightarrow> seed \<times> seed) 

128 
\<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" 

129 

130 
code_const random_fun_aux (Quickcheck "Random'_Engine.random'_fun") 

131 
 {* With enough criminal energy this can be abused to derive @{prop False}; 

132 
for this reason we use a distinguished target @{text Quickcheck} 

133 
not spoiling the regular trusted code generation *} 

134 

135 
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random 

136 
begin 

137 

138 
definition random_fun :: "index \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" where 

139 
"random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) split_seed" 

140 

141 
instance .. 

142 

143 
end 

144 

145 
code_reserved Quickcheck Random_Engine 

146 

147 
end 