src/HOL/Quickcheck_Narrowing.thy
author bulwahn
Fri Jan 20 09:28:53 2012 +0100 (2012-01-20)
changeset 46308 e5abbec2697a
parent 46032 0da934e135b0
child 46589 689311986778
permissions -rw-r--r--
adding narrowing instance for sets
     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 uses
     8   ("Tools/Quickcheck/PNF_Narrowing_Engine.hs")
     9   ("Tools/Quickcheck/Narrowing_Engine.hs")
    10   ("Tools/Quickcheck/narrowing_generators.ML")
    11 begin
    12 
    13 subsection {* Counterexample generator *}
    14 
    15 text {* We create a new target for the necessary code generation setup. *}
    16 
    17 setup {* Code_Target.extend_target ("Haskell_Quickcheck", (Code_Haskell.target, K I)) *}
    18 
    19 subsubsection {* Code generation setup *}
    20 
    21 code_type typerep
    22   (Haskell_Quickcheck "Typerep")
    23 
    24 code_const Typerep.Typerep
    25   (Haskell_Quickcheck "Typerep")
    26 
    27 code_reserved Haskell_Quickcheck Typerep
    28 
    29 subsubsection {* Type @{text "code_int"} for Haskell Quickcheck's Int type *}
    30 
    31 typedef (open) code_int = "UNIV \<Colon> int set"
    32   morphisms int_of of_int by rule
    33 
    34 lemma of_int_int_of [simp]:
    35   "of_int (int_of k) = k"
    36   by (rule int_of_inverse)
    37 
    38 lemma int_of_of_int [simp]:
    39   "int_of (of_int n) = n"
    40   by (rule of_int_inverse) (rule UNIV_I)
    41 
    42 lemma code_int:
    43   "(\<And>n\<Colon>code_int. PROP P n) \<equiv> (\<And>n\<Colon>int. PROP P (of_int n))"
    44 proof
    45   fix n :: int
    46   assume "\<And>n\<Colon>code_int. PROP P n"
    47   then show "PROP P (of_int n)" .
    48 next
    49   fix n :: code_int
    50   assume "\<And>n\<Colon>int. PROP P (of_int n)"
    51   then have "PROP P (of_int (int_of n))" .
    52   then show "PROP P n" by simp
    53 qed
    54 
    55 
    56 lemma int_of_inject [simp]:
    57   "int_of k = int_of l \<longleftrightarrow> k = l"
    58   by (rule int_of_inject)
    59 
    60 lemma of_int_inject [simp]:
    61   "of_int n = of_int m \<longleftrightarrow> n = m"
    62   by (rule of_int_inject) (rule UNIV_I)+
    63 
    64 instantiation code_int :: equal
    65 begin
    66 
    67 definition
    68   "HOL.equal k l \<longleftrightarrow> HOL.equal (int_of k) (int_of l)"
    69 
    70 instance proof
    71 qed (auto simp add: equal_code_int_def equal_int_def eq_int_refl)
    72 
    73 end
    74 
    75 instantiation code_int :: number
    76 begin
    77 
    78 definition
    79   "number_of = of_int"
    80 
    81 instance ..
    82 
    83 end
    84 
    85 lemma int_of_number [simp]:
    86   "int_of (number_of k) = number_of k"
    87   by (simp add: number_of_code_int_def number_of_is_id)
    88 
    89 
    90 definition nat_of :: "code_int => nat"
    91 where
    92   "nat_of i = nat (int_of i)"
    93 
    94 
    95 code_datatype "number_of \<Colon> int \<Rightarrow> code_int"
    96   
    97   
    98 instantiation code_int :: "{minus, linordered_semidom, semiring_div, linorder}"
    99 begin
   100 
   101 definition [simp, code del]:
   102   "0 = of_int 0"
   103 
   104 definition [simp, code del]:
   105   "1 = of_int 1"
   106 
   107 definition [simp, code del]:
   108   "n + m = of_int (int_of n + int_of m)"
   109 
   110 definition [simp, code del]:
   111   "n - m = of_int (int_of n - int_of m)"
   112 
   113 definition [simp, code del]:
   114   "n * m = of_int (int_of n * int_of m)"
   115 
   116 definition [simp, code del]:
   117   "n div m = of_int (int_of n div int_of m)"
   118 
   119 definition [simp, code del]:
   120   "n mod m = of_int (int_of n mod int_of m)"
   121 
   122 definition [simp, code del]:
   123   "n \<le> m \<longleftrightarrow> int_of n \<le> int_of m"
   124 
   125 definition [simp, code del]:
   126   "n < m \<longleftrightarrow> int_of n < int_of m"
   127 
   128 
   129 instance proof
   130 qed (auto simp add: code_int left_distrib zmult_zless_mono2)
   131 
   132 end
   133 
   134 lemma zero_code_int_code [code, code_unfold]:
   135   "(0\<Colon>code_int) = Numeral0"
   136   by (simp add: number_of_code_int_def Pls_def)
   137 
   138 lemma one_code_int_code [code, code_unfold]:
   139   "(1\<Colon>code_int) = Numeral1"
   140   by (simp add: number_of_code_int_def Pls_def Bit1_def)
   141 
   142 definition div_mod_code_int :: "code_int \<Rightarrow> code_int \<Rightarrow> code_int \<times> code_int" where
   143   [code del]: "div_mod_code_int n m = (n div m, n mod m)"
   144 
   145 lemma [code]:
   146   "div_mod_code_int n m = (if m = 0 then (0, n) else (n div m, n mod m))"
   147   unfolding div_mod_code_int_def by auto
   148 
   149 lemma [code]:
   150   "n div m = fst (div_mod_code_int n m)"
   151   unfolding div_mod_code_int_def by simp
   152 
   153 lemma [code]:
   154   "n mod m = snd (div_mod_code_int n m)"
   155   unfolding div_mod_code_int_def by simp
   156 
   157 lemma int_of_code [code]:
   158   "int_of k = (if k = 0 then 0
   159     else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
   160 proof -
   161   have 1: "(int_of k div 2) * 2 + int_of k mod 2 = int_of k" 
   162     by (rule mod_div_equality)
   163   have "int_of k mod 2 = 0 \<or> int_of k mod 2 = 1" by auto
   164   from this show ?thesis
   165     apply auto
   166     apply (insert 1) by (auto simp add: mult_ac)
   167 qed
   168 
   169 
   170 code_instance code_numeral :: equal
   171   (Haskell_Quickcheck -)
   172 
   173 setup {* fold (Numeral.add_code @{const_name number_code_int_inst.number_of_code_int}
   174   false Code_Printer.literal_numeral) ["Haskell_Quickcheck"]  *}
   175 
   176 code_const "0 \<Colon> code_int"
   177   (Haskell_Quickcheck "0")
   178 
   179 code_const "1 \<Colon> code_int"
   180   (Haskell_Quickcheck "1")
   181 
   182 code_const "minus \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> code_int"
   183   (Haskell_Quickcheck "(_/ -/ _)")
   184 
   185 code_const div_mod_code_int
   186   (Haskell_Quickcheck "divMod")
   187 
   188 code_const "HOL.equal \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
   189   (Haskell_Quickcheck infix 4 "==")
   190 
   191 code_const "op \<le> \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
   192   (Haskell_Quickcheck infix 4 "<=")
   193 
   194 code_const "op < \<Colon> code_int \<Rightarrow> code_int \<Rightarrow> bool"
   195   (Haskell_Quickcheck infix 4 "<")
   196 
   197 code_type code_int
   198   (Haskell_Quickcheck "Int")
   199 
   200 code_abort of_int
   201 
   202 subsubsection {* Narrowing's deep representation of types and terms *}
   203 
   204 datatype narrowing_type = SumOfProd "narrowing_type list list"
   205 datatype narrowing_term = Var "code_int list" narrowing_type | Ctr code_int "narrowing_term list"
   206 datatype 'a cons = C narrowing_type "(narrowing_term list => 'a) list"
   207 
   208 primrec map_cons :: "('a => 'b) => 'a cons => 'b cons"
   209 where
   210   "map_cons f (C ty cs) = C ty (map (%c. f o c) cs)"
   211 
   212 subsubsection {* From narrowing's deep representation of terms to @{theory Code_Evaluation}'s terms *}
   213 
   214 class partial_term_of = typerep +
   215   fixes partial_term_of :: "'a itself => narrowing_term => Code_Evaluation.term"
   216 
   217 lemma partial_term_of_anything: "partial_term_of x nt \<equiv> t"
   218   by (rule eq_reflection) (cases "partial_term_of x nt", cases t, simp)
   219  
   220 subsubsection {* Auxilary functions for Narrowing *}
   221 
   222 consts nth :: "'a list => code_int => 'a"
   223 
   224 code_const nth (Haskell_Quickcheck infixl 9  "!!")
   225 
   226 consts error :: "char list => 'a"
   227 
   228 code_const error (Haskell_Quickcheck "error")
   229 
   230 consts toEnum :: "code_int => char"
   231 
   232 code_const toEnum (Haskell_Quickcheck "toEnum")
   233 
   234 consts marker :: "char"
   235 
   236 code_const marker (Haskell_Quickcheck "''\\0'")
   237 
   238 subsubsection {* Narrowing's basic operations *}
   239 
   240 type_synonym 'a narrowing = "code_int => 'a cons"
   241 
   242 definition empty :: "'a narrowing"
   243 where
   244   "empty d = C (SumOfProd []) []"
   245   
   246 definition cons :: "'a => 'a narrowing"
   247 where
   248   "cons a d = (C (SumOfProd [[]]) [(%_. a)])"
   249 
   250 fun conv :: "(narrowing_term list => 'a) list => narrowing_term => 'a"
   251 where
   252   "conv cs (Var p _) = error (marker # map toEnum p)"
   253 | "conv cs (Ctr i xs) = (nth cs i) xs"
   254 
   255 fun nonEmpty :: "narrowing_type => bool"
   256 where
   257   "nonEmpty (SumOfProd ps) = (\<not> (List.null ps))"
   258 
   259 definition "apply" :: "('a => 'b) narrowing => 'a narrowing => 'b narrowing"
   260 where
   261   "apply f a d =
   262      (case f d of C (SumOfProd ps) cfs =>
   263        case a (d - 1) of C ta cas =>
   264        let
   265          shallow = (d > 0 \<and> nonEmpty ta);
   266          cs = [(%xs'. (case xs' of [] => undefined | x # xs => cf xs (conv cas x))). shallow, cf <- cfs]
   267        in C (SumOfProd [ta # p. shallow, p <- ps]) cs)"
   268 
   269 definition sum :: "'a narrowing => 'a narrowing => 'a narrowing"
   270 where
   271   "sum a b d =
   272     (case a d of C (SumOfProd ssa) ca => 
   273       case b d of C (SumOfProd ssb) cb =>
   274       C (SumOfProd (ssa @ ssb)) (ca @ cb))"
   275 
   276 lemma [fundef_cong]:
   277   assumes "a d = a' d" "b d = b' d" "d = d'"
   278   shows "sum a b d = sum a' b' d'"
   279 using assms unfolding sum_def by (auto split: cons.split narrowing_type.split)
   280 
   281 lemma [fundef_cong]:
   282   assumes "f d = f' d" "(\<And>d'. 0 <= d' & d' < d ==> a d' = a' d')"
   283   assumes "d = d'"
   284   shows "apply f a d = apply f' a' d'"
   285 proof -
   286   note assms moreover
   287   have "int_of (of_int 0) < int_of d' ==> int_of (of_int 0) <= int_of (of_int (int_of d' - int_of (of_int 1)))"
   288     by (simp add: of_int_inverse)
   289   moreover
   290   have "int_of (of_int (int_of d' - int_of (of_int 1))) < int_of d'"
   291     by (simp add: of_int_inverse)
   292   ultimately show ?thesis
   293     unfolding apply_def by (auto split: cons.split narrowing_type.split simp add: Let_def)
   294 qed
   295 
   296 subsubsection {* Narrowing generator type class *}
   297 
   298 class narrowing =
   299   fixes narrowing :: "code_int => 'a cons"
   300 
   301 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
   302 
   303 (* FIXME: hard-wired maximal depth of 100 here *)
   304 definition exists :: "('a :: {narrowing, partial_term_of} => property) => property"
   305 where
   306   "exists f = (case narrowing (100 :: code_int) of C ty cs => Existential ty (\<lambda> t. f (conv cs t)) (partial_term_of (TYPE('a))))"
   307 
   308 definition "all" :: "('a :: {narrowing, partial_term_of} => property) => property"
   309 where
   310   "all f = (case narrowing (100 :: code_int) of C ty cs => Universal ty (\<lambda>t. f (conv cs t)) (partial_term_of (TYPE('a))))"
   311 
   312 subsubsection {* class @{text is_testable} *}
   313 
   314 text {* The class @{text is_testable} ensures that all necessary type instances are generated. *}
   315 
   316 class is_testable
   317 
   318 instance bool :: is_testable ..
   319 
   320 instance "fun" :: ("{term_of, narrowing, partial_term_of}", is_testable) is_testable ..
   321 
   322 definition ensure_testable :: "'a :: is_testable => 'a :: is_testable"
   323 where
   324   "ensure_testable f = f"
   325 
   326 
   327 subsubsection {* Defining a simple datatype to represent functions in an incomplete and redundant way *}
   328 
   329 datatype ('a, 'b) ffun = Constant 'b | Update 'a 'b "('a, 'b) ffun"
   330 
   331 primrec eval_ffun :: "('a, 'b) ffun => 'a => 'b"
   332 where
   333   "eval_ffun (Constant c) x = c"
   334 | "eval_ffun (Update x' y f) x = (if x = x' then y else eval_ffun f x)"
   335 
   336 hide_type (open) ffun
   337 hide_const (open) Constant Update eval_ffun
   338 
   339 datatype 'b cfun = Constant 'b
   340 
   341 primrec eval_cfun :: "'b cfun => 'a => 'b"
   342 where
   343   "eval_cfun (Constant c) y = c"
   344 
   345 hide_type (open) cfun
   346 hide_const (open) Constant eval_cfun Abs_cfun Rep_cfun
   347 
   348 subsubsection {* Setting up the counterexample generator *}
   349 
   350 use "Tools/Quickcheck/narrowing_generators.ML"
   351 
   352 setup {* Narrowing_Generators.setup *}
   353 
   354 definition narrowing_dummy_partial_term_of :: "('a :: partial_term_of) itself => narrowing_term => term"
   355 where
   356   "narrowing_dummy_partial_term_of = partial_term_of"
   357 
   358 definition narrowing_dummy_narrowing :: "code_int => ('a :: narrowing) cons"
   359 where
   360   "narrowing_dummy_narrowing = narrowing"
   361 
   362 lemma [code]:
   363   "ensure_testable f =
   364     (let
   365       x = narrowing_dummy_narrowing :: code_int => bool cons;
   366       y = narrowing_dummy_partial_term_of :: bool itself => narrowing_term => term;
   367       z = (conv :: _ => _ => unit)  in f)"
   368 unfolding Let_def ensure_testable_def ..
   369 
   370 subsection {* Narrowing for sets *}
   371 
   372 instantiation set :: (narrowing) narrowing
   373 begin
   374 
   375 definition "narrowing_set = Quickcheck_Narrowing.apply (Quickcheck_Narrowing.cons set) narrowing"
   376 
   377 instance ..
   378 
   379 end
   380   
   381 subsection {* Narrowing for integers *}
   382 
   383 
   384 definition drawn_from :: "'a list => 'a cons"
   385 where "drawn_from xs = C (SumOfProd (map (%_. []) xs)) (map (%x y. x) xs)"
   386 
   387 function around_zero :: "int => int list"
   388 where
   389   "around_zero i = (if i < 0 then [] else (if i = 0 then [0] else around_zero (i - 1) @ [i, -i]))"
   390 by pat_completeness auto
   391 termination by (relation "measure nat") auto
   392 
   393 declare around_zero.simps[simp del]
   394 
   395 lemma length_around_zero:
   396   assumes "i >= 0" 
   397   shows "length (around_zero i) = 2 * nat i + 1"
   398 proof (induct rule: int_ge_induct[OF assms])
   399   case 1
   400   from 1 show ?case by (simp add: around_zero.simps)
   401 next
   402   case (2 i)
   403   from 2 show ?case
   404     by (simp add: around_zero.simps[of "i + 1"])
   405 qed
   406 
   407 instantiation int :: narrowing
   408 begin
   409 
   410 definition
   411   "narrowing_int d = (let (u :: _ => _ => unit) = conv; i = Quickcheck_Narrowing.int_of d in drawn_from (around_zero i))"
   412 
   413 instance ..
   414 
   415 end
   416 
   417 lemma [code, code del]: "partial_term_of (ty :: int itself) t == undefined"
   418 by (rule partial_term_of_anything)+
   419 
   420 lemma [code]:
   421   "partial_term_of (ty :: int itself) (Var p t) == Code_Evaluation.Free (STR ''_'') (Typerep.Typerep (STR ''Int.int'') [])"
   422   "partial_term_of (ty :: int itself) (Ctr i []) == (if i mod 2 = 0 then
   423      Code_Evaluation.term_of (- (int_of i) div 2) else Code_Evaluation.term_of ((int_of i + 1) div 2))"
   424 by (rule partial_term_of_anything)+
   425 
   426 text {* Defining integers by positive and negative copy of naturals *}
   427 (*
   428 datatype simple_int = Positive nat | Negative nat
   429 
   430 primrec int_of_simple_int :: "simple_int => int"
   431 where
   432   "int_of_simple_int (Positive n) = int n"
   433 | "int_of_simple_int (Negative n) = (-1 - int n)"
   434 
   435 instantiation int :: narrowing
   436 begin
   437 
   438 definition narrowing_int :: "code_int => int cons"
   439 where
   440   "narrowing_int d = map_cons int_of_simple_int ((narrowing :: simple_int narrowing) d)"
   441 
   442 instance ..
   443 
   444 end
   445 
   446 text {* printing the partial terms *}
   447 
   448 lemma [code]:
   449   "partial_term_of (ty :: int itself) t == Code_Evaluation.App (Code_Evaluation.Const (STR ''Quickcheck_Narrowing.int_of_simple_int'')
   450      (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''Quickcheck_Narrowing.simple_int'') [], Typerep.Typerep (STR ''Int.int'') []])) (partial_term_of (TYPE(simple_int)) t)"
   451 by (rule partial_term_of_anything)
   452 
   453 *)
   454 
   455 hide_type code_int narrowing_type narrowing_term cons property
   456 hide_const int_of of_int nat_of map_cons nth error toEnum marker empty C conv nonEmpty ensure_testable all exists drawn_from around_zero
   457 hide_const (open) Var Ctr "apply" sum cons
   458 hide_fact empty_def cons_def conv.simps nonEmpty.simps apply_def sum_def ensure_testable_def all_def exists_def
   459 
   460 
   461 end