aboutsummaryrefslogtreecommitdiff
path: root/doc/RefMan-ltac.tex
blob: a252a1fd984cac742682fedc742475f45d72678a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
\chapter{The tactic language}
\label{TacticLanguage}

%\geometry{a4paper,body={5in,8in}}

This chapter gives a compact documentation of Ltac, the tactic
language available in {\Coq}. We start by giving the syntax, and next,
we present the informal semantics. If you want to know more regarding
this language and especially about its fundations, you can refer
to~\cite{Del00}. Chapter~\ref{Tactics-examples} is devoted to giving
examples of use of this language on small but also with non-trivial
problems.


\section{Syntax}

\def\tacexpr{\textrm{\textsl{expr}}}
\def\tacexprlow{\textrm{\textsl{tacexpr$_1$}}}
\def\tacexprinf{\textrm{\textsl{tacexpr$_2$}}}
\def\tacexprpref{\textrm{\textsl{tacexpr$_3$}}}
\def\atom{\textrm{\textsl{atom}}}
\def\recclause{\textrm{\textsl{rec\_clause}}}
\def\letclause{\textrm{\textsl{let\_clause}}}
\def\matchrule{\textrm{\textsl{match\_rule}}}
\def\contextrule{\textrm{\textsl{context\_rule}}}
\def\contexthyps{\textrm{\textsl{context\_hyps}}}
\def\tacarg{\nterm{tacarg}}
\def\cpattern{\nterm{cpattern}}

The syntax of the tactic language is given in tables~\ref{ltac}
and~\ref{ltac_aux}. See section~\ref{BNF-syntax} for a description of
the BNF metasyntax used in these tables. Various already defined
entries will be used in this chapter: entries {\naturalnumber},
{\integer}, {\ident}, {\qualid}, {\term}, {\cpattern} and {\atomictac}
represent respectively the natural and integer numbers, the authorized
identificators and qualified names, {\Coq}'s terms and patterns and
all the atomic tactics described in chapter~\ref{Tactics}. The syntax
of {\cpattern} is the same as that of terms, but there can be specific
variables like {\tt ?id} where {\tt id} is a {\ident} or {\tt \_},
which are metavariables for pattern matching. {\tt ?id} allows us to
keep instantiations and to make constraints whereas {\tt \_} shows
that we are not interested in what will be matched. On the right hand
side, they are used without the question mark.

The main entry of the grammar is {\tacexpr}. This language is used in
proof mode but it can also be used in toplevel definitions as shown in
table~\ref{ltactop}.
\begin{Remarks}
\item The infix tacticals ``\dots\ {\tt ||} \dots'' and ``\dots\ {\tt
    ;} \dots'' are associative. 

\item As shown by the figure, tactical {\tt ||} binds more than the
prefix tacticals {\tt try}, {\tt repeat}, {\tt do}, {\tt info} and
{\tt abstract} which themselves bind more than the postfix tactical
``{\tt \dots\ ;[ \dots\ ]}'' which binds more than ``\dots\ {\tt ;}
\dots''.

For instance
\begin{tabbing}
{\tt try repeat \tac$_1$ ||
  \tac$_2$;\tac$_3$;[\tac$_{31}$|\dots|\tac$_{3n}$];\tac$_4$.}
\end{tabbing}
is understood as 
\begin{tabbing}
{\tt (try (repeat (\tac$_1$ || \tac$_2$)));} \\
{\tt ((\tac$_3$;[\tac$_{31}$|\dots|\tac$_{3n}$]);\tac$_4$).}
\end{tabbing}
\end{Remarks}


\begin{table}[htbp]
\noindent{}\framebox[6in][l]
{\parbox{6in}
{\begin{center}
\begin{tabular}{lcl}
{\tacexpr} & \cn{}::= &
           {\tacexpr} {\tt ;} {\tacexpr}\\
& \cn{}| & {\tacexpr} {\tt ; [} \nelist{\tacexpr}{|} {\tt ]}\\
& \cn{}| & {\tacexprpref}\\
\\
{\tacexprpref} & \cn{}::= &
           {\tt do} {\it (}{\naturalnumber} {\it |} {\ident}{\it )} {\tacexprpref}\\
& \cn{}| & {\tt info} {\tacexprpref}\\
& \cn{}| & {\tt progress} {\tacexprpref}\\
& \cn{}| & {\tt repeat} {\tacexprpref}\\
& \cn{}| & {\tt try} {\tacexprpref}\\
& \cn{}| & {\tacexprinf} \\
\\
{\tacexprinf} & \cn{}::= &
           {\tacexprlow} {\tt ||} {\tacexprpref}\\
& \cn{}| & {\tacexprlow}\\
\\
{\tacexprlow} & \cn{}::= &
{\tt fun} \nelist{\name}{} {\tt =>} {\atom}\\
& \cn{}| &
{\tt let} \nelist{\letclause}{\tt with} {\tt in}
{\atom}\\
& \cn{}| &
{\tt let rec} \nelist{\recclause}{\tt with} {\tt in}
{\tacexpr}\\
& \cn{}| &
{\tt match goal with} \nelist{\contextrule}{\tt |} {\tt end}\\
& \cn{}| &
{\tt match reverse goal with} \nelist{\contextrule}{\tt |} {\tt end}\\
& \cn{}| &
{\tt match} {\tacexpr} {\tt with} \nelist{\matchrule}{\tt |} {\tt end}\\
& \cn{}| & {\tt abstract} {\atom}\\
& \cn{}| & {\tt abstract} {\atom} {\tt using} {\ident} \\
& \cn{}| & {\tt first [} \nelist{\tacexpr}{\tt |} {\tt ]}\\
& \cn{}| & {\tt solve [} \nelist{\tacexpr}{\tt |} {\tt ]}\\
& \cn{}| & {\tt idtac} ~|~ {\tt idtac} {\qstring}\\
& \cn{}| & {\tt fail} ~|~ {\tt fail} {\naturalnumber} {\qstring}\\
& \cn{}| & {\tt fresh} ~|~ {\tt fresh} {\qstring}\\
& \cn{}| & {\tt context} {\ident} {\tt [} {\term} {\tt ]}\\
& \cn{}| & {\tt eval} {\nterm{redexpr}} {\tt in} {\term}\\
& \cn{}| & {\tt type of} {\term}\\
& \cn{}| & {\tt constr :} {\term}\\
& \cn{}| & \atomictac\\
& \cn{}| & {\qualid} \nelist{\tacarg}{}\\
& \cn{}| & {\atom}\\
\\
{\atom} & \cn{}::= &
           {\qualid} \\
& \cn{}| & ()\\
& \cn{}| & {\tt (} {\tacexpr} {\tt )}\\
\end{tabular}
\end{center}}}
\caption{Syntax of the tactic language}
\label{ltac}
\end{table}



\begin{table}[htbp]
\noindent{}\framebox[6in][l]
{\parbox{6in}
{\begin{center}
\begin{tabular}{lcl}
\tacarg & ::= & 
        {\qualid}\\
& $|$ & {\tt ()} \\
& $|$ & {\tt ltac :} {\atom}\\
& $|$ & {\term}\\
\\
\letclause & ::= & {\ident} \sequence{\name}{} {\tt :=} {\tacexpr}\\
\\
\recclause & ::= & {\ident} \nelist{\name}{} {\tt :=} {\tacexpr}\\
\\
\contextrule & ::= &
  \nelist{\contexthyps}{\tt ,} {\tt |-}{\cpattern} {\tt =>} {\tacexpr}\\
& $|$ & {\tt |-} {\cpattern} {\tt =>} {\tacexpr}\\
& $|$ & {\tt \_ =>} {\tacexpr}\\
\\
\contexthyps & ::= & {\name} {\tt :} {\cpattern}\\
\\
\matchrule & ::= &
           {\cpattern} {\tt =>} {\tacexpr}\\
& $|$ & {\tt context} {\zeroone{\ident}} {\tt [} {\cpattern} {\tt ]} {\tt =>} {\tacexpr}\\
& $|$ & {\tt \_ =>} {\tacexpr}\\
\end{tabular}
\end{center}}}
\caption{Syntax of the tactic language (continued)}
\label{ltac_aux}
\end{table}

\begin{table}[ht]
\noindent{}\framebox[6in][l]
{\parbox{6in}
{\begin{center}
\begin{tabular}{lcl}
\nterm{top} & ::= & {\tt Ltac} \nelist{\nterm{ltac\_def}} {\tt with} \\
\\
\nterm{ltac\_def} & ::= & {\ident} \sequence{\ident}{} {\tt :=} {\tacexpr}
\end{tabular}
\end{center}}}
\caption{Tactic toplevel definitions}
\label{ltactop}
\end{table}


%%
%% Semantics
%%
\section{Semantics}
\index[tactic]{Tacticals}\index{Tacticals}\label{Tacticals}

Tactic expressions can only be applied in the context of a goal.  The
evaluation yields either a term, an integer or a tactic. Intermediary
results can be terms or integers but the final result must be a tactic
which is then applied to the current goal.

There is a special case for {\tt match goal} expressions of which
the clauses evaluate to tactics. Such expressions can only be used as
end result of a tactic expression (never as argument of a local
definition or of an application).

The rest of this section explains the semantics of every construction
of Ltac.


%% \subsection{Values}

%% Values are given by table~\ref{ltacval}. All these values are tactic values,
%% i.e. to be applied to a goal, except {\tt Fun}, {\tt Rec} and $arg$ values.

%% \begin{table}[ht]
%% \noindent{}\framebox[6in][l]
%% {\parbox{6in}
%% {\begin{center}
%% \begin{tabular}{lp{0.1in}l}
%% $vexpr$ & \cn{}::= & $vexpr$ {\tt ;} $vexpr$\\
%% & \cn{}| & $vexpr$ {\tt ; [} {\it (}$vexpr$ {\tt |}{\it )}$^*$ $vexpr$ {\tt
%% ]}\\
%% & \cn{}| & $vatom$\\
%% \\
%% $vatom$ & \cn{}::= & {\tt Fun} \nelist{\inputfun}{}  {\tt ->} {\tacexpr}\\
%% %& \cn{}| & {\tt Rec} \recclause\\
%% & \cn{}| &
%% {\tt Rec} \nelist{\recclause}{\tt And} {\tt In}
%% {\tacexpr}\\
%% & \cn{}| &
%% {\tt Match Context With} {\it (}$context\_rule$ {\tt |}{\it )}$^*$
%% $context\_rule$\\
%% & \cn{}| & {\tt (} $vexpr$ {\tt )}\\
%% & \cn{}| & $vatom$ {\tt Orelse} $vatom$\\
%% & \cn{}| & {\tt Do} {\it (}{\naturalnumber} {\it |} {\ident}{\it )} $vatom$\\
%% & \cn{}| & {\tt Repeat} $vatom$\\
%% & \cn{}| & {\tt Try} $vatom$\\
%% & \cn{}| & {\tt First [} {\it (}$vexpr$ {\tt |}{\it )}$^*$ $vexpr$ {\tt ]}\\
%% & \cn{}| & {\tt Solve [} {\it (}$vexpr$ {\tt |}{\it )}$^*$ $vexpr$ {\tt ]}\\
%% & \cn{}| & {\tt Idtac}\\
%% & \cn{}| & {\tt Fail}\\
%% & \cn{}| & {\primitivetactic}\\
%% & \cn{}| & $arg$
%% \end{tabular}
%% \end{center}}}
%% \caption{Values of ${\cal L}_{tac}$}
%% \label{ltacval}
%% \end{table}

%% \subsection{Evaluation}

\subsubsection{Sequence}
\tacindex{;}
\index{;@{\tt ;}}
\index{Tacticals!;@{\tt {\tac$_1$};\tac$_2$}}

A sequence is an expression of the following form:
\begin{center}
{\tacexpr}$_1$ {\tt ;} {\tacexpr}$_2$
\end{center}
{\tacexpr}$_1$ and {\tacexpr}$_2$ are evaluated to $v_1$ and
$v_2$. $v_1$ and $v_2$ must be tactic values. $v_1$ is then applied
and $v_2$ is applied to every subgoal generated by the application of
$v_1$. Sequence is left associating.

\subsubsection{General sequence}
\tacindex{; [ | ]}
\index{; [ | ]@{\tt ;[\ldots$\mid$\ldots$\mid$\ldots]}}
\index{Tacticals!; [ | ]@{\tt {\tac$_0$};[{\tac$_1$}$\mid$\ldots$\mid$\tac$_n$]}}

We can generalize the previous sequence operator by:
\begin{center}
{\tacexpr}$_0$ {\tt ; [} {\tacexpr}$_1$ {\tt |} $...$ {\tt |}
{\tacexpr}$_n$ {\tt ]}
\end{center}
{\tacexpr}$_i$ is evaluated to $v_i$, for $i=0,...,n$. $v_0$ is
applied and $v_i$ is applied to the $i$-th generated subgoal by the
application of $v_0$, for $=1,...,n$. It fails if the application of
$v_0$ does not generate exactly $n$ subgoals.

\subsubsection{For loop}
\tacindex{do}
\index{Tacticals!do@{\tt do}}

There is a for loop that repeats a tactic {\num} times:
\begin{center}
{\tt do} {\num} {\tacexpr}
\end{center}
{\tacexpr} is evaluated to $v$. $v$ must be a tactic value. $v$ is
applied {\num} times. Supposing ${\num}>1$, after the first
application of $v$, $v$ is applied, at least once, to the generated
subgoals and so on. It fails if the application of $v$ fails before
the {\num} applications have been completed.

\subsubsection{Repeat loop}
\tacindex{repeat}
\index{Tacticals!repeat@{\tt repeat}}

We have a repeat loop with:
\begin{center}
{\tt repeat} {\tacexpr}
\end{center}
{\tacexpr} is evaluated to $v$. $v$ must be a tactic value. $v$ is
applied until it fails. Supposing $n>1$, after the first application
of $v$, $v$ is applied, at least once, to the generated subgoals and
so on. It stops when it fails for all the generated subgoals. It never
fails.

\subsubsection{Error catching}
\tacindex{try}
\index{Tacticals!try@{\tt try}}

We can catch the tactic errors with:
\begin{center}
{\tt try} {\tacexpr}
\end{center}
{\tacexpr} is evaluated to $v$. $v$ must be a tactic value. $v$ is
applied. If the application of $v$ fails, it catches the error and
leaves the goal unchanged. If the level of the exception is positive,
then the exception is re-raised with its level decremented.

\subsubsection{Detecting progress}
\tacindex{progress}

We can check if a tactic made progress with:
\begin{center}
{\tt progress} {\tacexpr}
\end{center}
{\tacexpr} is evaluated to $v$. $v$ must be a tactic value. $v$ is
applied. If the application of $v$ produced one subgoal equal to the
initial goal (up to syntactical equality), then an error of level 0 is
raised. 

\ErrMsg \errindex{Failed to progress}


\subsubsection{Branching}
\tacindex{||}
\index{Tacticals!orelse@{\tt ||}}

We can easily branch with the following structure:
\begin{center}
{\tacexpr}$_1$ {\tt ||} {\tacexpr}$_2$
\end{center}
{\tacexpr}$_1$ and {\tacexpr}$_2$ are evaluated to $v_1$ and
$v_2$. $v_1$ and $v_2$ must be tactic values. $v_1$ is applied and if
it fails then $v_2$ is applied. Branching is left associating.

\subsubsection{First tactic to work}
\tacindex{first}
\index{Tacticals!first@{\tt first}}

We may consider the first tactic to work (i.e. which does not fail) among a
panel of tactics:
\begin{center}
{\tt first [} {\tacexpr}$_1$ {\tt |} $...$ {\tt |} {\tacexpr}$_n$ {\tt ]}
\end{center}
{\tacexpr}$_i$ are evaluated to $v_i$ and $v_i$ must be tactic values, for 
$i=1,...,n$. Supposing $n>1$, it applies $v_1$, if it works, it stops else it
tries to apply $v_2$ and so on. It fails when there is no applicable tactic.

\ErrMsg \errindex{No applicable tactic}

\subsubsection{Solving}
\tacindex{solve}
\index{Tacticals!solve@{\tt solve}}

We may consider the first to solve (i.e. which generates no subgoal) among a
panel of tactics:
\begin{center}
{\tt solve [} {\tacexpr}$_1$ {\tt |} $...$ {\tt |} {\tacexpr}$_n$ {\tt ]}
\end{center}
{\tacexpr}$_i$ are evaluated to $v_i$ and $v_i$ must be tactic values, for 
$i=1,...,n$. Supposing $n>1$, it applies $v_1$, if it solves, it stops else it
tries to apply $v_2$ and so on. It fails if there is no solving tactic.

\ErrMsg \errindex{Cannot solve the goal}

\subsubsection{Identity}
\tacindex{idtac}
\index{Tacticals!idtac@{\tt idtac}}

The constant {\tt idtac} is the identity tactic: it leaves any goal
unchanged but it appears in the proof script.
\begin{center}
{\tt idtac} and {\tt idtac "message"}
\end{center}
The latter variant prints the string on the standard output.

\subsubsection{Failing}
\tacindex{fail}
\index{Tacticals!fail@{\tt fail}}

The tactic {\tt fail} is the always-failing tactic: it does not solve
any goal. It is useful for defining other tacticals since it can be
catched by {\tt try} or {\tt match goal}. There are three variants:
\begin{center}
{\tt fail $n$}, {\tt fail "message"} and {\tt fail $n$ "message"}
\end{center}
The number $n$ is the failure level. If no level is specified, it
defaults to $0$.  The level is used by {\tt try} and {\tt match goal}.
If $0$, it makes {\tt match goal} considering the next clause
(backtracking). If non zero, the current {\tt match goal} block or
{\tt try} command is aborted and the level is decremented.

\ErrMsg \errindex{Tactic Failure "message" (level $n$)}.

\subsubsection{Local definitions}
\tacindex{let}
\tacindex{let rec}

Local definitions can be done as follows:

%\newpage{}

%\phantom{}
%\vfill{}

\begin{tabular}{l}
{\tt let} {\ident}$_1$ {\tt :=} {\tacexpr}$_1$\\
{\tt with} {\ident}$_2$ {\tt :=} {\tacexpr}$_2$\\
...\\
{\tt with} {\ident}$_n$ {\tt :=} {\tacexpr}$_n$ {\tt in}\\
{\tacexpr}
\end{tabular}

each {\tacexpr}$_i$ is evaluated to $v_i$, then, {\tacexpr} is
evaluated by substituting $v_i$ to each occurrence of {\ident}$_i$,
for $i=1,...,n$. There is no dependencies between the {\tacexpr}$_i$
and the {\ident}$_i$.

Local definitions can be recursive by using {\tt let rec} instead of
{\tt let}. Only functions can be defined by recursion, so at least one
argument is required.

\subsubsection{Application}

An application is an expression of the following form:
\begin{center}
{\qualid} {\tacarg}$_1$ ... {\tacarg}$_n$
\end{center}
The reference {\qualid} must be bound to some defined tactic
definition expecting at least $n$ arguments.  The expressions
{\tacexpr}$_i$ are evaluated to $v_i$, for $i=1,...,n$.
%If {\tacexpr} is a {\tt Fun} or {\tt Rec} value then the body is evaluated by
%substituting $v_i$ to the formal parameters, for $i=1,...,n$. For recursive
%clauses, the bodies are lazily substituted (when an identifier to be evaluated
%is the name of a recursive clause).

%\subsection{Application of tactic values}

\subsubsection{Function construction}
\tacindex{fun}

A parameterized tactic can be built anonymously (without resorting to
local definitions) with:
\begin{center}
{\tt fun} {\ident${}_1$} ... {\ident${}_n$} {\tt =>} {\tacexpr}
\end{center}
Indeed, local definitions of functions are a syntactic sugar for
binding a {\tt fun} tactic to an identifier.

\subsubsection{Pattern matching on terms}
\tacindex{match}

We can carry out pattern matching on terms with:

\begin{tabular}{l}
{\tt match} {\tacexpr} {\tt with}\\
~~~{\cpattern}$_1$ {\tt =>} {\tacexpr}$_1$\\
~{\tt |} {\cpattern}$_2$ {\tt =>} {\tacexpr}$_2$\\
~...\\
~{\tt |} {\cpattern}$_n$ {\tt =>} {\tacexpr}$_n$\\
~{\tt |} {\tt \_} {\tt =>} {\tacexpr}$_{n+1}$\\
{\tt end}
\end{tabular}

The {\tacexpr} is evaluated and should yield a term which is matched
(non-linear first order unification) against {\cpattern}$_1$ then
{\tacexpr}$_1$ is evaluated into some value by substituting the
pattern matching instantiations to the metavariables. If the matching
with {\cpattern}$_1$ fails, {\cpattern}$_2$ is used and so on.  The
pattern {\_} matches any term and shunts all remaining patterns if
any. If {\tacexpr}$_1$ evaluates to a tactic, this tactic is not
immediately applied to the current goal (in contrast with {\tt match
goal}). If all clauses fail (in particular, there is no pattern {\_})
then a no-matching error is raised.

\begin{ErrMsgs}
\item \errindex{No matching clauses for match}\\
No pattern can be used and, in particular, there is no {\tt \_} pattern.
\item \errindex{Argument of match does not evaluate to a term}\\
This happens when {\tacexpr} does not denote a term.
\end{ErrMsgs}

\tacindex{context (in pattern)}
There is a special form of patterns to match a subterm against the
pattern:
\begin{center}
{\tt context} {\ident} {\tt [} {\cpattern} {\tt ]}
\end{center}
It matches any term which one subterm matches {\cpattern}. If there is
a match, the optional {\ident} is assign the ``matched context'', that
is the initial term where the matched subterm is replaced by a
hole. The definition of {\tt context} in expressions below will show
how to use such term contexts.

This operator never makes backtracking. If there are several subterms
matching the pattern, only the first match is considered. Note that
the order of matching is left unspecified.
%% TODO: clarify this point! It *should* be specified


\subsubsection{Pattern matching on goals}
\tacindex{match goal}
\tacindex{match reverse goal}

We can make pattern matching on goals using the following expression:

\begin{tabular}{l}
{\tt match goal with}\\
~~~$hyp_{1,1}${\tt ,}...{\tt ,}$hyp_{1,m_1}$
   ~~{\tt |-}{\cpattern}$_1${\tt =>} {\tacexpr}$_1$\\
~~{\tt |}$hyps_{2,1}${\tt ,}...{\tt ,}$hyp_{2,m_2}$
   ~~{\tt |-}{\cpattern}$_2${\tt =>} {\tacexpr}$_2$\\
~~...\\
~~{\tt |}$hyp_{n,1}${\tt ,}...{\tt ,}$hyp_{n,m_n}$
   ~~{\tt |-}{\cpattern}$_n${\tt =>} {\tacexpr}$_n$\\
~~{\tt |\_}~~~~{\tt =>} {\tacexpr}$_{n+1}$\\
{\tt end}
\end{tabular}

% TODO: specify order of hypothesis and explain reverse...

If each hypothesis pattern $hyp_{1,i}$, with $i=1,...,m_1$
is matched (non-linear first order unification) by an hypothesis of
the goal and if {\cpattern}$_1$ is matched by the conclusion of the
goal, then {\tacexpr}$_1$ is evaluated to $v_1$ by substituting the
pattern matching to the metavariables and the real hypothesis names
bound to the possible hypothesis names occurring in the hypothesis
patterns. If $v_1$ is a tactic value, then it is applied to the
goal. If this application fails, then another combination of
hypotheses is tried with the same proof context pattern. If there is
no other combination of hypotheses then the second proof context
pattern is tried and so on. If the next to last proof context pattern
fails then {\tacexpr}$_{n+1}$ is evaluated to $v_{n+1}$ and $v_{n+1}$
is applied.

\ErrMsg \errindex{No matching clauses for match goal}

No goal pattern can be used and, in particular, there is no {\tt
\_} goal pattern.
\medskip

It is important to know that each hypothesis of the goal can be
matched by at most one hypothesis pattern. The order of matching is
the following: hypothesis patterns are examined from the right to the
left (i.e. $hyp_{i,m_i}$ before $hyp_{i,1}$). For each hypothesis
pattern, the goal hypothesis are matched in order (fresher hypothesis
first), but it possible to reverse this order (older first) with
the {\tt match reverse goal with} variant.

\subsubsection{Filling a term context}
\tacindex{context (in expressions)}

The following expression is not a tactic in the sense that it does not
produce subgoals but generates a term to be used in tactic
expressions:
\begin{center}
{\tt context} {\ident} {\tt [} {\tacexpr} {\tt ]}
\end{center}
{\ident} must denote a context variable bound by a {\tt context}
pattern of a {\tt match} expression. This expression evaluates
replaces the hole of the value of {\ident} by the value of
{\tacexpr}.

\ErrMsg \errindex{not a context variable}

\subsubsection{Generating fresh hypothesis names}
\tacindex{fresh}

Tactics sometimes have to generate new names for hypothesis. Letting
the system decide a name with the {\tt intro} tactic is not so good
since it is very awkward to retrieve the name the system gave.

As before, the following expression returns a term:
\begin{center}
{\tt fresh} {\qstring}
\end{center}
It evaluates to an identifier unbound in the goal, which is obtained
by padding {\qstring} with a number if necessary. If no name is given,
the prefix is {\tt H}.

\subsubsection{{\tt type of} {\term}}
\tacindex{type of}

This tactic computes the type of {\term}.


\subsubsection{Computing in a constr}
\tacindex{eval}

Evaluation of a term can be performed with:
\begin{center}
{\tt eval} {\nterm{redexpr}} {\tt in} {\term}
\end{center}
where \nterm{redexpr} is a reduction tactic among {\tt red}, {\tt
hnf}, {\tt compute}, {\tt simpl}, {\tt cbv}, {\tt lazy}, {\tt unfold},
{\tt fold}, {\tt pattern}.


\subsubsection{Accessing tactic decomposition}
\tacindex{info}
\index{Tacticals!info@{\tt info}}

Tactical ``{\tt info} {\tacexpr}'' is not really a tactical. For
elementary tactics, this is equivalent to \tacexpr. For complex tactic
like \texttt{auto}, it displays the operations performed by the
tactic.


\subsubsection{Proving a subgoal as a separate lemma}
\tacindex{abstract}
\index{Tacticals!abstract@{\tt abstract}}
From the outside ``\texttt{abstract \tacexpr}'' is the same as
{\tt solve \tacexpr}. Internally it saves an auxiliary lemma called 
{\ident}\texttt{\_subproof}\textit{n} where {\ident} is the name of the
current goal and \textit{n} is chosen so that this is a fresh name.

This tactical is useful with tactics such as \texttt{omega} or
\texttt{discriminate} that generate huge proof terms. With that tool
the user can avoid the explosion at time of the \texttt{Save} command
without having to cut manually the proof in smaller lemmas.

\begin{Variants}
\item \texttt{abstract {\tacexpr} using {\ident}}.\\
  Give explicitly the name of the auxiliary lemma.
\end{Variants}

\ErrMsg \errindex{Proof is not complete}

\subsection{Tactic toplevel definitions}
\comindex{Ltac}

Basically, tactics toplevel definitions are made as follows:
%{\tt Tactic Definition} {\ident} {\tt :=} {\tacexpr}\\
%
%{\tacexpr} is evaluated to $v$ and $v$ is associated to {\ident}. Next, every
%script is evaluated by substituting $v$ to {\ident}.
%
%We can define functional definitions by:\\
\begin{center}
{\tt Ltac} {\ident} {\ident}$_1$ ... {\ident}$_n$ {\tt :=}
{\tacexpr}
\end{center}
This defines a new tactic that can be used in any tactic script or new
tactic toplevel definition.

\Rem The preceding definition can equivalently be written:
\begin{center}
{\tt Ltac} {\ident} {\tt := fun} {\ident}$_1$ ... {\ident}$_n$
{\tt =>} {\tacexpr}
\end{center}
Recursive and mutual recursive function definitions are also
possible with the syntax:
\begin{center}
\begin{tabular}{l}
{\tt Ltac} {\ident}$_1$ {\ident}$_{1,1}$ ...
{\ident}$_{1,m_1}$~~{\tt :=} {\tacexpr}$_1$\\
{\tt with} {\ident}$_2$ {\ident}$_{2,1}$ ... {\ident}$_{2,m_2}$~~{\tt :=}
{\tacexpr}$_2$\\
...\\
{\tt with} {\ident}$_n$ {\ident}$_{n,1}$ ... {\ident}$_{n,m_n}$~~{\tt :=}
{\tacexpr}$_n$
\end{tabular}
\end{center}

%This definition bloc is a set of definitions (use of
%the same previous syntactical sugar) and the other scripts are evaluated as
%usual except that the substitutions are lazily carried out (when an identifier
%to be evaluated is the name of a recursive definition).