src/HOL/ex/Quickcheck.thy
author haftmann
Wed, 17 Dec 2008 12:10:38 +0100
changeset 29132 3dac98ebae24
parent 28965 1de908189869
child 29579 cb520b766e00
permissions -rw-r--r--
restructured; circumvent sort problem
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     2
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     3
header {* A simple counterexample generator *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     4
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     5
theory Quickcheck
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
     6
imports Random Code_Eval Map
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     7
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     8
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     9
subsection {* The @{text random} class *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    10
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    11
class random = typerep +
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    12
  fixes random :: "index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    13
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    14
text {* Type @{typ "'a itself"} *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    15
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    16
instantiation itself :: ("{type, typerep}") random
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    17
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    18
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    19
definition
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    20
  "random _ = return (TYPE('a), \<lambda>u. Code_Eval.Const (STR ''TYPE'') TYPEREP('a))"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    21
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    22
instance ..
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    23
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    24
end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    25
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    26
text {* Type @{typ "'a \<Rightarrow> 'b"} *}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    27
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    28
ML {*
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    29
structure Random_Engine =
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    30
struct
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    31
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    32
open Random_Engine;
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    33
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    34
fun random_fun (T1 : typ) (T2 : typ) (eq : 'a -> 'a -> bool) (term_of : 'a -> term)
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    35
    (random : Random_Engine.seed -> ('b * (unit -> term)) * Random_Engine.seed)
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    36
    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    37
    (seed : Random_Engine.seed) =
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    38
  let
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    39
    val (seed', seed'') = random_split seed;
28524
644b62cf678f arbitrary is undefined
haftmann
parents: 28394
diff changeset
    40
    val state = ref (seed', [], Const (@{const_name undefined}, T1 --> T2));
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    41
    val fun_upd = Const (@{const_name fun_upd},
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    42
      (T1 --> T2) --> T1 --> T2 --> T1 --> T2);
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    43
    fun random_fun' x =
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    44
      let
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    45
        val (seed, fun_map, f_t) = ! state;
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    46
      in case AList.lookup (uncurry eq) fun_map x
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    47
       of SOME y => y
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    48
        | NONE => let
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    49
              val t1 = term_of x;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    50
              val ((y, t2), seed') = random seed;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    51
              val fun_map' = (x, y) :: fun_map;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    52
              val f_t' = fun_upd $ f_t $ t1 $ t2 ();
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    53
              val _ = state := (seed', fun_map', f_t');
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    54
            in y end
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    55
      end;
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    56
    fun term_fun' () = #3 (! state);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    57
  in ((random_fun', term_fun'), seed'') end;
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    58
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    59
end
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    60
*}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    61
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    62
axiomatization
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    63
  random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    64
    \<Rightarrow> (seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> seed) \<Rightarrow> (seed \<Rightarrow> seed \<times> seed)
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    65
    \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed"
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    66
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    67
code_const random_fun_aux (SML "Random'_Engine.random'_fun")
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    68
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    69
instantiation "fun" :: ("{eq, term_of}", "{type, random}") random
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    70
begin
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    71
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
    72
definition random_fun :: "index \<Rightarrow> seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> seed" where
28335
25326092cf9a renamed rtype to typerep
haftmann
parents: 28315
diff changeset
    73
  "random n = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (random n) split_seed"
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    74
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    75
instance ..
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    76
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    77
end
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    78
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
    79
code_reserved SML Random_Engine
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
    80
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    81
text {* Datatypes *}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    82
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    83
definition
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    84
  collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    85
  "collapse f = (do g \<leftarrow> f; g done)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    86
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    87
ML {*
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    88
structure StateMonad =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    89
struct
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    90
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    91
fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    92
fun liftT' sT = sT --> sT;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    93
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    94
fun return T sT x = Const (@{const_name return}, T --> liftT T sT) $ x;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    95
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    96
fun scomp T1 T2 sT f g = Const (@{const_name scomp},
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    97
  liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    98
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    99
end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   100
*}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   101
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   102
lemma random'_if:
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   103
  fixes random' :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   104
  assumes "random' 0 j = (\<lambda>s. undefined)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   105
    and "\<And>i. random' (Suc_index i) j = rhs2 i"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   106
  shows "random' i j s = (if i = 0 then undefined else rhs2 (i - 1) s)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   107
  by (cases i rule: index.exhaust) (insert assms, simp_all)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   108
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   109
setup {*
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   110
let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   111
  exception REC of string;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   112
  exception TYP of string;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   113
  fun mk_collapse thy ty = Sign.mk_const thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   114
    (@{const_name collapse}, [@{typ seed}, ty]);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   115
  fun term_ty ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   116
  fun mk_split thy ty ty' = Sign.mk_const thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   117
    (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT (term_ty ty') @{typ seed}]);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   118
  fun mk_scomp_split thy ty ty' t t' =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   119
    StateMonad.scomp (term_ty ty) (term_ty ty') @{typ seed} t
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   120
      (mk_split thy ty ty' $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   121
  fun mk_cons thy this_ty (c, args) =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   122
    let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   123
      val tys = map (fst o fst) args;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   124
      val c_ty = tys ---> this_ty;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   125
      val c = Const (c, tys ---> this_ty);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   126
      val t_indices = map (curry ( op * ) 2) (length tys - 1 downto 0);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   127
      val c_indices = map (curry ( op + ) 1) t_indices;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   128
      val c_t = list_comb (c, map Bound c_indices);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   129
      val t_t = Abs ("", @{typ unit}, Eval.mk_term Free Typerep.typerep
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   130
        (list_comb (c, map (fn k => Bound (k + 1)) t_indices))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   131
        |> map_aterms (fn t as Bound _ => t $ @{term "()"} | t => t));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   132
      val return = StateMonad.return (term_ty this_ty) @{typ seed}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   133
        (HOLogic.mk_prod (c_t, t_t));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   134
      val t = fold_rev (fn ((ty, _), random) =>
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   135
        mk_scomp_split thy ty this_ty random)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   136
          args return;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   137
      val is_rec = exists (snd o fst) args;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   138
    in (is_rec, t) end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   139
  fun mk_conss thy ty [] = NONE
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   140
    | mk_conss thy ty [(_, t)] = SOME t
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   141
    | mk_conss thy ty ts = SOME (mk_collapse thy (term_ty ty) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   142
          (Sign.mk_const thy (@{const_name select}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   143
            HOLogic.mk_list (StateMonad.liftT (term_ty ty) @{typ seed}) (map snd ts)));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   144
  fun mk_clauses thy ty (tyco, (ts_rec, ts_atom)) = 
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   145
    let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   146
      val SOME t_atom = mk_conss thy ty ts_atom;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   147
    in case mk_conss thy ty ts_rec
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   148
     of SOME t_rec => mk_collapse thy (term_ty ty) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   149
          (Sign.mk_const thy (@{const_name select_default}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   150
             @{term "i\<Colon>index"} $ t_rec $ t_atom)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   151
      | NONE => t_atom
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   152
    end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   153
  fun mk_random_eqs thy vs tycos =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   154
    let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   155
      val this_ty = Type (hd tycos, map TFree vs);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   156
      val this_ty' = StateMonad.liftT (term_ty this_ty) @{typ seed};
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   157
      val random_name = NameSpace.base @{const_name random};
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   158
      val random'_name = random_name ^ "_" ^ Class.type_name (hd tycos) ^ "'";
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   159
      fun random ty = Sign.mk_const thy (@{const_name random}, [ty]);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   160
      val random' = Free (random'_name,
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   161
        @{typ index} --> @{typ index} --> this_ty');
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   162
      fun atom ty = if Sign.of_sort thy (ty, @{sort random})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   163
        then ((ty, false), random ty $ @{term "j\<Colon>index"})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   164
        else raise TYP
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   165
          ("Will not generate random elements for type(s) " ^ quote (hd tycos));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   166
      fun dtyp tyco = ((this_ty, true), random' $ @{term "i\<Colon>index"} $ @{term "j\<Colon>index"});
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   167
      fun rtyp tyco tys = raise REC
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   168
        ("Will not generate random elements for mutual recursive type " ^ quote (hd tycos));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   169
      val rhss = DatatypePackage.construction_interpretation thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   170
            { atom = atom, dtyp = dtyp, rtyp = rtyp } vs tycos
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   171
        |> (map o apsnd o map) (mk_cons thy this_ty) 
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   172
        |> (map o apsnd) (List.partition fst)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   173
        |> map (mk_clauses thy this_ty)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   174
      val eqss = map ((apsnd o map) (HOLogic.mk_Trueprop o HOLogic.mk_eq) o (fn rhs => ((this_ty, random'), [
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   175
          (random' $ @{term "0\<Colon>index"} $ @{term "j\<Colon>index"}, Abs ("s", @{typ seed},
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   176
            Const (@{const_name undefined}, HOLogic.mk_prodT (term_ty this_ty, @{typ seed})))),
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   177
          (random' $ @{term "Suc_index i"} $ @{term "j\<Colon>index"}, rhs)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   178
        ]))) rhss;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   179
    in eqss end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   180
  fun random_inst [tyco] thy =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   181
        let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   182
          val (raw_vs, _) = DatatypePackage.the_datatype_spec thy tyco;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   183
          val vs = (map o apsnd)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   184
            (curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort random}) raw_vs;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   185
          val ((this_ty, random'), eqs') = singleton (mk_random_eqs thy vs) tyco;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   186
          val eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   187
            (Sign.mk_const thy (@{const_name random}, [this_ty]) $ @{term "i\<Colon>index"},
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   188
               random' $ @{term "i\<Colon>index"} $ @{term "i\<Colon>index"})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   189
          val del_func = Attrib.internal (fn _ => Thm.declaration_attribute
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   190
            (fn thm => Context.mapping (Code.del_eqn thm) I));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   191
          fun add_code simps lthy =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   192
            let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   193
              val thy = ProofContext.theory_of lthy;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   194
              val thm = @{thm random'_if}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   195
                |> Drule.instantiate' [SOME (Thm.ctyp_of thy this_ty)] [SOME (Thm.cterm_of thy random')]
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   196
                |> (fn thm => thm OF simps)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   197
                |> singleton (ProofContext.export lthy (ProofContext.init thy));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   198
              val c = (fst o dest_Const o fst o strip_comb o fst
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   199
                o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   200
            in
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   201
              lthy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   202
              |> LocalTheory.theory (Code.del_eqns c
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   203
                   #> PureThy.add_thm ((fst (dest_Free random') ^ "_code", thm), [Thm.kind_internal])
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   204
                   #-> Code.add_eqn)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   205
            end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   206
        in
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   207
          thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   208
          |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   209
          |> PrimrecPackage.add_primrec
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   210
               [(Binding.name (fst (dest_Free random')), SOME (snd (dest_Free random')), NoSyn)]
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   211
                 (map (fn eq => ((Binding.empty, [del_func]), eq)) eqs')
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   212
          |-> add_code
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   213
          |> `(fn lthy => Syntax.check_term lthy eq)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   214
          |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   215
          |> snd
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   216
          |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   217
          |> LocalTheory.exit_global
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   218
        end
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   219
    | random_inst tycos thy = raise REC
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   220
        ("Will not generate random elements for mutual recursive type(s) " ^ commas (map quote tycos));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   221
  fun add_random_inst tycos thy = random_inst tycos thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   222
     handle REC msg => (warning msg; thy)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   223
          | TYP msg => (warning msg; thy)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   224
in DatatypePackage.interpretation add_random_inst end
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   225
*}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   226
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   227
text {* Type @{typ int} *}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   228
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   229
instantiation int :: random
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   230
begin
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   231
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   232
definition
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   233
  "random n = (do
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   234
     (b, _) \<leftarrow> random n;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   235
     (m, t) \<leftarrow> random n;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   236
     return (if b then (int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   237
       else (- int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''HOL.uminus_class.uminus'') TYPEREP(int \<Rightarrow> int))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   238
         (Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   239
   done)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   240
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   241
instance ..
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   242
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   243
end
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   244
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   245
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   246
subsection {* Quickcheck generator *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   247
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   248
ML {*
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   249
structure Quickcheck =
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   250
struct
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   251
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   252
open Quickcheck;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   253
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   254
val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   255
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   256
fun mk_generator_expr thy prop tys =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   257
  let
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   258
    val bound_max = length tys - 1;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   259
    val bounds = map_index (fn (i, ty) =>
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   260
      (2 * (bound_max - i) + 1, 2 * (bound_max - i), 2 * i, ty)) tys;
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   261
    val result = list_comb (prop, map (fn (i, _, _, _) => Bound i) bounds);
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   262
    val terms = HOLogic.mk_list @{typ term} (map (fn (_, i, _, _) => Bound i $ @{term "()"}) bounds);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   263
    val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   264
      $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ terms);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   265
    val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"};
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   266
    fun mk_termtyp ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   267
    fun mk_split ty = Sign.mk_const thy
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   268
      (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT @{typ "term list option"} @{typ seed}]);
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
   269
    fun mk_scomp_split ty t t' =
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
   270
      StateMonad.scomp (mk_termtyp ty) @{typ "term list option"} @{typ seed} t (*FIXME*)
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   271
        (mk_split ty $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')));
26589
43cb72871897 renamed mbind to scomp
haftmann
parents: 26325
diff changeset
   272
    fun mk_bindclause (_, _, i, ty) = mk_scomp_split ty
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   273
      (Sign.mk_const thy (@{const_name random}, [ty]) $ Bound i)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   274
    val t = fold_rev mk_bindclause bounds (return $ check);
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   275
  in Abs ("n", @{typ index}, t) end;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   276
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   277
fun compile_generator_expr thy t =
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   278
  let
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   279
    val tys = (map snd o fst o strip_abs) t;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   280
    val t' = mk_generator_expr thy t tys;
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   281
    val f = Code_ML.eval_term ("Quickcheck.eval_ref", eval_ref) thy t' [];
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   282
  in f #> Random_Engine.run #> (Option.map o map) (Code.postprocess_term thy) end;
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   283
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   284
end
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   285
*}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   286
28309
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   287
setup {*
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   288
  Quickcheck.add_generator ("code", Quickcheck.compile_generator_expr o ProofContext.theory_of)
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   289
*}
c24bc53c815c some steps towards generic quickcheck framework
haftmann
parents: 28228
diff changeset
   290
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   291
subsection {* Examples *}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   292
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   293
theorem "map g (map f xs) = map (g o f) xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   294
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   295
  by (induct xs) simp_all
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   296
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   297
theorem "map g (map f xs) = map (f o g) xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   298
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   299
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   300
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   301
theorem "rev (xs @ ys) = rev ys @ rev xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   302
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   303
  by simp
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   304
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   305
theorem "rev (xs @ ys) = rev xs @ rev ys"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   306
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   307
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   308
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   309
theorem "rev (rev xs) = xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   310
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   311
  by simp
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   312
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   313
theorem "rev xs = xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   314
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   315
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   316
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   317
primrec app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   318
  "app [] x = x"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   319
  | "app (f # fs) x = app fs (f x)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   320
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   321
lemma "app (fs @ gs) x = app gs (app fs x)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   322
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   323
  by (induct fs arbitrary: x) simp_all
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   324
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   325
lemma "app (fs @ gs) x = app fs (app gs x)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   326
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   327
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   328
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   329
primrec occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   330
  "occurs a [] = 0"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   331
  | "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   332
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   333
primrec del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   334
  "del1 a [] = []"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   335
  | "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   336
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   337
lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   338
  -- {* Wrong. Precondition needed.*}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   339
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   340
  oops
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   341
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   342
lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   343
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   344
    -- {* Also wrong.*}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   345
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   346
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   347
lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   348
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   349
  by (induct xs) auto
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   350
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   351
primrec replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   352
  "replace a b [] = []"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   353
  | "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs)) 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   354
                            else (x#(replace a b xs)))"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   355
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   356
lemma "occurs a xs = occurs b (replace a b xs)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   357
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   358
  -- {* Wrong. Precondition needed.*}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   359
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   360
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   361
lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   362
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   363
  by (induct xs) simp_all
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   364
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   365
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   366
subsection {* Trees *}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   367
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   368
datatype 'a tree = Twig |  Leaf 'a | Branch "'a tree" "'a tree"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   369
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   370
primrec leaves :: "'a tree \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   371
  "leaves Twig = []"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   372
  | "leaves (Leaf a) = [a]"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   373
  | "leaves (Branch l r) = (leaves l) @ (leaves r)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   374
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   375
primrec plant :: "'a list \<Rightarrow> 'a tree" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   376
  "plant [] = Twig "
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   377
  | "plant (x#xs) = Branch (Leaf x) (plant xs)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   378
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   379
primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   380
  "mirror (Twig) = Twig "
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   381
  | "mirror (Leaf a) = Leaf a "
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   382
  | "mirror (Branch l r) = Branch (mirror r) (mirror l)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   383
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   384
theorem "plant (rev (leaves xt)) = mirror xt"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   385
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   386
    --{* Wrong! *} 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   387
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   388
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   389
theorem "plant (leaves xt @ leaves yt) = Branch xt yt"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   390
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   391
    --{* Wrong! *} 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   392
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   393
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   394
datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   395
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   396
primrec inOrder :: "'a ntree \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   397
  "inOrder (Tip a)= [a]"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   398
  | "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   399
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   400
primrec root :: "'a ntree \<Rightarrow> 'a" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   401
  "root (Tip a) = a"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   402
  | "root (Node f x y) = f"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   403
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   404
theorem "hd (inOrder xt) = root xt"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   405
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   406
    --{* Wrong! *} 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   407
  oops
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   408
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   409
lemma "int (f k) = k"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   410
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   411
  oops
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   412
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   413
end