src/HOL/Quickcheck_Narrowing.thy
author blanchet
Wed Sep 03 00:06:24 2014 +0200 (2014-09-03)
changeset 58152 6fe60a9a5bad
parent 56401 3b2db6132bfd
child 58310 91ea607a34d8
permissions -rw-r--r--
use 'datatype_new' in 'Main'
     1 (* Author: Lukas Bulwahn, TU Muenchen *)
     2 
     3 header {* Counterexample generator performing narrowing-based testing *}
     4 
     5 theory Quickcheck_Narrowing
     6 imports Quickcheck_Random
     7 keywords "find_unused_assms" :: diag
     8 begin
     9 
    10 subsection {* Counterexample generator *}
    11 
    12 subsubsection {* Code generation setup *}
    13 
    14 setup {* Code_Target.extend_target ("Haskell_Quickcheck", (Code_Haskell.target, I)) *}
    15 
    16 code_printing
    17   code_module Typerep \<rightharpoonup> (Haskell_Quickcheck) {*
    18 data Typerep = Typerep String [Typerep]
    19 *}
    20 | type_constructor typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep.Typerep"
    21 | constant Typerep.Typerep \<rightharpoonup> (Haskell_Quickcheck) "Typerep.Typerep"
    22 | type_constructor integer \<rightharpoonup> (Haskell_Quickcheck) "Prelude.Int"
    23 
    24 code_reserved Haskell_Quickcheck Typerep
    25 
    26 
    27 subsubsection {* Narrowing's deep representation of types and terms *}
    28 
    29 datatype_new narrowing_type =
    30   Narrowing_sum_of_products "narrowing_type list list"
    31 
    32 datatype_new narrowing_term =
    33   Narrowing_variable "integer list" narrowing_type
    34 | Narrowing_constructor integer "narrowing_term list"
    35 
    36 datatype_new (dead 'a) narrowing_cons =
    37   Narrowing_cons narrowing_type "(narrowing_term list \<Rightarrow> 'a) list"
    38 
    39 primrec map_cons :: "('a => 'b) => 'a narrowing_cons => 'b narrowing_cons"
    40 where
    41   "map_cons f (Narrowing_cons ty cs) = Narrowing_cons ty (map (\<lambda>c. f o c) cs)"
    42 
    43 subsubsection {* From narrowing's deep representation of terms to @{theory Code_Evaluation}'s terms *}
    44 
    45 class partial_term_of = typerep +
    46   fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term"
    47 
    48 lemma partial_term_of_anything: "partial_term_of x nt \<equiv> t"
    49   by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp)
    50  
    51 subsubsection {* Auxilary functions for Narrowing *}
    52 
    53 consts nth :: "'a list => integer => 'a"
    54 
    55 code_printing constant nth \<rightharpoonup> (Haskell_Quickcheck) infixl 9 "!!"
    56 
    57 consts error :: "char list => 'a"
    58 
    59 code_printing constant error \<rightharpoonup> (Haskell_Quickcheck) "error"
    60 
    61 consts toEnum :: "integer => char"
    62 
    63 code_printing constant toEnum \<rightharpoonup> (Haskell_Quickcheck) "Prelude.toEnum"
    64 
    65 consts marker :: "char"
    66 
    67 code_printing constant marker \<rightharpoonup> (Haskell_Quickcheck) "''\\0'"
    68 
    69 subsubsection {* Narrowing's basic operations *}
    70 
    71 type_synonym 'a narrowing = "integer => 'a narrowing_cons"
    72 
    73 definition empty :: "'a narrowing"
    74 where
    75   "empty d = Narrowing_cons (Narrowing_sum_of_products []) []"
    76   
    77 definition cons :: "'a => 'a narrowing"
    78 where
    79   "cons a d = (Narrowing_cons (Narrowing_sum_of_products [[]]) [(\<lambda>_. a)])"
    80 
    81 fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a"
    82 where
    83   "conv cs (Narrowing_variable p _) = error (marker # map toEnum p)"
    84 | "conv cs (Narrowing_constructor i xs) = (nth cs i) xs"
    85 
    86 fun non_empty :: "narrowing_type => bool"
    87 where
    88   "non_empty (Narrowing_sum_of_products ps) = (\<not> (List.null ps))"
    89 
    90 definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
    91 where
    92   "apply f a d =
    93      (case f d of Narrowing_cons (Narrowing_sum_of_products ps) cfs =>
    94        case a (d - 1) of Narrowing_cons ta cas =>
    95        let
    96          shallow = (d > 0 \<and> non_empty ta);
    97          cs = [(\<lambda>xs'. (case xs' of [] => undefined | x # xs => cf xs (conv cas x))). shallow, cf <- cfs]
    98        in Narrowing_cons (Narrowing_sum_of_products [ta # p. shallow, p <- ps]) cs)"
    99 
   100 definition sum :: "'a narrowing => 'a narrowing => 'a narrowing"
   101 where
   102   "sum a b d =
   103     (case a d of Narrowing_cons (Narrowing_sum_of_products ssa) ca => 
   104       case b d of Narrowing_cons (Narrowing_sum_of_products ssb) cb =>
   105       Narrowing_cons (Narrowing_sum_of_products (ssa @ ssb)) (ca @ cb))"
   106 
   107 lemma [fundef_cong]:
   108   assumes "a d = a' d" "b d = b' d" "d = d'"
   109   shows "sum a b d = sum a' b' d'"
   110 using assms unfolding sum_def by (auto split: narrowing_cons.split narrowing_type.split)
   111 
   112 lemma [fundef_cong]:
   113   assumes "f d = f' d" "(\<And>d'. 0 \<le> d' \<and> d' < d \<Longrightarrow> a d' = a' d')"
   114   assumes "d = d'"
   115   shows "apply f a d = apply f' a' d'"
   116 proof -
   117   note assms
   118   moreover have "0 < d' \<Longrightarrow> 0 \<le> d' - 1"
   119     by (simp add: less_integer_def less_eq_integer_def)
   120   ultimately show ?thesis
   121     by (auto simp add: apply_def Let_def
   122       split: narrowing_cons.split narrowing_type.split)
   123 qed
   124 
   125 subsubsection {* Narrowing generator type class *}
   126 
   127 class narrowing =
   128   fixes narrowing :: "integer => 'a narrowing_cons"
   129 
   130 datatype_new property = Universal narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term" | Existential narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term" | Property bool
   131 
   132 (* FIXME: hard-wired maximal depth of 100 here *)
   133 definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
   134 where
   135   "exists f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Existential ty (\<lambda> t. f (conv cs t)) (partial_term_of (TYPE('a))))"
   136 
   137 definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
   138 where
   139   "all f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Universal ty (\<lambda>t. f (conv cs t)) (partial_term_of (TYPE('a))))"
   140 
   141 subsubsection {* class @{text is_testable} *}
   142 
   143 text {* The class @{text is_testable} ensures that all necessary type instances are generated. *}
   144 
   145 class is_testable
   146 
   147 instance bool :: is_testable ..
   148 
   149 instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
   150 
   151 definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
   152 where
   153   "ensure_testable f = f"
   154 
   155 
   156 subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *}
   157 
   158 datatype_new (dead 'a, dead 'b) ffun = Constant 'b | Update 'a 'b "('a, 'b) ffun"
   159 
   160 primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
   161 where
   162   "eval_ffun (Constant c) x = c"
   163 | "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
   164 
   165 hide_type (open) ffun
   166 hide_const (open) Constant Update eval_ffun
   167 
   168 datatype_new (dead 'b) cfun = Constant 'b
   169 
   170 primrec eval_cfun :: "'b cfun => 'a => 'b"
   171 where
   172   "eval_cfun (Constant c) y = c"
   173 
   174 hide_type (open) cfun
   175 hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun
   176 
   177 subsubsection {* Setting up the counterexample generator *}
   178 
   179 ML_file "Tools/Quickcheck/narrowing_generators.ML"
   180 
   181 setup {* Narrowing_Generators.setup *}
   182 
   183 definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
   184 where
   185   "narrowing_dummy_partial_term_of = partial_term_of"
   186 
   187 definition narrowing_dummy_narrowing :: "integer => ('a :: narrowing) narrowing_cons"
   188 where
   189   "narrowing_dummy_narrowing = narrowing"
   190 
   191 lemma [code]:
   192   "ensure_testable f =
   193     (let
   194       x = narrowing_dummy_narrowing :: integer => bool narrowing_cons;
   195       y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term;
   196       z = (conv :: _ => _ => unit)  in f)"
   197 unfolding Let_def ensure_testable_def ..
   198 
   199 subsection {* Narrowing for sets *}
   200 
   201 instantiation set :: (narrowing) narrowing
   202 begin
   203 
   204 definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing"
   205 
   206 instance ..
   207 
   208 end
   209   
   210 subsection {* Narrowing for integers *}
   211 
   212 
   213 definition drawn_from :: "'a list \<Rightarrow> 'a narrowing_cons"
   214 where
   215   "drawn_from xs =
   216     Narrowing_cons (Narrowing_sum_of_products (map (\<lambda>_. []) xs)) (map (\<lambda>x _. x) xs)"
   217 
   218 function around_zero :: "int \<Rightarrow> int list"
   219 where
   220   "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))"
   221   by pat_completeness auto
   222 termination by (relation "measure nat") auto
   223 
   224 declare around_zero.simps [simp del]
   225 
   226 lemma length_around_zero:
   227   assumes "i >= 0" 
   228   shows "length (around_zero i) = 2 * nat i + 1"
   229 proof (induct rule: int_ge_induct [OF assms])
   230   case 1
   231   from 1 show ?case by (simp add: around_zero.simps)
   232 next
   233   case (2 i)
   234   from 2 show ?case
   235     by (simp add: around_zero.simps [of "i + 1"])
   236 qed
   237 
   238 instantiation int :: narrowing
   239 begin
   240 
   241 definition
   242   "narrowing_int d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
   243     in drawn_from (around_zero i))"
   244 
   245 instance ..
   246 
   247 end
   248 
   249 lemma [code, code del]: "partial_term_of (ty :: int itself) t \<equiv> undefined"
   250   by (rule partial_term_of_anything)+
   251 
   252 lemma [code]:
   253   "partial_term_of (ty :: int itself) (Narrowing_variable p t) \<equiv>
   254     Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])"
   255   "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \<equiv>
   256     (if i mod 2 = 0
   257      then Code_Evaluation.term_of (- (int_of_integer i) div 2)
   258      else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))"
   259   by (rule partial_term_of_anything)+
   260 
   261 instantiation integer :: narrowing
   262 begin
   263 
   264 definition
   265   "narrowing_integer d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
   266     in drawn_from (map integer_of_int (around_zero i)))"
   267 
   268 instance ..
   269 
   270 end
   271 
   272 lemma [code, code del]: "partial_term_of (ty :: integer itself) t \<equiv> undefined"
   273   by (rule partial_term_of_anything)+
   274 
   275 lemma [code]:
   276   "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \<equiv>
   277     Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])"
   278   "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \<equiv>
   279     (if i mod 2 = 0
   280      then Code_Evaluation.term_of (- i div 2)
   281      else Code_Evaluation.term_of ((i + 1) div 2))"
   282   by (rule partial_term_of_anything)+
   283 
   284 code_printing constant "Code_Evaluation.term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Haskell_Quickcheck) 
   285   "(let { t = Typerep.Typerep \"Code'_Numeral.integer\" [];
   286      mkFunT s t = Typerep.Typerep \"fun\" [s, t];
   287      numT = Typerep.Typerep \"Num.num\" [];
   288      mkBit 0 = Generated'_Code.Const \"Num.num.Bit0\" (mkFunT numT numT);
   289      mkBit 1 = Generated'_Code.Const \"Num.num.Bit1\" (mkFunT numT numT);
   290      mkNumeral 1 = Generated'_Code.Const \"Num.num.One\" numT;
   291      mkNumeral i = let { q = i `Prelude.div` 2; r = i `Prelude.mod` 2 }
   292        in Generated'_Code.App (mkBit r) (mkNumeral q);
   293      mkNumber 0 = Generated'_Code.Const \"Groups.zero'_class.zero\" t;
   294      mkNumber 1 = Generated'_Code.Const \"Groups.one'_class.one\" t;
   295      mkNumber i = if i > 0 then
   296          Generated'_Code.App
   297            (Generated'_Code.Const \"Num.numeral'_class.numeral\"
   298               (mkFunT numT t))
   299            (mkNumeral i)
   300        else
   301          Generated'_Code.App
   302            (Generated'_Code.Const \"Groups.uminus'_class.uminus\" (mkFunT t t))
   303            (mkNumber (- i)); } in mkNumber)"
   304 
   305 subsection {* The @{text find_unused_assms} command *}
   306 
   307 ML_file "Tools/Quickcheck/find_unused_assms.ML"
   308 
   309 subsection {* Closing up *}
   310 
   311 hide_type narrowing_type narrowing_term narrowing_cons property
   312 hide_const map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero
   313 hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons
   314 hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def
   315 
   316 end