src/HOL/Quickcheck.thy
changeset 31260 4d273d043d59
parent 31245 f8dcca332d4e
child 31267 4a85a4afc97d
     1.1 --- a/src/HOL/Quickcheck.thy	Tue May 26 13:40:50 2009 +0200
     1.2 +++ b/src/HOL/Quickcheck.thy	Tue May 26 17:29:32 2009 +0200
     1.3 @@ -4,6 +4,7 @@
     1.4  
     1.5  theory Quickcheck
     1.6  imports Random Code_Eval
     1.7 +uses ("Tools/quickcheck_generators.ML")
     1.8  begin
     1.9  
    1.10  notation fcomp (infixl "o>" 60)
    1.11 @@ -16,59 +17,7 @@
    1.12    fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
    1.13  
    1.14  
    1.15 -subsection {* Quickcheck generator *}
    1.16 -
    1.17 -ML {*
    1.18 -structure Quickcheck =
    1.19 -struct
    1.20 -
    1.21 -open Quickcheck;
    1.22 -
    1.23 -val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
    1.24 -
    1.25 -val target = "Quickcheck";
    1.26 -
    1.27 -fun mk_generator_expr thy prop tys =
    1.28 -  let
    1.29 -    val bound_max = length tys - 1;
    1.30 -    val bounds = map_index (fn (i, ty) =>
    1.31 -      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
    1.32 -    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
    1.33 -    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
    1.34 -    val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
    1.35 -      $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
    1.36 -    val return = @{term "Pair \<Colon> term list option \<Rightarrow> Random.seed \<Rightarrow> term list option \<times> Random.seed"};
    1.37 -    fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
    1.38 -    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
    1.39 -    fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
    1.40 -      liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
    1.41 -    fun mk_split ty = Sign.mk_const thy
    1.42 -      (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
    1.43 -    fun mk_scomp_split ty t t' =
    1.44 -      mk_scomp (mk_termtyp ty) @{typ "term list option"} @{typ Random.seed} t
    1.45 -        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
    1.46 -    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
    1.47 -      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i);
    1.48 -  in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
    1.49 -
    1.50 -fun compile_generator_expr thy t =
    1.51 -  let
    1.52 -    val tys = (map snd o fst o strip_abs) t;
    1.53 -    val t' = mk_generator_expr thy t tys;
    1.54 -    val f = Code_ML.eval (SOME target) ("Quickcheck.eval_ref", eval_ref)
    1.55 -      (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' [];
    1.56 -  in f #> Random_Engine.run end;
    1.57 -
    1.58 -end
    1.59 -*}
    1.60 -
    1.61 -setup {*
    1.62 -  Code_Target.extend_target (Quickcheck.target, (Code_ML.target_Eval, K I))
    1.63 -  #> Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
    1.64 -*}
    1.65 -
    1.66 -
    1.67 -subsection {* Fundamental types*}
    1.68 +subsection {* Fundamental and numeric types*}
    1.69  
    1.70  instantiation bool :: random
    1.71  begin
    1.72 @@ -91,66 +40,6 @@
    1.73  
    1.74  end
    1.75  
    1.76 -text {* Type @{typ "'a \<Rightarrow> 'b"} *}
    1.77 -
    1.78 -ML {*
    1.79 -structure Random_Engine =
    1.80 -struct
    1.81 -
    1.82 -open Random_Engine;
    1.83 -
    1.84 -fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
    1.85 -    (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
    1.86 -    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
    1.87 -    (seed : Random_Engine.seed) =
    1.88 -  let
    1.89 -    val (seed', seed'') = random_split seed;
    1.90 -    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
    1.91 -    val fun_upd = Const (@{const_name fun_upd},
    1.92 -      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
    1.93 -    fun random_fun' x =
    1.94 -      let
    1.95 -        val (seed, fun_map, f_t) = ! state;
    1.96 -      in case AList.lookup (uncurry eq) fun_map x
    1.97 -       of SOME y => y
    1.98 -        | NONE => let
    1.99 -              val t1 = term_of x;
   1.100 -              val ((y, t2), seed') = random seed;
   1.101 -              val fun_map' = (x, y) :: fun_map;
   1.102 -              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
   1.103 -              val _ = state := (seed', fun_map', f_t');
   1.104 -            in y end
   1.105 -      end;
   1.106 -    fun term_fun' () = #3 (! state);
   1.107 -  in ((random_fun', term_fun'), seed'') end;
   1.108 -
   1.109 -end
   1.110 -*}
   1.111 -
   1.112 -axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
   1.113 -  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
   1.114 -  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   1.115 -
   1.116 -code_const random_fun_aux (Quickcheck "Random'_Engine.random'_fun")
   1.117 -  -- {* With enough criminal energy this can be abused to derive @{prop False};
   1.118 -  for this reason we use a distinguished target @{text Quickcheck}
   1.119 -  not spoiling the regular trusted code generation *}
   1.120 -
   1.121 -instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
   1.122 -begin
   1.123 -
   1.124 -definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
   1.125 -  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) Random.split_seed"
   1.126 -
   1.127 -instance ..
   1.128 -
   1.129 -end
   1.130 -
   1.131 -code_reserved Quickcheck Random_Engine
   1.132 -
   1.133 -
   1.134 -subsection {* Numeric types *}
   1.135 -
   1.136  instantiation nat :: random
   1.137  begin
   1.138  
   1.139 @@ -175,119 +64,40 @@
   1.140  
   1.141  end
   1.142  
   1.143 -subsection {* Type copies *}
   1.144 +
   1.145 +subsection {* Complex generators *}
   1.146 +
   1.147 +definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
   1.148 +  "collapse f = (f o\<rightarrow> id)"
   1.149  
   1.150 -setup {*
   1.151 -let
   1.152 +definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
   1.153 +  "beyond k l = (if l > k then l else 0)"
   1.154 +
   1.155 +use "Tools/quickcheck_generators.ML"
   1.156 +setup {* Quickcheck_Generators.setup *}
   1.157 +
   1.158 +code_reserved Quickcheck Quickcheck_Generators
   1.159 +
   1.160 +text {* Type @{typ "'a \<Rightarrow> 'b"} *}
   1.161  
   1.162 -fun mk_random_typecopy tyco vs constr typ thy =
   1.163 -  let
   1.164 -    val Ts = map TFree vs;  
   1.165 -    val T = Type (tyco, Ts);
   1.166 -    fun mk_termifyT T = HOLogic.mk_prodT (T, @{typ "unit \<Rightarrow> term"})
   1.167 -    val Ttm = mk_termifyT T;
   1.168 -    val typtm = mk_termifyT typ;
   1.169 -    fun mk_const c Ts = Const (c, Sign.const_instance thy (c, Ts));
   1.170 -    fun mk_random T = mk_const @{const_name random} [T];
   1.171 -    val size = @{term "k\<Colon>code_numeral"};
   1.172 -    val v = "x";
   1.173 -    val t_v = Free (v, typtm);
   1.174 -    val t_constr = mk_const constr Ts;
   1.175 -    val lhs = mk_random T $ size;
   1.176 -    val rhs = HOLogic.mk_ST [(((mk_random typ) $ size, @{typ Random.seed}), SOME (v, typtm))]
   1.177 -      (HOLogic.mk_return Ttm @{typ Random.seed}
   1.178 -      (mk_const "Code_Eval.valapp" [typ, T]
   1.179 -        $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
   1.180 -      @{typ Random.seed} (SOME Ttm, @{typ Random.seed});
   1.181 -    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
   1.182 -  in   
   1.183 -    thy
   1.184 -    |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
   1.185 -    |> `(fn lthy => Syntax.check_term lthy eq)
   1.186 -    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
   1.187 -    |> snd
   1.188 -    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   1.189 -  end;
   1.190 +axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
   1.191 +  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
   1.192 +  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   1.193  
   1.194 -fun ensure_random_typecopy tyco thy =
   1.195 -  let
   1.196 -    val SOME { vs = raw_vs, constr, typ = raw_typ, ... } =
   1.197 -      TypecopyPackage.get_info thy tyco;
   1.198 -    val constrain = curry (Sorts.inter_sort (Sign.classes_of thy));
   1.199 -    val typ = map_atyps (fn TFree (v, sort) =>
   1.200 -      TFree (v, constrain sort @{sort random})) raw_typ;
   1.201 -    val vs' = Term.add_tfreesT typ [];
   1.202 -    val vs = map (fn (v, sort) =>
   1.203 -      (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
   1.204 -    val do_inst = Sign.of_sort thy (typ, @{sort random});
   1.205 -  in if do_inst then mk_random_typecopy tyco vs constr typ thy else thy end;
   1.206 +code_const random_fun_aux (Quickcheck "Quickcheck'_Generators.random'_fun")
   1.207 +  -- {* With enough criminal energy this can be abused to derive @{prop False};
   1.208 +  for this reason we use a distinguished target @{text Quickcheck}
   1.209 +  not spoiling the regular trusted code generation *}
   1.210  
   1.211 -in
   1.212 +instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
   1.213 +begin
   1.214  
   1.215 -TypecopyPackage.interpretation ensure_random_typecopy
   1.216 +definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
   1.217 +  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) Random.split_seed"
   1.218 +
   1.219 +instance ..
   1.220  
   1.221  end
   1.222 -*}
   1.223 -
   1.224 -
   1.225 -subsection {* Type copies *}
   1.226 -
   1.227 -setup {*
   1.228 -let
   1.229 -
   1.230 -fun mk_random_typecopy tyco vs constr typ thy =
   1.231 -  let
   1.232 -    val Ts = map TFree vs;
   1.233 -    val T = Type (tyco, Ts);
   1.234 -    fun mk_termifyT T = HOLogic.mk_prodT (T, @{typ "unit \<Rightarrow> term"})
   1.235 -    val Ttm = mk_termifyT T;
   1.236 -    val typtm = mk_termifyT typ;
   1.237 -    fun mk_const c Ts = Const (c, Sign.const_instance thy (c, Ts));
   1.238 -    fun mk_random T = mk_const @{const_name random} [T];
   1.239 -    val size = @{term "k\<Colon>code_numeral"};
   1.240 -    val v = "x";
   1.241 -    val t_v = Free (v, typtm);
   1.242 -    val t_constr = mk_const constr Ts;
   1.243 -    val lhs = mk_random T $ size;
   1.244 -    val rhs = HOLogic.mk_ST [(((mk_random typ) $ size, @{typ Random.seed}), SOME (v, typtm))]
   1.245 -      (HOLogic.mk_return Ttm @{typ Random.seed}
   1.246 -      (mk_const "Code_Eval.valapp" [typ, T]
   1.247 -        $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
   1.248 -      @{typ Random.seed} (SOME Ttm, @{typ Random.seed});
   1.249 -    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
   1.250 -  in
   1.251 -    thy
   1.252 -    |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
   1.253 -    |> `(fn lthy => Syntax.check_term lthy eq)
   1.254 -    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
   1.255 -    |> snd
   1.256 -    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   1.257 -  end;
   1.258 -
   1.259 -fun ensure_random_typecopy tyco thy =
   1.260 -  let
   1.261 -    val SOME { vs = raw_vs, constr, typ = raw_typ, ... } =
   1.262 -      TypecopyPackage.get_info thy tyco;
   1.263 -    val constrain = curry (Sorts.inter_sort (Sign.classes_of thy));
   1.264 -    val typ = map_atyps (fn TFree (v, sort) =>
   1.265 -      TFree (v, constrain sort @{sort random})) raw_typ;
   1.266 -    val vs' = Term.add_tfreesT typ [];
   1.267 -    val vs = map (fn (v, sort) =>
   1.268 -      (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
   1.269 -    val do_inst = Sign.of_sort thy (typ, @{sort random});
   1.270 -  in if do_inst then mk_random_typecopy tyco vs constr typ thy else thy end;
   1.271 -
   1.272 -in
   1.273 -
   1.274 -TypecopyPackage.interpretation ensure_random_typecopy
   1.275 -
   1.276 -end
   1.277 -*}
   1.278 -
   1.279 -
   1.280 -subsection {* Datatypes *}
   1.281 -
   1.282 -text {* under construction *}
   1.283  
   1.284  
   1.285  no_notation fcomp (infixl "o>" 60)