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