src/HOL/Nitpick_Examples/Manual_Nits.thy
changeset 63167 0909deb8059b
parent 61076 bdc1e2f0a86a
child 66453 cc19f7ca2ed6
equal deleted inserted replaced
63166:143f58bb34f9 63167:0909deb8059b
     3     Copyright   2009-2014
     3     Copyright   2009-2014
     4 
     4 
     5 Examples from the Nitpick manual.
     5 Examples from the Nitpick manual.
     6 *)
     6 *)
     7 
     7 
     8 section {* Examples from the Nitpick Manual *}
     8 section \<open>Examples from the Nitpick Manual\<close>
     9 
     9 
    10 (* The "expect" arguments to Nitpick in this theory and the other example
    10 (* The "expect" arguments to Nitpick in this theory and the other example
    11    theories are there so that the example can also serve as a regression test
    11    theories are there so that the example can also serve as a regression test
    12    suite. *)
    12    suite. *)
    13 
    13 
    14 theory Manual_Nits
    14 theory Manual_Nits
    15 imports Real "~~/src/HOL/Library/Quotient_Product"
    15 imports Real "~~/src/HOL/Library/Quotient_Product"
    16 begin
    16 begin
    17 
    17 
    18 section {* 2. First Steps *}
    18 section \<open>2. First Steps\<close>
    19 
    19 
    20 nitpick_params [sat_solver = MiniSat_JNI, max_threads = 1, timeout = 240]
    20 nitpick_params [sat_solver = MiniSat_JNI, max_threads = 1, timeout = 240]
    21 
    21 
    22 
    22 
    23 subsection {* 2.1. Propositional Logic *}
    23 subsection \<open>2.1. Propositional Logic\<close>
    24 
    24 
    25 lemma "P \<longleftrightarrow> Q"
    25 lemma "P \<longleftrightarrow> Q"
    26 nitpick [expect = genuine]
    26 nitpick [expect = genuine]
    27 apply auto
    27 apply auto
    28 nitpick [expect = genuine] 1
    28 nitpick [expect = genuine] 1
    29 nitpick [expect = genuine] 2
    29 nitpick [expect = genuine] 2
    30 oops
    30 oops
    31 
    31 
    32 
    32 
    33 subsection {* 2.2. Type Variables *}
    33 subsection \<open>2.2. Type Variables\<close>
    34 
    34 
    35 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    35 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    36 nitpick [verbose, expect = genuine]
    36 nitpick [verbose, expect = genuine]
    37 oops
    37 oops
    38 
    38 
    39 
    39 
    40 subsection {* 2.3. Constants *}
    40 subsection \<open>2.3. Constants\<close>
    41 
    41 
    42 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    42 lemma "x \<in> A \<Longrightarrow> (THE y. y \<in> A) \<in> A"
    43 nitpick [show_consts, expect = genuine]
    43 nitpick [show_consts, expect = genuine]
    44 nitpick [dont_specialize, show_consts, expect = genuine]
    44 nitpick [dont_specialize, show_consts, expect = genuine]
    45 oops
    45 oops
    49 nitpick [card 'a = 1-50, expect = none]
    49 nitpick [card 'a = 1-50, expect = none]
    50 (* sledgehammer *)
    50 (* sledgehammer *)
    51 by (metis the_equality)
    51 by (metis the_equality)
    52 
    52 
    53 
    53 
    54 subsection {* 2.4. Skolemization *}
    54 subsection \<open>2.4. Skolemization\<close>
    55 
    55 
    56 lemma "\<exists>g. \<forall>x. g (f x) = x \<Longrightarrow> \<forall>y. \<exists>x. y = f x"
    56 lemma "\<exists>g. \<forall>x. g (f x) = x \<Longrightarrow> \<forall>y. \<exists>x. y = f x"
    57 nitpick [expect = genuine]
    57 nitpick [expect = genuine]
    58 oops
    58 oops
    59 
    59 
    64 lemma "refl r \<Longrightarrow> sym r"
    64 lemma "refl r \<Longrightarrow> sym r"
    65 nitpick [expect = genuine]
    65 nitpick [expect = genuine]
    66 oops
    66 oops
    67 
    67 
    68 
    68 
    69 subsection {* 2.5. Natural Numbers and Integers *}
    69 subsection \<open>2.5. Natural Numbers and Integers\<close>
    70 
    70 
    71 lemma "\<lbrakk>i \<le> j; n \<le> (m::int)\<rbrakk> \<Longrightarrow> i * n + j * m \<le> i * m + j * n"
    71 lemma "\<lbrakk>i \<le> j; n \<le> (m::int)\<rbrakk> \<Longrightarrow> i * n + j * m \<le> i * m + j * n"
    72 nitpick [expect = genuine]
    72 nitpick [expect = genuine]
    73 nitpick [binary_ints, bits = 16, expect = genuine]
    73 nitpick [binary_ints, bits = 16, expect = genuine]
    74 oops
    74 oops
    85 nitpick [card nat = 1, expect = genuine]
    85 nitpick [card nat = 1, expect = genuine]
    86 nitpick [card nat = 2, expect = none]
    86 nitpick [card nat = 2, expect = none]
    87 oops
    87 oops
    88 
    88 
    89 
    89 
    90 subsection {* 2.6. Inductive Datatypes *}
    90 subsection \<open>2.6. Inductive Datatypes\<close>
    91 
    91 
    92 lemma "hd (xs @ [y, y]) = hd xs"
    92 lemma "hd (xs @ [y, y]) = hd xs"
    93 nitpick [expect = genuine]
    93 nitpick [expect = genuine]
    94 nitpick [show_consts, show_types, expect = genuine]
    94 nitpick [show_consts, show_types, expect = genuine]
    95 oops
    95 oops
    97 lemma "\<lbrakk>length xs = 1; length ys = 1\<rbrakk> \<Longrightarrow> xs = ys"
    97 lemma "\<lbrakk>length xs = 1; length ys = 1\<rbrakk> \<Longrightarrow> xs = ys"
    98 nitpick [show_types, expect = genuine]
    98 nitpick [show_types, expect = genuine]
    99 oops
    99 oops
   100 
   100 
   101 
   101 
   102 subsection {* 2.7. Typedefs, Records, Rationals, and Reals *}
   102 subsection \<open>2.7. Typedefs, Records, Rationals, and Reals\<close>
   103 
   103 
   104 definition "three = {0::nat, 1, 2}"
   104 definition "three = {0::nat, 1, 2}"
   105 typedef three = three
   105 typedef three = three
   106   unfolding three_def by blast
   106   unfolding three_def by blast
   107 
   107 
   127 
   127 
   128 lemma "add x y = add x x"
   128 lemma "add x y = add x x"
   129 nitpick [show_types, expect = genuine]
   129 nitpick [show_types, expect = genuine]
   130 oops
   130 oops
   131 
   131 
   132 ML {*
   132 ML \<open>
   133 fun my_int_postproc _ _ _ T (Const _ $ (Const _ $ t1 $ t2)) =
   133 fun my_int_postproc _ _ _ T (Const _ $ (Const _ $ t1 $ t2)) =
   134     HOLogic.mk_number T (snd (HOLogic.dest_number t1)
   134     HOLogic.mk_number T (snd (HOLogic.dest_number t1)
   135                          - snd (HOLogic.dest_number t2))
   135                          - snd (HOLogic.dest_number t2))
   136   | my_int_postproc _ _ _ _ t = t
   136   | my_int_postproc _ _ _ _ t = t
   137 *}
   137 \<close>
   138 
   138 
   139 declaration {*
   139 declaration \<open>
   140 Nitpick_Model.register_term_postprocessor @{typ my_int} my_int_postproc
   140 Nitpick_Model.register_term_postprocessor @{typ my_int} my_int_postproc
   141 *}
   141 \<close>
   142 
   142 
   143 lemma "add x y = add x x"
   143 lemma "add x y = add x x"
   144 nitpick [show_types]
   144 nitpick [show_types]
   145 oops
   145 oops
   146 
   146 
   155 lemma "4 * x + 3 * (y::real) \<noteq> 1 / 2"
   155 lemma "4 * x + 3 * (y::real) \<noteq> 1 / 2"
   156 nitpick [show_types, expect = genuine]
   156 nitpick [show_types, expect = genuine]
   157 oops
   157 oops
   158 
   158 
   159 
   159 
   160 subsection {* 2.8. Inductive and Coinductive Predicates *}
   160 subsection \<open>2.8. Inductive and Coinductive Predicates\<close>
   161 
   161 
   162 inductive even where
   162 inductive even where
   163 "even 0" |
   163 "even 0" |
   164 "even n \<Longrightarrow> even (Suc (Suc n))"
   164 "even n \<Longrightarrow> even (Suc (Suc n))"
   165 
   165 
   198 lemma "odd n \<Longrightarrow> odd (n - 2)"
   198 lemma "odd n \<Longrightarrow> odd (n - 2)"
   199 nitpick [card nat = 4, show_consts, expect = genuine]
   199 nitpick [card nat = 4, show_consts, expect = genuine]
   200 oops
   200 oops
   201 
   201 
   202 
   202 
   203 subsection {* 2.9. Coinductive Datatypes *}
   203 subsection \<open>2.9. Coinductive Datatypes\<close>
   204 
   204 
   205 codatatype 'a llist = LNil | LCons 'a "'a llist"
   205 codatatype 'a llist = LNil | LCons 'a "'a llist"
   206 
   206 
   207 primcorec iterates where
   207 primcorec iterates where
   208 "iterates f a = LCons a (iterates f (f a))"
   208 "iterates f a = LCons a (iterates f (f a))"
   219 nitpick [bisim_depth = -1, show_types, expect = quasi_genuine]
   219 nitpick [bisim_depth = -1, show_types, expect = quasi_genuine]
   220 nitpick [card = 1-5, expect = none]
   220 nitpick [card = 1-5, expect = none]
   221 sorry
   221 sorry
   222 
   222 
   223 
   223 
   224 subsection {* 2.10. Boxing *}
   224 subsection \<open>2.10. Boxing\<close>
   225 
   225 
   226 datatype tm = Var nat | Lam tm | App tm tm
   226 datatype tm = Var nat | Lam tm | App tm tm
   227 
   227 
   228 primrec lift where
   228 primrec lift where
   229 "lift (Var j) k = Var (if j < k then j else j + 1)" |
   229 "lift (Var j) k = Var (if j < k then j else j + 1)" |
   256 lemma "\<not> loose t 0 \<Longrightarrow> subst\<^sub>2 \<sigma> t = t"
   256 lemma "\<not> loose t 0 \<Longrightarrow> subst\<^sub>2 \<sigma> t = t"
   257 nitpick [card = 1-5, expect = none]
   257 nitpick [card = 1-5, expect = none]
   258 sorry
   258 sorry
   259 
   259 
   260 
   260 
   261 subsection {* 2.11. Scope Monotonicity *}
   261 subsection \<open>2.11. Scope Monotonicity\<close>
   262 
   262 
   263 lemma "length xs = length ys \<Longrightarrow> rev (zip xs ys) = zip xs (rev ys)"
   263 lemma "length xs = length ys \<Longrightarrow> rev (zip xs ys) = zip xs (rev ys)"
   264 nitpick [verbose, expect = genuine]
   264 nitpick [verbose, expect = genuine]
   265 oops
   265 oops
   266 
   266 
   268 nitpick [mono, expect = none]
   268 nitpick [mono, expect = none]
   269 nitpick [expect = genuine]
   269 nitpick [expect = genuine]
   270 oops
   270 oops
   271 
   271 
   272 
   272 
   273 subsection {* 2.12. Inductive Properties *}
   273 subsection \<open>2.12. Inductive Properties\<close>
   274 
   274 
   275 inductive_set reach where
   275 inductive_set reach where
   276 "(4::nat) \<in> reach" |
   276 "(4::nat) \<in> reach" |
   277 "n \<in> reach \<Longrightarrow> n < 4 \<Longrightarrow> 3 * n + 1 \<in> reach" |
   277 "n \<in> reach \<Longrightarrow> n < 4 \<Longrightarrow> 3 * n + 1 \<in> reach" |
   278 "n \<in> reach \<Longrightarrow> n + 2 \<in> reach"
   278 "n \<in> reach \<Longrightarrow> n + 2 \<in> reach"
   297 
   297 
   298 lemma "n \<in> reach \<Longrightarrow> 2 dvd n \<and> n \<ge> 4"
   298 lemma "n \<in> reach \<Longrightarrow> 2 dvd n \<and> n \<ge> 4"
   299 by (induct set: reach) arith+
   299 by (induct set: reach) arith+
   300 
   300 
   301 
   301 
   302 section {* 3. Case Studies *}
   302 section \<open>3. Case Studies\<close>
   303 
   303 
   304 nitpick_params [max_potential = 0]
   304 nitpick_params [max_potential = 0]
   305 
   305 
   306 
   306 
   307 subsection {* 3.1. A Context-Free Grammar *}
   307 subsection \<open>3.1. A Context-Free Grammar\<close>
   308 
   308 
   309 datatype alphabet = a | b
   309 datatype alphabet = a | b
   310 
   310 
   311 inductive_set S\<^sub>1 and A\<^sub>1 and B\<^sub>1 where
   311 inductive_set S\<^sub>1 and A\<^sub>1 and B\<^sub>1 where
   312   "[] \<in> S\<^sub>1"
   312   "[] \<in> S\<^sub>1"
   377 "w \<in> B\<^sub>4 \<longleftrightarrow> length [x \<leftarrow> w. x = b] = length [x \<leftarrow> w. x = a] + 1"
   377 "w \<in> B\<^sub>4 \<longleftrightarrow> length [x \<leftarrow> w. x = b] = length [x \<leftarrow> w. x = a] + 1"
   378 nitpick [card = 1-5, expect = none]
   378 nitpick [card = 1-5, expect = none]
   379 sorry
   379 sorry
   380 
   380 
   381 
   381 
   382 subsection {* 3.2. AA Trees *}
   382 subsection \<open>3.2. AA Trees\<close>
   383 
   383 
   384 datatype 'a aa_tree = \<Lambda> | N "'a::linorder" nat "'a aa_tree" "'a aa_tree"
   384 datatype 'a aa_tree = \<Lambda> | N "'a::linorder" nat "'a aa_tree" "'a aa_tree"
   385 
   385 
   386 primrec data where
   386 primrec data where
   387 "data \<Lambda> = undefined" |
   387 "data \<Lambda> = undefined" |