]> matita.cs.unibo.it Git - helm.git/blob - helm/papers/calculemus-2003/hbugs-calculemus-2003.tex
First part of the chapter about tutors. The automatic generation part is
[helm.git] / helm / papers / calculemus-2003 / hbugs-calculemus-2003.tex
1 \documentclass[runningheads]{llncs}
2 \pagestyle{headings}
3 \setcounter{page}{1}
4 \usepackage{graphicx}
5 \usepackage{amsfonts}
6
7 % \myincludegraphics{filename}{place}{width}{caption}{label}
8 \newcommand{\myincludegraphics}[5]{
9    \begin{figure}[#2]
10    \begin{center}
11    \includegraphics[width=#3]{eps/#1.eps}
12    \caption[#4]{#5}
13    \label{#1}
14    \end{center}
15    \end{figure}
16 }
17
18 \usepackage[show]{ed}
19 \usepackage{draftstamp}
20
21 \newcommand{\ws}{Web-Service}
22 \newcommand{\wss}{Web-Services}
23 \newcommand{\hbugs}{H-Bugs}
24 \newcommand{\helm}{HELM}
25 \newcommand{\Omegapp}{$\Omega$mega}
26 \newcommand{\OmegaAnts}{$\Omega$mega-Ants}
27
28 \title{Brokers and Web-Services for Automatic Deduction: a Case Study}
29
30 \author{Claudio Sacerdoti Coen \and Stefano Zacchiroli}
31
32 \institute{
33   Department of Computer Science\\
34   University of Bologna\\
35   Via di Mura Anteo Zamboni 7, 40127 Bologna, ITALY\\
36   \email{sacerdot@cs.unibo.it}
37   \and
38   Department of Computer Science\\
39   \'Ecole Normale Sup\'erieure\\
40   45, Rue d'Ulm, F-75230 Paris Cedex 05, FRANCE\\
41   \email{zack@cs.unibo.it}
42 }
43
44 \date{ }
45
46 \begin{document}
47 \sloppy
48 \maketitle
49
50 \begin{abstract}
51   We present a planning broker and several Web-Services for automatic deduction.
52   Each Web-Service implements one of the tactics usually available in an
53   interactive proof-assistant. When the broker is submitted a "proof status" (an
54   unfinished proof tree and a focus on an open goal) it dispatches the proof to
55   the Web-Services, collects the successfull results, and send them back to the
56   client as "hints" as soon as they are available.
57   
58   In our experience this architecture turns out to be helpful both for
59   experienced users (who can take benefit of distributing heavy computations)
60   and beginners (who can learn from it).
61 \end{abstract}
62
63 \section{Introduction}
64   The \ws{} approach at software development seems to be a working solution for
65   getting rid of a wide range of incompatibilities between communicating
66   software applications. W3C's efforts in standardizing related technologies
67   grant longevity and implementations availability for frameworks based on \wss{}\
68   for information exchange. As a direct conseguence, the number of such
69   frameworks is increasing and the World Wide Web is moving from a disorganized
70   repository of human-understandable HTML documents to a disorganized repository
71   of applications working on machine-understandable XML documents both for input
72   and output.
73   
74   The big challenge for the next future is to provide stable and reliable
75   services over this disorganized, unreliable and ever-evolving architecture.
76   The standard solution \ednote{zack: buhm! :-P} is providing a further level of
77   stable services (called \emph{brokers}) that behave as common gateway/address
78   for client applications to access a wide variety of services and abstract over
79    them.
80
81   Since the \emph{Declaration of Linz}, the MONET Consortium \cite{MONET},
82   following the guidelines \ednote{guidelines non e' molto appropriato, dato che
83   il concetto di broker non e' definito da W3C e co} of the \wss{}/brokers
84   approach, is working on the development of a framework aimed at providing a
85   set of software tools for the advertisement and discovering of mathematical
86   web services.
87   %CSC This framework turns out to be strongly based on both \wss{} and brokers.
88
89   Several groups have already developed \wss{} providing both computational and
90   reasoning capabilities \cite{???,???,???}\ednote{trovare dei puntatori carini
91   dalle conferenze calculemus}: the first ones are implemented on top of
92   Computer Algebra Systems; the last ones provide interfaces to well-known
93   theorem provers. Proof-planners, proof-assistants, CAS themselves and
94   domain-specific problem solvers are natural candidates to be client of these
95   services.  Nevertheless, so far the number of examples in the literature has
96   been extremely low and the concrete benefits are still to be assessed.
97
98   In this paper we present an architecture, namely \hbugs{}, implementing a
99   \emph{suggestion engine} for the proof assistant developed on behalf of the
100   \helm{} project. We provide several \wss{} (called \emph{tutors}) able to
101   suggest possible ways to proceed in a proof. The tutors are orchestrated
102   by a broker (a \ws{} itself) that is able to distribute a proof
103   status from a client (the proof-assistant) to the tutors;
104   each tutor try to make progress in the proof and, in case
105   of success, notify the client that shows an \emph{hint} to the user.
106   Both the broker and the tutors are instances of the homonymous entities of
107   the MONET framework.
108
109   A precursor of \hbugs{} is the \OmegaAnts{} project \cite{???},
110   which provided similar functionalities to the
111   \Omegapp{} proof-planner \cite{Omega}. The main architectural difference
112   between \hbugs{} and \OmegaAnts{} is that the latter is based on a
113   black-board architecture and it is not implemented using \wss{} and
114   brokers. Other differences will be detailed in Sect. \ref{conclusions}.
115
116   In Sect. \ref{architecture} we present the architecture of \hbugs{}.
117   Further implementation details are given in Sect. \ref{implementation}.
118   Sect. \ref{tutors} is an overview of the tutors that have been implemented.
119   As usual, the paper ends with the conclusions and future works.
120   
121 \oldpart
122 {CSC:  Non so se/dove mettere queste parti.
123  Zack: per ora facciamo senza e vediamo se/quanto spazio abbiamo, la prima parte
124        non e' molto utile, ma la seconda sugli usi tipici di proof assistant
125        come ws client si}
126 {
127   Despite of that the proof assistant case seems to be well suited to
128   investigate the usage of many different mathematical \wss{}. Indeed: most proof
129   assistants are still based on non-client/server architectures, are
130   application-centric instead of document-centric, offer a scarce level of
131   automation leaving entirely to the user the choice of which macro (usually
132   called \emph{tactic}) to use in order to make progress in a proof.
133
134   The average proof assistant can be, for example, a client of a \ws{}\
135   interfacing a specific or generic purpose theorem prover, or a client of a
136   \ws{} interfacing a CAS to simplify expressions in a particular mathematical
137   domain.
138 }
139
140 \section{Architecture}
141 \label{architecture}
142   \myincludegraphics{arch}{t}{8cm}{\hbugs{} architecture}{\hbugs{} architecture}
143
144   The \hbugs{} architecture (depicted in Fig. \ref{arch}) is based on three
145   different kinds of actors: \emph{clients}, \emph{brokers}, and \emph{tutors}.
146   Each actor present one or more \ws{} interface to its neighbours \hbugs{}
147   actors.
148
149   In this section we will detail the role and requiremente of each kind of
150   actors and discuss about the correspondencies between them and the MONET
151   entities described in \cite{MONET-Overview}.
152
153   \paragraph{Clients}
154     An \hbugs{} client is a software component able to produce \emph{proof
155     status} and to consume \emph{hints}.
156
157     \ednote{"status" ha il plurale?}
158     A proof status is a representation of an incomplete proof and is supposed to
159     be informative enough to be used by an interactive proof assistant. No
160     additional requirement exists on the proof status, but there should be an
161     agreement on its format between clients and tutors. An hint is a
162     representation of a step that can be performed in order to proceed in an
163     incomplete proof. Usually it represents a tactic available on some proof
164     assistant along with instantiation of its parameters for tactics which
165     require them.
166
167     Clients act both as \ws{} provider and requester (using W3C's terminology
168     \cite{ws-glossary}). They act as providers for the broker (to receive hints)
169     ans as requesters again for the broker (to submit new status). Clients
170     additionally use broker service to know which tutors are available and to
171     subscribe to one or more of them.
172
173     Usually, when the role of client is taken by an interactive proof assistant,
174     new status are sent to the broker as soon as the proof change (e.g. when the
175     user applies a tactic or when a new proof is started) and hints are shown to
176     the user be the means of some effect in the user interface (e.g. popping a
177     dialog box or enlightening a tactic button).
178
179     \hbugs{} clients act as MONET clients and ask broker to provide access to a
180     set of services (the tutors). \hbugs{} has no actors corresponding to MONET's
181     Broker Locating Service (since the client is supposed to know the URI of at
182     least one broker) and Mathematical Object Manager (since proof status are
183     built on the fly and not stored).
184
185   \paragraph{Brokers}
186     \hbugs{} brokers are the key actors of the \hbugs{} architecture since they
187     act as intermediaries between clients and tutors. They act both as \ws{}
188     provider and requester \emph{both} for clients and tutors.
189
190     With respect to client, a broker act as \ws{} provider receiving status and
191     forwarding them to one or more tutors. They act as \ws{} requester sending
192     hints to client as soon as they are available from tutors.
193
194     With respect to tutors, the \ws{} provider role is accomplished by receiving
195     hints as soon as they are produced; the \ws{} requester one is accomplished
196     by requesting computations (\emph{musings} in \hbugs{} terminology) on status
197     received by clients and stopping out of date ongoing musings.
198
199     Additionally brokers keep track of available tutors and subscription of each
200     client.
201
202     \hbugs{} brokers act as MONET brokers implementing the following components:
203     Client Manager, Service Registry Manager (keeping track of available
204     tutors), Planning Manager (chosing the available tutors among the ones to
205     which the client is subscribed), Execution Manager. \ednote{non e' chiaro se
206     in monet le risposte siano sincrone o meno, se cosi' fosse dobbiamo
207     specificare che nel nostro caso non lo sono}
208
209   \paragraph{Tutors}
210     Tutors are software component able to consume proof status producing hints.
211     \hbugs{} doesn't specify by which means hints should be produced, tutors can
212     use any means necessary (heuristics, external theorem prover or CAS, ...).
213     The only requirement is that exists an agreement on the formats of proof
214     status and hints.
215
216     tutors act both as \ws{} providers and requesters for broker. As providers,
217     they wait for commands requesting to start a new musing on a given proof
218     status or to stop an old, out of date, musing. As requesters, they signal to
219     the broker the end of a musing along with its outcome (an hint in case of
220     success or a notification of failure).
221
222     \hbugs{} tutors act as MONET services.
223
224 \section{???}
225 \label{implementation}
226
227 \section{The \hbugs Tutors}
228 \label{tutors}
229 To test the \hbugs architecture and to assess the utility of a suggestion
230 engine for the end user, we have implemented several tutors. In particular,
231 we have investigated three classes of tutors:
232 \begin{enumerate}
233  \item \emph{Tutors for beginners}. These are tutors that implement tactics
234    which are neither computationally expensive nor difficult to understand:
235    an expert user can always understand if the tactic can be applied or not
236    withouth having to try it. For example, the following implemented tutors
237    belong to this class:
238     \begin{itemize}
239      \item \emph{Assumption Tutor}: it ends the proof if the thesis is
240        equivalent\footnote{The equivalence relation is convertibility. In some
241        cases, expecially when non-trivial computations are involved, the user
242        is totally unable to figure out the convertibility of two terms.
243        In these cases the tutor becomes handy also for expert users.}
244        to one of the hypotheses.
245      \item \emph{Contradiction Tutor}: it ends the proof by \emph{reductio ad
246        adsurdum} if one hypothesis is equivalent to $False$.
247      \item \emph{Symmetry Tutor}: if the goal thesis is an equality, it
248        suggests to apply the commutative property.
249      \item \emph{Left/Right/Exists/Split/Constructor Tutors}: the Constructor
250        Tutor suggests to proceed in the proof by applying one or more
251        constructors when the goal thesis is an inductive type or a
252        proposition inductively defined according to the declarative style.
253        Since disjunction, conjunction and existential quantifications are
254        particular cases of inductive propositions,
255        all the other tutors of this class implements restrictions of the
256        the Constructor tactic: Left and Right suggest to prove a disjunction
257        by proving its left/right member; Split reduces the proof of a
258        conjunction to the two proof of its members; Exists suggests to
259        prove an existential quantification by providing a
260        witness\footnote{This task is left to the user.}.
261     \end{itemize}
262   Beginners, when first faced with a tactic-based proof-assistant, get
263   lost quite soon since the set of tactics is large and their names and
264   semantics must be remembered by heart. Tutorials are provided to guide
265   the user step-by-step in a few proofs, suggesting the tactics that must
266   be used. We believe that our beginners tutors can provide an auxiliary
267   learning tool: after the tutorial, the user is not suddendly left alone
268   with the system, but she can experiment with variations of the proof given
269   in the tutorial as much as she like, still getting useful suggestions.
270   Thus the user is allowed to focus on learning how to do a formal proof
271   instead of wasting efforts trying to remember the interface to the system.
272  \item{Tutors for Computationally Expensive Tactics}. Several tactics have
273   an unpredictable behaviour, in the sense that it is unfeasible to understand
274   wether they will succeed or they will failt when applied and what will be
275   their result. Among them, there are several tactics either computationally
276   expensive or resources consuming. In the first case, the user is not
277   willing to try a tactic and wait for a long time just to understand its
278   outcome: she would prefer to keep on concentrating on the proof and
279   have the tactic applied in background and receive out-of-band notification
280   of its success. The second case is similar, but the tactic application must
281   be performed on a remote machine to avoid overloading the user host
282   with several concurrent resource consuming applications.
283
284   Finally, several complex tactics and in particular all the tactics based
285   on reflexive techniques depend on a pretty large set of definitions, lemmas
286   and theorems. When these tactics are applied, the system needs to retrieve
287   and load all the lemmas. Pre-loading all the material needed by every
288   tactic can quickly lead to long initialization times and to large memory
289   footstamps. A specialized tutor running on a remote machine, instead,
290   can easily pre-load the required theorems.
291
292   As an example of computationally expensive task, we have implemented
293   a tutor for the \emph{Ring} tactic. The tutor is able to prove an equality
294   over a ring by reducing both members to a common normal form. The reduction,
295   which may require some time in complex cases,
296   is based on the usual commutative, associative and neutral element properties
297   of a ring. The tactic is implemented using a reflexive technique, which
298   means that the reduction trace is not stored in the proof-object itself:
299   the type-checker is able to perform the reduction on-the-fly thanks to
300   the conversion rules of the system. As a consequence, in the library there
301   must be stored both the algorithm used for the reduction and the proof of
302   correctness of the algorithm, based on the ring axioms. This big proof
303   and all of its lemmas must be retrieved and loaded in order to apply the
304   tactic. The Ring tutor loads and cache all the required theorems the
305   first time it is conctacted.
306  \item{Intelligent Tutors}. Expert users can already benefit from the previous
307   class of tutors. Nevertheless, to achieve a significative production gain,
308   they need more intelligent tutors implementing domain-specific theorem
309   provers or able to perform complex computations. These tutors are not just
310   plain implementations of tactics or decision procedures, but can be
311   more complex software agents interacting with third-parties software,
312   such as proof-planners, CAS or theorem-provers.
313
314   To test the productivity impact of intelligent tutors, we have implemented
315   a tutor that is interfaced with the HELM
316   Proof-Engine\footnote{\url{http://mowgli.cs.unibo.it/library.html}} and that
317   is able to look for every theorem in the distributed library that can
318   be applied to proceed in the proof. Even if the tutor deductive power
319   is extremely limited\footnote{We do not attempt to check if the new goals
320   obtained applying a lemma can be authomatically proved or, even better,
321   auhomatically disproved to reject the lemma.}, it is not unusual for
322   the tutor to come up with precious hints that can save several minutes of
323   works that would be spent in proving again already proven results or
324   figuring out where the lemmas could have been stored in the library.
325 \end{enumerate}
326
327 Once the code that implements a tactic or decision procedure is available,
328 transforming it into a tutor is not a difficult task, but it is surely
329 repetitive and error prone.
330
331 \section{???}
332 \label{conclusions}
333
334 % \section{Introduction}
335 % Since the development of the first proof-assistants based on the
336 % Curry-Howard isomorphism, it became clear that directly writing
337 % lambda-terms (henceforth called simply terms) was a difficult, repetitive,
338 % time-expensive and error prone activity; hence the introduction of
339 % meta-languages to describe procedures that are able to automatically
340 % generate the low-level terms.
341 % Nowadays, almost all the proof-assistants
342 % using terms description of proofs have many levels of abstractions, i.e.
343 % meta-languages, to create the terms, with the only remarkable exception
344 % of the ALF family \cite{ALF} in which terms are still directly written
345 % without any over-standing level of abstraction.
346
347 % In particular, there are
348 % usually at least two levels, that of tactics and that of the language in
349 % which the whole system and hence the tactics are written; once the tactics
350 % are implemented, to do a proof the user enters to the system a sequence of
351 % tactics, called a script; the script is then executed to create the term
352 % encoding of the proof, which is type-checked by the kernel of the
353 % proof-assistant. Writing a script interactively is much simpler than
354 % writing the term by hands; once written, though, it becomes impossible
355 % to understand a script without replaying it interactively. For this reason,
356 % we can hardly speak of the language of tactics as a high level language,
357 % even if it is ``compiled'' to the language of terms.
358
359 % To avoid confusion, in the rest of this paper we will
360 % avoid the use of the term ``proof'', using only ``script'' and ``term'';
361 % moreover, we will avoid also the terms ``proof-checking'' and ``type-checking'',
362 % replacing them with ``retyping'' to mean the type-checking of an already
363 % generated term; finally, we will use the term ``compiling'' to mean the
364 % execution of a script. Usually, compilation ends with
365 % the type-checking of the generated term; hence the choice of ``retyping'' for
366 % the type-checking of an already generated term.
367
368 % A long term consequence of the introduction of tactics has been the
369 % progressive lowering of interest in terms. In particular, users of
370 % modern proof-assistants as Coq \cite{Coq} may even ignore the existence of
371 % commands to show the terms generated by their scripts. These terms are
372 % usually very huge and quite unreadable, so they don't add any easily
373 % accessible information to the scripts. Hence implementors have loosed
374 % interest in terms too, as far as their size and compilation time are
375 % small enough to make the system response-time acceptable.
376 % When this is not the case, it is sometimes possible to trade space
377 % with time using reflexive tactics, in which the potentiality of complex
378 % type-systems to speak about themselves are exploited: reflexive tactics
379 % usually leads to a polynomial asympotical reduction in the size of the
380 % terms, at the cost of an increased reduction time during retyping and
381 % compilation. Often, though, reflexive tactics could not be used; moreover,
382 % reflexive tactics suffer of the same implementative problems of other tactics
383 % and so could be implemented in such a way to create huger than
384 % needed terms, even if asymptotically smaller than the best ones created
385 % without reflection.
386
387 % The low level of interest in terms of the implementors of
388 % tactics often leads to naive implementations (``If it works, it is OK'') and
389 % this to low-quality terms, which:
390 % \begin{enumerate}
391 %  \item are huger than needed because particular cases are not taken
392 %   into account during tactic development
393 %  \item require more time than needed for retyping due to
394 %   their size
395 %  \item are particularly unreadable because they don't
396 %   correspond to the ``natural'' way of writing the proof by hand
397 % \end{enumerate}
398 % To cope with the second problem, retyping is usually avoided allowing
399 % systems to reload saved terms without retyping them and using a
400 % checksum to ensure that the saved file has not been modified. This is
401 % perfectly reasonable accordingly to the traditional application-centric
402 % architecture of proof-assistants in which you have only one tool integrating
403 % all the functionalities and so you are free to use a proprietary format for data
404 % representation.
405
406 % In the last months, though, an ever increasing number of people and projects
407 % (see, for example, HELM \cite{EHELM}, MathWeb \cite{MATHWEB} and
408 % Formavie \cite{FORMAVIE})
409 % have been interested to switch from the application-centric model to the
410 % newer content-centric one, in which information is stored in
411 % standard formats (that is, XML based) to allow different applications to work
412 % on the same set of data. As a consequence, term size really becomes an
413 % important issue, due to the redundancy of standard formats, and retyping
414 % is needed because the applications can not trust each other, hence needing
415 % retyping and making retyping time critical.
416 % Moreover, as showed by Yann Coscoy in its PhD.
417 % thesis \cite{YANNTHESIS} or by the Alfa interface to the Agda system
418 % \cite{ALFA}, it is perfectly reasonable and also feasible to try
419 % to produce descriptions in natural languages of formal proofs encoded as
420 % terms.
421 % This approach, combined with the further possibility of applying the usual
422 % two-dimensional mathematic notation to the formulas that appears in the
423 % terms, is being followed by projects HELM \cite{HELM}, PCOQ \cite{PCOQ} and
424 % MathWeb \cite{MATHWEB} with promising results. It must be understood, though,
425 % that the quality (in terms of naturality and readability) of this kind of
426 % proofs rendering heavily depends on the quality of terms, making also
427 % the third characteristic of low-quality terms a critical issue.
428
429 % A totally different scenario in which term size and retyping time are
430 % critical is the one introduced by Necula and Lee \cite{Necula} under
431 % the name Proof Carrying Code (PCC). PCC is a technique that can be used
432 % for safe execution of untrusted code. In a typical instance of PCC, a code
433 % receiver establishes a set of safety rules that guarantee safe behavior
434 % of programs, and the code producer creates a formal safety proof that
435 % proves, for the untrusted code, adherence to the safety rules. Then, the
436 % proof is transmitted to the receiver together with the code and it is
437 % retyped before code execution. While very compact representation of the
438 % terms, highly based on type-inference and unification, could be used
439 % to reduce the size and retyping time \cite{Necula2}, designing proof-assistants
440 % to produce terms characterized by an high level of quality is still necessary.
441
442 % In the next section we introduce a particular class of metrics for
443 % tactics evaluation. In section \ref{equivalence} we consider the
444 % notion of tactics equivalence and we describe one of the bad habits
445 % of tactics implementors, which is overkilling; we also provide and analyze
446 % a simple example of overkilling tactic. In the last section we describe
447 % a concrete experience of fixing overkilling in the implementation of a
448 % reflexive tactic in system Coq and we analyze the gain with respect to
449 % term-size, retyping time and term readability.
450
451 % \section{From Metrics for Terms Evaluation to Metrics for Tactics Evaluation}
452 % The aim of this section is to show how metrics for term evaluation could
453 % induce metrics for tactic evaluation. Roughly speaking, this allows us to
454 % valuate tactics in terms of the quality of the terms produced. Even if
455 % we think that these kinds of metrics are interesting and worth studying,
456 % it must be understood that many other valuable forms of metrics could be
457 % defined on tactics, depending on what we are interested in. For example,
458 % we could be interested on compilation time, that is the sum of the time
459 % required to generate the term and the retyping time for it. Clearly, only
460 % the second component could be measured with a term metric and a good
461 % implementation of a tactic with respect to the metric considered could
462 % effectively decide to sacrifice term quality (and hence retyping time) to
463 % minimize the time spent to generate the term. The situation is very close to
464 % the one already encountered in compilers implementation, where there is
465 % always a compromise, usually user configurable, between minimizing compiling
466 % time and maximizing code quality.
467
468 % The section is organized as follows: first we recall the definition of
469 % tactic and we introduce metrics on terms; then we give the definition
470 % of some metrics induced by term metrics on tactics.
471
472 % \begin{definition}[Of tactic]
473 % We define a \emph{tactic} as a function that, given a goal $G$ (that is, a local
474 % context plus a type to inhabit) that satisfies a list of assumptions
475 % (preconditions) $P$,
476 % returns a couple $(L,t)$ where
477 % $L = {L_1,\ldots,L_n}$ is a (possible empty) list of proof-obligations
478 % (i.e. goals) and $t$ is a function that, given a list $l = {l_1,\ldots,l_n}$
479 % of terms such that $l_i$ inhabits\footnote{We say, with a small abuse of
480 % language, that a term $t$ inhabits a goal $G=(\Gamma,T)$ when $t$ is of type
481 % $T$ in the context $\Gamma$.} $L_i$ for each $i$ in ${1,\ldots,n}$, returns a
482 % term $t(l)$ inhabiting $G$.
483 % \end{definition}
484
485 % % You can get another looser definition of tactic just saying that a tactic
486 % % is a partial function instead than a total one. In this paper when referring
487 % % to tactics we always refer to the first definition.
488
489 % \begin{definition}[Of term metric]
490 % For any goal $G$, a term metric $\mu_G$ is any function in
491 % $\mathbb{N}^{\{t/t\;\mbox{inhabits}\;G\}}$.
492 % Two important class of term metrics are functional metrics and monotone
493 % metrics:
494 % \begin{enumerate}
495 % \item {\bf Functional metrics:} a metric $\mu_G$ is \emph{functional}
496 %  if for each term context (=term with an hole) $C[]$ and for all terms
497 %  $t_1$,$t_2$ if $\mu_G(t_1)=\mu_G(t_2)$ then $\mu_G(C[t_1])=\mu_G(C[t_2])$.
498 %  An equivalent definition is that a metric $\mu_G$ is functional
499 %  if for each term context $C[]$ the function
500 %  $\lambda t.\mu_G(C[\mu_G^{-1}(t)])$ is well defined.
501 % \item {\bf Monotone metrics:} a metric $\mu_G$ is \emph{monotone} if for
502 %  each term context $C[]$ and for all terms $t_1$,$t_2$ if
503 %  $\mu_G(t_1)\le\mu_G(t_2)$ then $\mu_G(C[t_1])\le\mu_G(C[t_2])$.
504 %  An equivalent definition is that a metric $\mu_G$ is functional if
505 %  for each term context $C[]$ the function
506 %  $\lambda t.\mu_G(C[\mu_G^{-1}(t)])$ is well defined and monotone.
507 % \end{enumerate}
508
509 % % Vecchie definizioni
510 % %\begin{enumerate}
511 % %\item {\bf Monotony:} a metric $\mu_G$ is \emph{monotone} if for each term
512 %  %context (=term with an hole) $C[]$ and for all terms $t_1$,$t_2$ if
513 %  %$\mu_G(t_1)\le\mu_G(t_2)$ then $\mu_G(C[t_1])\le\mu_G(C[t_2])$
514 % %\item {\bf Strict monotony:} a monotone metric $\mu_G$ is \emph{strictly
515 %  %monotone} if for each term context $C[]$ and
516 %  %for all terms $t_1$,$t_2$ if $\mu_G(t_1)=\mu_G(t_2)$ then
517 %  %$\mu_G(C[t_1])=\mu_G(C[t_2])$
518 % %\end{enumerate}
519 % \end{definition}
520
521 % Typical examples of term metrics are the size of a term, the time required
522 % to retype it or even an estimate of its ``naturality'' (or simplicity) to be
523 % defined somehow; the first two are also examples of monotone metrics
524 % and the third one could probably be defined as to be. So, in the rest
525 % of this paper, we will restrict to monotone metrics, even if the
526 % following definitions also work with weaker properties for general metrics.
527 % Here, however, we are not interested in defining such metrics, but in showing
528 % how they naturally induce metrics for tactics.
529
530 % Once a term metric is chosen, we get the notion of a best term (not unique!)
531 % inhabiting a goal:
532 % \begin{definition}[Of best terms inhabiting a goal]
533 % the term $t$ inhabiting a goal $G$ is said to be a best term inhabiting $G$
534 % w.r.t. the metric $\mu_G$ when
535 % $\mu_G(t) = min\{\mu_G(t') / t'\;\mbox{inhabits}\;G\}$.
536 % \end{definition}
537
538 % Using the previous notion, we can confront the behavior of two tactics
539 % on the same goal:
540
541 % \begin{definition}
542 % Let $\tau_1$ and $\tau_2$ be two tactics both applyable to a goal $G$
543 % such that $\tau_1(G) = (L_1,t_1)$ and $\tau_2(G) = (L_2,t_2)$.
544 % We say that $\tau_1$ is better or equal than $\tau_2$ for the goal $G$
545 % with respect to $\mu_G$ if, for all $l_1$ and $l_2$ lists of best terms
546 % inhabiting respectively $L_1$ and $L_2$, $\mu_G(t_1(l_1)) \le \mu_G(t_2(l_2))$
547 % holds.
548 % \end{definition}
549 % Note that confronting in this way ``equivalent'' tactics
550 % (whose definition is precised in the next section) gives us information
551 % on which implementation is better; doing the same thing on tactics that
552 % are not equivalent, instead, gives us information about what tactic to
553 % apply to obtain the best proof.
554
555 % A (functional) metric to confront two tactics only on a particular goal
556 % has the nice property to be a total order, but is quite useless. Hence,
557 % we will now
558 % define a bunch of different tactic metrics induced by term metrics
559 % that can be used to confront the behavior of tactics when applied to
560 % a generic goal. Some of them will be \emph{deterministic} partial orders;
561 % others will be total orders, but will provide only a \emph{probabilistic}
562 % estimate of the behavior. Both kinds of metrics are useful in practice
563 % when rating tactics implementation.
564 % \begin{definition}[Of locally deterministic better or equal tactic]
565 % $\tau_1$ is a locally deterministic (or locally
566 % uniform) better or equal tactic
567 % than $\tau_2$ w.r.t. $\mu$ (and in that case we write $\tau_1 \le_{\mu} \tau_2$
568 % or simply $\tau_1 \le \tau_2$), when
569 % for all goals $G$ satisfying the preconditions of both tactics we have that
570 % $\tau_1$ is better or equal than $\tau_2$ w.r.t. the metric $\mu_G$.
571 % \end{definition}
572
573 % \begin{definition}[Of locally deterministic better tactic]
574 % $\tau_1$ is a locally deterministic (or locally uniform)
575 % better tactic than $\tau_2$ 
576 % w.r.t. $\mu$ (and in that case we write $\tau_1 <_{\mu} \tau_2$
577 % or simply $\tau_1 < \tau_2$), when $\tau_1 \le_{\mu} \tau_2$ and
578 % exists a goal $G$ satisfying the preconditions of both tactics such that
579 % $\tau_1$ is better (but not equal!) than $\tau_2$ w.r.t. the metric $\mu_G$.
580 % \end{definition}
581
582 % \begin{definition}[Of locally probabilistic better or equal tactic of a factor K]
583 % $\tau_1$ is said to be a tactic locally probabilistic better or equal of a
584 % factor $0.5 \le K \le 1$ than $\tau_2$ w.r.t. $\mu$ and a particular expected
585 % goals distribution when the probability of having $\tau_1$ better or equal than
586 % $\tau_2$ w.r.t. the metric $\mu_G$ is greater or equal to $K$ when $G$ is
587 % chosen randomly according to the distribution.
588 % \end{definition}
589 % The set of terms being discrete, you can note that a deterministically better
590 % or equal tactic is a tactic probabilistically better or equal of a factor 1.
591
592
593 % To end this section, we can remark the strong dependence of the $\le$
594 % relation on the choice of metric $\mu$, so that it is easy to find
595 % two metrics $\mu_1,\mu_2$ such that $\tau_1 <_{\mu_1} \tau_2$ and
596 % $\tau_2 <_{\mu_2} \tau_1$. Luckily, tough, the main interesting metrics,
597 % term size, retyping time and naturality, are in practice highly correlated,
598 % though the correlation of the third one with the previous two could be a
599 % bit surprising. So, in the following section, we
600 % will not state what is the chosen term metric; you may think as
601 % any of them or even at some kind of weighted mean.
602
603
604 % \section{Equivalent Tactics and Overkilling}
605 % \label{equivalence}
606 % We are now interested in using the metrics defined in the previous section
607 % to confront tactics implementation. Before doing so, though, we have to
608 % identify what we consider to be different implementations of the
609 % same tactic. Our approach consists in identifying every implementation
610 % with the tactic it implements and then defining appropriate notions of
611 % equivalence for tactics: two equivalent tactics will then be considered
612 % as equivalent implementations and will be confronted using metrics.
613
614 % Defining two tactics as equivalent when they can solve exactly the same
615 % set of goals generating the same set of proof-obligations seems quite natural,
616 % but is highly unsatisfactory if not completely wrong. The reason is that, for
617 % equivalent tactics, we would like to have the \emph{property of substitutivity},
618 % that is substituting a tactic for an equivalent one in a script should give
619 % back an error-free script\footnote{A weaker notion of substitutivity is that
620 % substituting the term generated by a tactic for the term generated by an
621 % equivalent one in a generic well-typed term should always give back a
622 % well-typed term.}. In logical frameworks with dependent types,
623 % without proof-irrelevance and with universes as CIC \cite{Werner} though,
624 % it is possible for a term to inspect the term of a previous proof, behaving
625 % in a different way, for example, if the constructive proof of a conjunction
626 % is made proving the left or right side.
627 % So, two tactics, equivalent w.r.t.  the previous
628 % definition, that prove $A \vee A$ having at their disposal an
629 % hypothesis $A$ proving the first one the left and the second one
630 % the right part of the conjunction, could not be substituted one for the
631 % other if a subsequent term inspects the form of the generated proof.
632
633 % Put in another way, it seems quite reasonable to derive equivalence for
634 % tactics from the definition of an underlying equivalence for terms.
635 % The simplest form of such an equivalence relation is convertibility
636 % (up to proof-irrelevance) of closed terms and this is the relation
637 % we will use in this section and the following one. In particular,
638 % we will restrict ourselves to CIC and hence to
639 % $\beta\delta\iota$-convertibility\footnote{The Coq proof-assistant
640 % introduces the notion of \emph{opaque} and \emph{transparent} terms,
641 % differing only for the possibility of being inspected. Because the
642 % user could change the opacity status at any time, the notion of
643 % convertibility we must conservatively choose for the terms of Coq is
644 % $\beta\delta\iota$-convertibility after having set all the definitions
645 % as transparent.}.
646 % Convertibility, though, is a too restrictive notion that does not take
647 % in account, for example, commuting conversions. Looking for more suitable
648 % notions of equivalence is our main open issue for future work.
649
650 % \begin{definition}[Of terms closed in a local environment]
651 % A term $t$ is \emph{closed} in a local environment $\Gamma$ when $\Gamma$
652 % is defined on any free variable of $t$.
653 % \end{definition}
654
655 % \begin{definition}[Of equivalent tactics]
656 % We define two tactics $\tau_1$ and $\tau_2$ to be equivalent (and
657 % we write $\tau_1 \approx \tau_2$) when for each goal $G = (\Gamma,T)$ and for
658 % each list of terms closed in $\Gamma$ and inhabiting the proof-obligations
659 % generated respectively by $\tau_1$ and $\tau_2$, we have that the result terms
660 % produced by $\tau_1$ and $\tau_2$ are $\beta\delta\iota$-convertible.
661 % \end{definition}
662
663 % Once we have the definition of equivalent tactics, we can use metrics,
664 % either deterministic or probabilistic, to confront them. In particular,
665 % in the rest of this section and in the following one we will focus on the
666 % notion of deterministically overkilling tactic, defined as follows:
667
668 % \begin{definition}[Of overkilling tactics]
669 % A tactic $\tau_1$ is (deterministically) overkilling w.r.t. a metric
670 % $\mu$ when there exists another tactic $\tau_2$ such that
671 % $\tau_1 \approx \tau_2$ and $\tau_2 <_\mu \tau_1$.
672 % \end{definition}
673
674 % Fixing an overkilling tactic $\tau_1$ means replacing it with the
675 % tactic $\tau_2$ which is the witness of $\tau_1$ being overkilling.
676 % Note that the fixed tactic could still be overkilling.
677
678 % The name overkilling has been chosen because most of the time overkilling
679 % tactics are tactics that do not consider special cases, following the general
680 % algorithm. While in computer science it is often a good design pattern to
681 % prefer general solutions to ad-hoc ones, this is not a silver bullet:
682 % an example comes another time from compiler technology, where
683 % ad-hoc cases, i.e. optimizations, are greatly valuable if not necessary.
684 % In our context, ad-hoc cases could be considered either as optimizations,
685 % or as applications of Occam's razor to proofs to keep the simplest one.
686
687 % \subsection{A Simple Example of Overkilling Tactic}
688 % A first example of overkilling tactic in system Coq is
689 % \emph{Replace}, that works in this way: when the current goal
690 % is $G = (\Gamma,T)$, the
691 % tactic ``{\texttt Replace E$_1$ with E$_2$.}'' always produces a new
692 % principal proof-obligation $(\Gamma, T\{E_2/E_1\})$ and an auxiliary
693 % proof-obligation $(\Gamma, E_1=E_2)$ and uses the elimination scheme
694 % of equality on the term $E_1 = E_2$ and the two terms that inhabit the
695 % obligations to prove the current goal.
696
697 % To show that this tactic is overkilling, we will provide an example
698 % in which the tactic fails to find the best term, we will
699 % propose a different implementation that produces the best term and we will
700 % show the equivalence with the actual one.
701
702 % The example consists in applying the tactic in the case in which $E_1$ is
703 % convertible to $E_2$: the tactic proposes to the user the two
704 % proof-obligations and then builds the term as described above. We claim
705 % that the term inhabiting the principal proof-obligation also inhabits
706 % the goal and, used as the generated term, is surely smaller
707 % and quicker to retype than the one that is generated in the
708 % implementation; moreover, it
709 % is also as natural as the previous one, in the sense that the apparently
710 % lost information has simply become implicit in the reduction and could
711 % be easily rediscovered using type-inference algorithms as the one described
712 % in Coscoy's thesis \cite{YANNTHESIS}. So, a new implementation could
713 % simply recognize this special case and generate the better term.
714
715 % We will now show that the terms provided by the two implementations are
716 % $\beta\delta\iota$-convertible.
717 % Each closed terms in $\beta\delta\iota$-normal form
718 % inhabiting the proof that $E_1$ is equal to $E_2$ is equal to the only
719 % constructor of equality applied to a term convertible to the type of
720 % $E_1$ and to another term convertible to $E_1$; hence, once the principle
721 % of elimination of equality is applied to this term, we can first apply
722 % $\beta$-reduction and then $\iota$-reduction to obtain the term inhabiting
723 % the principal proof-obligation in which $E_1$ has been replaced by $E_2$.
724 % Since $E_1$ and $E_2$ are $\beta\delta\iota$-convertible by hypothesis and
725 % for the congruence properties of convertibility in CIC, we have that the
726 % generated term is $\beta\delta\iota$-convertible to the one inhabiting the
727 % principal proof-obligation.
728
729 % %The simplest example is when $E_1$ and $E_2$ are syntactically equal:
730 % %in this case the principal obligation proposed is identical to the current goal
731 % %and the equality elimination introduced could be replaced by the proof of
732 % %this obligation, leading to a smaller, quicker to retype and also more
733 % %natural\footnote{Even if naturality is in some way a subjective metric,
734 % %who would dare say that a proof in which you rewrite an expression with
735 % %itself is natural?} term. To show that handling this special case in this
736 % %way is equivalent to the current solution, we have to show that the two
737 % %terms are $\beta\delta\iota$-convertible when so are the subterms inhabiting
738 % %the obligations.
739 % %The only closed term in $\beta\delta\iota$-normal form
740 % %inhabiting the proof that $E_1$ is equal to $E_1$ is the only constructor
741 % %of equality applied to the type of $E_1$ and to $E_1$; hence we can
742 % %first apply $\beta$-reduction and then $\iota$-reduction to obtain
743 % %the second proof in which $E_1$ has been replaced by $E_1$, i.e. the
744 % %second proof.
745
746 % %So, for this example, the simple fix to avoid overkilling is checking
747 % %if $E_1$ and $E_2$ are syntactically equal and in case omitting the elimination;
748 % %curiously, this is done in Coq in the implementation of a very similar tactic
749 % %called \emph{Rewriting}.
750
751
752 % %A second, more involved, example is when $E_1$ and $E_2$ are not
753 % %syntactically equal, but $E_1$ reduces to $E_2$. Even in this case
754 % %the elimination could simply be avoided because the typing rules of
755 % %the logical system ensures us that a term that inhabits the principal
756 % %obligation also inhabits the current goal: the proof is equivalent to the
757 % %previous one, but for the last step in which $E_2$ is actually substituted for
758 % %$E_1$; by hypothesis, though, the two terms are $\beta\delta\iota$-convertible
759 % %and hence the thesis.
760
761 % %Surely smaller and faster to retype, the new term is also as natural
762 % %as the previous one, in the sense that the apparently lost information has
763 % %simply become implicit in the reduction and could be easily rediscovered
764 % %using type-inference algorithms as the one described in chapter ???
765 % %of Coscoy's thesis \ref{YANNTHESIS}.
766
767 % This example may seem quite stupid because, if the user is already able to
768 % prove the principal proof-obligation and because this new goal is totally
769 % equivalent to the original one, the user could simply redo the same steps
770 % without applying the rewriting at all. Most of the time, though, the
771 % convertibility of the two terms could be really complex to understand,
772 % greatly depending on the exact definitions given; indeed, the user could
773 % often be completely unaware of the convertibility of the two terms. Moreover,
774 % even in the cases in which the user understands the convertibility, the
775 % tactic has the important effect of changing the form of the current
776 % goal in order to simplify the task of completing the proof, which is the reason
777 % for the user to apply it.
778
779 % ~\\
780
781 % The previous example shows only a very small improvement in the produced
782 % term and could make you wonder if the effort of fixing overkilling and
783 % more in general if putting more attention to terms when implementing
784 % tactics is really worth the trouble. In the next section we describe as
785 % another example a concrete experience of fixing a complex reflexive tactic
786 % in system Coq that has lead to really significant improvements in term
787 % size, retyping time and naturality.
788
789 % \section{Fixing Overkilling: a Concrete Experience}
790 % Coq provides a reflexive tactic called \emph{Ring} to do associative-commutative
791 % rewriting in ring and semi-ring structures. The usual usage is,
792 % given the goal $E_1 = E_2$ where $E_1$ and $E_2$ are two expressions defined
793 % on the ring-structure, to prove the goal reducing it to proving
794 % $E'_1 = E'_2$ where $E'_i$ is the normal form of $E_i$. In fact, once
795 % obtained the goal $E'_1 = E'_2$, the tactic also tries to apply simple
796 % heuristics to automatically solve the goal.
797
798 % The actual implementation of the tactic by reflexion is quite complex and
799 % is described in \cite{Ring}. The main idea is described in Fig. \ref{ring1}:
800 % first of all, an inductive data type to describe abstract polynomial is
801 % made available. On this abstract polynomial, using well-founded recursion in
802 % Coq, a normalization function named $apolynomial\_normalize$ is defined;
803 % for technical reasons, the abstract data type of normalized polynomials
804 % is different from the one of un-normalized polynomials. Then, two
805 % interpretation functions, named $interp\_ap$ and $interp\_sacs$ are given
806 % to map the two forms of abstract polynomials to the concrete one. Finally,
807 % a theorem named $apolynomial\_normalize\_ok$ stating the equality of
808 % the interpretation of an abstract polynomial and the interpretation of
809 % its normal form is defined in Coq using well-founded induction. The above
810 % machinery could be used in this way: to prove that $E^I$ is equal to its
811 % normal form $E^{IV}$, the tactic computes an abstract polynomial $E^{II}$ that,
812 % once interpreted, reduces to $E^{I}$, and such that the interpretation
813 % of $E^{III} = (apolynomial\_normalize E^{II})$ could be shown to be equal
814 % to $E^{IV}$ applying $apolynomial\_normalize\_ok$.
815
816 % %such that $(interp\_ap\;E^{II})$ could be proved (theorem
817 % %$apolynomial\_normalize\_ok$) in Coq to be equal to
818 % %$(interp\_sacs\;E^{III})$ where
819 % %$E^{III} = (apolynomial\_normalize E^{II})$ is another abstract polynomial
820 % %in normal form and the three functions $interp\_ap$, $interp\_sacs$ and
821 % %$apolynomial\_normalize$ could all be defined inside Coq using well-founded
822 % %recursion/induction on the abstract polynomial definition.
823
824 % % \myincludegraphics{ring1}{t}{12cm}{Reflexion in Ring}{Reflexion in Ring}
825 % % \myincludegraphics{ring2}{t}{10cm}{Ring implementation (first half)}{Ring implementation (first half)}
826
827 % In Fig. \ref{ring2} the first half of the steps taken
828 % by the Ring tactic to prove $E_1 = E_2$ are shown\footnote{Here $E_1$
829 % stands for $E^I$.}.
830 % The first step is replacing $E_1 = E_2$ with $(interp\_ap\;E^{II}_1) = E_2$,
831 % justifying the rewriting using the only one constructor of equality due to
832 % the $\beta\delta\iota$-convertibility of $(interp\_ap\;E^{II}_1)$ with $E_1$.
833 % The second one is replacing $(interp\_ap\;E^{II})$ with
834 % $(interp\_sacs\;E^{III})$, justifying the rewriting using
835 % $apolynomial\_normalize\_ok$.
836
837 % Next, the two steps are done again on the left part of the equality,
838 % obtaining $(interp\_sacs\;E^{III}_1) = (interp\_sacs\;E^{III}_2)$,
839 % that is eventually solved trying simpler tactics as \emph{Reflexivity} or left
840 % to the user.
841
842 % The tactic is clearly overkilling, at least due to the usage of rewriting for
843 % convertible terms. Let's consider as a simple example the session in Fig.
844 % \ref{session}:
845 % \begin{figure}[t]
846 % %\begin{verbatim}
847 % \mbox{\hspace{3cm}\tt Coq $<$ Goal ``0*0==0``.}\\
848 % \mbox{\hspace{3cm}\tt 1 subgoal}\\
849 % ~\\
850 % \mbox{\hspace{3cm}\tt ~~=================}\\
851 % \mbox{\hspace{3cm}\tt ~~~~``0*0 == 0``}\\
852 % ~\\
853 % \mbox{\hspace{3cm}\tt Unnamed\_thm $<$ Ring.}\\
854 % \mbox{\hspace{3cm}\tt Subtree proved!}
855 % %\end{verbatim}
856 % \caption{A Coq session.}
857 % \label{session}
858 % \end{figure}
859 % in Fig. \ref{before} the $\lambda$-term created by the
860 % original overkilling implementation of Ring is shown. Following the previous
861 % explanation, it should be easily understandable. In particular, the
862 % four rewritings are clearly visible as applications of $eqT\_ind$, as
863 % are the two applications of $apolynomial\_normalize\_ok$ and the
864 % three usage of reflexivity, i.e. the two applications of $refl\_eqT$
865 % to justify the rewritings on the left and right members of the equality
866 % and the one that ends the proof.
867
868 % Let's start the analysis of overkilling in this implementation:
869 % \begin{description}
870 % \item[Always overkilling rewritings:] as already stated, four of the rewriting
871 %  steps are always overkilling because the rewritten term is convertible to
872 %  the original one due to the tactic implementation.
873 %  As proved in the previous section, all these rewritings could be simply
874 %  removed obtaining an equivalent tactic.
875 % \item[Overkilling rewritings due to members already normalized:] it may happen,
876 %  as in our example, that one (or even both) of the two members is already in
877 %  normal form. In this case the two rewriting steps for that member could be
878 %  simply removed obtaining an equivalent tactic as shown in the previous section.
879 % \item[Rewriting followed by reflexivity:] after having removed all
880 %  the overkilling rewritings, the general form of the $\lambda$-term produced
881 %  for $E_1 = E_2$ is the application of two rewritings ($E'_1$ for $E_1$ and
882 %  $E'_2$ for $E_2$), followed by a proof of $E'_1 = E'_2$. In many cases,
883 %  $E'_1$ and $E'_2$ are simply convertible and so the tactic finishes the
884 %  proof with an application of reflexivity to prove the equivalent goal
885 %  $E'_1 = E'_1$.
886 %  A smaller and also more natural solution is just to
887 %  rewrite $E'_1$ for $E_1$ and then proving $E'_1 = E_2$ applying the lemma
888 %  stating the symmetry of equality to the proof of $E_2 = E'_2$.
889 %  The equivalence to the original
890 %  tactic is trivial by $\beta\iota$-reduction because the lemma is proved
891 %  exactly doing the rewriting and then applying reflexivity:
892 %  $$
893 %  \begin{array}{l}
894 %  \lambda A:Type.\\
895 %  \hspace{0.2cm}\lambda x,y:A.\\
896 %  \hspace{0.4cm}\lambda H:(x==y).\\
897 %  \hspace{0.6cm}(eqT\_ind~A~x~ [x:A]a==x~(refl\_eqT~A~x)~y~H)
898 %  \end{array}
899 %  $$
900 % \end{description}
901 % In Fig. \ref{after} is shown the $\lambda$-term created by the same
902 % tactic after having fixed all the overkilling problems described above.
903
904 % \begin{figure}
905 % \begin{verbatim}
906 % Unnamed_thm < Show Proof.
907 % LOC: 
908 % Subgoals
909 % Proof:
910 % (eqT_ind R
911 %   (interp_ap R Rplus Rmult ``1`` ``0`` Ropp (Empty_vm R)
912 %     (APmult AP0 AP0)) [r:R]``r == 0``
913 %   (eqT_ind R
914 %     (interp_sacs R Rplus Rmult ``1`` ``0`` Ropp (Empty_vm R)
915 %       Nil_varlist) [r:R]``r == 0``
916 %     (eqT_ind R
917 %       (interp_ap R Rplus Rmult ``1`` ``0`` Ropp (Empty_vm R) AP0)
918 %       [r:R]
919 %        ``(interp_sacs R Rplus Rmult 1 r Ropp (Empty_vm R) Nil_varlist)
920 %        == r``
921 %       (eqT_ind R
922 %         (interp_sacs R Rplus Rmult ``1`` ``0`` Ropp (Empty_vm R)
923 %           Nil_varlist)
924 %         [r:R]
925 %          ``(interp_sacs R Rplus Rmult 1 r Ropp (Empty_vm R)
926 %            Nil_varlist) == r`` (refl_eqT R ``0``)
927 %         (interp_ap R Rplus Rmult ``1`` ``0`` Ropp (Empty_vm R) AP0)
928 %         (apolynomial_normalize_ok R Rplus Rmult ``1`` ``0`` Ropp
929 %           [_,_:R]false (Empty_vm R) RTheory AP0)) ``0``
930 %       (refl_eqT R ``0``))
931 %     (interp_ap R Rplus Rmult ``1`` ``0`` Ropp (Empty_vm R)
932 %       (APmult AP0 AP0))
933 %     (apolynomial_normalize_ok R Rplus Rmult ``1`` ``0`` Ropp
934 %       [_,_:R]false (Empty_vm R) RTheory (APmult AP0 AP0))) ``0*0``
935 %   (refl_eqT R ``0*0``))
936 % \end{verbatim}
937 % \caption{The $\lambda$-term created by the original overkilling implementation}
938 % \label{before}
939 % \end{figure}
940
941 % \begin{figure}
942 % \begin{verbatim}
943 % Unnamed_thm < Show Proof.
944 % LOC: 
945 % Subgoals
946 % Proof:
947 % (sym_eqT R ``0`` ``0*0``
948 %   (apolynomial_normalize_ok R Rplus Rmult ``1`` ``0`` Ropp
949 %     [_,_:R]false (Empty_vm R) RTheory (APmult AP0 AP0)))
950 % \end{verbatim}
951 % \caption{The $\lambda$-term created by the new implementation}
952 % \label{after}
953 % \end{figure}
954
955 % \clearpage
956 % \subsection{A Quantitative Analysis of the Gain Obtained}
957 % Let's now try a quantitative analysis of the gain with respect to term size,
958 % retyping time and naturality, considering the two interesting cases of
959 % no member or only one member already in normal form\footnote{If the two
960 % members are already in normal form, the new implementation simply applies
961 % once the only constructor of the equality to one of the two members. The tactic
962 % is also implemented to do the same thing also when the two members are not yet
963 % in normal forms, but are already convertible. We omit this other improvement
964 % in our analysis.}.
965
966 % \subsubsection{Term Size.}
967 % \paragraph{Terms metric definition:} given a term $t$, the metric $|.|$ associates to it its number of nodes $|t|$.
968 % \paragraph{Notation}: $|T|$ stands for the number of nodes in the actual parameters
969 %  given to $interp\_ap$, $interp\_sacs$ and $apolynomial\_normalize\_ok$ to
970 %  describe the concrete (semi)ring theory and the list of non-primitive
971 %  terms occurring in the goal to solve. In the example in figures \ref{before}
972 %  and \ref{after}, $|T|$ is the number of nodes in
973 %  \begin{texttt}[R Rplus Rmult ``1`` ``0`` Ropp (Empty\_vm R)]\end{texttt}.
974 %  $|R|$ stands for the number of nodes in the term which is the carrier
975 %  of the ring structure. In the same examples, $|R|$ is simply 1, i.e.
976 %  the number of nodes in \begin{texttt}R\end{texttt}.
977
978 % \begin{displaymath}
979 % \begin{array}{l}
980 % \mbox{\bf Original version:}\\
981 % \begin{array}{ll}
982 % 1 + (|E^{II}_1| + |T| + 1) + |E_2| + |E_1| + &
983 % \mbox{(I rewriting Left)} \\
984 % 1 + |E_1| +  &
985 % \mbox{(justification)} \\
986 % 1 + (|E^{III}_1| + |T| + 1) + |E_2| + (|E^{II}_1| + |T| + 1) + &
987 % \mbox{(II rewriting Left)} \\
988 % (|E^{II}_1| + |T| + 1) + &
989 % \mbox{(justification)} \\
990 % 1 + (|E^{II}_2| + |T| + 1) + (|E^{III}_1| + |T| + 1) + |E_2| + &
991 % \mbox{(I rewriting Right)} \\
992 % 1 + |E_2| + &
993 % \mbox{(justification)} \\
994 % 1 + (|E^{III}_2| + |T| + 1) + (|E^{III}_1| + |T| + 1) + &
995 % \mbox{(II rewriting Right)} \\
996 % ~(|E^{II}_2| + |T| + 1) +& \\
997 % (|E^{II}_2| + |T| + 1) + &
998 % \mbox{(justification)} \\
999 % 1 + |E_1| = &
1000 % \mbox{(reflexivity application)} \\
1001 % \hline
1002 % 4|E_1| + 2|E_2| + 3|E^{II}_1| + 3|E^{II}_2| + 3|E^{III}_1| + |E^{III}_2| +~ &
1003 % \mbox{\bf Total number} \\
1004 % ~10|T| + 17 &
1005 % \end{array}
1006 % \end{array}
1007 % \end{displaymath}
1008
1009 % \begin{displaymath}
1010 % \begin{array}{l}
1011 % \mbox{\bf New version, both members not in normal form:}\\
1012 % \begin{array}{ll}
1013 % 1 + |R| + |E_1| + |E'_2| + &
1014 % \mbox{(Rewriting Right)} \\
1015 % 1 + |T| + |E^{II}_2| + &
1016 % \mbox{(justification)} \\
1017 % 1 + |R| + |E'_2| + |E'_1| + |E_2| + &
1018 % \mbox{(Symmetry application)} \\
1019 % 1 + |T| + |E^{II}_1| = &
1020 % \mbox{(justification)} \\
1021 % \hline
1022 % 2|E_1| + |E_2| + |E^{II}_1| + |E^{II}_2| + 2|E'_2| + 2|T| +~ &
1023 % \mbox{\bf Total number} \\
1024 % ~2|R| + 4  & \\
1025 % ~ &
1026 % \end{array}
1027 % \end{array}
1028 % \end{displaymath}
1029 % \begin{displaymath}
1030 % \begin{array}{l}
1031 % \mbox{\bf New version, only the first member not in normal form:}\\
1032 % \begin{array}{ll}
1033 % 1 + |R| + |E_1| + |E'_2| + &
1034 % \mbox{(Rewriting)} \\
1035 % 1 + |T| + |E^{II}_2| = &
1036 % \mbox{(justification)} \\
1037 % \hline
1038 % |E_1| + |E'_2| + |E^{II}_2| + |T| + |R| + 2~~~  &
1039 % \mbox{\bf Total number} \\
1040 % ~ &
1041 % \end{array}
1042 % \end{array}
1043 % \end{displaymath}
1044
1045 % While the overall space complexity of the terms generated by the new
1046 % implementation is asymptotically equal to the one of the old implementation,
1047 % all the constants involved are much smaller, but for the one of
1048 % $E'_2$ (the two normal forms) that before was 0 and now is
1049 % equal to 2. Is it possible to have goals for which the new implementation
1050 % behaves worst than the old one? Unfortunately, yes. This happens when
1051 % the size of the two normal forms $E'_1$ and $E'_2$ is greatly huger than
1052 % ($E^{II}_1 + |T| + 1)$ and $(E^{II}_2 + |T| + 1)$. This happens when
1053 % the number of occurrences of non-primitive terms is much higher than
1054 % the number of non-primitive terms and the size of them is big. More
1055 % formally, being $m$ the number of non-primitive terms, $d$ the average
1056 % size and $n$ the number of occurrences, the new implementation creates bigger
1057 % terms than the previous one if
1058 % \begin{displaymath}
1059 % n \log_2 m + m  d < n  d
1060 % \end{displaymath}
1061 % where the difference between the two members is great enough to hide the gain
1062 % achieved lowering all the other constants.
1063 % The logarithmic factor in the previous
1064 % formula derives from the implementation of the map of variables to
1065 % non-primitive terms as a tree and the representation of occurrences with
1066 % the path inside the tree to retrieve the term.
1067
1068 % To fix the problem, for each non-primitive term occurring more than once
1069 % inside the normal forms, we can use a \emph{let \ldots in} local definition
1070 % to bind it to a fresh identifier; then we replace every occurrence of
1071 % the term inside the normal forms with the appropriate
1072 % identifier\footnote{This has not yet been implemented in Coq.}.
1073 % In this way, the above inequation becomes
1074 % \begin{displaymath}
1075 % n \log_2 m + m  d < n + m  d
1076 % \end{displaymath}
1077 % that is never satisfied.
1078
1079 % Here it is important to stress how the latest problem was easily
1080 % overlooked during the implementation and has been discovered
1081 % only during the previous analysis, strengthening our belief in
1082 % the importance of this kind of analysis for tactic implementations.
1083
1084 % In the next two paragraphs we will consider only the new implementation
1085 % with the above fixing.
1086
1087 % \subsubsection{Retyping Time.}
1088 % \paragraph{Terms metric definition:} given a term $t$, the metric $|.|$ associates to it the time $|t|$ required to retype it.
1089
1090 % Due to lack of space, we will omit a detailed analysis as the one given
1091 % for terms size. Nevertheless, we can observe that the retyping time required
1092 % is surely smaller because all the type-checking operations required for
1093 % the new implementation are already present in the old one, but for the
1094 % type-checking of the two normal forms, that have fewer complexity
1095 % than the type-checking of the two abstract normal forms, and the
1096 % \emph{let \ldots in} definitions that have the same complexity of the
1097 % type-checking of the variable map. Moreover, the quite expensive operation
1098 % of computing the two normal forms is already done during proof construction.
1099
1100 % %In the case in which both the members of the equality are not in normal form,
1101 % %we can't expect a great improvement in retyping time but for very cheap
1102 % %normalizations; this because retyping time is dominated by the factor due to
1103 % %$apolynomial\_normalize$ that is present in both implementations and is
1104 % %usually much higher than the factor due to the removed applications of
1105 % %$interp\_ap$, $interp\_sacs$ and the eliminations of equality.
1106
1107 % %The situation is very different if one of the two members is already
1108 % %convertible to its normal form and the reduction involved in the normalization
1109 % %is expensive. In this rather unlikely case, the new implementation
1110 % %avoids the reduction at all, roughly halving the overall retyping time.
1111
1112 % In section \ref{benchmarks} we present some benchmarks to give an idea of
1113 % the real gain obtained.
1114
1115 % \subsubsection{Naturality.}~\\~\\
1116 % The idea behind the \emph{Ring} tactic is to be able to prove
1117 % an equality showing that both members have the same normal form.
1118 % This simply amounts to show that each member is equal to the same
1119 % normal form, that is exactly what is done in the new implementation.
1120 % Indeed, every step that belonged to the old implementation and has been
1121 % changed or removed to fix overkilling used to lead to some unnatural step:
1122 % \begin{enumerate}
1123 % \item The fact that the normalization is not done on the concrete
1124 %  representation, but passing through two abstract ones that are
1125 %  interpreted on the concrete terms is an implementative detail that
1126 %  was not hidden as much as possible as it should be.
1127 % \item Normalizing a member of the equality that is already in normal form,
1128 %  is illogical and so unnatural. Hence it should be avoided, but it was not.
1129 % \item The natural way to show $A=B$ under the hypothesis $B=A$ is just
1130 %  to use the symmetric property of equality. Instead, the old implementation
1131 %  rewrote $B$ with $A$ using the hypothesis and proved the goal by reflexivity.
1132 % \item Using local definitions (\emph{let \ldots in}) as abbreviations
1133 %  rises the readability of the proof by shrinking its size removing
1134 %  subexpressions that are not involved in the computation.
1135 % \end{enumerate}
1136
1137 % \subsection{Some Benchmarks}
1138 % \label{benchmarks}To understand the actual gain in term size and retyping
1139 % time on real-life examples, we have done some benchmarks on the whole set
1140 % of theorems in the standard library of Coq that use the Ring tactic. The
1141 % results are shown in table~\ref{benchs}.
1142
1143 % Term size is the size of the disk dump of the terms. Re-typing time is the
1144 % user time spent by Coq in proof-checking already parsed terms. The reduction
1145 % of the terms size implies also a reduction in Coq parsing time, that is
1146 % difficult to compute because Coq files do not hold single terms, but whole
1147 % theories. Hence, the parsing time shown is really the user time spent by Coq
1148 % to parse not only the terms on which we are interested, but also all the
1149 % terms in their theories and the theories on which they depend. So, this last
1150 % measure greatly under-estimates the actual gain.
1151
1152 % Every benchmark has been repeated 100 times under different load conditions on
1153 % a 600Mhz Pentium III bi-processor equipped with 256Mb RAM. The timings shown
1154 % are mean values.
1155
1156 % \begin{table}
1157 % \begin{center}
1158 % \begin{tabular}{|l|c|c|c|}
1159 % \hline
1160 %  & ~Term size~ & Re-typing time & Parsing time\\
1161 % \hline
1162 % Old implementation & 20.27Mb & 4.59s & 2.425s \\
1163 % \hline
1164 % New implementation & 12.99Mb & 2.94s & 2.210s \\
1165 % \hline
1166 % Percentage reduction & 35.74\% & 35.95\% & 8.87\% \\
1167 % \hline
1168 % \end{tabular}
1169 % \end{center}
1170 % \caption{Some benchmarks}
1171 % \label{benchs}
1172 % \end{table}
1173
1174 % \section{Conclusions and Future Work}
1175 % Naive ways of implementing tactics lead to low quality terms that are
1176 % difficult to inspect and process. To improve the situation, we show
1177 % how metrics defined for terms naturally induce metrics for tactics and
1178 % tactics implementation and we advocate the usage of such metrics for
1179 % tactics evaluation and implementation. In particular, metrics could
1180 % be used to analyze the quality of an implementation or could be used at run
1181 % time by a tactic to choose what is the best way to proceed.
1182
1183 % To safely replace a tactic implementation with another one, it is important
1184 % to define when two tactics are equivalent, i.e. generate equivalent terms.
1185 % In this work, the equivalence relation chosen for terms has simply been 
1186 % $\beta\delta\iota$-convertibility, that in many situations seems too strong.
1187 % Hence, an important future work is the study of weaker forms of term
1188 % equivalence and the equivalence relations they induce on tactics. In particular,
1189 % it seems that proof-irrelevance, $\eta$-conversion and commuting conversions
1190 % must all be considered in the definition of a suitable equivalence relation.
1191
1192 \begin{thebibliography}{01}
1193
1194 % \bibitem{ALF} The ALF family of proof-assistants:\\
1195 % {\tt http://www.cs.chalmers.se/ComputingScience/Research/\\Logic/implementation.mhtml}
1196
1197 % \bibitem{Coq} The Coq proof-assistant:\\
1198 %  {\tt http://coq.inria.fr/}
1199
1200 % \bibitem{FORMAVIE} The Formavie project:\\
1201 %  {\tt http://http://www-sop.inria.fr/oasis/Formavie/}
1202
1203 % \bibitem{EHELM} The HELM project:\\
1204 %  {\tt http://www.cs.unibo.it/helm/}
1205
1206 % \bibitem{MATHWEB} The MathWeb project:\\
1207 %  {\tt http://www.mathweb.org/}
1208
1209 % \bibitem{PCOQ} The PCoq project:\\
1210 %  {\tt http://www-sop.inria.fr/lemme/pcoq/}
1211
1212 % \bibitem{HELM} A.Asperti, L.Padovani, C.Sacerdoti Coen, I.Schena.
1213 % Towards a library of formal mathematics. Panel session of
1214 % the 13th International Conference on Theorem Proving in Higher Order Logics (TPHOLS'2000),
1215 % Portland, Oregon, USA.
1216
1217 % \bibitem{Ring} S. Boutin. Using reflection to build efficient and certified
1218 %  decision procedures. In Martin Abadi and Takahashi Ito, editors, TACS'97,
1219 %  volume 1281. LNCS, Springer-Verlag, 1997.
1220
1221 % \bibitem{YANNTHESIS} Y.Coscoy. \emph{Explication textuelle de preuves pour le
1222 % Calcul des Constructions Inductives}, PhD. Thesis, Universit\'e de Nice-Sophia
1223 % Antipolis, 2000.
1224
1225 % \bibitem{ALFA} T. Hallgreen, Aarne Ranta. An Extensible Proof Text Editor.
1226 % Presented at LPAR2000.
1227
1228 % \bibitem{Necula} G. Necula, P. Lee. Safe Kernel Extensions Without Run-Time
1229 %  Checking. Presented at OSDI'96, October 1996.
1230
1231 % \bibitem{Necula2} G. Necula, P. Lee. Efficient Representation and Validation of Proofs. Presented at LICS'98, June 1998
1232
1233 % \bibitem{Werner} B. Werner. \emph{Une Th\'eorie des Constructions Inductives},
1234 %  PhD. Thesis, Universit\'e Paris VII, May 1994.
1235
1236 \end{thebibliography}
1237  
1238 \end{document}