src/HOL/Tools/quickcheck_generators.ML
author haftmann
Tue May 26 17:29:32 2009 +0200 (2009-05-26)
changeset 31260 4d273d043d59
child 31485 259a3c90016e
permissions -rw-r--r--
separate module for quickcheck generators
haftmann@31260
     1
(* Author: Florian Haftmann, TU Muenchen
haftmann@31260
     2
haftmann@31260
     3
Quickcheck generators for various types.
haftmann@31260
     4
*)
haftmann@31260
     5
haftmann@31260
     6
signature QUICKCHECK_GENERATORS =
haftmann@31260
     7
sig
haftmann@31260
     8
  val compile_generator_expr: theory -> term -> int -> term list option
haftmann@31260
     9
  type seed = Random_Engine.seed
haftmann@31260
    10
  val random_fun: typ -> typ -> ('a -> 'a -> bool) -> ('a -> term)
haftmann@31260
    11
    -> (seed -> ('b * (unit -> term)) * seed) -> (seed -> seed * seed)
haftmann@31260
    12
    -> seed -> (('a -> 'b) * (unit -> Term.term)) * seed
haftmann@31260
    13
  val ensure_random_typecopy: string -> theory -> theory
haftmann@31260
    14
  val eval_ref: (unit -> int -> int * int -> term list option * (int * int)) option ref
haftmann@31260
    15
  val setup: theory -> theory
haftmann@31260
    16
end;
haftmann@31260
    17
haftmann@31260
    18
structure Quickcheck_Generators : QUICKCHECK_GENERATORS =
haftmann@31260
    19
struct
haftmann@31260
    20
haftmann@31260
    21
(** building and compiling generator expressions **)
haftmann@31260
    22
haftmann@31260
    23
val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
haftmann@31260
    24
haftmann@31260
    25
val target = "Quickcheck";
haftmann@31260
    26
haftmann@31260
    27
fun mk_generator_expr thy prop tys =
haftmann@31260
    28
  let
haftmann@31260
    29
    val bound_max = length tys - 1;
haftmann@31260
    30
    val bounds = map_index (fn (i, ty) =>
haftmann@31260
    31
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
haftmann@31260
    32
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
haftmann@31260
    33
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
haftmann@31260
    34
    val check = @{term "If :: bool => term list option => term list option => term list option"}
haftmann@31260
    35
      $ result $ @{term "None :: term list option"} $ (@{term "Some :: term list => term list option "} $ terms);
haftmann@31260
    36
    val return = @{term "Pair :: term list option => Random.seed => term list option * Random.seed"};
haftmann@31260
    37
    fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
haftmann@31260
    38
    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit => term"});
haftmann@31260
    39
    fun mk_scomp T1 T2 sT f g = Const (@{const_name scomp},
haftmann@31260
    40
      liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
haftmann@31260
    41
    fun mk_split ty = Sign.mk_const thy
haftmann@31260
    42
      (@{const_name split}, [ty, @{typ "unit => term"}, liftT @{typ "term list option"} @{typ Random.seed}]);
haftmann@31260
    43
    fun mk_scomp_split ty t t' =
haftmann@31260
    44
      mk_scomp (mk_termtyp ty) @{typ "term list option"} @{typ Random.seed} t
haftmann@31260
    45
        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit => term"}, t')));
haftmann@31260
    46
    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
haftmann@31260
    47
      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i);
haftmann@31260
    48
  in Abs ("n", @{typ code_numeral}, fold_rev mk_bindclause bounds (return $ check)) end;
haftmann@31260
    49
haftmann@31260
    50
fun compile_generator_expr thy t =
haftmann@31260
    51
  let
haftmann@31260
    52
    val tys = (map snd o fst o strip_abs) t;
haftmann@31260
    53
    val t' = mk_generator_expr thy t tys;
haftmann@31260
    54
    val f = Code_ML.eval (SOME target) ("Quickcheck_Generators.eval_ref", eval_ref)
haftmann@31260
    55
      (fn proc => fn g => fn s => g s #>> (Option.map o map) proc) thy t' [];
haftmann@31260
    56
  in f #> Random_Engine.run end;
haftmann@31260
    57
haftmann@31260
    58
haftmann@31260
    59
(** typ "'a => 'b" **)
haftmann@31260
    60
haftmann@31260
    61
type seed = Random_Engine.seed;
haftmann@31260
    62
haftmann@31260
    63
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
haftmann@31260
    64
    (random : seed -> ('b * (unit -> term)) * seed)
haftmann@31260
    65
    (random_split : seed -> seed * seed)
haftmann@31260
    66
    (seed : seed) =
haftmann@31260
    67
  let
haftmann@31260
    68
    val (seed', seed'') = random_split seed;
haftmann@31260
    69
    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
haftmann@31260
    70
    val fun_upd = Const (@{const_name fun_upd},
haftmann@31260
    71
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
haftmann@31260
    72
    fun random_fun' x =
haftmann@31260
    73
      let
haftmann@31260
    74
        val (seed, fun_map, f_t) = ! state;
haftmann@31260
    75
      in case AList.lookup (uncurry eq) fun_map x
haftmann@31260
    76
       of SOME y => y
haftmann@31260
    77
        | NONE => let
haftmann@31260
    78
              val t1 = term_of x;
haftmann@31260
    79
              val ((y, t2), seed') = random seed;
haftmann@31260
    80
              val fun_map' = (x, y) :: fun_map;
haftmann@31260
    81
              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
haftmann@31260
    82
              val _ = state := (seed', fun_map', f_t');
haftmann@31260
    83
            in y end
haftmann@31260
    84
      end;
haftmann@31260
    85
    fun term_fun' () = #3 (! state);
haftmann@31260
    86
  in ((random_fun', term_fun'), seed'') end;
haftmann@31260
    87
haftmann@31260
    88
haftmann@31260
    89
(** type copies **)
haftmann@31260
    90
haftmann@31260
    91
fun mk_random_typecopy tyco vs constr typ thy =
haftmann@31260
    92
  let
haftmann@31260
    93
    val Ts = map TFree vs;  
haftmann@31260
    94
    val T = Type (tyco, Ts);
haftmann@31260
    95
    fun mk_termifyT T = HOLogic.mk_prodT (T, @{typ "unit => term"})
haftmann@31260
    96
    val Ttm = mk_termifyT T;
haftmann@31260
    97
    val typtm = mk_termifyT typ;
haftmann@31260
    98
    fun mk_const c Ts = Const (c, Sign.const_instance thy (c, Ts));
haftmann@31260
    99
    fun mk_random T = mk_const @{const_name random} [T];
haftmann@31260
   100
    val size = @{term "j::code_numeral"};
haftmann@31260
   101
    val v = "x";
haftmann@31260
   102
    val t_v = Free (v, typtm);
haftmann@31260
   103
    val t_constr = mk_const constr Ts;
haftmann@31260
   104
    val lhs = mk_random T $ size;
haftmann@31260
   105
    val rhs = HOLogic.mk_ST [(((mk_random typ) $ size, @{typ Random.seed}), SOME (v, typtm))]
haftmann@31260
   106
      (HOLogic.mk_return Ttm @{typ Random.seed}
haftmann@31260
   107
      (mk_const "Code_Eval.valapp" [typ, T]
haftmann@31260
   108
        $ HOLogic.mk_prod (t_constr, Abs ("u", @{typ unit}, HOLogic.reflect_term t_constr)) $ t_v))
haftmann@31260
   109
      @{typ Random.seed} (SOME Ttm, @{typ Random.seed});
haftmann@31260
   110
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@31260
   111
  in   
haftmann@31260
   112
    thy
haftmann@31260
   113
    |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
haftmann@31260
   114
    |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@31260
   115
    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
haftmann@31260
   116
    |> snd
haftmann@31260
   117
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
haftmann@31260
   118
  end;
haftmann@31260
   119
haftmann@31260
   120
fun ensure_random_typecopy tyco thy =
haftmann@31260
   121
  let
haftmann@31260
   122
    val SOME { vs = raw_vs, constr, typ = raw_typ, ... } =
haftmann@31260
   123
      TypecopyPackage.get_info thy tyco;
haftmann@31260
   124
    val constrain = curry (Sorts.inter_sort (Sign.classes_of thy));
haftmann@31260
   125
    val typ = map_atyps (fn TFree (v, sort) =>
haftmann@31260
   126
      TFree (v, constrain sort @{sort random})) raw_typ;
haftmann@31260
   127
    val vs' = Term.add_tfreesT typ [];
haftmann@31260
   128
    val vs = map (fn (v, sort) =>
haftmann@31260
   129
      (v, the_default (constrain sort @{sort typerep}) (AList.lookup (op =) vs' v))) raw_vs;
haftmann@31260
   130
    val do_inst = Sign.of_sort thy (typ, @{sort random});
haftmann@31260
   131
  in if do_inst then mk_random_typecopy tyco vs constr typ thy else thy end;
haftmann@31260
   132
haftmann@31260
   133
haftmann@31260
   134
(** datatypes **)
haftmann@31260
   135
haftmann@31260
   136
(* still under construction *)
haftmann@31260
   137
haftmann@31260
   138
haftmann@31260
   139
(** setup **)
haftmann@31260
   140
haftmann@31260
   141
val setup = Code_Target.extend_target (target, (Code_ML.target_Eval, K I))
haftmann@31260
   142
  #> Quickcheck.add_generator ("code", compile_generator_expr o ProofContext.theory_of)
haftmann@31260
   143
  #> TypecopyPackage.interpretation ensure_random_typecopy;
haftmann@31260
   144
haftmann@31260
   145
end;