src/HOL/Nitpick_Examples/Manual_Nits.thy
changeset 55888 cac1add157e8
parent 55072 8488fdc4ddc0
child 55889 6bfbec3dff62
equal deleted inserted replaced
55887:25bd4745ee38 55888:cac1add157e8
     1 (*  Title:      HOL/Nitpick_Examples/Manual_Nits.thy
     1 (*  Title:      HOL/Nitpick_Examples/Manual_Nits.thy
     2     Author:     Jasmin Blanchette, TU Muenchen
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Copyright   2009-2013
     3     Copyright   2009-2014
     4 
     4 
     5 Examples from the Nitpick manual.
     5 Examples from the Nitpick manual.
     6 *)
     6 *)
     7 
     7 
     8 header {* Examples from the Nitpick Manual *}
     8 header {* Examples from the Nitpick Manual *}
    13 
    13 
    14 theory Manual_Nits
    14 theory Manual_Nits
    15 imports Main Real "~~/src/HOL/Library/Quotient_Product"
    15 imports Main Real "~~/src/HOL/Library/Quotient_Product"
    16 begin
    16 begin
    17 
    17 
    18 chapter {* 2. First Steps *}
    18 
       
    19 section {* 2. First Steps *}
    19 
    20 
    20 nitpick_params [sat_solver = MiniSat_JNI, max_threads = 1, timeout = 240]
    21 nitpick_params [sat_solver = MiniSat_JNI, max_threads = 1, timeout = 240]
       
    22 
    21 
    23 
    22 subsection {* 2.1. Propositional Logic *}
    24 subsection {* 2.1. Propositional Logic *}
    23 
    25 
    24 lemma "P \<longleftrightarrow> Q"
    26 lemma "P \<longleftrightarrow> Q"
    25 nitpick [expect = genuine]
    27 nitpick [expect = genuine]
    26 apply auto
    28 apply auto
    27 nitpick [expect = genuine] 1
    29 nitpick [expect = genuine] 1
    28 nitpick [expect = genuine] 2
    30 nitpick [expect = genuine] 2
    29 oops
    31 oops
    30 
    32 
       
    33 
    31 subsection {* 2.2. Type Variables *}
    34 subsection {* 2.2. Type Variables *}
    32 
    35 
    33 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    36 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    34 nitpick [verbose, expect = genuine]
    37 nitpick [verbose, expect = genuine]
    35 oops
    38 oops
       
    39 
    36 
    40 
    37 subsection {* 2.3. Constants *}
    41 subsection {* 2.3. Constants *}
    38 
    42 
    39 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    43 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    40 nitpick [show_consts, expect = genuine]
    44 nitpick [show_consts, expect = genuine]
    45 nitpick [expect = none]
    49 nitpick [expect = none]
    46 nitpick [card 'a = 1\<emdash>50, expect = none]
    50 nitpick [card 'a = 1\<emdash>50, expect = none]
    47 (* sledgehammer *)
    51 (* sledgehammer *)
    48 by (metis the_equality)
    52 by (metis the_equality)
    49 
    53 
       
    54 
    50 subsection {* 2.4. Skolemization *}
    55 subsection {* 2.4. Skolemization *}
    51 
    56 
    52 lemma "\<exists>g. \<forall>x. g (f x) = x \<Longrightarrow> \<forall>y. \<exists>x. y = f x"
    57 lemma "\<exists>g. \<forall>x. g (f x) = x \<Longrightarrow> \<forall>y. \<exists>x. y = f x"
    53 nitpick [expect = genuine]
    58 nitpick [expect = genuine]
    54 oops
    59 oops
    58 oops
    63 oops
    59 
    64 
    60 lemma "refl r \<Longrightarrow> sym r"
    65 lemma "refl r \<Longrightarrow> sym r"
    61 nitpick [expect = genuine]
    66 nitpick [expect = genuine]
    62 oops
    67 oops
       
    68 
    63 
    69 
    64 subsection {* 2.5. Natural Numbers and Integers *}
    70 subsection {* 2.5. Natural Numbers and Integers *}
    65 
    71 
    66 lemma "\<lbrakk>i \<le> j; n \<le> (m\<Colon>int)\<rbrakk> \<Longrightarrow> i * n + j * m \<le> i * m + j * n"
    72 lemma "\<lbrakk>i \<le> j; n \<le> (m\<Colon>int)\<rbrakk> \<Longrightarrow> i * n + j * m \<le> i * m + j * n"
    67 nitpick [expect = genuine]
    73 nitpick [expect = genuine]
    79 lemma "P (op +\<Colon>nat\<Rightarrow>nat\<Rightarrow>nat)"
    85 lemma "P (op +\<Colon>nat\<Rightarrow>nat\<Rightarrow>nat)"
    80 nitpick [card nat = 1, expect = genuine]
    86 nitpick [card nat = 1, expect = genuine]
    81 nitpick [card nat = 2, expect = none]
    87 nitpick [card nat = 2, expect = none]
    82 oops
    88 oops
    83 
    89 
       
    90 
    84 subsection {* 2.6. Inductive Datatypes *}
    91 subsection {* 2.6. Inductive Datatypes *}
    85 
    92 
    86 lemma "hd (xs @ [y, y]) = hd xs"
    93 lemma "hd (xs @ [y, y]) = hd xs"
    87 nitpick [expect = genuine]
    94 nitpick [expect = genuine]
    88 nitpick [show_consts, show_datatypes, expect = genuine]
    95 nitpick [show_consts, show_datatypes, expect = genuine]
    89 oops
    96 oops
    90 
    97 
    91 lemma "\<lbrakk>length xs = 1; length ys = 1\<rbrakk> \<Longrightarrow> xs = ys"
    98 lemma "\<lbrakk>length xs = 1; length ys = 1\<rbrakk> \<Longrightarrow> xs = ys"
    92 nitpick [show_datatypes, expect = genuine]
    99 nitpick [show_datatypes, expect = genuine]
    93 oops
   100 oops
       
   101 
    94 
   102 
    95 subsection {* 2.7. Typedefs, Records, Rationals, and Reals *}
   103 subsection {* 2.7. Typedefs, Records, Rationals, and Reals *}
    96 
   104 
    97 definition "three = {0\<Colon>nat, 1, 2}"
   105 definition "three = {0\<Colon>nat, 1, 2}"
    98 typedef three = three
   106 typedef three = three
   147 
   155 
   148 lemma "4 * x + 3 * (y\<Colon>real) \<noteq> 1 / 2"
   156 lemma "4 * x + 3 * (y\<Colon>real) \<noteq> 1 / 2"
   149 nitpick [show_datatypes, expect = genuine]
   157 nitpick [show_datatypes, expect = genuine]
   150 oops
   158 oops
   151 
   159 
       
   160 
   152 subsection {* 2.8. Inductive and Coinductive Predicates *}
   161 subsection {* 2.8. Inductive and Coinductive Predicates *}
   153 
   162 
   154 inductive even where
   163 inductive even where
   155 "even 0" |
   164 "even 0" |
   156 "even n \<Longrightarrow> even (Suc (Suc n))"
   165 "even n \<Longrightarrow> even (Suc (Suc n))"
   189 
   198 
   190 lemma "odd n \<Longrightarrow> odd (n - 2)"
   199 lemma "odd n \<Longrightarrow> odd (n - 2)"
   191 nitpick [card nat = 4, show_consts, expect = genuine]
   200 nitpick [card nat = 4, show_consts, expect = genuine]
   192 oops
   201 oops
   193 
   202 
       
   203 
   194 subsection {* 2.9. Coinductive Datatypes *}
   204 subsection {* 2.9. Coinductive Datatypes *}
   195 
   205 
   196 codatatype 'a llist = LNil | LCons 'a "'a llist"
   206 codatatype 'a llist = LNil | LCons 'a "'a llist"
   197 
   207 
   198 primcorec iterates where
   208 primcorec iterates where
   208 
   218 
   209 lemma "\<lbrakk>xs = LCons a xs; ys = LCons a ys\<rbrakk> \<Longrightarrow> xs = ys"
   219 lemma "\<lbrakk>xs = LCons a xs; ys = LCons a ys\<rbrakk> \<Longrightarrow> xs = ys"
   210 nitpick [bisim_depth = -1, show_datatypes, expect = quasi_genuine]
   220 nitpick [bisim_depth = -1, show_datatypes, expect = quasi_genuine]
   211 nitpick [card = 1\<emdash>5, expect = none]
   221 nitpick [card = 1\<emdash>5, expect = none]
   212 sorry
   222 sorry
       
   223 
   213 
   224 
   214 subsection {* 2.10. Boxing *}
   225 subsection {* 2.10. Boxing *}
   215 
   226 
   216 datatype tm = Var nat | Lam tm | App tm tm
   227 datatype tm = Var nat | Lam tm | App tm tm
   217 
   228 
   245 
   256 
   246 lemma "\<not> loose t 0 \<Longrightarrow> subst\<^sub>2 \<sigma> t = t"
   257 lemma "\<not> loose t 0 \<Longrightarrow> subst\<^sub>2 \<sigma> t = t"
   247 nitpick [card = 1\<emdash>5, expect = none]
   258 nitpick [card = 1\<emdash>5, expect = none]
   248 sorry
   259 sorry
   249 
   260 
       
   261 
   250 subsection {* 2.11. Scope Monotonicity *}
   262 subsection {* 2.11. Scope Monotonicity *}
   251 
   263 
   252 lemma "length xs = length ys \<Longrightarrow> rev (zip xs ys) = zip xs (rev ys)"
   264 lemma "length xs = length ys \<Longrightarrow> rev (zip xs ys) = zip xs (rev ys)"
   253 nitpick [verbose, expect = genuine]
   265 nitpick [verbose, expect = genuine]
   254 oops
   266 oops
   255 
   267 
   256 lemma "\<exists>g. \<forall>x\<Colon>'b. g (f x) = x \<Longrightarrow> \<forall>y\<Colon>'a. \<exists>x. y = f x"
   268 lemma "\<exists>g. \<forall>x\<Colon>'b. g (f x) = x \<Longrightarrow> \<forall>y\<Colon>'a. \<exists>x. y = f x"
   257 nitpick [mono, expect = none]
   269 nitpick [mono, expect = none]
   258 nitpick [expect = genuine]
   270 nitpick [expect = genuine]
   259 oops
   271 oops
       
   272 
   260 
   273 
   261 subsection {* 2.12. Inductive Properties *}
   274 subsection {* 2.12. Inductive Properties *}
   262 
   275 
   263 inductive_set reach where
   276 inductive_set reach where
   264 "(4\<Colon>nat) \<in> reach" |
   277 "(4\<Colon>nat) \<in> reach" |
   284 oops
   297 oops
   285 
   298 
   286 lemma "n \<in> reach \<Longrightarrow> 2 dvd n \<and> n \<ge> 4"
   299 lemma "n \<in> reach \<Longrightarrow> 2 dvd n \<and> n \<ge> 4"
   287 by (induct set: reach) arith+
   300 by (induct set: reach) arith+
   288 
   301 
   289 datatype 'a bin_tree = Leaf 'a | Branch "'a bin_tree" "'a bin_tree"
       
   290 
       
   291 primrec labels where
       
   292 "labels (Leaf a) = {a}" |
       
   293 "labels (Branch t u) = labels t \<union> labels u"
       
   294 
       
   295 primrec swap where
       
   296 "swap (Leaf c) a b =
       
   297  (if c = a then Leaf b else if c = b then Leaf a else Leaf c)" |
       
   298 "swap (Branch t u) a b = Branch (swap t a b) (swap u a b)"
       
   299 
       
   300 lemma "{a, b} \<subseteq> labels t \<Longrightarrow> labels (swap t a b) = labels t"
       
   301 (* nitpick *)
       
   302 proof (induct t)
       
   303   case Leaf thus ?case by simp
       
   304 next
       
   305   case (Branch t u) thus ?case
       
   306   (* nitpick *)
       
   307   nitpick [non_std, show_all, expect = genuine]
       
   308 oops
       
   309 
       
   310 lemma "labels (swap t a b) =
       
   311        (if a \<in> labels t then
       
   312           if b \<in> labels t then labels t else (labels t - {a}) \<union> {b}
       
   313         else
       
   314           if b \<in> labels t then (labels t - {b}) \<union> {a} else labels t)"
       
   315 (* nitpick *)
       
   316 proof (induct t)
       
   317   case Leaf thus ?case by simp
       
   318 next
       
   319   case (Branch t u) thus ?case
       
   320   nitpick [non_std, card = 1\<emdash>4, expect = none]
       
   321   by auto
       
   322 qed
       
   323 
   302 
   324 section {* 3. Case Studies *}
   303 section {* 3. Case Studies *}
   325 
   304 
   326 nitpick_params [max_potential = 0]
   305 nitpick_params [max_potential = 0]
       
   306 
   327 
   307 
   328 subsection {* 3.1. A Context-Free Grammar *}
   308 subsection {* 3.1. A Context-Free Grammar *}
   329 
   309 
   330 datatype alphabet = a | b
   310 datatype alphabet = a | b
   331 
   311 
   397 "w \<in> A\<^sub>4 \<longleftrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b] + 1"
   377 "w \<in> A\<^sub>4 \<longleftrightarrow> length [x \<leftarrow> w. x = a] = length [x \<leftarrow> w. x = b] + 1"
   398 "w \<in> B\<^sub>4 \<longleftrightarrow> length [x \<leftarrow> w. x = b] = length [x \<leftarrow> w. x = a] + 1"
   378 "w \<in> B\<^sub>4 \<longleftrightarrow> length [x \<leftarrow> w. x = b] = length [x \<leftarrow> w. x = a] + 1"
   399 nitpick [card = 1\<emdash>5, expect = none]
   379 nitpick [card = 1\<emdash>5, expect = none]
   400 sorry
   380 sorry
   401 
   381 
       
   382 
   402 subsection {* 3.2. AA Trees *}
   383 subsection {* 3.2. AA Trees *}
   403 
   384 
   404 datatype 'a aa_tree = \<Lambda> | N "'a\<Colon>linorder" nat "'a aa_tree" "'a aa_tree"
   385 datatype 'a aa_tree = \<Lambda> | N "'a\<Colon>linorder" nat "'a aa_tree" "'a aa_tree"
   405 
   386 
   406 primrec data where
   387 primrec data where