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