src/HOL/ex/Quickcheck_Generators.thy
author haftmann
Fri, 17 Apr 2009 14:29:54 +0200
changeset 30945 0418e9bffbba
parent 30364 577edc39b501
child 31135 e2d777dcf161
permissions -rw-r--r--
separate channel for Quickcheck evaluations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     2
29808
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
     3
header {* Experimental counterexample generators *}
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     4
29808
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
     5
theory Quickcheck_Generators
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
     6
imports Quickcheck State_Monad
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     7
begin
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
     8
29808
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
     9
subsection {* Datatypes *}
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    10
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    11
definition
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    12
  collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    13
  "collapse f = (do g \<leftarrow> f; g done)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    14
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    15
ML {*
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    16
structure StateMonad =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    17
struct
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    18
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    19
fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    20
fun liftT' sT = sT --> sT;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    21
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    22
fun return T sT x = Const (@{const_name return}, T --> liftT T sT) $ x;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    23
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    24
fun scomp T1 T2 sT f g = Const (@{const_name scomp},
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    25
  liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    26
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    27
end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    28
*}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    29
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    30
lemma random'_if:
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    31
  fixes random' :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> seed"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    32
  assumes "random' 0 j = (\<lambda>s. undefined)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    33
    and "\<And>i. random' (Suc_index i) j = rhs2 i"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    34
  shows "random' i j s = (if i = 0 then undefined else rhs2 (i - 1) s)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    35
  by (cases i rule: index.exhaust) (insert assms, simp_all)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    36
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    37
setup {*
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    38
let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    39
  exception REC of string;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    40
  exception TYP of string;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    41
  fun mk_collapse thy ty = Sign.mk_const thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    42
    (@{const_name collapse}, [@{typ seed}, ty]);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    43
  fun term_ty ty = HOLogic.mk_prodT (ty, @{typ "unit \<Rightarrow> term"});
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    44
  fun mk_split thy ty ty' = Sign.mk_const thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    45
    (@{const_name split}, [ty, @{typ "unit \<Rightarrow> term"}, StateMonad.liftT (term_ty ty') @{typ seed}]);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    46
  fun mk_scomp_split thy ty ty' t t' =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    47
    StateMonad.scomp (term_ty ty) (term_ty ty') @{typ seed} t
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    48
      (mk_split thy ty ty' $ Abs ("", ty, Abs ("", @{typ "unit \<Rightarrow> term"}, t')))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    49
  fun mk_cons thy this_ty (c, args) =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    50
    let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    51
      val tys = map (fst o fst) args;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    52
      val c_ty = tys ---> this_ty;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    53
      val c = Const (c, tys ---> this_ty);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    54
      val t_indices = map (curry ( op * ) 2) (length tys - 1 downto 0);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    55
      val c_indices = map (curry ( op + ) 1) t_indices;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    56
      val c_t = list_comb (c, map Bound c_indices);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    57
      val t_t = Abs ("", @{typ unit}, Eval.mk_term Free Typerep.typerep
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    58
        (list_comb (c, map (fn k => Bound (k + 1)) t_indices))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    59
        |> map_aterms (fn t as Bound _ => t $ @{term "()"} | t => t));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    60
      val return = StateMonad.return (term_ty this_ty) @{typ seed}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    61
        (HOLogic.mk_prod (c_t, t_t));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    62
      val t = fold_rev (fn ((ty, _), random) =>
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    63
        mk_scomp_split thy ty this_ty random)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    64
          args return;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    65
      val is_rec = exists (snd o fst) args;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    66
    in (is_rec, t) end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    67
  fun mk_conss thy ty [] = NONE
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    68
    | mk_conss thy ty [(_, t)] = SOME t
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    69
    | mk_conss thy ty ts = SOME (mk_collapse thy (term_ty ty) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    70
          (Sign.mk_const thy (@{const_name select}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    71
            HOLogic.mk_list (StateMonad.liftT (term_ty ty) @{typ seed}) (map snd ts)));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    72
  fun mk_clauses thy ty (tyco, (ts_rec, ts_atom)) = 
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    73
    let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    74
      val SOME t_atom = mk_conss thy ty ts_atom;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    75
    in case mk_conss thy ty ts_rec
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    76
     of SOME t_rec => mk_collapse thy (term_ty ty) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    77
          (Sign.mk_const thy (@{const_name select_default}, [StateMonad.liftT (term_ty ty) @{typ seed}]) $
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    78
             @{term "i\<Colon>index"} $ t_rec $ t_atom)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    79
      | NONE => t_atom
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    80
    end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    81
  fun mk_random_eqs thy vs tycos =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    82
    let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    83
      val this_ty = Type (hd tycos, map TFree vs);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    84
      val this_ty' = StateMonad.liftT (term_ty this_ty) @{typ seed};
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
    85
      val random_name = Long_Name.base_name @{const_name random};
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    86
      val random'_name = random_name ^ "_" ^ Class.type_name (hd tycos) ^ "'";
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    87
      fun random ty = Sign.mk_const thy (@{const_name random}, [ty]);
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    88
      val random' = Free (random'_name,
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    89
        @{typ index} --> @{typ index} --> this_ty');
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    90
      fun atom ty = if Sign.of_sort thy (ty, @{sort random})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    91
        then ((ty, false), random ty $ @{term "j\<Colon>index"})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    92
        else raise TYP
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    93
          ("Will not generate random elements for type(s) " ^ quote (hd tycos));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    94
      fun dtyp tyco = ((this_ty, true), random' $ @{term "i\<Colon>index"} $ @{term "j\<Colon>index"});
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    95
      fun rtyp tyco tys = raise REC
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    96
        ("Will not generate random elements for mutual recursive type " ^ quote (hd tycos));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    97
      val rhss = DatatypePackage.construction_interpretation thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    98
            { atom = atom, dtyp = dtyp, rtyp = rtyp } vs tycos
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
    99
        |> (map o apsnd o map) (mk_cons thy this_ty) 
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   100
        |> (map o apsnd) (List.partition fst)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   101
        |> map (mk_clauses thy this_ty)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   102
      val eqss = map ((apsnd o map) (HOLogic.mk_Trueprop o HOLogic.mk_eq) o (fn rhs => ((this_ty, random'), [
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   103
          (random' $ @{term "0\<Colon>index"} $ @{term "j\<Colon>index"}, Abs ("s", @{typ seed},
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   104
            Const (@{const_name undefined}, HOLogic.mk_prodT (term_ty this_ty, @{typ seed})))),
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   105
          (random' $ @{term "Suc_index i"} $ @{term "j\<Colon>index"}, rhs)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   106
        ]))) rhss;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   107
    in eqss end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   108
  fun random_inst [tyco] thy =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   109
        let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   110
          val (raw_vs, _) = DatatypePackage.the_datatype_spec thy tyco;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   111
          val vs = (map o apsnd)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   112
            (curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort random}) raw_vs;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   113
          val ((this_ty, random'), eqs') = singleton (mk_random_eqs thy vs) tyco;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   114
          val eq = (HOLogic.mk_Trueprop o HOLogic.mk_eq)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   115
            (Sign.mk_const thy (@{const_name random}, [this_ty]) $ @{term "i\<Colon>index"},
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   116
               random' $ @{term "i\<Colon>index"} $ @{term "i\<Colon>index"})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   117
          val del_func = Attrib.internal (fn _ => Thm.declaration_attribute
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   118
            (fn thm => Context.mapping (Code.del_eqn thm) I));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   119
          fun add_code simps lthy =
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   120
            let
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   121
              val thy = ProofContext.theory_of lthy;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   122
              val thm = @{thm random'_if}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   123
                |> Drule.instantiate' [SOME (Thm.ctyp_of thy this_ty)] [SOME (Thm.cterm_of thy random')]
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   124
                |> (fn thm => thm OF simps)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   125
                |> singleton (ProofContext.export lthy (ProofContext.init thy));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   126
              val c = (fst o dest_Const o fst o strip_comb o fst
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   127
                o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of) thm;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   128
            in
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   129
              lthy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   130
              |> LocalTheory.theory (Code.del_eqns c
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 29132
diff changeset
   131
                   #> PureThy.add_thm ((Binding.name (fst (dest_Free random') ^ "_code"), thm), [Thm.kind_internal])
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   132
                   #-> Code.add_eqn)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   133
            end;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   134
        in
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   135
          thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   136
          |> TheoryTarget.instantiation ([tyco], vs, @{sort random})
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   137
          |> PrimrecPackage.add_primrec
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   138
               [(Binding.name (fst (dest_Free random')), SOME (snd (dest_Free random')), NoSyn)]
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   139
                 (map (fn eq => ((Binding.empty, [del_func]), eq)) eqs')
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   140
          |-> add_code
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   141
          |> `(fn lthy => Syntax.check_term lthy eq)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   142
          |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   143
          |> snd
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   144
          |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   145
          |> LocalTheory.exit_global
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   146
        end
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   147
    | random_inst tycos thy = raise REC
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   148
        ("Will not generate random elements for mutual recursive type(s) " ^ commas (map quote tycos));
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   149
  fun add_random_inst tycos thy = random_inst tycos thy
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   150
     handle REC msg => (warning msg; thy)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   151
          | TYP msg => (warning msg; thy)
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   152
in DatatypePackage.interpretation add_random_inst end
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   153
*}
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   154
29808
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
   155
b8b9d529663b split of already properly working part of Quickcheck infrastructure
haftmann
parents: 29579
diff changeset
   156
subsection {* Type @{typ int} *}
29132
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   157
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   158
instantiation int :: random
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   159
begin
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   160
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   161
definition
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   162
  "random n = (do
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   163
     (b, _) \<leftarrow> random n;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   164
     (m, t) \<leftarrow> random n;
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   165
     return (if b then (int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   166
       else (- int m, \<lambda>u. Code_Eval.App (Code_Eval.Const (STR ''HOL.uminus_class.uminus'') TYPEREP(int \<Rightarrow> int))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   167
         (Code_Eval.App (Code_Eval.Const (STR ''Int.int'') TYPEREP(nat \<Rightarrow> int)) (t ()))))
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   168
   done)"
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   169
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   170
instance ..
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   171
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   172
end
3dac98ebae24 restructured; circumvent sort problem
haftmann
parents: 28965
diff changeset
   173
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   174
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   175
subsection {* Examples *}
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26265
diff changeset
   176
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   177
theorem "map g (map f xs) = map (g o f) xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   178
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   179
  by (induct xs) simp_all
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   180
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   181
theorem "map g (map f xs) = map (f o g) xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   182
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   183
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   184
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   185
theorem "rev (xs @ ys) = rev ys @ rev xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   186
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   187
  by simp
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   188
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   189
theorem "rev (xs @ ys) = rev xs @ rev ys"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   190
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   191
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   192
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   193
theorem "rev (rev xs) = xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   194
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   195
  by simp
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   196
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   197
theorem "rev xs = xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   198
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   199
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   200
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   201
primrec app :: "('a \<Rightarrow> 'a) list \<Rightarrow> 'a \<Rightarrow> 'a" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   202
  "app [] x = x"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   203
  | "app (f # fs) x = app fs (f x)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   204
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   205
lemma "app (fs @ gs) x = app gs (app fs x)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   206
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   207
  by (induct fs arbitrary: x) simp_all
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   208
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   209
lemma "app (fs @ gs) x = app fs (app gs x)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   210
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   211
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   212
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   213
primrec occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   214
  "occurs a [] = 0"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   215
  | "occurs a (x#xs) = (if (x=a) then Suc(occurs a xs) else occurs a xs)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   216
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   217
primrec del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   218
  "del1 a [] = []"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   219
  | "del1 a (x#xs) = (if (x=a) then xs else (x#del1 a xs))"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   220
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   221
lemma "Suc (occurs a (del1 a xs)) = occurs a xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   222
  -- {* Wrong. Precondition needed.*}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   223
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   224
  oops
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   225
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   226
lemma "xs ~= [] \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   227
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   228
    -- {* Also wrong.*}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   229
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   230
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   231
lemma "0 < occurs a xs \<longrightarrow> Suc (occurs a (del1 a xs)) = occurs a xs"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   232
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   233
  by (induct xs) auto
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   234
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   235
primrec replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   236
  "replace a b [] = []"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   237
  | "replace a b (x#xs) = (if (x=a) then (b#(replace a b xs)) 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   238
                            else (x#(replace a b xs)))"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   239
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   240
lemma "occurs a xs = occurs b (replace a b xs)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   241
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   242
  -- {* Wrong. Precondition needed.*}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   243
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   244
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   245
lemma "occurs b xs = 0 \<or> a=b \<longrightarrow> occurs a xs = occurs b (replace a b xs)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   246
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   247
  by (induct xs) simp_all
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   248
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   249
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   250
subsection {* Trees *}
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   251
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   252
datatype 'a tree = Twig |  Leaf 'a | Branch "'a tree" "'a tree"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   253
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   254
primrec leaves :: "'a tree \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   255
  "leaves Twig = []"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   256
  | "leaves (Leaf a) = [a]"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   257
  | "leaves (Branch l r) = (leaves l) @ (leaves r)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   258
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   259
primrec plant :: "'a list \<Rightarrow> 'a tree" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   260
  "plant [] = Twig "
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   261
  | "plant (x#xs) = Branch (Leaf x) (plant xs)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   262
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   263
primrec mirror :: "'a tree \<Rightarrow> 'a tree" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   264
  "mirror (Twig) = Twig "
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   265
  | "mirror (Leaf a) = Leaf a "
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   266
  | "mirror (Branch l r) = Branch (mirror r) (mirror l)"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   267
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   268
theorem "plant (rev (leaves xt)) = mirror xt"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   269
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   270
    --{* Wrong! *} 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   271
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   272
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   273
theorem "plant (leaves xt @ leaves yt) = Branch xt yt"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   274
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   275
    --{* Wrong! *} 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   276
  oops
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   277
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   278
datatype 'a ntree = Tip "'a" | Node "'a" "'a ntree" "'a ntree"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   279
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   280
primrec inOrder :: "'a ntree \<Rightarrow> 'a list" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   281
  "inOrder (Tip a)= [a]"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   282
  | "inOrder (Node f x y) = (inOrder x)@[f]@(inOrder y)"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   283
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   284
primrec root :: "'a ntree \<Rightarrow> 'a" where
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   285
  "root (Tip a) = a"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   286
  | "root (Node f x y) = f"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   287
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   288
theorem "hd (inOrder xt) = root xt"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   289
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   290
    --{* Wrong! *} 
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   291
  oops
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   292
28315
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   293
lemma "int (f k) = k"
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   294
  quickcheck [generator = code]
d3cf88fe77bc generic quickcheck framework
haftmann
parents: 28309
diff changeset
   295
  oops
26325
6ecae5c8175b quickcheck with term reconstruction
haftmann
parents: 26275
diff changeset
   296
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents:
diff changeset
   297
end