doc-src/Nitpick/nitpick.tex
author blanchet
Wed, 10 Mar 2010 14:21:01 +0100
changeset 35695 80b2c22f8f00
parent 35665 ff2bf50505ab
child 35710 58acd48904bc
permissions -rw-r--r--
fixed soundness bug in Nitpick
     1 \documentclass[a4paper,12pt]{article}
     2 \usepackage[T1]{fontenc}
     3 \usepackage{amsmath}
     4 \usepackage{amssymb}
     5 \usepackage[english,french]{babel}
     6 \usepackage{color}
     7 \usepackage{footmisc}
     8 \usepackage{graphicx}
     9 %\usepackage{mathpazo}
    10 \usepackage{multicol}
    11 \usepackage{stmaryrd}
    12 %\usepackage[scaled=.85]{beramono}
    13 \usepackage{../iman,../pdfsetup}
    14 
    15 %\oddsidemargin=4.6mm
    16 %\evensidemargin=4.6mm
    17 %\textwidth=150mm
    18 %\topmargin=4.6mm
    19 %\headheight=0mm
    20 %\headsep=0mm
    21 %\textheight=234mm
    22 
    23 \def\Colon{\mathord{:\mkern-1.5mu:}}
    24 %\def\lbrakk{\mathopen{\lbrack\mkern-3.25mu\lbrack}}
    25 %\def\rbrakk{\mathclose{\rbrack\mkern-3.255mu\rbrack}}
    26 \def\lparr{\mathopen{(\mkern-4mu\mid}}
    27 \def\rparr{\mathclose{\mid\mkern-4mu)}}
    28 
    29 \def\unk{{?}}
    30 \def\undef{(\lambda x.\; \unk)}
    31 %\def\unr{\textit{others}}
    32 \def\unr{\ldots}
    33 \def\Abs#1{\hbox{\rm{\flqq}}{\,#1\,}\hbox{\rm{\frqq}}}
    34 \def\Q{{\smash{\lower.2ex\hbox{$\scriptstyle?$}}}}
    35 
    36 \hyphenation{Mini-Sat size-change First-Steps grand-parent nit-pick
    37 counter-example counter-examples data-type data-types co-data-type 
    38 co-data-types in-duc-tive co-in-duc-tive}
    39 
    40 \urlstyle{tt}
    41 
    42 \begin{document}
    43 
    44 \selectlanguage{english}
    45 
    46 \title{\includegraphics[scale=0.5]{isabelle_nitpick} \\[4ex]
    47 Picking Nits \\[\smallskipamount]
    48 \Large A User's Guide to Nitpick for Isabelle/HOL}
    49 \author{\hbox{} \\
    50 Jasmin Christian Blanchette \\
    51 {\normalsize Institut f\"ur Informatik, Technische Universit\"at M\"unchen} \\
    52 \hbox{}}
    53 
    54 \maketitle
    55 
    56 \tableofcontents
    57 
    58 \setlength{\parskip}{.7em plus .2em minus .1em}
    59 \setlength{\parindent}{0pt}
    60 \setlength{\abovedisplayskip}{\parskip}
    61 \setlength{\abovedisplayshortskip}{.9\parskip}
    62 \setlength{\belowdisplayskip}{\parskip}
    63 \setlength{\belowdisplayshortskip}{.9\parskip}
    64 
    65 % General-purpose enum environment with correct spacing
    66 \newenvironment{enum}%
    67     {\begin{list}{}{%
    68         \setlength{\topsep}{.1\parskip}%
    69         \setlength{\partopsep}{.1\parskip}%
    70         \setlength{\itemsep}{\parskip}%
    71         \advance\itemsep by-\parsep}}
    72     {\end{list}}
    73 
    74 \def\pre{\begingroup\vskip0pt plus1ex\advance\leftskip by\leftmargin
    75 \advance\rightskip by\leftmargin}
    76 \def\post{\vskip0pt plus1ex\endgroup}
    77 
    78 \def\prew{\pre\advance\rightskip by-\leftmargin}
    79 \def\postw{\post}
    80 
    81 \section{Introduction}
    82 \label{introduction}
    83 
    84 Nitpick \cite{blanchette-nipkow-2009} is a counterexample generator for
    85 Isabelle/HOL \cite{isa-tutorial} that is designed to handle formulas
    86 combining (co)in\-duc\-tive datatypes, (co)in\-duc\-tively defined predicates, and
    87 quantifiers. It builds on Kodkod \cite{torlak-jackson-2007}, a highly optimized
    88 first-order relational model finder developed by the Software Design Group at
    89 MIT. It is conceptually similar to Refute \cite{weber-2008}, from which it
    90 borrows many ideas and code fragments, but it benefits from Kodkod's
    91 optimizations and a new encoding scheme. The name Nitpick is shamelessly
    92 appropriated from a now retired Alloy precursor.
    93 
    94 Nitpick is easy to use---you simply enter \textbf{nitpick} after a putative
    95 theorem and wait a few seconds. Nonetheless, there are situations where knowing
    96 how it works under the hood and how it reacts to various options helps
    97 increase the test coverage. This manual also explains how to install the tool on
    98 your workstation. Should the motivation fail you, think of the many hours of
    99 hard work Nitpick will save you. Proving non-theorems is \textsl{hard work}.
   100 
   101 Another common use of Nitpick is to find out whether the axioms of a locale are
   102 satisfiable, while the locale is being developed. To check this, it suffices to
   103 write
   104 
   105 \prew
   106 \textbf{lemma}~``$\textit{False}$'' \\
   107 \textbf{nitpick}~[\textit{show\_all}]
   108 \postw
   109 
   110 after the locale's \textbf{begin} keyword. To falsify \textit{False}, Nitpick
   111 must find a model for the axioms. If it finds no model, we have an indication
   112 that the axioms might be unsatisfiable.
   113 
   114 Nitpick requires the Kodkodi package for Isabelle as well as a Java 1.5 virtual
   115 machine called \texttt{java}. The examples presented in this manual can be found
   116 in Isabelle's \texttt{src/HOL/Nitpick\_Examples/Manual\_Nits.thy} theory.
   117 
   118 Throughout this manual, we will explicitly invoke the \textbf{nitpick} command.
   119 Nitpick also provides an automatic mode that can be enabled using the
   120 ``Auto Nitpick'' option from the ``Isabelle'' menu in Proof General. In this
   121 mode, Nitpick is run on every newly entered theorem, much like Auto Quickcheck.
   122 The collective time limit for Auto Nitpick and Auto Quickcheck can be set using
   123 the ``Auto Counterexample Time Limit'' option.
   124 
   125 \newbox\boxA
   126 \setbox\boxA=\hbox{\texttt{nospam}}
   127 
   128 The known bugs and limitations at the time of writing are listed in
   129 \S\ref{known-bugs-and-limitations}. Comments and bug reports concerning Nitpick
   130 or this manual should be directed to
   131 \texttt{blan{\color{white}nospam}\kern-\wd\boxA{}chette@\allowbreak
   132 in.\allowbreak tum.\allowbreak de}.
   133 
   134 \vskip2.5\smallskipamount
   135 
   136 \textbf{Acknowledgment.} The author would like to thank Mark Summerfield for
   137 suggesting several textual improvements.
   138 % and Perry James for reporting a typo.
   139 
   140 \section{Overview}
   141 \label{overview}
   142 
   143 This section introduces Nitpick by presenting small examples. If possible, you
   144 should try out the examples on your workstation. Your theory file should start
   145 as follows:
   146 
   147 \prew
   148 \textbf{theory}~\textit{Scratch} \\
   149 \textbf{imports}~\textit{Main~Quotient\_Product~RealDef} \\
   150 \textbf{begin}
   151 \postw
   152 
   153 The results presented here were obtained using the JNI version of MiniSat and
   154 with multithreading disabled to reduce nondeterminism and a time limit of
   155 15~seconds (instead of 30~seconds). This was done by adding the line
   156 
   157 \prew
   158 \textbf{nitpick\_params} [\textit{sat\_solver}~= \textit{MiniSat\_JNI}, \,\textit{max\_threads}~= 1, \,\textit{timeout} = 15$\,s$]
   159 \postw
   160 
   161 after the \textbf{begin} keyword. The JNI version of MiniSat is bundled with
   162 Kodkodi and is precompiled for the major platforms. Other SAT solvers can also
   163 be installed, as explained in \S\ref{optimizations}. If you have already
   164 configured SAT solvers in Isabelle (e.g., for Refute), these will also be
   165 available to Nitpick.
   166 
   167 \subsection{Propositional Logic}
   168 \label{propositional-logic}
   169 
   170 Let's start with a trivial example from propositional logic:
   171 
   172 \prew
   173 \textbf{lemma}~``$P \longleftrightarrow Q$'' \\
   174 \textbf{nitpick}
   175 \postw
   176 
   177 You should get the following output:
   178 
   179 \prew
   180 \slshape
   181 Nitpick found a counterexample: \\[2\smallskipamount]
   182 \hbox{}\qquad Free variables: \nopagebreak \\
   183 \hbox{}\qquad\qquad $P = \textit{True}$ \\
   184 \hbox{}\qquad\qquad $Q = \textit{False}$
   185 \postw
   186 
   187 Nitpick can also be invoked on individual subgoals, as in the example below:
   188 
   189 \prew
   190 \textbf{apply}~\textit{auto} \\[2\smallskipamount]
   191 {\slshape goal (2 subgoals): \\
   192 \phantom{0}1. $P\,\Longrightarrow\, Q$ \\
   193 \phantom{0}2. $Q\,\Longrightarrow\, P$} \\[2\smallskipamount]
   194 \textbf{nitpick}~1 \\[2\smallskipamount]
   195 {\slshape Nitpick found a counterexample: \\[2\smallskipamount]
   196 \hbox{}\qquad Free variables: \nopagebreak \\
   197 \hbox{}\qquad\qquad $P = \textit{True}$ \\
   198 \hbox{}\qquad\qquad $Q = \textit{False}$} \\[2\smallskipamount]
   199 \textbf{nitpick}~2 \\[2\smallskipamount]
   200 {\slshape Nitpick found a counterexample: \\[2\smallskipamount]
   201 \hbox{}\qquad Free variables: \nopagebreak \\
   202 \hbox{}\qquad\qquad $P = \textit{False}$ \\
   203 \hbox{}\qquad\qquad $Q = \textit{True}$} \\[2\smallskipamount]
   204 \textbf{oops}
   205 \postw
   206 
   207 \subsection{Type Variables}
   208 \label{type-variables}
   209 
   210 If you are left unimpressed by the previous example, don't worry. The next
   211 one is more mind- and computer-boggling:
   212 
   213 \prew
   214 \textbf{lemma} ``$P~x\,\Longrightarrow\, P~(\textrm{THE}~y.\;P~y)$''
   215 \postw
   216 \pagebreak[2] %% TYPESETTING
   217 
   218 The putative lemma involves the definite description operator, {THE}, presented
   219 in section 5.10.1 of the Isabelle tutorial \cite{isa-tutorial}. The
   220 operator is defined by the axiom $(\textrm{THE}~x.\; x = a) = a$. The putative
   221 lemma is merely asserting the indefinite description operator axiom with {THE}
   222 substituted for {SOME}.
   223 
   224 The free variable $x$ and the bound variable $y$ have type $'a$. For formulas
   225 containing type variables, Nitpick enumerates the possible domains for each type
   226 variable, up to a given cardinality (8 by default), looking for a finite
   227 countermodel:
   228 
   229 \prew
   230 \textbf{nitpick} [\textit{verbose}] \\[2\smallskipamount]
   231 \slshape
   232 Trying 8 scopes: \nopagebreak \\
   233 \hbox{}\qquad \textit{card}~$'a$~= 1; \\
   234 \hbox{}\qquad \textit{card}~$'a$~= 2; \\
   235 \hbox{}\qquad $\qquad\vdots$ \\[.5\smallskipamount]
   236 \hbox{}\qquad \textit{card}~$'a$~= 8. \\[2\smallskipamount]
   237 Nitpick found a counterexample for \textit{card} $'a$~= 3: \\[2\smallskipamount]
   238 \hbox{}\qquad Free variables: \nopagebreak \\
   239 \hbox{}\qquad\qquad $P = \{a_2,\, a_3\}$ \\
   240 \hbox{}\qquad\qquad $x = a_3$ \\[2\smallskipamount]
   241 Total time: 580 ms.
   242 \postw
   243 
   244 Nitpick found a counterexample in which $'a$ has cardinality 3. (For
   245 cardinalities 1 and 2, the formula holds.) In the counterexample, the three
   246 values of type $'a$ are written $a_1$, $a_2$, and $a_3$.
   247 
   248 The message ``Trying $n$ scopes: {\ldots}''\ is shown only if the option
   249 \textit{verbose} is enabled. You can specify \textit{verbose} each time you
   250 invoke \textbf{nitpick}, or you can set it globally using the command
   251 
   252 \prew
   253 \textbf{nitpick\_params} [\textit{verbose}]
   254 \postw
   255 
   256 This command also displays the current default values for all of the options
   257 supported by Nitpick. The options are listed in \S\ref{option-reference}.
   258 
   259 \subsection{Constants}
   260 \label{constants}
   261 
   262 By just looking at Nitpick's output, it might not be clear why the
   263 counterexample in \S\ref{type-variables} is genuine. Let's invoke Nitpick again,
   264 this time telling it to show the values of the constants that occur in the
   265 formula:
   266 
   267 \prew
   268 \textbf{lemma}~``$P~x\,\Longrightarrow\, P~(\textrm{THE}~y.\;P~y)$'' \\
   269 \textbf{nitpick}~[\textit{show\_consts}] \\[2\smallskipamount]
   270 \slshape
   271 Nitpick found a counterexample for \textit{card} $'a$~= 3: \\[2\smallskipamount]
   272 \hbox{}\qquad Free variables: \nopagebreak \\
   273 \hbox{}\qquad\qquad $P = \{a_2,\, a_3\}$ \\
   274 \hbox{}\qquad\qquad $x = a_3$ \\
   275 \hbox{}\qquad Constant: \nopagebreak \\
   276 \hbox{}\qquad\qquad $\textit{The}~\textsl{fallback} = a_1$
   277 \postw
   278 
   279 We can see more clearly now. Since the predicate $P$ isn't true for a unique
   280 value, $\textrm{THE}~y.\;P~y$ can denote any value of type $'a$, even
   281 $a_1$. Since $P~a_1$ is false, the entire formula is falsified.
   282 
   283 As an optimization, Nitpick's preprocessor introduced the special constant
   284 ``\textit{The} fallback'' corresponding to $\textrm{THE}~y.\;P~y$ (i.e.,
   285 $\mathit{The}~(\lambda y.\;P~y)$) when there doesn't exist a unique $y$
   286 satisfying $P~y$. We disable this optimization by passing the
   287 \textit{full\_descrs} option:
   288 
   289 \prew
   290 \textbf{nitpick}~[\textit{full\_descrs},\, \textit{show\_consts}] \\[2\smallskipamount]
   291 \slshape
   292 Nitpick found a counterexample for \textit{card} $'a$~= 3: \\[2\smallskipamount]
   293 \hbox{}\qquad Free variables: \nopagebreak \\
   294 \hbox{}\qquad\qquad $P = \{a_2,\, a_3\}$ \\
   295 \hbox{}\qquad\qquad $x = a_3$ \\
   296 \hbox{}\qquad Constant: \nopagebreak \\
   297 \hbox{}\qquad\qquad $\hbox{\slshape THE}~y.\;P~y = a_1$
   298 \postw
   299 
   300 As the result of another optimization, Nitpick directly assigned a value to the
   301 subterm $\textrm{THE}~y.\;P~y$, rather than to the \textit{The} constant. If we
   302 disable this second optimization by using the command
   303 
   304 \prew
   305 \textbf{nitpick}~[\textit{dont\_specialize},\, \textit{full\_descrs},\,
   306 \textit{show\_consts}]
   307 \postw
   308 
   309 we finally get \textit{The}:
   310 
   311 \prew
   312 \slshape Constant: \nopagebreak \\
   313 \hbox{}\qquad $\mathit{The} = \undef{}
   314     (\!\begin{aligned}[t]%
   315     & \{a_1, a_2, a_3\} := a_3,\> \{a_1, a_2\} := a_3,\> \{a_1, a_3\} := a_3, \\[-2pt] %% TYPESETTING
   316     & \{a_1\} := a_1,\> \{a_2, a_3\} := a_1,\> \{a_2\} := a_2, \\[-2pt]
   317     & \{a_3\} := a_3,\> \{\} := a_3)\end{aligned}$
   318 \postw
   319 
   320 Notice that $\textit{The}~(\lambda y.\;P~y) = \textit{The}~\{a_2, a_3\} = a_1$,
   321 just like before.\footnote{The Isabelle/HOL notation $f(x :=
   322 y)$ denotes the function that maps $x$ to $y$ and that otherwise behaves like
   323 $f$.}
   324 
   325 Our misadventures with THE suggest adding `$\exists!x{.}$' (``there exists a
   326 unique $x$ such that'') at the front of our putative lemma's assumption:
   327 
   328 \prew
   329 \textbf{lemma}~``$\exists {!}x.\; P~x\,\Longrightarrow\, P~(\textrm{THE}~y.\;P~y)$''
   330 \postw
   331 
   332 The fix appears to work:
   333 
   334 \prew
   335 \textbf{nitpick} \\[2\smallskipamount]
   336 \slshape Nitpick found no counterexample.
   337 \postw
   338 
   339 We can further increase our confidence in the formula by exhausting all
   340 cardinalities up to 50:
   341 
   342 \prew
   343 \textbf{nitpick} [\textit{card} $'a$~= 1--50]\footnote{The symbol `--'
   344 can be entered as \texttt{-} (hyphen) or
   345 \texttt{\char`\\\char`\<midarrow\char`\>}.} \\[2\smallskipamount]
   346 \slshape Nitpick found no counterexample.
   347 \postw
   348 
   349 Let's see if Sledgehammer \cite{sledgehammer-2009} can find a proof:
   350 
   351 \prew
   352 \textbf{sledgehammer} \\[2\smallskipamount]
   353 {\slshape Sledgehammer: external prover ``$e$'' for subgoal 1: \\
   354 $\exists{!}x.\; P~x\,\Longrightarrow\, P~(\hbox{\slshape THE}~y.\; P~y)$ \\
   355 Try this command: \textrm{apply}~(\textit{metis~the\_equality})} \\[2\smallskipamount]
   356 \textbf{apply}~(\textit{metis~the\_equality\/}) \nopagebreak \\[2\smallskipamount]
   357 {\slshape No subgoals!}% \\[2\smallskipamount]
   358 %\textbf{done}
   359 \postw
   360 
   361 This must be our lucky day.
   362 
   363 \subsection{Skolemization}
   364 \label{skolemization}
   365 
   366 Are all invertible functions onto? Let's find out:
   367 
   368 \prew
   369 \textbf{lemma} ``$\exists g.\; \forall x.~g~(f~x) = x
   370  \,\Longrightarrow\, \forall y.\; \exists x.~y = f~x$'' \\
   371 \textbf{nitpick} \\[2\smallskipamount]
   372 \slshape
   373 Nitpick found a counterexample for \textit{card} $'a$~= 2 and \textit{card} $'b$~=~1: \\[2\smallskipamount]
   374 \hbox{}\qquad Free variable: \nopagebreak \\
   375 \hbox{}\qquad\qquad $f = \undef{}(b_1 := a_1)$ \\
   376 \hbox{}\qquad Skolem constants: \nopagebreak \\
   377 \hbox{}\qquad\qquad $g = \undef{}(a_1 := b_1,\> a_2 := b_1)$ \\
   378 \hbox{}\qquad\qquad $y = a_2$
   379 \postw
   380 
   381 Although $f$ is the only free variable occurring in the formula, Nitpick also
   382 displays values for the bound variables $g$ and $y$. These values are available
   383 to Nitpick because it performs skolemization as a preprocessing step.
   384 
   385 In the previous example, skolemization only affected the outermost quantifiers.
   386 This is not always the case, as illustrated below:
   387 
   388 \prew
   389 \textbf{lemma} ``$\exists x.\; \forall f.\; f~x = x$'' \\
   390 \textbf{nitpick} \\[2\smallskipamount]
   391 \slshape
   392 Nitpick found a counterexample for \textit{card} $'a$~= 2: \\[2\smallskipamount]
   393 \hbox{}\qquad Skolem constant: \nopagebreak \\
   394 \hbox{}\qquad\qquad $\lambda x.\; f =
   395     \undef{}(\!\begin{aligned}[t]
   396     & a_1 := \undef{}(a_1 := a_2,\> a_2 := a_1), \\[-2pt]
   397     & a_2 := \undef{}(a_1 := a_1,\> a_2 := a_1))\end{aligned}$
   398 \postw
   399 
   400 The variable $f$ is bound within the scope of $x$; therefore, $f$ depends on
   401 $x$, as suggested by the notation $\lambda x.\,f$. If $x = a_1$, then $f$ is the
   402 function that maps $a_1$ to $a_2$ and vice versa; otherwise, $x = a_2$ and $f$
   403 maps both $a_1$ and $a_2$ to $a_1$. In both cases, $f~x \not= x$.
   404 
   405 The source of the Skolem constants is sometimes more obscure:
   406 
   407 \prew
   408 \textbf{lemma} ``$\mathit{refl}~r\,\Longrightarrow\, \mathit{sym}~r$'' \\
   409 \textbf{nitpick} \\[2\smallskipamount]
   410 \slshape
   411 Nitpick found a counterexample for \textit{card} $'a$~= 2: \\[2\smallskipamount]
   412 \hbox{}\qquad Free variable: \nopagebreak \\
   413 \hbox{}\qquad\qquad $r = \{(a_1, a_1),\, (a_2, a_1),\, (a_2, a_2)\}$ \\
   414 \hbox{}\qquad Skolem constants: \nopagebreak \\
   415 \hbox{}\qquad\qquad $\mathit{sym}.x = a_2$ \\
   416 \hbox{}\qquad\qquad $\mathit{sym}.y = a_1$
   417 \postw
   418 
   419 What happened here is that Nitpick expanded the \textit{sym} constant to its
   420 definition:
   421 
   422 \prew
   423 $\mathit{sym}~r \,\equiv\,
   424  \forall x\> y.\,\> (x, y) \in r \longrightarrow (y, x) \in r.$
   425 \postw
   426 
   427 As their names suggest, the Skolem constants $\mathit{sym}.x$ and
   428 $\mathit{sym}.y$ are simply the bound variables $x$ and $y$
   429 from \textit{sym}'s definition.
   430 
   431 Although skolemization is a useful optimization, you can disable it by invoking
   432 Nitpick with \textit{dont\_skolemize}. See \S\ref{optimizations} for details.
   433 
   434 \subsection{Natural Numbers and Integers}
   435 \label{natural-numbers-and-integers}
   436 
   437 Because of the axiom of infinity, the type \textit{nat} does not admit any
   438 finite models. To deal with this, Nitpick's approach is to consider finite
   439 subsets $N$ of \textit{nat} and maps all numbers $\notin N$ to the undefined
   440 value (displayed as `$\unk$'). The type \textit{int} is handled similarly.
   441 Internally, undefined values lead to a three-valued logic.
   442 
   443 Here is an example involving \textit{int\/}:
   444 
   445 \prew
   446 \textbf{lemma} ``$\lbrakk i \le j;\> n \le (m{\Colon}\mathit{int})\rbrakk \,\Longrightarrow\, i * n + j * m \le i * m + j * n$'' \\
   447 \textbf{nitpick} \\[2\smallskipamount]
   448 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
   449 \hbox{}\qquad Free variables: \nopagebreak \\
   450 \hbox{}\qquad\qquad $i = 0$ \\
   451 \hbox{}\qquad\qquad $j = 1$ \\
   452 \hbox{}\qquad\qquad $m = 1$ \\
   453 \hbox{}\qquad\qquad $n = 0$
   454 \postw
   455 
   456 Internally, Nitpick uses either a unary or a binary representation of numbers.
   457 The unary representation is more efficient but only suitable for numbers very
   458 close to zero. By default, Nitpick attempts to choose the more appropriate
   459 encoding by inspecting the formula at hand. This behavior can be overridden by
   460 passing either \textit{unary\_ints} or \textit{binary\_ints} as option. For
   461 binary notation, the number of bits to use can be specified using
   462 the \textit{bits} option. For example:
   463 
   464 \prew
   465 \textbf{nitpick} [\textit{binary\_ints}, \textit{bits}${} = 16$]
   466 \postw
   467 
   468 With infinite types, we don't always have the luxury of a genuine counterexample
   469 and must often content ourselves with a potential one. The tedious task of
   470 finding out whether the potential counterexample is in fact genuine can be
   471 outsourced to \textit{auto} by passing \textit{check\_potential}. For example:
   472 
   473 \prew
   474 \textbf{lemma} ``$\forall n.\; \textit{Suc}~n \mathbin{\not=} n \,\Longrightarrow\, P$'' \\
   475 \textbf{nitpick} [\textit{card~nat}~= 100, \textit{check\_potential}] \\[2\smallskipamount]
   476 \slshape Warning: The conjecture either trivially holds for the given scopes or lies outside Nitpick's supported
   477 fragment. Only potential counterexamples may be found. \\[2\smallskipamount]
   478 Nitpick found a potential counterexample: \\[2\smallskipamount]
   479 \hbox{}\qquad Free variable: \nopagebreak \\
   480 \hbox{}\qquad\qquad $P = \textit{False}$ \\[2\smallskipamount]
   481 Confirmation by ``\textit{auto}'': The above counterexample is genuine.
   482 \postw
   483 
   484 You might wonder why the counterexample is first reported as potential. The root
   485 of the problem is that the bound variable in $\forall n.\; \textit{Suc}~n
   486 \mathbin{\not=} n$ ranges over an infinite type. If Nitpick finds an $n$ such
   487 that $\textit{Suc}~n \mathbin{=} n$, it evaluates the assumption to
   488 \textit{False}; but otherwise, it does not know anything about values of $n \ge
   489 \textit{card~nat}$ and must therefore evaluate the assumption to $\unk$, not
   490 \textit{True}. Since the assumption can never be satisfied, the putative lemma
   491 can never be falsified.
   492 
   493 Incidentally, if you distrust the so-called genuine counterexamples, you can
   494 enable \textit{check\_\allowbreak genuine} to verify them as well. However, be
   495 aware that \textit{auto} will usually fail to prove that the counterexample is
   496 genuine or spurious.
   497 
   498 Some conjectures involving elementary number theory make Nitpick look like a
   499 giant with feet of clay:
   500 
   501 \prew
   502 \textbf{lemma} ``$P~\textit{Suc}$'' \\
   503 \textbf{nitpick} \\[2\smallskipamount]
   504 \slshape
   505 Nitpick found no counterexample.
   506 \postw
   507 
   508 On any finite set $N$, \textit{Suc} is a partial function; for example, if $N =
   509 \{0, 1, \ldots, k\}$, then \textit{Suc} is $\{0 \mapsto 1,\, 1 \mapsto 2,\,
   510 \ldots,\, k \mapsto \unk\}$, which evaluates to $\unk$ when passed as
   511 argument to $P$. As a result, $P~\textit{Suc}$ is always $\unk$. The next
   512 example is similar:
   513 
   514 \prew
   515 \textbf{lemma} ``$P~(\textit{op}~{+}\Colon
   516 \textit{nat}\mathbin{\Rightarrow}\textit{nat}\mathbin{\Rightarrow}\textit{nat})$'' \\
   517 \textbf{nitpick} [\textit{card nat} = 1] \\[2\smallskipamount]
   518 {\slshape Nitpick found a counterexample:} \\[2\smallskipamount]
   519 \hbox{}\qquad Free variable: \nopagebreak \\
   520 \hbox{}\qquad\qquad $P = \{\}$ \\[2\smallskipamount]
   521 \textbf{nitpick} [\textit{card nat} = 2] \\[2\smallskipamount]
   522 {\slshape Nitpick found no counterexample.}
   523 \postw
   524 
   525 The problem here is that \textit{op}~+ is total when \textit{nat} is taken to be
   526 $\{0\}$ but becomes partial as soon as we add $1$, because $1 + 1 \notin \{0,
   527 1\}$.
   528 
   529 Because numbers are infinite and are approximated using a three-valued logic,
   530 there is usually no need to systematically enumerate domain sizes. If Nitpick
   531 cannot find a genuine counterexample for \textit{card~nat}~= $k$, it is very
   532 unlikely that one could be found for smaller domains. (The $P~(\textit{op}~{+})$
   533 example above is an exception to this principle.) Nitpick nonetheless enumerates
   534 all cardinalities from 1 to 8 for \textit{nat}, mainly because smaller
   535 cardinalities are fast to handle and give rise to simpler counterexamples. This
   536 is explained in more detail in \S\ref{scope-monotonicity}.
   537 
   538 \subsection{Inductive Datatypes}
   539 \label{inductive-datatypes}
   540 
   541 Like natural numbers and integers, inductive datatypes with recursive
   542 constructors admit no finite models and must be approximated by a subterm-closed
   543 subset. For example, using a cardinality of 10 for ${'}a~\textit{list}$,
   544 Nitpick looks for all counterexamples that can be built using at most 10
   545 different lists.
   546 
   547 Let's see with an example involving \textit{hd} (which returns the first element
   548 of a list) and $@$ (which concatenates two lists):
   549 
   550 \prew
   551 \textbf{lemma} ``$\textit{hd}~(\textit{xs} \mathbin{@} [y, y]) = \textit{hd}~\textit{xs}$'' \\
   552 \textbf{nitpick} \\[2\smallskipamount]
   553 \slshape Nitpick found a counterexample for \textit{card} $'a$~= 3: \\[2\smallskipamount]
   554 \hbox{}\qquad Free variables: \nopagebreak \\
   555 \hbox{}\qquad\qquad $\textit{xs} = []$ \\
   556 \hbox{}\qquad\qquad $\textit{y} = a_1$
   557 \postw
   558 
   559 To see why the counterexample is genuine, we enable \textit{show\_consts}
   560 and \textit{show\_\allowbreak datatypes}:
   561 
   562 \prew
   563 {\slshape Datatype:} \\
   564 \hbox{}\qquad $'a$~\textit{list}~= $\{[],\, [a_1],\, [a_1, a_1],\, \unr\}$ \\
   565 {\slshape Constants:} \\
   566 \hbox{}\qquad $\lambda x_1.\; x_1 \mathbin{@} [y, y] = \undef([] := [a_1, a_1])$ \\
   567 \hbox{}\qquad $\textit{hd} = \undef([] := a_2,\> [a_1] := a_1,\> [a_1, a_1] := a_1)$
   568 \postw
   569 
   570 Since $\mathit{hd}~[]$ is undefined in the logic, it may be given any value,
   571 including $a_2$.
   572 
   573 The second constant, $\lambda x_1.\; x_1 \mathbin{@} [y, y]$, is simply the
   574 append operator whose second argument is fixed to be $[y, y]$. Appending $[a_1,
   575 a_1]$ to $[a_1]$ would normally give $[a_1, a_1, a_1]$, but this value is not
   576 representable in the subset of $'a$~\textit{list} considered by Nitpick, which
   577 is shown under the ``Datatype'' heading; hence the result is $\unk$. Similarly,
   578 appending $[a_1, a_1]$ to itself gives $\unk$.
   579 
   580 Given \textit{card}~$'a = 3$ and \textit{card}~$'a~\textit{list} = 3$, Nitpick
   581 considers the following subsets:
   582 
   583 \kern-.5\smallskipamount %% TYPESETTING
   584 
   585 \prew
   586 \begin{multicols}{3}
   587 $\{[],\, [a_1],\, [a_2]\}$; \\
   588 $\{[],\, [a_1],\, [a_3]\}$; \\
   589 $\{[],\, [a_2],\, [a_3]\}$; \\
   590 $\{[],\, [a_1],\, [a_1, a_1]\}$; \\
   591 $\{[],\, [a_1],\, [a_2, a_1]\}$; \\
   592 $\{[],\, [a_1],\, [a_3, a_1]\}$; \\
   593 $\{[],\, [a_2],\, [a_1, a_2]\}$; \\
   594 $\{[],\, [a_2],\, [a_2, a_2]\}$; \\
   595 $\{[],\, [a_2],\, [a_3, a_2]\}$; \\
   596 $\{[],\, [a_3],\, [a_1, a_3]\}$; \\
   597 $\{[],\, [a_3],\, [a_2, a_3]\}$; \\
   598 $\{[],\, [a_3],\, [a_3, a_3]\}$.
   599 \end{multicols}
   600 \postw
   601 
   602 \kern-2\smallskipamount %% TYPESETTING
   603 
   604 All subterm-closed subsets of $'a~\textit{list}$ consisting of three values
   605 are listed and only those. As an example of a non-subterm-closed subset,
   606 consider $\mathcal{S} = \{[],\, [a_1],\,\allowbreak [a_1, a_2]\}$, and observe
   607 that $[a_1, a_2]$ (i.e., $a_1 \mathbin{\#} [a_2]$) has $[a_2] \notin
   608 \mathcal{S}$ as a subterm.
   609 
   610 Here's another m\"ochtegern-lemma that Nitpick can refute without a blink:
   611 
   612 \prew
   613 \textbf{lemma} ``$\lbrakk \textit{length}~\textit{xs} = 1;\> \textit{length}~\textit{ys} = 1
   614 \rbrakk \,\Longrightarrow\, \textit{xs} = \textit{ys}$''
   615 \\
   616 \textbf{nitpick} [\textit{show\_datatypes}] \\[2\smallskipamount]
   617 \slshape Nitpick found a counterexample for \textit{card} $'a$~= 3: \\[2\smallskipamount]
   618 \hbox{}\qquad Free variables: \nopagebreak \\
   619 \hbox{}\qquad\qquad $\textit{xs} = [a_1]$ \\
   620 \hbox{}\qquad\qquad $\textit{ys} = [a_2]$ \\
   621 \hbox{}\qquad Datatypes: \\
   622 \hbox{}\qquad\qquad $\textit{nat} = \{0,\, 1,\, 2,\, \unr\}$ \\
   623 \hbox{}\qquad\qquad $'a$~\textit{list} = $\{[],\, [a_1],\, [a_2],\, \unr\}$
   624 \postw
   625 
   626 Because datatypes are approximated using a three-valued logic, there is usually
   627 no need to systematically enumerate cardinalities: If Nitpick cannot find a
   628 genuine counterexample for \textit{card}~$'a~\textit{list}$~= 10, it is very
   629 unlikely that one could be found for smaller cardinalities.
   630 
   631 \subsection{Typedefs, Quotient Types, Records, Rationals, and Reals}
   632 \label{typedefs-records-rationals-and-reals}
   633 
   634 Nitpick generally treats types declared using \textbf{typedef} as datatypes
   635 whose single constructor is the corresponding \textit{Abs\_\kern.1ex} function.
   636 For example:
   637 
   638 \prew
   639 \textbf{typedef}~\textit{three} = ``$\{0\Colon\textit{nat},\, 1,\, 2\}$'' \\
   640 \textbf{by}~\textit{blast} \\[2\smallskipamount]
   641 \textbf{definition}~$A \mathbin{\Colon} \textit{three}$ \textbf{where} ``\kern-.1em$A \,\equiv\, \textit{Abs\_\allowbreak three}~0$'' \\
   642 \textbf{definition}~$B \mathbin{\Colon} \textit{three}$ \textbf{where} ``$B \,\equiv\, \textit{Abs\_three}~1$'' \\
   643 \textbf{definition}~$C \mathbin{\Colon} \textit{three}$ \textbf{where} ``$C \,\equiv\, \textit{Abs\_three}~2$'' \\[2\smallskipamount]
   644 \textbf{lemma} ``$\lbrakk P~A;\> P~B\rbrakk \,\Longrightarrow\, P~x$'' \\
   645 \textbf{nitpick} [\textit{show\_datatypes}] \\[2\smallskipamount]
   646 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
   647 \hbox{}\qquad Free variables: \nopagebreak \\
   648 \hbox{}\qquad\qquad $P = \{\Abs{0},\, \Abs{1}\}$ \\
   649 \hbox{}\qquad\qquad $x = \Abs{2}$ \\
   650 \hbox{}\qquad Datatypes: \\
   651 \hbox{}\qquad\qquad $\textit{nat} = \{0,\, 1,\, 2,\, \unr\}$ \\
   652 \hbox{}\qquad\qquad $\textit{three} = \{\Abs{0},\, \Abs{1},\, \Abs{2},\, \unr\}$
   653 \postw
   654 
   655 In the output above, $\Abs{n}$ abbreviates $\textit{Abs\_three}~n$.
   656 
   657 Quotient types are handled in much the same way. The following fragment defines
   658 the integer type \textit{my\_int} by encoding the integer $x$ by a pair of
   659 natural numbers $(m, n)$ such that $x + n = m$:
   660 
   661 \prew
   662 \textbf{fun} \textit{my\_int\_rel} \textbf{where} \\
   663 ``$\textit{my\_int\_rel}~(x,\, y)~(u,\, v) = (x + v = u + y)$'' \\[2\smallskipamount]
   664 %
   665 \textbf{quotient\_type}~\textit{my\_int} = ``$\textit{nat} \times \textit{nat\/}$''$\;{/}\;$\textit{my\_int\_rel} \\
   666 \textbf{by}~(\textit{auto simp add\/}:\ \textit{equivp\_def expand\_fun\_eq}) \\[2\smallskipamount]
   667 %
   668 \textbf{definition}~\textit{add\_raw}~\textbf{where} \\
   669 ``$\textit{add\_raw} \,\equiv\, \lambda(x,\, y)~(u,\, v).\; (x + (u\Colon\textit{nat}), y + (v\Colon\textit{nat}))$'' \\[2\smallskipamount]
   670 %
   671 \textbf{quotient\_definition} ``$\textit{add\/}\Colon\textit{my\_int} \Rightarrow \textit{my\_int} \Rightarrow \textit{my\_int\/}$'' \textbf{is} \textit{add\_raw} \\[2\smallskipamount]
   672 %
   673 \textbf{lemma} ``$\textit{add}~x~y = \textit{add}~x~x$'' \\
   674 \textbf{nitpick} [\textit{show\_datatypes}] \\[2\smallskipamount]
   675 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
   676 \hbox{}\qquad Free variables: \nopagebreak \\
   677 \hbox{}\qquad\qquad $x = \Abs{(0,\, 0)}$ \\
   678 \hbox{}\qquad\qquad $y = \Abs{(1,\, 0)}$ \\
   679 \hbox{}\qquad Datatypes: \\
   680 \hbox{}\qquad\qquad $\textit{nat} = \{0,\, 1,\, \unr\}$ \\
   681 \hbox{}\qquad\qquad $\textit{nat} \times \textit{nat}~[\textsl{boxed\/}] = \{(0,\, 0),\> (1,\, 0),\> \unr\}$ \\
   682 \hbox{}\qquad\qquad $\textit{my\_int} = \{\Abs{(0,\, 0)},\> \Abs{(1,\, 0)},\> \unr\}$
   683 \postw
   684 
   685 In the counterexample, $\Abs{(0,\, 0)}$ and $\Abs{(1,\, 0)}$ represent the
   686 integers $0$ and $1$, respectively. Other representants would have been
   687 possible---e.g., $\Abs{(5,\, 5)}$ and $\Abs{(12,\, 11)}$.
   688 
   689 Records are also handled as datatypes with a single constructor:
   690 
   691 \prew
   692 \textbf{record} \textit{point} = \\
   693 \hbox{}\quad $\textit{Xcoord} \mathbin{\Colon} \textit{int}$ \\
   694 \hbox{}\quad $\textit{Ycoord} \mathbin{\Colon} \textit{int}$ \\[2\smallskipamount]
   695 \textbf{lemma} ``$\textit{Xcoord}~(p\Colon\textit{point}) = \textit{Xcoord}~(q\Colon\textit{point})$'' \\
   696 \textbf{nitpick} [\textit{show\_datatypes}] \\[2\smallskipamount]
   697 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
   698 \hbox{}\qquad Free variables: \nopagebreak \\
   699 \hbox{}\qquad\qquad $p = \lparr\textit{Xcoord} = 1,\> \textit{Ycoord} = 1\rparr$ \\
   700 \hbox{}\qquad\qquad $q = \lparr\textit{Xcoord} = 0,\> \textit{Ycoord} = 0\rparr$ \\
   701 \hbox{}\qquad Datatypes: \\
   702 \hbox{}\qquad\qquad $\textit{int} = \{0,\, 1,\, \unr\}$ \\
   703 \hbox{}\qquad\qquad $\textit{point} = \{\!\begin{aligned}[t]
   704 & \lparr\textit{Xcoord} = 0,\> \textit{Ycoord} = 0\rparr, \\[-2pt] %% TYPESETTING
   705 & \lparr\textit{Xcoord} = 1,\> \textit{Ycoord} = 1\rparr,\, \unr\}\end{aligned}$
   706 \postw
   707 
   708 Finally, Nitpick provides rudimentary support for rationals and reals using a
   709 similar approach:
   710 
   711 \prew
   712 \textbf{lemma} ``$4 * x + 3 * (y\Colon\textit{real}) \not= 1/2$'' \\
   713 \textbf{nitpick} [\textit{show\_datatypes}] \\[2\smallskipamount]
   714 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
   715 \hbox{}\qquad Free variables: \nopagebreak \\
   716 \hbox{}\qquad\qquad $x = 1/2$ \\
   717 \hbox{}\qquad\qquad $y = -1/2$ \\
   718 \hbox{}\qquad Datatypes: \\
   719 \hbox{}\qquad\qquad $\textit{nat} = \{0,\, 1,\, 2,\, 3,\, 4,\, 5,\, 6,\, 7,\, \unr\}$ \\
   720 \hbox{}\qquad\qquad $\textit{int} = \{0,\, 1,\, 2,\, 3,\, 4,\, -3,\, -2,\, -1,\, \unr\}$ \\
   721 \hbox{}\qquad\qquad $\textit{real} = \{1,\, 0,\, 4,\, -3/2,\, 3,\, 2,\, 1/2,\, -1/2,\, \unr\}$
   722 \postw
   723 
   724 \subsection{Inductive and Coinductive Predicates}
   725 \label{inductive-and-coinductive-predicates}
   726 
   727 Inductively defined predicates (and sets) are particularly problematic for
   728 counterexample generators. They can make Quickcheck~\cite{berghofer-nipkow-2004}
   729 loop forever and Refute~\cite{weber-2008} run out of resources. The crux of
   730 the problem is that they are defined using a least fixed point construction.
   731 
   732 Nitpick's philosophy is that not all inductive predicates are equal. Consider
   733 the \textit{even} predicate below:
   734 
   735 \prew
   736 \textbf{inductive}~\textit{even}~\textbf{where} \\
   737 ``\textit{even}~0'' $\,\mid$ \\
   738 ``\textit{even}~$n\,\Longrightarrow\, \textit{even}~(\textit{Suc}~(\textit{Suc}~n))$''
   739 \postw
   740 
   741 This predicate enjoys the desirable property of being well-founded, which means
   742 that the introduction rules don't give rise to infinite chains of the form
   743 
   744 \prew
   745 $\cdots\,\Longrightarrow\, \textit{even}~k''
   746        \,\Longrightarrow\, \textit{even}~k'
   747        \,\Longrightarrow\, \textit{even}~k.$
   748 \postw
   749 
   750 For \textit{even}, this is obvious: Any chain ending at $k$ will be of length
   751 $k/2 + 1$:
   752 
   753 \prew
   754 $\textit{even}~0\,\Longrightarrow\, \textit{even}~2\,\Longrightarrow\, \cdots
   755        \,\Longrightarrow\, \textit{even}~(k - 2)
   756        \,\Longrightarrow\, \textit{even}~k.$
   757 \postw
   758 
   759 Wellfoundedness is desirable because it enables Nitpick to use a very efficient
   760 fixed point computation.%
   761 \footnote{If an inductive predicate is
   762 well-founded, then it has exactly one fixed point, which is simultaneously the
   763 least and the greatest fixed point. In these circumstances, the computation of
   764 the least fixed point amounts to the computation of an arbitrary fixed point,
   765 which can be performed using a straightforward recursive equation.}
   766 Moreover, Nitpick can prove wellfoundedness of most well-founded predicates,
   767 just as Isabelle's \textbf{function} package usually discharges termination
   768 proof obligations automatically.
   769 
   770 Let's try an example:
   771 
   772 \prew
   773 \textbf{lemma} ``$\exists n.\; \textit{even}~n \mathrel{\land} \textit{even}~(\textit{Suc}~n)$'' \\
   774 \textbf{nitpick}~[\textit{card nat}~= 100, \textit{unary\_ints}, \textit{verbose}] \\[2\smallskipamount]
   775 \slshape The inductive predicate ``\textit{even}'' was proved well-founded.
   776 Nitpick can compute it efficiently. \\[2\smallskipamount]
   777 Trying 1 scope: \\
   778 \hbox{}\qquad \textit{card nat}~= 100. \\[2\smallskipamount]
   779 Nitpick found a potential counterexample for \textit{card nat}~= 100: \\[2\smallskipamount]
   780 \hbox{}\qquad Empty assignment \\[2\smallskipamount]
   781 Nitpick could not find a better counterexample. \\[2\smallskipamount]
   782 Total time: 2274 ms.
   783 \postw
   784 
   785 No genuine counterexample is possible because Nitpick cannot rule out the
   786 existence of a natural number $n \ge 100$ such that both $\textit{even}~n$ and
   787 $\textit{even}~(\textit{Suc}~n)$ are true. To help Nitpick, we can bound the
   788 existential quantifier:
   789 
   790 \prew
   791 \textbf{lemma} ``$\exists n \mathbin{\le} 99.\; \textit{even}~n \mathrel{\land} \textit{even}~(\textit{Suc}~n)$'' \\
   792 \textbf{nitpick}~[\textit{card nat}~= 100, \textit{unary\_ints}] \\[2\smallskipamount]
   793 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
   794 \hbox{}\qquad Empty assignment
   795 \postw
   796 
   797 So far we were blessed by the wellfoundedness of \textit{even}. What happens if
   798 we use the following definition instead?
   799 
   800 \prew
   801 \textbf{inductive} $\textit{even}'$ \textbf{where} \\
   802 ``$\textit{even}'~(0{\Colon}\textit{nat})$'' $\,\mid$ \\
   803 ``$\textit{even}'~2$'' $\,\mid$ \\
   804 ``$\lbrakk\textit{even}'~m;\> \textit{even}'~n\rbrakk \,\Longrightarrow\, \textit{even}'~(m + n)$''
   805 \postw
   806 
   807 This definition is not well-founded: From $\textit{even}'~0$ and
   808 $\textit{even}'~0$, we can derive that $\textit{even}'~0$. Nonetheless, the
   809 predicates $\textit{even}$ and $\textit{even}'$ are equivalent.
   810 
   811 Let's check a property involving $\textit{even}'$. To make up for the
   812 foreseeable computational hurdles entailed by non-wellfoundedness, we decrease
   813 \textit{nat}'s cardinality to a mere 10:
   814 
   815 \prew
   816 \textbf{lemma}~``$\exists n \in \{0, 2, 4, 6, 8\}.\;
   817 \lnot\;\textit{even}'~n$'' \\
   818 \textbf{nitpick}~[\textit{card nat}~= 10,\, \textit{verbose},\, \textit{show\_consts}] \\[2\smallskipamount]
   819 \slshape
   820 The inductive predicate ``$\textit{even}'\!$'' could not be proved well-founded.
   821 Nitpick might need to unroll it. \\[2\smallskipamount]
   822 Trying 6 scopes: \\
   823 \hbox{}\qquad \textit{card nat}~= 10 and \textit{iter} $\textit{even}'$~= 0; \\
   824 \hbox{}\qquad \textit{card nat}~= 10 and \textit{iter} $\textit{even}'$~= 1; \\
   825 \hbox{}\qquad \textit{card nat}~= 10 and \textit{iter} $\textit{even}'$~= 2; \\
   826 \hbox{}\qquad \textit{card nat}~= 10 and \textit{iter} $\textit{even}'$~= 4; \\
   827 \hbox{}\qquad \textit{card nat}~= 10 and \textit{iter} $\textit{even}'$~= 8; \\
   828 \hbox{}\qquad \textit{card nat}~= 10 and \textit{iter} $\textit{even}'$~= 9. \\[2\smallskipamount]
   829 Nitpick found a counterexample for \textit{card nat}~= 10 and \textit{iter} $\textit{even}'$~= 2: \\[2\smallskipamount]
   830 \hbox{}\qquad Constant: \nopagebreak \\
   831 \hbox{}\qquad\qquad $\lambda i.\; \textit{even}'$ = $\undef(\!\begin{aligned}[t]
   832 & 2 := \{0, 2, 4, 6, 8, 1^\Q, 3^\Q, 5^\Q, 7^\Q, 9^\Q\}, \\[-2pt]
   833 & 1 := \{0, 2, 4, 1^\Q, 3^\Q, 5^\Q, 6^\Q, 7^\Q, 8^\Q, 9^\Q\}, \\[-2pt]
   834 & 0 := \{0, 2, 1^\Q, 3^\Q, 4^\Q, 5^\Q, 6^\Q, 7^\Q, 8^\Q, 9^\Q\})\end{aligned}$ \\[2\smallskipamount]
   835 Total time: 1140 ms.
   836 \postw
   837 
   838 Nitpick's output is very instructive. First, it tells us that the predicate is
   839 unrolled, meaning that it is computed iteratively from the empty set. Then it
   840 lists six scopes specifying different bounds on the numbers of iterations:\ 0,
   841 1, 2, 4, 8, and~9.
   842 
   843 The output also shows how each iteration contributes to $\textit{even}'$. The
   844 notation $\lambda i.\; \textit{even}'$ indicates that the value of the
   845 predicate depends on an iteration counter. Iteration 0 provides the basis
   846 elements, $0$ and $2$. Iteration 1 contributes $4$ ($= 2 + 2$). Iteration 2
   847 throws $6$ ($= 2 + 4 = 4 + 2$) and $8$ ($= 4 + 4$) into the mix. Further
   848 iterations would not contribute any new elements.
   849 
   850 Some values are marked with superscripted question
   851 marks~(`\lower.2ex\hbox{$^\Q$}'). These are the elements for which the
   852 predicate evaluates to $\unk$. Thus, $\textit{even}'$ evaluates to either
   853 \textit{True} or $\unk$, never \textit{False}.
   854 
   855 When unrolling a predicate, Nitpick tries 0, 1, 2, 4, 8, 12, 16, and 24
   856 iterations. However, these numbers are bounded by the cardinality of the
   857 predicate's domain. With \textit{card~nat}~= 10, no more than 9 iterations are
   858 ever needed to compute the value of a \textit{nat} predicate. You can specify
   859 the number of iterations using the \textit{iter} option, as explained in
   860 \S\ref{scope-of-search}.
   861 
   862 In the next formula, $\textit{even}'$ occurs both positively and negatively:
   863 
   864 \prew
   865 \textbf{lemma} ``$\textit{even}'~(n - 2) \,\Longrightarrow\, \textit{even}'~n$'' \\
   866 \textbf{nitpick} [\textit{card nat} = 10, \textit{show\_consts}] \\[2\smallskipamount]
   867 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
   868 \hbox{}\qquad Free variable: \nopagebreak \\
   869 \hbox{}\qquad\qquad $n = 1$ \\
   870 \hbox{}\qquad Constants: \nopagebreak \\
   871 \hbox{}\qquad\qquad $\lambda i.\; \textit{even}'$ = $\undef(\!\begin{aligned}[t]
   872 & 0 := \{0, 2, 1^\Q, 3^\Q, 4^\Q, 5^\Q, 6^\Q, 7^\Q, 8^\Q, 9^\Q\})\end{aligned}$  \\
   873 \hbox{}\qquad\qquad $\textit{even}' \subseteq \{0, 2, 4, 6, 8, \unr\}$
   874 \postw
   875 
   876 Notice the special constraint $\textit{even}' \subseteq \{0,\, 2,\, 4,\, 6,\,
   877 8,\, \unr\}$ in the output, whose right-hand side represents an arbitrary
   878 fixed point (not necessarily the least one). It is used to falsify
   879 $\textit{even}'~n$. In contrast, the unrolled predicate is used to satisfy
   880 $\textit{even}'~(n - 2)$.
   881 
   882 Coinductive predicates are handled dually. For example:
   883 
   884 \prew
   885 \textbf{coinductive} \textit{nats} \textbf{where} \\
   886 ``$\textit{nats}~(x\Colon\textit{nat}) \,\Longrightarrow\, \textit{nats}~x$'' \\[2\smallskipamount]
   887 \textbf{lemma} ``$\textit{nats} = \{0, 1, 2, 3, 4\}$'' \\
   888 \textbf{nitpick}~[\textit{card nat} = 10,\, \textit{show\_consts}] \\[2\smallskipamount]
   889 \slshape Nitpick found a counterexample:
   890 \\[2\smallskipamount]
   891 \hbox{}\qquad Constants: \nopagebreak \\
   892 \hbox{}\qquad\qquad $\lambda i.\; \textit{nats} = \undef(0 := \{\!\begin{aligned}[t]
   893 & 0^\Q, 1^\Q, 2^\Q, 3^\Q, 4^\Q, 5^\Q, 6^\Q, 7^\Q, 8^\Q, 9^\Q, \\[-2pt]
   894 & \unr\})\end{aligned}$ \\
   895 \hbox{}\qquad\qquad $nats \supseteq \{9, 5^\Q, 6^\Q, 7^\Q, 8^\Q, \unr\}$
   896 \postw
   897 
   898 As a special case, Nitpick uses Kodkod's transitive closure operator to encode
   899 negative occurrences of non-well-founded ``linear inductive predicates,'' i.e.,
   900 inductive predicates for which each the predicate occurs in at most one
   901 assumption of each introduction rule. For example:
   902 
   903 \prew
   904 \textbf{inductive} \textit{odd} \textbf{where} \\
   905 ``$\textit{odd}~1$'' $\,\mid$ \\
   906 ``$\lbrakk \textit{odd}~m;\>\, \textit{even}~n\rbrakk \,\Longrightarrow\, \textit{odd}~(m + n)$'' \\[2\smallskipamount]
   907 \textbf{lemma}~``$\textit{odd}~n \,\Longrightarrow\, \textit{odd}~(n - 2)$'' \\
   908 \textbf{nitpick}~[\textit{card nat} = 10,\, \textit{show\_consts}] \\[2\smallskipamount]
   909 \slshape Nitpick found a counterexample:
   910 \\[2\smallskipamount]
   911 \hbox{}\qquad Free variable: \nopagebreak \\
   912 \hbox{}\qquad\qquad $n = 1$ \\
   913 \hbox{}\qquad Constants: \nopagebreak \\
   914 \hbox{}\qquad\qquad $\textit{even} = \{0, 2, 4, 6, 8, \unr\}$ \\
   915 \hbox{}\qquad\qquad $\textit{odd}_{\textsl{base}} = \{1, \unr\}$ \\
   916 \hbox{}\qquad\qquad $\textit{odd}_{\textsl{step}} = \!
   917 \!\begin{aligned}[t]
   918   & \{(0, 0), (0, 2), (0, 4), (0, 6), (0, 8), (1, 1), (1, 3), (1, 5), \\[-2pt]
   919   & \phantom{\{} (1, 7), (1, 9), (2, 2), (2, 4), (2, 6), (2, 8), (3, 3),
   920        (3, 5), \\[-2pt]
   921   & \phantom{\{} (3, 7), (3, 9), (4, 4), (4, 6), (4, 8), (5, 5), (5, 7), (5, 9), \\[-2pt]
   922   & \phantom{\{} (6, 6), (6, 8), (7, 7), (7, 9), (8, 8), (9, 9), \unr\}\end{aligned}$ \\
   923 \hbox{}\qquad\qquad $\textit{odd} \subseteq \{1, 3, 5, 7, 9, 8^\Q, \unr\}$
   924 \postw
   925 
   926 \noindent
   927 In the output, $\textit{odd}_{\textrm{base}}$ represents the base elements and
   928 $\textit{odd}_{\textrm{step}}$ is a transition relation that computes new
   929 elements from known ones. The set $\textit{odd}$ consists of all the values
   930 reachable through the reflexive transitive closure of
   931 $\textit{odd}_{\textrm{step}}$ starting with any element from
   932 $\textit{odd}_{\textrm{base}}$, namely 1, 3, 5, 7, and 9. Using Kodkod's
   933 transitive closure to encode linear predicates is normally either more thorough
   934 or more efficient than unrolling (depending on the value of \textit{iter}), but
   935 for those cases where it isn't you can disable it by passing the
   936 \textit{dont\_star\_linear\_preds} option.
   937 
   938 \subsection{Coinductive Datatypes}
   939 \label{coinductive-datatypes}
   940 
   941 While Isabelle regrettably lacks a high-level mechanism for defining coinductive
   942 datatypes, the \textit{Coinductive\_List} theory from Andreas Lochbihler's
   943 \textit{Coinductive} AFP entry \cite{lochbihler-2010} provides a coinductive
   944 ``lazy list'' datatype, $'a~\textit{llist}$, defined the hard way. Nitpick
   945 supports these lazy lists seamlessly and provides a hook, described in
   946 \S\ref{registration-of-coinductive-datatypes}, to register custom coinductive
   947 datatypes.
   948 
   949 (Co)intuitively, a coinductive datatype is similar to an inductive datatype but
   950 allows infinite objects. Thus, the infinite lists $\textit{ps}$ $=$ $[a, a, a,
   951 \ldots]$, $\textit{qs}$ $=$ $[a, b, a, b, \ldots]$, and $\textit{rs}$ $=$ $[0,
   952 1, 2, 3, \ldots]$ can be defined as lazy lists using the
   953 $\textit{LNil}\mathbin{\Colon}{'}a~\textit{llist}$ and
   954 $\textit{LCons}\mathbin{\Colon}{'}a \mathbin{\Rightarrow} {'}a~\textit{llist}
   955 \mathbin{\Rightarrow} {'}a~\textit{llist}$ constructors.
   956 
   957 Although it is otherwise no friend of infinity, Nitpick can find counterexamples
   958 involving cyclic lists such as \textit{ps} and \textit{qs} above as well as
   959 finite lists:
   960 
   961 \prew
   962 \textbf{lemma} ``$\textit{xs} \not= \textit{LCons}~a~\textit{xs}$'' \\
   963 \textbf{nitpick} \\[2\smallskipamount]
   964 \slshape Nitpick found a counterexample for {\itshape card}~$'a$ = 1: \\[2\smallskipamount]
   965 \hbox{}\qquad Free variables: \nopagebreak \\
   966 \hbox{}\qquad\qquad $\textit{a} = a_1$ \\
   967 \hbox{}\qquad\qquad $\textit{xs} = \textsl{THE}~\omega.\; \omega = \textit{LCons}~a_1~\omega$
   968 \postw
   969 
   970 The notation $\textrm{THE}~\omega.\; \omega = t(\omega)$ stands
   971 for the infinite term $t(t(t(\ldots)))$. Hence, \textit{xs} is simply the
   972 infinite list $[a_1, a_1, a_1, \ldots]$.
   973 
   974 The next example is more interesting:
   975 
   976 \prew
   977 \textbf{lemma}~``$\lbrakk\textit{xs} = \textit{LCons}~a~\textit{xs};\>\,
   978 \textit{ys} = \textit{iterates}~(\lambda b.\> a)~b\rbrakk \,\Longrightarrow\, \textit{xs} = \textit{ys}$'' \\
   979 \textbf{nitpick} [\textit{verbose}] \\[2\smallskipamount]
   980 \slshape The type ``\kern1pt$'a$'' passed the monotonicity test. Nitpick might be able to skip
   981 some scopes. \\[2\smallskipamount]
   982 Trying 8 scopes: \\
   983 \hbox{}\qquad \textit{card} $'a$~= 1, \textit{card} ``\kern1pt$'a~\textit{list\/}$''~= 1,
   984 and \textit{bisim\_depth}~= 0. \\
   985 \hbox{}\qquad $\qquad\vdots$ \\[.5\smallskipamount]
   986 \hbox{}\qquad \textit{card} $'a$~= 8, \textit{card} ``\kern1pt$'a~\textit{list\/}$''~= 8,
   987 and \textit{bisim\_depth}~= 7. \\[2\smallskipamount]
   988 Nitpick found a counterexample for {\itshape card}~$'a$ = 2,
   989 \textit{card}~``\kern1pt$'a~\textit{list\/}$''~= 2, and \textit{bisim\_\allowbreak
   990 depth}~= 1:
   991 \\[2\smallskipamount]
   992 \hbox{}\qquad Free variables: \nopagebreak \\
   993 \hbox{}\qquad\qquad $\textit{a} = a_1$ \\
   994 \hbox{}\qquad\qquad $\textit{b} = a_2$ \\
   995 \hbox{}\qquad\qquad $\textit{xs} = \textsl{THE}~\omega.\; \omega = \textit{LCons}~a_1~\omega$ \\
   996 \hbox{}\qquad\qquad $\textit{ys} = \textit{LCons}~a_2~(\textsl{THE}~\omega.\; \omega = \textit{LCons}~a_1~\omega)$ \\[2\smallskipamount]
   997 Total time: 726 ms.
   998 \postw
   999 
  1000 The lazy list $\textit{xs}$ is simply $[a_1, a_1, a_1, \ldots]$, whereas
  1001 $\textit{ys}$ is $[a_2, a_1, a_1, a_1, \ldots]$, i.e., a lasso-shaped list with
  1002 $[a_2]$ as its stem and $[a_1]$ as its cycle. In general, the list segment
  1003 within the scope of the {THE} binder corresponds to the lasso's cycle, whereas
  1004 the segment leading to the binder is the stem.
  1005 
  1006 A salient property of coinductive datatypes is that two objects are considered
  1007 equal if and only if they lead to the same observations. For example, the lazy
  1008 lists $\textrm{THE}~\omega.\; \omega =
  1009 \textit{LCons}~a~(\textit{LCons}~b~\omega)$ and
  1010 $\textit{LCons}~a~(\textrm{THE}~\omega.\; \omega =
  1011 \textit{LCons}~b~(\textit{LCons}~a~\omega))$ are identical, because both lead
  1012 to the sequence of observations $a$, $b$, $a$, $b$, \hbox{\ldots} (or,
  1013 equivalently, both encode the infinite list $[a, b, a, b, \ldots]$). This
  1014 concept of equality for coinductive datatypes is called bisimulation and is
  1015 defined coinductively.
  1016 
  1017 Internally, Nitpick encodes the coinductive bisimilarity predicate as part of
  1018 the Kodkod problem to ensure that distinct objects lead to different
  1019 observations. This precaution is somewhat expensive and often unnecessary, so it
  1020 can be disabled by setting the \textit{bisim\_depth} option to $-1$. The
  1021 bisimilarity check is then performed \textsl{after} the counterexample has been
  1022 found to ensure correctness. If this after-the-fact check fails, the
  1023 counterexample is tagged as ``quasi genuine'' and Nitpick recommends to try
  1024 again with \textit{bisim\_depth} set to a nonnegative integer. Disabling the
  1025 check for the previous example saves approximately 150~milli\-seconds; the speed
  1026 gains can be more significant for larger scopes.
  1027 
  1028 The next formula illustrates the need for bisimilarity (either as a Kodkod
  1029 predicate or as an after-the-fact check) to prevent spurious counterexamples:
  1030 
  1031 \prew
  1032 \textbf{lemma} ``$\lbrakk xs = \textit{LCons}~a~\textit{xs};\>\, \textit{ys} = \textit{LCons}~a~\textit{ys}\rbrakk
  1033 \,\Longrightarrow\, \textit{xs} = \textit{ys}$'' \\
  1034 \textbf{nitpick} [\textit{bisim\_depth} = $-1$, \textit{show\_datatypes}] \\[2\smallskipamount]
  1035 \slshape Nitpick found a quasi genuine counterexample for $\textit{card}~'a$ = 2: \\[2\smallskipamount]
  1036 \hbox{}\qquad Free variables: \nopagebreak \\
  1037 \hbox{}\qquad\qquad $a = a_1$ \\
  1038 \hbox{}\qquad\qquad $\textit{xs} = \textsl{THE}~\omega.\; \omega =
  1039 \textit{LCons}~a_1~\omega$ \\
  1040 \hbox{}\qquad\qquad $\textit{ys} = \textsl{THE}~\omega.\; \omega = \textit{LCons}~a_1~\omega$ \\
  1041 \hbox{}\qquad Codatatype:\strut \nopagebreak \\
  1042 \hbox{}\qquad\qquad $'a~\textit{llist} =
  1043 \{\!\begin{aligned}[t]
  1044   & \textsl{THE}~\omega.\; \omega = \textit{LCons}~a_1~\omega, \\[-2pt]
  1045   & \textsl{THE}~\omega.\; \omega = \textit{LCons}~a_1~\omega,\> \unr\}\end{aligned}$
  1046 \\[2\smallskipamount]
  1047 Try again with ``\textit{bisim\_depth}'' set to a nonnegative value to confirm
  1048 that the counterexample is genuine. \\[2\smallskipamount]
  1049 {\upshape\textbf{nitpick}} \\[2\smallskipamount]
  1050 \slshape Nitpick found no counterexample.
  1051 \postw
  1052 
  1053 In the first \textbf{nitpick} invocation, the after-the-fact check discovered 
  1054 that the two known elements of type $'a~\textit{llist}$ are bisimilar.
  1055 
  1056 A compromise between leaving out the bisimilarity predicate from the Kodkod
  1057 problem and performing the after-the-fact check is to specify a lower
  1058 nonnegative \textit{bisim\_depth} value than the default one provided by
  1059 Nitpick. In general, a value of $K$ means that Nitpick will require all lists to
  1060 be distinguished from each other by their prefixes of length $K$. Be aware that
  1061 setting $K$ to a too low value can overconstrain Nitpick, preventing it from
  1062 finding any counterexamples.
  1063 
  1064 \subsection{Boxing}
  1065 \label{boxing}
  1066 
  1067 Nitpick normally maps function and product types directly to the corresponding
  1068 Kodkod concepts. As a consequence, if $'a$ has cardinality 3 and $'b$ has
  1069 cardinality 4, then $'a \times {'}b$ has cardinality 12 ($= 4 \times 3$) and $'a
  1070 \Rightarrow {'}b$ has cardinality 64 ($= 4^3$). In some circumstances, it pays
  1071 off to treat these types in the same way as plain datatypes, by approximating
  1072 them by a subset of a given cardinality. This technique is called ``boxing'' and
  1073 is particularly useful for functions passed as arguments to other functions, for
  1074 high-arity functions, and for large tuples. Under the hood, boxing involves
  1075 wrapping occurrences of the types $'a \times {'}b$ and $'a \Rightarrow {'}b$ in
  1076 isomorphic datatypes, as can be seen by enabling the \textit{debug} option.
  1077 
  1078 To illustrate boxing, we consider a formalization of $\lambda$-terms represented
  1079 using de Bruijn's notation:
  1080 
  1081 \prew
  1082 \textbf{datatype} \textit{tm} = \textit{Var}~\textit{nat}~$\mid$~\textit{Lam}~\textit{tm} $\mid$ \textit{App~tm~tm}
  1083 \postw
  1084 
  1085 The $\textit{lift}~t~k$ function increments all variables with indices greater
  1086 than or equal to $k$ by one:
  1087 
  1088 \prew
  1089 \textbf{primrec} \textit{lift} \textbf{where} \\
  1090 ``$\textit{lift}~(\textit{Var}~j)~k = \textit{Var}~(\textrm{if}~j < k~\textrm{then}~j~\textrm{else}~j + 1)$'' $\mid$ \\
  1091 ``$\textit{lift}~(\textit{Lam}~t)~k = \textit{Lam}~(\textit{lift}~t~(k + 1))$'' $\mid$ \\
  1092 ``$\textit{lift}~(\textit{App}~t~u)~k = \textit{App}~(\textit{lift}~t~k)~(\textit{lift}~u~k)$''
  1093 \postw
  1094 
  1095 The $\textit{loose}~t~k$ predicate returns \textit{True} if and only if
  1096 term $t$ has a loose variable with index $k$ or more:
  1097 
  1098 \prew
  1099 \textbf{primrec}~\textit{loose} \textbf{where} \\
  1100 ``$\textit{loose}~(\textit{Var}~j)~k = (j \ge k)$'' $\mid$ \\
  1101 ``$\textit{loose}~(\textit{Lam}~t)~k = \textit{loose}~t~(\textit{Suc}~k)$'' $\mid$ \\
  1102 ``$\textit{loose}~(\textit{App}~t~u)~k = (\textit{loose}~t~k \mathrel{\lor} \textit{loose}~u~k)$''
  1103 \postw
  1104 
  1105 Next, the $\textit{subst}~\sigma~t$ function applies the substitution $\sigma$
  1106 on $t$:
  1107 
  1108 \prew
  1109 \textbf{primrec}~\textit{subst} \textbf{where} \\
  1110 ``$\textit{subst}~\sigma~(\textit{Var}~j) = \sigma~j$'' $\mid$ \\
  1111 ``$\textit{subst}~\sigma~(\textit{Lam}~t) = {}$\phantom{''} \\
  1112 \phantom{``}$\textit{Lam}~(\textit{subst}~(\lambda n.\> \textrm{case}~n~\textrm{of}~0 \Rightarrow \textit{Var}~0 \mid \textit{Suc}~m \Rightarrow \textit{lift}~(\sigma~m)~1)~t)$'' $\mid$ \\
  1113 ``$\textit{subst}~\sigma~(\textit{App}~t~u) = \textit{App}~(\textit{subst}~\sigma~t)~(\textit{subst}~\sigma~u)$''
  1114 \postw
  1115 
  1116 A substitution is a function that maps variable indices to terms. Observe that
  1117 $\sigma$ is a function passed as argument and that Nitpick can't optimize it
  1118 away, because the recursive call for the \textit{Lam} case involves an altered
  1119 version. Also notice the \textit{lift} call, which increments the variable
  1120 indices when moving under a \textit{Lam}.
  1121 
  1122 A reasonable property to expect of substitution is that it should leave closed
  1123 terms unchanged. Alas, even this simple property does not hold:
  1124 
  1125 \pre
  1126 \textbf{lemma}~``$\lnot\,\textit{loose}~t~0 \,\Longrightarrow\, \textit{subst}~\sigma~t = t$'' \\
  1127 \textbf{nitpick} [\textit{verbose}] \\[2\smallskipamount]
  1128 \slshape
  1129 Trying 8 scopes: \nopagebreak \\
  1130 \hbox{}\qquad \textit{card~nat}~= 1, \textit{card tm}~= 1, and \textit{card} ``$\textit{nat} \Rightarrow \textit{tm}$'' = 1; \\
  1131 \hbox{}\qquad \textit{card~nat}~= 2, \textit{card tm}~= 2, and \textit{card} ``$\textit{nat} \Rightarrow \textit{tm}$'' = 2; \\
  1132 \hbox{}\qquad $\qquad\vdots$ \\[.5\smallskipamount]
  1133 \hbox{}\qquad \textit{card~nat}~= 8, \textit{card tm}~= 8, and \textit{card} ``$\textit{nat} \Rightarrow \textit{tm}$'' = 8. \\[2\smallskipamount]
  1134 Nitpick found a counterexample for \textit{card~nat}~= 6, \textit{card~tm}~= 6,
  1135 and \textit{card}~``$\textit{nat} \Rightarrow \textit{tm}$''~= 6: \\[2\smallskipamount]
  1136 \hbox{}\qquad Free variables: \nopagebreak \\
  1137 \hbox{}\qquad\qquad $\sigma = \undef(\!\begin{aligned}[t]
  1138 & 0 := \textit{Var}~0,\>
  1139   1 := \textit{Var}~0,\>
  1140   2 := \textit{Var}~0, \\[-2pt]
  1141 & 3 := \textit{Var}~0,\>
  1142   4 := \textit{Var}~0,\>
  1143   5 := \textit{Var}~0)\end{aligned}$ \\
  1144 \hbox{}\qquad\qquad $t = \textit{Lam}~(\textit{Lam}~(\textit{Var}~1))$ \\[2\smallskipamount]
  1145 Total time: $4679$ ms.
  1146 \postw
  1147 
  1148 Using \textit{eval}, we find out that $\textit{subst}~\sigma~t =
  1149 \textit{Lam}~(\textit{Lam}~(\textit{Var}~0))$. Using the traditional
  1150 $\lambda$-term notation, $t$~is
  1151 $\lambda x\, y.\> x$ whereas $\textit{subst}~\sigma~t$ is $\lambda x\, y.\> y$.
  1152 The bug is in \textit{subst\/}: The $\textit{lift}~(\sigma~m)~1$ call should be
  1153 replaced with $\textit{lift}~(\sigma~m)~0$.
  1154 
  1155 An interesting aspect of Nitpick's verbose output is that it assigned inceasing
  1156 cardinalities from 1 to 8 to the type $\textit{nat} \Rightarrow \textit{tm}$.
  1157 For the formula of interest, knowing 6 values of that type was enough to find
  1158 the counterexample. Without boxing, $46\,656$ ($= 6^6$) values must be
  1159 considered, a hopeless undertaking:
  1160 
  1161 \prew
  1162 \textbf{nitpick} [\textit{dont\_box}] \\[2\smallskipamount]
  1163 {\slshape Nitpick ran out of time after checking 4 of 8 scopes.}
  1164 \postw
  1165 
  1166 {\looseness=-1
  1167 Boxing can be enabled or disabled globally or on a per-type basis using the
  1168 \textit{box} option. Nitpick usually performs reasonable choices about which
  1169 types should be boxed, but option tweaking sometimes helps. A related optimization,
  1170 ``finalization,'' attempts to wrap functions that constant at all but finitely
  1171 many points (e.g., finite sets); see the documentation for the \textit{finalize}
  1172 option in \S\ref{scope-of-search} for details.
  1173 
  1174 }
  1175 
  1176 \subsection{Scope Monotonicity}
  1177 \label{scope-monotonicity}
  1178 
  1179 The \textit{card} option (together with \textit{iter}, \textit{bisim\_depth},
  1180 and \textit{max}) controls which scopes are actually tested. In general, to
  1181 exhaust all models below a certain cardinality bound, the number of scopes that
  1182 Nitpick must consider increases exponentially with the number of type variables
  1183 (and \textbf{typedecl}'d types) occurring in the formula. Given the default
  1184 cardinality specification of 1--8, no fewer than $8^4 = 4096$ scopes must be
  1185 considered for a formula involving $'a$, $'b$, $'c$, and $'d$.
  1186 
  1187 Fortunately, many formulas exhibit a property called \textsl{scope
  1188 monotonicity}, meaning that if the formula is falsifiable for a given scope,
  1189 it is also falsifiable for all larger scopes \cite[p.~165]{jackson-2006}.
  1190 
  1191 Consider the formula
  1192 
  1193 \prew
  1194 \textbf{lemma}~``$\textit{length~xs} = \textit{length~ys} \,\Longrightarrow\, \textit{rev}~(\textit{zip~xs~ys}) = \textit{zip~xs}~(\textit{rev~ys})$''
  1195 \postw
  1196 
  1197 where \textit{xs} is of type $'a~\textit{list}$ and \textit{ys} is of type
  1198 $'b~\textit{list}$. A priori, Nitpick would need to consider 512 scopes to
  1199 exhaust the specification \textit{card}~= 1--8. However, our intuition tells us
  1200 that any counterexample found with a small scope would still be a counterexample
  1201 in a larger scope---by simply ignoring the fresh $'a$ and $'b$ values provided
  1202 by the larger scope. Nitpick comes to the same conclusion after a careful
  1203 inspection of the formula and the relevant definitions:
  1204 
  1205 \prew
  1206 \textbf{nitpick}~[\textit{verbose}] \\[2\smallskipamount]
  1207 \slshape
  1208 The types ``\kern1pt$'a$'' and ``\kern1pt$'b$'' passed the monotonicity test.
  1209 Nitpick might be able to skip some scopes.
  1210  \\[2\smallskipamount]
  1211 Trying 8 scopes: \\
  1212 \hbox{}\qquad \textit{card} $'a$~= 1, \textit{card} $'b$~= 1,
  1213 \textit{card} \textit{nat}~= 1, \textit{card} ``$('a \times {'}b)$
  1214 \textit{list}''~= 1, \\
  1215 \hbox{}\qquad\quad \textit{card} ``\kern1pt$'a$ \textit{list}''~= 1, and
  1216 \textit{card} ``\kern1pt$'b$ \textit{list}''~= 1. \\
  1217 \hbox{}\qquad \textit{card} $'a$~= 2, \textit{card} $'b$~= 2,
  1218 \textit{card} \textit{nat}~= 2, \textit{card} ``$('a \times {'}b)$
  1219 \textit{list}''~= 2, \\
  1220 \hbox{}\qquad\quad \textit{card} ``\kern1pt$'a$ \textit{list}''~= 2, and
  1221 \textit{card} ``\kern1pt$'b$ \textit{list}''~= 2. \\
  1222 \hbox{}\qquad $\qquad\vdots$ \\[.5\smallskipamount]
  1223 \hbox{}\qquad \textit{card} $'a$~= 8, \textit{card} $'b$~= 8,
  1224 \textit{card} \textit{nat}~= 8, \textit{card} ``$('a \times {'}b)$
  1225 \textit{list}''~= 8, \\
  1226 \hbox{}\qquad\quad \textit{card} ``\kern1pt$'a$ \textit{list}''~= 8, and
  1227 \textit{card} ``\kern1pt$'b$ \textit{list}''~= 8.
  1228 \\[2\smallskipamount]
  1229 Nitpick found a counterexample for
  1230 \textit{card} $'a$~= 5, \textit{card} $'b$~= 5,
  1231 \textit{card} \textit{nat}~= 5, \textit{card} ``$('a \times {'}b)$
  1232 \textit{list}''~= 5, \textit{card} ``\kern1pt$'a$ \textit{list}''~= 5, and
  1233 \textit{card} ``\kern1pt$'b$ \textit{list}''~= 5:
  1234 \\[2\smallskipamount]
  1235 \hbox{}\qquad Free variables: \nopagebreak \\
  1236 \hbox{}\qquad\qquad $\textit{xs} = [a_1, a_2]$ \\
  1237 \hbox{}\qquad\qquad $\textit{ys} = [b_1, b_1]$ \\[2\smallskipamount]
  1238 Total time: 1636 ms.
  1239 \postw
  1240 
  1241 In theory, it should be sufficient to test a single scope:
  1242 
  1243 \prew
  1244 \textbf{nitpick}~[\textit{card}~= 8]
  1245 \postw
  1246 
  1247 However, this is often less efficient in practice and may lead to overly complex
  1248 counterexamples.
  1249 
  1250 If the monotonicity check fails but we believe that the formula is monotonic (or
  1251 we don't mind missing some counterexamples), we can pass the
  1252 \textit{mono} option. To convince yourself that this option is risky,
  1253 simply consider this example from \S\ref{skolemization}:
  1254 
  1255 \prew
  1256 \textbf{lemma} ``$\exists g.\; \forall x\Colon 'b.~g~(f~x) = x
  1257  \,\Longrightarrow\, \forall y\Colon {'}a.\; \exists x.~y = f~x$'' \\
  1258 \textbf{nitpick} [\textit{mono}] \\[2\smallskipamount]
  1259 {\slshape Nitpick found no counterexample.} \\[2\smallskipamount]
  1260 \textbf{nitpick} \\[2\smallskipamount]
  1261 \slshape
  1262 Nitpick found a counterexample for \textit{card} $'a$~= 2 and \textit{card} $'b$~=~1: \\
  1263 \hbox{}\qquad $\vdots$
  1264 \postw
  1265 
  1266 (It turns out the formula holds if and only if $\textit{card}~'a \le
  1267 \textit{card}~'b$.) Although this is rarely advisable, the automatic
  1268 monotonicity checks can be disabled by passing \textit{non\_mono}
  1269 (\S\ref{optimizations}).
  1270 
  1271 As insinuated in \S\ref{natural-numbers-and-integers} and
  1272 \S\ref{inductive-datatypes}, \textit{nat}, \textit{int}, and inductive datatypes
  1273 are normally monotonic and treated as such. The same is true for record types,
  1274 \textit{rat}, \textit{real}, and some \textbf{typedef}'d types. Thus, given the
  1275 cardinality specification 1--8, a formula involving \textit{nat}, \textit{int},
  1276 \textit{int~list}, \textit{rat}, and \textit{rat~list} will lead Nitpick to
  1277 consider only 8~scopes instead of $32\,768$.
  1278 
  1279 \subsection{Inductive Properties}
  1280 \label{inductive-properties}
  1281 
  1282 Inductive properties are a particular pain to prove, because the failure to
  1283 establish an induction step can mean several things:
  1284 %
  1285 \begin{enumerate}
  1286 \item The property is invalid.
  1287 \item The property is valid but is too weak to support the induction step.
  1288 \item The property is valid and strong enough; it's just that we haven't found
  1289 the proof yet.
  1290 \end{enumerate}
  1291 %
  1292 Depending on which scenario applies, we would take the appropriate course of
  1293 action:
  1294 %
  1295 \begin{enumerate}
  1296 \item Repair the statement of the property so that it becomes valid.
  1297 \item Generalize the property and/or prove auxiliary properties.
  1298 \item Work harder on a proof.
  1299 \end{enumerate}
  1300 %
  1301 How can we distinguish between the three scenarios? Nitpick's normal mode of
  1302 operation can often detect scenario 1, and Isabelle's automatic tactics help with
  1303 scenario 3. Using appropriate techniques, it is also often possible to use
  1304 Nitpick to identify scenario 2. Consider the following transition system,
  1305 in which natural numbers represent states:
  1306 
  1307 \prew
  1308 \textbf{inductive\_set}~\textit{reach}~\textbf{where} \\
  1309 ``$(4\Colon\textit{nat}) \in \textit{reach\/}$'' $\mid$ \\
  1310 ``$\lbrakk n < 4;\> n \in \textit{reach\/}\rbrakk \,\Longrightarrow\, 3 * n + 1 \in \textit{reach\/}$'' $\mid$ \\
  1311 ``$n \in \textit{reach} \,\Longrightarrow n + 2 \in \textit{reach\/}$''
  1312 \postw
  1313 
  1314 We will try to prove that only even numbers are reachable:
  1315 
  1316 \prew
  1317 \textbf{lemma}~``$n \in \textit{reach} \,\Longrightarrow\, 2~\textrm{dvd}~n$''
  1318 \postw
  1319 
  1320 Does this property hold? Nitpick cannot find a counterexample within 30 seconds,
  1321 so let's attempt a proof by induction:
  1322 
  1323 \prew
  1324 \textbf{apply}~(\textit{induct~set}{:}~\textit{reach\/}) \\
  1325 \textbf{apply}~\textit{auto}
  1326 \postw
  1327 
  1328 This leaves us in the following proof state:
  1329 
  1330 \prew
  1331 {\slshape goal (2 subgoals): \\
  1332 \phantom{0}1. ${\bigwedge}n.\;\, \lbrakk n \in \textit{reach\/};\, n < 4;\, 2~\textsl{dvd}~n\rbrakk \,\Longrightarrow\, 2~\textsl{dvd}~\textit{Suc}~(3 * n)$ \\
  1333 \phantom{0}2. ${\bigwedge}n.\;\, \lbrakk n \in \textit{reach\/};\, 2~\textsl{dvd}~n\rbrakk \,\Longrightarrow\, 2~\textsl{dvd}~\textit{Suc}~(\textit{Suc}~n)$
  1334 }
  1335 \postw
  1336 
  1337 If we run Nitpick on the first subgoal, it still won't find any
  1338 counterexample; and yet, \textit{auto} fails to go further, and \textit{arith}
  1339 is helpless. However, notice the $n \in \textit{reach}$ assumption, which
  1340 strengthens the induction hypothesis but is not immediately usable in the proof.
  1341 If we remove it and invoke Nitpick, this time we get a counterexample:
  1342 
  1343 \prew
  1344 \textbf{apply}~(\textit{thin\_tac}~``$n \in \textit{reach\/}$'') \\
  1345 \textbf{nitpick} \\[2\smallskipamount]
  1346 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
  1347 \hbox{}\qquad Skolem constant: \nopagebreak \\
  1348 \hbox{}\qquad\qquad $n = 0$
  1349 \postw
  1350 
  1351 Indeed, 0 < 4, 2 divides 0, but 2 does not divide 1. We can use this information
  1352 to strength the lemma:
  1353 
  1354 \prew
  1355 \textbf{lemma}~``$n \in \textit{reach} \,\Longrightarrow\, 2~\textrm{dvd}~n \mathrel{\lor} n \not= 0$''
  1356 \postw
  1357 
  1358 Unfortunately, the proof by induction still gets stuck, except that Nitpick now
  1359 finds the counterexample $n = 2$. We generalize the lemma further to
  1360 
  1361 \prew
  1362 \textbf{lemma}~``$n \in \textit{reach} \,\Longrightarrow\, 2~\textrm{dvd}~n \mathrel{\lor} n \ge 4$''
  1363 \postw
  1364 
  1365 and this time \textit{arith} can finish off the subgoals.
  1366 
  1367 A similar technique can be employed for structural induction. The
  1368 following mini formalization of full binary trees will serve as illustration:
  1369 
  1370 \prew
  1371 \textbf{datatype} $\kern1pt'a$~\textit{bin\_tree} = $\textit{Leaf}~{\kern1pt'a}$ $\mid$ $\textit{Branch}$ ``\kern1pt$'a$ \textit{bin\_tree}'' ``\kern1pt$'a$ \textit{bin\_tree}'' \\[2\smallskipamount]
  1372 \textbf{primrec}~\textit{labels}~\textbf{where} \\
  1373 ``$\textit{labels}~(\textit{Leaf}~a) = \{a\}$'' $\mid$ \\
  1374 ``$\textit{labels}~(\textit{Branch}~t~u) = \textit{labels}~t \mathrel{\cup} \textit{labels}~u$'' \\[2\smallskipamount]
  1375 \textbf{primrec}~\textit{swap}~\textbf{where} \\
  1376 ``$\textit{swap}~(\textit{Leaf}~c)~a~b =$ \\
  1377 \phantom{``}$(\textrm{if}~c = a~\textrm{then}~\textit{Leaf}~b~\textrm{else~if}~c = b~\textrm{then}~\textit{Leaf}~a~\textrm{else}~\textit{Leaf}~c)$'' $\mid$ \\
  1378 ``$\textit{swap}~(\textit{Branch}~t~u)~a~b = \textit{Branch}~(\textit{swap}~t~a~b)~(\textit{swap}~u~a~b)$''
  1379 \postw
  1380 
  1381 The \textit{labels} function returns the set of labels occurring on leaves of a
  1382 tree, and \textit{swap} exchanges two labels. Intuitively, if two distinct
  1383 labels $a$ and $b$ occur in a tree $t$, they should also occur in the tree
  1384 obtained by swapping $a$ and $b$:
  1385 
  1386 \prew
  1387 \textbf{lemma} $``\{a, b\} \subseteq \textit{labels}~t \,\Longrightarrow\, \textit{labels}~(\textit{swap}~t~a~b) = \textit{labels}~t$''
  1388 \postw
  1389 
  1390 Nitpick can't find any counterexample, so we proceed with induction
  1391 (this time favoring a more structured style):
  1392 
  1393 \prew
  1394 \textbf{proof}~(\textit{induct}~$t$) \\
  1395 \hbox{}\quad \textbf{case}~\textit{Leaf}~\textbf{thus}~\textit{?case}~\textbf{by}~\textit{simp} \\
  1396 \textbf{next} \\
  1397 \hbox{}\quad \textbf{case}~$(\textit{Branch}~t~u)$~\textbf{thus} \textit{?case}
  1398 \postw
  1399 
  1400 Nitpick can't find any counterexample at this point either, but it makes the
  1401 following suggestion:
  1402 
  1403 \prew
  1404 \slshape
  1405 Hint: To check that the induction hypothesis is general enough, try this command:
  1406 \textbf{nitpick}~[\textit{non\_std}, \textit{show\_all}].
  1407 \postw
  1408 
  1409 If we follow the hint, we get a ``nonstandard'' counterexample for the step:
  1410 
  1411 \prew
  1412 \slshape Nitpick found a nonstandard counterexample for \textit{card} $'a$ = 3: \\[2\smallskipamount]
  1413 \hbox{}\qquad Free variables: \nopagebreak \\
  1414 \hbox{}\qquad\qquad $a = a_1$ \\
  1415 \hbox{}\qquad\qquad $b = a_2$ \\
  1416 \hbox{}\qquad\qquad $t = \xi_1$ \\
  1417 \hbox{}\qquad\qquad $u = \xi_2$ \\
  1418 \hbox{}\qquad Datatype: \nopagebreak \\
  1419 \hbox{}\qquad\qquad $\alpha~\textit{btree} = \{\xi_1 \mathbin{=} \textit{Branch}~\xi_1~\xi_1,\> \xi_2 \mathbin{=} \textit{Branch}~\xi_2~\xi_2,\> \textit{Branch}~\xi_1~\xi_2\}$ \\
  1420 \hbox{}\qquad {\slshape Constants:} \nopagebreak \\
  1421 \hbox{}\qquad\qquad $\textit{labels} = \undef
  1422     (\!\begin{aligned}[t]%
  1423     & \xi_1 := \{a_2, a_3\},\> \xi_2 := \{a_1\},\> \\[-2pt]
  1424     & \textit{Branch}~\xi_1~\xi_2 := \{a_1, a_2, a_3\})\end{aligned}$ \\
  1425 \hbox{}\qquad\qquad $\lambda x_1.\> \textit{swap}~x_1~a~b = \undef
  1426     (\!\begin{aligned}[t]%
  1427     & \xi_1 := \xi_2,\> \xi_2 := \xi_2, \\[-2pt]
  1428     & \textit{Branch}~\xi_1~\xi_2 := \xi_2)\end{aligned}$ \\[2\smallskipamount]
  1429 The existence of a nonstandard model suggests that the induction hypothesis is not general enough or perhaps
  1430 even wrong. See the ``Inductive Properties'' section of the Nitpick manual for details (``\textit{isabelle doc nitpick}'').
  1431 \postw
  1432 
  1433 Reading the Nitpick manual is a most excellent idea.
  1434 But what's going on? The \textit{non\_std} option told the tool to look for
  1435 nonstandard models of binary trees, which means that new ``nonstandard'' trees
  1436 $\xi_1, \xi_2, \ldots$, are now allowed in addition to the standard trees
  1437 generated by the \textit{Leaf} and \textit{Branch} constructors.%
  1438 \footnote{Notice the similarity between allowing nonstandard trees here and
  1439 allowing unreachable states in the preceding example (by removing the ``$n \in
  1440 \textit{reach\/}$'' assumption). In both cases, we effectively enlarge the
  1441 set of objects over which the induction is performed while doing the step
  1442 in order to test the induction hypothesis's strength.}
  1443 Unlike standard trees, these new trees contain cycles. We will see later that
  1444 every property of acyclic trees that can be proved without using induction also
  1445 holds for cyclic trees. Hence,
  1446 %
  1447 \begin{quote}
  1448 \textsl{If the induction
  1449 hypothesis is strong enough, the induction step will hold even for nonstandard
  1450 objects, and Nitpick won't find any nonstandard counterexample.}
  1451 \end{quote}
  1452 %
  1453 But here the tool find some nonstandard trees $t = \xi_1$
  1454 and $u = \xi_2$ such that $a \notin \textit{labels}~t$, $b \in
  1455 \textit{labels}~t$, $a \in \textit{labels}~u$, and $b \notin \textit{labels}~u$.
  1456 Because neither tree contains both $a$ and $b$, the induction hypothesis tells
  1457 us nothing about the labels of $\textit{swap}~t~a~b$ and $\textit{swap}~u~a~b$,
  1458 and as a result we know nothing about the labels of the tree
  1459 $\textit{swap}~(\textit{Branch}~t~u)~a~b$, which by definition equals
  1460 $\textit{Branch}$ $(\textit{swap}~t~a~b)$ $(\textit{swap}~u~a~b)$, whose
  1461 labels are $\textit{labels}$ $(\textit{swap}~t~a~b) \mathrel{\cup}
  1462 \textit{labels}$ $(\textit{swap}~u~a~b)$.
  1463 
  1464 The solution is to ensure that we always know what the labels of the subtrees
  1465 are in the inductive step, by covering the cases where $a$ and/or~$b$ is not in
  1466 $t$ in the statement of the lemma:
  1467 
  1468 \prew
  1469 \textbf{lemma} ``$\textit{labels}~(\textit{swap}~t~a~b) = {}$ \\
  1470 \phantom{\textbf{lemma} ``}$(\textrm{if}~a \in \textit{labels}~t~\textrm{then}$ \nopagebreak \\
  1471 \phantom{\textbf{lemma} ``(\quad}$\textrm{if}~b \in \textit{labels}~t~\textrm{then}~\textit{labels}~t~\textrm{else}~(\textit{labels}~t - \{a\}) \mathrel{\cup} \{b\}$ \\
  1472 \phantom{\textbf{lemma} ``(}$\textrm{else}$ \\
  1473 \phantom{\textbf{lemma} ``(\quad}$\textrm{if}~b \in \textit{labels}~t~\textrm{then}~(\textit{labels}~t - \{b\}) \mathrel{\cup} \{a\}~\textrm{else}~\textit{labels}~t)$''
  1474 \postw
  1475 
  1476 This time, Nitpick won't find any nonstandard counterexample, and we can perform
  1477 the induction step using \textit{auto}.
  1478 
  1479 \section{Case Studies}
  1480 \label{case-studies}
  1481 
  1482 As a didactic device, the previous section focused mostly on toy formulas whose
  1483 validity can easily be assessed just by looking at the formula. We will now
  1484 review two somewhat more realistic case studies that are within Nitpick's
  1485 reach:\ a context-free grammar modeled by mutually inductive sets and a
  1486 functional implementation of AA trees. The results presented in this
  1487 section were produced with the following settings:
  1488 
  1489 \prew
  1490 \textbf{nitpick\_params} [\textit{max\_potential}~= 0,\, \textit{max\_threads} = 2]
  1491 \postw
  1492 
  1493 \subsection{A Context-Free Grammar}
  1494 \label{a-context-free-grammar}
  1495 
  1496 Our first case study is taken from section 7.4 in the Isabelle tutorial
  1497 \cite{isa-tutorial}. The following grammar, originally due to Hopcroft and
  1498 Ullman, produces all strings with an equal number of $a$'s and $b$'s:
  1499 
  1500 \prew
  1501 \begin{tabular}{@{}r@{$\;\,$}c@{$\;\,$}l@{}}
  1502 $S$ & $::=$ & $\epsilon \mid bA \mid aB$ \\
  1503 $A$ & $::=$ & $aS \mid bAA$ \\
  1504 $B$ & $::=$ & $bS \mid aBB$
  1505 \end{tabular}
  1506 \postw
  1507 
  1508 The intuition behind the grammar is that $A$ generates all string with one more
  1509 $a$ than $b$'s and $B$ generates all strings with one more $b$ than $a$'s.
  1510 
  1511 The alphabet consists exclusively of $a$'s and $b$'s:
  1512 
  1513 \prew
  1514 \textbf{datatype} \textit{alphabet}~= $a$ $\mid$ $b$
  1515 \postw
  1516 
  1517 Strings over the alphabet are represented by \textit{alphabet list}s.
  1518 Nonterminals in the grammar become sets of strings. The production rules
  1519 presented above can be expressed as a mutually inductive definition:
  1520 
  1521 \prew
  1522 \textbf{inductive\_set} $S$ \textbf{and} $A$ \textbf{and} $B$ \textbf{where} \\
  1523 \textit{R1}:\kern.4em ``$[] \in S$'' $\,\mid$ \\
  1524 \textit{R2}:\kern.4em ``$w \in A\,\Longrightarrow\, b \mathbin{\#} w \in S$'' $\,\mid$ \\
  1525 \textit{R3}:\kern.4em ``$w \in B\,\Longrightarrow\, a \mathbin{\#} w \in S$'' $\,\mid$ \\
  1526 \textit{R4}:\kern.4em ``$w \in S\,\Longrightarrow\, a \mathbin{\#} w \in A$'' $\,\mid$ \\
  1527 \textit{R5}:\kern.4em ``$w \in S\,\Longrightarrow\, b \mathbin{\#} w \in S$'' $\,\mid$ \\
  1528 \textit{R6}:\kern.4em ``$\lbrakk v \in B;\> v \in B\rbrakk \,\Longrightarrow\, a \mathbin{\#} v \mathbin{@} w \in B$''
  1529 \postw
  1530 
  1531 The conversion of the grammar into the inductive definition was done manually by
  1532 Joe Blow, an underpaid undergraduate student. As a result, some errors might
  1533 have sneaked in.
  1534 
  1535 Debugging faulty specifications is at the heart of Nitpick's \textsl{raison
  1536 d'\^etre}. A good approach is to state desirable properties of the specification
  1537 (here, that $S$ is exactly the set of strings over $\{a, b\}$ with as many $a$'s
  1538 as $b$'s) and check them with Nitpick. If the properties are correctly stated,
  1539 counterexamples will point to bugs in the specification. For our grammar
  1540 example, we will proceed in two steps, separating the soundness and the
  1541 completeness of the set $S$. First, soundness:
  1542 
  1543 \prew
  1544 \textbf{theorem}~\textit{S\_sound\/}: \\
  1545 ``$w \in S \longrightarrow \textit{length}~[x\mathbin{\leftarrow} w.\; x = a] =
  1546   \textit{length}~[x\mathbin{\leftarrow} w.\; x = b]$'' \\
  1547 \textbf{nitpick} \\[2\smallskipamount]
  1548 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
  1549 \hbox{}\qquad Free variable: \nopagebreak \\
  1550 \hbox{}\qquad\qquad $w = [b]$
  1551 \postw
  1552 
  1553 It would seem that $[b] \in S$. How could this be? An inspection of the
  1554 introduction rules reveals that the only rule with a right-hand side of the form
  1555 $b \mathbin{\#} {\ldots} \in S$ that could have introduced $[b]$ into $S$ is
  1556 \textit{R5}:
  1557 
  1558 \prew
  1559 ``$w \in S\,\Longrightarrow\, b \mathbin{\#} w \in S$''
  1560 \postw
  1561 
  1562 On closer inspection, we can see that this rule is wrong. To match the
  1563 production $B ::= bS$, the second $S$ should be a $B$. We fix the typo and try
  1564 again:
  1565 
  1566 \prew
  1567 \textbf{nitpick} \\[2\smallskipamount]
  1568 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
  1569 \hbox{}\qquad Free variable: \nopagebreak \\
  1570 \hbox{}\qquad\qquad $w = [a, a, b]$
  1571 \postw
  1572 
  1573 Some detective work is necessary to find out what went wrong here. To get $[a,
  1574 a, b] \in S$, we need $[a, b] \in B$ by \textit{R3}, which in turn can only come
  1575 from \textit{R6}:
  1576 
  1577 \prew
  1578 ``$\lbrakk v \in B;\> v \in B\rbrakk \,\Longrightarrow\, a \mathbin{\#} v \mathbin{@} w \in B$''
  1579 \postw
  1580 
  1581 Now, this formula must be wrong: The same assumption occurs twice, and the
  1582 variable $w$ is unconstrained. Clearly, one of the two occurrences of $v$ in
  1583 the assumptions should have been a $w$.
  1584 
  1585 With the correction made, we don't get any counterexample from Nitpick. Let's
  1586 move on and check completeness:
  1587 
  1588 \prew
  1589 \textbf{theorem}~\textit{S\_complete}: \\
  1590 ``$\textit{length}~[x\mathbin{\leftarrow} w.\; x = a] =
  1591    \textit{length}~[x\mathbin{\leftarrow} w.\; x = b]
  1592   \longrightarrow w \in S$'' \\
  1593 \textbf{nitpick} \\[2\smallskipamount]
  1594 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
  1595 \hbox{}\qquad Free variable: \nopagebreak \\
  1596 \hbox{}\qquad\qquad $w = [b, b, a, a]$
  1597 \postw
  1598 
  1599 Apparently, $[b, b, a, a] \notin S$, even though it has the same numbers of
  1600 $a$'s and $b$'s. But since our inductive definition passed the soundness check,
  1601 the introduction rules we have are probably correct. Perhaps we simply lack an
  1602 introduction rule. Comparing the grammar with the inductive definition, our
  1603 suspicion is confirmed: Joe Blow simply forgot the production $A ::= bAA$,
  1604 without which the grammar cannot generate two or more $b$'s in a row. So we add
  1605 the rule
  1606 
  1607 \prew
  1608 ``$\lbrakk v \in A;\> w \in A\rbrakk \,\Longrightarrow\, b \mathbin{\#} v \mathbin{@} w \in A$''
  1609 \postw
  1610 
  1611 With this last change, we don't get any counterexamples from Nitpick for either
  1612 soundness or completeness. We can even generalize our result to cover $A$ and
  1613 $B$ as well:
  1614 
  1615 \prew
  1616 \textbf{theorem} \textit{S\_A\_B\_sound\_and\_complete}: \\
  1617 ``$w \in S \longleftrightarrow \textit{length}~[x \mathbin{\leftarrow} w.\; x = a] = \textit{length}~[x \mathbin{\leftarrow} w.\; x = b]$'' \\
  1618 ``$w \in A \longleftrightarrow \textit{length}~[x \mathbin{\leftarrow} w.\; x = a] = \textit{length}~[x \mathbin{\leftarrow} w.\; x = b] + 1$'' \\
  1619 ``$w \in B \longleftrightarrow \textit{length}~[x \mathbin{\leftarrow} w.\; x = b] = \textit{length}~[x \mathbin{\leftarrow} w.\; x = a] + 1$'' \\
  1620 \textbf{nitpick} \\[2\smallskipamount]
  1621 \slshape Nitpick ran out of time after checking 7 of 8 scopes.
  1622 \postw
  1623 
  1624 \subsection{AA Trees}
  1625 \label{aa-trees}
  1626 
  1627 AA trees are a kind of balanced trees discovered by Arne Andersson that provide
  1628 similar performance to red-black trees, but with a simpler implementation
  1629 \cite{andersson-1993}. They can be used to store sets of elements equipped with
  1630 a total order $<$. We start by defining the datatype and some basic extractor
  1631 functions:
  1632 
  1633 \prew
  1634 \textbf{datatype} $'a$~\textit{aa\_tree} = \\
  1635 \hbox{}\quad $\Lambda$ $\mid$ $N$ ``\kern1pt$'a\Colon \textit{linorder}$'' \textit{nat} ``\kern1pt$'a$ \textit{aa\_tree}'' ``\kern1pt$'a$ \textit{aa\_tree}''  \\[2\smallskipamount]
  1636 \textbf{primrec} \textit{data} \textbf{where} \\
  1637 ``$\textit{data}~\Lambda = \undef$'' $\,\mid$ \\
  1638 ``$\textit{data}~(N~x~\_~\_~\_) = x$'' \\[2\smallskipamount]
  1639 \textbf{primrec} \textit{dataset} \textbf{where} \\
  1640 ``$\textit{dataset}~\Lambda = \{\}$'' $\,\mid$ \\
  1641 ``$\textit{dataset}~(N~x~\_~t~u) = \{x\} \cup \textit{dataset}~t \mathrel{\cup} \textit{dataset}~u$'' \\[2\smallskipamount]
  1642 \textbf{primrec} \textit{level} \textbf{where} \\
  1643 ``$\textit{level}~\Lambda = 0$'' $\,\mid$ \\
  1644 ``$\textit{level}~(N~\_~k~\_~\_) = k$'' \\[2\smallskipamount]
  1645 \textbf{primrec} \textit{left} \textbf{where} \\
  1646 ``$\textit{left}~\Lambda = \Lambda$'' $\,\mid$ \\
  1647 ``$\textit{left}~(N~\_~\_~t~\_) = t$'' \\[2\smallskipamount]
  1648 \textbf{primrec} \textit{right} \textbf{where} \\
  1649 ``$\textit{right}~\Lambda = \Lambda$'' $\,\mid$ \\
  1650 ``$\textit{right}~(N~\_~\_~\_~u) = u$''
  1651 \postw
  1652 
  1653 The wellformedness criterion for AA trees is fairly complex. Wikipedia states it
  1654 as follows \cite{wikipedia-2009-aa-trees}:
  1655 
  1656 \kern.2\parskip %% TYPESETTING
  1657 
  1658 \pre
  1659 Each node has a level field, and the following invariants must remain true for
  1660 the tree to be valid:
  1661 
  1662 \raggedright
  1663 
  1664 \kern-.4\parskip %% TYPESETTING
  1665 
  1666 \begin{enum}
  1667 \item[]
  1668 \begin{enum}
  1669 \item[1.] The level of a leaf node is one.
  1670 \item[2.] The level of a left child is strictly less than that of its parent.
  1671 \item[3.] The level of a right child is less than or equal to that of its parent.
  1672 \item[4.] The level of a right grandchild is strictly less than that of its grandparent.
  1673 \item[5.] Every node of level greater than one must have two children.
  1674 \end{enum}
  1675 \end{enum}
  1676 \post
  1677 
  1678 \kern.4\parskip %% TYPESETTING
  1679 
  1680 The \textit{wf} predicate formalizes this description:
  1681 
  1682 \prew
  1683 \textbf{primrec} \textit{wf} \textbf{where} \\
  1684 ``$\textit{wf}~\Lambda = \textit{True}$'' $\,\mid$ \\
  1685 ``$\textit{wf}~(N~\_~k~t~u) =$ \\
  1686 \phantom{``}$(\textrm{if}~t = \Lambda~\textrm{then}$ \\
  1687 \phantom{``$(\quad$}$k = 1 \mathrel{\land} (u = \Lambda \mathrel{\lor} (\textit{level}~u = 1 \mathrel{\land} \textit{left}~u = \Lambda \mathrel{\land} \textit{right}~u = \Lambda))$ \\
  1688 \phantom{``$($}$\textrm{else}$ \\
  1689 \hbox{}\phantom{``$(\quad$}$\textit{wf}~t \mathrel{\land} \textit{wf}~u
  1690 \mathrel{\land} u \not= \Lambda \mathrel{\land} \textit{level}~t < k
  1691 \mathrel{\land} \textit{level}~u \le k$ \\
  1692 \hbox{}\phantom{``$(\quad$}${\land}\; \textit{level}~(\textit{right}~u) < k)$''
  1693 \postw
  1694 
  1695 Rebalancing the tree upon insertion and removal of elements is performed by two
  1696 auxiliary functions called \textit{skew} and \textit{split}, defined below:
  1697 
  1698 \prew
  1699 \textbf{primrec} \textit{skew} \textbf{where} \\
  1700 ``$\textit{skew}~\Lambda = \Lambda$'' $\,\mid$ \\
  1701 ``$\textit{skew}~(N~x~k~t~u) = {}$ \\
  1702 \phantom{``}$(\textrm{if}~t \not= \Lambda \mathrel{\land} k =
  1703 \textit{level}~t~\textrm{then}$ \\
  1704 \phantom{``(\quad}$N~(\textit{data}~t)~k~(\textit{left}~t)~(N~x~k~
  1705 (\textit{right}~t)~u)$ \\
  1706 \phantom{``(}$\textrm{else}$ \\
  1707 \phantom{``(\quad}$N~x~k~t~u)$''
  1708 \postw
  1709 
  1710 \prew
  1711 \textbf{primrec} \textit{split} \textbf{where} \\
  1712 ``$\textit{split}~\Lambda = \Lambda$'' $\,\mid$ \\
  1713 ``$\textit{split}~(N~x~k~t~u) = {}$ \\
  1714 \phantom{``}$(\textrm{if}~u \not= \Lambda \mathrel{\land} k =
  1715 \textit{level}~(\textit{right}~u)~\textrm{then}$ \\
  1716 \phantom{``(\quad}$N~(\textit{data}~u)~(\textit{Suc}~k)~
  1717 (N~x~k~t~(\textit{left}~u))~(\textit{right}~u)$ \\
  1718 \phantom{``(}$\textrm{else}$ \\
  1719 \phantom{``(\quad}$N~x~k~t~u)$''
  1720 \postw
  1721 
  1722 Performing a \textit{skew} or a \textit{split} should have no impact on the set
  1723 of elements stored in the tree:
  1724 
  1725 \prew
  1726 \textbf{theorem}~\textit{dataset\_skew\_split\/}:\\
  1727 ``$\textit{dataset}~(\textit{skew}~t) = \textit{dataset}~t$'' \\
  1728 ``$\textit{dataset}~(\textit{split}~t) = \textit{dataset}~t$'' \\
  1729 \textbf{nitpick} \\[2\smallskipamount]
  1730 {\slshape Nitpick ran out of time after checking 7 of 8 scopes.}
  1731 \postw
  1732 
  1733 Furthermore, applying \textit{skew} or \textit{split} to a well-formed tree
  1734 should not alter the tree:
  1735 
  1736 \prew
  1737 \textbf{theorem}~\textit{wf\_skew\_split\/}:\\
  1738 ``$\textit{wf}~t\,\Longrightarrow\, \textit{skew}~t = t$'' \\
  1739 ``$\textit{wf}~t\,\Longrightarrow\, \textit{split}~t = t$'' \\
  1740 \textbf{nitpick} \\[2\smallskipamount]
  1741 {\slshape Nitpick found no counterexample.}
  1742 \postw
  1743 
  1744 Insertion is implemented recursively. It preserves the sort order:
  1745 
  1746 \prew
  1747 \textbf{primrec}~\textit{insort} \textbf{where} \\
  1748 ``$\textit{insort}~\Lambda~x = N~x~1~\Lambda~\Lambda$'' $\,\mid$ \\
  1749 ``$\textit{insort}~(N~y~k~t~u)~x =$ \\
  1750 \phantom{``}$({*}~(\textit{split} \circ \textit{skew})~{*})~(N~y~k~(\textrm{if}~x < y~\textrm{then}~\textit{insort}~t~x~\textrm{else}~t)$ \\
  1751 \phantom{``$({*}~(\textit{split} \circ \textit{skew})~{*})~(N~y~k~$}$(\textrm{if}~x > y~\textrm{then}~\textit{insort}~u~x~\textrm{else}~u))$''
  1752 \postw
  1753 
  1754 Notice that we deliberately commented out the application of \textit{skew} and
  1755 \textit{split}. Let's see if this causes any problems:
  1756 
  1757 \prew
  1758 \textbf{theorem}~\textit{wf\_insort\/}:\kern.4em ``$\textit{wf}~t\,\Longrightarrow\, \textit{wf}~(\textit{insort}~t~x)$'' \\
  1759 \textbf{nitpick} \\[2\smallskipamount]
  1760 \slshape Nitpick found a counterexample for \textit{card} $'a$ = 4: \\[2\smallskipamount]
  1761 \hbox{}\qquad Free variables: \nopagebreak \\
  1762 \hbox{}\qquad\qquad $t = N~a_1~1~\Lambda~\Lambda$ \\
  1763 \hbox{}\qquad\qquad $x = a_2$
  1764 \postw
  1765 
  1766 It's hard to see why this is a counterexample. To improve readability, we will
  1767 restrict the theorem to \textit{nat}, so that we don't need to look up the value
  1768 of the $\textit{op}~{<}$ constant to find out which element is smaller than the
  1769 other. In addition, we will tell Nitpick to display the value of
  1770 $\textit{insort}~t~x$ using the \textit{eval} option. This gives
  1771 
  1772 \prew
  1773 \textbf{theorem} \textit{wf\_insort\_nat\/}:\kern.4em ``$\textit{wf}~t\,\Longrightarrow\, \textit{wf}~(\textit{insort}~t~(x\Colon\textit{nat}))$'' \\
  1774 \textbf{nitpick} [\textit{eval} = ``$\textit{insort}~t~x$''] \\[2\smallskipamount]
  1775 \slshape Nitpick found a counterexample: \\[2\smallskipamount]
  1776 \hbox{}\qquad Free variables: \nopagebreak \\
  1777 \hbox{}\qquad\qquad $t = N~1~1~\Lambda~\Lambda$ \\
  1778 \hbox{}\qquad\qquad $x = 0$ \\
  1779 \hbox{}\qquad Evaluated term: \\
  1780 \hbox{}\qquad\qquad $\textit{insort}~t~x = N~1~1~(N~0~1~\Lambda~\Lambda)~\Lambda$
  1781 \postw
  1782 
  1783 Nitpick's output reveals that the element $0$ was added as a left child of $1$,
  1784 where both have a level of 1. This violates the second AA tree invariant, which
  1785 states that a left child's level must be less than its parent's. This shouldn't
  1786 come as a surprise, considering that we commented out the tree rebalancing code.
  1787 Reintroducing the code seems to solve the problem:
  1788 
  1789 \prew
  1790 \textbf{theorem}~\textit{wf\_insort\/}:\kern.4em ``$\textit{wf}~t\,\Longrightarrow\, \textit{wf}~(\textit{insort}~t~x)$'' \\
  1791 \textbf{nitpick} \\[2\smallskipamount]
  1792 {\slshape Nitpick ran out of time after checking 7 of 8 scopes.}
  1793 \postw
  1794 
  1795 Insertion should transform the set of elements represented by the tree in the
  1796 obvious way:
  1797 
  1798 \prew
  1799 \textbf{theorem} \textit{dataset\_insort\/}:\kern.4em
  1800 ``$\textit{dataset}~(\textit{insort}~t~x) = \{x\} \cup \textit{dataset}~t$'' \\
  1801 \textbf{nitpick} \\[2\smallskipamount]
  1802 {\slshape Nitpick ran out of time after checking 6 of 8 scopes.}
  1803 \postw
  1804 
  1805 We could continue like this and sketch a complete theory of AA trees. Once the
  1806 definitions and main theorems are in place and have been thoroughly tested using
  1807 Nitpick, we could start working on the proofs. Developing theories this way
  1808 usually saves time, because faulty theorems and definitions are discovered much
  1809 earlier in the process.
  1810 
  1811 \section{Option Reference}
  1812 \label{option-reference}
  1813 
  1814 \def\flushitem#1{\item[]\noindent\kern-\leftmargin \textbf{#1}}
  1815 \def\qty#1{$\left<\textit{#1}\right>$}
  1816 \def\qtybf#1{$\mathbf{\left<\textbf{\textit{#1}}\right>}$}
  1817 \def\optrue#1#2{\flushitem{\textit{#1} $\bigl[$= \qtybf{bool}$\bigr]$\quad [\textit{true}]\hfill (neg.: \textit{#2})}\nopagebreak\\[\parskip]}
  1818 \def\opfalse#1#2{\flushitem{\textit{#1} $\bigl[$= \qtybf{bool}$\bigr]$\quad [\textit{false}]\hfill (neg.: \textit{#2})}\nopagebreak\\[\parskip]}
  1819 \def\opsmart#1#2{\flushitem{\textit{#1} $\bigl[$= \qtybf{bool\_or\_smart}$\bigr]$\quad [\textit{smart}]\hfill (neg.: \textit{#2})}\nopagebreak\\[\parskip]}
  1820 \def\opnodefault#1#2{\flushitem{\textit{#1} = \qtybf{#2}} \nopagebreak\\[\parskip]}
  1821 \def\opdefault#1#2#3{\flushitem{\textit{#1} = \qtybf{#2}\quad [\textit{#3}]} \nopagebreak\\[\parskip]}
  1822 \def\oparg#1#2#3{\flushitem{\textit{#1} \qtybf{#2} = \qtybf{#3}} \nopagebreak\\[\parskip]}
  1823 \def\opargbool#1#2#3{\flushitem{\textit{#1} \qtybf{#2} $\bigl[$= \qtybf{bool}$\bigr]$\hfill (neg.: \textit{#3})}\nopagebreak\\[\parskip]}
  1824 \def\opargboolorsmart#1#2#3{\flushitem{\textit{#1} \qtybf{#2} $\bigl[$= \qtybf{bool\_or\_smart}$\bigr]$\hfill (neg.: \textit{#3})}\nopagebreak\\[\parskip]}
  1825 
  1826 Nitpick's behavior can be influenced by various options, which can be specified
  1827 in brackets after the \textbf{nitpick} command. Default values can be set
  1828 using \textbf{nitpick\_\allowbreak params}. For example:
  1829 
  1830 \prew
  1831 \textbf{nitpick\_params} [\textit{verbose}, \,\textit{timeout} = 60$\,s$]
  1832 \postw
  1833 
  1834 The options are categorized as follows:\ mode of operation
  1835 (\S\ref{mode-of-operation}), scope of search (\S\ref{scope-of-search}), output
  1836 format (\S\ref{output-format}), automatic counterexample checks
  1837 (\S\ref{authentication}), optimizations
  1838 (\S\ref{optimizations}), and timeouts (\S\ref{timeouts}).
  1839 
  1840 You can instruct Nitpick to run automatically on newly entered theorems by
  1841 enabling the ``Auto Nitpick'' option from the ``Isabelle'' menu in Proof
  1842 General. For automatic runs, \textit{user\_axioms} (\S\ref{mode-of-operation})
  1843 and \textit{assms} (\S\ref{mode-of-operation}) are implicitly enabled,
  1844 \textit{blocking} (\S\ref{mode-of-operation}), \textit{verbose}
  1845 (\S\ref{output-format}), and \textit{debug} (\S\ref{output-format}) are
  1846 disabled, \textit{max\_potential} (\S\ref{output-format}) is taken to be 0, and
  1847 \textit{timeout} (\S\ref{timeouts}) is superseded by the ``Auto Counterexample
  1848 Time Limit'' in Proof General's ``Isabelle'' menu. Nitpick's output is also more
  1849 concise.
  1850 
  1851 The number of options can be overwhelming at first glance. Do not let that worry
  1852 you: Nitpick's defaults have been chosen so that it almost always does the right
  1853 thing, and the most important options have been covered in context in
  1854 \S\ref{overview}.
  1855 
  1856 The descriptions below refer to the following syntactic quantities:
  1857 
  1858 \begin{enum}
  1859 \item[$\bullet$] \qtybf{string}: A string.
  1860 \item[$\bullet$] \qtybf{bool\/}: \textit{true} or \textit{false}.
  1861 \item[$\bullet$] \qtybf{bool\_or\_smart\/}: \textit{true}, \textit{false}, or \textit{smart}.
  1862 \item[$\bullet$] \qtybf{int\/}: An integer. Negative integers are prefixed with a hyphen.
  1863 \item[$\bullet$] \qtybf{int\_or\_smart\/}: An integer or \textit{smart}.
  1864 \item[$\bullet$] \qtybf{int\_range}: An integer (e.g., 3) or a range
  1865 of nonnegative integers (e.g., $1$--$4$). The range symbol `--' can be entered as \texttt{-} (hyphen) or \texttt{\char`\\\char`\<midarrow\char`\>}.
  1866 
  1867 \item[$\bullet$] \qtybf{int\_seq}: A comma-separated sequence of ranges of integers (e.g.,~1{,}3{,}\allowbreak6--8).
  1868 \item[$\bullet$] \qtybf{time}: An integer followed by $\textit{min}$ (minutes), $s$ (seconds), or \textit{ms}
  1869 (milliseconds), or the keyword \textit{none} ($\infty$ years).
  1870 \item[$\bullet$] \qtybf{const\/}: The name of a HOL constant.
  1871 \item[$\bullet$] \qtybf{term}: A HOL term (e.g., ``$f~x$'').
  1872 \item[$\bullet$] \qtybf{term\_list\/}: A space-separated list of HOL terms (e.g.,
  1873 ``$f~x$''~``$g~y$'').
  1874 \item[$\bullet$] \qtybf{type}: A HOL type.
  1875 \end{enum}
  1876 
  1877 Default values are indicated in square brackets. Boolean options have a negated
  1878 counterpart (e.g., \textit{blocking} vs.\ \textit{no\_blocking}). When setting
  1879 Boolean options, ``= \textit{true}'' may be omitted.
  1880 
  1881 \subsection{Mode of Operation}
  1882 \label{mode-of-operation}
  1883 
  1884 \begin{enum}
  1885 \optrue{blocking}{non\_blocking}
  1886 Specifies whether the \textbf{nitpick} command should operate synchronously.
  1887 The asynchronous (non-blocking) mode lets the user start proving the putative
  1888 theorem while Nitpick looks for a counterexample, but it can also be more
  1889 confusing. For technical reasons, automatic runs currently always block.
  1890 
  1891 \optrue{falsify}{satisfy}
  1892 Specifies whether Nitpick should look for falsifying examples (countermodels) or
  1893 satisfying examples (models). This manual assumes throughout that
  1894 \textit{falsify} is enabled.
  1895 
  1896 \opsmart{user\_axioms}{no\_user\_axioms}
  1897 Specifies whether the user-defined axioms (specified using 
  1898 \textbf{axiomatization} and \textbf{axioms}) should be considered. If the option
  1899 is set to \textit{smart}, Nitpick performs an ad hoc axiom selection based on
  1900 the constants that occur in the formula to falsify. The option is implicitly set
  1901 to \textit{true} for automatic runs.
  1902 
  1903 \textbf{Warning:} If the option is set to \textit{true}, Nitpick might
  1904 nonetheless ignore some polymorphic axioms. Counterexamples generated under
  1905 these conditions are tagged as ``quasi genuine.'' The \textit{debug}
  1906 (\S\ref{output-format}) option can be used to find out which axioms were
  1907 considered.
  1908 
  1909 \nopagebreak
  1910 {\small See also \textit{assms} (\S\ref{mode-of-operation}) and \textit{debug}
  1911 (\S\ref{output-format}).}
  1912 
  1913 \optrue{assms}{no\_assms}
  1914 Specifies whether the relevant assumptions in structured proofs should be
  1915 considered. The option is implicitly enabled for automatic runs.
  1916 
  1917 \nopagebreak
  1918 {\small See also \textit{user\_axioms} (\S\ref{mode-of-operation}).}
  1919 
  1920 \opfalse{overlord}{no\_overlord}
  1921 Specifies whether Nitpick should put its temporary files in
  1922 \texttt{\$ISABELLE\_\allowbreak HOME\_\allowbreak USER}, which is useful for
  1923 debugging Nitpick but also unsafe if several instances of the tool are run
  1924 simultaneously. The files are identified by the extensions
  1925 \texttt{.kki}, \texttt{.cnf}, \texttt{.out}, and
  1926 \texttt{.err}; you may safely remove them after Nitpick has run.
  1927 
  1928 \nopagebreak
  1929 {\small See also \textit{debug} (\S\ref{output-format}).}
  1930 \end{enum}
  1931 
  1932 \subsection{Scope of Search}
  1933 \label{scope-of-search}
  1934 
  1935 \begin{enum}
  1936 \oparg{card}{type}{int\_seq}
  1937 Specifies the sequence of cardinalities to use for a given type.
  1938 For free types, and often also for \textbf{typedecl}'d types, it usually makes
  1939 sense to specify cardinalities as a range of the form \textit{$1$--$n$}.
  1940 
  1941 \nopagebreak
  1942 {\small See also \textit{box} (\S\ref{scope-of-search}) and \textit{mono}
  1943 (\S\ref{scope-of-search}).}
  1944 
  1945 \opdefault{card}{int\_seq}{$\mathbf{1}$--$\mathbf{8}$}
  1946 Specifies the default sequence of cardinalities to use. This can be overridden
  1947 on a per-type basis using the \textit{card}~\qty{type} option described above.
  1948 
  1949 \oparg{max}{const}{int\_seq}
  1950 Specifies the sequence of maximum multiplicities to use for a given
  1951 (co)in\-duc\-tive datatype constructor. A constructor's multiplicity is the
  1952 number of distinct values that it can construct. Nonsensical values (e.g.,
  1953 \textit{max}~[]~$=$~2) are silently repaired. This option is only available for
  1954 datatypes equipped with several constructors.
  1955 
  1956 \opnodefault{max}{int\_seq}
  1957 Specifies the default sequence of maximum multiplicities to use for
  1958 (co)in\-duc\-tive datatype constructors. This can be overridden on a per-constructor
  1959 basis using the \textit{max}~\qty{const} option described above.
  1960 
  1961 \opsmart{binary\_ints}{unary\_ints}
  1962 Specifies whether natural numbers and integers should be encoded using a unary
  1963 or binary notation. In unary mode, the cardinality fully specifies the subset
  1964 used to approximate the type. For example:
  1965 %
  1966 $$\hbox{\begin{tabular}{@{}rll@{}}%
  1967 \textit{card nat} = 4 & induces & $\{0,\, 1,\, 2,\, 3\}$ \\
  1968 \textit{card int} = 4 & induces & $\{-1,\, 0,\, +1,\, +2\}$ \\
  1969 \textit{card int} = 5 & induces & $\{-2,\, -1,\, 0,\, +1,\, +2\}.$%
  1970 \end{tabular}}$$
  1971 %
  1972 In general:
  1973 %
  1974 $$\hbox{\begin{tabular}{@{}rll@{}}%
  1975 \textit{card nat} = $K$ & induces & $\{0,\, \ldots,\, K - 1\}$ \\
  1976 \textit{card int} = $K$ & induces & $\{-\lceil K/2 \rceil + 1,\, \ldots,\, +\lfloor K/2 \rfloor\}.$%
  1977 \end{tabular}}$$
  1978 %
  1979 In binary mode, the cardinality specifies the number of distinct values that can
  1980 be constructed. Each of these value is represented by a bit pattern whose length
  1981 is specified by the \textit{bits} (\S\ref{scope-of-search}) option. By default,
  1982 Nitpick attempts to choose the more appropriate encoding by inspecting the
  1983 formula at hand, preferring the binary notation for problems involving
  1984 multiplicative operators or large constants.
  1985 
  1986 \textbf{Warning:} For technical reasons, Nitpick always reverts to unary for
  1987 problems that refer to the types \textit{rat} or \textit{real} or the constants
  1988 \textit{Suc}, \textit{gcd}, or \textit{lcm}.
  1989 
  1990 {\small See also \textit{bits} (\S\ref{scope-of-search}) and
  1991 \textit{show\_datatypes} (\S\ref{output-format}).}
  1992 
  1993 \opdefault{bits}{int\_seq}{$\mathbf{1},\mathbf{2},\mathbf{3},\mathbf{4},\mathbf{6},\mathbf{8},\mathbf{10},\mathbf{12}$}
  1994 Specifies the number of bits to use to represent natural numbers and integers in
  1995 binary, excluding the sign bit. The minimum is 1 and the maximum is 31.
  1996 
  1997 {\small See also \textit{binary\_ints} (\S\ref{scope-of-search}).}
  1998 
  1999 \opargboolorsmart{wf}{const}{non\_wf}
  2000 Specifies whether the specified (co)in\-duc\-tively defined predicate is
  2001 well-founded. The option can take the following values:
  2002 
  2003 \begin{enum}
  2004 \item[$\bullet$] \textbf{\textit{true}}: Tentatively treat the (co)in\-duc\-tive
  2005 predicate as if it were well-founded. Since this is generally not sound when the
  2006 predicate is not well-founded, the counterexamples are tagged as ``quasi
  2007 genuine.''
  2008 
  2009 \item[$\bullet$] \textbf{\textit{false}}: Treat the (co)in\-duc\-tive predicate
  2010 as if it were not well-founded. The predicate is then unrolled as prescribed by
  2011 the \textit{star\_linear\_preds}, \textit{iter}~\qty{const}, and \textit{iter}
  2012 options.
  2013 
  2014 \item[$\bullet$] \textbf{\textit{smart}}: Try to prove that the inductive
  2015 predicate is well-founded using Isabelle's \textit{lexicographic\_order} and
  2016 \textit{size\_change} tactics. If this succeeds (or the predicate occurs with an
  2017 appropriate polarity in the formula to falsify), use an efficient fixed point
  2018 equation as specification of the predicate; otherwise, unroll the predicates
  2019 according to the \textit{iter}~\qty{const} and \textit{iter} options.
  2020 \end{enum}
  2021 
  2022 \nopagebreak
  2023 {\small See also \textit{iter} (\S\ref{scope-of-search}),
  2024 \textit{star\_linear\_preds} (\S\ref{optimizations}), and \textit{tac\_timeout}
  2025 (\S\ref{timeouts}).}
  2026 
  2027 \opsmart{wf}{non\_wf}
  2028 Specifies the default wellfoundedness setting to use. This can be overridden on
  2029 a per-predicate basis using the \textit{wf}~\qty{const} option above.
  2030 
  2031 \oparg{iter}{const}{int\_seq}
  2032 Specifies the sequence of iteration counts to use when unrolling a given
  2033 (co)in\-duc\-tive predicate. By default, unrolling is applied for inductive
  2034 predicates that occur negatively and coinductive predicates that occur
  2035 positively in the formula to falsify and that cannot be proved to be
  2036 well-founded, but this behavior is influenced by the \textit{wf} option. The
  2037 iteration counts are automatically bounded by the cardinality of the predicate's
  2038 domain.
  2039 
  2040 {\small See also \textit{wf} (\S\ref{scope-of-search}) and
  2041 \textit{star\_linear\_preds} (\S\ref{optimizations}).}
  2042 
  2043 \opdefault{iter}{int\_seq}{$\mathbf{1{,}2{,}4{,}8{,}12{,}16{,}24{,}32}$}
  2044 Specifies the sequence of iteration counts to use when unrolling (co)in\-duc\-tive
  2045 predicates. This can be overridden on a per-predicate basis using the
  2046 \textit{iter} \qty{const} option above.
  2047 
  2048 \opdefault{bisim\_depth}{int\_seq}{$\mathbf{7}$}
  2049 Specifies the sequence of iteration counts to use when unrolling the
  2050 bisimilarity predicate generated by Nitpick for coinductive datatypes. A value
  2051 of $-1$ means that no predicate is generated, in which case Nitpick performs an
  2052 after-the-fact check to see if the known coinductive datatype values are
  2053 bidissimilar. If two values are found to be bisimilar, the counterexample is
  2054 tagged as ``quasi genuine.'' The iteration counts are automatically bounded by
  2055 the sum of the cardinalities of the coinductive datatypes occurring in the
  2056 formula to falsify.
  2057 
  2058 \opargboolorsmart{box}{type}{dont\_box}
  2059 Specifies whether Nitpick should attempt to wrap (``box'') a given function or
  2060 product type in an isomorphic datatype internally. Boxing is an effective mean
  2061 to reduce the search space and speed up Nitpick, because the isomorphic datatype
  2062 is approximated by a subset of the possible function or pair values.
  2063 Like other drastic optimizations, it can also prevent the discovery of
  2064 counterexamples. The option can take the following values:
  2065 
  2066 \begin{enum}
  2067 \item[$\bullet$] \textbf{\textit{true}}: Box the specified type whenever
  2068 practicable.
  2069 \item[$\bullet$] \textbf{\textit{false}}: Never box the type.
  2070 \item[$\bullet$] \textbf{\textit{smart}}: Box the type only in contexts where it
  2071 is likely to help. For example, $n$-tuples where $n > 2$ and arguments to
  2072 higher-order functions are good candidates for boxing.
  2073 \end{enum}
  2074 
  2075 \nopagebreak
  2076 {\small See also \textit{finitize} (\S\ref{scope-of-search}), \textit{verbose}
  2077 (\S\ref{output-format}), and \textit{debug} (\S\ref{output-format}).}
  2078 
  2079 \opsmart{box}{dont\_box}
  2080 Specifies the default boxing setting to use. This can be overridden on a
  2081 per-type basis using the \textit{box}~\qty{type} option described above.
  2082 
  2083 \opargboolorsmart{finitize}{type}{dont\_finitize}
  2084 Specifies whether Nitpick should attempt to finitize a given type, which can be
  2085 a function type or an infinite ``shallow datatype'' (an infinite datatype whose
  2086 constructors don't appear in the problem).
  2087 
  2088 For function types, Nitpick performs a monotonicity analysis to detect functions
  2089 that are constant at all but finitely many points (e.g., finite sets) and treats
  2090 such occurrences specially, thereby increasing the precision. The option can
  2091 then take the following values:
  2092 
  2093 \begin{enum}
  2094 \item[$\bullet$] \textbf{\textit{false}}: Don't attempt to finitize the type.
  2095 \item[$\bullet$] \textbf{\textit{true}} or \textbf{\textit{smart}}: Finitize the
  2096 type wherever possible.
  2097 \end{enum}
  2098 
  2099 The semantics of the option is somewhat different for infinite shallow
  2100 datatypes:
  2101 
  2102 \begin{enum}
  2103 \item[$\bullet$] \textbf{\textit{true}}: Finitize the datatype. Since this is
  2104 unsound, counterexamples generated under these conditions are tagged as ``quasi
  2105 genuine.''
  2106 \item[$\bullet$] \textbf{\textit{false}}: Don't attempt to finitize the datatype.
  2107 \item[$\bullet$] \textbf{\textit{smart}}: Perform a monotonicity analysis to
  2108 detect whether the datatype can be safely finitized before finitizing it.
  2109 \end{enum}
  2110 
  2111 Like other drastic optimizations, finitization can sometimes prevent the
  2112 discovery of counterexamples.
  2113 
  2114 \nopagebreak
  2115 {\small See also \textit{box} (\S\ref{scope-of-search}), \textit{mono}
  2116 (\S\ref{scope-of-search}), \textit{verbose} (\S\ref{output-format}), and
  2117 \textit{debug} (\S\ref{output-format}).}
  2118 
  2119 \opsmart{finitize}{dont\_finitize}
  2120 Specifies the default finitization setting to use. This can be overridden on a
  2121 per-type basis using the \textit{finitize}~\qty{type} option described above.
  2122 
  2123 \opargboolorsmart{mono}{type}{non\_mono}
  2124 Specifies whether the given type should be considered monotonic when enumerating
  2125 scopes and finitizing types. If the option is set to \textit{smart}, Nitpick
  2126 performs a monotonicity check on the type. Setting this option to \textit{true}
  2127 can reduce the number of scopes tried, but it can also diminish the chance of
  2128 finding a counterexample, as demonstrated in \S\ref{scope-monotonicity}.
  2129 
  2130 \nopagebreak
  2131 {\small See also \textit{card} (\S\ref{scope-of-search}),
  2132 \textit{finitize} (\S\ref{scope-of-search}),
  2133 \textit{merge\_type\_vars} (\S\ref{scope-of-search}), and \textit{verbose}
  2134 (\S\ref{output-format}).}
  2135 
  2136 \opsmart{mono}{non\_mono}
  2137 Specifies the default monotonicity setting to use. This can be overridden on a
  2138 per-type basis using the \textit{mono}~\qty{type} option described above.
  2139 
  2140 \opfalse{merge\_type\_vars}{dont\_merge\_type\_vars}
  2141 Specifies whether type variables with the same sort constraints should be
  2142 merged. Setting this option to \textit{true} can reduce the number of scopes
  2143 tried and the size of the generated Kodkod formulas, but it also diminishes the
  2144 theoretical chance of finding a counterexample.
  2145 
  2146 {\small See also \textit{mono} (\S\ref{scope-of-search}).}
  2147 
  2148 \opargbool{std}{type}{non\_std}
  2149 Specifies whether the given (recursive) datatype should be given standard
  2150 models. Nonstandard models are unsound but can help debug structural induction
  2151 proofs, as explained in \S\ref{inductive-properties}.
  2152 
  2153 \optrue{std}{non\_std}
  2154 Specifies the default standardness to use. This can be overridden on a per-type
  2155 basis using the \textit{std}~\qty{type} option described above.
  2156 \end{enum}
  2157 
  2158 \subsection{Output Format}
  2159 \label{output-format}
  2160 
  2161 \begin{enum}
  2162 \opfalse{verbose}{quiet}
  2163 Specifies whether the \textbf{nitpick} command should explain what it does. This
  2164 option is useful to determine which scopes are tried or which SAT solver is
  2165 used. This option is implicitly disabled for automatic runs.
  2166 
  2167 \opfalse{debug}{no\_debug}
  2168 Specifies whether Nitpick should display additional debugging information beyond
  2169 what \textit{verbose} already displays. Enabling \textit{debug} also enables
  2170 \textit{verbose} and \textit{show\_all} behind the scenes. The \textit{debug}
  2171 option is implicitly disabled for automatic runs.
  2172 
  2173 \nopagebreak
  2174 {\small See also \textit{overlord} (\S\ref{mode-of-operation}) and
  2175 \textit{batch\_size} (\S\ref{optimizations}).}
  2176 
  2177 \optrue{show\_skolems}{hide\_skolem}
  2178 Specifies whether the values of Skolem constants should be displayed as part of
  2179 counterexamples. Skolem constants correspond to bound variables in the original
  2180 formula and usually help us to understand why the counterexample falsifies the
  2181 formula.
  2182 
  2183 \nopagebreak
  2184 {\small See also \textit{skolemize} (\S\ref{optimizations}).}
  2185 
  2186 \opfalse{show\_datatypes}{hide\_datatypes}
  2187 Specifies whether the subsets used to approximate (co)in\-duc\-tive datatypes should
  2188 be displayed as part of counterexamples. Such subsets are sometimes helpful when
  2189 investigating whether a potential counterexample is genuine or spurious, but
  2190 their potential for clutter is real.
  2191 
  2192 \opfalse{show\_consts}{hide\_consts}
  2193 Specifies whether the values of constants occurring in the formula (including
  2194 its axioms) should be displayed along with any counterexample. These values are
  2195 sometimes helpful when investigating why a counterexample is
  2196 genuine, but they can clutter the output.
  2197 
  2198 \opfalse{show\_all}{dont\_show\_all}
  2199 Enabling this option effectively enables \textit{show\_skolems},
  2200 \textit{show\_datatypes}, and \textit{show\_consts}.
  2201 
  2202 \opdefault{max\_potential}{int}{$\mathbf{1}$}
  2203 Specifies the maximum number of potential counterexamples to display. Setting
  2204 this option to 0 speeds up the search for a genuine counterexample. This option
  2205 is implicitly set to 0 for automatic runs. If you set this option to a value
  2206 greater than 1, you will need an incremental SAT solver: For efficiency, it is
  2207 recommended to install the JNI version of MiniSat and set \textit{sat\_solver} =
  2208 \textit{MiniSat\_JNI}. Also be aware that many of the counterexamples may look
  2209 identical, unless the \textit{show\_all} (\S\ref{output-format}) option is
  2210 enabled.
  2211 
  2212 \nopagebreak
  2213 {\small See also \textit{check\_potential} (\S\ref{authentication}) and
  2214 \textit{sat\_solver} (\S\ref{optimizations}).}
  2215 
  2216 \opdefault{max\_genuine}{int}{$\mathbf{1}$}
  2217 Specifies the maximum number of genuine counterexamples to display. If you set
  2218 this option to a value greater than 1, you will need an incremental SAT solver:
  2219 For efficiency, it is recommended to install the JNI version of MiniSat and set
  2220 \textit{sat\_solver} = \textit{MiniSat\_JNI}. Also be aware that many of the
  2221 counterexamples may look identical, unless the \textit{show\_all}
  2222 (\S\ref{output-format}) option is enabled.
  2223 
  2224 \nopagebreak
  2225 {\small See also \textit{check\_genuine} (\S\ref{authentication}) and
  2226 \textit{sat\_solver} (\S\ref{optimizations}).}
  2227 
  2228 \opnodefault{eval}{term\_list}
  2229 Specifies the list of terms whose values should be displayed along with
  2230 counterexamples. This option suffers from an ``observer effect'': Nitpick might
  2231 find different counterexamples for different values of this option.
  2232 
  2233 \oparg{format}{term}{int\_seq}
  2234 Specifies how to uncurry the value displayed for a variable or constant.
  2235 Uncurrying sometimes increases the readability of the output for high-arity
  2236 functions. For example, given the variable $y \mathbin{\Colon} {'a}\Rightarrow
  2237 {'b}\Rightarrow {'c}\Rightarrow {'d}\Rightarrow {'e}\Rightarrow {'f}\Rightarrow
  2238 {'g}$, setting \textit{format}~$y$ = 3 tells Nitpick to group the last three
  2239 arguments, as if the type had been ${'a}\Rightarrow {'b}\Rightarrow
  2240 {'c}\Rightarrow {'d}\times {'e}\times {'f}\Rightarrow {'g}$. In general, a list
  2241 of values $n_1,\ldots,n_k$ tells Nitpick to show the last $n_k$ arguments as an
  2242 $n_k$-tuple, the previous $n_{k-1}$ arguments as an $n_{k-1}$-tuple, and so on;
  2243 arguments that are not accounted for are left alone, as if the specification had
  2244 been $1,\ldots,1,n_1,\ldots,n_k$.
  2245 
  2246 \nopagebreak
  2247 {\small See also \textit{uncurry} (\S\ref{optimizations}).}
  2248 
  2249 \opdefault{format}{int\_seq}{$\mathbf{1}$}
  2250 Specifies the default format to use. Irrespective of the default format, the
  2251 extra arguments to a Skolem constant corresponding to the outer bound variables
  2252 are kept separated from the remaining arguments, the \textbf{for} arguments of
  2253 an inductive definitions are kept separated from the remaining arguments, and
  2254 the iteration counter of an unrolled inductive definition is shown alone. The
  2255 default format can be overridden on a per-variable or per-constant basis using
  2256 the \textit{format}~\qty{term} option described above.
  2257 \end{enum}
  2258 
  2259 \subsection{Authentication}
  2260 \label{authentication}
  2261 
  2262 \begin{enum}
  2263 \opfalse{check\_potential}{trust\_potential}
  2264 Specifies whether potential counterexamples should be given to Isabelle's
  2265 \textit{auto} tactic to assess their validity. If a potential counterexample is
  2266 shown to be genuine, Nitpick displays a message to this effect and terminates.
  2267 
  2268 \nopagebreak
  2269 {\small See also \textit{max\_potential} (\S\ref{output-format}).}
  2270 
  2271 \opfalse{check\_genuine}{trust\_genuine}
  2272 Specifies whether genuine and quasi genuine counterexamples should be given to
  2273 Isabelle's \textit{auto} tactic to assess their validity. If a ``genuine''
  2274 counterexample is shown to be spurious, the user is kindly asked to send a bug
  2275 report to the author at
  2276 \texttt{blan{\color{white}nospam}\kern-\wd\boxA{}chette@in.tum.de}.
  2277 
  2278 \nopagebreak
  2279 {\small See also \textit{max\_genuine} (\S\ref{output-format}).}
  2280 
  2281 \opnodefault{expect}{string}
  2282 Specifies the expected outcome, which must be one of the following:
  2283 
  2284 \begin{enum}
  2285 \item[$\bullet$] \textbf{\textit{genuine}}: Nitpick found a genuine counterexample.
  2286 \item[$\bullet$] \textbf{\textit{quasi\_genuine}}: Nitpick found a ``quasi
  2287 genuine'' counterexample (i.e., a counterexample that is genuine unless
  2288 it contradicts a missing axiom or a dangerous option was used inappropriately).
  2289 \item[$\bullet$] \textbf{\textit{potential}}: Nitpick found a potential counterexample.
  2290 \item[$\bullet$] \textbf{\textit{none}}: Nitpick found no counterexample.
  2291 \item[$\bullet$] \textbf{\textit{unknown}}: Nitpick encountered some problem (e.g.,
  2292 Kodkod ran out of memory).
  2293 \end{enum}
  2294 
  2295 Nitpick emits an error if the actual outcome differs from the expected outcome.
  2296 This option is useful for regression testing.
  2297 \end{enum}
  2298 
  2299 \subsection{Optimizations}
  2300 \label{optimizations}
  2301 
  2302 \def\cpp{C\nobreak\raisebox{.1ex}{+}\nobreak\raisebox{.1ex}{+}}
  2303 
  2304 \sloppy
  2305 
  2306 \begin{enum}
  2307 \opdefault{sat\_solver}{string}{smart}
  2308 Specifies which SAT solver to use. SAT solvers implemented in C or \cpp{} tend
  2309 to be faster than their Java counterparts, but they can be more difficult to
  2310 install. Also, if you set the \textit{max\_potential} (\S\ref{output-format}) or
  2311 \textit{max\_genuine} (\S\ref{output-format}) option to a value greater than 1,
  2312 you will need an incremental SAT solver, such as \textit{MiniSat\_JNI}
  2313 (recommended) or \textit{SAT4J}.
  2314 
  2315 The supported solvers are listed below:
  2316 
  2317 \let\foo
  2318 
  2319 \begin{enum}
  2320 
  2321 \item[$\bullet$] \textbf{\textit{MiniSat}}: MiniSat is an efficient solver
  2322 written in \cpp{}. To use MiniSat, set the environment variable
  2323 \texttt{MINISAT\_HOME} to the directory that contains the \texttt{minisat}
  2324 executable.%
  2325 \footnote{Important note for Cygwin users: The path must be specified using
  2326 native Windows syntax. Make sure to escape backslashes properly.%
  2327 \label{cygwin-paths}}
  2328 The \cpp{} sources and executables for MiniSat are available at
  2329 \url{http://minisat.se/MiniSat.html}. Nitpick has been tested with versions 1.14
  2330 and 2.0 beta (2007-07-21).
  2331 
  2332 \item[$\bullet$] \textbf{\textit{MiniSat\_JNI}}: The JNI (Java Native Interface)
  2333 version of MiniSat is bundled in \texttt{nativesolver.\allowbreak tgz}, which
  2334 you will find on Kodkod's web site \cite{kodkod-2009}. Unlike the standard
  2335 version of MiniSat, the JNI version can be used incrementally.
  2336 
  2337 %%% No longer true:
  2338 %%% "It is bundled with Kodkodi and requires no further installation or
  2339 %%% configuration steps. Alternatively,"
  2340 \item[$\bullet$] \textbf{\textit{PicoSAT}}: PicoSAT is an efficient solver
  2341 written in C. You can install a standard version of
  2342 PicoSAT and set the environment variable \texttt{PICOSAT\_HOME} to the directory
  2343 that contains the \texttt{picosat} executable.%
  2344 \footref{cygwin-paths}
  2345 The C sources for PicoSAT are
  2346 available at \url{http://fmv.jku.at/picosat/} and are also bundled with Kodkodi.
  2347 Nitpick has been tested with version 913.
  2348 
  2349 \item[$\bullet$] \textbf{\textit{zChaff}}: zChaff is an efficient solver written
  2350 in \cpp{}. To use zChaff, set the environment variable \texttt{ZCHAFF\_HOME} to
  2351 the directory that contains the \texttt{zchaff} executable.%
  2352 \footref{cygwin-paths}
  2353 The \cpp{} sources and executables for zChaff are available at
  2354 \url{http://www.princeton.edu/~chaff/zchaff.html}. Nitpick has been tested with
  2355 versions 2004-05-13, 2004-11-15, and 2007-03-12.
  2356 
  2357 \item[$\bullet$] \textbf{\textit{zChaff\_JNI}}: The JNI version of zChaff is
  2358 bundled in \texttt{native\-solver.\allowbreak tgz}, which you will find on
  2359 Kodkod's web site \cite{kodkod-2009}.
  2360 
  2361 \item[$\bullet$] \textbf{\textit{RSat}}: RSat is an efficient solver written in
  2362 \cpp{}. To use RSat, set the environment variable \texttt{RSAT\_HOME} to the
  2363 directory that contains the \texttt{rsat} executable.%
  2364 \footref{cygwin-paths}
  2365 The \cpp{} sources for RSat are available at
  2366 \url{http://reasoning.cs.ucla.edu/rsat/}. Nitpick has been tested with version
  2367 2.01.
  2368 
  2369 \item[$\bullet$] \textbf{\textit{BerkMin}}: BerkMin561 is an efficient solver
  2370 written in C. To use BerkMin, set the environment variable
  2371 \texttt{BERKMIN\_HOME} to the directory that contains the \texttt{BerkMin561}
  2372 executable.\footref{cygwin-paths}
  2373 The BerkMin executables are available at
  2374 \url{http://eigold.tripod.com/BerkMin.html}.
  2375 
  2376 \item[$\bullet$] \textbf{\textit{BerkMin\_Alloy}}: Variant of BerkMin that is
  2377 included with Alloy 4 and calls itself ``sat56'' in its banner text. To use this
  2378 version of BerkMin, set the environment variable
  2379 \texttt{BERKMINALLOY\_HOME} to the directory that contains the \texttt{berkmin}
  2380 executable.%
  2381 \footref{cygwin-paths}
  2382 
  2383 \item[$\bullet$] \textbf{\textit{Jerusat}}: Jerusat 1.3 is an efficient solver
  2384 written in C. To use Jerusat, set the environment variable
  2385 \texttt{JERUSAT\_HOME} to the directory that contains the \texttt{Jerusat1.3}
  2386 executable.%
  2387 \footref{cygwin-paths}
  2388 The C sources for Jerusat are available at
  2389 \url{http://www.cs.tau.ac.il/~ale1/Jerusat1.3.tgz}.
  2390 
  2391 \item[$\bullet$] \textbf{\textit{SAT4J}}: SAT4J is a reasonably efficient solver
  2392 written in Java that can be used incrementally. It is bundled with Kodkodi and
  2393 requires no further installation or configuration steps. Do not attempt to
  2394 install the official SAT4J packages, because their API is incompatible with
  2395 Kodkod.
  2396 
  2397 \item[$\bullet$] \textbf{\textit{SAT4J\_Light}}: Variant of SAT4J that is
  2398 optimized for small problems. It can also be used incrementally.
  2399 
  2400 \item[$\bullet$] \textbf{\textit{HaifaSat}}: HaifaSat 1.0 beta is an
  2401 experimental solver written in \cpp. To use HaifaSat, set the environment
  2402 variable \texttt{HAIFASAT\_\allowbreak HOME} to the directory that contains the
  2403 \texttt{HaifaSat} executable.%
  2404 \footref{cygwin-paths}
  2405 The \cpp{} sources for HaifaSat are available at
  2406 \url{http://cs.technion.ac.il/~gershman/HaifaSat.htm}.
  2407 
  2408 \item[$\bullet$] \textbf{\textit{smart}}: If \textit{sat\_solver} is set to
  2409 \textit{smart}, Nitpick selects the first solver among MiniSat,
  2410 PicoSAT, zChaff, RSat, BerkMin, BerkMin\_Alloy, Jerusat, MiniSat\_JNI, and zChaff\_JNI
  2411 that is recognized by Isabelle. If none is found, it falls back on SAT4J, which
  2412 should always be available. If \textit{verbose} (\S\ref{output-format}) is
  2413 enabled, Nitpick displays which SAT solver was chosen.
  2414 \end{enum}
  2415 \fussy
  2416 
  2417 \opdefault{batch\_size}{int\_or\_smart}{smart}
  2418 Specifies the maximum number of Kodkod problems that should be lumped together
  2419 when invoking Kodkodi. Each problem corresponds to one scope. Lumping problems
  2420 together ensures that Kodkodi is launched less often, but it makes the verbose
  2421 output less readable and is sometimes detrimental to performance. If
  2422 \textit{batch\_size} is set to \textit{smart}, the actual value used is 1 if
  2423 \textit{debug} (\S\ref{output-format}) is set and 64 otherwise.
  2424 
  2425 \optrue{destroy\_constrs}{dont\_destroy\_constrs}
  2426 Specifies whether formulas involving (co)in\-duc\-tive datatype constructors should
  2427 be rewritten to use (automatically generated) discriminators and destructors.
  2428 This optimization can drastically reduce the size of the Boolean formulas given
  2429 to the SAT solver.
  2430 
  2431 \nopagebreak
  2432 {\small See also \textit{debug} (\S\ref{output-format}).}
  2433 
  2434 \optrue{specialize}{dont\_specialize}
  2435 Specifies whether functions invoked with static arguments should be specialized.
  2436 This optimization can drastically reduce the search space, especially for
  2437 higher-order functions.
  2438 
  2439 \nopagebreak
  2440 {\small See also \textit{debug} (\S\ref{output-format}) and
  2441 \textit{show\_consts} (\S\ref{output-format}).}
  2442 
  2443 \optrue{skolemize}{dont\_skolemize}
  2444 Specifies whether the formula should be skolemized. For performance reasons,
  2445 (positive) $\forall$-quanti\-fiers that occur in the scope of a higher-order
  2446 (positive) $\exists$-quanti\-fier are left unchanged.
  2447 
  2448 \nopagebreak
  2449 {\small See also \textit{debug} (\S\ref{output-format}) and
  2450 \textit{show\_skolems} (\S\ref{output-format}).}
  2451 
  2452 \optrue{star\_linear\_preds}{dont\_star\_linear\_preds}
  2453 Specifies whether Nitpick should use Kodkod's transitive closure operator to
  2454 encode non-well-founded ``linear inductive predicates,'' i.e., inductive
  2455 predicates for which each the predicate occurs in at most one assumption of each
  2456 introduction rule. Using the reflexive transitive closure is in principle
  2457 equivalent to setting \textit{iter} to the cardinality of the predicate's
  2458 domain, but it is usually more efficient.
  2459 
  2460 {\small See also \textit{wf} (\S\ref{scope-of-search}), \textit{debug}
  2461 (\S\ref{output-format}), and \textit{iter} (\S\ref{scope-of-search}).}
  2462 
  2463 \optrue{uncurry}{dont\_uncurry}
  2464 Specifies whether Nitpick should uncurry functions. Uncurrying has on its own no
  2465 tangible effect on efficiency, but it creates opportunities for the boxing 
  2466 optimization.
  2467 
  2468 \nopagebreak
  2469 {\small See also \textit{box} (\S\ref{scope-of-search}), \textit{debug}
  2470 (\S\ref{output-format}), and \textit{format} (\S\ref{output-format}).}
  2471 
  2472 \optrue{fast\_descrs}{full\_descrs}
  2473 Specifies whether Nitpick should optimize the definite and indefinite
  2474 description operators (THE and SOME). The optimized versions usually help
  2475 Nitpick generate more counterexamples or at least find them faster, but only the
  2476 unoptimized versions are complete when all types occurring in the formula are
  2477 finite.
  2478 
  2479 {\small See also \textit{debug} (\S\ref{output-format}).}
  2480 
  2481 \optrue{peephole\_optim}{no\_peephole\_optim}
  2482 Specifies whether Nitpick should simplify the generated Kodkod formulas using a
  2483 peephole optimizer. These optimizations can make a significant difference.
  2484 Unless you are tracking down a bug in Nitpick or distrust the peephole
  2485 optimizer, you should leave this option enabled.
  2486 
  2487 \opdefault{sym\_break}{int}{20}
  2488 Specifies an upper bound on the number of relations for which Kodkod generates
  2489 symmetry breaking predicates. According to the Kodkod documentation
  2490 \cite{kodkod-2009-options}, ``in general, the higher this value, the more
  2491 symmetries will be broken, and the faster the formula will be solved. But,
  2492 setting the value too high may have the opposite effect and slow down the
  2493 solving.''
  2494 
  2495 \opdefault{sharing\_depth}{int}{3}
  2496 Specifies the depth to which Kodkod should check circuits for equivalence during
  2497 the translation to SAT. The default of 3 is the same as in Alloy. The minimum
  2498 allowed depth is 1. Increasing the sharing may result in a smaller SAT problem,
  2499 but can also slow down Kodkod.
  2500 
  2501 \opfalse{flatten\_props}{dont\_flatten\_props}
  2502 Specifies whether Kodkod should try to eliminate intermediate Boolean variables.
  2503 Although this might sound like a good idea, in practice it can drastically slow
  2504 down Kodkod.
  2505 
  2506 \opdefault{max\_threads}{int}{0}
  2507 Specifies the maximum number of threads to use in Kodkod. If this option is set
  2508 to 0, Kodkod will compute an appropriate value based on the number of processor
  2509 cores available.
  2510 
  2511 \nopagebreak
  2512 {\small See also \textit{batch\_size} (\S\ref{optimizations}) and
  2513 \textit{timeout} (\S\ref{timeouts}).}
  2514 \end{enum}
  2515 
  2516 \subsection{Timeouts}
  2517 \label{timeouts}
  2518 
  2519 \begin{enum}
  2520 \opdefault{timeout}{time}{$\mathbf{30}$ s}
  2521 Specifies the maximum amount of time that the \textbf{nitpick} command should
  2522 spend looking for a counterexample. Nitpick tries to honor this constraint as
  2523 well as it can but offers no guarantees. For automatic runs,
  2524 \textit{timeout} is ignored; instead, Auto Quickcheck and Auto Nitpick share
  2525 a time slot whose length is specified by the ``Auto Counterexample Time
  2526 Limit'' option in Proof General.
  2527 
  2528 \nopagebreak
  2529 {\small See also \textit{max\_threads} (\S\ref{optimizations}).}
  2530 
  2531 \opdefault{tac\_timeout}{time}{$\mathbf{500}$\,ms}
  2532 Specifies the maximum amount of time that the \textit{auto} tactic should use
  2533 when checking a counterexample, and similarly that \textit{lexicographic\_order}
  2534 and \textit{size\_change} should use when checking whether a (co)in\-duc\-tive
  2535 predicate is well-founded. Nitpick tries to honor this constraint as well as it
  2536 can but offers no guarantees.
  2537 
  2538 \nopagebreak
  2539 {\small See also \textit{wf} (\S\ref{scope-of-search}),
  2540 \textit{check\_potential} (\S\ref{authentication}),
  2541 and \textit{check\_genuine} (\S\ref{authentication}).}
  2542 \end{enum}
  2543 
  2544 \section{Attribute Reference}
  2545 \label{attribute-reference}
  2546 
  2547 Nitpick needs to consider the definitions of all constants occurring in a
  2548 formula in order to falsify it. For constants introduced using the
  2549 \textbf{definition} command, the definition is simply the associated
  2550 \textit{\_def} axiom. In contrast, instead of using the internal representation
  2551 of functions synthesized by Isabelle's \textbf{primrec}, \textbf{function}, and
  2552 \textbf{nominal\_primrec} packages, Nitpick relies on the more natural
  2553 equational specification entered by the user.
  2554 
  2555 Behind the scenes, Isabelle's built-in packages and theories rely on the
  2556 following attributes to affect Nitpick's behavior:
  2557 
  2558 \begin{itemize}
  2559 \flushitem{\textit{nitpick\_def}}
  2560 
  2561 \nopagebreak
  2562 This attribute specifies an alternative definition of a constant. The
  2563 alternative definition should be logically equivalent to the constant's actual
  2564 axiomatic definition and should be of the form
  2565 
  2566 \qquad $c~{?}x_1~\ldots~{?}x_n \,\equiv\, t$,
  2567 
  2568 where ${?}x_1, \ldots, {?}x_n$ are distinct variables and $c$ does not occur in
  2569 $t$.
  2570 
  2571 \flushitem{\textit{nitpick\_simp}}
  2572 
  2573 \nopagebreak
  2574 This attribute specifies the equations that constitute the specification of a
  2575 constant. For functions defined using the \textbf{primrec}, \textbf{function},
  2576 and \textbf{nominal\_\allowbreak primrec} packages, this corresponds to the
  2577 \textit{simps} rules. The equations must be of the form
  2578 
  2579 \qquad $c~t_1~\ldots\ t_n \,=\, u.$
  2580 
  2581 \flushitem{\textit{nitpick\_psimp}}
  2582 
  2583 \nopagebreak
  2584 This attribute specifies the equations that constitute the partial specification
  2585 of a constant. For functions defined using the \textbf{function} package, this
  2586 corresponds to the \textit{psimps} rules. The conditional equations must be of
  2587 the form
  2588 
  2589 \qquad $\lbrakk P_1;\> \ldots;\> P_m\rbrakk \,\Longrightarrow\, c\ t_1\ \ldots\ t_n \,=\, u$.
  2590 
  2591 \flushitem{\textit{nitpick\_intro}}
  2592 
  2593 \nopagebreak
  2594 This attribute specifies the introduction rules of a (co)in\-duc\-tive predicate.
  2595 For predicates defined using the \textbf{inductive} or \textbf{coinductive}
  2596 command, this corresponds to the \textit{intros} rules. The introduction rules
  2597 must be of the form
  2598 
  2599 \qquad $\lbrakk P_1;\> \ldots;\> P_m;\> M~(c\ t_{11}\ \ldots\ t_{1n});\>
  2600 \ldots;\> M~(c\ t_{k1}\ \ldots\ t_{kn})\rbrakk \,\Longrightarrow\, c\ u_1\
  2601 \ldots\ u_n$,
  2602 
  2603 where the $P_i$'s are side conditions that do not involve $c$ and $M$ is an
  2604 optional monotonic operator. The order of the assumptions is irrelevant.
  2605 
  2606 \end{itemize}
  2607 
  2608 When faced with a constant, Nitpick proceeds as follows:
  2609 
  2610 \begin{enum}
  2611 \item[1.] If the \textit{nitpick\_simp} set associated with the constant
  2612 is not empty, Nitpick uses these rules as the specification of the constant.
  2613 
  2614 \item[2.] Otherwise, if the \textit{nitpick\_psimp} set associated with
  2615 the constant is not empty, it uses these rules as the specification of the
  2616 constant.
  2617 
  2618 \item[3.] Otherwise, it looks up the definition of the constant:
  2619 
  2620 \begin{enum}
  2621 \item[1.] If the \textit{nitpick\_def} set associated with the constant
  2622 is not empty, it uses the latest rule added to the set as the definition of the
  2623 constant; otherwise it uses the actual definition axiom.
  2624 \item[2.] If the definition is of the form
  2625 
  2626 \qquad $c~{?}x_1~\ldots~{?}x_m \,\equiv\, \lambda y_1~\ldots~y_n.\; \textit{lfp}~(\lambda f.\; t)$,
  2627 
  2628 then Nitpick assumes that the definition was made using an inductive package and
  2629 based on the introduction rules marked with \textit{nitpick\_\allowbreak
  2630 ind\_\allowbreak intros} tries to determine whether the definition is
  2631 well-founded.
  2632 \end{enum}
  2633 \end{enum}
  2634 
  2635 As an illustration, consider the inductive definition
  2636 
  2637 \prew
  2638 \textbf{inductive}~\textit{odd}~\textbf{where} \\
  2639 ``\textit{odd}~1'' $\,\mid$ \\
  2640 ``\textit{odd}~$n\,\Longrightarrow\, \textit{odd}~(\textit{Suc}~(\textit{Suc}~n))$''
  2641 \postw
  2642 
  2643 Isabelle automatically attaches the \textit{nitpick\_intro} attribute to
  2644 the above rules. Nitpick then uses the \textit{lfp}-based definition in
  2645 conjunction with these rules. To override this, we can specify an alternative
  2646 definition as follows:
  2647 
  2648 \prew
  2649 \textbf{lemma} $\mathit{odd\_def}'$ [\textit{nitpick\_def}]:\kern.4em ``$\textit{odd}~n \,\equiv\, n~\textrm{mod}~2 = 1$''
  2650 \postw
  2651 
  2652 Nitpick then expands all occurrences of $\mathit{odd}~n$ to $n~\textrm{mod}~2
  2653 = 1$. Alternatively, we can specify an equational specification of the constant:
  2654 
  2655 \prew
  2656 \textbf{lemma} $\mathit{odd\_simp}'$ [\textit{nitpick\_simp}]:\kern.4em ``$\textit{odd}~n = (n~\textrm{mod}~2 = 1)$''
  2657 \postw
  2658 
  2659 Such tweaks should be done with great care, because Nitpick will assume that the
  2660 constant is completely defined by its equational specification. For example, if
  2661 you make ``$\textit{odd}~(2 * k + 1)$'' a \textit{nitpick\_simp} rule and neglect to provide rules to handle the $2 * k$ case, Nitpick will define
  2662 $\textit{odd}~n$ arbitrarily for even values of $n$. The \textit{debug}
  2663 (\S\ref{output-format}) option is extremely useful to understand what is going
  2664 on when experimenting with \textit{nitpick\_} attributes.
  2665 
  2666 \section{Standard ML Interface}
  2667 \label{standard-ml-interface}
  2668 
  2669 Nitpick provides a rich Standard ML interface used mainly for internal purposes
  2670 and debugging. Among the most interesting functions exported by Nitpick are
  2671 those that let you invoke the tool programmatically and those that let you
  2672 register and unregister custom coinductive datatypes.
  2673 
  2674 \subsection{Invocation of Nitpick}
  2675 \label{invocation-of-nitpick}
  2676 
  2677 The \textit{Nitpick} structure offers the following functions for invoking your
  2678 favorite counterexample generator:
  2679 
  2680 \prew
  2681 $\textbf{val}\,~\textit{pick\_nits\_in\_term} : \\
  2682 \hbox{}\quad\textit{Proof.state} \rightarrow \textit{params} \rightarrow \textit{bool} \rightarrow \textit{term~list} \rightarrow \textit{term} \\
  2683 \hbox{}\quad{\rightarrow}\; \textit{string} * \textit{Proof.state}$ \\
  2684 $\textbf{val}\,~\textit{pick\_nits\_in\_subgoal} : \\
  2685 \hbox{}\quad\textit{Proof.state} \rightarrow \textit{params} \rightarrow \textit{bool} \rightarrow \textit{int} \rightarrow \textit{string} * \textit{Proof.state}$
  2686 \postw
  2687 
  2688 The return value is a new proof state paired with an outcome string
  2689 (``genuine'', ``quasi\_genuine'', ``potential'', ``none'', or ``unknown''). The
  2690 \textit{params} type is a large record that lets you set Nitpick's options. The
  2691 current default options can be retrieved by calling the following function
  2692 defined in the \textit{Nitpick\_Isar} structure:
  2693 
  2694 \prew
  2695 $\textbf{val}\,~\textit{default\_params} :\,
  2696 \textit{theory} \rightarrow (\textit{string} * \textit{string})~\textit{list} \rightarrow \textit{params}$
  2697 \postw
  2698 
  2699 The second argument lets you override option values before they are parsed and
  2700 put into a \textit{params} record. Here is an example:
  2701 
  2702 \prew
  2703 $\textbf{val}\,~\textit{params} = \textit{Nitpick\_Isar.default\_params}~\textit{thy}~[(\textrm{``}\textrm{timeout}\textrm{''},\, \textrm{``}\textrm{none}\textrm{''})]$ \\
  2704 $\textbf{val}\,~(\textit{outcome},\, \textit{state}') = \textit{Nitpick.pick\_nits\_in\_subgoal}~\begin{aligned}[t]
  2705 & \textit{state}~\textit{params}~\textit{false} \\[-2pt]
  2706 & \textit{subgoal}\end{aligned}$
  2707 \postw
  2708 
  2709 \let\antiq=\textrm
  2710 
  2711 \subsection{Registration of Coinductive Datatypes}
  2712 \label{registration-of-coinductive-datatypes}
  2713 
  2714 If you have defined a custom coinductive datatype, you can tell Nitpick about
  2715 it, so that it can use an efficient Kodkod axiomatization similar to the one it
  2716 uses for lazy lists. The interface for registering and unregistering coinductive
  2717 datatypes consists of the following pair of functions defined in the
  2718 \textit{Nitpick} structure:
  2719 
  2720 \prew
  2721 $\textbf{val}\,~\textit{register\_codatatype} :\,
  2722 \textit{typ} \rightarrow \textit{string} \rightarrow \textit{styp~list} \rightarrow \textit{theory} \rightarrow \textit{theory}$ \\
  2723 $\textbf{val}\,~\textit{unregister\_codatatype} :\,
  2724 \textit{typ} \rightarrow \textit{theory} \rightarrow \textit{theory}$
  2725 \postw
  2726 
  2727 The type $'a~\textit{llist}$ of lazy lists is already registered; had it
  2728 not been, you could have told Nitpick about it by adding the following line
  2729 to your theory file:
  2730 
  2731 \prew
  2732 $\textbf{setup}~\,\{{*}\,~\!\begin{aligned}[t]
  2733 & \textit{Nitpick.register\_codatatype} \\[-2pt]
  2734 & \qquad @\{\antiq{typ}~``\kern1pt'a~\textit{llist}\textrm{''}\}~@\{\antiq{const\_name}~ \textit{llist\_case}\} \\[-2pt] %% TYPESETTING
  2735 & \qquad (\textit{map}~\textit{dest\_Const}~[@\{\antiq{term}~\textit{LNil}\},\, @\{\antiq{term}~\textit{LCons}\}])\,\ {*}\}\end{aligned}$
  2736 \postw
  2737 
  2738 The \textit{register\_codatatype} function takes a coinductive type, its case
  2739 function, and the list of its constructors. The case function must take its
  2740 arguments in the order that the constructors are listed. If no case function
  2741 with the correct signature is available, simply pass the empty string.
  2742 
  2743 On the other hand, if your goal is to cripple Nitpick, add the following line to
  2744 your theory file and try to check a few conjectures about lazy lists:
  2745 
  2746 \prew
  2747 $\textbf{setup}~\,\{{*}\,~\textit{Nitpick.unregister\_codatatype}~@\{\antiq{typ}~``
  2748 \kern1pt'a~\textit{list}\textrm{''}\}\ \,{*}\}$
  2749 \postw
  2750 
  2751 Inductive datatypes can be registered as coinductive datatypes, given
  2752 appropriate coinductive constructors. However, doing so precludes
  2753 the use of the inductive constructors---Nitpick will generate an error if they
  2754 are needed.
  2755 
  2756 \section{Known Bugs and Limitations}
  2757 \label{known-bugs-and-limitations}
  2758 
  2759 Here are the known bugs and limitations in Nitpick at the time of writing:
  2760 
  2761 \begin{enum}
  2762 \item[$\bullet$] Underspecified functions defined using the \textbf{primrec},
  2763 \textbf{function}, or \textbf{nominal\_\allowbreak primrec} packages can lead
  2764 Nitpick to generate spurious counterexamples for theorems that refer to values
  2765 for which the function is not defined. For example:
  2766 
  2767 \prew
  2768 \textbf{primrec} \textit{prec} \textbf{where} \\
  2769 ``$\textit{prec}~(\textit{Suc}~n) = n$'' \\[2\smallskipamount]
  2770 \textbf{lemma} ``$\textit{prec}~0 = \undef$'' \\
  2771 \textbf{nitpick} \\[2\smallskipamount]
  2772 \quad{\slshape Nitpick found a counterexample for \textit{card nat}~= 2: 
  2773 \nopagebreak
  2774 \\[2\smallskipamount]
  2775 \hbox{}\qquad Empty assignment} \nopagebreak\\[2\smallskipamount]
  2776 \textbf{by}~(\textit{auto simp}:~\textit{prec\_def})
  2777 \postw
  2778 
  2779 Such theorems are considered bad style because they rely on the internal
  2780 representation of functions synthesized by Isabelle, which is an implementation
  2781 detail.
  2782 
  2783 \item[$\bullet$] Nitpick maintains a global cache of wellfoundedness conditions,
  2784 which can become invalid if you change the definition of an inductive predicate
  2785 that is registered in the cache. To clear the cache,
  2786 run Nitpick with the \textit{tac\_timeout} option set to a new value (e.g.,
  2787 501$\,\textit{ms}$).
  2788 
  2789 \item[$\bullet$] Nitpick produces spurious counterexamples when invoked after a
  2790 \textbf{guess} command in a structured proof.
  2791 
  2792 \item[$\bullet$] The \textit{nitpick\_} attributes and the
  2793 \textit{Nitpick.register\_} functions can cause havoc if used improperly.
  2794 
  2795 \item[$\bullet$] Although this has never been observed, arbitrary theorem
  2796 morphisms could possibly confuse Nitpick, resulting in spurious counterexamples.
  2797 
  2798 \item[$\bullet$] All constants, types, free variables, and schematic variables
  2799 whose names start with \textit{Nitpick}{.} are reserved for internal use.
  2800 \end{enum}
  2801 
  2802 \let\em=\sl
  2803 \bibliography{../manual}{}
  2804 \bibliographystyle{abbrv}
  2805 
  2806 \end{document}