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