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