src/HOL/Nitpick.thy
author huffman
Fri Mar 30 12:32:35 2012 +0200 (2012-03-30)
changeset 47220 52426c62b5d0
parent 46950 d0181abdbdac
child 47909 5f1afeebafbc
permissions -rw-r--r--
replace lemmas eval_nat_numeral with a simpler reformulation
     1 (*  Title:      HOL/Nitpick.thy
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2008, 2009, 2010
     4 
     5 Nitpick: Yet another counterexample generator for Isabelle/HOL.
     6 *)
     7 
     8 header {* Nitpick: Yet Another Counterexample Generator for Isabelle/HOL *}
     9 
    10 theory Nitpick
    11 imports Map Quotient SAT Record
    12 keywords "nitpick" :: diag and "nitpick_params" :: thy_decl
    13 uses ("Tools/Nitpick/kodkod.ML")
    14      ("Tools/Nitpick/kodkod_sat.ML")
    15      ("Tools/Nitpick/nitpick_util.ML")
    16      ("Tools/Nitpick/nitpick_hol.ML")
    17      ("Tools/Nitpick/nitpick_preproc.ML")
    18      ("Tools/Nitpick/nitpick_mono.ML")
    19      ("Tools/Nitpick/nitpick_scope.ML")
    20      ("Tools/Nitpick/nitpick_peephole.ML")
    21      ("Tools/Nitpick/nitpick_rep.ML")
    22      ("Tools/Nitpick/nitpick_nut.ML")
    23      ("Tools/Nitpick/nitpick_kodkod.ML")
    24      ("Tools/Nitpick/nitpick_model.ML")
    25      ("Tools/Nitpick/nitpick.ML")
    26      ("Tools/Nitpick/nitpick_isar.ML")
    27      ("Tools/Nitpick/nitpick_tests.ML")
    28 begin
    29 
    30 typedecl bisim_iterator
    31 
    32 axiomatization unknown :: 'a
    33            and is_unknown :: "'a \<Rightarrow> bool"
    34            and bisim :: "bisim_iterator \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"
    35            and bisim_iterator_max :: bisim_iterator
    36            and Quot :: "'a \<Rightarrow> 'b"
    37            and safe_The :: "('a \<Rightarrow> bool) \<Rightarrow> 'a"
    38 
    39 datatype ('a, 'b) fun_box = FunBox "('a \<Rightarrow> 'b)"
    40 datatype ('a, 'b) pair_box = PairBox 'a 'b
    41 
    42 typedecl unsigned_bit
    43 typedecl signed_bit
    44 
    45 datatype 'a word = Word "('a set)"
    46 
    47 text {*
    48 Alternative definitions.
    49 *}
    50 
    51 lemma Ex1_unfold [nitpick_unfold, no_atp]:
    52 "Ex1 P \<equiv> \<exists>x. {x. P x} = {x}"
    53 apply (rule eq_reflection)
    54 apply (simp add: Ex1_def set_eq_iff)
    55 apply (rule iffI)
    56  apply (erule exE)
    57  apply (erule conjE)
    58  apply (rule_tac x = x in exI)
    59  apply (rule allI)
    60  apply (rename_tac y)
    61  apply (erule_tac x = y in allE)
    62 by auto
    63 
    64 lemma rtrancl_unfold [nitpick_unfold, no_atp]: "r\<^sup>* \<equiv> (r\<^sup>+)\<^sup>="
    65   by (simp only: rtrancl_trancl_reflcl)
    66 
    67 lemma rtranclp_unfold [nitpick_unfold, no_atp]:
    68 "rtranclp r a b \<equiv> (a = b \<or> tranclp r a b)"
    69 by (rule eq_reflection) (auto dest: rtranclpD)
    70 
    71 lemma tranclp_unfold [nitpick_unfold, no_atp]:
    72 "tranclp r a b \<equiv> (a, b) \<in> trancl {(x, y). r x y}"
    73 by (simp add: trancl_def)
    74 
    75 definition prod :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<times> 'b) set" where
    76 "prod A B = {(a, b). a \<in> A \<and> b \<in> B}"
    77 
    78 definition refl' :: "('a \<times> 'a) set \<Rightarrow> bool" where
    79 "refl' r \<equiv> \<forall>x. (x, x) \<in> r"
    80 
    81 definition wf' :: "('a \<times> 'a) set \<Rightarrow> bool" where
    82 "wf' r \<equiv> acyclic r \<and> (finite r \<or> unknown)"
    83 
    84 definition card' :: "'a set \<Rightarrow> nat" where
    85 "card' A \<equiv> if finite A then length (SOME xs. set xs = A \<and> distinct xs) else 0"
    86 
    87 definition setsum' :: "('a \<Rightarrow> 'b\<Colon>comm_monoid_add) \<Rightarrow> 'a set \<Rightarrow> 'b" where
    88 "setsum' f A \<equiv> if finite A then listsum (map f (SOME xs. set xs = A \<and> distinct xs)) else 0"
    89 
    90 inductive fold_graph' :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> bool" where
    91 "fold_graph' f z {} z" |
    92 "\<lbrakk>x \<in> A; fold_graph' f z (A - {x}) y\<rbrakk> \<Longrightarrow> fold_graph' f z A (f x y)"
    93 
    94 text {*
    95 The following lemmas are not strictly necessary but they help the
    96 \textit{special\_level} optimization.
    97 *}
    98 
    99 lemma The_psimp [nitpick_psimp, no_atp]:
   100   "P = (op =) x \<Longrightarrow> The P = x"
   101   by auto
   102 
   103 lemma Eps_psimp [nitpick_psimp, no_atp]:
   104 "\<lbrakk>P x; \<not> P y; Eps P = y\<rbrakk> \<Longrightarrow> Eps P = x"
   105 apply (case_tac "P (Eps P)")
   106  apply auto
   107 apply (erule contrapos_np)
   108 by (rule someI)
   109 
   110 lemma unit_case_unfold [nitpick_unfold, no_atp]:
   111 "unit_case x u \<equiv> x"
   112 apply (subgoal_tac "u = ()")
   113  apply (simp only: unit.cases)
   114 by simp
   115 
   116 declare unit.cases [nitpick_simp del]
   117 
   118 lemma nat_case_unfold [nitpick_unfold, no_atp]:
   119 "nat_case x f n \<equiv> if n = 0 then x else f (n - 1)"
   120 apply (rule eq_reflection)
   121 by (case_tac n) auto
   122 
   123 declare nat.cases [nitpick_simp del]
   124 
   125 lemma list_size_simp [nitpick_simp, no_atp]:
   126 "list_size f xs = (if xs = [] then 0
   127                    else Suc (f (hd xs) + list_size f (tl xs)))"
   128 "size xs = (if xs = [] then 0 else Suc (size (tl xs)))"
   129 by (case_tac xs) auto
   130 
   131 text {*
   132 Auxiliary definitions used to provide an alternative representation for
   133 @{text rat} and @{text real}.
   134 *}
   135 
   136 function nat_gcd :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
   137 [simp del]: "nat_gcd x y = (if y = 0 then x else nat_gcd y (x mod y))"
   138 by auto
   139 termination
   140 apply (relation "measure (\<lambda>(x, y). x + y + (if y > x then 1 else 0))")
   141  apply auto
   142  apply (metis mod_less_divisor xt1(9))
   143 by (metis mod_mod_trivial mod_self nat_neq_iff xt1(10))
   144 
   145 definition nat_lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
   146 "nat_lcm x y = x * y div (nat_gcd x y)"
   147 
   148 definition int_gcd :: "int \<Rightarrow> int \<Rightarrow> int" where
   149 "int_gcd x y = int (nat_gcd (nat (abs x)) (nat (abs y)))"
   150 
   151 definition int_lcm :: "int \<Rightarrow> int \<Rightarrow> int" where
   152 "int_lcm x y = int (nat_lcm (nat (abs x)) (nat (abs y)))"
   153 
   154 definition Frac :: "int \<times> int \<Rightarrow> bool" where
   155 "Frac \<equiv> \<lambda>(a, b). b > 0 \<and> int_gcd a b = 1"
   156 
   157 axiomatization Abs_Frac :: "int \<times> int \<Rightarrow> 'a"
   158            and Rep_Frac :: "'a \<Rightarrow> int \<times> int"
   159 
   160 definition zero_frac :: 'a where
   161 "zero_frac \<equiv> Abs_Frac (0, 1)"
   162 
   163 definition one_frac :: 'a where
   164 "one_frac \<equiv> Abs_Frac (1, 1)"
   165 
   166 definition num :: "'a \<Rightarrow> int" where
   167 "num \<equiv> fst o Rep_Frac"
   168 
   169 definition denom :: "'a \<Rightarrow> int" where
   170 "denom \<equiv> snd o Rep_Frac"
   171 
   172 function norm_frac :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
   173 [simp del]: "norm_frac a b = (if b < 0 then norm_frac (- a) (- b)
   174                               else if a = 0 \<or> b = 0 then (0, 1)
   175                               else let c = int_gcd a b in (a div c, b div c))"
   176 by pat_completeness auto
   177 termination by (relation "measure (\<lambda>(_, b). if b < 0 then 1 else 0)") auto
   178 
   179 definition frac :: "int \<Rightarrow> int \<Rightarrow> 'a" where
   180 "frac a b \<equiv> Abs_Frac (norm_frac a b)"
   181 
   182 definition plus_frac :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
   183 [nitpick_simp]:
   184 "plus_frac q r = (let d = int_lcm (denom q) (denom r) in
   185                     frac (num q * (d div denom q) + num r * (d div denom r)) d)"
   186 
   187 definition times_frac :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" where
   188 [nitpick_simp]:
   189 "times_frac q r = frac (num q * num r) (denom q * denom r)"
   190 
   191 definition uminus_frac :: "'a \<Rightarrow> 'a" where
   192 "uminus_frac q \<equiv> Abs_Frac (- num q, denom q)"
   193 
   194 definition number_of_frac :: "int \<Rightarrow> 'a" where
   195 "number_of_frac n \<equiv> Abs_Frac (n, 1)"
   196 
   197 definition inverse_frac :: "'a \<Rightarrow> 'a" where
   198 "inverse_frac q \<equiv> frac (denom q) (num q)"
   199 
   200 definition less_frac :: "'a \<Rightarrow> 'a \<Rightarrow> bool" where
   201 [nitpick_simp]:
   202 "less_frac q r \<longleftrightarrow> num (plus_frac q (uminus_frac r)) < 0"
   203 
   204 definition less_eq_frac :: "'a \<Rightarrow> 'a \<Rightarrow> bool" where
   205 [nitpick_simp]:
   206 "less_eq_frac q r \<longleftrightarrow> num (plus_frac q (uminus_frac r)) \<le> 0"
   207 
   208 definition of_frac :: "'a \<Rightarrow> 'b\<Colon>{inverse,ring_1}" where
   209 "of_frac q \<equiv> of_int (num q) / of_int (denom q)"
   210 
   211 use "Tools/Nitpick/kodkod.ML"
   212 use "Tools/Nitpick/kodkod_sat.ML"
   213 use "Tools/Nitpick/nitpick_util.ML"
   214 use "Tools/Nitpick/nitpick_hol.ML"
   215 use "Tools/Nitpick/nitpick_mono.ML"
   216 use "Tools/Nitpick/nitpick_preproc.ML"
   217 use "Tools/Nitpick/nitpick_scope.ML"
   218 use "Tools/Nitpick/nitpick_peephole.ML"
   219 use "Tools/Nitpick/nitpick_rep.ML"
   220 use "Tools/Nitpick/nitpick_nut.ML"
   221 use "Tools/Nitpick/nitpick_kodkod.ML"
   222 use "Tools/Nitpick/nitpick_model.ML"
   223 use "Tools/Nitpick/nitpick.ML"
   224 use "Tools/Nitpick/nitpick_isar.ML"
   225 use "Tools/Nitpick/nitpick_tests.ML"
   226 
   227 setup {*
   228   Nitpick_Isar.setup #>
   229   Nitpick_HOL.register_ersatz_global
   230     [(@{const_name card}, @{const_name card'}),
   231      (@{const_name setsum}, @{const_name setsum'}),
   232      (@{const_name fold_graph}, @{const_name fold_graph'}),
   233      (@{const_name wf}, @{const_name wf'})]
   234 *}
   235 
   236 hide_const (open) unknown is_unknown bisim bisim_iterator_max Quot safe_The
   237     FunBox PairBox Word prod refl' wf' card' setsum'
   238     fold_graph' nat_gcd nat_lcm int_gcd int_lcm Frac Abs_Frac Rep_Frac zero_frac
   239     one_frac num denom norm_frac frac plus_frac times_frac uminus_frac
   240     number_of_frac inverse_frac less_frac less_eq_frac of_frac
   241 hide_type (open) bisim_iterator fun_box pair_box unsigned_bit signed_bit word
   242 hide_fact (open) Ex1_unfold rtrancl_unfold rtranclp_unfold tranclp_unfold
   243     prod_def refl'_def wf'_def card'_def setsum'_def
   244     fold_graph'_def The_psimp Eps_psimp unit_case_unfold nat_case_unfold
   245     list_size_simp nat_gcd_def nat_lcm_def int_gcd_def int_lcm_def Frac_def
   246     zero_frac_def one_frac_def num_def denom_def norm_frac_def frac_def
   247     plus_frac_def times_frac_def uminus_frac_def number_of_frac_def
   248     inverse_frac_def less_frac_def less_eq_frac_def of_frac_def
   249 
   250 end