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