src/HOL/Tools/quickcheck_generators.ML
changeset 31260 4d273d043d59
child 31485 259a3c90016e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/quickcheck_generators.ML	Tue May 26 17:29:32 2009 +0200
     1.3 @@ -0,0 +1,145 @@
     1.4 +(* Author: Florian Haftmann, TU Muenchen
     1.5 +
     1.6 +Quickcheck generators for various types.
     1.7 +*)
     1.8 +
     1.9 +signature QUICKCHECK_GENERATORS =
    1.10 +sig
    1.11 +  val compile_generator_expr: theory -> term -> int -> term list option
    1.12 +  type seed = Random_Engine.seed
    1.13 +  val random_fun: typ -> typ -> ('a -> 'a -> bool) -> ('a -> term)
    1.14 +    -> (seed -> ('b * (unit -> term)) * seed) -> (seed -> seed * seed)
    1.15 +    -> seed -> (('a -> 'b) * (unit -> Term.term)) * seed
    1.16 +  val ensure_random_typecopy: string -> theory -> theory
    1.17 +  val eval_ref: (unit -> int -> int * int -> term list option * (int * int)) option ref
    1.18 +  val setup: theory -> theory
    1.19 +end;
    1.20 +
    1.21 +structure Quickcheck_Generators : QUICKCHECK_GENERATORS =
    1.22 +struct
    1.23 +
    1.24 +(** building and compiling generator expressions **)
    1.25 +
    1.26 +val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
    1.27 +
    1.28 +val target = "Quickcheck";
    1.29 +
    1.30 +fun mk_generator_expr thy prop tys =
    1.31 +  let
    1.32 +    val bound_max = length tys - 1;
    1.33 +    val bounds = map_index (fn (i, ty) =>
    1.34 +      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
    1.35 +    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
    1.36 +    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
    1.37 +    val check = @{term "If :: bool => term list option => term list option => term list option"}
    1.38 +      $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option "} $ terms);
    1.39 +    val return = @{term "Pair :: term list option => Random.seed => term list option * Random.seed"};
    1.40 +    fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
    1.41 +    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit => term"});
    1.42 +    fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
    1.43 +      liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
    1.44 +    fun mk_split ty = Sign.mk_const thy
    1.45 +      (@{const_name split}, [ty, @{typ "unit => term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
    1.46 +    fun mk_scomp_split ty t t' =
    1.47 +      mk_scomp (mk_termtyp ty) @{typ "term list option"} @{typ Random.seed} t
    1.48 +        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit => term"}, t')));
    1.49 +    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
    1.50 +      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i);
    1.51 +  in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
    1.52 +
    1.53 +fun compile_generator_expr thy t =
    1.54 +  let
    1.55 +    val tys = (map snd o fst o strip_abs) t;
    1.56 +    val t' = mk_generator_expr thy t tys;
    1.57 +    val f = Code_ML.eval (SOME target) ("Quickcheck_Generators.eval_ref", eval_ref)
    1.58 +      (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' [];
    1.59 +  in f #> Random_Engine.run end;
    1.60 +
    1.61 +
    1.62 +(** typ "'a => 'b" **)
    1.63 +
    1.64 +type seed = Random_Engine.seed;
    1.65 +
    1.66 +fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
    1.67 +    (random : seed -> ('b * (unit -> term)) * seed)
    1.68 +    (random_split : seed -> seed * seed)
    1.69 +    (seed : seed) =
    1.70 +  let
    1.71 +    val (seed', seed'') = random_split seed;
    1.72 +    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
    1.73 +    val fun_upd = Const (@{const_name fun_upd},
    1.74 +      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
    1.75 +    fun random_fun' x =
    1.76 +      let
    1.77 +        val (seed, fun_map, f_t) = ! state;
    1.78 +      in case AList.lookup (uncurry eq) fun_map x
    1.79 +       of SOME y => y
    1.80 +        | NONE => let
    1.81 +              val t1 = term_of x;
    1.82 +              val ((y, t2), seed') = random seed;
    1.83 +              val fun_map' = (x, y) :: fun_map;
    1.84 +              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
    1.85 +              val _ = state := (seed', fun_map', f_t');
    1.86 +            in y end
    1.87 +      end;
    1.88 +    fun term_fun' () = #3 (! state);
    1.89 +  in ((random_fun', term_fun'), seed'') end;
    1.90 +
    1.91 +
    1.92 +(** type copies **)
    1.93 +
    1.94 +fun mk_random_typecopy tyco vs constr typ thy =
    1.95 +  let
    1.96 +    val Ts = map TFree vs;  
    1.97 +    val T = Type (tyco, Ts);
    1.98 +    fun mk_termifyT T = HOLogic.mk_prodT (T, @{typ "unit => term"})
    1.99 +    val Ttm = mk_termifyT T;
   1.100 +    val typtm = mk_termifyT typ;
   1.101 +    fun mk_const c Ts = Const (c, Sign.const_instance thy (c, Ts));
   1.102 +    fun mk_random T = mk_const @{const_name random} [T];
   1.103 +    val size = @{term "j::code_numeral"};
   1.104 +    val v = "x";
   1.105 +    val t_v = Free (v, typtm);
   1.106 +    val t_constr = mk_const constr Ts;
   1.107 +    val lhs = mk_random T $ size;
   1.108 +    val rhs = HOLogic.mk_ST [(((mk_random typ) $ size, @{typ Random.seed}), SOME (v, typtm))]
   1.109 +      (HOLogic.mk_return Ttm @{typ Random.seed}
   1.110 +      (mk_const "Code_Eval.valapp" [typ, T]
   1.111 +        $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
   1.112 +      @{typ Random.seed} (SOME Ttm, @{typ Random.seed});
   1.113 +    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
   1.114 +  in   
   1.115 +    thy
   1.116 +    |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
   1.117 +    |> `(fn lthy => Syntax.check_term lthy eq)
   1.118 +    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
   1.119 +    |> snd
   1.120 +    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
   1.121 +  end;
   1.122 +
   1.123 +fun ensure_random_typecopy tyco thy =
   1.124 +  let
   1.125 +    val SOME { vs = raw_vs, constr, typ = raw_typ, ... } =
   1.126 +      TypecopyPackage.get_info thy tyco;
   1.127 +    val constrain = curry (Sorts.inter_sort (Sign.classes_of thy));
   1.128 +    val typ = map_atyps (fn TFree (v, sort) =>
   1.129 +      TFree (v, constrain sort @{sort random})) raw_typ;
   1.130 +    val vs' = Term.add_tfreesT typ [];
   1.131 +    val vs = map (fn (v, sort) =>
   1.132 +      (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
   1.133 +    val do_inst = Sign.of_sort thy (typ, @{sort random});
   1.134 +  in if do_inst then mk_random_typecopy tyco vs constr typ thy else thy end;
   1.135 +
   1.136 +
   1.137 +(** datatypes **)
   1.138 +
   1.139 +(* still under construction *)
   1.140 +
   1.141 +
   1.142 +(** setup **)
   1.143 +
   1.144 +val setup = Code_Target.extend_target (target, (Code_ML.target_Eval, K I))
   1.145 +  #> Quickcheck.add_generator ("code", compile_generator_expr o ProofContext.theory_of)
   1.146 +  #> TypecopyPackage.interpretation ensure_random_typecopy;
   1.147 +
   1.148 +end;