src/HOL/Quickcheck_Narrowing.thy
author blanchet
Tue Sep 16 19:23:37 2014 +0200 (2014-09-16)
changeset 58350 919149921e46
parent 58334 7553a1bcecb7
child 58400 d0d3c30806b4
permissions -rw-r--r--
added 'extraction' plugins -- this might help 'HOL-Proofs'
     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 (plugins only: code extraction) narrowing_type =
    30   Narrowing_sum_of_products "narrowing_type list list"
    31 
    32 datatype (plugins only: code extraction) narrowing_term =
    33   Narrowing_variable "integer list" narrowing_type
    34 | Narrowing_constructor integer "narrowing_term list"
    35 
    36 datatype (plugins only: code extraction) (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 (plugins only: code extraction) property =
   131   Universal narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term"
   132 | Existential narrowing_type "(narrowing_term => property)" "narrowing_term => Code_Evaluation.term"
   133 | Property bool
   134 
   135 (* FIXME: hard-wired maximal depth of 100 here *)
   136 definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
   137 where
   138   "exists f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Existential ty (\<lambda> t. f (conv cs t)) (partial_term_of (TYPE('a))))"
   139 
   140 definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
   141 where
   142   "all f = (case narrowing (100 :: integer) of Narrowing_cons ty cs => Universal ty (\<lambda>t. f (conv cs t)) (partial_term_of (TYPE('a))))"
   143 
   144 subsubsection {* class @{text is_testable} *}
   145 
   146 text {* The class @{text is_testable} ensures that all necessary type instances are generated. *}
   147 
   148 class is_testable
   149 
   150 instance bool :: is_testable ..
   151 
   152 instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
   153 
   154 definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
   155 where
   156   "ensure_testable f = f"
   157 
   158 
   159 subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *}
   160 
   161 datatype (plugins only: code quickcheck_narrowing extraction) (dead 'a, dead 'b) ffun =
   162   Constant 'b
   163 | Update 'a 'b "('a, 'b) ffun"
   164 
   165 primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
   166 where
   167   "eval_ffun (Constant c) x = c"
   168 | "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
   169 
   170 hide_type (open) ffun
   171 hide_const (open) Constant Update eval_ffun
   172 
   173 datatype (plugins only: code quickcheck_narrowing extraction) (dead 'b) cfun = Constant 'b
   174 
   175 primrec eval_cfun :: "'b cfun => 'a => 'b"
   176 where
   177   "eval_cfun (Constant c) y = c"
   178 
   179 hide_type (open) cfun
   180 hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun
   181 
   182 subsubsection {* Setting up the counterexample generator *}
   183 
   184 ML_file "Tools/Quickcheck/narrowing_generators.ML"
   185 
   186 setup {* Narrowing_Generators.setup *}
   187 
   188 definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
   189 where
   190   "narrowing_dummy_partial_term_of = partial_term_of"
   191 
   192 definition narrowing_dummy_narrowing :: "integer => ('a :: narrowing) narrowing_cons"
   193 where
   194   "narrowing_dummy_narrowing = narrowing"
   195 
   196 lemma [code]:
   197   "ensure_testable f =
   198     (let
   199       x = narrowing_dummy_narrowing :: integer => bool narrowing_cons;
   200       y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term;
   201       z = (conv :: _ => _ => unit)  in f)"
   202 unfolding Let_def ensure_testable_def ..
   203 
   204 subsection {* Narrowing for sets *}
   205 
   206 instantiation set :: (narrowing) narrowing
   207 begin
   208 
   209 definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing"
   210 
   211 instance ..
   212 
   213 end
   214   
   215 subsection {* Narrowing for integers *}
   216 
   217 
   218 definition drawn_from :: "'a list \<Rightarrow> 'a narrowing_cons"
   219 where
   220   "drawn_from xs =
   221     Narrowing_cons (Narrowing_sum_of_products (map (\<lambda>_. []) xs)) (map (\<lambda>x _. x) xs)"
   222 
   223 function around_zero :: "int \<Rightarrow> int list"
   224 where
   225   "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))"
   226   by pat_completeness auto
   227 termination by (relation "measure nat") auto
   228 
   229 declare around_zero.simps [simp del]
   230 
   231 lemma length_around_zero:
   232   assumes "i >= 0" 
   233   shows "length (around_zero i) = 2 * nat i + 1"
   234 proof (induct rule: int_ge_induct [OF assms])
   235   case 1
   236   from 1 show ?case by (simp add: around_zero.simps)
   237 next
   238   case (2 i)
   239   from 2 show ?case
   240     by (simp add: around_zero.simps [of "i + 1"])
   241 qed
   242 
   243 instantiation int :: narrowing
   244 begin
   245 
   246 definition
   247   "narrowing_int d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
   248     in drawn_from (around_zero i))"
   249 
   250 instance ..
   251 
   252 end
   253 
   254 lemma [code, code del]: "partial_term_of (ty :: int itself) t \<equiv> undefined"
   255   by (rule partial_term_of_anything)+
   256 
   257 lemma [code]:
   258   "partial_term_of (ty :: int itself) (Narrowing_variable p t) \<equiv>
   259     Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])"
   260   "partial_term_of (ty :: int itself) (Narrowing_constructor i []) \<equiv>
   261     (if i mod 2 = 0
   262      then Code_Evaluation.term_of (- (int_of_integer i) div 2)
   263      else Code_Evaluation.term_of ((int_of_integer i + 1) div 2))"
   264   by (rule partial_term_of_anything)+
   265 
   266 instantiation integer :: narrowing
   267 begin
   268 
   269 definition
   270   "narrowing_integer d = (let (u :: _ \<Rightarrow> _ \<Rightarrow> unit) = conv; i = int_of_integer d
   271     in drawn_from (map integer_of_int (around_zero i)))"
   272 
   273 instance ..
   274 
   275 end
   276 
   277 lemma [code, code del]: "partial_term_of (ty :: integer itself) t \<equiv> undefined"
   278   by (rule partial_term_of_anything)+
   279 
   280 lemma [code]:
   281   "partial_term_of (ty :: integer itself) (Narrowing_variable p t) \<equiv>
   282     Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Code_Numeral.integer'') [])"
   283   "partial_term_of (ty :: integer itself) (Narrowing_constructor i []) \<equiv>
   284     (if i mod 2 = 0
   285      then Code_Evaluation.term_of (- i div 2)
   286      else Code_Evaluation.term_of ((i + 1) div 2))"
   287   by (rule partial_term_of_anything)+
   288 
   289 code_printing constant "Code_Evaluation.term_of :: integer \<Rightarrow> term" \<rightharpoonup> (Haskell_Quickcheck) 
   290   "(let { t = Typerep.Typerep \"Code'_Numeral.integer\" [];
   291      mkFunT s t = Typerep.Typerep \"fun\" [s, t];
   292      numT = Typerep.Typerep \"Num.num\" [];
   293      mkBit 0 = Generated'_Code.Const \"Num.num.Bit0\" (mkFunT numT numT);
   294      mkBit 1 = Generated'_Code.Const \"Num.num.Bit1\" (mkFunT numT numT);
   295      mkNumeral 1 = Generated'_Code.Const \"Num.num.One\" numT;
   296      mkNumeral i = let { q = i `Prelude.div` 2; r = i `Prelude.mod` 2 }
   297        in Generated'_Code.App (mkBit r) (mkNumeral q);
   298      mkNumber 0 = Generated'_Code.Const \"Groups.zero'_class.zero\" t;
   299      mkNumber 1 = Generated'_Code.Const \"Groups.one'_class.one\" t;
   300      mkNumber i = if i > 0 then
   301          Generated'_Code.App
   302            (Generated'_Code.Const \"Num.numeral'_class.numeral\"
   303               (mkFunT numT t))
   304            (mkNumeral i)
   305        else
   306          Generated'_Code.App
   307            (Generated'_Code.Const \"Groups.uminus'_class.uminus\" (mkFunT t t))
   308            (mkNumber (- i)); } in mkNumber)"
   309 
   310 subsection {* The @{text find_unused_assms} command *}
   311 
   312 ML_file "Tools/Quickcheck/find_unused_assms.ML"
   313 
   314 subsection {* Closing up *}
   315 
   316 hide_type narrowing_type narrowing_term narrowing_cons property
   317 hide_const map_cons nth error toEnum marker empty Narrowing_cons conv non_empty ensure_testable all exists drawn_from around_zero
   318 hide_const (open) Narrowing_variable Narrowing_constructor "apply" sum cons
   319 hide_fact empty_def cons_def conv.simps non_empty.simps apply_def sum_def ensure_testable_def all_def exists_def
   320 
   321 end