~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to doc/refman/RefMan-ltac.tex

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
\chapter[The tactic language]{The tactic language\label{TacticLanguage}}
 
2
 
 
3
%\geometry{a4paper,body={5in,8in}}
 
4
 
 
5
This chapter gives a compact documentation of Ltac, the tactic
 
6
language available in {\Coq}. We start by giving the syntax, and next,
 
7
we present the informal semantics. If you want to know more regarding
 
8
this language and especially about its foundations, you can refer
 
9
to~\cite{Del00}. Chapter~\ref{Tactics-examples} is devoted to giving
 
10
examples of use of this language on small but also with non-trivial
 
11
problems.
 
12
 
 
13
 
 
14
\section{Syntax}
 
15
 
 
16
\def\tacexpr{\textrm{\textsl{expr}}}
 
17
\def\tacexprlow{\textrm{\textsl{tacexpr$_1$}}}
 
18
\def\tacexprinf{\textrm{\textsl{tacexpr$_2$}}}
 
19
\def\tacexprpref{\textrm{\textsl{tacexpr$_3$}}}
 
20
\def\atom{\textrm{\textsl{atom}}}
 
21
%%\def\recclause{\textrm{\textsl{rec\_clause}}}
 
22
\def\letclause{\textrm{\textsl{let\_clause}}}
 
23
\def\matchrule{\textrm{\textsl{match\_rule}}}
 
24
\def\contextrule{\textrm{\textsl{context\_rule}}}
 
25
\def\contexthyps{\textrm{\textsl{context\_hyps}}}
 
26
\def\tacarg{\nterm{tacarg}}
 
27
\def\cpattern{\nterm{cpattern}}
 
28
 
 
29
The syntax of the tactic language is given Figures~\ref{ltac}
 
30
and~\ref{ltac_aux}. See Chapter~\ref{BNF-syntax} for a description of
 
31
the BNF metasyntax used in these grammar rules. Various already
 
32
defined entries will be used in this chapter: entries
 
33
{\naturalnumber}, {\integer}, {\ident}, {\qualid}, {\term},
 
34
{\cpattern} and {\atomictac} represent respectively the natural and
 
35
integer numbers, the authorized identificators and qualified names,
 
36
{\Coq}'s terms and patterns and all the atomic tactics described in
 
37
Chapter~\ref{Tactics}. The syntax of {\cpattern} is the same as that
 
38
of terms, but it is extended with pattern matching metavariables. In
 
39
{\cpattern}, a pattern-matching metavariable is represented with the
 
40
syntax {\tt ?id} where {\tt id} is an {\ident}. The notation {\tt \_}
 
41
can also be used to denote metavariable whose instance is
 
42
irrelevant. In the notation {\tt ?id}, the identifier allows us to
 
43
keep instantiations and to make constraints whereas {\tt \_} shows
 
44
that we are not interested in what will be matched. On the right hand
 
45
side of pattern-matching clauses, the named metavariable are used
 
46
without the question mark prefix. There is also a special notation for
 
47
second-order pattern-matching problems: in an applicative pattern of
 
48
the form {\tt @?id id$_1$ \ldots id$_n$}, the variable {\tt id}
 
49
matches any complex expression with (possible) dependencies in the
 
50
variables {\tt id$_1$ \ldots id$_n$} and returns a functional term of
 
51
the form {\tt fun id$_1$ \ldots id$_n$ => {\term}}.
 
52
 
 
53
 
 
54
The main entry of the grammar is {\tacexpr}. This language is used in
 
55
proof mode but it can also be used in toplevel definitions as shown in
 
56
Figure~\ref{ltactop}.
 
57
 
 
58
\begin{Remarks}
 
59
\item The infix tacticals ``\dots\ {\tt ||} \dots'' and ``\dots\ {\tt
 
60
    ;} \dots'' are associative. 
 
61
 
 
62
\item In {\tacarg}, there is an overlap between {\qualid} as a
 
63
direct tactic argument and {\qualid} as a particular case of
 
64
{\term}. The resolution is done by first looking for a reference of
 
65
the tactic language and if it fails, for a reference to a term. To
 
66
force the resolution as a reference of the tactic language, use the
 
67
form {\tt ltac :} {\qualid}. To force the resolution as a reference to
 
68
a term, use the syntax {\tt ({\qualid})}.
 
69
 
 
70
\item As shown by the figure, tactical {\tt ||} binds more than the
 
71
prefix tacticals {\tt try}, {\tt repeat}, {\tt do}, {\tt info} and
 
72
{\tt abstract} which themselves bind more than the postfix tactical
 
73
``{\tt \dots\ ;[ \dots\ ]}'' which binds more than ``\dots\ {\tt ;}
 
74
\dots''.
 
75
 
 
76
For instance
 
77
\begin{quote}
 
78
{\tt try repeat \tac$_1$ ||
 
79
  \tac$_2$;\tac$_3$;[\tac$_{31}$|\dots|\tac$_{3n}$];\tac$_4$.}
 
80
\end{quote}
 
81
is understood as 
 
82
\begin{quote}
 
83
{\tt (try (repeat (\tac$_1$ || \tac$_2$)));} \\
 
84
{\tt ((\tac$_3$;[\tac$_{31}$|\dots|\tac$_{3n}$]);\tac$_4$).}
 
85
\end{quote}
 
86
\end{Remarks}
 
87
 
 
88
 
 
89
\begin{figure}[htbp]
 
90
\begin{centerframe}
 
91
\begin{tabular}{lcl}
 
92
{\tacexpr} & ::= &
 
93
           {\tacexpr} {\tt ;} {\tacexpr}\\
 
94
& | & {\tacexpr} {\tt ; [} \nelist{\tacexpr}{|} {\tt ]}\\
 
95
& | & {\tacexprpref}\\
 
96
\\
 
97
{\tacexprpref} & ::= &
 
98
           {\tt do} {\it (}{\naturalnumber} {\it |} {\ident}{\it )} {\tacexprpref}\\
 
99
& | & {\tt info} {\tacexprpref}\\
 
100
& | & {\tt progress} {\tacexprpref}\\
 
101
& | & {\tt repeat} {\tacexprpref}\\
 
102
& | & {\tt try} {\tacexprpref}\\
 
103
& | & {\tacexprinf} \\
 
104
\\
 
105
{\tacexprinf} & ::= &
 
106
           {\tacexprlow} {\tt ||} {\tacexprpref}\\
 
107
& | & {\tacexprlow}\\
 
108
\\
 
109
{\tacexprlow} & ::= &
 
110
{\tt fun} \nelist{\name}{} {\tt =>} {\atom}\\
 
111
& | &
 
112
{\tt let} \zeroone{\tt rec} \nelist{\letclause}{\tt with} {\tt in}
 
113
{\atom}\\
 
114
& | &
 
115
{\tt match goal with} \nelist{\contextrule}{\tt |} {\tt end}\\
 
116
& | &
 
117
{\tt match reverse goal with} \nelist{\contextrule}{\tt |} {\tt end}\\
 
118
& | &
 
119
{\tt match} {\tacexpr} {\tt with} \nelist{\matchrule}{\tt |} {\tt end}\\
 
120
& | &
 
121
{\tt lazymatch goal with} \nelist{\contextrule}{\tt |} {\tt end}\\
 
122
& | &
 
123
{\tt lazymatch reverse goal with} \nelist{\contextrule}{\tt |} {\tt end}\\
 
124
& | &
 
125
{\tt lazymatch} {\tacexpr} {\tt with} \nelist{\matchrule}{\tt |} {\tt end}\\
 
126
& | & {\tt abstract} {\atom}\\
 
127
& | & {\tt abstract} {\atom} {\tt using} {\ident} \\
 
128
& | & {\tt first [} \nelist{\tacexpr}{\tt |} {\tt ]}\\
 
129
& | & {\tt solve [} \nelist{\tacexpr}{\tt |} {\tt ]}\\
 
130
& | & {\tt idtac} \sequence{\messagetoken}{}\\
 
131
& | & {\tt fail} \zeroone{\naturalnumber} \sequence{\messagetoken}{}\\
 
132
& | & {\tt fresh} ~|~ {\tt fresh} {\qstring}\\
 
133
& | & {\tt context} {\ident} {\tt [} {\term} {\tt ]}\\
 
134
& | & {\tt eval} {\nterm{redexpr}} {\tt in} {\term}\\
 
135
& | & {\tt type of} {\term}\\
 
136
& | & {\tt external} {\qstring} {\qstring} \nelist{\tacarg}{}\\
 
137
& | & {\tt constr :} {\term}\\
 
138
& | & \atomictac\\
 
139
& | & {\qualid} \nelist{\tacarg}{}\\
 
140
& | & {\atom}\\
 
141
\\
 
142
{\atom} & ::= &
 
143
           {\qualid} \\
 
144
& | & ()\\
 
145
& | & {\integer}\\
 
146
& | & {\tt (} {\tacexpr} {\tt )}\\
 
147
\\
 
148
{\messagetoken}\!\!\!\!\!\! & ::= & {\qstring} ~|~ {\term} ~|~ {\integer} \\
 
149
\end{tabular}
 
150
\end{centerframe}
 
151
\caption{Syntax of the tactic language}
 
152
\label{ltac}
 
153
\end{figure}
 
154
 
 
155
 
 
156
 
 
157
\begin{figure}[htbp]
 
158
\begin{centerframe}
 
159
\begin{tabular}{lcl}
 
160
\tacarg & ::= & 
 
161
        {\qualid}\\
 
162
& $|$ & {\tt ()} \\
 
163
& $|$ & {\tt ltac :} {\atom}\\
 
164
& $|$ & {\term}\\
 
165
\\
 
166
\letclause & ::= & {\ident} \sequence{\name}{} {\tt :=} {\tacexpr}\\
 
167
\\
 
168
\contextrule & ::= &
 
169
  \nelist{\contexthyps}{\tt ,} {\tt |-}{\cpattern} {\tt =>} {\tacexpr}\\
 
170
& $|$ & {\tt |-} {\cpattern} {\tt =>} {\tacexpr}\\
 
171
& $|$ & {\tt \_ =>} {\tacexpr}\\
 
172
\\
 
173
\contexthyps & ::= & {\name} {\tt :} {\cpattern}\\
 
174
\\
 
175
\matchrule & ::= &
 
176
           {\cpattern} {\tt =>} {\tacexpr}\\
 
177
& $|$ & {\tt context} {\zeroone{\ident}} {\tt [} {\cpattern} {\tt ]} {\tt =>} {\tacexpr}\\
 
178
& $|$ & {\tt \_ =>} {\tacexpr}\\
 
179
\end{tabular}
 
180
\end{centerframe}
 
181
\caption{Syntax of the tactic language (continued)}
 
182
\label{ltac_aux}
 
183
\end{figure}
 
184
 
 
185
\begin{figure}[ht]
 
186
\begin{centerframe}
 
187
\begin{tabular}{lcl}
 
188
\nterm{top} & ::= & {\tt Ltac} \nelist{\nterm{ltac\_def}} {\tt with} \\
 
189
\\
 
190
\nterm{ltac\_def} & ::= & {\ident} \sequence{\ident}{} {\tt :=}
 
191
{\tacexpr}\\
 
192
& $|$ &{\qualid} \sequence{\ident}{} {\tt ::=}{\tacexpr}
 
193
\end{tabular}
 
194
\end{centerframe}
 
195
\caption{Tactic toplevel definitions}
 
196
\label{ltactop}
 
197
\end{figure}
 
198
 
 
199
 
 
200
%%
 
201
%% Semantics
 
202
%%
 
203
\section{Semantics}
 
204
%\index[tactic]{Tacticals}
 
205
\index{Tacticals}
 
206
%\label{Tacticals}
 
207
 
 
208
Tactic expressions can only be applied in the context of a goal.  The
 
209
evaluation yields either a term, an integer or a tactic. Intermediary
 
210
results can be terms or integers but the final result must be a tactic
 
211
which is then applied to the current goal.
 
212
 
 
213
There is a special case for {\tt match goal} expressions of which
 
214
the clauses evaluate to tactics. Such expressions can only be used as
 
215
end result of a tactic expression (never as argument of a non recursive local
 
216
definition or of an application).
 
217
 
 
218
The rest of this section explains the semantics of every construction
 
219
of Ltac.
 
220
 
 
221
 
 
222
%% \subsection{Values}
 
223
 
 
224
%% Values are given by Figure~\ref{ltacval}. All these values are tactic values,
 
225
%% i.e. to be applied to a goal, except {\tt Fun}, {\tt Rec} and $arg$ values.
 
226
 
 
227
%% \begin{figure}[ht]
 
228
%% \noindent{}\framebox[6in][l]
 
229
%% {\parbox{6in}
 
230
%% {\begin{center}
 
231
%% \begin{tabular}{lp{0.1in}l}
 
232
%% $vexpr$ & ::= & $vexpr$ {\tt ;} $vexpr$\\
 
233
%% & | & $vexpr$ {\tt ; [} {\it (}$vexpr$ {\tt |}{\it )}$^*$ $vexpr$ {\tt
 
234
%% ]}\\
 
235
%% & | & $vatom$\\
 
236
%% \\
 
237
%% $vatom$ & ::= & {\tt Fun} \nelist{\inputfun}{}  {\tt ->} {\tacexpr}\\
 
238
%% %& | & {\tt Rec} \recclause\\
 
239
%% & | &
 
240
%% {\tt Rec} \nelist{\recclause}{\tt And} {\tt In}
 
241
%% {\tacexpr}\\
 
242
%% & | &
 
243
%% {\tt Match Context With} {\it (}$context\_rule$ {\tt |}{\it )}$^*$
 
244
%% $context\_rule$\\
 
245
%% & | & {\tt (} $vexpr$ {\tt )}\\
 
246
%% & | & $vatom$ {\tt Orelse} $vatom$\\
 
247
%% & | & {\tt Do} {\it (}{\naturalnumber} {\it |} {\ident}{\it )} $vatom$\\
 
248
%% & | & {\tt Repeat} $vatom$\\
 
249
%% & | & {\tt Try} $vatom$\\
 
250
%% & | & {\tt First [} {\it (}$vexpr$ {\tt |}{\it )}$^*$ $vexpr$ {\tt ]}\\
 
251
%% & | & {\tt Solve [} {\it (}$vexpr$ {\tt |}{\it )}$^*$ $vexpr$ {\tt ]}\\
 
252
%% & | & {\tt Idtac}\\
 
253
%% & | & {\tt Fail}\\
 
254
%% & | & {\primitivetactic}\\
 
255
%% & | & $arg$
 
256
%% \end{tabular}
 
257
%% \end{center}}}
 
258
%% \caption{Values of ${\cal L}_{tac}$}
 
259
%% \label{ltacval}
 
260
%% \end{figure}
 
261
 
 
262
%% \subsection{Evaluation}
 
263
 
 
264
\subsubsection[Sequence]{Sequence\tacindex{;}
 
265
\index{Tacticals!;@{\tt {\tac$_1$};\tac$_2$}}}
 
266
 
 
267
A sequence is an expression of the following form:
 
268
\begin{quote}
 
269
{\tacexpr}$_1$ {\tt ;} {\tacexpr}$_2$
 
270
\end{quote}
 
271
{\tacexpr}$_1$ and {\tacexpr}$_2$ are evaluated to $v_1$ and
 
272
$v_2$. $v_1$ and $v_2$ must be tactic values. $v_1$ is then applied
 
273
and $v_2$ is applied to every subgoal generated by the application of
 
274
$v_1$. Sequence is left-associative.
 
275
 
 
276
\subsubsection[General sequence]{General sequence\tacindex{;[\ldots$\mid$\ldots$\mid$\ldots]}}
 
277
%\tacindex{; [ | ]}
 
278
%\index{; [ | ]@{\tt ;[\ldots$\mid$\ldots$\mid$\ldots]}}
 
279
\index{Tacticals!; [ \mid ]@{\tt {\tac$_0$};[{\tac$_1$}$\mid$\ldots$\mid$\tac$_n$]}}
 
280
 
 
281
We can generalize the previous sequence operator as
 
282
\begin{quote}
 
283
{\tacexpr}$_0$ {\tt ; [} {\tacexpr}$_1$ {\tt |} $...$ {\tt |}
 
284
{\tacexpr}$_n$ {\tt ]}
 
285
\end{quote}
 
286
{\tacexpr}$_i$ is evaluated to $v_i$, for $i=0,...,n$. $v_0$ is
 
287
applied and $v_i$ is applied to the $i$-th generated subgoal by the
 
288
application of $v_0$, for $=1,...,n$. It fails if the application of
 
289
$v_0$ does not generate exactly $n$ subgoals.
 
290
 
 
291
\variant If no tactic is given for the $i$-th generated subgoal, it
 
292
behaves as if the tactic {\tt idtac} were given. For instance, {\tt
 
293
split ; [ | auto ]} is a shortcut for
 
294
{\tt split ; [ idtac | auto ]}.
 
295
 
 
296
 
 
297
\subsubsection[For loop]{For loop\tacindex{do}
 
298
\index{Tacticals!do@{\tt do}}}
 
299
 
 
300
There is a for loop that repeats a tactic {\num} times:
 
301
\begin{quote}
 
302
{\tt do} {\num} {\tacexpr}
 
303
\end{quote}
 
304
{\tacexpr} is evaluated to $v$. $v$ must be a tactic value. $v$ is
 
305
applied {\num} times. Supposing ${\num}>1$, after the first
 
306
application of $v$, $v$ is applied, at least once, to the generated
 
307
subgoals and so on. It fails if the application of $v$ fails before
 
308
the {\num} applications have been completed.
 
309
 
 
310
\subsubsection[Repeat loop]{Repeat loop\tacindex{repeat}
 
311
\index{Tacticals!repeat@{\tt repeat}}}
 
312
 
 
313
We have a repeat loop with:
 
314
\begin{quote}
 
315
{\tt repeat} {\tacexpr}
 
316
\end{quote}
 
317
{\tacexpr} is evaluated to $v$. If $v$ denotes a tactic, this tactic
 
318
is applied to the goal. If the application fails, the tactic is
 
319
applied recursively to all the generated subgoals until it eventually
 
320
fails.  The recursion stops in a subgoal when the tactic has failed.
 
321
The tactic {\tt repeat {\tacexpr}} itself never fails.
 
322
 
 
323
\subsubsection[Error catching]{Error catching\tacindex{try}
 
324
\index{Tacticals!try@{\tt try}}}
 
325
 
 
326
We can catch the tactic errors with:
 
327
\begin{quote}
 
328
{\tt try} {\tacexpr}
 
329
\end{quote}
 
330
{\tacexpr} is evaluated to $v$. $v$ must be a tactic value. $v$ is
 
331
applied. If the application of $v$ fails, it catches the error and
 
332
leaves the goal unchanged. If the level of the exception is positive,
 
333
then the exception is re-raised with its level decremented.
 
334
 
 
335
\subsubsection[Detecting progress]{Detecting progress\tacindex{progress}}
 
336
 
 
337
We can check if a tactic made progress with:
 
338
\begin{quote}
 
339
{\tt progress} {\tacexpr}
 
340
\end{quote}
 
341
{\tacexpr} is evaluated to $v$. $v$ must be a tactic value. $v$ is
 
342
applied. If the application of $v$ produced one subgoal equal to the
 
343
initial goal (up to syntactical equality), then an error of level 0 is
 
344
raised. 
 
345
 
 
346
\ErrMsg \errindex{Failed to progress}
 
347
 
 
348
\subsubsection[Branching]{Branching\tacindex{$\mid\mid$}
 
349
\index{Tacticals!orelse@{\tt $\mid\mid$}}}
 
350
 
 
351
We can easily branch with the following structure:
 
352
\begin{quote}
 
353
{\tacexpr}$_1$ {\tt ||} {\tacexpr}$_2$
 
354
\end{quote}
 
355
{\tacexpr}$_1$ and {\tacexpr}$_2$ are evaluated to $v_1$ and
 
356
$v_2$. $v_1$ and $v_2$ must be tactic values. $v_1$ is applied and if
 
357
it fails to progress then $v_2$ is applied. Branching is left-associative.
 
358
 
 
359
\subsubsection[First tactic to work]{First tactic to work\tacindex{first}
 
360
\index{Tacticals!first@{\tt first}}}
 
361
 
 
362
We may consider the first tactic to work (i.e. which does not fail) among a
 
363
panel of tactics:
 
364
\begin{quote}
 
365
{\tt first [} {\tacexpr}$_1$ {\tt |} $...$ {\tt |} {\tacexpr}$_n$ {\tt ]}
 
366
\end{quote}
 
367
{\tacexpr}$_i$ are evaluated to $v_i$ and $v_i$ must be tactic values, for 
 
368
$i=1,...,n$. Supposing $n>1$, it applies $v_1$, if it works, it stops else it
 
369
tries to apply $v_2$ and so on. It fails when there is no applicable tactic.
 
370
 
 
371
\ErrMsg \errindex{No applicable tactic}
 
372
 
 
373
\subsubsection[Solving]{Solving\tacindex{solve}
 
374
\index{Tacticals!solve@{\tt solve}}}
 
375
 
 
376
We may consider the first to solve (i.e. which generates no subgoal) among a
 
377
panel of tactics:
 
378
\begin{quote}
 
379
{\tt solve [} {\tacexpr}$_1$ {\tt |} $...$ {\tt |} {\tacexpr}$_n$ {\tt ]}
 
380
\end{quote}
 
381
{\tacexpr}$_i$ are evaluated to $v_i$ and $v_i$ must be tactic values, for 
 
382
$i=1,...,n$. Supposing $n>1$, it applies $v_1$, if it solves, it stops else it
 
383
tries to apply $v_2$ and so on. It fails if there is no solving tactic.
 
384
 
 
385
\ErrMsg \errindex{Cannot solve the goal}
 
386
 
 
387
\subsubsection[Identity]{Identity\tacindex{idtac}
 
388
\index{Tacticals!idtac@{\tt idtac}}}
 
389
 
 
390
The constant {\tt idtac} is the identity tactic: it leaves any goal
 
391
unchanged but it appears in the proof script.
 
392
 
 
393
\variant {\tt idtac \nelist{\messagetoken}{}}
 
394
 
 
395
This prints the given tokens. Strings and integers are printed
 
396
literally. If a term is given, it is printed, its variables being
 
397
interpreted in the current environment. In particular, if a variable
 
398
is given, its value is printed.
 
399
 
 
400
 
 
401
\subsubsection[Failing]{Failing\tacindex{fail}
 
402
\index{Tacticals!fail@{\tt fail}}}
 
403
 
 
404
The tactic {\tt fail} is the always-failing tactic: it does not solve
 
405
any goal. It is useful for defining other tacticals since it can be
 
406
catched by {\tt try} or {\tt match goal}. 
 
407
 
 
408
\begin{Variants}
 
409
\item {\tt fail $n$}\\
 
410
The number $n$ is the failure level. If no level is specified, it
 
411
defaults to $0$.  The level is used by {\tt try} and {\tt match goal}.
 
412
If $0$, it makes {\tt match goal} considering the next clause
 
413
(backtracking). If non zero, the current {\tt match goal} block or
 
414
{\tt try} command is aborted and the level is decremented.
 
415
 
 
416
\item {\tt fail \nelist{\messagetoken}{}}\\
 
417
The given tokens are used for printing the failure message.
 
418
 
 
419
\item {\tt fail $n$ \nelist{\messagetoken}{}}\\
 
420
This is a combination of the previous variants.
 
421
\end{Variants}
 
422
 
 
423
\ErrMsg \errindex{Tactic Failure {\it message} (level $n$)}.
 
424
 
 
425
\subsubsection[Local definitions]{Local definitions\index{Ltac!let}
 
426
\index{Ltac!let rec}
 
427
\index{let!in Ltac}
 
428
\index{let rec!in Ltac}}
 
429
 
 
430
Local definitions can be done as follows:
 
431
\begin{quote}
 
432
{\tt let} {\ident}$_1$ {\tt :=} {\tacexpr}$_1$\\
 
433
{\tt with} {\ident}$_2$ {\tt :=} {\tacexpr}$_2$\\
 
434
...\\
 
435
{\tt with} {\ident}$_n$ {\tt :=} {\tacexpr}$_n$ {\tt in}\\
 
436
{\tacexpr}
 
437
\end{quote}
 
438
each {\tacexpr}$_i$ is evaluated to $v_i$, then, {\tacexpr} is
 
439
evaluated by substituting $v_i$ to each occurrence of {\ident}$_i$,
 
440
for $i=1,...,n$. There is no dependencies between the {\tacexpr}$_i$
 
441
and the {\ident}$_i$.
 
442
 
 
443
Local definitions can be recursive by using {\tt let rec} instead of
 
444
{\tt let}. In this latter case, the definitions are evaluated lazily
 
445
so that the {\tt rec} keyword can be used also in non recursive cases
 
446
so as to avoid the eager evaluation of local definitions.
 
447
 
 
448
\subsubsection{Application}
 
449
 
 
450
An application is an expression of the following form:
 
451
\begin{quote}
 
452
{\qualid} {\tacarg}$_1$ ... {\tacarg}$_n$
 
453
\end{quote}
 
454
The reference {\qualid} must be bound to some defined tactic
 
455
definition expecting at least $n$ arguments.  The expressions
 
456
{\tacexpr}$_i$ are evaluated to $v_i$, for $i=1,...,n$.
 
457
%If {\tacexpr} is a {\tt Fun} or {\tt Rec} value then the body is evaluated by
 
458
%substituting $v_i$ to the formal parameters, for $i=1,...,n$. For recursive
 
459
%clauses, the bodies are lazily substituted (when an identifier to be evaluated
 
460
%is the name of a recursive clause).
 
461
 
 
462
%\subsection{Application of tactic values}
 
463
 
 
464
\subsubsection[Function construction]{Function construction\index{fun!in Ltac}
 
465
\index{Ltac!fun}}
 
466
 
 
467
A parameterized tactic can be built anonymously (without resorting to
 
468
local definitions) with:
 
469
\begin{quote}
 
470
{\tt fun} {\ident${}_1$} ... {\ident${}_n$} {\tt =>} {\tacexpr}
 
471
\end{quote}
 
472
Indeed, local definitions of functions are a syntactic sugar for
 
473
binding a {\tt fun} tactic to an identifier.
 
474
 
 
475
\subsubsection[Pattern matching on terms]{Pattern matching on terms\index{Ltac!match}
 
476
\index{match!in Ltac}}
 
477
 
 
478
We can carry out pattern matching on terms with:
 
479
\begin{quote}
 
480
{\tt match} {\tacexpr} {\tt with}\\
 
481
~~~{\cpattern}$_1$ {\tt =>} {\tacexpr}$_1$\\
 
482
~{\tt |} {\cpattern}$_2$ {\tt =>} {\tacexpr}$_2$\\
 
483
~...\\
 
484
~{\tt |} {\cpattern}$_n$ {\tt =>} {\tacexpr}$_n$\\
 
485
~{\tt |} {\tt \_} {\tt =>} {\tacexpr}$_{n+1}$\\
 
486
{\tt end}
 
487
\end{quote}
 
488
The expression {\tacexpr} is evaluated and should yield a term which
 
489
is matched against {\cpattern}$_1$. The matching is non-linear: if a
 
490
metavariable occurs more than once, it should match the same
 
491
expression every time. It is first-order except on the
 
492
variables of the form {\tt @?id} that occur in head position of an
 
493
application. For these variables, the matching is second-order and
 
494
returns a functional term.
 
495
 
 
496
If the matching with {\cpattern}$_1$ succeeds, then {\tacexpr}$_1$ is
 
497
evaluated into some value by substituting the pattern matching
 
498
instantiations to the metavariables. If {\tacexpr}$_1$ evaluates to a
 
499
tactic and the {\tt match} expression is in position to be applied to
 
500
a goal (e.g. it is not bound to a variable by a {\tt let in}), then
 
501
this tactic is applied. If the tactic succeeds, the list of resulting
 
502
subgoals is the result of the {\tt match} expression. If
 
503
{\tacexpr}$_1$ does not evaluate to a tactic or if the {\tt match}
 
504
expression is not in position to be applied to a goal, then the result
 
505
of the evaluation of {\tacexpr}$_1$ is the result of the {\tt match}
 
506
expression.
 
507
 
 
508
If the matching with {\cpattern}$_1$ fails, or if it succeeds but the
 
509
evaluation of {\tacexpr}$_1$ fails, or if the evaluation of
 
510
{\tacexpr}$_1$ succeeds but returns a tactic in execution position
 
511
whose execution fails, then {\cpattern}$_2$ is used and so on.  The
 
512
pattern {\_} matches any term and shunts all remaining patterns if
 
513
any. If all clauses fail (in particular, there is no pattern {\_})
 
514
then a no-matching-clause error is raised.
 
515
 
 
516
\begin{ErrMsgs}
 
517
 
 
518
\item \errindex{No matching clauses for match}
 
519
 
 
520
  No pattern can be used and, in particular, there is no {\tt \_} pattern.
 
521
 
 
522
\item \errindex{Argument of match does not evaluate to a term}
 
523
 
 
524
  This happens when {\tacexpr} does not denote a term.
 
525
 
 
526
\end{ErrMsgs}
 
527
 
 
528
\begin{Variants}
 
529
\item \index{context!in pattern}
 
530
There is a special form of patterns to match a subterm against the
 
531
pattern:
 
532
\begin{quote}
 
533
{\tt context} {\ident} {\tt [} {\cpattern} {\tt ]}
 
534
\end{quote}
 
535
It matches any term which one subterm matches {\cpattern}. If there is
 
536
a match, the optional {\ident} is assign the ``matched context'', that
 
537
is the initial term where the matched subterm is replaced by a
 
538
hole. The definition of {\tt context} in expressions below will show
 
539
how to use such term contexts.
 
540
 
 
541
If the evaluation of the right-hand-side of a valid match fails, the
 
542
next matching subterm is tried. If no further subterm matches, the
 
543
next clause is tried. Matching subterms are considered top-bottom and
 
544
from left to right (with respect to the raw printing obtained by
 
545
setting option {\tt Printing All}, see Section~\ref{SetPrintingAll}).
 
546
 
 
547
\begin{coq_example}
 
548
Ltac f x :=
 
549
  match x with
 
550
    context f [S ?X] => 
 
551
    idtac X;                    (* To display the evaluation order *)
 
552
    assert (p := refl_equal 1 : X=1);    (* To filter the case X=1 *)
 
553
    let x:= context f[O] in assert (x=O) (* To observe the context *)
 
554
  end.
 
555
Goal True.
 
556
f (3+4).
 
557
\end{coq_example}
 
558
 
 
559
\item \index{lazymatch!in Ltac}
 
560
\index{Ltac!lazymatch}
 
561
Using {\tt lazymatch} instead of {\tt match} has an effect if the
 
562
right-hand-side of a clause returns a tactic. With {\tt match}, the
 
563
tactic is applied to the current goal (and the next clause is tried if
 
564
it fails). With {\tt lazymatch}, the tactic is directly returned as
 
565
the result of the whole {\tt lazymatch} block without being first
 
566
tried to be applied to the goal. Typically, if the {\tt lazymatch}
 
567
block is bound to some variable $x$ in a {\tt let in}, then tactic
 
568
expression gets bound to the variable $x$.
 
569
 
 
570
\end{Variants}
 
571
 
 
572
\subsubsection[Pattern matching on goals]{Pattern matching on goals\index{Ltac!match goal}
 
573
\index{Ltac!match reverse goal}
 
574
\index{match goal!in Ltac}
 
575
\index{match reverse goal!in Ltac}}
 
576
 
 
577
We can make pattern matching on goals using the following expression:
 
578
\begin{quote}
 
579
\begin{tabbing}
 
580
{\tt match goal with}\\
 
581
~~\={\tt |} $hyp_{1,1}${\tt ,}...{\tt ,}$hyp_{1,m_1}$
 
582
   ~~{\tt |-}{\cpattern}$_1${\tt =>} {\tacexpr}$_1$\\
 
583
  \>{\tt |} $hyp_{2,1}${\tt ,}...{\tt ,}$hyp_{2,m_2}$
 
584
   ~~{\tt |-}{\cpattern}$_2${\tt =>} {\tacexpr}$_2$\\
 
585
~~...\\
 
586
  \>{\tt |} $hyp_{n,1}${\tt ,}...{\tt ,}$hyp_{n,m_n}$
 
587
   ~~{\tt |-}{\cpattern}$_n${\tt =>} {\tacexpr}$_n$\\
 
588
  \>{\tt |\_}~~~~{\tt =>} {\tacexpr}$_{n+1}$\\
 
589
{\tt end}
 
590
\end{tabbing}
 
591
\end{quote}
 
592
 
 
593
If each hypothesis pattern $hyp_{1,i}$, with $i=1,...,m_1$
 
594
is matched (non-linear first order unification) by an hypothesis of
 
595
the goal and if {\cpattern}$_1$ is matched by the conclusion of the
 
596
goal, then {\tacexpr}$_1$ is evaluated to $v_1$ by substituting the
 
597
pattern matching to the metavariables and the real hypothesis names
 
598
bound to the possible hypothesis names occurring in the hypothesis
 
599
patterns. If $v_1$ is a tactic value, then it is applied to the
 
600
goal. If this application fails, then another combination of
 
601
hypotheses is tried with the same proof context pattern. If there is
 
602
no other combination of hypotheses then the second proof context
 
603
pattern is tried and so on. If the next to last proof context pattern
 
604
fails then {\tacexpr}$_{n+1}$ is evaluated to $v_{n+1}$ and $v_{n+1}$
 
605
is applied. Note also that matching against subterms (using the {\tt
 
606
context} {\ident} {\tt [} {\cpattern} {\tt ]}) is available and may
 
607
itself induce extra backtrackings.
 
608
 
 
609
\ErrMsg \errindex{No matching clauses for match goal}
 
610
 
 
611
No clause succeeds, i.e. all matching patterns, if any,
 
612
fail at the application of the right-hand-side.
 
613
 
 
614
\medskip
 
615
 
 
616
It is important to know that each hypothesis of the goal can be
 
617
matched by at most one hypothesis pattern. The order of matching is
 
618
the following: hypothesis patterns are examined from the right to the
 
619
left (i.e. $hyp_{i,m_i}$ before $hyp_{i,1}$). For each hypothesis
 
620
pattern, the goal hypothesis are matched in order (fresher hypothesis
 
621
first), but it possible to reverse this order (older first) with
 
622
the {\tt match reverse goal with} variant.
 
623
 
 
624
\variant
 
625
\index{lazymatch goal!in Ltac}
 
626
\index{Ltac!lazymatch goal}
 
627
\index{lazymatch reverse goal!in Ltac}
 
628
\index{Ltac!lazymatch reverse goal}
 
629
Using {\tt lazymatch} instead of {\tt match} has an effect if the
 
630
right-hand-side of a clause returns a tactic. With {\tt match}, the
 
631
tactic is applied to the current goal (and the next clause is tried if
 
632
it fails). With {\tt lazymatch}, the tactic is directly returned as
 
633
the result of the whole {\tt lazymatch} block without being first
 
634
tried to be applied to the goal. Typically, if the {\tt lazymatch}
 
635
block is bound to some variable $x$ in a {\tt let in}, then tactic
 
636
expression gets bound to the variable $x$.
 
637
 
 
638
\begin{coq_example}
 
639
Ltac test_lazy :=
 
640
  lazymatch goal with
 
641
  | _ => idtac "here"; fail 
 
642
  | _ => idtac "wasn't lazy"; trivial
 
643
  end.
 
644
Ltac test_eager :=
 
645
  match goal with
 
646
  | _ => idtac "here"; fail 
 
647
  | _ => idtac "wasn't lazy"; trivial
 
648
  end.
 
649
Goal True.
 
650
test_lazy || idtac "was lazy".
 
651
test_eager || idtac "was lazy".
 
652
\end{coq_example}
 
653
 
 
654
\subsubsection[Filling a term context]{Filling a term context\index{context!in expression}}
 
655
 
 
656
The following expression is not a tactic in the sense that it does not
 
657
produce subgoals but generates a term to be used in tactic
 
658
expressions:
 
659
\begin{quote}
 
660
{\tt context} {\ident} {\tt [} {\tacexpr} {\tt ]}
 
661
\end{quote}
 
662
{\ident} must denote a context variable bound by a {\tt context}
 
663
pattern of a {\tt match} expression. This expression evaluates
 
664
replaces the hole of the value of {\ident} by the value of
 
665
{\tacexpr}.
 
666
 
 
667
\ErrMsg \errindex{not a context variable}
 
668
 
 
669
 
 
670
\subsubsection[Generating fresh hypothesis names]{Generating fresh hypothesis names\index{Ltac!fresh}
 
671
\index{fresh!in Ltac}}
 
672
 
 
673
Tactics sometimes have to generate new names for hypothesis. Letting
 
674
the system decide a name with the {\tt intro} tactic is not so good
 
675
since it is very awkward to retrieve the name the system gave.
 
676
The following expression returns an identifier:
 
677
\begin{quote}
 
678
{\tt fresh} \nelist{\textrm{\textsl{component}}}{}
 
679
\end{quote}
 
680
It evaluates to an identifier unbound in the goal. This fresh
 
681
identifier is obtained by concatenating the value of the
 
682
\textrm{\textsl{component}}'s (each of them is, either an {\ident} which
 
683
has to refer to a name, or directly a name denoted by a
 
684
{\qstring}). If the resulting name is already used, it is padded
 
685
with a number so that it becomes fresh. If no component is
 
686
given, the name is a fresh derivative of the name {\tt H}.
 
687
 
 
688
\subsubsection[Computing in a constr]{Computing in a constr\index{Ltac!eval}
 
689
\index{eval!in Ltac}}
 
690
 
 
691
Evaluation of a term can be performed with:
 
692
\begin{quote}
 
693
{\tt eval} {\nterm{redexpr}} {\tt in} {\term}
 
694
\end{quote}
 
695
where \nterm{redexpr} is a reduction tactic among {\tt red}, {\tt
 
696
hnf}, {\tt compute}, {\tt simpl}, {\tt cbv}, {\tt lazy}, {\tt unfold},
 
697
{\tt fold}, {\tt pattern}.
 
698
 
 
699
\subsubsection{Type-checking a term}
 
700
%\tacindex{type of}
 
701
\index{Ltac!type of}
 
702
\index{type of!in Ltac}
 
703
 
 
704
The following returns the type of {\term}:
 
705
 
 
706
\begin{quote}
 
707
{\tt type of} {\term}
 
708
\end{quote}
 
709
 
 
710
\subsubsection[Accessing tactic decomposition]{Accessing tactic decomposition\tacindex{info}
 
711
\index{Tacticals!info@{\tt info}}}
 
712
 
 
713
Tactical ``{\tt info} {\tacexpr}'' is not really a tactical. For
 
714
elementary tactics, this is equivalent to \tacexpr. For complex tactic
 
715
like \texttt{auto}, it displays the operations performed by the
 
716
tactic.
 
717
 
 
718
\subsubsection[Proving a subgoal as a separate lemma]{Proving a subgoal as a separate lemma\tacindex{abstract}
 
719
\index{Tacticals!abstract@{\tt abstract}}}
 
720
 
 
721
From the outside ``\texttt{abstract \tacexpr}'' is the same as
 
722
{\tt solve \tacexpr}. Internally it saves an auxiliary lemma called 
 
723
{\ident}\texttt{\_subproof}\textit{n} where {\ident} is the name of the
 
724
current goal and \textit{n} is chosen so that this is a fresh name.
 
725
 
 
726
This tactical is useful with tactics such as \texttt{omega} or
 
727
\texttt{discriminate} that generate huge proof terms. With that tool
 
728
the user can avoid the explosion at time of the \texttt{Save} command
 
729
without having to cut manually the proof in smaller lemmas.
 
730
 
 
731
\begin{Variants}
 
732
\item \texttt{abstract {\tacexpr} using {\ident}}.\\
 
733
  Give explicitly the name of the auxiliary lemma.
 
734
\end{Variants}
 
735
 
 
736
\ErrMsg \errindex{Proof is not complete}
 
737
 
 
738
\subsubsection[Calling an external tactic]{Calling an external tactic\index{Ltac!external}}
 
739
 
 
740
The tactic {\tt external} allows to run an executable outside the
 
741
{\Coq} executable. The communication is done via an XML encoding of
 
742
constructions. The syntax of the command is
 
743
 
 
744
\begin{quote}
 
745
{\tt external} "\textsl{command}" "\textsl{request}" \nelist{\tacarg}{}
 
746
\end{quote}
 
747
 
 
748
The string \textsl{command}, to be interpreted in the default
 
749
execution path of the operating system, is the name of the external
 
750
command. The string \textsl{request} is the name of a request to be
 
751
sent to the external command. Finally the list of tactic arguments
 
752
have to evaluate to terms. An XML tree of the following form is sent
 
753
to the standard input of the external command.
 
754
\medskip
 
755
 
 
756
\begin{tabular}{l}
 
757
\texttt{<REQUEST req="}\textsl{request}\texttt{">}\\
 
758
the XML tree of the first argument\\
 
759
{\ldots}\\
 
760
the XML tree of the last argument\\
 
761
\texttt{</REQUEST>}\\
 
762
\end{tabular}
 
763
\medskip
 
764
 
 
765
Conversely, the external command must send on its standard output an
 
766
XML tree of the following forms:
 
767
 
 
768
\medskip
 
769
\begin{tabular}{l}
 
770
\texttt{<TERM>}\\
 
771
the XML tree of a term\\
 
772
\texttt{</TERM>}\\
 
773
\end{tabular}
 
774
\medskip
 
775
 
 
776
\noindent or 
 
777
 
 
778
\medskip
 
779
\begin{tabular}{l}
 
780
\texttt{<CALL uri="}\textsl{ltac\_qualified\_ident}\texttt{">}\\
 
781
the XML tree of a first argument\\
 
782
{\ldots}\\
 
783
the XML tree of a last argument\\
 
784
\texttt{</CALL>}\\
 
785
\end{tabular}
 
786
 
 
787
\medskip
 
788
\noindent where \textsl{ltac\_qualified\_ident} is the name of a
 
789
defined {\ltac} function and each subsequent XML tree is recursively a
 
790
\texttt{CALL} or a \texttt{TERM} node.
 
791
 
 
792
The Document Type Definition (DTD) for terms of the Calculus of
 
793
Inductive Constructions is the one developed as part of the MoWGLI
 
794
European project. It can be found in the file {\tt dev/doc/cic.dtd} of
 
795
the {\Coq} source archive.
 
796
 
 
797
An example of parser for this DTD, written in the Objective Caml -
 
798
Camlp4 language, can be found in the file {\tt parsing/g\_xml.ml4} of
 
799
the {\Coq} source archive.
 
800
 
 
801
\section[Tactic toplevel definitions]{Tactic toplevel definitions\comindex{Ltac}}
 
802
 
 
803
\subsection{Defining {\ltac} functions}
 
804
 
 
805
Basically, {\ltac} toplevel definitions are made as follows:
 
806
%{\tt Tactic Definition} {\ident} {\tt :=} {\tacexpr}\\
 
807
%
 
808
%{\tacexpr} is evaluated to $v$ and $v$ is associated to {\ident}. Next, every
 
809
%script is evaluated by substituting $v$ to {\ident}.
 
810
%
 
811
%We can define functional definitions by:\\
 
812
\begin{quote}
 
813
{\tt Ltac} {\ident} {\ident}$_1$ ... {\ident}$_n$ {\tt :=}
 
814
{\tacexpr}
 
815
\end{quote}
 
816
This defines a new {\ltac} function that can be used in any tactic
 
817
script or new {\ltac} toplevel definition.
 
818
 
 
819
\Rem The preceding definition can equivalently be written:
 
820
\begin{quote}
 
821
{\tt Ltac} {\ident} {\tt := fun} {\ident}$_1$ ... {\ident}$_n$
 
822
{\tt =>} {\tacexpr}
 
823
\end{quote}
 
824
Recursive and mutual recursive function definitions are also
 
825
possible with the syntax:
 
826
\begin{quote}
 
827
{\tt Ltac} {\ident}$_1$ {\ident}$_{1,1}$ ...
 
828
{\ident}$_{1,m_1}$~~{\tt :=} {\tacexpr}$_1$\\
 
829
{\tt with} {\ident}$_2$ {\ident}$_{2,1}$ ... {\ident}$_{2,m_2}$~~{\tt :=}
 
830
{\tacexpr}$_2$\\
 
831
...\\
 
832
{\tt with} {\ident}$_n$ {\ident}$_{n,1}$ ... {\ident}$_{n,m_n}$~~{\tt :=}
 
833
{\tacexpr}$_n$
 
834
\end{quote}
 
835
\medskip
 
836
It is also possible to \emph{redefine} an existing user-defined tactic
 
837
using the syntax:
 
838
\begin{quote}
 
839
{\tt Ltac} {\qualid} {\ident}$_1$ ... {\ident}$_n$ {\tt ::=}
 
840
{\tacexpr}
 
841
\end{quote}
 
842
A previous definition of \qualid must exist in the environment.
 
843
The new definition will always be used instead of the old one and
 
844
it goes accross module boundaries.
 
845
 
 
846
\subsection[Printing {\ltac} tactics]{Printing {\ltac} tactics\comindex{Print Ltac}}
 
847
 
 
848
Defined {\ltac} functions can be displayed using the command
 
849
 
 
850
\begin{quote}
 
851
{\tt Print Ltac {\qualid}.}
 
852
\end{quote}
 
853
 
 
854
\section[Debugging {\ltac} tactics]{Debugging {\ltac} tactics\comindex{Set Ltac Debug}
 
855
\comindex{Unset Ltac Debug}
 
856
\comindex{Test Ltac Debug}}
 
857
 
 
858
The {\ltac} interpreter comes with a step-by-step debugger. The
 
859
debugger can be activated using the command
 
860
 
 
861
\begin{quote}
 
862
{\tt Set Ltac Debug.}
 
863
\end{quote}
 
864
 
 
865
\noindent and deactivated using the command
 
866
 
 
867
\begin{quote}
 
868
{\tt Unset Ltac Debug.}
 
869
\end{quote}
 
870
 
 
871
To know if the debugger is on, use the command \texttt{Test Ltac Debug}.
 
872
When the debugger is activated, it stops at every step of the
 
873
evaluation of the current {\ltac} expression and it prints information
 
874
on what it is doing. The debugger stops, prompting for a command which
 
875
can be one of the following:
 
876
 
 
877
\medskip
 
878
\begin{tabular}{ll}
 
879
simple newline: & go to the next step\\
 
880
h: & get help\\
 
881
x: & exit current evaluation\\
 
882
s: & continue current evaluation without stopping\\
 
883
r$n$: & advance $n$ steps further\\
 
884
\end{tabular}
 
885
\endinput
 
886
 
 
887
\subsection{Permutation on closed lists}
 
888
 
 
889
\begin{figure}[b]
 
890
\begin{center}
 
891
\fbox{\begin{minipage}{0.95\textwidth}
 
892
\begin{coq_example*}
 
893
Require Import List.
 
894
Section Sort.
 
895
Variable A : Set.
 
896
Inductive permut : list A -> list A -> Prop :=
 
897
  | permut_refl   : forall l, permut l l
 
898
  | permut_cons   :
 
899
      forall a l0 l1, permut l0 l1 -> permut (a :: l0) (a :: l1)
 
900
  | permut_append : forall a l, permut (a :: l) (l ++ a :: nil)
 
901
  | permut_trans  :
 
902
      forall l0 l1 l2, permut l0 l1 -> permut l1 l2 -> permut l0 l2.
 
903
End Sort.
 
904
\end{coq_example*}
 
905
\end{center}
 
906
\caption{Definition of the permutation predicate}
 
907
\label{permutpred}
 
908
\end{figure}
 
909
 
 
910
 
 
911
Another more complex example is the problem of permutation on closed
 
912
lists. The aim is to show that a closed list is a permutation of
 
913
another one.  First, we define the permutation predicate as shown on
 
914
Figure~\ref{permutpred}.
 
915
 
 
916
\begin{figure}[p]
 
917
\begin{center}
 
918
\fbox{\begin{minipage}{0.95\textwidth}
 
919
\begin{coq_example}
 
920
Ltac Permut n :=
 
921
  match goal with
 
922
  | |- (permut _ ?l ?l) => apply permut_refl
 
923
  | |- (permut _ (?a :: ?l1) (?a :: ?l2)) =>
 
924
      let newn := eval compute in (length l1) in
 
925
      (apply permut_cons; Permut newn)
 
926
  | |- (permut ?A (?a :: ?l1) ?l2) =>
 
927
      match eval compute in n with
 
928
      | 1 => fail
 
929
      | _ =>
 
930
          let l1' := constr:(l1 ++ a :: nil) in
 
931
          (apply (permut_trans A (a :: l1) l1' l2);
 
932
            [ apply permut_append | compute; Permut (pred n) ])
 
933
      end
 
934
  end.
 
935
Ltac PermutProve :=
 
936
  match goal with
 
937
  | |- (permut _ ?l1 ?l2) =>
 
938
      match eval compute in (length l1 = length l2) with
 
939
      | (?n = ?n) => Permut n
 
940
      end
 
941
  end.
 
942
\end{coq_example}
 
943
\end{minipage}}
 
944
\end{center}
 
945
\caption{Permutation tactic}
 
946
\label{permutltac}
 
947
\end{figure}
 
948
 
 
949
\begin{figure}[p]
 
950
\begin{center}
 
951
\fbox{\begin{minipage}{0.95\textwidth}
 
952
\begin{coq_example*}
 
953
Lemma permut_ex1 :
 
954
  permut nat (1 :: 2 :: 3 :: nil) (3 :: 2 :: 1 :: nil).
 
955
Proof.
 
956
PermutProve.
 
957
Qed.
 
958
 
 
959
Lemma permut_ex2 :
 
960
  permut nat
 
961
    (0 :: 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: 9 :: nil)
 
962
    (0 :: 2 :: 4 :: 6 :: 8 :: 9 :: 7 :: 5 :: 3 :: 1 :: nil).
 
963
Proof.
 
964
PermutProve.
 
965
Qed.
 
966
\end{coq_example*}
 
967
\end{minipage}}
 
968
\end{center}
 
969
\caption{Examples of {\tt PermutProve} use}
 
970
\label{permutlem}
 
971
\end{figure}
 
972
 
 
973
Next, we can write naturally the tactic and the result can be seen on
 
974
Figure~\ref{permutltac}. We can notice that we use two toplevel
 
975
definitions {\tt PermutProve} and {\tt Permut}. The function to be
 
976
called is {\tt PermutProve} which computes the lengths of the two
 
977
lists and calls {\tt Permut} with the length if the two lists have the
 
978
same length. {\tt Permut} works as expected.  If the two lists are
 
979
equal, it concludes. Otherwise, if the lists have identical first
 
980
elements, it applies {\tt Permut} on the tail of the lists.  Finally,
 
981
if the lists have different first elements, it puts the first element
 
982
of one of the lists (here the second one which appears in the {\tt
 
983
  permut} predicate) at the end if that is possible, i.e., if the new
 
984
first element has been at this place previously. To verify that all
 
985
rotations have been done for a list, we use the length of the list as
 
986
an argument for {\tt Permut} and this length is decremented for each
 
987
rotation down to, but not including, 1 because for a list of length
 
988
$n$, we can make exactly $n-1$ rotations to generate at most $n$
 
989
distinct lists. Here, it must be noticed that we use the natural
 
990
numbers of {\Coq} for the rotation counter. On Figure~\ref{ltac}, we
 
991
can see that it is possible to use usual natural numbers but they are
 
992
only used as arguments for primitive tactics and they cannot be
 
993
handled, in particular, we cannot make computations with them. So, a
 
994
natural choice is to use {\Coq} data structures so that {\Coq} makes
 
995
the computations (reductions) by {\tt eval compute in} and we can get
 
996
the terms back by {\tt match}.
 
997
 
 
998
With {\tt PermutProve}, we can now prove lemmas such those shown on
 
999
Figure~\ref{permutlem}.
 
1000
 
 
1001
 
 
1002
\subsection{Deciding intuitionistic propositional logic}
 
1003
 
 
1004
\begin{figure}[tbp]
 
1005
\begin{center}
 
1006
\fbox{\begin{minipage}{0.95\textwidth}
 
1007
\begin{coq_example}
 
1008
Ltac Axioms :=
 
1009
  match goal with
 
1010
  | |- True => trivial
 
1011
  | _:False |- _  => elimtype False; assumption
 
1012
  | _:?A |- ?A  => auto
 
1013
  end.
 
1014
Ltac DSimplif :=
 
1015
  repeat
 
1016
   (intros;
 
1017
    match goal with
 
1018
     | id:(~ _) |- _ => red in id
 
1019
     | id:(_ /\ _) |- _ =>
 
1020
         elim id; do 2 intro; clear id
 
1021
     | id:(_ \/ _) |- _ =>
 
1022
         elim id; intro; clear id
 
1023
     | id:(?A /\ ?B -> ?C) |- _ =>
 
1024
         cut (A -> B -> C);
 
1025
          [ intro | intros; apply id; split; assumption ]
 
1026
     | id:(?A \/ ?B -> ?C) |- _ =>
 
1027
         cut (B -> C);
 
1028
          [ cut (A -> C);
 
1029
             [ intros; clear id
 
1030
             | intro; apply id; left; assumption ]
 
1031
          | intro; apply id; right; assumption ]
 
1032
     | id0:(?A -> ?B),id1:?A |- _ =>
 
1033
         cut B; [ intro; clear id0 | apply id0; assumption ]
 
1034
     | |- (_ /\ _) => split
 
1035
     | |- (~ _) => red
 
1036
     end).
 
1037
\end{coq_example}
 
1038
\end{minipage}}
 
1039
\end{center}
 
1040
\caption{Deciding intuitionistic propositions (1)}
 
1041
\label{tautoltaca}
 
1042
\end{figure}
 
1043
 
 
1044
\begin{figure}
 
1045
\begin{center}
 
1046
\fbox{\begin{minipage}{0.95\textwidth}
 
1047
\begin{coq_example}
 
1048
Ltac TautoProp :=
 
1049
  DSimplif;
 
1050
   Axioms ||
 
1051
     match goal with
 
1052
     | id:((?A -> ?B) -> ?C) |- _ =>
 
1053
          cut (B -> C);
 
1054
          [ intro; cut (A -> B);
 
1055
             [ intro; cut C;
 
1056
                [ intro; clear id | apply id; assumption ]
 
1057
             | clear id ]
 
1058
          | intro; apply id; intro; assumption ]; TautoProp
 
1059
     | id:(~ ?A -> ?B) |- _ =>
 
1060
         cut (False -> B);
 
1061
          [ intro; cut (A -> False);
 
1062
             [ intro; cut B;
 
1063
                [ intro; clear id | apply id; assumption ]
 
1064
             | clear id ]
 
1065
          | intro; apply id; red; intro; assumption ]; TautoProp
 
1066
     | |- (_ \/ _) => (left; TautoProp) || (right; TautoProp)
 
1067
     end.
 
1068
\end{coq_example}
 
1069
\end{minipage}}
 
1070
\end{center}
 
1071
\caption{Deciding intuitionistic propositions (2)}
 
1072
\label{tautoltacb}
 
1073
\end{figure}
 
1074
 
 
1075
The pattern matching on goals allows a complete and so a powerful
 
1076
backtracking when returning tactic values. An interesting application
 
1077
is the problem of deciding intuitionistic propositional logic.
 
1078
Considering the contraction-free sequent calculi {\tt LJT*} of
 
1079
Roy~Dyckhoff (\cite{Dyc92}), it is quite natural to code such a tactic
 
1080
using the tactic language. On Figure~\ref{tautoltaca}, the tactic {\tt
 
1081
  Axioms} tries to conclude using usual axioms. The {\tt DSimplif}
 
1082
tactic applies all the reversible rules of Dyckhoff's system.
 
1083
Finally, on Figure~\ref{tautoltacb}, the {\tt TautoProp} tactic (the
 
1084
main tactic to be called) simplifies with {\tt DSimplif}, tries to
 
1085
conclude with {\tt Axioms} and tries several paths using the
 
1086
backtracking rules (one of the four Dyckhoff's rules for the left
 
1087
implication to get rid of the contraction and the right or).
 
1088
 
 
1089
\begin{figure}[tb]
 
1090
\begin{center}
 
1091
\fbox{\begin{minipage}{0.95\textwidth}
 
1092
\begin{coq_example*}
 
1093
Lemma tauto_ex1 : forall A B:Prop, A /\ B -> A \/ B.
 
1094
Proof.
 
1095
TautoProp.
 
1096
Qed.
 
1097
 
 
1098
Lemma tauto_ex2 :
 
1099
   forall A B:Prop, (~ ~ B -> B) -> (A -> B) -> ~ ~ A -> B.
 
1100
Proof.
 
1101
TautoProp.
 
1102
Qed.
 
1103
\end{coq_example*}
 
1104
\end{minipage}}
 
1105
\end{center}
 
1106
\caption{Proofs of tautologies with {\tt TautoProp}}
 
1107
\label{tautolem}
 
1108
\end{figure}
 
1109
 
 
1110
For example, with {\tt TautoProp}, we can prove tautologies like those of
 
1111
Figure~\ref{tautolem}.
 
1112
 
 
1113
 
 
1114
\subsection{Deciding type isomorphisms}
 
1115
 
 
1116
A more tricky problem is to decide equalities between types and modulo
 
1117
isomorphisms. Here, we choose to use the isomorphisms of the simply typed
 
1118
$\lb{}$-calculus with Cartesian product and $unit$ type (see, for example,
 
1119
\cite{RC95}). The axioms of this $\lb{}$-calculus are given by
 
1120
Figure~\ref{isosax}.
 
1121
 
 
1122
\begin{figure}
 
1123
\begin{center}
 
1124
\fbox{\begin{minipage}{0.95\textwidth}
 
1125
\begin{coq_eval}
 
1126
Reset Initial.
 
1127
\end{coq_eval}
 
1128
\begin{coq_example*}
 
1129
Open Scope type_scope.
 
1130
Section Iso_axioms.
 
1131
Variables A B C : Set.
 
1132
Axiom Com : A * B = B * A.
 
1133
Axiom Ass : A * (B * C) = A * B * C.
 
1134
Axiom Cur : (A * B -> C) = (A -> B -> C).
 
1135
Axiom Dis : (A -> B * C) = (A -> B) * (A -> C).
 
1136
Axiom P_unit : A * unit = A.
 
1137
Axiom AR_unit : (A -> unit) = unit.
 
1138
Axiom AL_unit : (unit -> A) = A.
 
1139
Lemma Cons : B = C -> A * B = A * C.
 
1140
Proof.
 
1141
intro Heq; rewrite Heq; apply refl_equal.
 
1142
Qed.
 
1143
End Iso_axioms.
 
1144
\end{coq_example*}
 
1145
\end{minipage}}
 
1146
\end{center}
 
1147
\caption{Type isomorphism axioms}
 
1148
\label{isosax}
 
1149
\end{figure}
 
1150
 
 
1151
The tactic to judge equalities modulo this axiomatization can be written as
 
1152
shown on Figures~\ref{isosltac1} and~\ref{isosltac2}. The algorithm is quite
 
1153
simple. Types are reduced using axioms that can be oriented (this done by {\tt
 
1154
MainSimplif}). The normal forms are sequences of Cartesian
 
1155
products without Cartesian product in the left component. These normal forms
 
1156
are then compared modulo permutation of the components (this is done by {\tt
 
1157
CompareStruct}). The main tactic to be called and realizing this algorithm is
 
1158
{\tt IsoProve}.
 
1159
 
 
1160
\begin{figure}
 
1161
\begin{center}
 
1162
\fbox{\begin{minipage}{0.95\textwidth}
 
1163
\begin{coq_example}
 
1164
Ltac DSimplif trm :=
 
1165
  match trm with
 
1166
  | (?A * ?B * ?C) =>
 
1167
      rewrite <- (Ass A B C); try MainSimplif
 
1168
  | (?A * ?B -> ?C) =>
 
1169
      rewrite (Cur A B C); try MainSimplif
 
1170
  | (?A -> ?B * ?C) =>
 
1171
      rewrite (Dis A B C); try MainSimplif
 
1172
  | (?A * unit) =>
 
1173
      rewrite (P_unit A); try MainSimplif
 
1174
  | (unit * ?B) =>
 
1175
      rewrite (Com unit B); try MainSimplif
 
1176
  | (?A -> unit) =>
 
1177
      rewrite (AR_unit A); try MainSimplif
 
1178
  | (unit -> ?B) =>
 
1179
      rewrite (AL_unit B); try MainSimplif
 
1180
  | (?A * ?B) =>
 
1181
      (DSimplif A; try MainSimplif) || (DSimplif B; try MainSimplif)
 
1182
  | (?A -> ?B) =>
 
1183
      (DSimplif A; try MainSimplif) || (DSimplif B; try MainSimplif)
 
1184
  end
 
1185
 with MainSimplif :=
 
1186
  match goal with
 
1187
  | |- (?A = ?B) => try DSimplif A; try DSimplif B
 
1188
  end.
 
1189
Ltac Length trm :=
 
1190
  match trm with
 
1191
  | (_ * ?B) => let succ := Length B in constr:(S succ)
 
1192
  | _ => constr:1
 
1193
  end.
 
1194
Ltac assoc := repeat rewrite <- Ass.
 
1195
\end{coq_example}
 
1196
\end{minipage}}
 
1197
\end{center}
 
1198
\caption{Type isomorphism tactic (1)}
 
1199
\label{isosltac1}
 
1200
\end{figure}
 
1201
 
 
1202
\begin{figure}
 
1203
\begin{center}
 
1204
\fbox{\begin{minipage}{0.95\textwidth}
 
1205
\begin{coq_example}
 
1206
Ltac DoCompare n :=
 
1207
  match goal with
 
1208
  | [ |- (?A = ?A) ] => apply refl_equal
 
1209
  | [ |- (?A * ?B = ?A * ?C) ] =>
 
1210
    apply Cons; let newn := Length B in DoCompare newn
 
1211
  | [ |- (?A * ?B = ?C) ] =>
 
1212
    match eval compute in n with
 
1213
    | 1 => fail
 
1214
    | _ =>
 
1215
      pattern (A * B) at 1; rewrite Com; assoc; DoCompare (pred n)
 
1216
    end
 
1217
  end.
 
1218
Ltac CompareStruct :=
 
1219
  match goal with
 
1220
  | [ |- (?A = ?B) ] =>
 
1221
      let l1 := Length A
 
1222
      with l2 := Length B in
 
1223
      match eval compute in (l1 = l2) with
 
1224
      | (?n = ?n) => DoCompare n
 
1225
      end
 
1226
  end.
 
1227
Ltac IsoProve := MainSimplif; CompareStruct.
 
1228
\end{coq_example}
 
1229
\end{minipage}}
 
1230
\end{center}
 
1231
\caption{Type isomorphism tactic (2)}
 
1232
\label{isosltac2}
 
1233
\end{figure}
 
1234
 
 
1235
Figure~\ref{isoslem} gives examples of what can be solved by {\tt IsoProve}.
 
1236
 
 
1237
\begin{figure}
 
1238
\begin{center}
 
1239
\fbox{\begin{minipage}{0.95\textwidth}
 
1240
\begin{coq_example*}
 
1241
Lemma isos_ex1 : 
 
1242
  forall A B:Set, A * unit * B = B * (unit * A).
 
1243
Proof.
 
1244
intros; IsoProve.
 
1245
Qed.
 
1246
 
 
1247
Lemma isos_ex2 :
 
1248
  forall A B C:Set,
 
1249
    (A * unit -> B * (C * unit)) =
 
1250
    (A * unit -> (C -> unit) * C) * (unit -> A -> B).
 
1251
Proof.
 
1252
intros; IsoProve.
 
1253
Qed.
 
1254
\end{coq_example*}
 
1255
\end{minipage}}
 
1256
\end{center}
 
1257
\caption{Type equalities solved by {\tt IsoProve}}
 
1258
\label{isoslem}
 
1259
\end{figure}
 
1260
 
 
1261
%%% Local Variables: 
 
1262
%%% mode: latex
 
1263
%%% TeX-master: "Reference-Manual"
 
1264
%%% End: