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