src/HOL/ex/Quickcheck.thy
author haftmann
Wed Mar 12 19:38:14 2008 +0100 (2008-03-12)
changeset 26265 4b63b9e9b10d
child 26267 ba710daf77a7
permissions -rw-r--r--
separated Random.thy from Quickcheck.thy
haftmann@26265
     1
(*  ID:         $Id$
haftmann@26265
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@26265
     3
*)
haftmann@26265
     4
haftmann@26265
     5
header {* A simple counterexample generator *}
haftmann@26265
     6
haftmann@26265
     7
theory Quickcheck
haftmann@26265
     8
imports Random Eval
haftmann@26265
     9
begin
haftmann@26265
    10
haftmann@26265
    11
subsection {* The @{text random} class *}
haftmann@26265
    12
haftmann@26265
    13
class random = type +
haftmann@26265
    14
  fixes random :: "index \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
haftmann@26265
    15
haftmann@26265
    16
print_classes
haftmann@26265
    17
haftmann@26265
    18
instantiation itself :: (type) random
haftmann@26265
    19
begin
haftmann@26265
    20
haftmann@26265
    21
definition
haftmann@26265
    22
  "random _ = return TYPE('a)"
haftmann@26265
    23
haftmann@26265
    24
instance ..
haftmann@26265
    25
haftmann@26265
    26
end
haftmann@26265
    27
haftmann@26265
    28
lemma random_aux_if:
haftmann@26265
    29
  fixes random' :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
haftmann@26265
    30
  assumes "random' 0 j = undefined"
haftmann@26265
    31
    and "\<And>i. random' (Suc_index i) j = rhs2 i"
haftmann@26265
    32
  shows "random' i j s = (if i = 0 then undefined else rhs2 (i - 1) s)"
haftmann@26265
    33
  by (cases i rule: index.exhaust) (insert assms, simp_all add: undefined_fun)
haftmann@26265
    34
haftmann@26265
    35
setup {*
haftmann@26265
    36
let
haftmann@26265
    37
  exception REC;
haftmann@26265
    38
  fun random_inst tyco thy =
haftmann@26265
    39
    let
haftmann@26265
    40
      val { descr, index, ... } = DatatypePackage.the_datatype thy tyco;
haftmann@26265
    41
      val _ = if length descr > 1 then raise REC else ();
haftmann@26265
    42
      val (raw_vs, _) = DatatypePackage.the_datatype_spec thy tyco;
haftmann@26265
    43
      val vs = (map o apsnd)
haftmann@26265
    44
        (curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort random}) raw_vs;
haftmann@26265
    45
      val ty = Type (tyco, map TFree vs);
haftmann@26265
    46
      val typ_of = DatatypeAux.typ_of_dtyp descr vs;
haftmann@26265
    47
      val SOME (_, _, constrs) = AList.lookup (op =) descr index;
haftmann@26265
    48
      val randomN = NameSpace.base @{const_name random};
haftmann@26265
    49
      val random_aux_name = randomN ^ "_" ^ Class.type_name tyco ^ "'";
haftmann@26265
    50
      fun lift_ty ty = StateMonad.liftT ty @{typ seed};
haftmann@26265
    51
      val ty_aux = @{typ index} --> @{typ index} --> lift_ty ty;
haftmann@26265
    52
      fun random ty =
haftmann@26265
    53
        Const (@{const_name random}, @{typ index} --> lift_ty ty);
haftmann@26265
    54
      val random_aux = Free (random_aux_name, ty_aux);
haftmann@26265
    55
      fun add_cons_arg dty (is_rec, t) =
haftmann@26265
    56
        let
haftmann@26265
    57
          val ty' = typ_of dty;
haftmann@26265
    58
          val rec_call = case try DatatypeAux.dest_DtRec dty
haftmann@26265
    59
           of SOME index' => index = index'
haftmann@26265
    60
            | NONE => false
haftmann@26265
    61
          val random' = if rec_call
haftmann@26265
    62
            then random_aux $ @{term "i\<Colon>index"} $ @{term "j\<Colon>index"}
haftmann@26265
    63
            else random ty' $ @{term "j\<Colon>index"}
haftmann@26265
    64
          val is_rec' = is_rec orelse DatatypeAux.is_rec_type dty;
haftmann@26265
    65
          val t' = StateMonad.mbind ty' ty @{typ seed} random' (Abs ("", ty', t))
haftmann@26265
    66
        in (is_rec', t') end;
haftmann@26265
    67
      fun mk_cons_t (c, dtys) =
haftmann@26265
    68
        let
haftmann@26265
    69
          val ty' = map typ_of dtys ---> ty;
haftmann@26265
    70
          val t = StateMonad.return ty @{typ seed} (list_comb (Const (c, ty'),
haftmann@26265
    71
            map Bound (length dtys - 1 downto 0)));
haftmann@26265
    72
          val (is_rec, t') = fold_rev add_cons_arg dtys (false, t);
haftmann@26265
    73
        in (is_rec, StateMonad.run ty @{typ seed} t') end;
haftmann@26265
    74
      fun check_empty [] = NONE
haftmann@26265
    75
        | check_empty xs = SOME xs;
haftmann@26265
    76
      fun bundle_cons_ts cons_ts =
haftmann@26265
    77
        let
haftmann@26265
    78
          val ts = map snd cons_ts;
haftmann@26265
    79
          val t = HOLogic.mk_list (lift_ty ty) ts;
haftmann@26265
    80
          val t' = Const (@{const_name select}, HOLogic.listT (lift_ty ty) --> lift_ty (lift_ty ty)) $ t;
haftmann@26265
    81
          val t'' = Const (@{const_name collapse}, lift_ty (lift_ty ty) --> lift_ty ty) $ t';
haftmann@26265
    82
        in t'' end;
haftmann@26265
    83
      fun bundle_conss (some_rec_t, nonrec_t) =
haftmann@26265
    84
        let
haftmann@26265
    85
          val t = case some_rec_t
haftmann@26265
    86
           of SOME rec_t => Const (@{const_name collapse}, lift_ty (lift_ty ty) --> lift_ty ty)
haftmann@26265
    87
               $ (Const (@{const_name select_default},
haftmann@26265
    88
                   @{typ index} --> lift_ty ty --> lift_ty ty --> lift_ty (lift_ty ty))
haftmann@26265
    89
                  $ @{term "i\<Colon>index"} $ rec_t $ nonrec_t)
haftmann@26265
    90
            | NONE => nonrec_t
haftmann@26265
    91
        in t end;
haftmann@26265
    92
      val random_rhs = constrs
haftmann@26265
    93
        |> map mk_cons_t 
haftmann@26265
    94
        |> List.partition fst
haftmann@26265
    95
        |> apfst (Option.map bundle_cons_ts o check_empty)
haftmann@26265
    96
        |> apsnd bundle_cons_ts
haftmann@26265
    97
        |> bundle_conss;
haftmann@26265
    98
      val random_aux_undef = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
haftmann@26265
    99
        (random_aux $ @{term "0\<Colon>index"} $ @{term "j\<Colon>index"}, Const (@{const_name undefined}, lift_ty ty))
haftmann@26265
   100
      val random_aux_eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
haftmann@26265
   101
        (random_aux $ @{term "Suc_index i"} $ @{term "j\<Colon>index"}, random_rhs);
haftmann@26265
   102
      val random_eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq) (Const (@{const_name random},
haftmann@26265
   103
        @{typ index} --> lift_ty ty) $ @{term "i\<Colon>index"},
haftmann@26265
   104
          random_aux $ @{term "i\<Colon>index"} $ @{term "i\<Colon>index"});
haftmann@26265
   105
      val del_func = Attrib.internal (fn _ => Thm.declaration_attribute
haftmann@26265
   106
        (fn thm => Context.mapping (Code.del_func thm) I));
haftmann@26265
   107
      fun add_code simps lthy =
haftmann@26265
   108
        let
haftmann@26265
   109
          val thy = ProofContext.theory_of lthy;
haftmann@26265
   110
          val thm = @{thm random_aux_if}
haftmann@26265
   111
            |> Drule.instantiate' [SOME (Thm.ctyp_of thy ty)] [SOME (Thm.cterm_of thy random_aux)]
haftmann@26265
   112
            |> (fn thm => thm OF simps)
haftmann@26265
   113
            |> singleton (ProofContext.export lthy (ProofContext.init thy))
haftmann@26265
   114
        in
haftmann@26265
   115
          lthy
haftmann@26265
   116
          |> LocalTheory.theory (PureThy.note Thm.internalK (random_aux_name ^ "_code", thm)
haftmann@26265
   117
               #-> Code.add_func)
haftmann@26265
   118
        end;
haftmann@26265
   119
    in
haftmann@26265
   120
      thy
haftmann@26265
   121
      |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
haftmann@26265
   122
      |> PrimrecPackage.add_primrec [(random_aux_name, SOME ty_aux, NoSyn)]
haftmann@26265
   123
           [(("", [del_func]), random_aux_undef), (("", [del_func]), random_aux_eq)]
haftmann@26265
   124
      |-> add_code
haftmann@26265
   125
      |> `(fn lthy => Syntax.check_term lthy random_eq)
haftmann@26265
   126
      |-> (fn eq => Specification.definition (NONE, (("", []), eq)))
haftmann@26265
   127
      |> snd
haftmann@26265
   128
      |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
haftmann@26265
   129
      |> LocalTheory.exit
haftmann@26265
   130
      |> ProofContext.theory_of
haftmann@26265
   131
    end;
haftmann@26265
   132
  fun add_random_inst [tyco] = (fn thy => random_inst tyco thy handle REC =>
haftmann@26265
   133
        (warning ("Will not generated random elements for mutual recursive type " ^ quote tyco); thy))
haftmann@26265
   134
    | add_random_inst tycos = tap (fn _ => warning
haftmann@26265
   135
        ("Will not generated random elements for mutual recursive type(s) " ^ commas (map quote tycos)));
haftmann@26265
   136
in DatatypePackage.interpretation add_random_inst end
haftmann@26265
   137
*}
haftmann@26265
   138
haftmann@26265
   139
instantiation int :: random
haftmann@26265
   140
begin
haftmann@26265
   141
haftmann@26265
   142
definition
haftmann@26265
   143
  "random n = (do
haftmann@26265
   144
     (b, m) \<leftarrow> random n;
haftmann@26265
   145
     return (if b then int m else - int m)
haftmann@26265
   146
   done)"
haftmann@26265
   147
haftmann@26265
   148
instance ..
haftmann@26265
   149
haftmann@26265
   150
end
haftmann@26265
   151
haftmann@26265
   152
instantiation set :: (random) random
haftmann@26265
   153
begin
haftmann@26265
   154
haftmann@26265
   155
primrec random_set' :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> 'a set \<times> seed" where
haftmann@26265
   156
  "random_set' 0 j = undefined"
haftmann@26265
   157
  | "random_set' (Suc_index i) j = collapse (select_default i
haftmann@26265
   158
       (do x \<leftarrow> random i; xs \<leftarrow> random_set' i j; return (insert x xs) done)
haftmann@26265
   159
       (return {}))"
haftmann@26265
   160
haftmann@26265
   161
lemma random_set'_code [code func]:
haftmann@26265
   162
  "random_set' i j s = (if i = 0 then undefined else collapse (select_default (i - 1)
haftmann@26265
   163
       (do x \<leftarrow> random (i - 1); xs \<leftarrow> random_set' (i - 1) j; return (insert x xs) done)
haftmann@26265
   164
       (return {})) s)"
haftmann@26265
   165
  by (rule random_aux_if random_set'.simps)+
haftmann@26265
   166
haftmann@26265
   167
definition
haftmann@26265
   168
  "random i = random_set' i i"
haftmann@26265
   169
haftmann@26265
   170
instance ..
haftmann@26265
   171
haftmann@26265
   172
end
haftmann@26265
   173
haftmann@26265
   174
code_reserved SML Quickcheck
haftmann@26265
   175
haftmann@26265
   176
haftmann@26265
   177
subsection {* Quickcheck generator *}
haftmann@26265
   178
haftmann@26265
   179
ML {*
haftmann@26265
   180
structure Quickcheck =
haftmann@26265
   181
struct
haftmann@26265
   182
haftmann@26265
   183
val eval_ref : (unit -> int -> int * int -> term list option * (int * int)) option ref = ref NONE;
haftmann@26265
   184
haftmann@26265
   185
fun mk_generator_expr prop tys =
haftmann@26265
   186
  let
haftmann@26265
   187
    val bounds = map_index (fn (i, ty) => (i, ty)) tys;
haftmann@26265
   188
    val result = list_comb (prop, map (fn (i, _) => Bound (length tys - i - 1)) bounds);
haftmann@26265
   189
    val terms = map (fn (i, ty) => Const (@{const_name Eval.term_of}, ty --> @{typ term}) $ Bound (length tys - i - 1)) bounds;
haftmann@26265
   190
    val check = @{term "If \<Colon> bool \<Rightarrow> term list option \<Rightarrow> term list option \<Rightarrow> term list option"}
haftmann@26265
   191
      $ result $ @{term "None \<Colon> term list option"} $ (@{term "Some \<Colon> term list \<Rightarrow> term list option "} $ HOLogic.mk_list @{typ term} terms);
haftmann@26265
   192
    val return = @{term "Pair \<Colon> term list option \<Rightarrow> seed \<Rightarrow> term list option \<times> seed"};
haftmann@26265
   193
    fun mk_bindtyp ty = @{typ seed} --> HOLogic.mk_prodT (ty, @{typ seed});
haftmann@26265
   194
    fun mk_bindclause (i, ty) t = Const (@{const_name mbind}, mk_bindtyp ty
haftmann@26265
   195
      --> (ty --> mk_bindtyp @{typ "term list option"}) --> mk_bindtyp @{typ "term list option"})
haftmann@26265
   196
      $ (Const (@{const_name random}, @{typ index} --> mk_bindtyp ty)
haftmann@26265
   197
        $ Bound i) $ Abs ("a", ty, t);
haftmann@26265
   198
    val t = fold_rev mk_bindclause bounds (return $ check);
haftmann@26265
   199
  in Abs ("n", @{typ index}, t) end;
haftmann@26265
   200
haftmann@26265
   201
fun compile_generator_expr thy prop tys =
haftmann@26265
   202
  let
haftmann@26265
   203
    val f = CodePackage.eval_term ("Quickcheck.eval_ref", eval_ref) thy
haftmann@26265
   204
      (mk_generator_expr prop tys) [];
haftmann@26265
   205
  in f #> Random_Engine.run #> (Option.map o map) (Code.postprocess_term thy) end;
haftmann@26265
   206
haftmann@26265
   207
fun VALUE prop tys thy =
haftmann@26265
   208
  let
haftmann@26265
   209
    val t = mk_generator_expr prop tys;
haftmann@26265
   210
    val eq = Logic.mk_equals (Free ("VALUE", fastype_of t), t)
haftmann@26265
   211
  in
haftmann@26265
   212
    thy
haftmann@26265
   213
    |> TheoryTarget.init NONE
haftmann@26265
   214
    |> Specification.definition (NONE, (("", []), eq))
haftmann@26265
   215
    |> snd
haftmann@26265
   216
    |> LocalTheory.exit
haftmann@26265
   217
    |> ProofContext.theory_of
haftmann@26265
   218
  end;
haftmann@26265
   219
haftmann@26265
   220
end
haftmann@26265
   221
*}
haftmann@26265
   222
haftmann@26265
   223
(*setup {* Quickcheck.VALUE @{term "\<lambda>(n::nat) (m::nat) (q::nat). n = m + q + 1"} [@{typ nat}, @{typ nat}, @{typ nat}] *}
haftmann@26265
   224
export_code VALUE in SML module_name QuickcheckExample file "~~/../../gen_code/quickcheck.ML"
haftmann@26265
   225
use "~~/../../gen_code/quickcheck.ML"
haftmann@26265
   226
ML {* Random_Engine.run (QuickcheckExample.range 1) *}*)
haftmann@26265
   227
haftmann@26265
   228
ML {* val f = Quickcheck.compile_generator_expr @{theory}
haftmann@26265
   229
  @{term "\<lambda>(n::nat) (m::nat) (q::nat). n = m + q + 1"} [@{typ nat}, @{typ nat}, @{typ nat}] *}
haftmann@26265
   230
haftmann@26265
   231
ML {* f 5 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   232
ML {* f 5 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   233
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   234
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   235
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   236
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   237
ML {* f 25 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   238
ML {* f 1 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   239
ML {* f 1 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   240
ML {* f 2 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   241
ML {* f 2 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   242
haftmann@26265
   243
ML {* val f = Quickcheck.compile_generator_expr @{theory}
haftmann@26265
   244
  @{term "\<lambda>(n::int) (m::int) (q::int). n = m + q + 1"} [@{typ int}, @{typ int}, @{typ int}] *}
haftmann@26265
   245
haftmann@26265
   246
(*definition "FOO = (True, Suc 0)"
haftmann@26265
   247
haftmann@26265
   248
code_module (test) QuickcheckExample
haftmann@26265
   249
  file "~~/../../gen_code/quickcheck'.ML"
haftmann@26265
   250
  contains FOO*)
haftmann@26265
   251
haftmann@26265
   252
ML {* f 5 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   253
ML {* f 5 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   254
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   255
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   256
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   257
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   258
ML {* f 25 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   259
ML {* f 1 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   260
ML {* f 1 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   261
ML {* f 2 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   262
ML {* f 2 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   263
ML {* f 3 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   264
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   265
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   266
haftmann@26265
   267
ML {* val f = Quickcheck.compile_generator_expr @{theory}
haftmann@26265
   268
  @{term "\<lambda>(xs\<Colon>int list) ys. rev (xs @ ys) = rev xs @ rev ys"}
haftmann@26265
   269
  [@{typ "int list"}, @{typ "int list"}] *}
haftmann@26265
   270
haftmann@26265
   271
ML {* f 15 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   272
ML {* f 5 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   273
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   274
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   275
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   276
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   277
ML {* f 25 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   278
ML {* f 1 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   279
ML {* f 1 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   280
ML {* f 2 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   281
ML {* f 2 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   282
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   283
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   284
ML {* f 5 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   285
ML {* f 8 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   286
ML {* f 8 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   287
ML {* f 8 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   288
ML {* f 88 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   289
haftmann@26265
   290
subsection {* Incremental function generator *}
haftmann@26265
   291
haftmann@26265
   292
ML {*
haftmann@26265
   293
structure Quickcheck =
haftmann@26265
   294
struct
haftmann@26265
   295
haftmann@26265
   296
open Quickcheck;
haftmann@26265
   297
haftmann@26265
   298
fun random_fun (eq : 'a -> 'a -> bool)
haftmann@26265
   299
    (random : Random_Engine.seed -> 'b * Random_Engine.seed)
haftmann@26265
   300
    (random_split : Random_Engine.seed -> Random_Engine.seed * Random_Engine.seed)
haftmann@26265
   301
    (seed : Random_Engine.seed) =
haftmann@26265
   302
  let
haftmann@26265
   303
    val (seed', seed'') = random_split seed;
haftmann@26265
   304
    val state = ref (seed', []);
haftmann@26265
   305
    fun random_fun' x =
haftmann@26265
   306
      let
haftmann@26265
   307
        val (seed, fun_map) = ! state;
haftmann@26265
   308
      in case AList.lookup (uncurry eq) fun_map x
haftmann@26265
   309
       of SOME y => y
haftmann@26265
   310
        | NONE => let
haftmann@26265
   311
              val (y, seed') = random seed;
haftmann@26265
   312
              val _ = state := (seed', (x, y) :: fun_map);
haftmann@26265
   313
            in y end
haftmann@26265
   314
      end;
haftmann@26265
   315
  in (random_fun', seed'') end;
haftmann@26265
   316
haftmann@26265
   317
end
haftmann@26265
   318
*}
haftmann@26265
   319
haftmann@26265
   320
axiomatization
haftmann@26265
   321
  random_fun_aux :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> (seed \<Rightarrow> 'b \<times> seed)
haftmann@26265
   322
    \<Rightarrow> (seed \<Rightarrow> seed \<times> seed) \<Rightarrow> seed \<Rightarrow> ('a \<Rightarrow> 'b) \<times> seed"
haftmann@26265
   323
haftmann@26265
   324
code_const random_fun_aux (SML "Quickcheck.random'_fun")
haftmann@26265
   325
haftmann@26265
   326
instantiation "fun" :: (term_of, term_of) term_of
haftmann@26265
   327
begin
haftmann@26265
   328
haftmann@26265
   329
instance ..
haftmann@26265
   330
haftmann@26265
   331
end
haftmann@26265
   332
haftmann@26265
   333
code_const "Eval.term_of :: ('a\<Colon>term_of \<Rightarrow> 'b\<Colon>term_of) \<Rightarrow> _"
haftmann@26265
   334
  (SML "(fn '_ => Const (\"arbitrary\", dummyT))")
haftmann@26265
   335
haftmann@26265
   336
instantiation "fun" :: (eq, "{type, random}") random
haftmann@26265
   337
begin
haftmann@26265
   338
haftmann@26265
   339
definition
haftmann@26265
   340
  "random n = random_fun_aux (op =) (random n) split_seed"
haftmann@26265
   341
haftmann@26265
   342
instance ..
haftmann@26265
   343
haftmann@26265
   344
end
haftmann@26265
   345
haftmann@26265
   346
ML {* val f = Quickcheck.compile_generator_expr @{theory}
haftmann@26265
   347
  @{term "\<lambda>f k. int (f k) = k"} [@{typ "int \<Rightarrow> nat"}, @{typ int}] *}
haftmann@26265
   348
haftmann@26265
   349
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   350
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   351
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   352
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   353
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   354
ML {* f 20 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   355
haftmann@26265
   356
ML {* val f = Quickcheck.compile_generator_expr @{theory}
haftmann@26265
   357
  @{term "\<lambda>(A \<Colon> nat set) B. card (A \<union> B) = card A + card B"} [@{typ "nat set"}, @{typ "nat set"}] *}
haftmann@26265
   358
haftmann@26265
   359
ML {* f 1 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   360
ML {* f 2 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   361
ML {* f 3 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   362
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   363
ML {* f 5 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   364
ML {* f 6 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   365
ML {* f 10 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   366
ML {* f 10 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   367
haftmann@26265
   368
ML {* val f = Quickcheck.compile_generator_expr @{theory}
haftmann@26265
   369
  @{term "\<lambda>(s \<Colon> string). s \<noteq> rev s"} [@{typ string}] *}
haftmann@26265
   370
haftmann@26265
   371
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   372
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   373
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   374
ML {* f 4 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   375
ML {* f 10 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   376
ML {* f 10 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   377
ML {* f 10 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   378
ML {* f 10 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   379
ML {* f 10 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   380
ML {* f 8 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   381
ML {* f 8 |> (Option.map o map) (Sign.string_of_term @{theory}) *}
haftmann@26265
   382
haftmann@26265
   383
end