%%%% Commands to cover (and index):
%% Matrix stuff: matrix, map, source, target, jacobian, minors, det, resolution,
%% Ext, Tor, what a module is, presentation, coker, image, kernel, annihilator,
%% sheaf cohomology, transpose, prune, trim, random, chain complexes, dd, C.dd_i,
%% ring maps. saturate
%%% Didn't put in ``annihilator'', random, chain complexes

\title{Projective Geometry and Homological Algebra}
\titlerunning{Projective Geometry and Homological Algebra}
\toctitle{Projective Geometry and Homological Algebra}
\author{David Eisenbud\thanks{Supported by the NSF.}}
\authorrunning{D. Eisenbud}
% \institute{1000 Centennial Drive, Mathematical Sciences Research Institute, Berkeley, CA
% 94720, USA, and
% University of California at Berkeley, Department of Mathematics, Berkeley, CA
% 94720, USA}

\maketitle

\begin{abstract}
We provide an introduction to many of the homological
commands in \Mtwo (modules, free resolutions, Ext and Tor\dots)
by means of examples showing how to use homological tools to
study projective varieties.
\end{abstract}

%%% TeX requirements
% \input diagrams.tex
\def\P{{\mathbb P}} %% Projective space. Change to \Bbb if available -- done
\def\Z{{\mathbb Z}} %% The integers. Change to \Bbb if available -- done
\def\H{{\rm H}}
\def\cO{{\cal O}}
\def\O{{\cal O}}
\def\iso{{\cong}}

In this chapter we will illustrate how one can
manipulate
projective varieties and sheaves,
using the rich collection of tools
\Mtwo provides.
One of our goals is to show how homological
methods can be effective in solving concrete geometric problems.
\index{homological methods!introduction to}

The first four sections can be read by anyone who knows about
projective varieties at the level of a first graduate course
and knows the definitions of Ext and Tor. The last section assumes
that the reader is familiar with the theory of curves and surfaces
roughly at the level of the books of Hartshorne \cite{Hartshorne} and
Harris \cite{Harris}.

We will work with projective schemes over a field {\tt
kk}. \Mtwo can work over any finite field of characteristic at most
\index{32749}
32749, and also a variety of fields in characteristic 0 (except for
the primary decomposition commands, which at this writing are still
restricted to positive characteristics). Our main interest is in
geometry over an algebraically closed field of characteristic
0. Nevertheless, it is most convenient to work over a large prime
{}field.
\index{finite fields!use of}
It is known that the intermediate results in Gr\"obner basis
computations (as in the Euclidean Algorithm computations they
generalize) often involve coefficients far larger than those in the
input data, so that work in characteristic zero essentially requires
infinite precision arithmetic, a significant additional overhead. If
we work over a finite field where the scalars can be represented in
one machine word, we avoid this coefficient explosion. Experience with
the sort of computations we will be doing shows that working over
$\Z/p$, where $p$ is a moderately large prime, gives results
identical to the results we would get in characteristic 0.
%% This isn't strictly true -- look at all the coefficients in your output
%% that have more than 3 digits: their digits will probably change in as p
%% varies.
Of course one still
has to be careful about the fact that our fields are not
algebraically closed, especially when using primary
decomposition. The largest prime $p$ we can work with being 32749, we
choose the field ${\bf Z}/32749$.
The name of the \Mtwo constant representing the
integers is {\tt ZZ}, and by analogy we will call our field {\tt kk}:
\beginOutput
i1 : kk = ZZ/32749\\
\emptyLine
o1 = kk\\
\emptyLine
o1 : QuotientRing\\
\endOutput
%% I had to leave the characteristic at 32003, because your code breaks when
%% I put in 32749, which is actually the largest characteristic available.
%% The reason it breaks is probably that the coefficients in the polynomials
%% defining idealX are no longer correct, since they were precomputed in
%% characteristic 32003.  Should we compute idealX automatically?  One way
%% would be to define a function in a file called "mystery.m2" that computes
%% it for you.  You could reveal the code of the function later.

In \Mtwo we will represent \ie{projective space} $\P^n$ by its
homogeneous coordinate ring ${\tt ringPn} = {\tt kk}[x_0,\dots,x_n]$.  A
projective scheme $X$ in $\P^n$ may be most conveniently
represented, depending on the situation, by its \ie{homogeneous ideal} {\tt
idealX} or its \ie{homogeneous coordinate ring}, represented either as a
ring {\tt ringPn/idealX} or as a module {\tt OX} over {\tt ringPn}.
\index{coherent sheaf!representation of}
\index{sheaf!representation of}
Coherent sheaves on the projective space, or on its subvarieties, will
be represented by finitely generated
graded
modules over {\tt ringPn}, using the
\ie{Serre correspondence}.
%%% Dan, the previous line didn't print in the version you sent!
%%%  -- It's printing for me.
For example, the structure sheaf $\cO_X$
of the subvariety $X$ would be represented by the module
{\tt ringPn\char`\^1/idealX}; here {\tt ringPn\char`\^1}
denotes the free module of rank one over the ring
{\tt ringPn}.

\section{The Twisted Cubic}

\index{twisted cubic}\index{cubic space curve}
As a first illustration, we give three constructions
of the twisted cubic
curve in $\P^3$. We represent $\P^3$ by
\beginOutput
i2 : ringP3 = kk[x_0..x_3]\\
\emptyLine
o2 = ringP3\\
\emptyLine
o2 : PolynomialRing\\
\endOutput

The twisted cubic is the image of the map $\P^1\to\P^3$ sending
a point with homogeneous coordinates $(s,t)$ to the point with
homogeneous coordinates $(s^3, s^2t, st^2, t^3)$. We can compute
its relations directly with
\index{matrix}\index{map of rings}
\beginOutput
i3 : ringP1 = kk[s,t]\\
\emptyLine
o3 = ringP1\\
\emptyLine
o3 : PolynomialRing\\
\endOutput
\beginOutput
i4 : cubicMap = map(ringP1,ringP3,\{s^3, s^2*t, s*t^2, t^3\})\\
\emptyLine
\                         3   2      2   3\\
o4 = map(ringP1,ringP3,\{s , s t, s*t , t \})\\
\emptyLine
o4 : RingMap ringP1 <--- ringP3\\
\endOutput
\index{kernel of a ring map}%
\beginOutput
i5 : idealCubic = kernel cubicMap\\
\emptyLine
\             2                       2\\
o5 = ideal (x  - x x , x x  - x x , x  - x x )\\
\             2    1 3   1 2    0 3   1    0 2\\
\emptyLine
o5 : Ideal of ringP3\\
\endOutput
We could also use \Mtwo's built-in facility, and say
\indexcmd{monomialCurveIdeal}
\beginOutput
i6 : idealCubic2 = monomialCurveIdeal(ringP3,\{1,2,3\})\\
\emptyLine
\                          2          2\\
o6 = ideal (x x  - x x , x  - x x , x  - x x )\\
\             1 2    0 3   2    1 3   1    0 2\\
\emptyLine
o6 : Ideal of ringP3\\
\endOutput
which uses precisely the same method.

Of course we might remember that the ideal of the twisted
cubic is generated by the $2\times 2$ minors of the
matrix
$$\begin{pmatrix}
x_0&x_1&x_2\cr
x_1&x_2&x_3
\end{pmatrix},
$$
which we can realize with the commands
\beginOutput
i7 : M = matrix\{\{x_0,x_1,x_2\},\{x_1,x_2,x_3\}\}\\
\emptyLine
o7 = | x_0 x_1 x_2 |\\
\     | x_1 x_2 x_3 |\\
\emptyLine
\                  2            3\\
o7 : Matrix ringP3  <--- ringP3\\
\endOutput
\indexcmd{minors}\index{ideal}%
\beginOutput
i8 : idealCubic3 = minors(2, M)\\
\emptyLine
\               2                           2\\
o8 = ideal (- x  + x x , - x x  + x x , - x  + x x )\\
\               1    0 2     1 2    0 3     2    1 3\\
\emptyLine
o8 : Ideal of ringP3\\
\endOutput
We can get some useful information about the ideal
{\tt idealCubic} with
\indexcmd{codimension}%
\beginOutput
i9 : codim idealCubic\\
\emptyLine
o9 = 2\\
\endOutput
\index{degree of a projective variety}%
\beginOutput
i10 : degree idealCubic\\
\emptyLine
o10 = 3\\
\endOutput
This shows that we do indeed have a cubic curve. Note that the
command
\index{dimension of a projective variety}%
\beginOutput
i11 : dim idealCubic\\
\emptyLine
o11 = 2\\
\endOutput
gives 2, not 1; it represents the dimension of the ideal
in {\tt ringP3}, the dimension of the affine cone over the
curve.

We can easily assure ourselves that these ideals are the same.
{}For example, to see whether the ideal {\tt idealCubic}
is contained in the ideal of minors of {\tt M}, we can
reduce the former modulo the latter, and see whether
we get zero. The reduction operator {\tt \%} takes two
maps with the same target as its arguments, so
we must replace each ideal by a matrix whose entries
generate it. This is done by the function {\tt gens} as in
\indexcmd{gens}\indexcmd{generators}%
\beginOutput
i12 : gens idealCubic\\
\emptyLine
o12 = | x_2^2-x_1x_3 x_1x_2-x_0x_3 x_1^2-x_0x_2 |\\
\emptyLine
\                   1            3\\
o12 : Matrix ringP3  <--- ringP3\\
\endOutput
Thus for one of the inclusions we check
\indexcmd{\%}%
\index{reduced form}%
\beginOutput
i13 : 0 == (gens idealCubic){\char`\%}(gens idealCubic3)\\
\emptyLine
o13 = true\\
\endOutput
Both inclusions can be checked automatically in this way
with
\beginOutput
i14 : idealCubic == idealCubic3\\
\emptyLine
o14 = true\\
\endOutput

\section{The Cotangent Bundle of $\P^3$}

\index{cotangent bundle}%
Many invariants of varieties are defined in
terms of their tangent and cotangent bundles. We
identify a bundle with its sheaf of
sections, which is locally free.
Any coherent locally free sheaf arises this way.
(One can also regard a bundle
as a variety in its own right, but this view
is used in algebraic geometry more rarely.)
In this section and the next we construct
the cotangent  bundle $\Omega_{\P^3}$ of $\P^3$ and
its restriction to the twisted cubic above.

Consulting Hartshorne \cite[Theorem II.8.13]{Hartshorne}, we find
that the cotangent bundle to $\P^n$ can be described
by the {\it cotangent sequence\/}:
\index{cotangent sequence}%
$$
0\rTo
\Omega_{\P^n}\rTo
\cO_{\P^n}(-1)^{n+1}\rTo^f
\cO_{\P^n} \rTo
0
$$
where $f$ is defined by the matrix
of variables $(x_0,\dots,x_n)$.
We can translate this description directly into the
language of \Mtwo, here in the case $n=3$:
\beginOutput
i15 : f = vars ringP3\\
\emptyLine
o15 = | x_0 x_1 x_2 x_3 |\\
\emptyLine
\                   1            4\\
o15 : Matrix ringP3  <--- ringP3\\
\endOutput
\index{kernel of a module map}%
\beginOutput
i16 : OmegaP3 = kernel f\\
\emptyLine
o16 = image \{1\} | 0    0    0    -x_1 -x_2 -x_3 |\\
\            \{1\} | 0    -x_2 -x_3 x_0  0    0    |\\
\            \{1\} | -x_3 x_1  0    0    x_0  0    |\\
\            \{1\} | x_2  0    x_1  0    0    x_0  |\\
\emptyLine
\                                        4\\
o16 : ringP3-module, submodule of ringP3\\
\endOutput
\index{modules!how to represent}%
Note that the module which we specified as a kernel
is now given as the image of a matrix. We can recover this
matrix with
\beginOutput
i17 : g=generators OmegaP3\\
\emptyLine
o17 = \{1\} | 0    0    0    -x_1 -x_2 -x_3 |\\
\      \{1\} | 0    -x_2 -x_3 x_0  0    0    |\\
\      \{1\} | -x_3 x_1  0    0    x_0  0    |\\
\      \{1\} | x_2  0    x_1  0    0    x_0  |\\
\emptyLine
\                   4            6\\
o17 : Matrix ringP3  <--- ringP3\\
\endOutput
and we could correspondingly write
\indexcmd{image}%
\beginOutput
i18 : OmegaP3=image g\\
\emptyLine
o18 = image \{1\} | 0    0    0    -x_1 -x_2 -x_3 |\\
\            \{1\} | 0    -x_2 -x_3 x_0  0    0    |\\
\            \{1\} | -x_3 x_1  0    0    x_0  0    |\\
\            \{1\} | x_2  0    x_1  0    0    x_0  |\\
\emptyLine
\                                        4\\
o18 : ringP3-module, submodule of ringP3\\
\endOutput
An even more elementary way to give a module is by generators
and relations, and we can see this ``free presentation'' too with
\indexcmd{presentation}%
\beginOutput
i19 : presentation OmegaP3\\
\emptyLine
o19 = \{2\} | x_1  0    0    x_0  |\\
\      \{2\} | x_3  x_0  0    0    |\\
\      \{2\} | -x_2 0    x_0  0    |\\
\      \{2\} | 0    x_2  x_3  0    |\\
\      \{2\} | 0    -x_1 0    x_3  |\\
\      \{2\} | 0    0    -x_1 -x_2 |\\
\emptyLine
\                   6            4\\
o19 : Matrix ringP3  <--- ringP3\\
\endOutput
The astute reader will have noticed that we have just been computing
the first few terms in the free resolution of the cokernel of the
map of free modules corresponding to
{\tt f}. We could see the whole resolution at once with
\index{resolution!free}\indexcmd{res}\indexcmd{coker}\indexcmd{cokernel}
\index{chain complex}\index{complex}
\beginOutput
i20 : G = res coker f\\
\emptyLine
\            1           4           6           4           1\\
o20 = ringP3  <-- ringP3  <-- ringP3  <-- ringP3  <-- ringP3  <-- 0\\
\                                                                   \\
\      0           1           2           3           4           5\\
\emptyLine
o20 : ChainComplex\\
\endOutput
and then see all the matrices in the resolution with
\indexcmd{dd}\index{differentials of a complex}
\index{complex!differentials in}
\beginOutput
i21 : G.dd\\
\emptyLine
\                1                                4\\
o21 = 0 : ringP3  <----------------------- ringP3  : 1\\
\                     | x_0 x_1 x_2 x_3 |\\
\emptyLine
\                4                                                  6\\
\      1 : ringP3  <----------------------------------------- ringP3  : 2\\
\                     \{1\} | -x_1 -x_2 0    -x_3 0    0    |\\
\                     \{1\} | x_0  0    -x_2 0    -x_3 0    |\\
\                     \{1\} | 0    x_0  x_1  0    0    -x_3 |\\
\                     \{1\} | 0    0    0    x_0  x_1  x_2  |\\
\emptyLine
\                6                                        4\\
\      2 : ringP3  <------------------------------- ringP3  : 3\\
\                     \{2\} | x_2  x_3  0    0    |\\
\                     \{2\} | -x_1 0    x_3  0    |\\
\                     \{2\} | x_0  0    0    x_3  |\\
\                     \{2\} | 0    -x_1 -x_2 0    |\\
\                     \{2\} | 0    x_0  0    -x_2 |\\
\                     \{2\} | 0    0    x_0  x_1  |\\
\emptyLine
\                4                         1\\
\      3 : ringP3  <---------------- ringP3  : 4\\
\                     \{3\} | -x_3 |\\
\                     \{3\} | x_2  |\\
\                     \{3\} | -x_1 |\\
\                     \{3\} | x_0  |\\
\emptyLine
\                1\\
\      4 : ringP3  <----- 0 : 5\\
\                     0\\
\emptyLine
o21 : ChainComplexMap\\
\endOutput
or just one of them, say the second, with
\index{dd\_i@{\tt dd\_i}, i-th differential of a complex}
\beginOutput
i22 : G.dd_2\\
\emptyLine
o22 = \{1\} | -x_1 -x_2 0    -x_3 0    0    |\\
\      \{1\} | x_0  0    -x_2 0    -x_3 0    |\\
\      \{1\} | 0    x_0  x_1  0    0    -x_3 |\\
\      \{1\} | 0    0    0    x_0  x_1  x_2  |\\
\emptyLine
\                   4            6\\
o22 : Matrix ringP3  <--- ringP3\\
\endOutput
Note that this matrix does not look exactly the same as
the matrix produced by computing the kernel of {\tt f}.
This is because when \Mtwo is asked to compute a whole
resolution, it does not do the ``obvious'' thing and
compute kernels over and over; it defaults to a more efficient
algorithm, first proposed by Frank Schreyer
\cite[Appendix]{s1}.
\index{Schreyer's algorithm for free resolutions}

Any graded map of free modules, such as a map in a graded
{}free resolution of a graded module, comes with some numerical data:
the degrees of the
generators of the source and target free modules.
We can extract this information one module at a time with
the command {\tt degrees}, as in
\indexcmd{degrees}
\indexcmd{source}\indexcmd{target}
\beginOutput
i23 : degrees source G.dd_2\\
\emptyLine
o23 = \{\{2\}, \{2\}, \{2\}, \{2\}, \{2\}, \{2\}\}\\
\emptyLine
o23 : List\\
\endOutput
\beginOutput
i24 : degrees target G.dd_2\\
\emptyLine
o24 = \{\{1\}, \{1\}, \{1\}, \{1\}\}\\
\emptyLine
o24 : List\\
\endOutput

\Mtwo has a more convenient
mechanism for examining this numerical data,
which we take time out to explain. First, for the resolution just
computed, we can call
\beginOutput
i25 : betti G\\
\emptyLine
o25 = total: 1 4 6 4 1\\
\          0: 1 4 6 4 1\\
\endOutput
\index{Betti diagram}\index{diagram, Betti}\indexcmd{betti}%
The diagram shows the degrees of the generators of each free module
in the resolution in coded form. To understand the code, it may
be helpful to look at a less symmetric example, say the free
resolution of {\tt ringP3\char`\^1/I} where $I$ is
the ideal generated by the minors of the following $2\times 4$ matrix.
\beginOutput
i26 : m = matrix\{\{x_0^3, x_1^2, x_2,x_3\},\{x_1^3,x_2^2,x_3,0\}\}\\
\emptyLine
o26 = | x_0^3 x_1^2 x_2 x_3 |\\
\      | x_1^3 x_2^2 x_3 0   |\\
\emptyLine
\                   2            4\\
o26 : Matrix ringP3  <--- ringP3\\
\endOutput
We do this with
\beginOutput
i27 : I = minors(2,m)\\
\emptyLine
\                5    3 2     3      3       3    2      3      2      2\\
o27 = ideal (- x  + x x , - x x  + x x , - x  + x x , -x x , -x x , -x )\\
\                1    0 2     1 2    0 3     2    1 3    1 3    2 3    3\\
\emptyLine
o27 : Ideal of ringP3\\
\endOutput
\beginOutput
i28 : F = res(ringP3^1/I)\\
\emptyLine
\            1           6           8           3\\
o28 = ringP3  <-- ringP3  <-- ringP3  <-- ringP3  <-- 0\\
\                                                       \\
\      0           1           2           3           4\\
\emptyLine
o28 : ChainComplex\\
\endOutput
\beginOutput
i29 : betti F\\
\emptyLine
o29 = total: 1 6 8 3\\
\          0: 1 . . .\\
\          1: . 1 . .\\
\          2: . 2 2 .\\
\          3: . 2 2 .\\
\          4: . 1 4 3\\
\endOutput

The resulting Betti diagram should be interpreted as follows.
{}First, the maps go from right to left, so the beginning of the
resolution is on the left. The given Betti diagram
thus corresponds to an exact sequence of graded free modules
$$
{}F_0\lTo F_1\lTo F_2\lTo F_3\lTo 0.
$$
The top row of the diagram, 1,6,8,3, shows the
ranks of the free modules $F_i$ in the resolution. For example the 1
on the left means that $F_0$ has rank 1 (and,
indeed, the module {\tt ringP3\char`\^1/I} we are resolving is cyclic).
The 6 shows that the rank of $F_1$ is 6, or equivalently that
the ideal {\tt I} is minimally generated by 6 elements---in this
case the $6 = {\binom 4 2}$ minors of size 2 of the $2\times 4$
matrix $m$.

The first column of the diagram shows degrees. The successive
columns indicate how many generators of each degree occur in the
successive $F_i$.  The free module $F_0$ has a single generator in
degree 0, and this is the significance of the second column.  Note
that $F_1$ could not have any generators of degree less than or equal
to zero, because the resolution is minimal! Thus for compactness, the
diagram is skewed: in each successive column the places correspond to
larger degrees.  More precisely, a number $a$ occurring opposite the
degree indication ``{\tt i:}'' in the column corresponding to $F_j$ signifies
that $F_j$ has $a$ generators in degree $i+j$.  Thus for example the 1
in the third column opposite the one on the left corresponds to a
generator of degree 2 in the free module $F_1$; and
altogether $F_1$ has one generator of degree 2, two generators of
degree 3, two of degree 4 and one of degree 5.

Returning to the diagram
\beginOutput
i30 : betti G\\
\emptyLine
o30 = total: 1 4 6 4 1\\
\          0: 1 4 6 4 1\\
\endOutput
we see that the successive free modules of {\tt G} are
each generated in degree 1 higher than the previous one; that
is, the matrices in {\tt G.dd} all have linear entries, as
we have already seen.

\section{The Cotangent Bundle of a Projective Variety}

\index{cotangent bundle}%
It is easy to construct the cotangent bundle $\Omega_X$ of a projective
variety $X$ starting from the cotangent bundle
of the ambient
projective space. We use the {\it conormal sequence}
\index{conormal sequence}
(Hartshorne \cite[Proposition II.8.12]{Hartshorne} or Eisenbud
\cite[Proposition 16.3]{eCA}).
Writing $I$ for the ideal of a variety $X$ in $\P^n$ there is
an exact sequence of sheaves
$$
I \rTo^\delta \Omega_{\P^n}\otimes \cO_X\rTo \Omega_X\rTo 0
$$
where the map $\delta$ takes a function $f$
to the element $df\otimes 1$. If $I$ is generated by
{}forms $f_1,\dots,f_m$ then $\delta$ is represented
by the \ie{Jacobian matrix} $(df_i/dx_j)$.

{}First of all, we must compute a module corresponding to
$\Omega_{\P^n}\otimes \cO_X$, the restriction of the sheaf
$\Omega_{\P^n}$ to $X$. The simplest approach would be
to take the tensor product of graded modules representing
$\Omega_{\P^n}$ and  $\cO_X$. The result would represent the
right sheaf, but would not be the module of twisted global
sections of $\Omega_{\P^n}\otimes \cO_X$ (the unique module
of depth two representing the sheaf). This would make further
computations less efficient.

Thus we take a different approach:
since the cotangent sequence given
in the previous section
is a sequence of locally free sheaves, it is locally split, and
thus remains exact when tensored by $\cO_X$. Consequently
$\Omega_{\P^n}\otimes \cO_X$ is also represented by the kernel
of the map $f\otimes  \cO_X$, where $f$ is the map used
in the definition of the cotangent bundle of $\P^n$.
In \Mtwo, working on $\P^3$, with $X$ the twisted cubic, we
can translate this into
\beginOutput
i31 : OmegaP3res = kernel (f ** (ringP3^1/idealCubic))\\
\emptyLine
o31 = subquotient (\{1\} | -x_3 0    0    -x_2 -x_3 0    -x_1 -x_2 -x_3  $\cdot\cdot\cdot$\\
\                   \{1\} | x_2  -x_3 0    x_1  0    -x_3 x_0  0    0     $\cdot\cdot\cdot$\\
\                   \{1\} | 0    x_2  -x_3 0    x_1  0    0    x_0  0     $\cdot\cdot\cdot$\\
\                   \{1\} | 0    0    x_2  0    0    x_1  0    0    x_0   $\cdot\cdot\cdot$\\
\emptyLine
\                                          4\\
o31 : ringP3-module, subquotient of ringP3\\
\endOutput
(The operator {\tt \char`\*\char`\*} is \Mtwo's symbol for tensor product.)
Since the map is a map between free modules
over {\tt ringP3/idealCubic}, the kernel has depth (at least) two.

Next, we form the Jacobian matrix of the generators
of
% the ideal
{\tt idealCubic}, which represents a map
from this ideal to the free module {\tt ringP3\char`\^4}.
\indexcmd{jacobian}
\beginOutput
i32 : delta1 = jacobian idealCubic\\
\emptyLine
o32 = \{1\} | 0    -x_3 -x_2 |\\
\      \{1\} | -x_3 x_2  2x_1 |\\
\      \{1\} | 2x_2 x_1  -x_0 |\\
\      \{1\} | -x_1 -x_0 0    |\\
\emptyLine
\                   4            3\\
o32 : Matrix ringP3  <--- ringP3\\
\endOutput
We need to make this into a map to {\tt OmegaP3res}, which
as defined is a subquotient of {\tt ringP3\char`\^4}. To this end
we must first express the image of {\tt delta1} in terms of
the generators of {\tt OmegaP3res}. The \ie{division command} {\tt //}
does this with
\beginOutput
i33 : delta2 = delta1 // (gens OmegaP3res)\\
\emptyLine
o33 = \{2\} | 0  1  0  |\\
\      \{2\} | 2  0  0  |\\
\      \{2\} | 0  0  0  |\\
\      \{2\} | 0  0  2  |\\
\      \{2\} | 0  1  0  |\\
\      \{2\} | -1 0  0  |\\
\      \{2\} | 0  0  0  |\\
\      \{2\} | 0  0  -1 |\\
\      \{2\} | 0  -1 0  |\\
\emptyLine
\                   9            3\\
o33 : Matrix ringP3  <--- ringP3\\
\endOutput
Once this is done we can use this matrix to form the
necessary map $\delta: I\to \Omega_{\P^3}\otimes\O_X$:
\index{map of modules}
\beginOutput
i34 : delta = map(OmegaP3res, module idealCubic, delta2)\\
\emptyLine
o34 = \{2\} | 0  1  0  |\\
\      \{2\} | 2  0  0  |\\
\      \{2\} | 0  0  0  |\\
\      \{2\} | 0  0  2  |\\
\      \{2\} | 0  1  0  |\\
\      \{2\} | -1 0  0  |\\
\      \{2\} | 0  0  0  |\\
\      \{2\} | 0  0  -1 |\\
\      \{2\} | 0  -1 0  |\\
\emptyLine
o34 : Matrix\\
\endOutput
A minimal free presentation of $\Omega_X$ --- or rather of
one module over
{\tt ringP3} that represents it --- can be obtained with
\indexcmd{prune}\index{presentation!minimal}
\beginOutput
i35 : OmegaCubic = prune coker delta\\
\emptyLine
o35 = cokernel \{2\} | -10917x_3 0    -10917x_3 x_2      0        0      $\cdot\cdot\cdot$\\
\               \{2\} | 0         0    x_2       0        16374x_3 0      $\cdot\cdot\cdot$\\
\               \{2\} | 0         -x_3 0         16373x_3 0        x_2    $\cdot\cdot\cdot$\\
\               \{2\} | x_3       x_2  0         0        0        0      $\cdot\cdot\cdot$\\
\               \{2\} | 0         0    0         0        0        -2x_3  $\cdot\cdot\cdot$\\
\               \{2\} | 0         0    0         0        x_2      0      $\cdot\cdot\cdot$\\
\emptyLine
\                                       6\\
o35 : ringP3-module, quotient of ringP3\\
\endOutput
We have used the function {\tt prune} to compute
minimal presentation matrices; these often make subsequent
computations faster, and also allow us to inspect the final
answer more easily.

The module {\tt OmegaCubic} represents the sheaf $\Omega_{X}$, where
$X$ is the cubic, but it is not the simplest possibility.
A better representative is the graded module
$\oplus_{d\in \Z} \H^0(\Omega_X(d))$. We can at least find
a minimal presentation of the
submodule $\oplus_{d\geq 0} \H^0(\Omega_X(d))$ with
\index{sheaf cohomology}\index{cohomology!sheaf}\indexcmd{HH}\indexcmd{sheaf}
\beginOutput
i36 : prune HH^0((sheaf OmegaCubic)(>=0))\\
\emptyLine
o36 = cokernel \{1\} | 16374x_3 16374x_2 16374x_1 |\\
\               \{1\} | x_2      x_1      x_0      |\\
\emptyLine
\                                       2\\
o36 : ringP3-module, quotient of ringP3\\
\endOutput
The large coefficients appearing in the matrix arise in finite characteristic as the result
of chance division by small integers.
We see from the degrees labeling the rows of the matrix in the
output of this command that
the generators of the submodule are in degree 1,
so in particular $\H^0(\Omega_X) = 0$.
It follows that that
$\H^0(\Omega_X(d)) = 0$ for all $d\leq 0$, so
the submodule we computed was actually the whole module
that we wanted! (If this had not been the case we could have tried
{\tt HH\char`\^0((sheaf OmegaCubic)(>=d))} to compute the cohomology
of all the twists greater than a given negative integer $d$,
or simply used the submodule we had already computed, since
it also represents the sheaf $\Omega_X$.)

The sequence of commands we have used to construct the
cotangent sheaf can be obtained also with
the following built-in commands.
\beginOutput
i37 : Cubic = Proj(ringP3/idealCubic)\\
\emptyLine
o37 = Cubic\\
\emptyLine
o37 : ProjectiveVariety\\
\endOutput
\beginOutput
i38 : cotangentSheaf Cubic\\
\emptyLine
o38 = cokernel \{1\} | x_2  x_1  x_0  |\\
\               \{1\} | -x_3 -x_2 -x_1 |\\
\emptyLine
\                                                  2\\
o38 : coherent sheaf on Cubic, quotient of OO      (-1)\\
\                                             Cubic\\
\endOutput

Since $X$ is a smooth curve, its cotangent bundle is equal
to its {\it canonical bundle}, and also to its {\it dualizing sheaf}
(see Hartshorne \cite[sections II.8 and III.7]{Hartshorne}
{}for definitions).
\index{cotangent bundle}\index{canonical bundle}\index{dualizing sheaf}
We will
see another (generally more efficient) method of computing this
dualizing sheaf by using {\tt Ext} and \ie{duality} theory.

\goodbreak

\section{Intersections by Serre's Method}

\index{intersection theory}\index{Serre's intersection formula}%
To introduce homological algebra in a simple geometric context,
consider the problem of computing the \ie{intersection multiplicities}
of two varieties $X$ and $Y$ in $\P^n$, assuming for simplicity
that $\mathop{\rm dim} X +\mathop{\rm dim} Y = n$ and that the two
meet in a zero-dimensional scheme. Beginning in the
19th century, many people struggled to make a definition of
local intersection multiplicity
that would make {\it B\'ezout's Theorem\/} true: the product
of the degrees of $X$ and $Y$ should be the number of points
of intersection, each counted with its local intersection multiplicity
(multiplied by the degree of the point, if the point is not
rational over the ground field).
\index{Bezout's Theorem@B\'ezout's Theorem}
In the simplest case, where the two varieties are Cohen-Macaulay,
the right answer is that a point $p$ should count with multiplicity
equal to the length of the local ring
\index{length of a module}
$\cO_{X,p}\otimes_{\cO_{\P^n,p}} \cO_{Y,p}$,
and at first it was naively assumed that this would be the right answer
in general.

Here is a famous example in ${\bf P}^4$ showing that
the naive value can be wrong: in it, the scheme $X$ is a 2-plane
and the scheme $Y=L_1\cup L_2$ is the union of two 2-planes.
The planes $L_1$ and $L_2$ meet at just one point $p$,
and we assume that $X$ passes through $p$ as well, and
is general enough so that it meets
$Y$ only in $p$.
Since $\mathop{\rm degree}(X) = 1, \mathop{\rm degree}(Y) = 2$,
B\'ezout's Theorem requires that
the multiplicity of the intersection at $p$ should be 2.
However, we have:
\beginOutput
i39 : ringP4 = kk[x_0..x_4]\\
\emptyLine
o39 = ringP4\\
\emptyLine
o39 : PolynomialRing\\
\endOutput
\beginOutput
i40 : idealX = ideal(x_1+x_3, x_2+x_4)\\
\emptyLine
o40 = ideal (x  + x , x  + x )\\
\              1    3   2    4\\
\emptyLine
o40 : Ideal of ringP4\\
\endOutput
\beginOutput
i41 : idealL1 = ideal(x_1,x_2)\\
\emptyLine
o41 = ideal (x , x )\\
\              1   2\\
\emptyLine
o41 : Ideal of ringP4\\
\endOutput
\beginOutput
i42 : idealL2 = ideal(x_3,x_4)\\
\emptyLine
o42 = ideal (x , x )\\
\              3   4\\
\emptyLine
o42 : Ideal of ringP4\\
\endOutput
\beginOutput
i43 : idealY = intersect(idealL1,idealL2)\\
\emptyLine
o43 = ideal (x x , x x , x x , x x )\\
\              2 4   1 4   2 3   1 3\\
\emptyLine
o43 : Ideal of ringP4\\
\endOutput
\beginOutput
i44 : degree(idealX+idealY)\\
\emptyLine
o44 = 3\\
\endOutput
That is,
the length of
$\cO_{X,p}\otimes_{\cO_{\P^n,p}} \cO_{Y,p}$
is 3 rather than 2. (We can do this computation
without first passing to local rings because there
is only one point of intersection, and because all the
constructions we are using commute with localization.)

It was the happy discovery
of Jean-Pierre Serre \cite[V.B.3]{Serre} that the naive
measure of intersection multiplicity can be fixed in a simple
way that works for all intersections in smooth varieties.
One simply replaces the length of the tensor product
$$
\cO_{X,p}\otimes_{\cO_{\P^n,p}} \cO_{Y,p}=
\mathop{\rm Tor}\nolimits_0^{\cO_{{\P^n},p} }
               (\cO_{X,p}, \cO_{Y,p})
$$
with the alternating sum of the Tor functors
$$
\sum_i(-1)^i \mathop{\rm length}
\mathop{\rm Tor}\nolimits_i^{\cO_{{\P^n},p} }
               (\cO_{X,p}, \cO_{Y,p}).
$$
In \Mtwo we can proceed as follows:
\indexcmd{Tor}
\beginOutput
i45 : degree Tor_0(ringP4^1/idealX, ringP4^1/idealY)\\
\emptyLine
o45 = 3\\
\endOutput
\beginOutput
i46 : degree Tor_1(ringP4^1/idealX, ringP4^1/idealY)\\
\emptyLine
o46 = 1\\
\endOutput
\beginOutput
i47 : degree Tor_2(ringP4^1/idealX, ringP4^1/idealY)\\
\emptyLine
o47 = 0\\
\endOutput
The other Tor's are 0 because the projective
dimension of {\tt ringP4\char`\^1/idealX} is only two,
as we see from
\beginOutput
i48 : res (ringP4^1/idealX)\\
\emptyLine
\            1           2           1\\
o48 = ringP4  <-- ringP4  <-- ringP4  <-- 0\\
\                                           \\
\      0           1           2           3\\
\emptyLine
o48 : ChainComplex\\
\endOutput
Thus, indeed, the alternating sum is 2, and B\'ezout's
Theorem is upheld.

\section{A Mystery Variety in $\P^3$}

In the file {\tt mystery.m2} is a function called {\tt mystery} that will
compute the ideal of a subvariety $X$ of $\P^3$.  We'll reveal what it does
at the end of the chapter.  Let's run it.
\beginOutput
i49 : ringP3 = kk[x_0..x_3];\\
\endOutput
\beginOutput
i50 : load "mystery.m2"\\
\endOutput
\beginOutput
i51 : idealX = mystery ringP3\\
\emptyLine
\              4       2      2        2 2   2 2           2            $\cdot\cdot\cdot$\\
o51 = ideal (x  - 2x x x  - x x x  + x x , x x  - 10915x x x  - 10917x $\cdot\cdot\cdot$\\
\              1     0 1 3    1 2 3    0 3   0 1         0 1 2          $\cdot\cdot\cdot$\\
\emptyLine
o51 : Ideal of ringP3\\
\endOutput
We can't see all the generators of the ideal; the same file contains a
function {\tt prettyPrint} which will display the generators visibly.
\beginOutput
i52 : prettyPrint gens idealX\\
x_1^4-2*x_0*x_1^2*x_3-x_1^2*x_2*x_3+x_0^2*x_3^2,\\
x_0^2*x_1^2-10915*x_0*x_1^2*x_2-10917*x_0^3*x_3+10916*x_0^2*x_2*x_3-\\
\   10916*x_0*x_2^2*x_3-10916*x_1*x_3^3,\\
x_0*x_1^2*x_2^2+11909*x_0^4*x_3+5954*x_0^3*x_2*x_3+2977*x_0^2*x_2^2*x_3+\\
\   11910*x_0*x_2^3*x_3-2978*x_1^3*x_3^2+14887*x_0*x_1*x_3^3+\\
\   11910*x_1*x_2*x_3^3,\\
x_0*x_1^3*x_2-13099*x_1^3*x_2^2-6550*x_0^3*x_1*x_3-\\
\   13100*x_0^2*x_1*x_2*x_3-6550*x_0*x_1*x_2^2*x_3+13099*x_1*x_2^3*x_3+\\
\   13100*x_1^2*x_3^3+13099*x_0*x_3^4,\\
x_0^5+5*x_0^2*x_2^3+5*x_0*x_2^4-3*x_0*x_1^3*x_3-4*x_1^3*x_2*x_3+\\
\   4*x_0^2*x_1*x_3^2+10*x_0*x_1*x_2*x_3^2+5*x_1*x_2^2*x_3^2,\\
x_1^2*x_2^4-8932*x_0^4*x_2*x_3+11909*x_0^3*x_2^2*x_3+5954*x_0^2*x_2^3*x_3-\\
\   8934*x_0*x_2^4*x_3-x_2^5*x_3+2*x_0*x_1^3*x_3^2-5952*x_1^3*x_2*x_3^2-\\
\   x_0^2*x_1*x_3^3-2979*x_0*x_1*x_2*x_3^3-8934*x_1*x_2^2*x_3^3+x_3^6\\
\endOutput
Imagine that you found yourself looking at the
scheme $X$ in $\P^3$ defined by the 6 equations above.
\beginOutput
i53 : X = variety idealX\\
\emptyLine
o53 = X\\
\emptyLine
o53 : ProjectiveVariety\\
\endOutput
How would you analyze the scheme $X$?
We will illustrate one approach.

In outline, we will first look at the topological invariants:
\index{topology of a projective variety}
the number and dimensions of the irreducible components,
and how they meet if there is more than one; the topological
type of each component; and the degree of each component in
$\P^3$. We will then see what we can say about the analytic
invariants of $X$ using \ie{adjunction theory} (we give some references
at the end).

Since we are interested in the projective scheme defined by
{\tt idealX} we could work with any ideal having the same
saturation. It is usually the case that working with the
saturation itself greatly eases subsequent computation so,
as a matter of good practice,
we begin by checking whether the ideal is saturated. If
not, we should replace it with its saturation.
\indexcmd{saturate}\index{saturation}
\beginOutput
i54 : idealX == saturate idealX\\
\emptyLine
o54 = true\\
\endOutput
Thus we see that {\tt idealX} is already saturated.
Perhaps the most basic invariant of $X$ is its dimension:
\beginOutput
i55 : dim X\\
\emptyLine
o55 = 1\\
\endOutput
This shows that {\tt X} consists of a curve, and
possibly some zero-dimensional components.
The command
\beginOutput
i56 : idealXtop = top idealX\\
\emptyLine
\              4       2      2        2 2   2 2           2            $\cdot\cdot\cdot$\\
o56 = ideal (x  - 2x x x  - x x x  + x x , x x  - 10915x x x  - 10917x $\cdot\cdot\cdot$\\
\              1     0 1 3    1 2 3    0 3   0 1         0 1 2          $\cdot\cdot\cdot$\\
\emptyLine
o56 : Ideal of ringP3\\
\endOutput
\index{top dimensional part of an ideal}%
%% Dan the \index command above seems to insert an extra space
%% at the beginning of the line!
%%
%% The extra space comes from not putting a % at the end of the line.
returns the ideal of the largest dimensional components of {\tt X}.
If there were 0-dimensional components (or if idealX were not saturated)
then {\tt idealXtop} would be larger than {\tt idealX}.
To test this we reduce {\tt idealXtop} modulo {\tt idealX}
and see whether we get 0:
\beginOutput
i57 : (gens idealXtop){\char`\%}(gens idealX) == 0\\
\emptyLine
o57 = true\\
\endOutput
Thus
{\tt X}
is a purely one-dimensional scheme.

Is {\tt X}
singular?
\beginOutput
i58 : codim singularLocus idealX\\
\emptyLine
o58 = 4\\
\endOutput
%%Dan, Same spacing problem again, below!
\index{singular locus of a scheme}%
%% Same fix, above.
A variety of codimension 4 in $\P^3$ must be empty, so $X$
is a nonsingular curve.

A nonsingular curve in $\P^3$ could still be reducible,
but since the intersection of two components
would be a singular point, the curve would then be disconnected.
A straightforward
way to decide is to use the command {\tt decompose},
which returns a list of irreducible components defined over {\tt kk}.
The length
of this list,
\beginOutput
i59 : # decompose idealX\\
\emptyLine
o59 = 1\\
\endOutput
\indexcmd{decompose}\index{decompose a variety}%
\index{primary decomposition}%
\index{irreducible decomposition}%
is thus the number of irreducible components
that are defined over {\tt kk}, and
we see there is only one. (Warning: at this writing (December 2000),
the command
``decompose'' works only in positive characteristic).

Often what we really want to know is
whether {\tt X} is
{\it absolutely irreducible\/} (that is, irreducible over the
algebraic closure of {\tt kk}).
\index{irreducible!absolutely}
\index{absolutely irreducible}
The property of being smooth transfers to the algebraic
closure, so again the question is the number of connected
components we would get over the algebraic closure.
{}For any reduced scheme {\tt X} over a perfect
{}field (such as our finite field {\tt kk}) this number is
$\mathop{\rm h}\nolimits^0\cO_X := \mathop{\rm dim}\nolimits_{\tt kk}\mathop{\rm H}\nolimits^0\cO_X$.
We compute it with
\beginOutput
i60 : HH^0 OO_X\\
\emptyLine
\        1\\
o60 = kk\\
\emptyLine
o60 : kk-module, free\\
\endOutput
\beginOutput
i61 : rank oo\\
\emptyLine
o61 = 1\\
\endOutput
This command works much faster than the decompose command.
(You can compute the time by adding the command {\tt time}
\indexcmd{time}
to the beginning of the line where the command to be timed
starts.) Since we already know that {\tt idealX} is saturated,
this also shows that {\tt idealX} is prime.

We next ask for the genus of the curve $X$.
\index{genus of a curve}
Here the {\it genus} may be defined as the dimension of
the space $\operatorname{H}\nolimits^1\cO_X$.
We can get this space with
\beginOutput
i62 : HH^1 OO_X\\
\emptyLine
\        6\\
o62 = kk\\
\emptyLine
o62 : kk-module, free\\
\endOutput
The genus of the curve is the dimension of this space,
which we can see to be 6.
Next, the cohomology class of {\tt X} in $\P^3$ is determined
by the degree of {\tt X}:
\beginOutput
i63 : degree idealX\\
\emptyLine
o63 = 10\\
\endOutput
In sum: {\tt X} is a smooth, absolutely irreducible curve of
genus 6 and degree 10.

We next ask for
analytic information about the curve and the embedding.
A reasonable place to start is
with the relation between the line bundle defining the
embedding and the canonical sheaf $\omega_X$.
Notice first that the degree of the hyperplane divisor (the
degree of the curve) is 10 = 2g-2, the same as the canonical
bundle. By Riemann-Roch the embedding line bundle either is the canonical
bundle or has first cohomology 0, which we can check with
\beginOutput
i64 : P3 = Proj ringP3\\
\emptyLine
o64 = P3\\
\emptyLine
o64 : ProjectiveVariety\\
\endOutput
\beginOutput
i65 : HH^1((OO_P3(1)/idealX)(>=0))\\
\emptyLine
o65 = cokernel | x_3 x_2 x_1 x_0 |\\
\emptyLine
\                                       1\\
o65 : ringP3-module, quotient of ringP3\\
\endOutput
Let's examine the degree of the generator of that module.
\beginOutput
i66 : degrees oo\\
\emptyLine
o66 = \{\{0\}\}\\
\emptyLine
o66 : List\\
\endOutput
% Note the construction {\tt ringP3\char`\^\{1\}}, which denotes
% the free module of rank 1 corresponding to $\cO_{\P^3}(1)$.
% (In general, {\tt ringP3\char`\^\{a,b,\dots\}} denotes
% the free module over {\tt ringP3} corresponding to the sheaf
% $\cO_{\P^3}(a)\oplus \cO_{\P^3}(b)\oplus \cdots$ --- that is,
% the free module with generators in degrees $-a, -b, \dots$.)
From that and the presentation matrix above
%% $${\tt dualModule\char`\_\{0\}\ \char`\|\ x\char`\_3\ x\char`\_2\ x\char`\_1\ x\char`\_0\ \char`\|}$$
%%Dan, please check that the previous line will typeset correctly!
%% I've changed it a bit -- I don't understand the juxtaposition of the
%% expression and the matrix.  Also, dualModule hasn't been defined yet!
%% Why not just reserve \tt font for computer stuff, and try to typeset math here?
we see that this cohomology module is the residue class field
${\tt ringP3/(x_0,x_1,x_2,x_3)}$,
concentrated in degree 0.
Thus the embedding line bundle $\cO_X(1)$
is isomorphic to $\omega_X$. On the other hand
the dimension of the space of sections of this line bundle has already been
computed; it is $g = 6$. The curve is embedded in $\P^3$, so
only 4 of these sections were used---the embedding is a projection
of the same curve,  embedded in  $\P^6$ by the {\it canonical map\/}.
\index{canonical embedding}

We next ask more about the curve itself. After the genus,
the \ie{gonality} and the \ie{Clifford index} are
among the most interesting invariants.
Recall that the
{\it gonality\/} of $X$ is the smallest degree of a mapping from
$X$ to $\P^1$. To define the Clifford index
of $X$
we first define the {\it Clifford index of a line bundle\/} $L$
on $X$ to be
$\mathop{\rm degree}(L)-2(\mathop{\rm h}\nolimits^0(L)-1)$.
{}For example, the Clifford indices of the structure sheaf
$\cO_X$ and the canonical sheaf $\omega_X$ are both equal to 0.
The {\it Clifford index of the curve\/}
$X$ is defined to be the minimum value
of the Clifford index of a line bundle $L$
on $X$ for which both
$\mathop{\rm h}\nolimits^0(L)\geq 2$ and $\mathop{\rm h}\nolimits^1(L)\geq 2$.
The Clifford index of a curve of genus $g$
lies between 0 (for a \ie{hyperelliptic curve})
\index{curve!hyperelliptic} and
$\lfloor (g-1)/2\rfloor$ (for a general curve\index{curve!general}).
The Clifford index of any curve is bounded above
by the gonality minus 2.

{}For a curve of genus 6 such as $X$, the gonality is either
2 (the hyperelliptic case), 3 (the trigonal case) or 4
(the value for general curves). The Clifford index, on the
other hand is either 0 (the hyperelliptic case) or 1
(the case of a \ie{trigonal curve}
\index{curve!trigonal} OR a smooth
\index{curve!plane quintic} plane quintic curve---which
is necessarily of gonality 4)
or 2 (the case of a general curve). Thus for most curves
(and this is true in any genus)
the Clifford index is equal to the gonality minus 2.

We can make a start on distinguishing these cases already:
since our curve is embedded in $\P^3$ by a subseries of the
canonical series, $X$ cannot be hyperelliptic (for hyperelliptic
curves, the canonical series maps the curve two-to-one onto
a rational curve.)

To make further progress we use an idea of Mark Green
\index{Green's conjecture}
(see Green and Lazarsfeld \cite{gl}). Green conjectured a formula
{}for the Clifford index that depends only on
numerical data about the free resolution of the
curve in its complete canonical embedding (where the hyperplanes
cut out all the canonical divisors).
The conjecture is known for genus 6 and
in many other cases; see for example Schreyer \cite{s}.

We therefore begin by
computing the canonical embedding of $X$. We could proceed
to find the \ie{canonical bundle}
as in the computation for $\P^3$ above, or indeed
as $\cO_X(1)$, but instead we describe the general
method that is most efficient: duality, as described (for
example) in the book of Altman and Kleiman \cite{ak}. The
module $\oplus_{d\in \Z}\mathop{\rm H}\nolimits^0(\omega_X(d))$ can be
computed as
\indexcmd{Ext}
\beginOutput
i67 : omegaX = Ext^(codim idealX)(ringP3^1/idealX, ringP3^\{-4\})\\
\emptyLine
o67 = cokernel \{0\}  | 9359x_3           -4677x_3         -10105x_1     $\cdot\cdot\cdot$\\
\               \{0\}  | 12014x_1          2552x_1          2626x_0       $\cdot\cdot\cdot$\\
\               \{-1\} | x_0x_3-2553x_2x_3 x_1^2-1702x_2x_3 x_0x_1-8086x_ $\cdot\cdot\cdot$\\
\emptyLine
\                                       3\\
o67 : ringP3-module, quotient of ringP3\\
\endOutput

To find the equations of
the \ie{canonical embedding} of $X$, we
first compute a basis of ${\rm H}^0(\omega_X)$, which
is the degree 0 part of the module {\tt omegaX}.
The desired equations are computed as the algebraic
relations among the images of this basis under any
monomorphism $\omega_X \to \O_X$.

As the ring {\tt ringP3/idealX} is a domain,
and $\omega_X$ is the module corresponding to
a line bundle, any nonzero map
{}from  $\omega_X$ to {\tt ringP3/idealX}  will be
an embedding. We can compute the module of such maps with
\beginOutput
i68 : dualModule = Hom(omegaX, ringP3^1/idealX)\\
\emptyLine
o68 = subquotient (\{0\} | x_0^3x_2^2+10915x_0^2x_2^3+807x_0x_2^4+4043x_ $\cdot\cdot\cdot$\\
\                   \{0\} | 10105x_0x_1x_2^3+6063x_1x_2^4+11820x_0x_1^2x_ $\cdot\cdot\cdot$\\
\                   \{1\} | 10105x_0^2x_2^2-11322x_0x_2^3+11322x_2^4+8396 $\cdot\cdot\cdot$\\
\emptyLine
\                                          3\\
o68 : ringP3-module, subquotient of ringP3\\
\endOutput
and examine it with
\beginOutput
i69 : betti prune dualModule\\
\emptyLine
o69 = relations : total: 10 26\\
\                      3:  3  2\\
\                      4:  6 14\\
\                      5:  1  9\\
\                      6:  .  1\\
\endOutput
For want of a better idea
we take the first generator, {\tt dualModule\char`\_\char`\{0\char`\}}, which
we can turn into an actual homomorphism with
\beginOutput
i70 : f = homomorphism dualModule_\{0\}\\
\emptyLine
o70 = | x_0^3x_2^2+10915x_0^2x_2^3+807x_0x_2^4+4043x_2^5+7655x_0x_1x_2 $\cdot\cdot\cdot$\\
\emptyLine
o70 : Matrix\\
\endOutput
The image of a basis of $\omega_X$ is
given by the columns of the matrix
\beginOutput
i71 : canGens = f*basis(0,omegaX)\\
\emptyLine
o71 = | x_0^3x_2^2+10915x_0^2x_2^3+807x_0x_2^4+4043x_2^5+7655x_0x_1x_2 $\cdot\cdot\cdot$\\
\emptyLine
o71 : Matrix\\
\endOutput
regarded as elements of
\beginOutput
i72 : ringX = ringP3/idealX\\
\emptyLine
o72 = ringX\\
\emptyLine
o72 : QuotientRing\\
\endOutput
Because of the particular homomorphism we chose,
they have degree 5.

We can now compute the defining ideal for $X$ in its canonical
embedding as the relations on these elements. We first define
a ring with 6 variables corresponding to the columns of {\tt canGens}
\beginOutput
i73 : ringP5 = kk[x_0..x_5]\\
\emptyLine
o73 = ringP5\\
\emptyLine
o73 : PolynomialRing\\
\endOutput
and then compute the canonical ideal as the
kernel of the corresponding map from
this ring to {\tt ringX} with
\indexcmd{trim}
\beginOutput
i74 : idealXcan = trim kernel map(ringX, ringP5, \\
\                                     substitute(matrix canGens,ringX),\\
\                                     DegreeMap => i -> 5*i)\\
\emptyLine
\              2                                                        $\cdot\cdot\cdot$\\
o74 = ideal (x  + 5040x x  - 8565x x  - 11589x x , x x  - 6048x x  - 1 $\cdot\cdot\cdot$\\
\              3        0 5        2 5         4 5   1 3        0 5     $\cdot\cdot\cdot$\\
\emptyLine
o74 : Ideal of ringP5\\
\endOutput
Here the command {\tt trim} is used to extract a minimal set
of generators of the desired ideal, and the command {\tt matrix}
replaces the map of (nonfree) modules {\tt canGens} by the matrix that
gives its action on the generators.  The {\tt DegreeMap} option specifies a
function which transforms degrees (represented as lists of integers) as the
ring homomorphism does; using it here makes the ring map homogeneous.

To get information about the Clifford index, we examine the
{}free resolution with
\beginOutput
i75 : betti res idealXcan\\
\emptyLine
o75 = total: 1 9 16 9 1\\
\          0: 1 .  . . .\\
\          1: . 6  8 3 .\\
\          2: . 3  8 6 .\\
\          3: . .  . . 1\\
\endOutput
Quite generally, for a non-hyperelliptic curve of genus
$g\geq 3$ the ideal
of the canonical embedding requires ${g-\binom 2 2}$
quadratic generators, in our case 6. It is known that
the curve is trigonal (Clifford index 1) if and only if the
ideal also requires cubic generators, that is, the
{}first term in the free resolution requires generators of degree
$3 = 1+2$; and Green's conjecture says in general that the
curve has Clifford index $c$ if the $c-1$ term in the
resolution does not require generators of degree
$(c-1)+2 = c+1$ but the $c$ term does require generators
of degree $c+2$. Thus from the Betti diagram above, and
the truth of Green's conjecture in low genus, we
see that our curve has Clifford index 1 and is thus either
trigonal or a plane quintic.

If $X$ is trigonal, that is, $X$ has a map of degree 3 to $\P^1$,
then the fibers of this map form a linear series whose elements
are divisors of degree three. The geometric form
\index{Riemann-Roch theorem!geometric}
of the Riemann-Roch theorem says that if
$$
p_1,\dots,p_d\in X\subset \P^g
$$
are points on a canonically embedded curve $X$,
then the dimension of the linear system in which the divisor
$p_1+\cdots+p_d$ moves is the amount by which the points fail
to be linearly independent: $d-1$ minus the dimension of the
projective plane spanned by the points.
In particular, the 3 points in the fiber of a three-to-one
map to $\P^1$ are linearly dependent, that is,
they span a projective line.
This ``explains'' why the ideal of a
trigonal curve requires cubic generators: the quadrics all
contain three points of these lines and thus contain the whole
lines! It is known (see St-Donat \cite{s-d}) that, in the
trigonal case, the 6 quadrics in the ideal of the canonical curve
generate the defining ideal of the variety which is the union of these
lines, and that variety is a \ie{rational normal scroll}\index{scroll!rational normal}.
In case $X$ is a plane quintic, the {\it adjunction formula\/}
(Hartshorne \cite[II.8.20.3]{Hartshorne})
shows that the canonical embedding of $X$ is
obtained from the plane embedding by composing with the Veronese
embedding of the plane in $\P^5$ as the \ie{Veronese surface};
and the 6 quadrics in the ideal
of the canonical curve generate the defining ideal of the
Veronese surface.

Thus if we let $S$ denote the variety defined by the quadrics
in the ideal of $X$, we can decide whether $X$ is a
trigonal curve or a plane quintic by deciding whether $S$ is
a rational normal scroll or a Veronese surface.
To compute the ideal of $S$ we first ascertain which
of the generators of the ideal of the canonical curve
have degree 2 with
\indexcmd{positions}
\beginOutput
i76 : deg2places = positions(degrees idealXcan, i->i==\{2\})\\
\emptyLine
o76 = \{0, 1, 2, 3, 4, 5\}\\
\emptyLine
o76 : List\\
\endOutput
and then compute
\beginOutput
i77 : idealS= ideal (gens idealXcan)_deg2places\\
\emptyLine
\              2                                                        $\cdot\cdot\cdot$\\
o77 = ideal (x  + 5040x x  - 8565x x  - 11589x x , x x  - 6048x x  - 1 $\cdot\cdot\cdot$\\
\              3        0 5        2 5         4 5   1 3        0 5     $\cdot\cdot\cdot$\\
\emptyLine
o77 : Ideal of ringP5\\
\endOutput

One of the scrolls that could appear is singular, the cone
over the rational quartic in $\P^4$. We check for singularity
{}first:
\beginOutput
i78 : codim singularLocus idealS\\
\emptyLine
o78 = 6\\
\endOutput
Since the codimension is 6, the surface $S$ is nonsingular,
and thus must be one of the nonsingular scrolls or the Veronese
surface (which is by definition the image of ${\bf P}^2$,
embedded in ${\bf P}^5$ by the linear series of conics.)

The ideals defining any rational normal
scroll of codimension 3, and the ideal
of a Veronese surface all have free resolutions with
the same Betti diagrams, so we need a subtler method
to determine the identity of $S$. The most powerful tool
{}for such purposes is adjunction theory; we will use a
simple version.

The idea is to compare the embedding bundle
(the ``hyperplane bundle'') with the canonical bundle.
On the Veronese surface,  the canonical bundle is
the bundle associated to $-3$ lines in ${\bf P}^2$,
while the hyperplane bundle is associated to
2 lines in ${\bf P}^2$. Thus the inverse of the
square of the canonical bundle is the cube of the
hyperplane bundle, $\O_S(3)$. For a scroll on the other hand,
these two bundles are different.

As before we follow the
homological method for computing the canonical bundle:
\beginOutput
i79 : omegaS = Ext^(codim idealS)(ringP5^1/idealS, ringP5^\{-6\})\\
\emptyLine
o79 = cokernel \{2\} | 4032x_5  0       14811x_5 -4032x_3    6549x_3     $\cdot\cdot\cdot$\\
\               \{2\} | x_3      x_2     x_1      -x_4        x_0-14291x_ $\cdot\cdot\cdot$\\
\               \{2\} | -6852x_5 6549x_3 362x_5   x_1-6248x_3 0           $\cdot\cdot\cdot$\\
\emptyLine
\                                       3\\
o79 : ringP5-module, quotient of ringP5\\
\endOutput
\beginOutput
i80 : OS = ringP5^1/idealS\\
\emptyLine
o80 = cokernel | x_3^2+5040x_0x_5-8565x_2x_5-11589x_4x_5 x_1x_3-6048x_ $\cdot\cdot\cdot$\\
\emptyLine
\                                       1\\
o80 : ringP5-module, quotient of ringP5\\
\endOutput

We want the square of the canonical bundle, which we can compute
as the tensor square
\beginOutput
i81 : omegaS**omegaS\\
\emptyLine
o81 = cokernel \{4\} | 4032x_5  0       14811x_5 -4032x_3    6549x_3     $\cdot\cdot\cdot$\\
\               \{4\} | x_3      x_2     x_1      -x_4        x_0-14291x_ $\cdot\cdot\cdot$\\
\               \{4\} | -6852x_5 6549x_3 362x_5   x_1-6248x_3 0           $\cdot\cdot\cdot$\\
\               \{4\} | 0        0       0        0           0           $\cdot\cdot\cdot$\\
\               \{4\} | 0        0       0        0           0           $\cdot\cdot\cdot$\\
\               \{4\} | 0        0       0        0           0           $\cdot\cdot\cdot$\\
\               \{4\} | 0        0       0        0           0           $\cdot\cdot\cdot$\\
\               \{4\} | 0        0       0        0           0           $\cdot\cdot\cdot$\\
\               \{4\} | 0        0       0        0           0           $\cdot\cdot\cdot$\\
\emptyLine
\                                       9\\
o81 : ringP5-module, quotient of ringP5\\
\endOutput
But while this module represents the correct sheaf, it is
hard to interpret, since it may not be (is not, in this case)
the module of all twisted global sections of the square of the
line bundle. Since the free resolution of {\tt OS}
(visible inside the Betti diagram of the
resolution of {\tt idealXcan}) has length 3,
the module {\tt OS}  has depth 2. Thus we can
{}find the module of all twisted global sections
of {\tt omega2S} by taking the double dual
\beginOutput
i82 : omega2S = Hom(Hom(omegaS**omegaS, OS),OS)\\
\emptyLine
o82 = cokernel \{3\} | x_3^2+5040x_0x_5-8565x_2x_5-11589x_4x_5 x_1x_3-60 $\cdot\cdot\cdot$\\
\emptyLine
\                                       1\\
o82 : ringP5-module, quotient of ringP5\\
\endOutput

We see from the output that this module is
generated by 1 element of degree 3.
It follows that
$\omega_S^2\cong \cO_S(-3)$. This in turn shows
that $S$ is the Veronese surface.

We now know that the canonical embedding of the curve
$X$ is the Veronese map applied to a planar embedding
of $X$ of degree 5, and we can ask to see the plane embedding.
Since the {\it anticanonical bundle\/} $\omega_S^{-1}$
on $S$ corresponds to 3 lines
in the plane and the hyperplane bundle to 2 lines,
we can recover the line bundle corresponding to 1 line,
giving
the isomorphism of $X$ to the plane, as the quotient
\beginOutput
i83 : L = Hom(omegaS, OS**(ringP5^\{-1\}))\\
\emptyLine
o83 = subquotient (\{-1\} | 14401x_2+16185x_4    x_0-14291x_4 -5359x_1+1 $\cdot\cdot\cdot$\\
\                   \{-1\} | -1488x_1-10598x_3    -6549x_3     -11789x_5  $\cdot\cdot\cdot$\\
\                   \{-1\} | x_0+7742x_2-15779x_4 x_2          x_1+6551x_ $\cdot\cdot\cdot$\\
\emptyLine
\                                          3\\
o83 : ringP5-module, subquotient of ringP5\\
\endOutput
and the line bundle on {\tt Xcan} that gives the
embedding in $\P^2$ will be the restriction of {\tt L}
to {\tt Xcan}.
To realize the map from $X$ to $\P^2$, we proceed as before:
\beginOutput
i84 : dualModule = Hom(L, OS)\\
\emptyLine
o84 = subquotient (| x_0+7742x_2-15779x_4 14401x_2+16185x_4 x_1-301x_3 $\cdot\cdot\cdot$\\
\                   | x_2                  x_0-14291x_4      4032x_3    $\cdot\cdot\cdot$\\
\                   | x_1+6551x_3          -5359x_1+14409x_3 -9874x_5   $\cdot\cdot\cdot$\\
\emptyLine
\                                          3\\
o84 : ringP5-module, subquotient of ringP5\\
\endOutput
\beginOutput
i85 : betti generators dualModule\\
\emptyLine
o85 = total: 3 3\\
\          0: 3 3\\
\endOutput
Again, we may choose any homomorphism from {\tt L}
to {\tt OS}, for example
\beginOutput
i86 : g = homomorphism dualModule_\{0\}\\
\emptyLine
o86 = | x_0+7742x_2-15779x_4 x_2 x_1+6551x_3 |\\
\emptyLine
o86 : Matrix\\
\endOutput
\beginOutput
i87 : toP2 = g*basis(0,L)\\
\emptyLine
o87 = | x_0+7742x_2-15779x_4 x_2 x_1+6551x_3 |\\
\emptyLine
o87 : Matrix\\
\endOutput
\beginOutput
i88 : ringXcan = ringP5/idealXcan\\
\emptyLine
o88 = ringXcan\\
\emptyLine
o88 : QuotientRing\\
\endOutput
\beginOutput
i89 : ringP2 = kk[x_0..x_2]\\
\emptyLine
o89 = ringP2\\
\emptyLine
o89 : PolynomialRing\\
\endOutput
\beginOutput
i90 : idealXplane = trim kernel map(ringXcan, ringP2, \\
\                                        substitute(matrix toP2,ringXcan))\\
\emptyLine
\             5         4           3 2        2 3           4        5 $\cdot\cdot\cdot$\\
o90 = ideal(x  + 13394x x  - 13014x x  + 9232x x  + 12418x x  - 2746x  $\cdot\cdot\cdot$\\
\             0         0 1         0 1        0 1         0 1        1 $\cdot\cdot\cdot$\\
\emptyLine
o90 : Ideal of ringP2\\
\endOutput

We have effectively computed the square root of the line bundle
embedding $X$ in $\P^3$ with which we started, and exchanged a messy
set of defining equations of an unknown scheme for a single equation
defining a smooth plane curve whose properties are easy to deduce.
The  same curve may also be defined by a much simpler plane equation
(see Appendix \ref{How} below). I do not know any general method for choosing
a coordinate transformation to simplify a given equation! Can the
reader find one that will work at least in this case?

There is not yet a textbook-level exposition of the
sort of methods we have used
(although an introduction
will be contained in a forthcoming elementary book
of Decker and Schreyer).
The reader who would like to go further into
such ideas can find a high-level survey of how adjunction theory
is used
in the paper of Decker and Schreyer \cite{ds}.
For a group of powerful methods with a different flavor,
see Aure, Decker, Hulek, Popescu, and
Ranestad \cite{adhpr}.

\appendix

\section{How the ``Mystery Variety'' was Made}\label{How}

{}For those who would like to try out the
computations above over a different field (perhaps the
{}field of rational numbers {\tt QQ}),
and for the curious, we include the code
used to produce the equations of the variety $X$ above.

Start with the Fermat quintic in the plane
\beginOutput
i91 : ringP2 = kk[x_0..x_2]\\
\emptyLine
o91 = ringP2\\
\emptyLine
o91 : PolynomialRing\\
\endOutput
\beginOutput
i92 : idealC2 = ideal(x_0^5+x_1^5+x_2^5)\\
\emptyLine
\             5    5    5\\
o92 = ideal(x  + x  + x )\\
\             0    1    2\\
\emptyLine
o92 : Ideal of ringP2\\
\endOutput
Embed it by the Veronese map in $\P^5$:
\beginOutput
i93 : ringC2 = ringP2/idealC2\\
\emptyLine
o93 = ringC2\\
\emptyLine
o93 : QuotientRing\\
\endOutput
\beginOutput
i94 : ringP5 = kk[x_0..x_5]\\
\emptyLine
o94 = ringP5\\
\emptyLine
o94 : PolynomialRing\\
\endOutput
\beginOutput
i95 : idealC5 = trim kernel map(ringC2, ringP5, \\
\              gens (ideal vars ringC2)^2)\\
\emptyLine
\              2                                    2                   $\cdot\cdot\cdot$\\
o95 = ideal (x  - x x , x x  - x x , x x  - x x , x  - x x , x x  - x  $\cdot\cdot\cdot$\\
\              4    3 5   2 4    1 5   2 3    1 4   2    0 5   1 2    0 $\cdot\cdot\cdot$\\
\emptyLine
o95 : Ideal of ringP5\\
\endOutput
{}Finally, choose a projection into $\P^3$, from a line not meeting
{\tt C5}, which is an isomorphism
onto its image. (This requires the image to be a smooth curve
of degree 10).
\beginOutput
i96 : ringC5 = ringP5/idealC5\\
\emptyLine
o96 = ringC5\\
\emptyLine
o96 : QuotientRing\\
\endOutput
\beginOutput
i97 : use ringC5\\
\emptyLine
o97 = ringC5\\
\emptyLine
o97 : QuotientRing\\
\endOutput
\beginOutput
i98 : idealC = trim kernel map(ringC5, ringP3,\\
\              matrix\{\{x_0+x_1,x_2,x_3,x_5\}\})\\
\emptyLine
\              4       2      2        2 2   2 2           2            $\cdot\cdot\cdot$\\
o98 = ideal (x  - 2x x x  - x x x  + x x , x x  - 10915x x x  - 10917x $\cdot\cdot\cdot$\\
\              1     0 1 3    1 2 3    0 3   0 1         0 1 2          $\cdot\cdot\cdot$\\
\emptyLine
o98 : Ideal of ringP3\\
\endOutput
Let's check that this is the same ideal as that of the mystery variety.
\beginOutput
i99 : idealC == idealX\\
\emptyLine
o99 = true\\
\endOutput
Here is the code of the function {\tt mystery}, which does the steps above.
\beginOutput
i100 : code mystery\\
\emptyLine
o100 = -- mystery.m2:1-13\\
\       mystery = ringP3 -> (\\
\          kk := coefficientRing ringP3;\\
\          x := local x;\\
\          ringP2 := kk[x_0..x_2];\\
\          idealC2 := ideal(x_0^5+x_1^5+x_2^5);\\
\          ringC2 := ringP2/idealC2;\\
\          ringP5 := kk[x_0..x_5];\\
\          idealC5 := trim kernel map(ringC2, ringP5, \\
\               gens (ideal vars ringC2)^2);\\
\          ringC5 := ringP5/idealC5;\\
\          use ringC5;\\
\          trim kernel map(ringC5, ringP3,\\
\             matrix\{\{x_0+x_1,x_2,x_3,x_5\}\}))\\
\endOutput
And here is the code of the function {\tt prettyPrint}.
\beginOutput
i101 : code prettyPrint\\
\emptyLine
o101 = -- mystery.m2:15-51\\
\       prettyPrint = f -> (\\
\          -- accept a matrix f and print its entries prettily,\\
\          -- separated by commas\\
\          wid := 74;\\
\          -- page width\\
\          post := (c,s) -> (\\
\             -- This function concatenates string c to end of each\\
\             -- string in list s except the last one\\
\             concatenate {\char`\\} pack_2 between_c s);\\
\          strings := post_"," (toString {\char`\\} flatten entries f);\\
\          -- list of strings, one for each polynomial, with commas\\
\          istate := ("",0);\\
\          -- initial state = (out : output string, col : column number)\\
\          strings = apply(\\
\             strings,\\
\             poly -> first fold(\\
\                -- break each poly into lines\\
\                (state,term) -> (\\
\                   (out,col) -> (\\
\                      if col + #term > wid -- too wide?\\
\                      then (\\
\                         out = out | "{\char`\\}n   "; \\
\                         col = 3;\\
\                         -- insert line break\\
\                         );\\
\                      (out | term, col + #term) -- new state\\
\                      )\\
\                   ) state,\\
\                istate,\\
\                fold( -- separate poly into terms \\
\                   \{"+","-"\},\\
\                   \{poly\},\\
\                   (delimiter,poly) -> flatten( \\
\                      post_delimiter {\char`\\} separate_delimiter {\char`\\} poly\\
\                      ))));\\
\          print stack strings;  -- stack them vertically, then print\\
\          )\\
\endOutput

% \end


\newcommand{\etas}{\ensuremath{\eta_{\mathrm{s}}}}

\chapter{Introduction}

This document contains commonly used essential templates to write a
\LaTeX\ document. This document is to be used along with the files and
folders provided. Writing a \LaTeX\ document is very simple.  Often
students need only very simple constructs.  This document shows
certain essential features that almost all technical report writing
requires. Please consult the PDF file for the output of the document,
and then look at the corresponding \LaTeX\ file to reproduce it.  The
document illustrates the following constructs
\begin{itemize}
\item Unnumbered and numbered Lists
\item Equations
\item Defining short macros for frequently used symbols
\item Bibliography
\item Figures
\item Tables
\end{itemize}

The normal procedure for compiling a \LaTeX\ document that contains
bibliographic entries is to follow the following steps
\begin{enumerate}
\item \verb|pdflatex mainrep|
\item \verb|bibtex mainrep|
\item \verb|pdflatex mainrep|
\item \verb|pdflatex mainrep|
\end{enumerate}
In the above example \verb|mainrep| is the main \LaTeX\ file.

\section{First section of this chapter}

This is the first chapter, which resides in a directory (folder)
intro. Each chapter can contain \verb|section|, \verb|subsection|
and so on.

\subsection{Equations and Math symbols}

Equations should be set in a separate mode.  For details on getting
various types of aligned equations, consult the \AmS-\LaTeX\
documentation \verb|amsldoc.pdf|. Simple equations are set as
\begin{equation}
\label{eq:sinx}
\int \mathrm{d}x \; \cos x =  \sin x
\end{equation}
Equation~\eqref{eq:sinx} is the integral of the cosine
function. Mathematical symbols must always be put inside \verb|$$|,
when they appear outside a math environment (such as \verb|equation|,
\verb|align|, \verb|gather|, etc).  The symbol ``ex'' must be written as
$x$ and not as x.

Another commonly used construct for equations is the \verb|align|
environment to align several equations along a vertical line. It is
usually the $=$ sign across which the alignment is done.  The
point of alignment for each equation is specified using the ampersand symbol
\begin{align}
a &= b  \\
a + e + f + g & = m + n + z \\
x + 2 & = x^{3} + 3 x^{2} + 2 x + 5
\end{align}

\subsection{Commonly used Symbols}
For mathematical symbols it is very convenient to define frequently
used symbols as a short macro. For example if you are to be using the
symbol $\eta_{\mathrm{s}}$ frequently it is convenient to define it in
as:\\
\verb|\newcommand{\etas}{\ensuremath{\eta_{\mathrm{s}}}}| \\
in the preamble and to simply refer it to in the text as \etas\ or in
a mathematical equation as $\etas = \eta \, ( 1 + \phi)$.
%%
%

\section{How to write nomenclature}

\subsection{General guidelines:}
\begin{enumerate}
	\item Use \verb|\nomenclature[prefix]{symbol}{description}| for symbols, the best place for this command is immediately after you introduce the symbol for the first time
	\item Shorten the long command:\\ \verb|\newcommand{\nm}[2]{\nomenclature{#1}{#2}}|
	\item Create compiler for nomenclature with the given code: \\
	\textbf{makeindex \%.nlo -s nomencl.ist -o \%.nls -t \%.nlg }\\
	For TeXstudio: go to options > build > user command > write- `user1: Nomenclature' amd paste the above code\\
	For compiling the nomenclature: go to tools > user > Nomenclature
\end{enumerate}

\subsection{Grouped nomenclature}
\begin{enumerate}
\item For acronyms, use:\\
 \verb|\nmA[sorting letter]{symbol}{descritpon}|
\item For roman symbols, use:\\
\verb|\nmR[sorting letter]{symbol}{descritpon}|
\item For greek symbols, use:\\
 \verb|\nmG[sorting letter]{symbol}{descritpon}|
 \item For superscripts, use:\\
 \verb|\nmS[sorting letter]{symbol}{descritpon}|
 \item For subscripts, use:\\
 \verb|\nms[sorting letter]{symbol}{descritpon}|
 \item For any other symbol, use:\\
 \verb|\nmX[sorting letter]{symbol}{descritpon}|\\
 Name of other symbols can be changed with \verb|\OtherSym{Name of symbols}|
\end{enumerate}
%%
\subsection{Some examples}
\begin{enumerate}
\item \verb|\nmA[FF]{FFA}{Free fatty acid}|
\item \verb|\nmA[AO]{AOR}{Angle of repose}|
\item \verb|\nmR[Ra]{$R$}{Radius of circle}|
\item \verb|\nmR[ra]{$r$}{Intrinsic length}|
\item \verb|\nmR[Gr]{$G_\mathrm{r}$}{Gravity}|
\item \verb|\nmG[al]{$\alpha_{\mathrm{a}}$}{Angular acceleration}|
\item \verb|\nmG[et]{$\eta$}{Viscosity}|
\item \verb|\nmG[be]{$\beta$}{Shape factor}|
\item \verb|\nmS[v]{$v$}{Vapor phase}|
\item \verb|\nmS[g]{$g$}{Gas phase}|
\item \verb|\nms[i]{$i$}{Indices}|
\item \verb|\nms[x]{$x$}{Variable in x-direction}|
\item \verb|\nmX[f]{foo}{foo|
\end{enumerate}

\nmA[FF]{FFA}{Free fatty acid}
\nmA[AO]{AOR}{Angle of repose}

\nmR[Ra]{$R$}{Radius of circle}
\nmR[ra]{$r$}{Intrinsic length}
%\nmR[Gr]{$G_\mathrm{r}$}{Gravity}

\nmG[al]{$\alpha_{\mathrm{a}}$}{Angular acceleration}
\nmG[et]{$\eta$}{Viscosity}
%\nmG[be]{$\beta$}{Shape factor}

\nmS[v]{$v$}{Vapor phase}
\nmS[g]{$g$}{Gas phase}

\nms[i]{$i$}{Indices}
\nms[x]{$x$}{Variable in x-direction}

\nmX[f]{foo}{foo}

%%

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../mainrep"
%%% End:


\providecommand{\main}{..}
\documentclass[\main/thesis.tex]{subfiles}
\begin{document}
\chapter{Translating Propositions and Proofs}\label{translation}

In this chapter, we demonstrate how to translate propositions and proofs between
\lstinline|ℕ| and a subset of \lstinline|Numeral|.

People have put a lot of effort into proving theorems and properties on
\lstinline|ℕ|.
However, all these results are unusable for \lstinline|Numeral|,
although \lstinline|ℕ| and \lstinline|Numeral| both have a similar structure and
share the same purpose of representing natural numbers.
We cannot simply apply a property of \lstinline|ℕ|, say,
the commutative property of addition, on instances of \lstinline|Numeral|.

The reason behind this misery comes from the difference between so-called
\textbf{object languages} and \textbf{metalanguages}.
The relation and similarities between \lstinline|ℕ| and \lstinline|Numeral|
we have observed are expressed using some \textit{metalanguage} in our mind;
while the propositions and proofs we want to reuse are formalized with Agda,
the \textit{object language} we have been dwelling on.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}

            % the frame
            \path[clip] (0, -3) rectangle (12, 3);

            % UR
            \draw[ultra thick] (1, 1) rectangle (11,3);
            \node at (6, 2) {underlying representation of propositions};

            % SR
            \draw[ultra thick] (1, -1) rectangle (5,-3);
            \node[text width=5cm, scale=0.8, align=center] at (3, -2)
                {surface representation of propositions of \lstinline|ℕ|};

            % SR
            \draw[ultra thick] (7, -1) rectangle (11,-3);
            \node[text width=5cm, scale=0.8, align=center] at (9, -2)
                {surface representation of propositions of \lstinline|Numeral|};

            % the border line
            \node[right] at (0, 0.5) {in our mind};
            \draw[dotted, ultra thick] (0,0) -- (12,0);
            \node[right] at (0, -0.5) {in Agda};

            \draw[->, ultra thick] (3, -0.9) -- (3, 0.9);
            \draw[->, ultra thick] (9, 0.9) -- (9, -0.9);

        \end{tikzpicture}
    \end{adjustbox}
\end{center}

When we are proving propositions of \lstinline|Numeral| base on the ones that
already existed on \lstinline|ℕ|, we are actually translating propositions via
some metalanguage in our mind.

To free ourselves from these brainworks, we can encode the
underlying representation of propositions in Agda,
so that we can manipulate and translate them with the
help of constructions such as functions available from the object language.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}

            % the frame
            \path[clip] (0, -4) rectangle (12, 5);

            % UR
            \draw[ultra thick] (1, 1) rectangle (11,3);
            \node at (6, 2) {underlying representation of propositions};

            % SR
            \draw[ultra thick] (1, -1) rectangle (5,-3);
            \node[text width=5cm, scale=0.8, align=center] at (3, -2)
                {surface representation of propositions of \lstinline|ℕ|};

            % SR
            \draw[ultra thick] (7, -1) rectangle (11,-3);
            \node[text width=5cm, scale=0.8, align=center] at (9, -2)
                {surface representation of propositions of \lstinline|Numeral|};

            % the border line
            \draw[dotted, ultra thick] (0,4) -- (12,4);
            \node[right] at (0, 3.5) {in Agda};

            \draw[->, ultra thick] (3, -0.9) -- (3, 0.9);
            \draw[->, ultra thick] (9, 0.9) -- (9, -0.9);

        \end{tikzpicture}
    \end{adjustbox}
\end{center}

The underlying representation of propositions can be encoded in Agda
using a generic programming technique called \textit{universe construction}
\cite{norell2009dependently}.

\section{Universe Constructions}

A universe is a set of types;
a universe construction consists of:

\begin{itemize}
    \item A universe
    \item A datatype of ``codes''
    \item A decoding function that maps ``codes'' to the types in the universe
\end{itemize}

\subsection{A Simple Universe}

There is an universe construction we are familiar with: the universe
of \lstinline|⊤| and \lstinline|⊥|.

\subsubsection{Types}

\begin{lstlisting}
data ⊤ : Set where
    tt : ⊤
data ⊥ : Set where
\end{lstlisting}

\subsubsection{Codes}

\begin{lstlisting}
data Bool : Set where
    true  : Bool
    false : Bool
\end{lstlisting}

\subsubsection{Decoder}

\begin{lstlisting}
isTrue : Bool → Set
isTrue true  = ⊤
isTrue false = ⊥
\end{lstlisting}

As we have shown, we can use this universe to implement, for instance, a safe
\lstinline|head| function that cannot be type-checked on empty inputs.

\begin{lstlisting}
null : ∀ {A} → (xs : List A) → Bool
null []       = true
null (x ∷ xs) = false

not : Bool → Bool
not true  = false
not false = true

head : ∀ {A} → (xs : List A) → isTrue (not (null xs)) → A
head []       ()
head (x ∷ xs) proof = x
\end{lstlisting}

We can generate code from \lstinline|null|,
manipulate the code with \lstinline|not|,
and finally, realize them to \lstinline|⊤| or \lstinline|⊥| with \lstinline|isTrue|.

\subsection{Summary}

Below is a table that sums up the correspondences of universe construction.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
    \begin{tabular}{ l l l l l }
    universe & corresponds to & surface representation & corresponds to & semantics \\
    codes & corresponds to & underlying representation & corresponds to & syntax \\
    decoder & corresponds to & realization & corresponds to & interpretation \\
    \end{tabular}
    \end{adjustbox}
\end{center}

\section{First-order Logic on the Representations of Natural Numbers}

% \paragraph{Remark}
% What we meant by ``proposition'' in this chapter has a broader sense than that in
% \textit{propositional logic}. It also includes ``predicates'' that have
% quantifiers ranging over. These two words will be used interchangeably.

To translate propositions (and predicates) from \lstinline|ℕ| to
\lstinline|Numeral| (or vice versa),
we need an underlying syntax for expressing these propositions.
For that matter, we will demonstrate how to build a minimalistic first-order
logic with universe constructions.

\subsection{Syntax}
Syntax and semantics are two key parts of first-order logic.

What we called ``proposition'' are actually \textit{predicates} that have
quantifiers ranging over. Predicates are composed of smaller predicates or
basic units that we called \textit{terms}.

There are many kinds of predicates we can make on natural numbers.
However, we will target only at the predicates that can be expressed in both
\lstinline|ℕ| and \lstinline|Numeral|.

Therefore, the set of terms will be inductively defined by only two rules:
\begin{itemize}
    \item \textbf{Variable}: Any variable of the \textit{domain of discourse} (\lstinline|ℕ| or
        \lstinline|Numeral|) is a term.
    \item \textbf{Addition}: Any two terms put together with the addition function
        of that domain is a term as well.
\end{itemize}

We will only have terms that look like this:

\begin{lstlisting}
x + (y + z)
\end{lstlisting}

The set of predicates is inductively defined by three rules:

\begin{itemize}
    \item \textbf{Equality}: If $\phi$ and $\psi$ are two terms, then
        $ \phi \equiv \psi $ is a predicate.
    \item \textbf{Implication}: If $\phi$ and $\psi$ are two predicates, then
        $ \phi \implies \psi $ is also a predicate.
    \item \textbf{Universal Quantification}: If $\phi$ is predicate that may
        contain $ 1 + n $ free variables, then $ \forall \phi $ is a predicate that
        may only contain $ n $ free variables.
\end{itemize}

With these connectives and quantifiers, we can make predicates such as:

\begin{lstlisting}
∀ x y z . (x + y) + z ≈ x + (y + z)
\end{lstlisting}

\subsection{Codes for Syntax}

To describe the syntax, we have two datatypes for codes,
one for \textit{terms} and another for \textit{predicates}.

\subsubsection{Terms}

\begin{lstlisting}
data Term : ℕ → Set where
    var : ∀ {n} → Fin n → Term n
    _+T_ : ∀ {n} → Term n → Term n → Term n
\end{lstlisting}

The code for terms is indexed by a \lstinline|ℕ| which denotes the number
of \textit{free variables} that may occur in a predicate.
The \lstinline|Fin n| that \lstinline|var| takes designates its binder.
\subsubsection{Predicates}

\begin{lstlisting}
data Predicate : ℕ → Set where
    _≋P_ : ∀ {n} → (t₁ : Term n) → (t₂ : Term n) → Predicate n
    _→P_ : ∀ {n} → (p₁ : Predicate n) → (p₂ : Predicate n)
         → Predicate n
    ∀P   : ∀ {n} → (p : Predicate (suc n)) → Predicate n
\end{lstlisting}

The code for predicates indexed by the number of \textit{free variables} as well.
\lstinline|_≋P_| and \lstinline|_→P_| correspond to equality and implication
connectives respectively.
It is worth noting that \lstinline|∀P| returns a predicate with a smaller index
because one of the free variable of the given predicate has been bounded.

\subsubsection{Examples}

The transitivity of identity of the equality connective can be described
as follows:

\begin{lstlisting}
≋-trans : Predicate zero
≋-trans = let   x = var (# 0)
                y = var (# 1)
                z = var (# 2)
    in ∀P (∀P (∀P (((x ≋P y) →P (y ≋P z)) →P (x ≋P z))))
\end{lstlisting}

\subsection{Semantics}

The semantics determine meanings behind these propositions, and by meanings,
we mean the selection of types of the universe we are constructing.

\subsubsection{Signatures}

A syntax can be interpreted as many different semantics.
Each semantics has its own choice of interpreting a piece of syntax,
we call these choices a \textit{signature}.
Therefore each semantics has its own signature.

The datatype of signature can defined as follows:

\begin{lstlisting}
record Signature : Set₁ where
    constructor sig
    field
        carrier : Set
        _⊕_ : carrier → carrier → carrier
        _≈_ : carrier → carrier → Set
\end{lstlisting}

Different semantics have different signatures.

\begin{lstlisting}
ℕ-sig : Signature
ℕ-sig = sig ℕ _+_ _≡_

Numeral-sig : (b d : ℕ) → True (Continuous? b d 0) → Signature
Numeral-sig b d cont = sig (Numeral b d 0) (_+_ {cont = cont}) _≋_
\end{lstlisting}

Note that \lstinline|ℕ-sig| and \lstinline|Numeral-sig| are equipped with
different equality connectives.
The one equipped by \lstinline|Numeral-sig| is defined as follows:

\begin{lstlisting}
_≋_ : ∀ {b d o}
    → (xs ys : Numeral b d o)
    → Set
xs ≋ ys = ⟦ xs ⟧ ≡ ⟦ ys ⟧
\end{lstlisting}

Two numerals considered equal by \lstinline|_≋_| whenever they evaluate to the
same value.
On the other hand, two numerals are considered equal by \lstinline|_≡_| only
when they are in the same \textit{canonical form}.
However, unlike \lstinline|ℕ|, canonical forms for representing numbers may not
exist for systems of \lstinline|Numeral| that are \textit{redundant}.
In other words, \lstinline|_≋_| is extensional while \lstinline|_≡_| is
intensional.

Also, the index $ o $ of \lstinline|Numeral| is fixed to $ 0 $.
We will explain why the domain of discourse is limited that way in the
later section.

\subsubsection{Meaning of Variables}

Terms and predicates can have free variables, but variables are given
meanings only when they are substituted.

\begin{lstlisting}
var : ∀ {n} → (i : Fin n) → Term n
\end{lstlisting}

\lstinline|var| have to pick a number $ i $ of \lstinline|Fin n| as its name,
because values of all variables are stored in a \lstinline|Vec n| (a list of length $ n $.)
Upon substitution, we pick the $ i $th element of the list as its value.

\subsubsection{Interpreting Terms}

The interpreter of the syntax for terms is defined as follows:

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
⟦_⟧T : ∀ {n}
    → Term n
    → (sig : Signature)
    → Vec (carrier sig) n
    → carrier sig
⟦ var i ⟧T _ env = lookup i env
⟦ term₁ +T term₂ ⟧T (sig A _⊕_ _≈_) env
    = ⟦ term₁ ⟧T (sig A _⊕_ _≈_) env ⊕ ⟦ term₂ ⟧T (sig A _⊕_ _≈_) env
\end{lstlisting}

\lstinline|carrier sig| evaluates to the carrier of the signature, the domain
of discours. For example, \lstinline|carrier ℕ-sig| evaluates to \lstinline|ℕ|.
In addition to the term, \lstinline|⟦_⟧T| also takes a signature, meanings of
all variables, and returns an element of the carrier.

\paragraph{\lstinline|var|}
\lstinline|lookup| retrieves the $i$th value for the variable from \lstinline|env|.
This is where the variable substitution happens.

\paragraph{\lstinline|+T|}
The semantics of both terms are interpreted recursively,
and then computed with \lstinline|_⊕_|,
the addition function supplied by the signature.

\subsubsection{Interpreting Predicates}

The interpreter of the syntax for predicates is defined as follows:

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
⟦_⟧P : ∀ {n}
    → Predicate n
    → (sig : Signature)
    → Env (carrier sig) n
    → Set
⟦ t₁ ≋P t₂ ⟧P (sig carrier _⊕_ _≈_) env
    = ⟦ t₁ ⟧T (sig carrier _⊕_ _≈_) env ≈ ⟦ t₂ ⟧T (sig carrier _⊕_ _≈_) env
⟦ p →P q   ⟧P signature env = ⟦ p ⟧P signature env → ⟦ q ⟧P signature env
⟦ ∀P pred  ⟧P signature env = ∀ x → ⟦ pred ⟧P signature (x ∷ env)
\end{lstlisting}

Similar to that of interpreting terms, except that \lstinline|⟦_⟧P| returns a
\textit{type}.

\paragraph{\lstinline|_≋P_|}
The semantics of both terms are interpreted using \lstinline|⟦_⟧T|
whereas \lstinline|_≈_| is the equality connective supplied by the signature.

\paragraph{\lstinline|_→P_|}
The semantics of both predicates are interpreted recursively
and connected with the native implication \lstinline|_→_| provided by Agda.

\paragraph{\lstinline|∀P|}
A new variable is introduced and then added to the list of meanings of variables
for the interpreted predicate to consume.

\subsubsection{Examples}

We can have both semantics of \lstinline|≋-trans| by supplying different
signatures.

\begin{lstlisting}
≋-trans-ℕ : Set
≋-trans-ℕ = ⟦ ≋-trans ⟧P ℕ-sig []

≋-trans-Numeral : (b d : ℕ) → True (Continuous? b d 0) → Set
≋-trans-Numeral b d prop
    = ⟦ ≋-trans ⟧P (Numeral-sig b d prop) []
\end{lstlisting}

\lstinline|≋-trans-ℕ| evaluates to:

\begin{lstlisting}
(x y z : ℕ) → (z ≡ y → y ≡ x) → z ≡ x
\end{lstlisting}

\lstinline|≋-trans-Numeral 10 10 _| evaluates to:

\begin{lstlisting}
(x y z : Numeral 10 10 0)
    → (⟦ z ≡ ⟦ y ⟧ → ⟦ y ⟧ ≡ ⟦ x ⟧)
    → ⟦ z ⟧ ≡ ⟦ x ⟧
\end{lstlisting}

\section{Converting between Semantics of \lstinline|ℕ| and \lstinline|Numeral|}

\subsection{Terms}

We start from proving that both semantics of terms are equal after evaluation.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
toℕ-term-homo : ∀ {b d n}
    → (cont : True (Continuous? b d 0))
    → (t : Term n)
    → (env : Vec (Numeral b d 0) n)
    → ⟦ t ⟧T ℕ-sig (map ⟦_⟧ env) ≡ ⟦ ⟦ t ⟧T (Numeral-sig b d cont) env ⟧
toℕ-term-homo         cont (var i)   env = lookup-map ⟦_⟧ env i
toℕ-term-homo {b} {d} cont (t₁ +T t₂) env
    rewrite toℕ-term-homo cont t₁ env
          | toℕ-term-homo cont t₂ env
    = sym (toℕ-+-homo cont
        (⟦ t₁ ⟧T (Numeral-sig b d cont) env)
        (⟦ t₂ ⟧T (Numeral-sig b d cont) env))
\end{lstlisting}

We can see how \lstinline|⟦_⟧| \textit{preserves} the interpretation of syntax.
\lstinline|lookup-map| is a lemma which states that:

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
lookup-map ⟦_⟧ env i : lookup i (map ⟦_⟧ xs) ≡ ⟦ lookup i xs ⟧
\end{lstlisting}

The \lstinline|rewrite| construction takes a proof of type \lstinline|lhs ≡ rhs|
and pattern match on the proof so that \lstinline|lhs| and \lstinline|rhs| may
be unified as one.
If \lstinline|⟦_⟧| preserves addition functions of both semantics
then it will preserve the interpretation of \lstinline|+T|, too.

\subsection{Predicates}

Our wish is to translate semantics of predicates from \lstinline|ℕ| to
\lstinline|Numeral|.
However, because of the contravariant nature of the input type of function types
\lstinline|_→_| that are introduced when interpreting implication connectives,
we also need to know how to translate semantics the other way around,
from \lstinline|Numeral| to \lstinline|ℕ|.

These two interpreters are defined simultaneously with the \lstinline|mutual|
construction.

\subsubsection{From \lstinline|ℕ| to \lstinline|Numeral|}

\begin{lstlisting}
toℕ-pred-ℕ⇒Numeral : ∀ {b d n}
    → (cont : True (Continuous? b (suc d) 0))
    → (pred : Predicate n)
    → (env : Vec (Numeral b (suc d) 0) n)
    → ⟦ pred ⟧P ℕ-sig (map ⟦_⟧ env)
    → ⟦ pred ⟧P (Numeral-sig b (suc d) cont) env
toℕ-pred-ℕ⇒Numeral {b} {d} cont (t₁ ≋P t₂) env sem-ℕ =
    begin
        ⟦ ⟦ t₁ ⟧T (Numeral-sig b (suc d) cont) env ⟧
    ≡⟨ sym (toℕ-term-homo cont t₁ env) ⟩
        ⟦ t₁ ⟧T ℕ-sig (map ⟦_⟧ env)
    ≡⟨ sem-ℕ ⟩
        ⟦ t₂ ⟧T ℕ-sig (map ⟦_⟧ env)
    ≡⟨ toℕ-term-homo cont t₂ env ⟩
        ⟦ ⟦ t₂ ⟧T (Numeral-sig b (suc d) cont) env ⟧
    ∎
toℕ-pred-ℕ⇒Numeral cont (p →P q) env sem-ℕ ⟦p⟧P-Numeral =
    toℕ-pred-ℕ⇒Numeral cont q env
        (sem-ℕ (toℕ-pred-Numeral⇒ℕ cont p env ⟦p⟧P-Numeral))
toℕ-pred-ℕ⇒Numeral cont (∀P pred) env sem-ℕ x =
    toℕ-pred-ℕ⇒Numeral cont pred (x ∷ env) (sem-ℕ ⟦ x ⟧)
\end{lstlisting}

\paragraph{\lstinline|_≋P_|}
The reasoning can be replaced with the \lstinline|rewrite| construction.
However, we are keeping it for conciseness.
\lstinline|sem-ℕ| is the original semantics of \lstinline|ℕ| stating that
the semantics of \lstinline|t₁| and \lstinline|t₂| are equal.

\paragraph{\lstinline|_→P_|}
Since \lstinline|_→P_| is interpreted as an implication (hence a function type)
\lstinline|_→_| on \lstinline|Numeral|, we are given \lstinline|⟦p⟧P-Numeral|
as the antecedent of the implication.
However, what we have is \lstinline|sem-ℕ|, an implication on the
semantics of \lstinline|ℕ|.
This where we need the result of translation from the opposite direction.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}
            \matrix (m) [matrix of nodes,row sep=4em,column sep=10em,minimum width=2em]
                {
                    {\lstinline|Numeral|} & {\lstinline|ℕ|} \\
                    {\lstinline|Numeral|} & {\lstinline|ℕ|} \\
                };
          \path[-stealth]
                (m-1-1)
                    edge node [above] {{\lstinline|toℕ-pred-Numeral⇒ℕ|}} (m-1-2)
                (m-1-2)
                    edge node [right] {{\lstinline|sem-ℕ|}} (m-2-2)
                (m-2-2)
                    edge node [below] {{\lstinline|toℕ-pred-ℕ⇒Numeral|}} (m-2-1);
        \end{tikzpicture}
    \end{adjustbox}
\end{center}

\paragraph{\lstinline|∀P|}
\lstinline|∀P| is interpreted as a function with an argument \lstinline|x|
of type \lstinline|Numeral|.
We can get a semantics of \lstinline|Numeral| by recursively calling itself
with the list of meanings of variables extended by the value of \lstinline|x|.

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}
            \matrix (m) [matrix of nodes,row sep=4em,column sep=10em,minimum width=2em]
                {
                    {\lstinline|Numeral|} & {\lstinline|ℕ|} \\
                    {\lstinline|Numeral|} & {\lstinline|ℕ|} \\
                };
          \path[-stealth]
                (m-1-1)
                    edge node [above] {{\lstinline|⟦_⟧|}} (m-1-2)
                (m-1-2)
                    edge node [right] {{\lstinline|sem-ℕ|}} (m-2-2)
                (m-2-2)
                    edge node [below] {{\lstinline|toℕ-pred-ℕ⇒Numeral|}} (m-2-1);
        \end{tikzpicture}
    \end{adjustbox}
\end{center}

\subsubsection{From \lstinline|Numeral| to \lstinline|ℕ|}

\begin{lstlisting}
toℕ-pred-Numeral⇒ℕ : ∀ {b d n}
    → (cont : True (Continuous? b (suc d) 0))
    → (pred : Predicate n)
    → (env : Vec (Numeral b (suc d) 0) n)
    → ⟦ pred ⟧P (Numeral-sig b (suc d) cont) env
    → ⟦ pred ⟧P ℕ-sig (map ⟦_⟧ env)
toℕ-pred-Numeral⇒ℕ {b} {d} cont (t₁ ≋P t₂) env sem-Num =
    begin
        ⟦ t₁ ⟧T ℕ-sig (map ⟦_⟧ env)
    ≡⟨ toℕ-term-homo cont t₁ env ⟩
        ⟦ ⟦ t₁ ⟧T (Numeral-sig b (suc d) cont) env ⟧
    ≡⟨ sem-Num ⟩
        ⟦ ⟦ t₂ ⟧T (Numeral-sig b (suc d) cont) env ⟧
    ≡⟨ sym (toℕ-term-homo cont t₂ env) ⟩
        ⟦ t₂ ⟧T ℕ-sig (map ⟦_⟧ env)
    ∎
toℕ-pred-Numeral⇒ℕ cont (p →P q) env sem-Num ⟦p⟧P
    = toℕ-pred-Numeral⇒ℕ cont q env
        (sem-Num
            (toℕ-pred-ℕ⇒Numeral cont p env ⟦p⟧P))
toℕ-pred-Numeral⇒ℕ {b} {d} cont (∀P pred) env sem-Num n
    with n ≟ ⟦ fromℕ {cont = cont} n z≤n ⟧
toℕ-pred-Numeral⇒ℕ {b} {d} cont (∀P pred) env sem-Num n
    | yes eq
    rewrite eq
    = toℕ-pred-Numeral⇒ℕ
        cont pred
        (fromℕ {cont = cont} n _ ∷ env)
        (sem-Num (fromℕ {cont = cont} n _))
toℕ-pred-Numeral⇒ℕ {b} {d} cont (∀P pred) env sem-Num n
    | no ¬eq
    = contradiction (sym (fromℕ-toℕ cont n _)) ¬eq
\end{lstlisting}

The translation of semantics from \lstinline|Numeral| to \lstinline|ℕ| is mostly
the same as in \lstinline|toℕ-pred-ℕ⇒Numeral|,
except for the case of \lstinline|∀P|.

\paragraph{\lstinline|∀P|}
Suppose there exists a numeral \lstinline|xs| such that \lstinline|n ≡ ⟦ xs ⟧|.
Because \lstinline|⟦_⟧| may not be injective, we do not have a left inverse of
\lstinline|⟦_⟧|.
Therefore we cannot assert that \lstinline|xs ≡ fromℕ ⟦ xs ⟧ _|.
However, since we do not care about the canonical forms of numerals,
we can convince Agda that \lstinline|xs ≋ fromℕ ⟦ xs ⟧ _| is good enough.
To do this, we force evaluate the decidable equality between
\lstinline|fromℕ {cont = cont} n z≤n| and \lstinline|n| and unify them using the
\lstinline|rewrite| construction.

\subsubsection{Examples}

Suppose we already have a proof for the commutative property of addition
on \lstinline|ℕ|: \lstinline|+-comm-ℕ : ∀ a b → a + b ≡ b + a|.
To convert \lstinline|+-comm-ℕ| to a proof that works on \lstinline|Numeral| as
well, the first step is to describe the predicate with our syntax.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
+-comm-Predicate : Predicate 0
+-comm-Predicate = ∀P (∀P ((var (# 1) +T var (# 0)) ≋P (var (# 0) +T var (# 1))))
\end{lstlisting}

Hand it to \lstinline|toℕ-pred-ℕ⇒Numeral| and we are done.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
+-comm-Num : ∀ {b d}
    → {cont : True (Continuous? b (suc d) 0)}
    → (xs ys : Numeral b (suc d) 0)
    → (_+_ {cont = cont} xs ys) ≋ (_+_ {cont = cont} ys xs)
+-comm-Num {cont = cont} = toℕ-pred-ℕ⇒Numeral cont +-comm-Predicate [] +-comm-ℕ
\end{lstlisting}

\end{document}