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