1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
\achapter{Implicit Coercions}
\aauthor{Amokrane Saïbi}
\label{Coercions-full}
\index{Coercions!presentation}
\asection{General Presentation}
This section describes the inheritance mechanism of {\Coq}. In {\Coq} with
inheritance, we are not interested in adding any expressive power to
our theory, but only convenience. Given a term, possibly not typable,
we are interested in the problem of determining if it can be well
typed modulo insertion of appropriate coercions. We allow to write:
\begin{itemize}
\item $(f~a)$ where $f:(x:A)B$ and $a:A'$ when $A'$ can
be seen in some sense as a subtype of $A$.
\item $x:A$ when $A$ is not a type, but can be seen in
a certain sense as a type: set, group, category etc.
\item $(f~a)$ when $f$ is not a function, but can be seen in a certain sense
as a function: bijection, functor, any structure morphism etc.
\end{itemize}
\asection{Classes}
\index{Coercions!classes}
A class with $n$ parameters is any defined name with a type
$(x_1:A_1)..(x_n:A_n)s$ where $s$ is a sort. Thus a class with
parameters is considered as a single class and not as a family of
classes. An object of a class $C$ is any term of type $(C~t_1
.. t_n)$. In addition to these user-classes, we have two abstract
classes:
\begin{itemize}
\item {\tt SORTCLASS}, the class of sorts;
its objects are the terms whose type is a sort.
\item {\tt FUNCLASS}, the class of functions;
its objects are all the terms with a functional
type, i.e. of form $(x:A)B$.
\end{itemize}
\asection{Coercions}
\index{Coercions!FUNCLASS}
\index{Coercions!SORTCLASS}
A name $f$ can be declared as a coercion between a source user-class
$C$ with $n$ parameters and a target class $D$ if one of these
conditions holds:
\begin{itemize}
\item $D$ is a user-class, then the type of $f$ must have the form
$(x_1:A_1)..(x_n:A_n)(y:(C~x_1..x_n)) (D~u_1..u_m)$ where $m$
is the number of parameters of $D$.
\item $D$ is {\tt FUNCLASS}, then the type of $f$ must have the form
$(x_1:A_1)..(x_n:A_n)(y:(C~x_1..x_n))(x:A)B$.
\item $D$ is {\tt SORTCLASS}, then the type of $f$ must have the form
$(x_1:A_1)..(x_n:A_n)(y:(C~x_1..x_n))s$.
\end{itemize}
We then write $f:C \mbox{\texttt{>->}} D$. The restriction on the type
of coercions is called {\em the uniform inheritance condition}.
Remark that the abstract classes {\tt FUNCLASS} and {\tt SORTCLASS}
cannot be source classes.
To coerce an object $t:(C~t_1..t_n)$ of $C$ towards $D$, we have to
apply the coercion $f$ to it; the obtained term $(f~t_1..t_n~t)$ is
then an object of $D$.
\asubsection{Identity Coercions}
\index{Coercions!identity}
Identity coercions are special cases of coercions used to go around
the uniform inheritance condition. Let $C$ and $D$ be two classes
with respectively $n$ and $m$ parameters and
$f:(x_1:T_1)..(x_k:T_k)(y:(C~u_1..u_n))(D~v_1..v_m)$ a function which
does not verify the uniform inheritance condition. To declare $f$ as
coercion, one has first to declare a subclass $C'$ of $C$:
$$C' := [x_1:T_1]..[x_k:T_k](C~u_1..u_n)$$
\noindent We then define an {\em identity coercion} between $C'$ and $C$:
\begin{eqnarray*}
Id\_C'\_C & := & [x_1:T_1]..[x_k:T_k][y:(C'~x_1..x_k)]\\
& & (y::(C~u_1..u_n))
\end{eqnarray*}
We can now declare $f$ as coercion from $C'$ to $D$, since we can
``cast'' its type as
$(x_1:T_1)..(x_k:T_k)(y:(C'~x_1..x_k))(D~v_1..v_m)$.\\ The identity
coercions have a special status: to coerce an object $t:(C'~t_1..t_k)$
of $C'$ towards $C$, we have not to insert explicitly $Id\_C'\_C$
since $(Id\_C'\_C~t_1..t_k~t)$ is convertible with $t$. However we
``rewrite'' the type of $t$ to become an object of $C$; in this case,
it becomes $(C~u_1^*..u_k^*)$ where each $u_i^*$ is the result of the
substitution in $u_i$ of the variables $x_j$ by $t_j$.
\asection{Inheritance Graph}
\index{Coercions!inheritance graph}
Coercions form an inheritance graph with classes as nodes. We call
{\em path coercion} an ordered list of coercions between two nodes of
the graph. A class $C$ is said to be a subclass of $D$ if there is a
coercion path in the graph from $C$ to $D$; we also say that $C$
inherits from $D$. Our mechanism supports multiple inheritance since a
class may inherit from several classes, contrary to simple inheritance
where a class inherits from at most one class. However there must be
at most one path between two classes. If this is not the case, only
the oldest one is {\em valid} and the others are ignored. So the order
of declaration of coercions is important.
We extend notations for coercions to path coercions. For instance
$[f_1;..;f_k]:C \mbox{\texttt{>->}} D$ is the coercion path composed
by the coercions $f_1..f_k$. The application of a path-coercion to a
term consists of the successive application of its coercions.
\asection{Commands}
\asubsection{\tt Class {\ident}.}\comindex{Class}
Declares the name {\ident} as a new class.
\begin{ErrMsgs}
\item {\ident} \errindex{not declared}
\item {\ident} \errindex{is already a class}
\item \errindex{Type of {\ident} does not end with a sort}
\end{ErrMsgs}
\asubsection{\tt Class Local {\ident}.}
Declares the name {\ident} as a new local class to the current section.
\asubsection{\tt Coercion {\ident} : {\ident$_1$} >-> {\ident$_2$}.}
\comindex{Coercion}
Declares the name {\ident} as a coercion between {\ident$_1$} and
{\ident$_2$}. The classes {\ident$_1$} and {\ident$_2$} are first
declared if necessary.
\begin{ErrMsgs}
\item {\ident} \errindex{not declared}
\item {\ident} \errindex{is already a coercion}
\item \errindex{FUNCLASS cannot be a source class}
\item \errindex{SORTCLASS cannot be a source class}
\item \errindex{Does not correspond to a coercion} \\
{\ident} is not a function.
\item \errindex{We do not find the source class {\ident$_1$}}
\item {\ident} \errindex{does not respect the inheritance uniform condition}
\item \errindex{The target class does not correspond to {\ident$_2$}}
\end{ErrMsgs}
When the coercion {\ident} is added to the inheritance graph, non
valid path coercions are ignored; they are signaled by a warning.
\\[0.3cm]
\noindent {\bf Warning :}
\begin{enumerate}
\item \begin{tabbing}
{\tt Ambiguous paths: }\= $[f_1^1;..;f_{n_1}^1] : C_1\mbox{\tt >->}D_1$\\
\> ... \\
\>$[f_1^m;..;f_{n_m}^m] : C_m\mbox{\tt >->}D_m$
\end{tabbing}
\end{enumerate}
\asubsection{\tt Coercion Local {\ident}:{\ident$_1$} >->
{\ident$_2$}.}
Declares the name {\ident} as a local coercion to the current section.
\asubsection{\tt Identity Coercion {\ident}:{\ident$_1$} >-> {\ident$_2$}.}
We check that {\ident$_1$} is a constant with a value of the form
$[x_1:T_1]..[x_n:T_n](\mbox{\ident}_2~t_1..t_m)$ where $m$ is the
number of parameters of \ident$_2$. Then we define an identity
function with the type
$(x_1:T_1)..(x_n:T_n)(y:(\mbox{\ident}_1~x_1..x_n))
({\mbox{\ident}_2}~t_1..t_m)$, and we declare it as an identity
coercion between {\ident$_1$} and {\ident$_2$}.
\begin{ErrMsgs}
\item \errindex{Clash with previous constant {\ident}}
\item {\ident$_1$} \errindex{must be a transparent constant}
\end{ErrMsgs}
\asubsection
{\tt Identity Coercion Local {\ident}:{\ident$_1$} >-> {\ident$_2$}.}
Declares the name {\ident} as a local identity coercion to the current section.
\asubsection{\tt Print Classes.}
\comindex{Print Classes}
Print the list of declared classes in the current context.
\asubsection{\tt Print Coercions.}
\comindex{Print Coercions}
Print the list of declared coercions in the current context.
\asubsection{\tt Print Graph.}
\comindex{Print Graph}
Print the list of valid path coercions in the current context.
\asection{Coercions and Pretty-Printing}
To every declared coercion $f$, we automatically define an
associated pretty-printing rule, also named $f$, to hide the coercion
applications. Thus $(f~t_1..t_n~t)$ is printed as $t$ where $n$ is the
number of parameters of the source class of $f$. The user can change
this behavior just by overwriting the rule $f$ by a new one with the
same name (see chapter~\ref{Addoc-syntax} for more details about
pretty-printing rules). If $f$ is a coercion to {\tt FUNCLASS},
another pretty-printing rule called $f1$ is also generated. This last
rule prints $(f~t_1..t_n~t_{n+1}..t_m)$ as $(f~t_{n+1}..t_m)$.
In the following examples, we changed the coercion pretty-printing
rules to show the inserted coercions.
\asection{Inheritance Mechanism -- Examples}
There are three situations:
\begin{itemize}
\item $(f~a)$ is ill-typed where $f:(x:A)B$ and $a:A'$. If there is a
path coercion between $A'$ and $A$, $(f~a)$ is transformed into
$(f~a')$ where $a'$ is the result of the application of this
path coercion to $a$.
%\begin{\small}
\begin{coq_example}
Variables C:nat->Set; D:nat->bool->Set; E:bool->Set.
Variable f : (n:nat)(C n) -> (D (S n) true).
Coercion f : C >-> D.
Variable g : (n:nat)(b:bool)(D n b) -> (E b).
Coercion g : D >-> E.
Variable c : (C O).
Variable T : (E true) -> nat.
Check (T c).
\end{coq_example}
%\end{small}
We give now an example using identity coercions.
%\begin{small}
\begin{coq_example}
Definition D' := [b:bool](D (S O) b).
Identity Coercion IdD'D : D' >-> D.
Print IdD'D.
Variable d' : (D' true).
Check (T d').
\end{coq_example}
%\end{small}
In the case of functional arguments, we use the monotonic rule of
sub-typing. Approximatively, to coerce $t:(x:A)B$ towards $(x:A')B'$,
one have to coerce $A'$ towards $A$ and $B$ towards $B'$. An example
is given below:
%\begin{small}
\begin{coq_example}
Variables A,B:Set; h:A->B.
Coercion h : A >-> B.
Variable U : (A -> (E true)) -> nat.
Variable t : B -> (C O).
Check (U t).
\end{coq_example}
%\end{small}
Remark the changes in the result following the modification of the
previous example.
%\begin{small}
\begin{coq_example}
Variable U' : ((C O) -> B) -> nat.
Variable t' : (E true) -> A.
Check (U' t').
\end{coq_example}
%\end{small}
\item An assumption $x:A$ when $A$ is not a type, is ill-typed. It is
replaced by $x:A'$ where $A'$ is the result of the application
to $A$ of the path coercion between the class of $A$ and {\tt
SORTCLASS} if it exists. This case occurs in the abstraction
$[x:A]t$, universal quantification $(x:A)B$, global variables
and parameters of (co-)inductive definitions and functions. In
$(x:A)B$, such a path coercion may be applied to $B$ also if
necessary.
%\begin{small}
\begin{coq_example}
Variable Graph : Type.
Variable Node : Graph -> Type.
Coercion Node : Graph >-> SORTCLASS.
Variable G : Graph.
Variable Arrows : G -> G -> Type.
Check Arrows.
Variable fg : G -> G.
Check fg.
\end{coq_example}
%\end{small}
\item $(f~a)$ is ill-typed because $f:A$ is not a function. The term
$f$ is replaced by the term obtained by applying to $f$ the path
coercion between $A$ and {\tt FUNCLASS} if it exists.
%\begin{small}
\begin{coq_example}
Variable bij : Set -> Set -> Set.
Variable ap : (A,B:Set)(bij A B) -> A -> B.
Coercion ap : bij >-> FUNCLASS.
Variable b : (bij nat nat).
Check (b O).
\end{coq_example}
%\end{small}
Let us see the resulting graph of this session.
%\begin{small}
\begin{coq_example}
Print Graph.
\end{coq_example}
%\end{small}
\end{itemize}
\asection{Classes as Records}
\index{Coercions!and records}
We allow the definition of {\em Structures with Inheritance} (or
classes as records) by extending the existing {\tt Record} macro
(see section~\ref{Record}). Its new syntax is:
\begin{center}
\begin{tabular}{l}
{\tt Record \zeroone{>}{\ident} [ {\params} ] : {\sort} := \zeroone{\ident$_0$} \verb+{+} \\
~~~~\begin{tabular}{l}
{\tt \ident$_1$ $[$:$|$:>$]$ \term$_1$ ;} \\
... \\
{\tt \ident$_n$ $[$:$|$:>$]$ \term$_n$ \verb+}+. }
\end{tabular}
\end{tabular}
\end{center}
The identifier {\ident} is the name of the defined record and {\sort}
is its type. The identifier {\ident$_0$} is the name of its
constructor. The identifiers {\ident$_1$}, .., {\ident$_n$} are the
names of its fields and {\term$_1$}, .., {\term$_n$} their respective
types. The alternative {\tt $[$:$|$:>$]$} is ``{\tt :}'' or ``{\tt
:>}''. If {\tt {\ident$_i$}:>{\term$_i$}}, then {\ident$_i$} is
automatically declared as coercion from {\ident} to the class of
{\term$_i$}. Remark that {\ident$_i$} always verifies the uniform
inheritance condition. The keyword
{\tt Structure}\comindex{Structure} is a synonym of {\tt
Record}.
\asection{Coercions and Sections}
\index{Coercions!and sections}
The inheritance mechanism is compatible with the section
mechanism. The global classes and coercions defined inside a section
are redefined after its closing, using their new value and new
type. The classes and coercions which are local to the section are
simply forgotten (no warning message is printed).
Coercions with a local source class or a local target class, and
coercions which do no more verify the uniform inheritance condition
are also forgotten.
\asection{Examples}
\begin{itemize}
\item Coercion between inductive types
\begin{coq_example}
Definition bool_in_nat := [b:bool]if b then O else (S O).
Coercion bool_in_nat : bool >-> nat.
Check O=true.
\end{coq_example}
\Warning
\item \verb|Check true=O.| fails. This is ``normal'' behaviour of
coercions. To validate \verb|true=O|, the coercion is searched from
\verb=nat= to \verb=bool=. There is no one.
\item Coercion to a sort
\begin{coq_eval}
Reset Graph.
\end{coq_eval}
\begin{coq_example}
Variable Graph : Type.
Variable Node : Graph -> Type.
Coercion Node : Graph >-> SORTCLASS.
Variable G : Graph.
Variable Arrows : G -> G -> Type.
Check Arrows.
Variable fg : G -> G.
Check fg.
\end{coq_example}
\item Coercion to a function
\begin{coq_example}
Variable bij : Set -> Set -> Set.
Variable ap : (A,B:Set)(bij A B) -> A -> B.
Coercion ap : bij >-> FUNCLASS.
Variable b : (bij nat nat).
Check (b O).
\end{coq_example}
\item Transitivity of coercion
\begin{coq_eval}
Reset C.
\end{coq_eval}
\begin{coq_example}
Variables C : nat -> Set; D : nat -> bool -> Set; E : bool -> Set.
Variable f : (n:nat)(C n) -> (D (S n) true).
Coercion f : C >-> D.
Variable g : (n:nat)(b:bool)(D n b) -> (E b).
Coercion g : D >-> E.
Variable c : (C O).
Variable T : (E true) -> nat.
Check (T c).
\end{coq_example}
\item Identity coercion
\begin{coq_example}
Definition D' := [b:bool](D (S O) b).
Identity Coercion IdD'D : D' >-> D.
Print IdD'D.
Variable d' : (D' true).
Check (T d').
\end{coq_example}
\end{itemize}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "Reference-Manual"
%%% End:
|