% LAST EDIT: Thu Apr 28 10:05:07 1994 by Juergen Exner (hektor!jue) 
% LAST EDIT: Sun Feb 13 18:09:17 1994 by Juergen Exner (hektor!jue) 
% LAST EDIT: Mon Nov 15 22:30:02 1993 by Juergen Exner (hektor!jue) 
% LAST EDIT: Fri Nov 12 15:43:03 1993 by Juergen Exner (hektor!jue) 
% LAST EDIT: Mon Oct 18 16:34:53 1993 by Juergen Exner (hektor!jue) 
% LAST EDIT: Wed Oct 13 15:14:32 1993 by Juergen Exner (hektor!jue) 
\chapter{Names in \opal}
\label{chap:names}
\novice
 Names are the basis for all programming because you need names to identify
the objects of your algorithm.
In \opal\ the rules for constructing names are more complex than in
most traditional languages for two reasons:

First, \opal\ also allows the construction of  identifiers with graphical
symbols like ``\pro{+}'', ``\pro{-}'', ``\pro{\%}'' or ``\pro{\#}'',
which can be used the same way as the established identifiers
made up of  letters and digits. 
This will be explained in the following section.

Second, \opal\ supports overloading and parameterization and thus requires
a method for annotations (see Section~\ref{sec:name}: ``What's the
name of the game?''). 


\section{Constructing Identifiers}
\label{sec:ide}
\novice
In this section we will explain which characters can be used to
build an identifier and which rules have to be fulfilled.

For constructing an identifier all  printable characters are divided
into three classes:
\begin{itemize}
\item upper-case letters,  lower-case letters and  digits
  (e.g., ``\pro{A}'', ``\pro{h}'', ``\pro{1}'')
\item  graphical symbols: these are all printable character with the
   exception of  letters, digits and separators\footnote{%
Although   question mark ``\pro{?}'' and  underscore
  ``\pro{\_}'' belong to this group too, they have special meanings
  (see below).}. 
Examples are ``\pro{+}'', ``\pro{\$}'', ``\pro{\char`@}'', ``\pro{\{}'',
``\pro{!}'' 
\item  separators: these are ``\pro{(}'', ``\pro{)}'', ``\pro{,}'',
  ``\pro{`}'', ``\pro{"}'', ``\pro{[}'', ``\pro{]}'' together with
   space,  tabulator and newline. 
   The last three are often called ``white space''.

\end{itemize}

You may construct identifiers from either of the first two classes.
This means ``\pro{HelloWorld}'',
``\pro{a1very2long3and4silly5identifier6with7a8lot9of0digits}'', \newline
``\pro{A}'', ``\pro{z}'', 
``\pro{2345}'', ``\pro{+}'', ``\pro{\#}'', ``\pro{---->}'',
``\pro{\%!}'' are legal identifiers in \opal.

But note that you cannot mix letters and digits with graphical
characters in one identifier, e.g., ``\pro{my1Value<@>37arguments}'' is a
list of  three identifiers  ``\pro{my1Value}'', ``\pro{<@>}'' and
``\pro{37arguments}''.

\begin{sloppypar}
    The case of a letter is significant, so ``\pro{helloworld}'',
``\pro{HelloWorld}'' and  ``\pro{HELLOWORLD}'' are three different
identifiers. 
\end{sloppypar}


\medskip
 Separators cannot be used in identifiers at all. They are
reserved for special purposes and delimit any identifier they are
connected to. 

Summarizing, one can say an identifier is the longest possible
sequence of characters,  either of letters and digits or graphical
characters.


\subsection{Question Mark and Underscore}
\label{sec:question mark}
\novice
Although question marks are graphical symbols, they can also be used
as trailing characters of an identifier based on letters or
digits. 
This exception was introduced because the discriminators of data types
are constructed by appending  a question mark to the constructor (for
details, see Chapter \ref{chap:types}: ``Types'').

\medskip
\advanced
An underscore-character ``\pro{\_}'' has a special meaning too.
First it is a member of both character classes, letters as well as
graphical symbols.
Hence it can be used to switch between the character classes
within one identifier. 
E.g.,~``\pro{my1Value\_<@>\_37arguments}'' is---in contrast to
above---only one identifier. 

Furthermore, a single underscore is a reserved keyword  with
two applications (as wildcard and as keyword for sections, see
\ref{sec:wildcard} and \ref{sec:sections} for details). 
Therefore you should be careful when using underscores.
%
%\begin{itemize}
%\item as a ``wildcard'' in pattern-based function definition (similar
%  e.g.~to Prolog); see Section \ref{sec:wildcard}: ``Using Wildcards in
%  Pattern-based Definitions'' for details.
%\item as a keyword for sections, i.e.~a shorthand notation for simple
%  lambda abstractions; see Section \ref{sec:sections} for details.
%\end{itemize}

\subsection{Keywords}
\label{sec:keywords}
\novice
Some identifiers are reserved as keywords, e.g.,~``\pro{IF}'',
``\pro{IMPLEMENTATION}'', ``\pro{FUN}'', ``\pro{:}'',``\pro{\_}'', ``\pro{->}''
(see ``The Programming Language \opal'' for a complete list of all keywords).

These keywords cannot be used as identifiers any more, but it is no
problem to use them as part of an identifier: ``\pro{myFUN}'',
``\pro{THENPART}'',``\pro{IFthereishope}'',``\pro{::}'' and
``\pro{-->}'' are legal identifiers.

Moreover, because upper and lower case are significant,
``\pro{if}'' and ``\pro{fun}'' are legal identifiers  and not keywords.

\important This is a common reason for curious errors.
A  programmer will write
\begin{prog}
      FUN help : ...
\end{prog}

\noindent to start the declaration of the function \pro{help}. 
The line 
\begin{prog}
      FUNhelp : ...
\end{prog}
obviously means something completely different (in this case it will
be an error), but a programmer will recognize this error at once. 

The same error---a missing space---written with graphical symbols is
much less obvious! 
You must take care to write
\begin{prog}
  FUN # : nat -> nat
\end{prog}
instead of 
\begin{prog}
  FUN #: nat -> nat
\end{prog}
So, if you receive a curious error message, first check that you have
included all necessary separators between identifiers and keywords.

\section{``What's the name of the game?''}
\label{sec:name}
\novice
In the previous section we discussed the construction of single
identifiers.
For several reasons---the two most important are
overloading\footnote{%
  Using the same identifier for different functions
  is called overloading. In traditional programming languages this
  feature is  common for built-in data types (e.g., the symbol \pro{+} for
  addition of natural and real numbers), but it's very rarely
  supported for user-defined functions.}  
and parameterized structures
(see Section \ref{sec:struct.param})---an identifier alone does not
suffice to really identify an object under all circumstances.
It is for this reason that an  identifier can be annotated with additional
information. 

 By carefully analyzing the environment of an identifier,  the
 compiler will nearly always detect by itself which operation to be 
used.
In very complex cases this detection may fail and you will receive an error
message saying something like ``ambiguous identification''.
In these cases you can annotate the identifier to help the compiler.

\advanced
In addition, you must annotate the instance of a parameterized structure
at least once, either at the import or at the application point (see
Chapter \ref{sec:struct.param} for details).

\medskip
 Annotations are always appended to an identifier, a white
space\footnote{Blanks, tabulators and newlines}  may be
added between identifier and annotations, but this is not necessary.

 The following annotations in particular are possible. You may omit
each of them, but if you supply two or more, they must be supplied in
the order presented below:
\begin{itemize}
\item{\bf Origin:} The origin of an object is the name of the
  structure in which this object was declared. 

The annotation of  the origin is introduced by a ``\pro{'}'', followed by
the name of the origin structure.

E.g., \pro{-'Nat} identifies the function \pro{-} from the structure
\pro{Nat}, whereas \pro{-'Int} identifies the function \pro{-} from the
structure \pro{Int}.

\item{\bf Instantiation:} The instantiation of an object of a
  parameterized structure (see \ref{sec:struct.param} for details)
  will be annotated by appending the parameters  to
  the identifier in square brackets.
E.g.,~if you want to use the function \pro{in} from the library
structure \pro{Set} with a set of natural numbers you may annotate
\pro{  in[nat,<]}---or in even more detail---\pro{  in'Set[nat'Nat,
  <'Nat]}. 

\item {\bf  Kind:} The kind of an object is either the keyword
  ``\pro{SORT}'' or the functionality of the object.
The kind is appended with a ``\pro{:}''. E.g.,~the name
\pro{-~:int->int}\/ identifies the unary minus, whereas
\pro{-~:int**int->int}\ identifies the usual dyadic minus\footnote{%
Remember the space between \pro{-} and \pro{:}.}. 
\end{itemize}





% Local Variables: 
% mode: latex
% TeX-master: "tutorial"
% End: 
