src/HOL/Library/Quickcheck.thy
author haftmann
Fri Apr 17 14:29:54 2009 +0200 (2009-04-17)
changeset 30945 0418e9bffbba
parent 29823 0ab754d13ccd
child 30970 3fe2e418a071
permissions -rw-r--r--
separate channel for Quickcheck evaluations
haftmann@29132
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@26265
     2
haftmann@26265
     3
header {* A simple counterexample generator *}
haftmann@26265
     4
haftmann@26265
     5
theory Quickcheck
haftmann@29132
     6
imports Random Code_Eval Map
haftmann@26265
     7
begin
haftmann@26265
     8
haftmann@26265
     9
subsection {* The @{text random} class *}
haftmann@26265
    10
haftmann@28335
    11
class random = typerep +
haftmann@26325
    12
  fixes random :: "index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
haftmann@26265
    13
haftmann@26267
    14
text {* Type @{typ "'a itself"} *}
haftmann@26265
    15
haftmann@28335
    16
instantiation itself :: ("{type, typerep}") random
haftmann@26265
    17
begin
haftmann@26265
    18
haftmann@26265
    19
definition
haftmann@29823
    20
  "random _ = Pair (TYPE('a), \<lambda>u. Code_Eval.Const (STR ''TYPE'') TYPEREP('a))"
haftmann@26265
    21
haftmann@26265
    22
instance ..
haftmann@26265
    23
haftmann@26265
    24
end
haftmann@26265
    25
haftmann@26267
    26
haftmann@29808
    27
subsection {* Quickcheck generator *}
haftmann@29132
    28
haftmann@29132
    29
ML {*
haftmann@29132
    30
structure StateMonad =
haftmann@29132
    31
struct
haftmann@29132
    32
haftmann@29132
    33
fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
haftmann@29132
    34
fun liftT' sT = sT --> sT;
haftmann@29132
    35
haftmann@29808
    36
fun return T sT x = Const (@{const_name Pair}, T --> liftT T sT) $ x;
haftmann@29132
    37
haftmann@29132
    38
fun scomp T1 T2 sT f g = Const (@{const_name scomp},
haftmann@29132
    39
  liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
haftmann@29132
    40
haftmann@29132
    41
end;
haftmann@29132
    42
haftmann@26265
    43
structure Quickcheck =
haftmann@26265
    44
struct
haftmann@26265
    45
haftmann@28309
    46
open Quickcheck;
haftmann@28309
    47
haftmann@26265
    48
val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
haftmann@26265
    49
haftmann@30945
    50
val target = "Quickcheck";
haftmann@30945
    51
haftmann@26325
    52
fun mk_generator_expr thy prop tys =
haftmann@26265
    53
  let
haftmann@26325
    54
    val bound_max = length tys - 1;
haftmann@26325
    55
    val bounds = map_index (fn (i, ty) =>
haftmann@26325
    56
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
haftmann@26325
    57
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
haftmann@26325
    58
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
haftmann@26265
    59
    val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
haftmann@26325
    60
      $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
haftmann@26265
    61
    val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"};
haftmann@26325
    62
    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
haftmann@26325
    63
    fun mk_split ty = Sign.mk_const thy
haftmann@26325
    64
      (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT @{typ "term list option"} @{typ seed}]);
haftmann@26589
    65
    fun mk_scomp_split ty t t' =
haftmann@26589
    66
      StateMonad.scomp (mk_termtyp ty) @{typ "term list option"} @{typ seed} t (*FIXME*)
haftmann@26325
    67
        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
haftmann@26589
    68
    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
haftmann@26325
    69
      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i)
haftmann@26265
    70
    val t = fold_rev mk_bindclause bounds (return $ check);
haftmann@26265
    71
  in Abs ("n", @{typ index}, t) end;
haftmann@26265
    72
haftmann@28309
    73
fun compile_generator_expr thy t =
haftmann@26265
    74
  let
haftmann@28309
    75
    val tys = (map snd o fst o strip_abs) t;
haftmann@28309
    76
    val t' = mk_generator_expr thy t tys;
haftmann@30945
    77
    val f = Code_ML.eval (SOME target) ("Quickcheck.eval_ref", eval_ref) thy t' [];
haftmann@28309
    78
  in f #> Random_Engine.run #> (Option.map o map) (Code.postprocess_term thy) end;
haftmann@26265
    79
haftmann@26265
    80
end
haftmann@26265
    81
*}
haftmann@26265
    82
haftmann@28309
    83
setup {*
haftmann@30945
    84
  Code_Target.extend_target (Quickcheck.target, (Code_ML.target_Eval, K I))
haftmann@30945
    85
  #> Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
haftmann@28309
    86
*}
haftmann@28309
    87
haftmann@30945
    88
haftmann@30945
    89
subsection {* Type @{typ "'a \<Rightarrow> 'b"} *}
haftmann@30945
    90
haftmann@30945
    91
ML {*
haftmann@30945
    92
structure Random_Engine =
haftmann@30945
    93
struct
haftmann@30945
    94
haftmann@30945
    95
open Random_Engine;
haftmann@30945
    96
haftmann@30945
    97
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
haftmann@30945
    98
    (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
haftmann@30945
    99
    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
haftmann@30945
   100
    (seed : Random_Engine.seed) =
haftmann@30945
   101
  let
haftmann@30945
   102
    val (seed', seed'') = random_split seed;
haftmann@30945
   103
    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
haftmann@30945
   104
    val fun_upd = Const (@{const_name fun_upd},
haftmann@30945
   105
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
haftmann@30945
   106
    fun random_fun' x =
haftmann@30945
   107
      let
haftmann@30945
   108
        val (seed, fun_map, f_t) = ! state;
haftmann@30945
   109
      in case AList.lookup (uncurry eq) fun_map x
haftmann@30945
   110
       of SOME y => y
haftmann@30945
   111
        | NONE => let
haftmann@30945
   112
              val t1 = term_of x;
haftmann@30945
   113
              val ((y, t2), seed') = random seed;
haftmann@30945
   114
              val fun_map' = (x, y) :: fun_map;
haftmann@30945
   115
              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
haftmann@30945
   116
              val _ = state := (seed', fun_map', f_t');
haftmann@30945
   117
            in y end
haftmann@30945
   118
      end;
haftmann@30945
   119
    fun term_fun' () = #3 (! state);
haftmann@30945
   120
  in ((random_fun', term_fun'), seed'') end;
haftmann@30945
   121
haftmann@26265
   122
end
haftmann@30945
   123
*}
haftmann@30945
   124
haftmann@30945
   125
axiomatization
haftmann@30945
   126
  random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
haftmann@30945
   127
    \<Rightarrow> (seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> seed) \<Rightarrow> (seed \<Rightarrow> seed \<times> seed)
haftmann@30945
   128
    \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed"
haftmann@30945
   129
haftmann@30945
   130
code_const random_fun_aux (Quickcheck "Random'_Engine.random'_fun")
haftmann@30945
   131
  -- {* With enough criminal energy this can be abused to derive @{prop False};
haftmann@30945
   132
  for this reason we use a distinguished target @{text Quickcheck}
haftmann@30945
   133
  not spoiling the regular trusted code generation *}
haftmann@30945
   134
haftmann@30945
   135
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
haftmann@30945
   136
begin
haftmann@30945
   137
haftmann@30945
   138
definition random_fun :: "index \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" where
haftmann@30945
   139
  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) split_seed"
haftmann@30945
   140
haftmann@30945
   141
instance ..
haftmann@30945
   142
haftmann@30945
   143
end
haftmann@30945
   144
haftmann@30945
   145
code_reserved Quickcheck Random_Engine
haftmann@30945
   146
haftmann@30945
   147
end