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