| 1 |
|
| 2 |
\documentclass{article} |
| 3 |
|
| 4 |
\usepackage{verbatim} |
| 5 |
\usepackage{amsmath} |
| 6 |
\usepackage{amssymb} |
| 7 |
\usepackage{array} |
| 8 |
\usepackage{fullpage} |
| 9 |
|
| 10 |
\author{B.~Barras} |
| 11 |
\title{An introduction to syntax of Coq V8} |
| 12 |
|
| 13 |
%% Le _ est un caractère normal |
| 14 |
\catcode`\_=13 |
| 15 |
\let\subscr=_ |
| 16 |
\def_{\ifmmode\sb\else\subscr\fi} |
| 17 |
|
| 18 |
\def\NT#1{\langle\textit{#1}\rangle} |
| 19 |
\def\NTL#1#2{\langle\textit{#1}\rangle_{#2}} |
| 20 |
\def\TERM#1{\textsf{\bf #1}} |
| 21 |
|
| 22 |
\newenvironment{transbox} |
| 23 |
{\begin{center}\tt\begin{tabular}{l|ll} \hfil\textrm{V7} & \hfil\textrm{V8} \\ \hline} |
| 24 |
{\end{tabular}\end{center}} |
| 25 |
\def\TRANS#1#2 |
| 26 |
{\begin{tabular}[t]{@{}l@{}}#1\end{tabular} & |
| 27 |
\begin{tabular}[t]{@{}l@{}}#2\end{tabular} \\} |
| 28 |
\def\TRANSCOM#1#2#3 |
| 29 |
{\begin{tabular}[t]{@{}l@{}}#1\end{tabular} & |
| 30 |
\begin{tabular}[t]{@{}l@{}}#2\end{tabular} & #3 \\} |
| 31 |
|
| 32 |
\begin{document} |
| 33 |
|
| 34 |
\maketitle |
| 35 |
|
| 36 |
The goal of this document is to introduce by example to the new syntax of |
| 37 |
Coq. It is strongly recommended to read first the definition of the new |
| 38 |
syntax, but this document should also be useful for the eager user who wants |
| 39 |
to start with the new syntax quickly. |
| 40 |
|
| 41 |
|
| 42 |
\section{Changes in lexical conventions w.r.t. V7} |
| 43 |
|
| 44 |
\subsection{Identifiers} |
| 45 |
|
| 46 |
The lexical conventions changed: \TERM{_} is not a regular identifier |
| 47 |
anymore. It is used in terms as a placeholder for subterms to be inferred |
| 48 |
at type-checking, and in patterns as a non-binding variable. |
| 49 |
|
| 50 |
Furthermore, only letters (unicode letters), digits, single quotes and |
| 51 |
_ are allowed after the first character. |
| 52 |
|
| 53 |
\subsection{Quoted string} |
| 54 |
|
| 55 |
Quoted strings are used typically to give a filename (which may not |
| 56 |
be a regular identifier). As before they are written between double |
| 57 |
quotes ("). Unlike for V7, there is no escape character: characters |
| 58 |
are written normally but the double quote which is doubled. |
| 59 |
|
| 60 |
\section{Main changes in terms w.r.t. V7} |
| 61 |
|
| 62 |
|
| 63 |
\subsection{Precedence of application} |
| 64 |
|
| 65 |
In the new syntax, parentheses are not really part of the syntax of |
| 66 |
application. The precedence of application (10) is tighter than all |
| 67 |
prefix and infix notations. It makes it possible to remove parentheses |
| 68 |
in many contexts. |
| 69 |
|
| 70 |
\begin{transbox} |
| 71 |
\TRANS{(A x)->(f x)=(g y)}{A x -> f x = g y} |
| 72 |
\TRANS{(f [x]x)}{f (fun x => x)} |
| 73 |
\end{transbox} |
| 74 |
|
| 75 |
|
| 76 |
\subsection{Arithmetics and scopes} |
| 77 |
|
| 78 |
The specialized notation for \TERM{Z} and \TERM{R} (introduced by |
| 79 |
symbols \TERM{`} and \TERM{``}) have disappeared. They have been |
| 80 |
replaced by the general notion of scope. |
| 81 |
|
| 82 |
\begin{center} |
| 83 |
\begin{tabular}{l|l|l} |
| 84 |
type & scope name & delimiter \\ |
| 85 |
\hline |
| 86 |
types & type_scope & \TERM{T} \\ |
| 87 |
\TERM{bool} & bool_scope & \\ |
| 88 |
\TERM{nat} & nat_scope & \TERM{nat} \\ |
| 89 |
\TERM{Z} & Z_scope & \TERM{Z} \\ |
| 90 |
\TERM{R} & R_scope & \TERM{R} \\ |
| 91 |
\TERM{positive} & positive_scope & \TERM{P} |
| 92 |
\end{tabular} |
| 93 |
\end{center} |
| 94 |
|
| 95 |
In order to use notations of arithmetics on \TERM{Z}, its scope must be opened with command \verb+Open Scope Z_scope.+ Another possibility is using the scope change notation (\TERM{\%}). The latter notation is to be used when notations of several scopes appear in the same expression. |
| 96 |
|
| 97 |
In examples below, scope changes are not needed if the appropriate scope |
| 98 |
has been opened. Scope nat_scope is opened in the initial state of Coq. |
| 99 |
\begin{transbox} |
| 100 |
\TRANSCOM{`0+x=x+0`}{0+x=x+0}{\textrm{Z_scope}} |
| 101 |
\TRANSCOM{``0 + [if b then ``1`` else ``2``]``}{0 + if b then 1 else 2}{\textrm{R_scope}} |
| 102 |
\TRANSCOM{(0)}{0}{\textrm{nat_scope}} |
| 103 |
\end{transbox} |
| 104 |
|
| 105 |
Below is a table that tells which notation is available in which |
| 106 |
scope. The relative precedences and associativity of operators is the |
| 107 |
same as in usual mathematics. See the reference manual for more |
| 108 |
details. However, it is important to remember that unlike V7, the type |
| 109 |
operators for product and sum are left associative, in order not to |
| 110 |
clash with arithmetic operators. |
| 111 |
|
| 112 |
\begin{center} |
| 113 |
\begin{tabular}{l|l} |
| 114 |
scope & notations \\ |
| 115 |
\hline |
| 116 |
nat_scope & $+ ~- ~* ~< ~\leq ~> ~\geq$ \\ |
| 117 |
Z_scope & $+ ~- ~* ~/ ~\TERM{mod} ~< ~\leq ~> ~\geq ~?=$ \\ |
| 118 |
R_scope & $+ ~- ~* ~/ ~< ~\leq ~> ~\geq$ \\ |
| 119 |
type_scope & $* ~+$ \\ |
| 120 |
bool_scope & $\TERM{\&\&} ~\TERM{$||$} ~\TERM{-}$ \\ |
| 121 |
list_scope & $\TERM{::} ~\TERM{++}$ |
| 122 |
\end{tabular} |
| 123 |
\end{center} |
| 124 |
(Note: $\leq$ is written \TERM{$<=$}) |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
\subsection{Notation for implicit arguments} |
| 129 |
|
| 130 |
The explicitation of arguments is closer to the \emph{bindings} notation in |
| 131 |
tactics. Argument positions follow the argument names of the head constant. |
| 132 |
|
| 133 |
\begin{transbox} |
| 134 |
\TRANS{f 1!t1 2!t2}{f (x:=t1) (y:=t2)} |
| 135 |
\TRANS{!f t1 t2}{@f t1 t2} |
| 136 |
\end{transbox} |
| 137 |
|
| 138 |
|
| 139 |
\subsection{Universal quantification} |
| 140 |
|
| 141 |
The universal quantification and dependent product types are now |
| 142 |
materialized with the \TERM{forall} keyword before the binders and a |
| 143 |
comma after the binders. |
| 144 |
|
| 145 |
The syntax of binders also changed significantly. A binder can simply be |
| 146 |
a name when its type can be inferred. In other cases, the name and the type |
| 147 |
of the variable are put between parentheses. When several consecutive |
| 148 |
variables have the same type, they can be grouped. Finally, if all variables |
| 149 |
have the same type parentheses can be omitted. |
| 150 |
|
| 151 |
\begin{transbox} |
| 152 |
\TRANS{(x:A)B}{forall (x:~A), B ~~\textrm{or}~~ forall x:~A, B} |
| 153 |
\TRANS{(x,y:nat)P}{forall (x y :~nat), P ~~\textrm{or}~~ forall x y :~nat, P} |
| 154 |
\TRANS{(x,y:nat;z:A)P}{forall (x y :~nat) (z:A), P} |
| 155 |
\TRANS{(x,y,z,t:?)P}{forall x y z t, P} |
| 156 |
\TRANS{(x,y:nat;z:?)P}{forall (x y :~nat) z, P} |
| 157 |
\end{transbox} |
| 158 |
|
| 159 |
\subsection{Abstraction} |
| 160 |
|
| 161 |
The notation for $\lambda$-abstraction follows that of universal |
| 162 |
quantification. The binders are surrounded by keyword \TERM{fun} |
| 163 |
and $\Rightarrow$ (\verb+=>+ in ascii). |
| 164 |
|
| 165 |
\begin{transbox} |
| 166 |
\TRANS{[x,y:nat; z](f a b c)}{fun (x y:nat) z => f a b c} |
| 167 |
\end{transbox} |
| 168 |
|
| 169 |
|
| 170 |
\subsection{Pattern-matching} |
| 171 |
|
| 172 |
Beside the usage of the keyword pair \TERM{match}/\TERM{with} instead of |
| 173 |
\TERM{Cases}/\TERM{of}, the main change is the notation for the type of |
| 174 |
branches and return type. It is no longer written between \TERM{$<$ $>$} before |
| 175 |
the \TERM{Cases} keyword, but interleaved with the destructured objects. |
| 176 |
|
| 177 |
The idea is that for each destructured object, one may specify a variable |
| 178 |
name to tell how the branches types depend on this destructured objects (case |
| 179 |
of a dependent elimination), and also how they depend on the value of the |
| 180 |
arguments of the inductive type of the destructured objects. The type of |
| 181 |
branches is then given after the keyword \TERM{return}, unless it can be |
| 182 |
inferred. |
| 183 |
|
| 184 |
Moreover, when the destructured object is a variable, one may use this |
| 185 |
variable in the return type. |
| 186 |
|
| 187 |
\begin{transbox} |
| 188 |
\TRANS{Cases n of\\~~ O => O \\| (S k) => (1) end}{match n with\\~~ 0 => 0 \\| (S k) => 1 end} |
| 189 |
\TRANS{Cases m n of \\~~0 0 => t \\| ... end}{match m, n with \\~~0, 0 => t \\| .. end} |
| 190 |
\TRANS{<[n:nat](P n)>Cases T of ... end}{match T as n return P n with ... end} |
| 191 |
\TRANS{<[n:nat][p:(even n)]\~{}(odd n)>Cases p of\\~~ ... \\end}{match p in even n return \~{} odd n with\\~~ ...\\end} |
| 192 |
\end{transbox} |
| 193 |
|
| 194 |
|
| 195 |
\subsection{Fixpoints and cofixpoints} |
| 196 |
|
| 197 |
An easier syntax for non-mutual fixpoints is provided, making it very close |
| 198 |
to the usual notation for non-recursive functions. The decreasing argument |
| 199 |
is now indicated by an annotation between curly braces, regardless of the |
| 200 |
binders grouping. The annotation can be omitted if the binders introduce only |
| 201 |
one variable. The type of the result can be omitted if inferable. |
| 202 |
|
| 203 |
\begin{transbox} |
| 204 |
\TRANS{Fix plus\{plus [n:nat] : nat -> nat :=\\~~ [m]...\}}{fix plus (n m:nat) \{struct n\}: nat := ...} |
| 205 |
\TRANS{Fix fact\{fact [n:nat]: nat :=\\ |
| 206 |
~~Cases n of\\~~~~ O => (1) \\~~| (S k) => (mult n (fact k)) end\}}{fix fact |
| 207 |
(n:nat) :=\\ |
| 208 |
~~match n with \\~~~~0 => 1 \\~~| (S k) => n * fact k end} |
| 209 |
\end{transbox} |
| 210 |
|
| 211 |
There is a syntactic sugar for mutual fixpoints associated to a local |
| 212 |
definition: |
| 213 |
|
| 214 |
\begin{transbox} |
| 215 |
\TRANS{let f := Fix f \{f [x:A] : T := M\} in\\(g (f y))}{let fix f (x:A) : T := M in\\g (f x)} |
| 216 |
\end{transbox} |
| 217 |
|
| 218 |
The same applies to cofixpoints, annotations are not allowed in that case. |
| 219 |
|
| 220 |
\subsection{Notation for type cast} |
| 221 |
|
| 222 |
\begin{transbox} |
| 223 |
\TRANS{O :: nat}{0 : nat} |
| 224 |
\end{transbox} |
| 225 |
|
| 226 |
\section{Main changes in tactics w.r.t. V7} |
| 227 |
|
| 228 |
The main change is that all tactic names are lowercase. This also holds for |
| 229 |
Ltac keywords. |
| 230 |
|
| 231 |
\subsection{Ltac} |
| 232 |
|
| 233 |
Definitions of macros are introduced by \TERM{Ltac} instead of |
| 234 |
\TERM{Tactic Definition}, \TERM{Meta Definition} or \TERM{Recursive |
| 235 |
Definition}. |
| 236 |
|
| 237 |
Rules of a match command are not between square brackets anymore. |
| 238 |
|
| 239 |
Context (understand a term with a placeholder) instantiation \TERM{inst} |
| 240 |
became \TERM{context}. Syntax is unified with subterm matching. |
| 241 |
|
| 242 |
\begin{transbox} |
| 243 |
\TRANS{match t with [C[x=y]] => inst C[y=x]}{match t with context C[x=y] => context C[y=x]} |
| 244 |
\end{transbox} |
| 245 |
|
| 246 |
\subsection{Named arguments of theorems} |
| 247 |
|
| 248 |
\begin{transbox} |
| 249 |
\TRANS{Apply thm with x:=t 1:=u}{apply thm with (x:=t) (1:=u)} |
| 250 |
\end{transbox} |
| 251 |
|
| 252 |
|
| 253 |
\subsection{Occurrences} |
| 254 |
|
| 255 |
To avoid ambiguity between a numeric literal and the optional |
| 256 |
occurrence numbers of this term, the occurrence numbers are put after |
| 257 |
the term itself. This applies to tactic \TERM{pattern} and also |
| 258 |
\TERM{unfold} |
| 259 |
\begin{transbox} |
| 260 |
\TRANS{Pattern 1 2 (f x) 3 4 d y z}{pattern (f x at 1 2) (d at 3 4) y z} |
| 261 |
\end{transbox} |
| 262 |
|
| 263 |
\section{Main changes in vernacular commands w.r.t. V7} |
| 264 |
|
| 265 |
|
| 266 |
\subsection{Binders} |
| 267 |
|
| 268 |
The binders of vernacular commands changed in the same way as those of |
| 269 |
fixpoints. This also holds for parameters of inductive definitions. |
| 270 |
|
| 271 |
|
| 272 |
\begin{transbox} |
| 273 |
\TRANS{Definition x [a:A] : T := M}{Definition x (a:A) : T := M} |
| 274 |
\TRANS{Inductive and [A,B:Prop]: Prop := \\~~conj : A->B->(and A B)}% |
| 275 |
{Inductive and (A B:Prop): Prop := \\~~conj : A -> B -> and A B} |
| 276 |
\end{transbox} |
| 277 |
|
| 278 |
\subsection{Hints} |
| 279 |
|
| 280 |
The syntax of \emph{extern} hints changed: the pattern and the tactic |
| 281 |
to be applied are separated by a \TERM{$\Rightarrow$}. |
| 282 |
\begin{transbox} |
| 283 |
\TRANS{Hint Extern 4 (toto ?) Apply lemma}{Hint Extern 4 (toto _) => apply lemma} |
| 284 |
\end{transbox} |
| 285 |
|
| 286 |
\end{document} |