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