]> matita.cs.unibo.it Git - helm.git/blob - helm/papers/matita/matita2.tex
5262e151b6a16c2d463f346a37cbf948ad48a5d1
[helm.git] / helm / papers / matita / matita2.tex
1 \documentclass[]{kluwer}
2 \usepackage{color}
3 \usepackage{graphicx}
4 \usepackage{hyperref}
5 \usepackage{color}
6 \usepackage{fancyvrb}
7 \usepackage[show]{ed}
8
9 \newcommand{\component}{component}
10 \newcommand{\components}{components}
11
12 \newcommand{\BOXML}{BoxML}
13 \newcommand{\COQ}{Coq}
14 \newcommand{\GDOME}{Gdome}
15 \newcommand{\GTK}{GTK+}
16 \newcommand{\GTKMATHVIEW}{\textsc{GtkMathView}}
17 \newcommand{\HELM}{Helm}
18 \newcommand{\HINT}{\textsc{Hint}}
19 % \newcommand{\IN}{\ensuremath{\dN}}
20 \newcommand{\LEGO}{Lego}
21 \newcommand{\MATHML}{MathML}
22 \newcommand{\MATITA}{Matita}
23 \newcommand{\MATITAC}{\texttt{matitac}}
24 \newcommand{\MATITADEP}{\texttt{matitadep}}
25 \newcommand{\MOWGLI}{MoWGLI}
26 \newcommand{\MOWGLIIST}{IST-2001-33562}
27 \newcommand{\NUPRL}{NuPRL}
28 \newcommand{\OCAML}{OCaml}
29 \newcommand{\REF}[3]{\ensuremath{\mathit{Ref}_{#1}(#2,#3)}}
30 \newcommand{\REWRITEHINT}{\textsc{RewriteHint}}
31 \newcommand{\TEXMACRO}[1]{\texttt{\char92 #1}}
32 \newcommand{\UWOBO}{UWOBO}
33 \newcommand{\GETTER}{Getter}
34 \newcommand{\WHELP}{Whelp}
35
36 \newcommand{\DOT}{\ensuremath{\mbox{\textbf{.}}}}
37 \newcommand{\SEMICOLON}{\ensuremath{\mbox{\textbf{;}}}}
38 \newcommand{\BRANCH}{\ensuremath{\mbox{\textbf{[}}}}
39 \newcommand{\SHIFT}{\ensuremath{\mbox{\textbf{\textbar}}}}
40 \newcommand{\POS}[1]{\ensuremath{#1\mbox{\textbf{:}}}}
41 \newcommand{\MERGE}{\ensuremath{\mbox{\textbf{]}}}}
42 \newcommand{\FOCUS}[1]{\ensuremath{\mathtt{focus}~#1}}
43 \newcommand{\UNFOCUS}{\ensuremath{\mathtt{unfocus}}}
44 \newcommand{\SKIP}{\MATHTT{skip}}
45 \newcommand{\TACTIC}[1]{\ensuremath{\mathtt{tactic}~#1}}
46
47 \newcommand{\NT}[1]{\ensuremath{\langle\mathit{#1}\rangle}}
48 \newcommand{\URI}[1]{\texttt{#1}}
49 \newcommand{\OP}[1]{``\texttt{#1}''}
50 \newcommand{\FILE}[1]{\texttt{#1}}
51 \newcommand{\TAC}[1]{\texttt{#1}}
52 \newcommand{\TODO}[1]{\textbf{TODO: #1}}
53
54 \definecolor{gray}{gray}{0.85} % 1 -> white; 0 -> black
55
56 \newenvironment{grafite}{\VerbatimEnvironment
57  \begin{SaveVerbatim}{boxtmp}}%
58  {\end{SaveVerbatim}\setlength{\fboxsep}{3mm}%
59   \begin{center}
60    \fcolorbox{black}{gray}{\BUseVerbatim[boxwidth=0.9\linewidth]{boxtmp}}
61   \end{center}}
62
63 \newcounter{pass}
64 \newcommand{\PASS}{\stepcounter{pass}\arabic{pass}}
65
66 \newsavebox{\tmpxyz}
67 \newcommand{\sequent}[2]{
68   \savebox{\tmpxyz}[0.9\linewidth]{
69     \begin{minipage}{0.9\linewidth}
70       \ensuremath{#1} \\
71       \rule{3cm}{0.03cm}\\
72       \ensuremath{#2}
73     \end{minipage}}\setlength{\fboxsep}{3mm}%
74   \begin{center}
75    \fcolorbox{black}{gray}{\usebox{\tmpxyz}}
76   \end{center}}
77
78 \bibliographystyle{klunum}
79
80 \begin{document}
81
82 \begin{opening}
83  \title{The \MATITA{} Proof Assistant}
84
85  \author{Andrea \surname{Asperti} \email{asperti@cs.unibo.it}}
86  \author{Claudio \surname{Sacerdoti Coen} \email{sacerdot@cs.unibo.it}}
87  \author{Enrico \surname{Tassi} \email{tassi@cs.unibo.it}}
88  \author{Stefano \surname{Zacchiroli} \email{zacchiro@cs.unibo.it}}
89
90  \institute{Department of Computer Science, University of Bologna\\
91  Mura Anteo Zamboni, 7 --- 40127 Bologna, ITALY}
92
93  \runningtitle{The \MATITA{} proof assistant}
94  \runningauthor{Asperti, Sacerdoti Coen, Tassi, Zacchiroli}
95
96  \begin{motto}
97   ``We are nearly bug-free'' -- \emph{CSC, Oct 2005}
98  \end{motto}
99
100  \begin{abstract}
101   \TODO{scrivere abstract}
102  \end{abstract}
103
104  \keywords{Proof Assistant, Mathematical Knowledge Management, XML, Authoring,
105  Digital Libraries}
106 \end{opening}
107
108 % toc & co: to be removed in the final paper version
109 \tableofcontents
110 \listoffigures
111 \listoftables
112
113 \section{Introduction}
114 \label{sec:intro}
115
116 \MATITA{} is the Proof Assistant under development by the \HELM{}
117 team~\cite{mkm-helm} at the University of Bologna, under the direction of
118 Prof.~Asperti. This paper describes the overall architecture of
119 the system, focusing on its most distinctive and innovative 
120 features.
121
122 \subsection{Historical perspective}
123
124 The origins of \MATITA{} go back to 1999. At the time we were mostly 
125 interested in developing tools and techniques to enhance the accessibility
126 via Web of libraries of formalized mathematics. Due to its dimension, the
127 library of the \COQ~\cite{CoqManual} proof assistant (of the order of
128 35'000 theorems) 
129 was chosen as a privileged test bench for our work, although experiments
130 have been also conducted with other systems, and notably 
131 with \NUPRL~\cite{nuprl-book}.
132 The work, mostly performed in the framework of the recently concluded 
133 European project \MOWGLIIST{} \MOWGLI~\cite{pechino}, mainly consisted in the 
134 following steps:
135 \begin{enumerate}
136
137  \item exporting the information from the internal representation of
138   \COQ{} to a system and platform independent format. Since XML was at
139   the time an emerging standard, we naturally adopted that technology,
140   fostering a content-centric architecture~\cite{content-centric} where
141   the documents of the library were the the main components around which
142   everything else has to be built;
143
144  \item developing indexing and searching techniques supporting semantic
145   queries to the library; 
146
147  \item developing languages and tools for a high-quality notational
148   rendering of mathematical information.\footnote{We have been active in
149   the \MATHML{} Working group since 1999.} 
150
151 \end{enumerate}
152
153 According to our content-centric commitment, the library exported from
154 \COQ{} was conceived as being distributed and most of the tools were developed
155 as Web services. The user can interact with the library and the tools by
156 means of a Web interface that orchestrates the Web services.
157
158 Web services and other tools have been implemented as front-ends
159 to a set of software components, collectively called the \HELM{} components.
160 At the end of the \MOWGLI{} project we already disposed of the following
161 tools and software components:
162 \begin{itemize}
163
164  \item XML specifications for the Calculus of Inductive Constructions,
165   with components for parsing and saving mathematical objects in such a
166   format~\cite{exportation-module};
167
168  \item metadata specifications with components for indexing and querying the
169   XML knowledge base;
170
171  \item a proof checker (i.e. the \emph{kernel} of a proof assistant), 
172   implemented to check that we exported from the \COQ{} library all the 
173   logically relevant content;
174
175  \item a sophisticated term parser (used by the search engine), able to deal 
176   with potentially ambiguous and incomplete information, typical of the 
177   mathematical notation~\cite{disambiguation};
178
179  \item a \emph{refiner} component, i.e. a type inference system, based on
180   partially specified terms, used by the disambiguating parser;
181
182  \item complex transformation algorithms for proof rendering in natural
183   language~\cite{remathematization};
184
185  \item an innovative, \MATHML-compliant rendering widget~\cite{padovani}
186   for the \GTK{} graphical environment,\footnote{\url{http://www.gtk.org/}}
187   supporting high-quality bidimensional
188   rendering, and semantic selection, i.e. the possibility to select semantically
189   meaningful rendering expressions, and to paste the respective content into
190   a different text area.
191
192 \end{itemize}
193
194 Starting from all this, developing our own proof assistant was not
195 too far away: essentially, we ``just'' had to
196 add an authoring interface, and a set of functionalities for the
197 overall management of the library, integrating everything into a
198 single system. \MATITA{} is the result of this effort. 
199
200 \subsection{The system}
201
202 \MATITA{} is a proof assistant (also called interactive theorem prover).
203 It is based on the Calculus of (Co)Inductive Constructions
204 (CIC)~\cite{Werner} that is a dependently typed lambda-calculus \`a la
205 Church enriched with primitive inductive and co-inductive data types.
206 Via the Curry-Howard isomorphism, the calculus can be seen as a very
207 rich higher order logic and proofs can be simply represented and
208 stored as lambda-terms. \COQ{} and \LEGO~\cite{lego} are other systems
209 that adopt (variations of) CIC as their foundation.
210
211 The proof language of \MATITA{} is procedural, in the tradition of the LCF
212 theorem prover~\cite{lcf}. \COQ, \NUPRL, PVS, Isabelle are all examples of
213 others systems
214 whose proof language is procedural. Traditionally, in a procedural system
215 the user interacts only with the \emph{script}, while proof terms are internal
216 records kept by the system. On the contrary, in \MATITA{} proof terms are
217 praised as declarative versions of the proof. Playing that role, they are the
218 primary mean of communication of proofs (once rendered to natural language
219 for human audiences).
220
221 The user interfaces now adopted by all the proof assistants based on a
222 procedural proof language have been inspired by the CtCoq pioneering
223 system~\cite{ctcoq1}. One successful incarnation of the ideas introduced
224 by CtCoq is the Proof General generic interface~\cite{proofgeneral},
225 that has set a sort of
226 standard way to interact with the system. Several procedural proof assistants
227 have either adopted or cloned Proof General as their main user interface.
228 The authoring interface of \MATITA{} is a clone of the Proof General interface.
229 On the contrary, the interface to interact with the library is rather
230 innovative and directly inspired by the Web interfaces to our Web servers.
231
232 \MATITA{} is backward compatible with the XML library of proof objects exported
233 from \COQ{}, but, in order to test the actual usability of the system, we are
234 also developing a new library of basic results from scratch.
235
236 \subsection{Relationship with \COQ{}}
237
238 At first sight, \MATITA{} looks as (and partly is) a \COQ{} clone. This is
239 more the effect of the circumstances of its creation described 
240 above than the result of a deliberate design. In particular, we
241 (essentially) share the same foundational dialect of \COQ{} (the
242 Calculus of (Co)Inductive Constructions), the same implementation
243 language (\OCAML\footnote{\url{http://caml.inria.fr/}}),
244 and the same (procedural, script based) authoring philosophy.
245 However, the analogy essentially stops here and no code is shared
246 between the two systems.
247
248 In a sense, we like to think of \MATITA{} as the way \COQ{} would 
249 look like if entirely rewritten from scratch: just to give an
250 idea, although \MATITA{} currently supports almost all functionalities of
251 \COQ{}, it links 60'000 lines of \OCAML{} code, against the 166'000 lines linked
252 by \COQ{} (and we are convinced that, starting from scratch again,
253 we could reduce our code even further in a sensible way).
254
255 Moreover, the complexity of the code of \MATITA{} is greatly reduced with
256 respect to \COQ. For instance, the API of the components of \MATITA{} comprise
257 989 functions, to be compared with the 4'286 functions of \COQ.
258
259 Finally, \MATITA{} has several innovative features over \COQ{} that derive
260 from the integration of Mathematical Knowledge Management tools with proof
261 assistants. Among them, the advanced indexing tools over the library and
262 the parser for ambiguous mathematical notation.
263
264 The size and complexity improvements over \COQ{} must be understood
265 historically. \COQ~\cite{CoqArt} is a quite old
266 system whose development started 20 years ago. Since then,
267 several developers have took over the code and several new research ideas
268 that were not considered in the original architecture have been experimented
269 and integrated in the system. Moreover, there exists a lot of developments
270 for \COQ{} that require backward compatibility between each pair of releases;
271 since many central functionalities of a proof assistant are based on heuristics
272 or arbitrary choices to overcome undecidability (e.g. for higher order
273 unification), changing these functionalities maintaining backward compatibility
274 is very difficult. Finally, the code of \COQ{} has been greatly optimized
275 over the years; optimization reduces maintainability and rises the complexity
276 of the code.
277
278 In writing \MATITA{} we have not been hindered by backward compatibility and
279 we have took advantage of the research results and experiences previously
280 developed by others, comprising the authors of \COQ. Moreover, starting from
281 scratch, we have designed in advance the architecture and we have split
282 the code in coherent minimally coupled components.
283
284 In the future we plan to exploit \MATITA{} as a test bench for new ideas and
285 extensions. Keeping the single components and the whole architecture as
286 simple as possible is thus crucial to foster future experiments and to
287 allow other developers to quickly understand our code and contribute.
288
289 %For direct experience of the authors, the learning curve to understand and
290 %be able to contribute to \COQ{}'s code is quite steep and requires direct
291 %and frequent interactions with \COQ{} developers.
292
293 \section{Architecture}
294 \label{architettura}
295
296 \begin{figure}[!ht]
297  \begin{center}
298   \includegraphics[width=0.9\textwidth,height=0.8\textheight]{pics/libraries-clusters}
299   \caption[\MATITA{} components and related applications]{\MATITA{}
300    components and related applications, with thousands of line of
301    codes (klocs)\strut}
302   \label{fig:libraries}
303  \end{center}
304 \end{figure}
305
306 Fig.~\ref{fig:libraries} shows the architecture of the \emph{\components}
307 (circle nodes) and \emph{applications} (squared nodes) developed in the
308 \HELM{} project. Each node is annotated with the number of lines of
309 source code (comprising comments).
310
311 Applications and \components{} depend on other \components{} forming a
312 directed acyclic graph (DAG). Each \component{} can be decomposed in
313 a set of \emph{modules} also forming a DAG.
314
315 Modules and \components{} provide coherent sets of functionalities
316 at different scales. Applications that require only a few functionalities
317 depend on a restricted set of \components.
318
319 Only the proof assistant \MATITA{} and the \WHELP{} search engine are
320 applications meant to be used directly by the user. All the other applications
321 are Web services developed in the \HELM{} and \MOWGLI{} projects and already
322 described elsewhere. In particular:
323 \begin{itemize}
324
325  \item The \emph{\GETTER}~\cite{zack-master} is a Web service to
326   retrieve an (XML) document from a physical location (URL) given its
327   logical name (URI). The Getter is responsible of updating a table that
328   maps URIs to URLs. Thanks to the Getter it is possible to work on a
329   logically monolithic library that is physically distributed on the
330   network.
331
332  \item \emph{\WHELP}~\cite{whelp} is a search engine to index and
333   locate mathematical concepts (axioms, theorems, definitions) in the
334   logical library managed by the Getter. Typical examples of
335   \WHELP{} queries are those that search for a theorem that generalize or
336   instantiate a given formula, or that can be immediately applied to
337   prove a given goal. The output of Whelp is an XML document that lists
338   the URIs of a complete set of candidates that are likely to satisfy
339   the given query. The set is complete in the sense that no concept that
340   actually satisfies the query is thrown away. However, the query is
341   only approximated in the sense that false matches can be returned.
342
343  \item \emph{\UWOBO}~\cite{zack-master} is a Web service that, given the
344   URI of a mathematical concept in the distributed library, renders it
345   according to the user provided two dimensional mathematical notation.
346   \UWOBO{} may also inline the rendering of mathematical concepts into
347   arbitrary documents before returning them.  The Getter is used by
348   \UWOBO{} to retrieve the document to be rendered.
349
350  \item The \emph{Proof Checker}~\cite{zack-master} is a Web service
351   that, given the URI of a concept in the distributed library, checks its
352   correctness. Since the concept is likely to depend in an acyclic way
353   on other concepts, the proof checker is also responsible of building
354   in a top-down way the DAG of all dependencies, checking in turn every
355   concept for correctness.
356
357  \item The \emph{Dependency Analyzer}~\cite{zack-master} is a Web
358   service that can produce a textual or graphical representation of the
359   dependencies of a concept.
360
361 \end{itemize}
362
363 The dependency of a \component{} or application over another \component{} can
364 be satisfied by linking the \component{} in the same executable.
365 For those \components{} whose functionalities are also provided by the
366 aforementioned Web services, it is also possible to link stub code that
367 forwards the request to a remote Web service. For instance, the
368 \GETTER{} application is just a wrapper to the \GETTER{} \component{}
369 that allows it to be used as a Web service. \MATITA{} can directly link
370 the code of the \GETTER{} \component, or it can use a stub library with
371 the same API that forwards every request to the Web service.
372
373 To better understand the architecture of \MATITA{} and the role of each
374 \component, we can focus on the representation of the mathematical
375 information.  In CIC terms are used to represent mathematical formulae,
376 types and proofs. \MATITA{} is able to handle terms at four different
377 levels of specification. On each level it is possible to provide a
378 different set of functionalities. The four different levels are: fully
379 specified terms; partially specified terms; content level terms;
380 presentation level terms.
381
382 \subsection{Fully specified terms}
383 \label{sec:fullyintro}
384
385  \emph{Fully specified terms} are CIC terms where no information is
386    missing or left implicit. A fully specified term should be well-typed.
387    The mathematical concepts (axioms, definitions, theorems) that are stored
388    in our mathematical library are fully specified and well-typed terms.
389    Fully specified terms are extremely verbose (to make type-checking
390    decidable). Their syntax is fixed and does not resemble the usual
391    extendible mathematical notation. They are not meant for direct user
392    consumption.
393
394    The \texttt{cic} \component{} defines the data type that represents CIC terms
395    and provides a parser for terms stored in XML format.
396
397    The most important \component{} that deals with fully specified terms is
398    \texttt{cic\_proof\_checking}. It implements the procedure that verifies
399    if a fully specified term is well-typed. It also implements the
400    \emph{conversion} judgement that verifies if two given terms are
401    computationally equivalent (i.e. they share the same normal form).
402
403    Terms may reference other mathematical concepts in the library.
404    One commitment of our project is that the library should be physically
405    distributed. The \GETTER{} \component{} manages the distribution,
406    providing a mapping from logical names (URIs) to the physical location
407    of a concept (an URL). The \texttt{urimanager} \component{} provides the URI
408    data type and several utility functions over URIs. The
409    \texttt{cic\_proof\_checking} \component{} calls the \GETTER{}
410    \component{} every time it needs to retrieve the definition of a mathematical
411    concept referenced by a term that is being type-checked. 
412
413    The Proof Checker application is the Web service that provides an interface
414    to the \texttt{cic\_proof\_checking} \component.
415
416    We use metadata and a sort of crawler to index the mathematical concepts
417    in the distributed library. We are interested in retrieving a concept
418    by matching, instantiation or generalization of a user or system provided
419    mathematical formula. Thus we need to collect metadata over the fully
420    specified terms and to store the metadata in some kind of (relational)
421    database for later usage. The \texttt{hmysql} \component{} provides
422    a simplified
423    interface to a (possibly remote) MySQL\footnote{\url{http://www.mysql.com/}}
424    database system used to store the metadata.
425    The \texttt{metadata} \component{} defines the data type of the metadata
426    we are collecting and the functions that extracts the metadata from the
427    mathematical concepts (the main functionality of the crawler).
428    The \texttt{whelp} \component{} implements a search engine that performs
429    approximated queries by matching/instantiation/generalization. The queries
430    operate only on the metadata and do not involve any actual matching
431    (see the \texttt{cic\_unification} \component in
432    Sect.~\ref{sec:partiallyintro}). Not performing any actual matching
433    a query only returns a complete and hopefully small set of matching
434    candidates. The process that has issued the query is responsible of
435    actually retrieving from the distributed library the candidates to prune
436    out false matches if interested in doing so.
437
438    The \WHELP{} application is the Web service that provides an interface to
439    the \texttt{whelp} \component.
440
441    According to our vision, the library is developed collaboratively so that
442    changing or removing a concept can invalidate other concepts in the library.
443    Moreover, changing or removing a concept requires a corresponding change
444    in the metadata database. The \texttt{library} \component{} is responsible
445    of preserving the coherence of the library and the database. For instance,
446    when a concept is removed, all the concepts that depend on it and their
447    metadata are removed from the library. This aspect will be better detailed
448    in Sect.~\ref{sec:libmanagement}.
449    
450 \subsection{Partially specified terms}
451 \label{sec:partiallyintro}
452
453 \emph{Partially specified terms} are CIC terms where subterms can be omitted.
454 Omitted subterms can bear no information at all or they may be associated to
455 a sequent. The formers are called \emph{implicit terms} and they occur only
456 linearly. The latters may occur multiple times and are called
457 \emph{metavariables}~\cite{geuvers-jojgov,munoz}.
458 An \emph{explicit substitution} is applied to each
459 occurrence of a metavariable. A metavariable stands for a term whose type is
460 given by the conclusion of the sequent. The term must be closed in the
461 context that is given by the ordered list of hypotheses of the sequent.
462 The explicit substitution instantiates every hypothesis with an actual
463 value for the variable bound by the hypothesis.
464
465 Partially specified terms are not required to be well-typed. However a
466 partially specified term should be \emph{refinable}. A \emph{refiner} is
467 a type-inference procedure that can instantiate implicit terms and
468 metavariables and that can introduce
469 \emph{implicit coercions}~\cite{barthe95implicit} to make a
470 partially specified term well-typed. The refiner of \MATITA{} is implemented
471 in the \texttt{cic\_unification} \component. As the type checker is based on
472 the conversion check, the refiner is based on \emph{unification}~\cite{strecker}
473 that is a procedure that makes two partially specified term convertible by
474 instantiating as few as possible metavariables that occur in them.
475
476 Since terms are used in CIC to represent proofs, correct incomplete
477 proofs are represented by refinable partially specified terms. The metavariables
478 that occur in the proof correspond to the conjectures still to be proved.
479 The sequent associated to the metavariable is the conjecture the user needs to
480 prove.
481
482 \emph{Tactics} are the procedures that the user can apply to progress in the
483 proof. A tactic proves a conjecture possibly creating new (and hopefully
484 simpler) conjectures. The implementation of tactics is given in the
485 \texttt{tactics} \component. It is heavily based on the refinement and
486 unification procedures of the \texttt{cic\_unification} \component.
487
488 The \texttt{grafite} \component{} defines the abstract syntax tree (AST) for the
489 commands of the \MATITA{} proof assistant. Most of the commands are tactics.
490 Other commands are used to give definitions and axioms or to state theorems
491 and lemmas. The \texttt{grafite\_engine} \component{} is the core of \MATITA.
492 It implements the semantics of each command in the grafite AST as a function
493 from status to status.  It implements also an undo function to go back to
494 previous statuses.
495
496 As fully specified terms, partially specified terms are not well suited
497 for user consumption since their syntax is not extendible and it is not
498 possible to adopt the usual mathematical notation. However they are already
499 an improvement over fully specified terms since they allow to omit redundant
500 information that can be inferred by the refiner.
501
502 \subsection{Content level terms}
503 \label{sec:contentintro}
504
505 The language used to communicate proofs and especially formulae with the
506 user does not only needs to be extendible and accommodate the usual mathematical
507 notation. It must also reflect the comfortable degree of imprecision and
508 ambiguity that the mathematical language provides.
509
510 For instance, it is common practice in mathematics to speak of a generic
511 equality that can be used to compare any two terms. However, it is well known
512 that several equalities can be distinguished as soon as we care for decidability
513 or for their computational properties. For instance equality over real
514 numbers is well known to be undecidable, whereas it is decidable over
515 rational numbers.
516
517 Similarly, we usually speak of natural numbers and their operations and
518 properties without caring about their representation. However the computational
519 properties of addition over the binary representation are very different from
520 those of addition over the unary representation. And addition over two natural
521 numbers is definitely different from addition over two real numbers.
522
523 Formalized mathematics cannot hide these differences and obliges the user to be
524 very precise on the types he is using and their representation. However,
525 to communicate formulae with the user and with external tools, it seems good
526 practice to stick to the usual imprecise mathematical ontology. In the
527 Mathematical Knowledge Management community this imprecise language is called
528 the \emph{content level}~\cite{adams} representation of formulae.
529
530 In \MATITA{} we provide translations from partially specified terms
531 to content level terms and the other way around. The first translation can also
532 be applied to fully specified terms since a fully specified term is a special
533 case of partially specified term where no metavariable or implicit term occurs.
534
535 The translation from partially specified terms to content level terms must
536 discriminate between terms used to represent proofs and terms used to represent
537 formulae. The firsts are translated to a content level representation of
538 proof steps that can in turn easily be rendered in natural language
539 using techniques inspired by~\cite{natural,YANNTHESIS}. The representation
540 adopted has greatly influenced the OMDoc~\cite{omdoc} proof format that is now
541 isomorphic to it. Terms that represent formulae are translated to \MATHML{}
542 Content formulae. \MATHML{} Content~\cite{mathml} is a W3C standard
543 for the representation of content level formulae in an extensible XML format.
544
545 The translation to content level is implemented in the
546 \texttt{acic\_content} \component. Its input are \emph{annotated partially
547 specified terms}, that are maximally unshared
548 partially specified terms enriched with additional typing information for each
549 subterm. This information is used to discriminate between terms that represent
550 proofs and terms that represent formulae. Part of it is also stored at the
551 content level since it is required to generate the natural language rendering
552 of proofs. The terms need to be maximally unshared (i.e. they must be a tree
553 and not a DAG). The reason is that to different occurrences of a subterm
554 we need to associate different typing information.
555 This association is made easier when the term is represented as a tree since
556 it is possible to label each node with an unique identifier and associate
557 the typing information using a map on the identifiers.
558 The \texttt{cic\_acic} \component{} unshares and annotates terms. It is used
559 by the \texttt{library} \component{} since fully specified terms are stored
560 in the library in their annotated form.
561
562 We do not provide yet a reverse translation from content level proofs to
563 partially specified terms. But in \texttt{cic\_disambiguation} we do provide
564 the reverse translation for formulae. The mapping from
565 content level formulae to partially specified terms is not unique due to
566 the ambiguity of the content level. As a consequence the translation
567 is guided by an \emph{interpretation}, that is a function that chooses for
568 every ambiguous formula one partially specified term. The
569 \texttt{cic\_disambiguation} \component{} implements the
570 disambiguation algorithm presented in~\cite{disambiguation} that is
571 responsible of building in an efficient way the set of all correct
572 interpretations. An interpretation is correct if the partially specified term
573 obtained using the interpretation is refinable.
574
575 In Sect.~\ref{sec:partiallyintro} we described the semantics of
576 a command as a
577 function from status to status. We also hinted that the formulae in a
578 command are encoded as partially specified terms. However, consider the
579 command ``\texttt{replace} $x$ \texttt{with} $y^2$''. Until the occurrence
580 of $x$ to be replaced is located, its context is unknown. Since $y^2$ must
581 replace $x$ in that context, its encoding as a term cannot be computed
582 until $x$ is located. In other words, $y^2$ must be disambiguated in the
583 context of the occurrence $x$ it must replace.
584
585 The elegant solution we have implemented consists in representing terms
586 in a command as functions from a context to a partially refined term. The
587 function is obtained by partially applying our disambiguation function to
588 the content level term to be disambiguated. Our solution should be compared with
589 the one adopted in the \COQ{} system, where ambiguity is only relative to
590 De Brujin indexes.
591 In \COQ, variables can be bound either by name or by position. A term
592 occurring in a command has all its variables bound by name to avoid the need of
593 a context during disambiguation.  This makes more complex every
594 operation over terms (i.e. according to our architecture every module that
595 depends on \texttt{cic}) since the code must deal consistently with both kinds
596 of binding. Moreover, this solution cannot cope with other forms of ambiguity
597 (as the context dependent meaning of the exponent in the previous example).
598
599 \subsection{Presentation level terms}
600 \label{sec:presentationintro}
601
602 Content level terms are a sort of abstract syntax trees for mathematical
603 formulae and proofs. The concrete syntax given to these abstract trees
604 is called \emph{presentation level}.
605
606 The main important difference between the content level language and the
607 presentation level language is that only the former is extendible. Indeed,
608 the presentation level language is a finite language that comprises all
609 the usual mathematical symbols. Mathematicians invent new notions every
610 single day, but they stick to a set of symbols that is more or less fixed.
611
612 The fact that the presentation language is finite allows the definition of
613 standard languages. In particular, for formulae we have adopt \MATHML{}
614 Presentation~\cite{mathml} that is an XML dialect standardized by the W3C. To
615 visually
616 represent proofs it is enough to embed formulae in plain text enriched with
617 formatting boxes. Since the language of formatting boxes is very simple,
618 many equivalent specifications exist and we have adopted our own, called
619 \BOXML.
620
621 The \texttt{content\_pres} \component{} contains the implementation of the
622 translation from content level terms to presentation level terms. The
623 rendering of presentation level terms is left to the application that uses
624 the \component. However, in the \texttt{hgdome} \component{} we provide a few
625 utility functions to build a \GDOME~\cite{gdome2} \MATHML+\BOXML{} tree from our
626 presentation
627 level terms. \GDOME{} \MATHML+\BOXML{} trees can be rendered by the
628 \GTKMATHVIEW{}
629 widget developed by Luca Padovani~\cite{padovani}. The widget is
630 particularly interesting since it allows the implementation of \emph{semantic
631 selection}.
632
633 Semantic selection is a technique that consists in enriching the presentation
634 level terms with pointers to the content level terms and to the partially
635 specified terms they correspond to. Highlight of formulae in the widget is
636 constrained to selection of meaningful expressions, i.e. expressions that
637 correspond to a lower level term, that is a content term or a partially or
638 fully specified term.
639 Once the rendering of a lower level term is
640 selected it is possible for the application to retrieve the pointer to the
641 lower level term. An example of applications of semantic selection is
642 \emph{semantic copy \& paste}: the user can select an expression and paste it
643 elsewhere preserving its semantics (i.e. the partially specified term),
644 possibly performing some semantic transformation over it (e.g. renaming
645 variables that would be captured or lambda-lifting free variables).
646
647 The reverse translation from presentation level terms to content level terms
648 is implemented by a parser that is also found in \texttt{content\_pres}.
649 Differently from the translation from content level terms to partially
650 refined terms, this translation is not ambiguous. The reason is that the
651 parsing tool we have adopted (CamlP4) is not able to parse ambiguous
652 grammars. Thus we require the mapping from presentation level terms
653 (concrete syntax) to content level terms (abstract syntax) to be unique.
654 This means that the user must fix once and for all the associativity and
655 precedence level of every operator he is using. In practice this limitation
656 does not seem too strong. The reason is that the target of the
657 translation is an ambiguous language and the user is free to associate
658 to every content level term several different interpretations (as a
659 partially specified term).
660
661 Both the direct and reverse translation from presentation to content level
662 terms are parameterized over the user provided mathematical notation. 
663 The \texttt{lexicon} \component{} is responsible of managing the lexicon,
664 that is the set of active notations. It defines an abstract syntax tree
665 of commands to declare and activate new notations and it implements the
666 semantics of these commands. It also implements undoing of the semantic
667 actions. Among the commands there are hints to the
668 disambiguation algorithm that are used to control and speed up disambiguation.
669 These mechanisms will be further discussed in Sect.~\ref{sec:disambiguation}.
670
671 Finally, the \texttt{grafite\_parser} \component{} implements a parser for
672 the concrete syntax of the commands of \MATITA. The parser process a stream
673 of characters and returns a stream of abstract syntax trees (the ones
674 defined by the \texttt{grafite} component and whose semantics is given
675 by \texttt{grafite\_engine}). When the parser meets a command that changes
676 the lexicon, it invokes the \texttt{lexicon} \component{} to immediately
677 process the command. When the parser needs to parse a term at the presentation
678 level, it invokes the already described parser for terms contained in
679 \texttt{content\_pres}.
680
681 The \MATITA{} proof assistant and the \WHELP{} search engine are both linked
682 against the \texttt{grafite\_parser} \components{}
683 since they provide an interface to the user. In both cases the formulae
684 written by the user are parsed using the \texttt{content\_pres} \component{} and
685 then disambiguated using the \texttt{cic\_disambiguation} \component.  However,
686 only \MATITA{} is linked against the \texttt{grafite\_engine} and
687 \texttt{tactics} components (summing up to a total of 11'200 lines of code)
688 since \WHELP{} can only execute those ASTs that correspond to queries
689 (implemented in the \texttt{whelp} component).
690
691 The \UWOBO{} Web service wraps the \texttt{content\_pres} \component,
692 providing a rendering service for the documents in the distributed library.
693 To render a document given its URI, \UWOBO{} retrieves it using the
694 \GETTER{} obtaining a document with fully specified terms. Then it translates
695 it to the presentation level passing through the content level. Finally
696 it returns the result document to be rendered by the user's
697 browser.
698
699 The \components{} not yet described (\texttt{extlib}, \texttt{xml},
700 \texttt{logger}, \texttt{registry} and \texttt{utf8\_macros}) are 
701 minor \components{} that provide a core of useful functions and basic
702 services missing from the standard library of the programming language.
703 %In particular, the \texttt{xml} \component{} is used to easily represent,
704 %parse and pretty-print XML files.
705
706 \section{The interface to the library}
707 \label{sec:library}
708
709 A proof assistant provides both an interface to interact with its library and
710 an \emph{authoring} interface to develop new proofs and theories. According
711 to its historical origins, \MATITA{} strives to provide innovative
712 functionalities for the interaction with the library. It is more traditional
713 in its script based authoring interface. In the remaining part of the paper we
714 focus on the user view of \MATITA.
715
716 The library of \MATITA{} comprises mathematical concepts (theorems,
717 axioms, definitions) and notation. The concepts are authored sequentially
718 using scripts that are (ordered) sequences of procedural commands.
719 Once they are produced we store them independently in the library.
720 The only relation implicitly kept between the concepts are the logical,
721 acyclic dependencies among them. This way the library forms a global (and
722 distributed) hypertext.
723
724 \begin{figure}[!ht]
725  \begin{center}
726   \includegraphics[width=0.45\textwidth]{pics/cicbrowser-screenshot-browsing}
727   \hspace{0.05\textwidth}
728   \includegraphics[width=0.45\textwidth]{pics/cicbrowser-screenshot-query}
729   \caption{Browsing and searching the library\strut}
730   \label{fig:cicbrowser1}
731  \end{center}
732 \end{figure}
733
734 \begin{figure}[!ht]
735  \begin{center}
736   \includegraphics[width=0.70\textwidth]{pics/cicbrowser-screenshot-con}
737   \caption[Natural language rendering]{Natural language rendering of a theorem
738   from the library\strut}
739   \label{fig:cicbrowser2}
740  \end{center}
741 \end{figure}
742
743 Several useful operations can be implemented on the library only,
744 regardless of the scripts. For instance, searching and browsing is
745 implemented by the ``cicBrowser'' window available from the \MATITA{}
746 GUI. Using it, the hierarchical structure of the library can be
747 explored (on the left of Fig.~\ref{fig:cicbrowser1}), the natural
748 language rendering of proofs can be inspected
749 (Fig.~\ref{fig:cicbrowser2}), and content based searches on the
750 library can be performed (on the right of Fig.~\ref{fig:cicbrowser1}).
751 Content based searches are described in
752 Sect.~\ref{sec:indexing}.  Other examples of library operations are
753 disambiguation of content level terms (see
754 Sect.~\ref{sec:disambiguation}) and automatic proof searching (see
755 Sect.~\ref{sec:automation}).
756
757 The key requisite for the previous operations is that the library must
758 be fully accessible and in a logically consistent state. To preserve
759 consistency, a concept cannot be altered or removed unless the part of the
760 library that depends on it is modified accordingly. To allow incremental
761 changes and cooperative development, consistent revisions are necessary.
762 For instance, to modify a definition, the user could fork a new version
763 of the library where the definition is updated and all the concepts that
764 used to rely on it are absent. The user is then responsible to restore
765 the removed part in the new branch, merging the branch when the library is
766 fully restored.
767
768 To implement the proposed versioning system on top of a standard one
769 it is necessary to implement \emph{invalidation} first. Invalidation
770 is the operation that locates and removes from the library all the concepts
771 that depend on a given one. As described in Sect.~\ref{sec:libmanagement} removing
772 a concept from the library also involves deleting its metadata from the
773 database.
774
775 For non collaborative development, full versioning can be avoided, but
776 invalidation is still required. Since nobody else is relying on the
777 user development, the user is free to change and invalidate part of the library
778 without branching. Invalidation is still necessary to avoid using a
779 concept that is no longer valid.
780 So far, in \MATITA{} we address only this non collaborative scenario
781 (see Sect.~\ref{sec:libmanagement}). Collaborative development and versioning
782 is still under design.
783
784 Scripts are not seen as constituents of the library. They are not published
785 and indexed, so they cannot be searched or browsed using \HELM{} tools.
786 However, they play a central role for the maintenance of the library.
787 Indeed, once a concept is invalidated, the only way to restore it is to
788 fix the possibly broken script that used to generate it.
789 Moreover, during the authoring phase, scripts are a natural way to
790 group concepts together. They also constitute a less fine grained clustering
791 of concepts for invalidation.
792
793 In the rest of this section we present in more details the functionalities of
794 \MATITA{} related to library management and exploitation.
795 Sect.~\ref{sec:authoring} is devoted to the description of the peculiarities of
796 the \MATITA{} authoring interface.
797
798 \subsection{Indexing and searching}
799 \label{sec:indexing}
800
801 The \MATITA{} system is first of all an interface between the user and
802 the mathematical library. For this reason, it is important to be
803 able to search and retrieve mathematical concepts in a quick and 
804 effective way, assuming as little knowledge as possible about the 
805 library. To this aim, \MATITA{} uses a sophisticated indexing mechanism
806 for mathematical concepts, based on a rich metadata set that has been 
807 tuned along the European project \MOWGLIIST{} \MOWGLI. The metadata
808 set, and the searching facilities built on top of them --- collected 
809 in the so called \WHELP{} search engine --- have been
810 extensively described in~\cite{whelp}. Let us just recall here that
811 the \WHELP{} metadata model is essentially based a single ternary relation 
812 \REF{p}{s}{t} stating that a concept $s$ refers a concept $t$ at a
813 given position $p$, where the position specify the place of the 
814 occurrence of $t$ inside $s$ (we currently work with a fixed set of 
815 positions, discriminating the hypothesis from the conclusion and
816 outermost form innermost occurrences). This approach is extremely 
817 flexible, since extending the set of positions 
818 we may improve the granularity and the precision of our indexing technique,
819 with no additional architectural impact.
820
821 Every time a new mathematical concept is created and saved by the user it gets 
822 indexed, and becomes immediately visible in the library. Several 
823 interesting and innovative features of \MATITA{} described in the following
824 sections rely in a direct or indirect way on its metadata system and
825 the search features. Here, we shall just recall some of its most
826 direct applications.
827
828 A first, very simple but not negligible feature is the \emph{duplicate check}.
829 As soon as a theorem is stated, just before starting its proof, 
830 the library is searched 
831 to check that no other equivalent statement has been already proved
832 (based on the pattern matching functionality of \WHELP); if this is the case,
833 a warning is raised to the user. At present, the notion of equivalence 
834 adopted by \MATITA{} is convertibility, but we may imagine to weaken it 
835 in the future, covering for instance isomorphisms.    
836
837 Another useful \WHELP{} operation is \HINT; we may invoke this query
838 at any moment during the authoring of a proof, resulting in the list
839 of all theorems of the library which can be applied to the current
840 goal. In practice, this is mostly used not really to discover what theorems
841 can be applied to a given goal, but to actually retrieve a theorem that 
842 we wish to apply, but whose name we have forgotten.
843 In fact, even if \MATITA{} adopts a semi-rigid naming convention for 
844 statements (see Sect.~\ref{sec:naming}) that greatly simplifies the effort
845 of recalling names, the naming discipline remains one of the most
846 annoying aspects of formal developments, and \HINT{} provides
847 a very friendly solution.
848
849 In the near future, we expect to extend the \HINT{} query to
850 a \REWRITEHINT, resulting in all equational statements that
851 can be applied to rewrite the current goal.
852
853 \subsection{Disambiguation}
854 \label{sec:disambiguation}
855
856 Software applications that involve input of mathematical content should strive
857 to require the user as less drift from informal mathematics as possible. We
858 believe this to be a fundamental aspect of such applications user interfaces.
859 Being that drift in general very large when inputing
860 proofs~\cite{debrujinfactor}, in \MATITA{} we achieved good results for
861 mathematical formulae which can be input using a \TeX-like encoding (the
862 concrete syntax corresponding to presentation level terms) and are then
863 translated (in multiple steps) to partially specified terms as sketched in
864 Sect.~\ref{sec:contentintro}.
865
866 The key ingredient of the translation is the generic disambiguation algorithm
867 implemented in the \texttt{disambiguation} component of Fig.~\ref{fig:libraries}
868 and presented in~\cite{disambiguation}. In this section we detail how to use
869 that algorithm in the context of the development of a library of formalized
870 mathematics. We will see that using multiple passes of the algorithm, varying
871 some of its parameters, helps in keeping the input terse without sacrificing
872 expressiveness.
873
874 \subsubsection{Disambiguation aliases}
875 \label{sec:disambaliases}
876
877 Consider the following command that states a theorem over integer numbers:
878
879 \begin{grafite}
880 theorem Zlt_compat:
881   \forall x,y,z. x < y \to y < z \to x < z.
882 \end{grafite}
883
884 The symbol \OP{<} is likely to be overloaded in the library
885 (at least over natural numbers). 
886 Thus, according to the disambiguation algorithm, two different
887 refinable partially specified terms could be associated to it.
888 \MATITA{} asks the user what interpretation he meant. However, to avoid
889 posing the same question in case of a future re-execution (e.g. undo/redo),
890 the choice must be recorded. Since scripts need to be re-executed after
891 invalidation, the choice record must be permanently stored somewhere. The most
892 natural place is the script itself.
893
894 In \MATITA{} disambiguation is governed by \emph{disambiguation aliases}.
895 They are mappings, stored in the library, from ambiguity sources
896 (identifiers, symbols and literal numbers at the content level) to partially
897 specified terms. In case of overloaded sources there exists multiple aliases
898 with the same source. It is possible to record \emph{disambiguation
899 preferences} to select one of the aliases of an overloaded source.
900
901 Preferences can be explicitly given in the script (using the
902 misleading \texttt{alias} commands), but
903 are also implicitly added when a new concept is introduced (\emph{implicit
904 preferences}) or after a successful disambiguation that did not require
905 user interaction. Explicit preferences are added automatically by \MATITA{} to
906 record the disambiguation choices of the user. For instance, after the
907 disambiguation of the command above, the script is altered as follows:
908
909 \begin{grafite}
910 alias symbol "lt" = "integer 'less than'".
911 theorem Zlt_compat:
912   \forall x,y,z. x < y \to y < z \to x < z.
913 \end{grafite}
914
915 The ``alias'' command in the example sets the preferred alias for the
916 \OP{lt} symbol.
917
918 Implicit preferences for new concepts are set since a concept just defined is
919 likely to be the preferred one in the rest of the script. Implicit preferences
920 learned from disambiguation of previous commands grant the coherence of
921 the disambiguation in the rest of the script and speed up disambiguation
922 reducing the search space.
923
924 Disambiguation preferences are included in the lexicon status
925 (see Sect.~\ref{sec:presentationintro}) that is part of the authoring interface
926 status.  Unlike aliases, they are not part of the library.
927
928 When starting a new authoring session the set of disambiguation preferences
929 is empty. Until it contains a preference for each overloaded symbol to be
930 used in the script, the user can be faced with questions from the disambiguator.
931 To reduce the likelihood of user interactions, we introduced
932 the \texttt{include} command. With \texttt{include} it is possible to import
933 at once in the current session the set of preferences that was in effect
934 at the end of the execution of a given script.
935
936 Preferences can be changed. For instance, at the start of the development
937 of integer numbers the preference for the symbol \OP{<} is likely
938 to be the one over natural numbers; sooner or later it will be set to the one
939 over integer numbers.
940
941 Nothing forbids the set of preferences to become incoherent. For this reason
942 the disambiguator cannot always respect the user preferences.
943 Consider, for example:
944 \begin{grafite}
945 theorem Zlt_mono:
946   \forall x,y, k. x < y \to x < y + k.
947 \end{grafite}
948
949 No refinable partially specified term corresponds to the preferences:
950 \OP{+} over natural numbers, \OP{<} over integer numbers. To overcome this
951 limitation we organized disambiguation in \emph{multiple passes}: when the
952 disambiguator fails, disambiguation is tried again with a less strict set of
953 preferences.
954
955 Several disambiguation parameters can vary among passes. With respect to
956 preference handling we implemented three passes.  In the first pass, called
957 \emph{mono-preferences}, we consider only the aliases corresponding to the
958 current set of preferences.  In the second pass, called
959 \emph{multi-preferences}, we
960 consider every alias corresponding to a current or past preference.  For
961 instance, in the example above disambiguation succeeds in the multi-preference
962 pass. In the third pass, called \emph{library-preferences}, all aliases
963 available in the library are considered.
964
965 The rationale behind this choice is trying to respect user preferences in early
966 passes that complete quickly in case of failure; later passes are slower but
967 have more chances of success.
968
969 \subsubsection{Operator instances}
970 \label{sec:disambinstances}
971
972 Consider now the following theorem:
973 \begin{grafite}
974 theorem lt_to_Zlt_pos_pos:
975   \forall n,m: nat. n < m \to pos n < pos m. 
976 \end{grafite}
977 and assume that there exist in the library aliases for \OP{<} over natural
978 numbers and over integer numbers. None of the passes described above is able to
979 disambiguate \texttt{lt\_to\_Zlt\_pos\_pos}, no matter how preferences are set.
980 This is because the \OP{<} operator occurs twice in the content level term (it
981 has two \emph{instances}) and two different interpretations for it have to be
982 used in order to obtain a refinable partially specified term.
983
984 To address this issue, we have the ability to consider each instance of a single
985 symbol as a different ambiguous expression in the content level term,
986 enabling the use of a different alias for each of them.
987 Exploiting or not this feature is
988 one of the disambiguation pass parameters. A disambiguation pass which exploit
989 it is said to be using \emph{fresh instances} (opposed to a \emph{shared
990 instances} pass).
991
992 Fresh instances lead to a non negligible performance loss (since the choice of
993 an alias for one instance does not constraint the choice of the others). For
994 this reason we always attempt a fresh instances pass only after attempting a
995 shared instances pass.
996
997 \paragraph{One-shot preferences} Disambiguation preferences as seen so far are
998 instance-independent. However, implicit preferences obtained as a result of a
999 disambiguation pass which uses fresh instances ought to be instance-dependent.
1000 Informally, the set of preferences that can be respected by the disambiguator on
1001 the theorem above is: ``the first instance of the \OP{<} symbol is over natural
1002 numbers, while the second is on integer numbers''.
1003
1004 Instance-dependent preferences are meaningful only for the term whose
1005 disambiguation generated it. For this reason we call them \emph{one-shot
1006 preferences} and \MATITA{} does not use them to disambiguate further terms in
1007 the script.
1008
1009 \subsubsection{Implicit coercions}
1010 \label{sec:disambcoercions}
1011
1012 Consider the following theorem about derivation:
1013 \begin{grafite}
1014 theorem power_deriv:
1015   \forall n: nat, x: R. d x^n dx = n * x^(n - 1).
1016 \end{grafite}
1017 and assume that in the library there is an alias mapping \OP{\^} to a partially
1018 specified term having type: \texttt{R \TEXMACRO{to} nat \TEXMACRO{to} R}. In
1019 order to disambiguate \texttt{power\_deriv}, the occurrence of \texttt{n} on the
1020 right hand side of the equality need to be ``injected'' from \texttt{nat} to
1021 \texttt{R}.  The refiner of \MATITA{} supports
1022 \emph{implicit coercions}~\cite{barthe95implicit} for
1023 this reason: given as input the above presentation level term, it will return a
1024 partially specified term where in place of \texttt{n} the application of a
1025 coercion from \texttt{nat} to \texttt{R} appears (assuming such a coercion has
1026 been defined in advance).
1027
1028 Implicit coercions are not always desirable. For example, in disambiguating
1029 \texttt{\TEXMACRO{forall} x: nat. n < n + 1} we do not want the term which uses
1030 2 coercions from \texttt{nat} to \texttt{R} around \OP{<} arguments to show up
1031 among the possible partially specified term choices. For this reason we always
1032 attempt a disambiguation pass which require the refiner not to use the coercions
1033 before attempting a coercion-enabled pass.
1034
1035 The choice of whether implicit coercions are enabled or not interact with the
1036 choice about operator instances. Indeed, consider again
1037 \texttt{lt\_to\_Zlt\_pos\_pos}, which can be disambiguated using fresh operator
1038 instances. In case there exists a coercion from natural numbers to (positive)
1039 integers (which indeed does), the
1040 theorem can be disambiguated using twice that coercion on the left hand side of
1041 the implication. The obtained partially specified term however would not
1042 probably be the expected one, being a theorem which proves a trivial
1043 implication.
1044 Motivated by this and similar examples we choose to always prefer fresh
1045 instances over implicit coercions, i.e.  we always attempt disambiguation
1046 passes with fresh instances
1047 and no implicit coercions before attempting passes with implicit coercions.
1048
1049 \subsubsection{Disambiguation passes}
1050 \label{sec:disambpasses}
1051
1052 According to the criteria described above, in \MATITA{} we perform the
1053 disambiguation passes depicted in Tab.~\ref{tab:disambpasses}. In
1054 our experience that choice gives reasonable performance and minimizes the need
1055 of user interaction during the disambiguation.
1056
1057 \begin{table}[ht]
1058  \caption{Disambiguation passes sequence\strut}
1059  \label{tab:disambpasses} 
1060  \begin{center}
1061   \begin{tabular}{c|c|c|c}
1062    \multicolumn{1}{p{1.5cm}|}{\centering\raisebox{-1.5ex}{\textbf{Pass}}}
1063    & \multicolumn{1}{p{3.1cm}|}{\centering\textbf{Preferences}}
1064    & \multicolumn{1}{p{2.5cm}|}{\centering\textbf{Operator instances}}
1065    & \multicolumn{1}{p{2.5cm}}{\centering\textbf{Implicit coercions}} \\
1066    \hline
1067    \PASS & Mono-preferences     & Shared instances  & Disabled \\
1068    \PASS & Multi-preferences    & Shared instances  & Disabled \\
1069    \PASS & Mono-preferences     & Fresh instances   & Disabled \\
1070    \PASS & Multi-preferences    & Fresh instances   & Disabled \\
1071    \PASS & Mono-preferences     & Fresh instances   & Enabled  \\
1072    \PASS & Multi-preferences    & Fresh instances   & Enabled  \\
1073    \PASS & Library-preferences  & Fresh instances   & Enabled
1074   \end{tabular}
1075  \end{center}
1076 \end{table}
1077
1078 \subsection{Generation and invalidation}
1079 \label{sec:libmanagement}
1080
1081 %The aim of this section is to describe the way \MATITA{} 
1082 %preserves the consistency and the availability of the library
1083 %using the \WHELP{} technology, in response to the user alteration or 
1084 %removal of mathematical objects.
1085 %
1086 %As already sketched in Sect.~\ref{sec:fullyintro} what we generate 
1087 %from a script is split among two storage media, a
1088 %classical filesystem and a relational database. The former is used to
1089 %store the XML encoding of the objects defined in the script, the
1090 %disambiguation aliases and the interpretation and notational convention defined,
1091 %while the latter is used to store all the metadata needed by
1092 %\WHELP.
1093 %
1094 %While the consistency of the data store in the two media has
1095 %nothing to do with the nature of
1096 %the content of the library and is thus uninteresting (but really
1097 %tedious to implement and keep bug-free), there is a deeper
1098 %notion of mathematical consistency we need to provide. Each object
1099 %must reference only defined object (i.e. each proof must use only
1100 %already proved theorems). 
1101
1102 In this section we will focus on how \MATITA{} ensures the library 
1103 consistency during the formalization of a mathematical theory, 
1104 giving the user the freedom of adding, removing, modifying objects
1105 without loosing the feeling of an always visible and browsable
1106 library.
1107
1108 \subsubsection{Invalidation}
1109
1110 Invalidation (see Sect.~\ref{sec:library}) is implemented in two phases.
1111
1112 The first one is the calculation of all the concepts that recursively
1113 depend on the ones we are invalidating. It can be performed
1114 using the relational database that stores the metadata.
1115 This technique is the same used by the \emph{Dependency Analyzer}
1116 and is described in~\cite{zack-master}.
1117
1118 The second phase is the removal of all the results of the generation,
1119 metadata included.
1120
1121 \subsubsection{Regeneration}
1122
1123 %The typechecker component guarantees that if an object is well typed
1124 %it depends only on well typed objects available in the library,
1125 %that is exactly what we need to be sure that the logic consistency of
1126 %the library is preserved.
1127
1128 To regenerate an invalidated part of the library \MATITA{} re-executes
1129 the scripts that produced the invalidated concepts.  The main 
1130 problem is to find a suitable order of execution of the scripts.
1131
1132 For this purpose we provide a tool called \MATITADEP{}
1133 that takes in input the list of scripts that compose the development and
1134 outputs their dependencies in a format suitable for the GNU \texttt{make}
1135 tool.\footnote{\url{http://www.gnu.org/software/make/}}
1136 The user is not asked to run \MATITADEP{} by hand, but
1137 simply to tell \MATITA{} the root directory of his development (where all
1138 script files can be found) and \MATITA{} will handle all the generation
1139 related tasks, including dependencies calculation.
1140
1141 To compute dependencies it is enough to look at the script files for
1142 literal of included explicit disambiguation preferences
1143 (see Sect.~\ref{sec:disambaliases}). 
1144
1145 The re-execution of a script to regenerate part of the library
1146 requires the preliminary invalidation of the concepts generated by the
1147 script.
1148
1149 \subsubsection{Batch vs Interactive}
1150
1151 \MATITA{} includes an interactive authoring interface and a batch
1152 ``compiler'' (\MATITAC). 
1153
1154 Only the former is intended to be used directly by the
1155 user, the latter is automatically invoked by \MATITA{}
1156 to regenerate parts of the library previously invalidated.
1157
1158 While they share the same engine for generation and invalidation, they
1159 provide different granularity. \MATITAC{} is only able to re-execute a
1160 whole script and similarly to invalidate all the concepts generated
1161 by a script (together with all the other scripts that rely on a concept defined
1162 in it). 
1163
1164 \subsection{Automation}
1165 \label{sec:automation}
1166
1167 In the long run, one would expect to work with a proof assistant 
1168 like \MATITA, using only three basic tactics: \TAC{intro}, \TAC{elim},
1169 and \TAC{auto}
1170 (possibly integrated by a moderate use of \TAC{cut}). The state of the art
1171 in automated deduction is still far away from this goal, but 
1172 this is one of the main development direction of \MATITA. 
1173
1174 Even in this field, the underlying philosophy of \MATITA{} is to 
1175 free the user from any burden relative to the overall management
1176 of the library. For instance, in \COQ, the user is responsible to 
1177 define small collections of theorems to be used as a parameter 
1178 by the \TAC{auto} tactic;
1179 in \MATITA, it is the system itself that automatically retrieves, from
1180 the whole library, a subset of theorems worth to be considered 
1181 according to the signature of the current goal and context. 
1182
1183 The basic tactic merely iterates the use of the \TAC{apply} tactic
1184 (with no \TAC{intro}). The search tree may be pruned according to two
1185 main parameters: the \emph{depth} (whit the obvious meaning), and the 
1186 \emph{width} that is the maximum number of (new) open goals allowed at
1187 any instant. \MATITA{} has only one notion of metavariable, corresponding
1188 to the so called existential variables of \COQ; so, \MATITA's \TAC{auto}
1189 tactic should be compared with \COQ's \TAC{EAuto} tactic.
1190
1191 Recently we have extended automation with paramodulation based 
1192 techniques. At present, the system works reasonably well with
1193 equational rewriting, where the notion of equality is parametric
1194 and can be specified by the user: the system only requires 
1195 a proof of {\em reflexivity} and {\em paramodulation} (or rewriting, 
1196 as it is usually called in the proof assistant community).
1197
1198 Given an equational goal, \MATITA{} recovers all known equational facts
1199 from the library (and the local context), applying a variant of
1200 the so called {\em given-clause algorithm}~\cite{paramodulation}, 
1201 that is the the procedure currently used by the majority of modern
1202 automatic theorem provers. 
1203
1204 The given-clause algorithm is essentially composed by an alternation
1205 of a \emph{saturation} phase and a \emph{demodulation} phase.
1206 The former derives new facts by a set of active
1207 facts and a new \emph{given} clause suitably selected from a set of passive
1208 equations. The latter tries to simplify the equations
1209 orienting them according to a suitable weight associated to terms.
1210 \MATITA{} currently supports several different weighting functions
1211 comprising Knuth-Bendix ordering (kbo) and recursive path ordering (rpo), 
1212 that integrates particularly well with normalization.
1213
1214 Demodulation alone is already a quite powerful technique, and 
1215 it has been turned into a tactic by itself: the \TAC{demodulate}
1216 tactic, which can be seen as a kind of generalization of \TAC{simplify}. 
1217 The following portion of script describes two
1218 interesting cases of application of this tactic (both of them relying 
1219 on elementary arithmetic equations):
1220
1221 \begin{grafite}
1222 theorem example1: 
1223   \forall x: nat. (x+1)*(x-1) = x*x - 1.
1224 intro.
1225 apply (nat_case x);
1226   [ simplify; reflexivity
1227   | intro; demodulate; reflexivity ]
1228 qed.
1229 \end{grafite}
1230
1231 \begin{grafite}
1232 theorem example2: 
1233   \forall x,y: nat. (x+y)*(x+y) = x*x + 2*x*y + y*y.
1234 intros; demodulate; reflexivity
1235 qed.
1236 \end{grafite}
1237
1238 In the future we expect to integrate applicative and equational 
1239 rewriting. In particular, the overall idea would be to integrate
1240 applicative rewriting with demodulation, treating saturation as an
1241 operation to be performed in batch mode, e.g. during the night. 
1242
1243 \subsection{Naming convention}
1244 \label{sec:naming}
1245
1246 A minor but not entirely negligible aspect of \MATITA{} is that of
1247 adopting a (semi)-rigid naming convention for concept names, derived by 
1248 our studies about metadata for statements. 
1249 The convention is only applied to theorems 
1250 (not definitions), and relates theorem names to their statements.
1251 The basic rules are the following:
1252 \begin{itemize}
1253
1254  \item each name is composed by an ordered list of (short)
1255   identifiers occurring in a left to right traversal of the statement; 
1256
1257  \item all names should (but this is not strictly compulsory) 
1258   separated by an underscore;
1259
1260  \item names occurring in two different hypotheses, or in an hypothesis
1261   and in the conclusion must be separated by the string \texttt{\_to\_};
1262
1263  \item the identifier may be followed by a numerical suffix, or a
1264   single or double apostrophe.
1265
1266 \end{itemize}
1267
1268 Take for instance the statement:
1269 \begin{grafite}
1270 \forall n: nat. n = plus n O
1271 \end{grafite}
1272 Possible legal names are: \texttt{plus\_n\_O}, \texttt{plus\_O}, 
1273 \texttt{eq\_n\_plus\_n\_O} and so on.
1274
1275 Similarly, consider the statement
1276 \begin{grafite}
1277 \forall n,m: nat. n < m to n \leq m
1278 \end{grafite}
1279 In this case \texttt{lt\_to\_le} is a legal name, 
1280 while \texttt{lt\_le} is not.
1281
1282 But what about, say, the symmetric law of equality? Probably you would like 
1283 to name such a theorem with something explicitly recalling symmetry.
1284 The correct approach, 
1285 in this case, is the following. You should start with defining the 
1286 symmetric property for relations:
1287 \begin{grafite}
1288 definition symmetric \def
1289   \lambda A: Type. \lambda R. \forall x,y: A.
1290     R x y \to R y x.
1291 \end{grafite}
1292 Then, you may state the symmetry of equality as:
1293 \begin{grafite}
1294 \forall A: Type. symmetric A (eq A)
1295 \end{grafite}
1296 and \texttt{symmetric\_eq} is a legal name for such a theorem. 
1297
1298 So, somehow unexpectedly, the introduction of semi-rigid naming convention
1299 has an important beneficial effect on the global organization of the library, 
1300 forcing the user to define abstract concepts and properties before 
1301 using them (and formalizing such use).
1302
1303 Two cases have a special treatment. The first one concerns theorems whose
1304 conclusion is a (universally quantified) predicate variable, i.e. 
1305 theorems of the shape
1306 $\forall P,\dots,.P(t)$.
1307 In this case you may replace the conclusion with the string
1308 \texttt{elim} or \texttt{case}.
1309 For instance the name \texttt{nat\_elim2} is a legal name for the double
1310 induction principle.
1311
1312 The other special case is that of statements whose conclusion is a
1313 match expression. 
1314 A typical example is the following:
1315 \begin{grafite}
1316 \forall n,m: nat. 
1317   match (eqb n m) with
1318   [ true  \Rightarrow n = m 
1319   | false \Rightarrow n \neq m ]
1320 \end{grafite}
1321 where \texttt{eqb} is boolean equality.
1322 In this cases, the name can be build starting from the matched
1323 expression and the suffix \texttt{\_to\_Prop}. In the above example, 
1324 \texttt{eqb\_to\_Prop} is accepted. 
1325
1326 \section{The authoring interface}
1327 \label{sec:authoring}
1328
1329 The authoring interface of \MATITA{} is very similar to Proof
1330 General~\cite{proofgeneral}.  We
1331 chose not to build the \MATITA{} UI over Proof General for two reasons. First
1332 of all we wanted to integrate our XML-based rendering technologies, mainly
1333 \GTKMATHVIEW, in the UI.
1334 At the time of writing Proof General supports only text based
1335 rendering.\footnote{This may change with future releases of Proof General
1336 based on Eclipse (\url{http://www.eclipse.org/}), but is not yet the
1337 case.} The second reason is that we wanted
1338 to build the \MATITA{} UI on top of a state-of-the-art and widespread graphical
1339 toolkit as \GTK{} is.
1340
1341 Fig.~\ref{fig:screenshot} is a screenshot of the \MATITA{} authoring interface,
1342 featuring two windows. The background one is very like to the Proof General
1343 interface. The main difference is that we use the \GTKMATHVIEW{} widget to
1344 render sequents. Since \GTKMATHVIEW{} renders \MATHML{} markup we take
1345 advantage of the whole bidimensional mathematical notation. The foreground
1346 window is an instance of the cicBrowser (see Sect.~\ref{sec:library}) used to
1347 render in natural language the proof being developed.
1348
1349 Note that the syntax used in the script view is \TeX-like, but
1350 Unicode\footnote{\url{http://www.unicode.org/}} is 
1351 also fully supported so that mathematical glyphs can be input as such.
1352
1353 \begin{figure}[!ht]
1354  \begin{center}
1355   \includegraphics[width=0.95\textwidth]{pics/matita-screenshot}
1356   \caption{Authoring interface\strut}
1357   \label{fig:screenshot}
1358  \end{center}
1359 \end{figure}
1360
1361 Since the concepts of script based proof authoring are well-known, the
1362 remaining part of this section is dedicated to the distinguishing
1363 features of the \MATITA{} authoring interface.
1364
1365 \subsection{Direct manipulation of terms}
1366 \label{sec:directmanip}
1367
1368 While terms are input as \TeX-like formulae in \MATITA, they are converted to a
1369 mixed \MATHML+\BOXML{} markup for output purposes and then rendered by
1370 \GTKMATHVIEW. As described in~\cite{latexmathml} this mixed choice enables both
1371 high-quality bidimensional rendering of terms (including the use of fancy
1372 layout schemata like radicals and matrices) and the use of a
1373 concise and widespread textual syntax.
1374
1375 Keeping pointers from the presentations level terms down to the
1376 partially specified ones, \MATITA{} enables direct manipulation of
1377 rendered (sub)terms in the form of hyperlinks and semantic selection.
1378
1379 \emph{Hyperlinks} have anchors on the occurrences of constant and
1380 inductive type constructors and point to the corresponding definitions
1381 in the library. Anchors are available notwithstanding the use of
1382 user-defined mathematical notation: as can be seen on the right of
1383 Fig.~\ref{fig:directmanip}, where we clicked on $\nmid$, symbols
1384 encoding complex notations retain all the hyperlinks of constants or
1385 constructors used in the notation.
1386
1387 \emph{Semantic selection} enables the selection of mixed
1388 \MATHML+\BOXML{} markup, constraining the selection to markup
1389 representing meaningful CIC (sub)terms. In the example on the left of
1390 Fig.~\ref{fig:directmanip} is thus possible to select the subterm
1391 $\mathrm{prime}~n$, whereas it would not be possible to select
1392 $\to n$ since the former denotes an application while the
1393 latter is not a subterm. Once a meaningful (sub)term has been
1394 selected actions like reductions or tactic applications can be performed on it.
1395
1396 \begin{figure}[!ht]
1397  \begin{center}
1398   \includegraphics[width=0.40\textwidth]{pics/matita-screenshot-selection}
1399   \hspace{0.05\textwidth}
1400   \raisebox{0.4cm}{\includegraphics[width=0.50\textwidth]{pics/matita-screenshot-href}}
1401   \caption[Semantic selection and hyperlinks]{Semantic selection (on the left)
1402   and hyperlinks (on the right)\strut}
1403   \label{fig:directmanip}
1404  \end{center}
1405 \end{figure}
1406
1407 \subsection{Patterns}
1408 \label{sec:patterns}
1409
1410 In several situations working with direct manipulation of terms is 
1411 simpler and faster than typing the corresponding textual 
1412 commands~\cite{proof-by-pointing}.
1413 Nonetheless we need to record actions and selections in scripts.
1414
1415 In \MATITA{} \emph{patterns} are textual representations of selections.
1416 Users can select using the GUI and then ask the system to paste the
1417 corresponding pattern in this script. More often this process is
1418 transparent to the user: once an action is performed on a selection,
1419 the corresponding
1420 textual command is computed and inserted in the script.
1421
1422 \subsubsection{Pattern syntax}
1423
1424 Patterns are composed of two parts: \NT{sequent\_path} and
1425 \NT{wanted}; their concrete syntax is reported in Tab.~\ref{tab:pathsyn}.
1426
1427 \NT{sequent\_path} mocks-up a sequent, discharging unwanted subterms
1428 with \OP{?} and selecting the interesting parts with the placeholder
1429 \OP{\%}.  \NT{wanted} is a term that lives in the context of the
1430 placeholders.
1431
1432 Textual patterns produced from a graphical selection are made of the
1433 \NT{sequent\_path} only. Such patterns can represent every selection,
1434 but are quite verbose. The \NT{wanted} part of the syntax is meant to
1435 help the users in writing concise and elegant patterns by hand.
1436
1437 \begin{table}
1438  \caption{Patterns concrete syntax\strut}
1439  \label{tab:pathsyn}
1440 \hrule
1441 \[
1442 \begin{array}{@{}rcll@{}}
1443   \NT{pattern} & 
1444     ::= & [~\verb+in+~\NT{sequent\_path}~]~[~\verb+match+~\NT{wanted}~] & \\
1445   \NT{sequent\_path} & 
1446     ::= & \{~\NT{ident}~[~\verb+:+~\NT{multipath}~]~\}~
1447       [~\verb+\vdash+~\NT{multipath}~] & \\
1448   \NT{multipath} & ::= & \NT{term\_with\_placeholders} & \\
1449   \NT{wanted} & ::= & \NT{term} & \\
1450 \end{array}
1451 \]
1452 \hrule
1453 \end{table}
1454
1455 \subsubsection{Pattern evaluation}
1456
1457 Patterns are evaluated in two phases. The first selects roots
1458 (subterms) of the sequent, using the \NT{sequent\_path}, while the
1459 second searches the \NT{wanted} term starting from that roots.
1460 % Both are optional steps, and by convention the empty pattern selects
1461 % the whole conclusion.
1462
1463 \begin{description}
1464 \item[Phase 1]
1465   concerns only \NT{sequent\_path}. \NT{ident} is an hypothesis name and selects
1466   the assumption where the following optional \NT{multipath} will operate.
1467   \verb+\vdash+ can be considered the name for the goal.  If the whole pattern
1468   is omitted, the whole goal will be selected.  If one or more hypothesis names
1469   are given, the selection is restricted to that assumptions. If a
1470   $\NT{multipath}$ is omitted the whole assumption is selected. Remember that
1471   the user can be mostly unaware of patterns concrete syntax, since the system
1472   is able to write down a \NT{sequent\_path} starting from a graphical
1473   selection.
1474
1475   A \NT{multipath} is a CIC term in which a special constant \OP{\%} is allowed.
1476   The roots of discharged subterms are marked with \OP{?}, while \OP{\%} is used
1477   to select roots.  The default \NT{multipath}, the one that selects the whole
1478   term, is simply \OP{\%}.  Valid \NT{multipath} are, for example, \texttt{(? \%
1479   ?)} or \texttt{\% \TEXMACRO{to} (\% ?)} that respectively select the first
1480   argument of an application or the source of an arrow and the head of the
1481   application that is found in the arrow target.
1482
1483   This phase not only selects terms (roots of subterms) but determines also
1484   their context that will be possibly used in the next phase.
1485
1486 \item[Phase 2] 
1487   plays a role only if \NT{wanted} is specified. From the first phase we
1488   have some terms, that we will use as roots, and their context.
1489   For each of these contexts the \NT{wanted} term is disambiguated in it
1490   and the corresponding root is searched for a subterm that can be unified to
1491   \NT{wanted}. The result of this search is the selection the
1492   pattern represents.
1493
1494 \end{description}
1495
1496 \subsubsection{Examples}
1497
1498 Consider the following sequent:
1499 \sequent{n: nat\\m: nat\\H: m + n = n}{m = O}
1500
1501 To change the right part of the equality of the $H$
1502 hypothesis with $O + n$, the user selects and pastes it as the pattern
1503 in the following statement.
1504 \begin{grafite}
1505   change in H:(? ? ? %) with (O + n).
1506 \end{grafite}
1507
1508 To understand the pattern (or produce it by hand) the user should be aware that
1509 the notation $m + n = n$ hides the term $\mathrm{eq}~\mathrm{nat}~(m + n)~n$, so
1510 that the pattern selects only the third argument of $\mathrm{eq}$.
1511
1512 The experienced user may also write by hand a concise pattern to change at once
1513 all the occurrences of $n$ in the hypothesis $H$:
1514 \begin{grafite}
1515   change in H match n with (O + n).
1516 \end{grafite}
1517
1518 In this case the \NT{sequent\_path} selects the whole $H$, while
1519 the second phase locates $n$.
1520
1521 The latter pattern is equivalent to the following one, that the system
1522 can automatically generate from the selection.
1523 \begin{grafite}
1524   change in H:(? ? (? ? %) %) with (O + n).
1525 \end{grafite}
1526
1527 \subsubsection{Comparison with \COQ{}}
1528
1529 In \MATITA{} all the tactics that act on subterms of the current sequent
1530 accept pattern arguments. Additional arguments can be disambiguated in the
1531 contexts of the terms selected by the pattern
1532 (\emph{context-dependent arguments}).
1533
1534 %\NOTE{attualmente rewrite e fold non supportano fase 2. per
1535 %supportarlo bisogna far loro trasformare il pattern phase1+phase2 
1536 %in un pattern phase1only come faccio nell'ultimo esempio. lo si fa
1537 %con una pattern\_of(select(pattern))}
1538
1539 \COQ{} has two different ways of restricting the application of tactics to
1540 subterms of the current sequent, both relying on the same special syntax to
1541 identify a term occurrence.
1542
1543 The first way is to use this special syntax to tell the
1544 tactic what occurrences of a wanted term should be affected.
1545 The second is to prepare the sequent with another tactic called
1546 \TAC{pattern} and then apply the real tactic. Note that the choice is not
1547 left to the user, since some tactics needs the sequent to be prepared
1548 with pattern and do not accept directly this special syntax.
1549
1550 The idea is that to identify a subterm of the sequent we can
1551 write it and say that we want, for example, its third and fifth
1552 occurrences (counting from left to right). In our previous example,
1553 to change only the left part of the equivalence, the correct command
1554 would be:
1555 \begin{grafite}
1556   change n at 2 in H with (O + n)
1557 \end{grafite} 
1558 meaning that in the hypothesis \texttt{H} the \texttt{n} we want to change is
1559 the second we encounter proceeding from left to right.
1560
1561 The tactic \TAC{pattern} computes a
1562 $\beta$-expansion of a part of the sequent with respect to some
1563 occurrences of the given term. In the previous example the following
1564 command:
1565 \begin{grafite}
1566   pattern n at 2 in H
1567 \end{grafite}
1568 would have resulted in the sequent:
1569 \sequent{n: nat\\m : nat\\H: (fun n0: nat => m + n = n0) n}{m = 0}
1570 where \texttt{H} is $\beta$-expanded over the second \texttt{n}
1571 occurrence. 
1572
1573 At this point, since \COQ{} unification algorithm is essentially first-order,
1574 the application of an elimination principle (of the form $\forall P.\forall
1575 x.(H~x)\to (P~x)$) will unify \texttt{x} with \texttt{n} and \texttt{P} with
1576 \texttt{(fun n0: nat => m + n = n0)}.
1577
1578 Since \TAC{rewrite}, \TAC{replace} and several other tactics boils down to
1579 the application of the equality elimination principle, the previous
1580 trick implements the expected behavior.
1581
1582 The idea behind this way of identifying subterms in not really far
1583 from the idea behind patterns, but fails in extending to
1584 complex notation, since it relies on a mono-dimensional sequent representation.
1585 Real math notation places arguments upside-down (like in indexed sums or
1586 integrations) or even puts them inside a bidimensional matrix.  
1587 In these cases using the mouse to select the wanted term is probably the 
1588 more effective way to tell the system what to do. 
1589
1590 One of the goals of \MATITA{} is to use modern publishing techniques, and
1591 adopting a method for restricting tactics application domain that discourages 
1592 using heavy math notation would have definitively been a bad choice.
1593
1594 In \MATITA{}, tactics accepting pattern arguments can be more expressive than
1595 the equivalent tactics in \COQ{} since variables bound in the pattern context,
1596 can occur in context-dependent arguments. For example, consider the sequent:
1597 \sequent{n: nat\\x: nat\\H: \forall m. n + m*n = x + m}{m = O}
1598 In \MATITA{} the user can issue the command:
1599 \begin{grafite}
1600 change in H: \forall _. (? ? % ?) with (S m) * n.
1601 \end{grafite}
1602 to change $n+m*n$ with $(S~m)*n$. To achieve the same effect in \COQ, the
1603 user is obliged to change the whole hypothesis rewriting its right hand side
1604 as well.
1605
1606 \subsection{Tacticals}
1607 \label{sec:tinycals}
1608
1609 The procedural proof language implemented in \MATITA{} is pretty standard,
1610 with a notable exception for tacticals.
1611
1612 Tacticals first appeared in LCF~\cite{lcf} as higher order tactics.
1613 They can be seen as control flow constructs like looping, branching,
1614 error recovery and sequential composition. 
1615
1616 The following simple example shows a \COQ{} script made of four dot-terminated
1617 commands:
1618 \begin{grafite}
1619 Theorem trivial: 
1620   forall A B:Prop,
1621     A = B -> ((A -> B) /\ (B -> A)).
1622   intros [A B H].
1623   split; intro; 
1624     [ rewrite < H; assumption
1625     | rewrite > H; assumption
1626     ].
1627 Qed.
1628 \end{grafite}
1629
1630 The third command is an application of the sequencing tactical \OP{$\ldots$~;~$\ldots$},
1631 that combines the tactic \TAC{split} with the application of the branching
1632 tactical \OP{$\ldots$~;[~$\ldots$~|~$\ldots$~|~$\ldots$~]} to other tactics or tacticals.
1633
1634 The usual implementation of tacticals executes them atomically as any
1635 other command. In \MATITA{} this is not the case: each punctuation
1636 symbol is executed as a single command.
1637
1638 \subsubsection{Common issues of tacticals}
1639 We will examine the two main problems of procedural proof languages:
1640 maintainability and readability. 
1641
1642 Tacticals are not only used to make scripts shorter by factoring out
1643 common cases and repeating commands. They are the primary way of making
1644 scripts more maintainable. They also have the well-known duty of
1645 structuring the proof using the branching tactical.
1646
1647 However, authoring a proof structured with tacticals is annoying.
1648 Consider for example a proof by induction, and imagine you
1649 are using one of the state of the art graphical interfaces for proof assistant
1650 like Proof General. After applying the induction principle you have to choose:
1651 immediately structure the proof or postpone the structuring.
1652 If you decide for the former you have to apply the branching tactical and write
1653 at once tactics for all the cases. Since the user does not even know the
1654 generated goals yet, he can only replace all the cases with the identity
1655 tactic and execute the command, just to receive feedback on the first
1656 goal. Then he has to go one step back to replace the first identity
1657 tactic with the wanted one and repeat the process until all the
1658 branches are closed.
1659
1660 One could imagine that a structured script is simpler to understand.
1661 This is not the case.
1662 A proof script, being not declarative, is not meant to be read.
1663 However, the user has the need of explaining it to others.
1664 This is achieved by interactively re-playing the script to show each
1665 intermediate proof status. Tacticals make this operation uncomfortable.
1666 Indeed, a tactical is executed atomically, while it is obvious that it
1667 performs lot of smaller steps we are interested in.
1668 To show the intermediate steps, the proof must be de-structured on the
1669 fly, for example replacing \OP{;} with \OP{.} where possible.
1670
1671 \MATITA{} has a peculiar tacticals implementation that provides the
1672 same benefits as classical tacticals, while not burdening the user
1673 during proof authoring and re-playing.
1674
1675 \subsubsection{The \MATITA{} approach}
1676
1677 \begin{table}
1678  \caption{Concrete syntax of tacticals\strut}
1679  \label{tab:tacsyn}
1680 \hrule
1681 \[
1682 \begin{array}{@{}rcll@{}}
1683   \NT{punctuation} & 
1684     ::= & \SEMICOLON \quad|\quad \DOT \quad|\quad \SHIFT \quad|\quad \BRANCH \quad|\quad \MERGE \quad|\quad \POS{\mathrm{NUMBER}~} & \\
1685   \NT{block\_kind} & 
1686     ::= & \verb+focus+ ~|~ \verb+try+ ~|~ \verb+solve+ ~|~ \verb+first+ ~|~ \verb+repeat+ ~|~ \verb+do+~\mathrm{NUMBER} & \\
1687   \NT{block\_delim} & 
1688     ::= & \verb+begin+ ~|~ \verb+end+ & \\
1689   \NT{command} & 
1690     ::= & \verb+skip+ ~|~ \NT{tactic} ~|~ \NT{block\_delim} ~|~ \NT{block\_kind} ~|~ \NT{punctuation} \\
1691 \end{array}
1692 \]
1693 \hrule
1694 \end{table}
1695
1696 \MATITA{} tacticals syntax is reported in Tab.~\ref{tab:tacsyn}.
1697 LCF tacticals have been replaced by unstructured more primitive commands;
1698 every LCF tactical is semantically equivalent to a sequential composition of
1699 them. As usual, each command is executed atomically, so that a sequence
1700 corresponding to an LCF tactical is now executed in multiple steps.
1701
1702 For instance, reconsider the previous example of a proof by induction.
1703 In \MATITA{} the user can apply the induction principle, and just
1704 open the branching punctuation symbol \OP{[}. Then he can interact with the
1705 system (applying tactics and so forth) until he decides to move to the
1706 next branch using \OP{|}. After the last branch, the punctuation symbol
1707 \OP{]} must be used to collect goals possibly left open, accordingly to
1708 the semantics of the LCF branching tactical \OP{$\ldots$~;[~$\ldots$~|~$\ldots$~|~$\ldots$~]}. The result effortlessly obtained is a structured script.
1709
1710 The user is not forced to fully structure his script. If he wants, he
1711 can even write completely unstructured proofs using only the \OP{.}
1712 punctuation symbol.
1713
1714 Re-playing a proof is also straightforward since there is no longer any need
1715 to manually destructure the proof.
1716
1717 \section{Standard library}
1718 \label{sec:stdlib}
1719
1720 \MATITA{} is \COQ{} compatible, in the sense that every theorem of \COQ{} can be
1721 read, checked and referenced in further developments.  However, in order to test
1722 the actual usability of the system, a new library of results has been started
1723 from scratch. In this case, of course, we wrote (and offer) the source scripts,
1724 while in the case of \COQ{} \MATITA{} may only rely on XML files of \COQ{}
1725 objects. 
1726
1727 The current library just comprises about one thousand theorems in 
1728 elementary aspects of arithmetics up to the multiplicative property for 
1729 Eulers' totient function $\phi$.
1730
1731 The library is organized in five main directories: \texttt{logic} (connectives,
1732 quantifiers, equality, \ldots), \texttt{datatypes} (basic datatypes and type
1733 constructors), \texttt{nat} (natural numbers), \texttt{Z} (integers), \texttt{Q}
1734 (rationals). The most complex development is \texttt{nat}, organized in 25
1735 scripts, listed in Tab.~\ref{tab:scripts}.
1736
1737 \begin{table}[ht]
1738  \begin{tabular}{lll}
1739   \FILE{nat.ma}    & \FILE{plus.ma} & \FILE{times.ma}  \\
1740   \FILE{minus.ma}  & \FILE{exp.ma}  & \FILE{compare.ma} \\
1741   \FILE{orders.ma} & \FILE{le\_arith.ma} &  \FILE{lt\_arith.ma} \\   
1742   \FILE{factorial.ma} & \FILE{sigma\_and\_pi.ma} & \FILE{minimization.ma}  \\
1743   \FILE{div\_and\_mod.ma} & \FILE{gcd.ma} & \FILE{congruence.ma} \\
1744   \FILE{primes.ma} & \FILE{nth\_prime.ma} & \FILE{ord.ma} \\
1745   \FILE{count.ma}  & \FILE{relevant\_equations.ma} & \FILE{permutation.ma} \\ 
1746   \FILE{factorization.ma} & \FILE{chinese\_reminder.ma} &
1747   \FILE{fermat\_little\_th.ma} \\     
1748   \FILE{totient.ma} & & \\
1749  \end{tabular}
1750  \caption{Scripts on natural numbers in the standard library\strut}
1751  \label{tab:scripts}
1752 \end{table}
1753
1754 We do not plan to maintain the library in a centralized way, 
1755 as most of the systems do. On the contrary we are currently
1756 developing Wiki-technologies to support collaborative 
1757 development of the library, encouraging people to expand, 
1758 modify and elaborate previous contributions.
1759
1760 \section{Conclusions}
1761 \label{sec:conclusion}
1762
1763 \TODO{conclusioni}
1764
1765 \acknowledgements
1766 We would like to thank all the people that during the past
1767 7 years collaborated in the \HELM{} project and contributed to 
1768 the development of \MATITA{}, and in particular
1769 M.~Galat\`a, A.~Griggio, F.~Guidi, P.~Di~Lena, L.~Padovani, I.~Schena, M.~Selmi,
1770 and V.~Tamburrelli.
1771
1772 \theendnotes
1773
1774 \bibliography{matita}
1775
1776 \end{document}