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