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