\documentclass[a4paper]{book}
\usepackage{amsmath,amssymb,amsthm}
\usepackage[utf8]{inputenc}
\usepackage[pdftex]{graphicx}
\usepackage{index}

\makeindex

\input commands.tex

\newcommand{\macrochapter}{5}
\newcommand{\secexceptions}{2}


\title{\theme\ Language Manual}
\author{Tommi Höynälänmaa}


\begin{document}


\frontmatter


\maketitle


%% \begin{titlepage}
%%   \title{\theme\ Language Manual}
%%   \author{Tommi H\"oyn\"al\"anmaa}
%%   \maketitle
%% \end{titlepage}


Copyright (C) 2012-2020 Tommi Höynälänmaa


\tableofcontents


\listoffigures


\mainmatter


\chapter{Introduction}


The purpose of programming language \theme\ is to extend Scheme with
static typing. \theme\ has an object system with single inheritance and
multimethods. \theme\ also has parametrized types and parametrized
procedures. \defterm{Translation} shall mean the compilation and linking
of a \theme\ program.
\theme\ is mainly intended to be a compiled language.
The standard of programming language Scheme can be found at
\cite{shinn2017}. Theory of type systems in functional programming
languages can be found at \cite{bds2013}.
Homepage for guile can be found at
http://www.gnu.org/software/guile/.
Homepage for Scheme48 is located in http://s48.org/.
Homepage for the functional programming language ocaml is located in
http://caml.inria.fr/.
\theme\ resembles Jaap Weel's Theme \cite{weel2007} but \theme\ is more dynamic
and the objects in \theme\ need to have type tags.
I remember seeing a programming language called ``bits'',
extending Scheme by a static type system, but I was
unable to find it again.


\chapter{Hello World}


\index{Hello World}


Here is the implementation of ``Hello World'' in \theme:
\begin{codeblock}
  \cl{0.5cm}
  (define-proper-program hello-world \\
  \\
  \cl{1.0cm}
  (import (standard-library core) \\
  \cl{2.5cm}
	  (standard-library console-io)) \\
  \\
  \cl{1.0cm}
  (define main \\
  \cl{2.0cm}
    (lambda (() <none> nonpure) \\
  \cl{3.0cm}
	(console-display-line "Hello, world!"))))
\end{codeblock}


\chapter{Programs and Modules}
\label{ch:programs-and-modules}


All the code in \theme\ is organized into \defterm{units}. A unit is
either a \defterm{program}, an \defterm{interface} or a
\defterm{body}. A program is either a \defterm{proper program} or a
\defterm{script}.
A combination of an
interface and the body that implements it is called a
\defterm{module}. See sections \ref{subsec:define-proper-program},
\ref{subsec:define-script},
\ref{subsec:define-interface}, and \ref{subsec:define-body} for the
syntax for defining units.
\index{unit}
\index{program}
\index{proper program}
\index{script}
\index{module}
\index{interface}
\index{body}

\index{\keyword{import}}
\index{\keyword{import-and-reexport}}
\index{\keyword{use}}
\index{\keyword{prelink-body}}

A proper program has to define a procedure called called \code{main}. The
accepted argument types and result type of
\code{main} depend on the target platform. But every \theme\
implementation is required to accept the following for \code{main}:
\begin{enumerate}
\item Result type \builtin{<none>} or \builtin{<integer>}
\item Empty argument list, argument list consisting of one argument
  with type \code{(:uniform-list <string>)}, or
  a single argument of a tuple type consisting only of
  \builtin{<strings>}.
\end{enumerate}
\index{main program}
When a proper program is executed all the toplevel expressions in the program
and in the modules it imports are executed and then the procedure
\code{main} is called. A script contains no \code{main}
procedure. When a script is executed all the toplevel expressions in the program
and in the modules it imports are executed.

An interface contains all the definitions or declarations for the
variables that the module exports. An interface contains only
declarations for the procedures and the parametrized procedures that
the module exports. A body contains definitions of all private
variables of the module and definitions of all the procedures and the
parametrized procedures declared in the interface. Both the interface
and the body may import other modules using keyword \keyword{import}
or \keyword{import-and-reexport}. An interface may reexport
variables imported from other modules.

The module imports between the interfaces may not be cycled. I.e. if
an interface A imports module B directly or indirectly the interface
of B may not import module A. However, the body of B may import module
A.

When an interface of a module A imports other modules the definitions
and declarations in the imported modules do not become visible
automatically when the module A is imported. However, an interface may
contain \keyword{reexport} statements, which export a variable
imported from another interface.
An interface may also contain \keyword{import-and-reexport}
statements, which import a module and reexport all the variables it
exports.
The variables imported into an
interface become visible in the corresponding body automatically.
A body always imports the interface of the module implicitly. This import may
not be specified explicitly in the \keyword{import} clause of the body.
Modules can also be used without importing its contents into the
toplevel namespace. This is done with keyword \keyword{use}.
The variables in this kind of modules are accessed with syntax
\code{(@ \syntaxvar{module} \syntaxvar{variable})}.

An interface must not contain any toplevel procedure calls.
A body or a program may contain toplevel procedure calls. A body or a
program containing toplevel procedure calls must ensure that the
called procedures are linked properly using the form
\keyword{prelink-body}, see section \ref{subsec:prelink-body}.


\chapter{Variables, Objects, and Types}


\section{Variables}


\index{variable}
\index{constant}
\index{mutable variable}
\index{lexical scoping}
A variable whose value cannot be changed is called a
\defterm{constant}. A variable whose value can be changed is called a
\defterm{mutable variable}. A \defterm{volatile variable} is a mutable
variable that can be changed by pure expressions (expressions without
side effects).
Note that it is possible to change the
components of a constant, e.g. setting elements of a constant
vector.
Variables are lexically scoped as in Scheme.

A variable that is declared but not defined is called
\defterm{incomplete}. You cannot define a variable to be equal with an
incomplete mutable variable. If you have declared a constant you
cannot define it to be equal with an incomplete constant.
However, if a variable has been defined in a prelinked module you can
use the variable as a value. See section \ref{subsec:prelink-body}.


\section{Classes and Logical Types}


Every \theme\ object has a \defterm{static type} and a
\defterm{dynamic type}. The static type of an object is the
translation time type of the object and the dynamic type of an object
is its runtime type.  The dynamic type of a \theme\ object is always a
\defterm{class}. Types that are not classes are called
\defterm{logical types}.
\index{static type}
\index{dynamic type}
\index{class}

A type may \defterm{inherit} from another type. A type always inherits
from itself. When type \syntaxformulacmd{\objectvar{A}}
inherits from type \syntaxformulacmd{\objectvar{B}} and variable \code{y}
has been declared
with type \syntaxformulacmd{\objectvar{B}} a value \code{y} of type
\syntaxformulacmd{\objectvar{A}} can be
assigned to \code{y}. We write
\syntaxformulacmd{\inherits{\objectvar{A}}{\objectvar{B}}}
to mean that \syntaxformulacmd{\objectvar{A}} inherits from
  \syntaxformulacmd{\objectvar{B}}.
When the static or dynamic type of a value or a variable \code{y}
is \syntaxformulacmd{\objectvar{A}} and \syntaxformulacmd{\objectvar{A}}
  inherits from \syntaxformulacmd{\objectvar{B}} we say that 
\code{y} is \defterm{instance} of type \syntaxformulacmd{\objectvar{B}}.
Every type except
\code{<none>} inherits from the class \code{<object>}. Every class is
an instance of class \code{<class>}. Class \code{<class>} is an
instance of itself. A class whose instances are classes is called a
\defterm{metaclass}.
\index{inheritance}

%% Do we need further restrictions here?
Every class that is not an instance of a parametrized class
%%\code{:pair} or \code{:procedure}
is called a \defterm{simple class}.
\index{simple class}
Every simple class except \code{<object>} and \code{<none>} has an
\defterm{immediate superclass}, which is itself a class.
\index{immediate superclass}
We write
\syntaxformulacmd{\imminh{\objectvar{A}}{\objectvar{B}}}
to mean that \syntaxformulacmd{\objectvar{B}} is
the immediate superclass of \syntaxformulacmd{\objectvar{A}}.
A class \syntaxformulacmd{\objectvar{A}} inherits from a class
\syntaxformulacmd{\objectvar{B}} if and only if
\syntaxformulacmd{\imminh{\objectvar{A}}{\objectvar{B}}} or there exists
a finite sequence
\syntaxformulacmd{\indexedobjectvar{X}{1}, \sldots,
  \indexedobjectvar{X}{n}}
consisting of classes
so that
\syntaxformulacmd{\objectvar{A} \imminhsymb\ \indexedobjectvar{X}{1} \imminhsymb\ \sldots
  \imminhsymb\ \indexedobjectvar{X}{n} \imminhsymb\ \objectvar{B}}.
The dynamic type of an object \syntaxformulacmd{\objectvar{y}} is always a subtype of the
static type of \syntaxformulacmd{\objectvar{y}}. See section
\ref{subsec:define-class} for the syntax for
defining new classes. See subsection \ref{subsec:subtyping-algorithm}
for the algorithm that checks if one type is a
subtype of another type.

Every class has the following boolean-valued attributes:
\begin{itemize}
\item \defterm{inheritable}
\item \defterm{immutable}
\item \defterm{equality by value}
\end{itemize}
\index{class attributes}
A class is inheritable if and only if it is allowed to be a superclass
of another class. If a class is immutable no fields of instances of
the class can be changed. If a class is equal by value two instances
of the class are equal if and only if all of their fields are
equal. Otherwise instances of a class are equal if and only if they
are the same object.


Each field of a class has a name, type, read access specifier, write
access specifier, and an optional initial value. Possible values of
the access specifiers are \code{public}, \code{module}, and
\code{hidden}. Specifier \code{public} means that the field is
accessible everywhere. Specifier \code{module} means that the field is
accessible only in the same module where the class is
defined. Specifier \code{hidden} means that the field is accessible
nowhere. Its value can be set in object creation (in \keyword{make}
expression), though. A field may have an initial value, which has to
be an instance of the type of the field.
When an object is created with \keyword{make} only the values of fields
without an initial value are given as the arguments of \keyword{make}.
Keyword \keyword{make} actually calls the constructor of a
class in order to create an object. Expression
\begin{syntaxformula}
  \slp \sbuiltin{make}
  \objectvar{class} \indexedobjectvar{arg}{1} \sldots
  \indexedobjectvar{arg}{n} \srp
\end{syntaxformula}
is equivalent to
\begin{syntaxformula}
  \slp \slp \skeyword{constructor} \objectvar{class} \srp
  \indexedobjectvar{arg}{1} \sldots \indexedobjectvar{arg}{n}
  \srp
\end{syntaxformula}.
\index{constructor}

The access of a constructor is specified in a similar way.
If a constructor is not visible somewhere keyword \keyword{make}
cannot be used for the class at that position.
Note that if you want to define an abstract class which can be
inherited but not instantiated define the constructor access to
\code{hidden}.


A class may define a zero value, which can be accessed with syntax
\code{(zero \objectvar{class})}, see section
\ref{subsec:expr-zero}. This is
useful for parametrized numerical classes. A parametrized class may
define a zero value for its instances, see file \\
\code{theme-code/tests/test220.thp}.
\index{zero value}
For example, vector addition can be implemented as follows:
\begin{codeblock}
  \cl{0.5cm}
  (define-param-proc my-sum (\%number) \\
  \cl{2.0cm}
		     (((v1 (:mutable-value-vector \%number)) \\
  \cl{2.0cm}
		       (v2 (:mutable-value-vector \%number))) \\
  \cl{2.0cm}
		      (:mutable-value-vector \%number) \\
  \cl{2.0cm}
		      (force-pure)) \\
  \cl{1.0cm}
    (let ((len1 (mutable-value-vector-length v1)) \\
  \cl{2.1cm}
          (len2 (mutable-value-vector-length v2))) \\
  \cl{1.5cm}
      (assert (= len1 len2)) \\
  \cl{1.5cm}
      (let ((result (make-mutable-value-vector \\
  \cl{4.0cm}
                     \%number len1 (zero \%number)))) \\
  \cl{2.0cm}
        (do ((i <integer> 0 (+ i 1))) \\
  \cl{2.8cm}
            ((>= i len1)) \\
  \cl{2.5cm}
          (mutable-value-vector-set! \\
  \cl{3.0cm}
           result i \\
  \cl{3.0cm}
           (+ (mutable-value-vector-ref v1 i) \\
  \cl{3.5cm}
              (mutable-value-vector-ref v2 i)))) \\
  \cl{2.0cm}
        result)))
\end{codeblock}


A diagram about an example simple class inheritance hierarchy is
presented in figure \ref{fig:simple-class-hierarchy}.
A thick line means ``A is an instance of B'' and a thin line
``A inherits from B''. A rectangle means a class and a circle a
non-class object.


\begin{figure}[h]
  \centering
  \includegraphics[scale=0.5]{simple-classes.pdf}
  \caption{Example inheritance hierarchy for simple classes.}
  \label{fig:simple-class-hierarchy}
\end{figure}


Logical types are specified simply be defining a constant whose value
is some type. Here is an example of a logical type definition:


\begin{codeblock}
  \cindtop (\ckeyword{define} <my-type> (\cbuiltin{:union} \cbuiltin{<real>}
  \cbuiltin{<integer>}))
\end{codeblock}


\section{Parametrized Types}


See sections \ref{subsec:define-param-class} and
\ref{subsec:define-param-logical-type} for the syntax of parametrized
type definitions. Parametrized types are types that have
\defterm{type parameters}. When values (types) are assigned to the
type variables we get an instance of the parametrized type.
%% Think about the following.
Instances
of parametrized classes are classes and instances of parametrized
logical types are logical types. Instances are created with syntax
%% menee päällekkäin Expressions-luvun kanssa
\begin{syntaxblock}
  \slp \objectvar{parametrized-type} \indexedsyntaxvar{type-parameter}{1}
  \sldots
  \indexedsyntaxvar{type-parameter}{n} \srp
\end{syntaxblock}
A diagram about an example parametrized class inheritance hierarchy is
presented in figure \ref{fig:param-class-hierarchy}.
A thick line means ``A is an instance of B'' and a thin line
``A inherits from B''. A rectangle means a class and a circle a
non-class object.
\index{parametrized type}


\begin{figure}[h]
  \centering
  \includegraphics[scale=0.5]{param-classes.pdf}
  \caption{Example inheritance hierarchy for parametrized classes.}
  \label{fig:param-class-hierarchy}
\end{figure}


\section{Signatures and Parametrized Signatures}


A signature is a data type defined by specifying the procedures that
the object belonging to the signature has to implement. They
resemble Java interfaces but signatures are multiply
dispatched. Parametrized signatures are signatures parametrized by
type parameters. See sections \ref{subsec:expr-define-param-signature}
and \ref{subsec:expr-define-signature}.
\index{signature}
\index{parametrized signature}

If an application of a procedure contains signatures as an
argument type we use the following algorithm to check if the
application is valid:
\begin{enumerate}
  \item
    If the arguments contain free type variables the type is checked
    runtime or when the type variables are bound.
  \item
    Substitute keyword \skeyword{this} by the signature itself in all the
    procedure specifiers referring to the same procedure as the procedure
    to be called.
  \item
    If such specifiers were found check that the application argument
    type list is a subtype of an argument type of some of the
    substituted procedure specifiers. Otherwise handle the
    application as a normal procedure call.
\end{enumerate}

See section \ref{subsec:adts} for examples about signatures.


\section{Built-in Classes}


The classes listed in this section are also called \defterm{primitive
  classes}. An instance of a primitive class is called a
\defterm{primitive object}.
\index{primitive class}
\index{primitive object}


\subsection{\code{<object>}}

\index{\code{<object>}}
Every value in \theme\ is an instance of \code{<object>}. Every type
except \code{<none>} is a subtype of \code{<object>}. Class
\code{<object>} defines no fields. Class \code{<object>} is
inheritable, immutable, and not equal by value. Note that subclasses
of \code{<object>} do not need to be immutable.


\subsection{\code{<class>}}


\index{\code{<class>}}
Every class in \theme\ is an instance of \code{<class>}. Class
\code{<class>} is an instance of itself. Class \code{<class>} is
inheritable, immutable, and not equal by value.


\subsection{\code{<integer>}}


\index{\code{<integer>}}
Instances of class \code{<integer>} are integer numbers. Class
\code{<integer>} is immutable, equal by value, and not inheritable.


\subsection{\code{<real>}}


\index{\code{<real>}}
Instances of class \code{<real>} are real numbers. Class
\code{<real>} is immutable, equal by value, and not inheritable.
Note that \code{<integer>} objects are not instances of \code{<real>}.


\subsection{\code{<boolean>}}


\index{\code{<boolean>}}
Boolean values are similar to Scheme boolean values.
Class \code{<boolean>} is immutable, equal by value, and not
inheritable.


\subsection{\code{<null>}}


\index{\code{<null>}}
Class \code{<null>} is the class of an empty list.
The empty list object is denoted by \code{null} or \code{()}
and it behaves similarly to the empty list in Scheme.
Class \code{<null>} is immutable, equal by value, and not
inheritable. Note that if you use notation \code{()} you usually have
to quote it as in Scheme.


\subsection{\code{<symbol>}}


\index{\code{<symbol>}}
Symbols are similar to Scheme symbols.
Class \code{<symbol>} is immutable, equal by value, and not
inheritable.


\subsection{\code{<string>}}


\index{\code{<string>}}
Strings are similar to Scheme strings.
Class \code{<string>} is immutable, equal by value, and not
inheritable.


\subsection{\code{<character>}}


\index{\code{<character>}}
Characters are similar to Scheme characters.
Class \code{<character>} is immutable, equal by value, and not
inheritable.


\section{Built-in Logical Types}


\subsection{\builtin{<type>}}


\index{\builtin{<type>}}
Every type (class or logical type) in \theme\ is an instance of
\builtin{<type>}.


\subsection{\builtin{<none>}}


\index{\builtin{<none>}}
No object in \theme\ is an instance of \builtin{<none>}. The result
type of a procedure returning no value shall be \builtin{<none>}.


\section{Built-in Parametrized Classes}


The builtin parametrized classes are:
\begin{itemize}
\item \builtin{:procedure}
\item \builtin{:simple-proc}
\item \builtin{:param-proc}
\item \builtin{:gen-proc}
\item \builtin{:vector}
\item \builtin{:mutable-vector}
\item \builtin{:value-vector}
\item \builtin{:mutable-value-vector}
\item \builtin{:pair}
\end{itemize}


\index{\builtin{:procedure}}
\index{\builtin{:simple-proc}}
\index{\builtin{:param-proc}}
\index{\builtin{:gen-proc}}
\index{\builtin{:vector}}
\index{\builtin{:mutable-vector}}
\index{\builtin{:value-vector}}
\index{\builtin{:mutable-value-vector}}
\index{\builtin{:pair}}


See chapter \ref{ch:procedures} for descriptions of the procedure
classes. See subsection \ref{subsec:vectors} for descriptions of the
vector classes. See subsection \ref{subsec:pairs} for descriptions of
pairs.


\section{Built-in Parametrized Logical Types}


\subsection{\builtin{:union}}


\index{\builtin{:union}}

Let \syntaxformulacmd{\objectvar{u}} be a union type created by
\syntaxformulacmd{(\sbuiltin{:union}
  \indexedsyntaxvar{a}{1} \sldots \indexedsyntaxvar{a}{n})}.
Let
\begin{syntaxformula}
  \indexedobjectvar{t}{1} \sldots \indexedobjectvar{t}{m}
\end{syntaxformula}
be the translated argument list generated from
\begin{syntaxformula}
  \indexedsyntaxvar{a}{1} \sldots \indexedsyntaxvar{a}{n}
\end{syntaxformula},
see section \ref{subsec:static-type-expressions}.
An object
\syntaxformulacmd{\objectvar{obj}} is an
instance of \syntaxformulacmd{\objectvar{u}} if and only if
\syntaxformulacmd{\objectvar{obj}} is an instance of
some
\begin{syntaxformula}
  \indexedobjectvar{t}{k}
\end{syntaxformula},
\begin{syntaxformula}
  k = 1, \sldots, n
\end{syntaxformula}.
Object \syntaxformulacmd{\objectvar{obj}} is allowed
to be an instance of multiple component types
\syntaxformulacmd{\indexedobjectvar{t}{k'}}.


\subsection{\builtin{:uniform-list}}


\index{\builtin{:uniform-list}}

Let \syntaxformulacmd{\objectvar{u}} be a uniform list type created by
\syntaxformulacmd{(\sbuiltin{:uniform-list} \syntaxvar{a})}. Let
%%\syntaxformulacmd{(\sbuiltin{:uniform-list}\ \syntaxvar{a})}. Let
\syntaxformulacmd{(\objectvar{t})} be the translated argument list
generated from
\syntaxformulacmd{(\syntaxvar{a})}.
Objects of logical type \syntaxformulacmd{\objectvar{u}}
are lists having elements of type \syntaxformulacmd{\objectvar{t}}.
A parametrized logical type equivalent to \builtin{:uniform-list} can
be created with code

\begin{codeblock}
  \noindent
  \mbox{ } \hspace{0.5cm} (\ckeyword{declare} :my-list \cbuiltin{<param-logical-type>}) \\
  \noindent
  \mbox{ } \hspace{0.5cm} (\ckeyword{define-param-logical-type} :my-list (\%type) \\
  \noindent
  \mbox{ } \hspace{1cm} (\cbuiltin{:union} (\cbuiltin{:pair} \%type (:my-list \%type))
    \cbuiltin{<null>})
\end{codeblock}


\section{Recursive Definitions}


\index{recursion}


In general, when you define a variable recursively you have to forward
declare it. However, forward declaration is not needed with
\keyword{define-procedure} and \keyword{define-param-proc}.
Notice how a forward declaration of a logical type is done in the
following case:
\begin{codeblock}
  \noindent
  \mbox{ } \hspace{0.5cm} (\ckeyword{declare} <my-list> :union) \\
  \noindent
  \mbox{ } \hspace{0.5cm} (\ckeyword{define} <my-list> (:union (:pair
  <integer> <my-list>) <null>))  
\end{codeblock}


\section{Vectors}
\label{subsec:vectors}


\index{vector}


\subsection{General}


Parametrized classes \builtin{:vector}, \builtin{:mutable-vector},
\builtin{:value-vector}, and \builtin{:mutable-value-vector} are
called \defterm{vector metaclasses}. Instances of vector metaclasses
are called \defterm{general vector classes}. Objects of general vector
classes are called \defterm{general vectors}.


\subsection{Normal Vectors}


\index{\builtin{:vector}}
\index{normal vector}


Instances of \builtin{:vector} are called \defterm{normal vector classes}.
Objects of class \syntaxformulacmd{(\sbuiltin{:vector} \objectvar{t})} are immutable
one-dimensional vectors having elements of type \syntaxformulacmd{\objectvar{t}}.
See subsections \ref{subsec:sp-vector} and \ref{subsec:sp-make-vector}
for the creation of vectors.
The first element of a vector has index 0.


\subsection{Mutable Vectors}


\index{\builtin{:mutable-vector}}
\index{mutable vector}


Instances of \builtin{:mutable-vector} are called \defterm{mutable vector classes}.
Objects of class \syntaxformulacmd{(\sbuiltin{:mutable-vector} \objectvar{t})} are
mutable one-dimensional vectors having elements of type
\syntaxformulacmd{\objectvar{t}}.
See subsections \ref{subsec:sp-mutable-vector} and
\ref{subsec:sp-make-mutable-vector} for the creation of mutable
vectors.
The first element of a mutable vector has index 0.


\subsection{Value Vectors}


\index{\builtin{:value-vector}}
\index{value vector}


Instances of \builtin{:value-vector} are called \defterm{value vector classes}.
Class \builtin{:value-vector} is similar to \builtin{:vector} except
the instances of \builtin{:value-vector} are equal by value.
See subsections \ref{subsec:sp-value-vector} and
\ref{subsec:sp-make-value-vector} for the creation of value vectors.
The first element of a value vector has index 0.


\subsection{Mutable Value Vectors}


\index{\builtin{:mutable-value-vector}}
\index{mutable value vector}


Instances of \builtin{:mutable-value-vector} are called
\defterm{mutable value vector classes}.
Class \builtin{:mutable-value-vector} is similar to
\builtin{:mutable-vector} except
the instances of \builtin{:mutable-value-vector} are equal by value.
See subsections \ref{subsec:sp-mutable-value-vector} and
\ref{subsec:sp-make-mutable-value-vector} for the creation of mutable
value vectors.
The first element of a mutable value vector has index 0.


\section{Pairs and Tuples}
\label{subsec:pairs}


\index{pair}


When \indexedsyntaxvar{a}{1} and \indexedsyntaxvar{a}{2} are objects
the class of the pair \syntaxformulacmd{(\indexedsyntaxvarf{a}{1}
. \indexedsyntaxvarf{a}{2})} is \syntaxformulacmd{(\sbuiltin{:pair}
  \indexedsyntaxvarf{t}{1} \indexedsyntaxvarf{t}{2})} where  
\indexedsyntaxvar{t}{1} is the class of \indexedsyntaxvar{a}{1} and
\indexedsyntaxvar{t}{2} is the class of \indexedsyntaxvar{a}{2}.


Let
\begin{syntaxformula}
  \objectvar{u}
\end{syntaxformula}
be a pair class created by
\begin{syntaxformula}
  \slp \sbuiltin{:pair} \indexedsyntaxvar{a}{1}
  \indexedsyntaxvar{a}{2} \srp
\end{syntaxformula}.
Let
\syntaxformulacmd{\slp \indexedobjectvar{t}{1} \indexedobjectvar{t}{2}
\srp}
be the translated argument list generated from
\syntaxformulacmd{\slp \indexedsyntaxvar{a}{1} \indexedsyntaxvar{a}{2}
\srp}.
Object of type
\begin{syntaxformula}
  \objectvar{u}
\end{syntaxformula}
is an immutable pair
whose first component is of type
\begin{syntaxformula}
  \indexedobjectvar{t}{1}
\end{syntaxformula}
and second component of type
\begin{syntaxformula}
  \indexedobjectvar{t}{2}
\end{syntaxformula}.
Let
\begin{syntaxformula}
  \indexedsyntaxvar{a}{1}
\end{syntaxformula},
\begin{syntaxformula}
  \indexedsyntaxvar{a}{2}
\end{syntaxformula},
\begin{syntaxformula}
  \indexedsyntaxvar{b}{1}
\end{syntaxformula}, and
\begin{syntaxformula}
  \indexedsyntaxvar{b}{2}
\end{syntaxformula}
be type formulas.
Let
\syntaxformulacmd{\slp \indexedobjectvar{t}{1} \indexedobjectvar{t}{2} \srp}
be the translated argument list generated from
\syntaxformulacmd{\slp \indexedsyntaxvar{a}{1} \indexedsyntaxvar{a}{2} \srp}
and
\syntaxformulacmd{\slp \indexedobjectvar{u}{1} \indexedobjectvar{u}{2} \srp}
the translated argument list generated from
\syntaxformulacmd{\slp \indexedsyntaxvar{b}{1} \indexedsyntaxvar{b}{2} \srp}
Now type
\begin{syntaxformula}
  \slp \sbuiltin{:pair} \indexedsyntaxvar{a}{1} \indexedsyntaxvar{a}{2} \srp
\end{syntaxformula}
is a subtype of
\begin{syntaxformula}
  \slp \sbuiltin{:pair} \indexedsyntaxvar{b}{1} \indexedsyntaxvar{b}{2} \srp
\end{syntaxformula}
if and only if
\begin{syntaxformula}
  \indexedsyntaxvar{t}{1}
\end{syntaxformula}
is a subtype of
\begin{syntaxformula}
  \indexedsyntaxvar{u}{1}
\end{syntaxformula}
and
\begin{syntaxformula}
  \indexedsyntaxvar{t}{2}
\end{syntaxformula}
is a subtype of
\begin{syntaxformula}
  \indexedsyntaxvar{u}{2}
\end{syntaxformula}.


\index{tuple}

A tuple type is a type of a finite sequence of possibly nonuniform
objects.
Formally,
if \indexedsyntaxvarf{t}{k}, \(k = 1,\ldots,n\), are types the tuple
type \code{(:tuple \indexedsyntaxvar{t}{1}, ...,
  \indexedsyntaxvar{t}{n})} is equivalent to
\code{(:pair \indexedsyntaxvar{t}{1} (:pair \indexedsyntaxvar{t}{2}
  ... (:pair \indexedsyntaxvar{t}{n} <null>) ...))}.


\section{Foreign Function Interface}


\index{foreign function interface}
\index{\keyword{prim-proc}}
\index{\keyword{unchecked-prim-proc}}
\index{\keyword{param-prim-proc}}
\index{\keyword{unchecked-param-prim-proc}}
\index{\keyword{define-prim-class}}
\index{\keyword{define-goops-class}}
\index{\keyword{define-normal-goops-class}}


The semantics of \keyword{prim-proc},
\keyword{unchecked-prim-proc}, \keyword{param-prim-proc},
\keyword{unchecked-param-prim-proc}, \keyword{define-prim-class},
\keyword{define-goops-class}, and \keyword{define-normal-goops-class}
depend on the \theme\ translation target platform. 
See \ref{subsec:expr-prim-proc} and \ref{subsec:expr-param-prim-proc}
for the definition of primitive procedures.
The keyword \keyword{define-normal-goops-class} is discussed in the
standard library reference.
If you want to use your own Scheme procedures
with these keywords you can
specify the Scheme files to be loaded into the runtime
\theme\ environment with environment variable
\code{THEME\_D\_CUSTOM\_CODE}. Separate the file names with :'s.
See files \code{theme-d-code/tests/test223.thp} and
\code{runtime/run2.scm} for an example.
A Scheme implementation of a parametrized primitive procedure has to
take the type parameters as arguments before the proper procedure
arguments. See \code{theme-d-code/tests/test142.thp} and \newline
\code{theme-d-code/tests/aux-my-map.scm} for an example.
Custom primitive classes may be defined with keyword
\keyword{define-prim-class}. See section
\ref{subsec:define-prim-class} and tests \code{test223},
\code{test224}, and \code{test226}. GOOPS classes may be imported into
Theme-D with keyword \keyword{define-goops-class}. See section
\ref{subsec:define-goops-class} and tests \code{test279} and
\code{test280}.

Foreign function interface may cause problems with the linker output
stripping. For example, suppose you define class \code{<gtk-widget>}
and its subclass \code{<gtk-window>}.  Suppose also that you define
procedure \code{gtk-window-new} in your foreign code so that the
procedure returns a \code{gtk-window} but its declared return type is
\code{<gtk-widget>}. Then it is possible that the linker strips the
class \code{<gtk-window>} off from the target code even though it is
needed by the type system. This problem may be solved by a
\code{prevent-stripping} expression. For Theme-D-Gnome this can also
be solved by using creator procedures instead of GTK constructors.

The custom primitive classes have to be disjoint with each other and
with built-in primitive classes. That is, no object shall belong to
two different primitive classes. GOOPS classes may overlap with each
other but no two GOOPS classes shall be identical.


\section{Algorithm to Compute Subtype Relation}
\label{subsec:subtyping-algorithm}


\subsection{\algorithmname{IsSubtype}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a type \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: another type \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} if
\indexedobjectvarf{t}{1} is a subtype of \indexedobjectvarf{t}{2}, \code{\#f} otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtype}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}, \objectvarf{M}} \\


%%If the types of
%%\sta\ and \stb\ are different we always return \false.


\begin{enumerate}
\item
  If \sta\ is incomplete or \stb\ is incomplete return
  \true\ iff \sta\ and \stb\ are the same object and
  \false\ otherwise.
\item
  If \((t_1, t_2) \in M\) return \true.
\item
  Set \(M' := M \cup \{ (t_1, t_2) \}\).
\item
  If \(t_1 = t_2\) return \true.
\item
  If \(t_2 = \mbuiltin{<object>}\) return \true.
\item
  If \sta\ and \stb\ are type variables return \true\ iff \sta\ and
  \stb\ are equal.
\item
  If \sta\ and \stb\ are primitive classes then return \true\ iff they
  are the same object.
\item
  If \sta\ is not a signature and \stb\ is a signature return
  \algorithmappl{IsSubtypeXSignature}{\sta, \stb, \(M'\)}.
\item
  If \sta\ and \stb\ is are signatures return
  \algorithmappl{IsSignatureSubtype}{\sta, \stb, \(M'\)}.
\item
  If \sta\ is a union return
  \algorithmappl{IsSubtypeUnionX}{\sta, \stb, \(M'\)}.
\item
  If \stb\ is a union return
  \algorithmappl{IsSubtypeXUnion}{\sta, \stb, \(M'\)}.
\item
  If \(t_1 = \mbuiltin{<none>}\) then return \true\ iff
  \(t_2 = \mbuiltin{<none>}\).
\item
  If \(t_2 = \mbuiltin{<none>}\) then return \true\
  iff \(t_1 = \mbuiltin{<none>}\).
\item If both \sta\ and \stb\ are pair classes return
  \algorithmappl{IsSubtypePair}{\sta, \stb, \(M'\)}.
  If only one of \sta\ and \stb\ is a pair class return \false.
\item
  If both \sta\ and \stb\ are procedure types return
  \algorithmappl{IsSubtypeGeneralProc}{\sta, \stb, \(M'\)}.
\item If \sta\ and \stb\ are both vector classes, \sta\ =
  \code{(:vector <a>)} and \stb\ = \code{(:vector <b>)},
  return
  \algorithmappl{IsSubtype}{\code{<a>}, \code{<b>}, \(M'\)}.
\item If \sta\ and \stb\ are both value vector classes, \sta\ =
  \code{(:value-vector <a>)} and \stb\ = \code{(:value-vector <b>)},
  return
  \algorithmappl{IsSubtype}{\code{<a>}, \code{<b>}, \(M'\)}.
\item
  If both \sta\ and \stb\ are instances of a parametrized logical type
  whose type arguments contain type modifiers return 
  \true\ iff the contents of \sta\ and \stb\ are equal and
  \false\ otherwise.
\item
  If \sta\ and \stb\ are splice expressions return \true\ iff the
  component type of \sta\ is a subtype of the component type of \stb.
  If only \stb\ is a splice expression return \false.
\item
  If \sta\ and \stb\ are rest expressions return \true\ iff the
  component type of \sta\ is a subtype of the component type of \stb.
  If only \stb\ is a rest expression return \false.
\item
  If \sta\ and \stb\ are type list expressions return
  \algorithmappl{IsGeneralListSubtype}{\objectvarf{a}, \objectvarf{b}}
  where \objectvarf{a} and \objectvarf{b} are the contents of
  \sta\ and \stb\, respectively.
  If only \stb\ is a type list expression return \false.
\item
  If \sta\ and \stb\ are type loop expressions return
  \algorithmappl{IsSubtypeLoop}{\sta, \stb, \(M'\)}.
  If only \stb\ is a type loop expression return \false.
\item
  If \sta\ and \stb\ are type join expressions return
  \algorithmappl{IsGeneralListSubtype}{\objectvarf{a}, \objectvarf{b}}
  where \objectvarf{a} and \objectvarf{b} are the contents of
  \sta\ and \stb\, respectively.
  If only \stb\ is a type join expression return \false.
\item
  If \sta\ and \stb\ are instances of a parametrized class return
  \algorithmappl{IsSubtypeParamClassInst}{\sta, \stb, \(M'\)}.
\item
  If one of \sta\ and \stb\ is an instance of a parametrized class and
  one is class that is not an instance of a parametrized class return
  \algorithmappl{IsSubtypeParamClassMixed}{\sta, \stb, \(M'\)}.
\item If \sta\ and \stb\ are classes return
  \algorithmappl{IsSubtypeSimple}{\sta, \stb}.
\item else return \false.
\end{enumerate}


\subsection{\algorithmname{IsSubtypeSimple}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a class \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: another class \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} if
\indexedobjectvarf{t}{1} is a subtype of \indexedobjectvarf{t}{2}, \code{\#f} otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeSimple}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}} \\

\begin{enumerate}
\item If \indexedobjectvarf{t}{1} = \indexedobjectvarf{t}{2} return \true\ else
\item if \indexedobjectvarf{t}{2} = \builtin{<object>} return \true\ else
\item if \indexedobjectvarf{t}{1} = \builtin{<object>} and
  \indexedobjectvarf{t}{2} \(\neq\) \builtin{<object>} return \false\ else
\item return \algorithmappl{IsSubtypeSimple}{\objectvarf{s}, %
    \indexedobjectvarf{t}{2}} where
  \begin{objectformula}
    \imminh{\indexedobjectvar{t}{1}}{\objectvar{s}}
  \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{IsGeneralListSubtype}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{a}: a list of type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{b}: another list of type expressions \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} iff
\objectvarf{a} is a subtype of \objectvarf{b} \\


\noindent
\textit{Algorithm:} \algorithmappl{IsGeneralListSubtype}{\objectvarf{a}, %
  \objectvarf{b}} \\

Let
\begin{math}
  \mobjectvar{a} = (\indexedmobjectvar{a}{1}, \ldots, \indexedmobjectvar{a}{n})
\end{math}
and
\begin{math}
  \mobjectvar{b} = (\indexedmobjectvar{b}{1}, \ldots, \indexedmobjectvar{b}{m})
\end{math}
Return \true\ iff \(n = m\) and \(\indexedmobjectvar{a}{i}\) is a subtype
of \(\indexedmobjectvar{b}{i}\) for all \(i = 1, \ldots, n\).


\subsection{\algorithmname{IsSubtypeXUnion}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a class \\
\mbox{ } \hspace{1cm} \objectvarf{u}: a union type \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ if
\objectvarf{t} is a subtype of \objectvarf{u}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeXUnion}{\objectvarf{t}, %
  \objectvarf{u}, \objectvarf{M}} \\


\begin{enumerate}
\item
  Let \objectvarf{v} be the vector of the member types of \objectvarf{u},
  \begin{objectformula}
    \setvar{\objectvar{v}}{\slp \indexedobjectvar{u}{1} \sldots
      \indexedobjectvar{u}{n} \srp}
  \end{objectformula}.
\item
  Let
  \begin{objectformula}  
    \setvar{\objectvar{result}}{\false}
  \end{objectformula}.
\item
  For \(i := 1, \ldots, n\)
  \begin{enumerate}
  \item
    If
    \begin{objectformula}  
      \algorithmappl{IsSubtype}{\objectvar{t},\indexedobjectvar{u}{i}, %
        \objectvar{M}}
    \end{objectformula}
    then set
    \begin{objectformula}  
      \setvar{\objectvar{result}}{\true}
    \end{objectformula}
    and break the loop.
  \end{enumerate}
\item
  Return 
  \objectvarf{result}.
\end{enumerate}


\subsection{\algorithmname{IsSubtypeUnionX}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{u}: a union type \\
\mbox{ } \hspace{1cm} \objectvarf{t}: a class \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ if
\objectvarf{u} is a subtype of \objectvarf{t}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeUnionX}{\objectvarf{u}, %
  \objectvarf{t}, \objectvarf{M}} \\


\begin{enumerate}
\item
  Let \objectvarf{v} be the vector of the member types of \objectvarf{u},
  \begin{objectformula}
    \setvar{\objectvar{v}}{\slp \indexedobjectvar{u}{1} \sldots
      \indexedobjectvar{u}{n} \srp}
  \end{objectformula}.
\item
  Let
  \begin{objectformula}  
    \setvar{\objectvar{result}}{\true}
  \end{objectformula}.
\item
  For \(i := 1, \ldots, n\)
  \begin{enumerate}
  \item
    If 
    \begin{objectformula}  
      \algorithmappl{IsSubtype}{\indexedobjectvar{u}{i}, %
        \objectvar{t} \objectvar{M}} = \false
    \end{objectformula}
    then set
    \begin{objectformula}  
      \setvar{\objectvar{result}}{\false}
    \end{objectformula}
    and break the loop.
  \end{enumerate}
\item
  Return 
  \objectvarf{result}
\end{enumerate}


\subsection{\algorithmname{IsSubtypePair}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a pair class \\
\mbox{ } \hspace{1cm} \objectvarf{u}: a pair class \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ if
\objectvarf{t} is a subtype of \objectvarf{u}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypePair}{\objectvarf{t}, %
  \objectvarf{u}, \objectvarf{M}} \\

Let
\begin{objectformula}
  \slp \indexedobjectvarf{a}{1} \indexedobjectvarf{a}{2} \srp
\end{objectformula}
be the component types of \objectvarf{t}
and
\begin{objectformula}
  \slp \indexedobjectvarf{b}{1} \indexedobjectvarf{b}{2} \srp
\end{objectformula}
be the component types of \objectvarf{u}.


\begin{enumerate}
\item
  If
  \begin{objectformula}
    \algorithmappl{IsSubtype}{\indexedobjectvar{a}{1}, %
        \indexedobjectvar{b}{1}, \objectvar{M}}
  \end{objectformula}
  return
  \begin{objectformula}
    \algorithmappl{IsSubtype}{\indexedobjectvar{a}{2}, %
      \indexedobjectvar{b}{2}, \objectvar{M}}
  \end{objectformula}
  else return
  \begin{objectformula}
    \false
  \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{IsSubtypeGeneralProc}}
\label{subsec:IsSubtypeGeneralProc}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a procedure type \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: a procedure type \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ iff
\objectvarf{t} is a subtype of \objectvarf{u} \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeGeneralProc}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}, \objectvarf{M}} \\


If any of the following is true return \false:
\begin{enumerate}
\item
  Object \sta\ is an abstract procedure type and \stb\ is not an
  abstract procedure type.
\item
  Object \sta\ is a simple procedure class and \stb\ is either a
  parametrized or generic procedure class.
\item
  Object \sta\ is either a parametrized or generic procedure class
  and \stb\ is a simple procedure class.
\item
  Object \sta\ is a parametrized procedure class and \stb\ is a
  generic procedure class.
\end{enumerate}
If some of the following is true:
\begin{enumerate}
\item Objects \sta\ and \stb\ are abstract procedure types.
\item Objects \sta\ and \stb\ are simple procedure classes.
\item Object \sta\ is a simple procedure class and \stb\ an
  abstract procedure type.
\end{enumerate}
return \algorithmappl{IsSubtypeProc}{\sta, \stb, \objectvarf{M}}.

If \sta\ is a parametrized procedure class and \stb\ is an abstract
procedure class return
\algorithmappl{IsSubtypeParamAbstract}{\sta, \stb, \objectvarf{M}}.
If \sta\ and \stb\ are parametrized procedure classes return
\algorithmappl{IsSubtypeParamProc}{\sta, \stb}.
If \sta\ is a generic procedure class and \stb\ is an abstract
procedure class return
\algorithmappl{IsSubtypeGenAbstract}{\sta, \stb, \objectvarf{M}}.
If \sta\ and \stb\ are generic procedure classes return
\algorithmappl{IsSubtypeGenericProc}{\sta, \stb, \objectvarf{M}}.

If \sta\ is a generic procedure class and \stb\ is a parametrized
procedure class return \true\ iff the tree structure of some of the
methods of \sta\ is identical to \stb (type variables may be named
differently).


\subsection{\algorithmname{ProcAttributesMatch}}
\label{subsec:ProcAttributesMatch}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \((p_1, a_1, n_1 )\): attributes of the
first procedure \\
\mbox{ } \hspace{1cm} \((p_2, a_2, n_2 \): attributes of the
second procedure \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ iff the first
procedure type can be a subtype of the second


The attributes are: (purity, always returns, and never returns). All
of them are boolean valued. The algorithm returns \true\ iff both of
the following conditions are true:
\begin{itemize}
  \item
    \begin{math}
      \lnot ( ( \lnot p_1 ) \land p_2 )
    \end{math}
  \item
    \begin{math}
      ((\lnot a_2) \land (\lnot n_2))
      \lor (a_1 = a_2 \land n_1 = n_2)
    \end{math}
\end{itemize}


\subsection{\algorithmname{IsSubtypeProc}}
\label{subsec:IsSubtypeProc}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{u}: a procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ if
\objectvarf{t} is a subtype of \objectvarf{u}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeProc}{\objectvarf{t}, %
  \objectvarf{u}, \objectvarf{M}} \\


Let \indexedobjectvarf{A}{1} be the procedure attributes of
\objectvarf{t} and \indexedobjectvarf{A}{2} the procedure attributes
of \objectvarf{u}.
Let \indexedobjectvarf{a}{1} be the argument list type of \objectvarf{t},
\indexedobjectvarf{r}{1} the result type of \objectvarf{t}, and
\indexedobjectvarf{p}{1} the purity (boolean value) of \objectvarf{t}.
Define the corresponding variables \indexedobjectvarf{a}{2},
\indexedobjectvarf{r}{2}, and \indexedobjectvarf{p}{2} for \objectvarf{u}.


If
\algorithmappl{ProcAttributesMatch}{\indexedobjectvarf{A}{1},\indexedobjectvarf{A}{2}}
is true then
\begin{enumerate}
\item
  Let
  \begin{objectformula}
    \setvar{\indexedobjectvar{st}{1}}%
    {\algorithmappl{IsSubtype}{\indexedobjectvar{a}{2},
        \indexedobjectvar{a}{1}, \objectvar{M}}} .
  \end{objectformula}
\item
  If
  \begin{objectformula}
    \indexedobjectvar{st}{1} = \true
  \end{objectformula}
  then return
  \begin{objectformula}
    \algorithmappl{IsSubtype}{\indexedobjectvar{r}{1},
      \indexedobjectvar{r}{2}, \objectvar{M}}
  \end{objectformula}
  else return
  \begin{objectformula}
    \false
  \end{objectformula}.
\end{enumerate}
else return
\begin{objectformula}
  \false
\end{objectformula}.


\subsection{\algorithmname{IsSubtypeParamAbstract}}
\label{subsec:IsSubtypeParamAbstract}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a parametrized procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{u}: an abstract procedure type \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ if
\objectvarf{t} is a subtype of \objectvarf{u}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeParamAbstract}{\objectvarf{t}, %
  \objectvarf{u}, \objectvarf{M}} \\


Let \indexedobjectvarf{A}{1} be the procedure attributes of
\objectvarf{t} and \indexedobjectvarf{A}{2} the procedure attributes
of \objectvarf{u}.
If
\algorithmappl{ProcAttributesMatch}{\indexedobjectvarf{A}{1},\indexedobjectvarf{A}{2}}
is true then
\begin{enumerate}
\item
  Deduce type parameters for types \objectvarf{t} and \objectvar{u}. See section
  \ref{sec:param-proc-appl}.
\item
  If some of the type parameters in objects \objectvarf{t} and \objectvarf{u}
  could not be deduced return \false.
\item Substitute the deduced type parameter values to objects
  \objectvarf{t} and \objectvarf{u}.
  Denote the result objects \objectvartp\ and \objectvarup.
  Let \indexedobjectvarf{a}{1} be the argument list type of \objectvartp\ and
  \indexedobjectvarf{r}{1} the result type of \objectvartp.
  Define the corresponding variables \indexedobjectvarf{a}{2} and
  \indexedobjectvarf{r}{2} for \objectvarup.
\item
  If \indexedobjectvarf{r}{1} is a subtype of \indexedobjectvarf{r}{2}
  and \indexedobjectvarf{a}{2} is a subtype of \indexedobjectvarf{a}{1} (note
  the order) return \true\ else return \false.
\end{enumerate}
else return \false.


\subsection{\algorithmname{IsSubtypeParamProc}}
\label{subsec:IsSubtypeParamProc}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a parametrized procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{u}: a parametrized procedure class \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \true\ if
\objectvarf{t} is identical to \objectvarf{u}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeParamProc}{\objectvarf{t}, %
  \objectvarf{u}} \\


If \objectvarf{t} and \objectvarf{u} have the same number of type parameters
create new type variables and substitute them into \objectvarf{t} and
\objectvarf{u}.
Return \true\ iff the new type \objectvarf{t'} is a subtype of the new
type \objectvarf{u'}.


\subsection{\algorithmname{IsSubtypeGenAbstract}}
\label{subsec:IsSubtypeGenAbstract}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{u}: an abstract procedure type \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ if
\objectvarf{t} is a subtype of \objectvarf{u}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeGenAbstract}{\objectvarf{t}, %
  \objectvarf{u}, \objectvarf{M}} \\


\begin{enumerate}
  \item
    Let
    \begin{objectformula}
      \setvar{\objectvar{m}}{\textrm{the list of methods of \objectvar{t}}}
    \end{objectformula} and
    \begin{objectformula}
      \setvar{\objectvar{n}}{\textrm{the number of methods in \objectvar{m}}}
    \end{objectformula}.
  \item
    For \(i := 1, \ldots, n\)
    \begin{enumerate}
      \item
        If
        \begin{objectformula}
          \algorithmappl{IsSubtype}{\objectvar{m}[i],
              \objectvar{u}, \objectvar{M}}
        \end{objectformula}
        break the loop and return \true.
    \end{enumerate}
  \item
    Return \false.
\end{enumerate}


\subsection{\algorithmname{IsSubtypeGenericProc}}
\label{subsec:IsSubtypeGenericProc}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{u}: a generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \true\ if
\objectvarf{t} is a subtype of \objectvarf{u}, \false\ otherwise \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeGenericProc}{\objectvarf{t}, %
  \objectvarf{u}, \objectvarf{M}} \\


\begin{enumerate}
  \item
    Let
    \begin{objectformula}
      \setvar{\indexedobjectvar{m}{1}}{\textrm{the list of methods of \objectvar{t}}}
    \end{objectformula},
    \begin{objectformula}
      \setvar{\indexedobjectvar{m}{2}}{\textrm{the list of methods of \objectvar{u}}}
    \end{objectformula},
    \begin{objectformula}
      \setvar{\indexedobjectvar{n}{1}}{\textrm{the number of methods in \indexedobjectvar{m}{1}}}
    \end{objectformula}, and
    \begin{objectformula}
      \setvar{\indexedobjectvar{n}{2}}{\textrm{the number of methods in \indexedobjectvar{m}{2}}}
    \end{objectformula}.
  \item
    Let
    \begin{objectformula}
      \setvar{\objectvar{result2}}{\true}
    \end{objectformula}.
  \item
    For \(i := 1, \ldots, n_1\)
    \begin{enumerate}
      \item
        \begin{objectformula}
          \setvar{\objectvar{result1}}{\false}
        \end{objectformula}
      \item
        For \(j := 1, \ldots, n_2\)
        \begin{enumerate}
          \item
            If
            \begin{objectformula}
              \algorithmappl{IsSubtype}{\indexedobjectvar{m}{1}[i],
                  \indexedobjectvar{m}{2}[j], \objectvar{M}}
            \end{objectformula} then set
            \begin{objectformula}
              \setvar{\objectvar{result1}}{\true}
            \end{objectformula}
            and break the inner loop.
        \end{enumerate}
      \item
        If
        \begin{objectformula}
          \objectvar{result1} = \false
        \end{objectformula}
        then set
        \begin{objectformula}
          \setvar{\objectvar{result2}}{\false}
        \end{objectformula}
        and break the outer loop.
    \end{enumerate}
    \item
      Return
      \begin{objectformula}
        \objectvar{result2}
      \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{IsSubtypeParamClassInst}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a class \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: another class \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} if
\indexedobjectvarf{t}{1} is a subtype of \indexedobjectvarf{t}{2}, \code{\#f} otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{IsSubtypeParamClassInst}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}, \objectvarf{M}} \\


\begin{enumerate}
\item If \indexedobjectvarf{t}{2} = \builtin{<object>} return \true\ else
\item if \indexedobjectvarf{t}{1} = \builtin{<object>} and
  \indexedobjectvarf{t}{2} \(\neq\) \builtin{<object>} return \false\ else
\item
  if
  \begin{objectformula}
    \algorithmappl{ParamClassInstEqual}{\indexedobjectvarf{t}{1},
      \indexedobjectvarf{t}{2}, \objectvar{M}}
  \end{objectformula}
  return \true\ 
  else return
  \algorithmappl{IsSubtype}{\objectvarf{s}, %
    \indexedobjectvarf{t}{2}, M} where
  \begin{objectformula}
    \imminh{\indexedobjectvar{t}{1}}{\objectvar{s}}
  \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{IsSubtypeParamClassMixed}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a class \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: another class \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} if
\indexedobjectvarf{t}{1} is a subtype of \indexedobjectvarf{t}{2}, \code{\#f} otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{IsSubtypeParamClassMixed}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}, \objectvarf{M}} \\


\begin{enumerate}
\item If \indexedobjectvarf{t}{2} = \builtin{<object>} return \true\ else
\item if \indexedobjectvarf{t}{1} = \builtin{<object>} and
  \indexedobjectvarf{t}{2} \(\neq\) \builtin{<object>} return \false\ else
\item
  else return
  \algorithmappl{IsSubtype}{\objectvarf{s}, %
    \indexedobjectvarf{t}{2}, M} where
  \begin{objectformula}
    \imminh{\indexedobjectvar{t}{1}}{\objectvar{s}}
  \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{ParamClassInstEqual}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a class \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: another class \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \code{\#t} if
\indexedobjectvarf{t}{1} is equal to \indexedobjectvarf{t}{2}, \code{\#f} otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{ParamClassInstEqual}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}, \objectvarf{M}} \\


Let
\begin{objectformula}
  \setvar{\indexedobjectvar{p}{1}}{\true\ iff \indexedobjectvar{t}{1} is an
    instance of a parametrized class}
\end{objectformula}
and
\begin{objectformula}
  \setvar{\indexedobjectvar{p}{2}}{\true\ iff \indexedobjectvar{t}{2} is an
    instance of a parametrized class}
\end{objectformula}.


\begin{enumerate}
\item
  If \((\lnot p_1) \land (\lnot p_2)\) return \(t_1 = t_2\) as an object
\item
  else if \(((\lnot p_1) \land p_2) \lor (p_1 \land (\lnot p_2))\)
  return \false
\item
  else if
  \begin{enumerate}
  \item
    \begin{objectformula}
      \slp \sbuiltin{class-of} \indexedobjectvar{t}{1} \srp
    \end{objectformula}
    is equal to
    \begin{objectformula}
      \slp \sbuiltin{class-of} \indexedobjectvar{t}{2} \srp
    \end{objectformula}
    as an object,
  \item
    Class \indexedobjectvarf{t}{1} has as many type parameters as
    class \indexedobjectvarf{t}{2} (we know here that both
    \indexedobjectvarf{t}{1} and \indexedobjectvarf{t}{2} have to be
    instances of parametrized classes), and
  \item
    Each of the type parameter of \indexedobjectvarf{t}{1} is equal to the
    corresponding type parameter of \indexedobjectvarf{t}{2} (Here
    equality of types \objectvarf{a} and \objectvarf{b} means that
    \objectformulacmd{\inherits{\objectvar{a}}{\objectvar{b}}} and
    \objectformulacmd{\inherits{\objectvar{b}}{\objectvar{a}}})
  \end{enumerate}
  return \true\ else return \false.
\end{enumerate}


\subsection{\algorithmname{IsSubtypeLoop}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a loop expression \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: another loop expression \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} iff
\indexedobjectvarf{t}{1} is a subtype of \indexedobjectvarf{t}{2} \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeLoop}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}} \\

If the iteration variables of \indexedobjectvarf{t}{1} and
\indexedobjectvarf{t}{2} are the same return \true\ iff the subtype
lists of \indexedobjectvarf{t}{1} and \indexedobjectvarf{t}{2} are
equal (have equal tree structures) and the iteration expression of
\indexedobjectvarf{t}{1} is a subtype of the iteration expression of
\indexedobjectvarf{t}{2}.
If the iteration variables are not equal create a new type variable,
substitute it into \indexedobjectvarf{t}{1} and
\indexedobjectvarf{t}{2}, and do the same check as above.


\subsection{\algorithmname{IsSubtypeXSignature}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a type that is not a signature \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: a signature type \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} iff
\indexedobjectvarf{t}{1} is a subtype of \indexedobjectvarf{t}{2} \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSubtypeXSignature}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}, \(M\)} \\


We have \objectformulacmd{\inherits{\indexedobjectvar{t}{1}}{\indexedobjectvar{t}{2}}} iff
for each specifier
\begin{objectformula}
  \objectvar{s} = \olp \objectvar{proc-name} \objectvar{args}
  \objectvar{result} \objectvar{attributes} \orp
\end{objectformula}
in the complete specifier list of \indexedobjectvarf{t}{2}
there exists a procedure (simple, parametrized, or generic) with name
\objectvarf{proc-name} so that the class of this procedure is a subtype
of the abstract procedure type
\begin{objectformula}
  \olp \obuiltin{:procedure} \objectvar{args}
  \objectvar{result} \objectvar{attributes} \orp
\end{objectformula}
where the keyword \code{this} has been substituted with type
\indexedobjectvarf{t}{1}.
We will use this algorithm for computing the subtyping of parametrized
signatures, too.


\subsection{\algorithmname{IsSignatureSubtype}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{1}: a signature type \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{t}{2}: a signature type \\
\mbox{ } \hspace{1cm} \objectvarf{M}: the set (list) of types already visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{is-subtype?} : \code{\#t} iff
\indexedobjectvarf{t}{1} is a subtype of \indexedobjectvarf{t}{2} \\


\noindent
\textit{Algorithm:} \algorithmappl{IsSignatureSubtype}{\indexedobjectvarf{t}{1}, %
  \indexedobjectvarf{t}{2}, \(M\)} \\


We have \objectformulacmd{\inherits{\indexedobjectvar{t}{1}}{\indexedobjectvar{t}{2}}} iff
for each specifier
\begin{objectformula}
  \indexedobjectvar{s}{2} = \olp \indexedobjectvar{proc-name}{2}
  \indexedobjectvar{args}{2}
  \indexedobjectvar{result}{2} \indexedobjectvar{attributes}{2} \orp
\end{objectformula}
in the complete specifier list of \indexedobjectvarf{t}{2}
there exists a specifier
\begin{objectformula}
  \indexedobjectvar{s}{1} = \olp \indexedobjectvar{proc-name}{1}
  \indexedobjectvar{args}{1}
  \indexedobjectvar{result}{1} \indexedobjectvar{attributes}{1} \orp
\end{objectformula}
in the complete specifier list of \indexedobjectvarf{t}{1}
so that the procedure names \indexedobjectvarf{proc-name}{1} and
\indexedobjectvarf{proc-name}{2} are equal
and
\begin{objectformula}
  \olp \obuiltin{:procedure} \indexedobjectvar{args}{1}
  \indexedobjectvar{result}{1} \indexedobjectvar{attributes}{1} \orp
\end{objectformula}
is a subtype of
\begin{objectformula}
  \olp \obuiltin{:procedure} \indexedobjectvar{args}{2}
  \indexedobjectvar{result}{2} \indexedobjectvar{attributes}{2} \orp .
\end{objectformula}


\section{Algorithms to Compute Equivalence of Objects}


\subsection{General}


When we refer to Scheme procedures in this section we assume that they
behave as specified in \cite{shinn2017}. Note that algorithms
\algorithmname{EqualPrimitiveValues?} and
\algorithmname{EqualPrimitiveObjects?} differ only in their handling of
strings.


\subsection{\algorithmname{EqualValues?}}
\label{subsubsec:EqualValues?}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{obj1}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{obj2}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \true\ if \objectvarf{obj1} is equal to
\objectvarf{obj2}, \false\ otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualValues?}{\objectvarf{obj1}, \objectvarf{obj2},
  \objectvarf{v}} \\


\begin{enumerate}
\item
  If \objectvarf{obj1} and \objectvarf{obj2} are the same nonprimitive
  object return \true.
\item
  If
  \begin{math}
    ( \mobjectvar{obj1} \;  \mobjectvar{obj2} ) \in \mobjectvar{v}
  \end{math}
  return \true.
\item
  Let \objectvarf{cl1} to be the class of \objectvarf{obj1} and
  \objectvarf{cl2} the class of \objectvarf{obj2}.
\item
  If not
  \algorithmappl{EqualTypes?}{\objectvarf{cl1}, \objectvarf{cl2},
    \objectvarf{visited}}
  return \false.
\item
  If \objectvarf{obj1} (and \objectvarf{obj2}) is a primitive object
  return
  \algorithmappl{EqualPrimitiveValues?}{\objectvarf{obj1},
    \objectvarf{obj2}}.
\item
  Let
  \begin{codeformula}
    \vprime := (cons (cons \objectvar{obj1} \objectvar{obj2}) \objectvar{v}) 
  \end{codeformula}.
\item
  If \objectvarf{obj1} (and \objectvarf{obj2}) is a pair return
  \algorithmappl{EqualPairs?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\item
  If \objectvarf{cl1} (and \objectvarf{cl2}) is a type return
  \algorithmappl{EqualTypes?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\item
  If
  \begin{objectformula}
    \objectvar{cl1} = \olp \obuiltin{:value-vector} \objectvar{t} \orp
  \end{objectformula}
  or
  \begin{objectformula}
    \objectvar{cl1} = \olp \obuiltin{:mutable-value-vector}
    \objectvar{t} \orp
  \end{objectformula}
  for some type \objectvarf{t} return
  \algorithmappl{EqualVectors?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\item
  If \objectvarf{cl1} (and \objectvarf{cl2}) is equal by value return
  \algorithmappl{EqualByValue?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\item
  Otherwise return \false.
\end{enumerate}


\subsection{\algorithmname{EqualContents?}}
\label{subsubsec:EqualContents?}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{obj1}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{obj2}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \true\ if the contents of \objectvarf{obj1} are
equal to the contents of \objectvarf{obj2}, \false\ otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualContents?}{\objectvarf{obj1}, \objectvarf{obj2},
  \objectvarf{v}} \\


\begin{enumerate}
\item
  If \objectvarf{obj1} and \objectvarf{obj2} are the same nonprimitive
  object return \true.
\item
  If
  \begin{math}
    ( \mobjectvar{obj1} \;  \mobjectvar{obj2} ) \in \mobjectvar{v}
  \end{math}
  return \true.
\item
  Let \objectvarf{cl1} to be the class of \objectvarf{obj1} and
  \objectvarf{cl2} the class of \objectvarf{obj2}.
\item
  If not
  \algorithmappl{EqualTypes?}{\objectvarf{cl1}, \objectvarf{cl2},
    \objectvarf{visited}}
  return \false.
\item
  If \objectvarf{obj1} (and \objectvarf{obj2}) is a primitive object
  return
  \algorithmappl{EqualPrimitiveValues?}{\objectvarf{obj1},
    \objectvarf{obj2}}.
\item
  Let
  \begin{codeformula}
    \vprime := (cons (cons \objectvar{obj1} \objectvar{obj2}) \objectvar{v}) 
  \end{codeformula}.
\item
  If \objectvarf{obj1} (and \objectvarf{obj2}) is a pair return
  \algorithmappl{EqualPairContents?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\item
  If \objectvarf{cl1} (and \objectvarf{cl2}) is a type return
  \algorithmappl{EqualTypes?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\item
  If \objectvarf{cl1} (and \objectvarf{cl2}) is a general vector class return
  %% \begin{objectformula}
  %%   \objectvar{cl1} = \olp \obuiltin{:vector} \objectvar{t} \orp
  %% \end{objectformula},
  %% \begin{objectformula}
  %%   \objectvar{cl1} = \olp \obuiltin{:mutable-vector} \objectvar{t} \orp
  %% \end{objectformula},
  %% \begin{objectformula}
  %%   \objectvar{cl1} = \olp \obuiltin{:value-vector} \objectvar{t} \orp
  %% \end{objectformula}, or
  %% \begin{objectformula}
  %%   \objectvar{cl1} = \olp \obuiltin{:mutable-value-vector}
  %%   \objectvar{t} \orp
  %% \end{objectformula}
  %% for some type \objectvarf{t} return
  \algorithmappl{EqualVectorContents?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\item
  Otherwise return
  \algorithmappl{EqualFields?}{\objectvarf{obj1}, \objectvarf{obj2},
    \objectvarf{\vprime}}.
\end{enumerate}


\subsection{\algorithmname{EqualObjects?}}
\label{subsubsec:EqualObjects?}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{obj1}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{obj2}: an object \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \true\ if \objectvarf{obj1} and
\objectvarf{obj2} are the same object, \false\ otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualObjects?}{\objectvarf{obj1}, \objectvarf{obj2}} \\


\begin{enumerate}
\item
  If \objectvarf{obj1} is a primitive object
  \begin{enumerate}
  \item
    Let \objectvarf{cl1} be the class of \objectvarf{obj1} and
    \objectvarf{cl2} the class of \objectvarf{obj2}.
  \item
    If \algorithmappl{EqualTypes?}{\objectvarf{cl1}, \objectvarf{cl2},
      ()}
    \begin{enumerate}
    \item
      return \algorithmappl{EqualPrimitiveObjects?}{\objectvarf{obj1},
      \objectvarf{obj2}}
    \end{enumerate}
    else
    \begin{enumerate}
    \item
      return \false
    \end{enumerate}
  \end{enumerate}
  else
  \begin{enumerate}
  \item
    If \objectvarf{obj1} and \objectvarf{obj2} are the same
    nonprimitive object return \true\ else return \false.
  \end{enumerate}
\end{enumerate}


\subsection{\algorithmname{EqualTypes?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t1}: a type \\
\mbox{ } \hspace{1cm} \objectvarf{t2}: a type \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of type pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \true\ if types \objectvarf{t1} and
\objectvarf{t2} are equal, \false\ otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualTypes?}{\objectvarf{t1}, \objectvarf{t2},
  \objectvarf{v}} \\


\begin{enumerate}
\item
  If \objectvarf{t1} and \objectvarf{t2} are the same nonprimitive
  object return \true.
\item
  If
  \begin{math}
    ( \mobjectvar{t1} \;  \mobjectvar{t2} ) \in \mobjectvar{v}
  \end{math}
  return \true.
\item
  Let
  \begin{codeformula}
    \vprime := (cons (cons \objectvar{t1} \objectvar{t2}) \objectvar{v}) 
  \end{codeformula}.
\item
  If \objectvarf{t1} and \objectvarf{t2} are pair classes return
  \begin{displaymath}
    \algorithmappl{EqualTypes?}{\mobjectvar{t1}[1],
      \mobjectvar{t2}[1], v'}
    \land
    \algorithmappl{EqualTypes?}{\mobjectvar{t1}[2],
      \mobjectvar{t2}[2], v'} .
  \end{displaymath}
\item
  If \objectvarf{t1} or \objectvarf{t2} is a pair class return \false.
\item
  If \objectvarf{t1} is a general vector class then
  \begin{enumerate}
  \item
    If the classes of \objectvarf{t1} and \objectvarf{t2} are not
    the same nonprimitive object return \false.
  \item
    Let \objectvarf{u1} be the component type of \objectvarf{t1} and
    \objectvarf{u2} the component type of \objectvarf{t2}.
  \item
    Return
    \algorithmappl{EqualTypes?}{\objectvarf{u1}, \objectvarf{u2},
      \objectvarf{\vprime}}.
  \end{enumerate}
\item
  If \objectvarf{t1} and \objectvarf{t2} are classes
  \begin{enumerate}
  \item
    If \objectvarf{t1} and \objectvarf{t2} are instances of a
    parametrized class return the value of
    \algorithmappl{ParamClassInstEqual}{\indexedobjectvarf{t}{1}, %
      \indexedobjectvarf{t}{2}, ()}
  \item
    If \objectvarf{t1} or \objectvarf{t2} is an instance of a
    parametrized class return \false.
  \item
    If \objectvarf{t1} and \objectvarf{t2} are the same nonprimitive
    object return \true\ else return \false.
  \end{enumerate}
\item
  If \objectvarf{t1} or \objectvarf{t2} is a class return \false.
\item
  Otherwise return
  \begin{objectformula}
    \inherits{\objectvar{t1}}{\objectvar{t2}} \(\land\)
    \inherits{\objectvar{t2}}{\objectvar{t1}}
  \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{EqualByValue?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{obj1}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{obj2}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{equal?} : \code{<boolean>} \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualByValue?}{\objectvarf{obj1}, \objectvarf{obj2},
  \objectvarf{v}} \\


\textit{Note:} We assume that the classes of \objectvarf{obj1} and
\objectvarf{obj2} are equal in the sense of
\algorithmname{EqualTypes?}.


\begin{enumerate}
\item
  Let \objectvarf{cl} be the class of \objectvarf{obj1} (and
  \objectvarf{obj2}).
\item
  Let
  \begin{objectformula}
    \setvar{\objectvar{result}}{\true}
  \end{objectformula}.
\item
  For each field \objectvarf{fld} in the field list of class
  \objectvarf{cl} do
  \begin{enumerate}
  \item
    Let
    \objectvarf{f1} := the value of the field \objectvarf{fld} in
    object \objectvarf{obj1}
    and
    \objectvarf{f2} := the value of the field \objectvarf{fld} in
    object \objectvarf{obj2}.
  \item
    If not \algorithmappl{EqualValues?}{\objectvarf{f1}, \objectvarf{f2},
      \objectvarf{v}}
    set
    \begin{objectformula}
      \setvar{\objectvar{result}}{\false}
    \end{objectformula}
    and break the loop.
  \end{enumerate}
\item
  Return \objectvarf{result}.
\end{enumerate}


\subsection{\algorithmname{EqualFields?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{obj1}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{obj2}: an object \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{equal?} : \code{<boolean>} \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualFields?}{\objectvarf{obj1}, \objectvarf{obj2},
  \objectvarf{v}} \\


\textit{Note:} We assume that the classes of \objectvarf{obj1} and
\objectvarf{obj2} are equal in the sense of
\algorithmname{EqualTypes?}.


\begin{enumerate}
\item
  Let \objectvarf{cl} be the class of \objectvarf{obj1} (and
  \objectvarf{obj2}).
\item
  Let
  \begin{objectformula}
    \setvar{\objectvar{result}}{\true}
  \end{objectformula}.
\item
  For each field \objectvarf{fld} in the field list of class
  \objectvarf{cl} do
  \begin{enumerate}
  \item
    Let
    \objectvarf{f1} := the value of the field \objectvarf{fld} in
    object \objectvarf{obj1}
    and
    \objectvarf{f2} := the value of the field \objectvarf{fld} in
    object \objectvarf{obj2}.
  \item
    If not \algorithmappl{EqualContents?}{\objectvarf{f1}, \objectvarf{f2},
      \objectvarf{v}}
    set
    \begin{objectformula}
      \setvar{\objectvar{result}}{\false}
    \end{objectformula}
    and break the loop.
  \end{enumerate}
\item
  Return \objectvarf{result}.
\end{enumerate}


\subsection{\algorithmname{EqualPairs?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{p1}: a pair \\
\mbox{ } \hspace{1cm} \objectvarf{p2}: a pair \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm}
\begin{objectformula}
  \algorithmappl{EqualValues?}{\objectvar{p1}[1], \objectvar{p2}[1],
    \objectvar{v}}
  \(\land\)
  \algorithmappl{EqualValues?}{\objectvar{p1}[2], \objectvar{p2}[2],
    \objectvar{v}}
\end{objectformula}


\subsection{\algorithmname{EqualPairContents?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{p1}: a pair \\
\mbox{ } \hspace{1cm} \objectvarf{p2}: a pair \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm}
\begin{objectformula}
  \algorithmappl{EqualContents?}{\objectvar{p1}[1], \objectvar{p2}[1],
    \objectvar{v}}
  \(\land\)
  \algorithmappl{EqualContents?}{\objectvar{p1}[2], \objectvar{p2}[2],
    \objectvar{v}}
\end{objectformula}


\subsection{\algorithmname{EqualPrimitiveValues?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{obj1}: a primitive value \\
\mbox{ } \hspace{1cm} \objectvarf{obj2}: a primitive value \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{equal?} : \code{<boolean>} \\


\noindent
\textit{Preconditions:} \\
The classes of \objectvarf{obj1} and \objectvarf{obj2} have to be
equal and they have to be a primitive class. \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualPrimitiveValues?}{\objectvarf{obj1}, \objectvarf{obj2}} \\


\begin{enumerate}
\item
  If \objectvarf{obj1} is a \builtin{<boolean>} or \builtin{<symbol>}
  value return the
  result of Scheme expression \codeformulacmd{(eq? \objectvar{obj1}
    \objectvar{obj2})}
\item
  If \objectvarf{obj1} is an integer or a real number return the
  result of Scheme expression \codeformulacmd{(= \objectvar{obj1}
    \objectvar{obj2})}.
\item
  If \objectvarf{obj1} = \code{null} return \true.
\item
  If \objectvarf{obj1} is a \builtin{<character>},
  \builtin{<input-port>}, or \builtin{<output-port>}
  value return the
  result of Scheme expression \codeformulacmd{(eqv? \objectvar{obj1}
    \objectvar{obj2})}
\item
  If \objectvarf{obj1} is a \builtin{<string>} value return the result
  of Scheme expression \codeformulacmd{(string=? \objectvar{obj1}
    \objectvar{obj2})}
\end{enumerate}


\subsection{\algorithmname{EqualPrimitiveObjects?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{obj1}: a primitive value \\
\mbox{ } \hspace{1cm} \objectvarf{obj2}: a primitive value \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{equal?} : \code{<boolean>} \\


\noindent
\textit{Preconditions:} \\
The classes of \objectvarf{obj1} and \objectvarf{obj2} have to be
equal and they have to be a primitive class. \\


\noindent
\textit{Algorithm:}
\algorithmappl{EqualPrimitiveObjects?}{\objectvarf{obj1}, \objectvarf{obj2}} \\


\begin{enumerate}
\item
  If \objectvarf{obj1} is a \builtin{<boolean>} or \builtin{<symbol>}
  value return the
  result of Scheme expression \codeformulacmd{(eq? \objectvar{obj1}
    \objectvar{obj2})}
\item
  If \objectvarf{obj1} is an integer or a real number return the
  result of Scheme expression \codeformulacmd{(= \objectvar{obj1}
    \objectvar{obj2})}.
\item
  If \objectvarf{obj1} = \code{null} return \true.
\item
  If \objectvarf{obj1} is a \builtin{<character>}, \builtin{<string>},
  \builtin{<input-port>}, or \builtin{<output-port>}
  value return the
  result of Scheme expression \codeformulacmd{(eqv? \objectvar{obj1}
    \objectvar{obj2})}
\end{enumerate}


\subsection{\algorithmname{EqualVectors?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{v1}: a general vector \\
\mbox{ } \hspace{1cm} \objectvarf{v2}: a general vector \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \true\ if the lengths of \objectvarf{v1} and
\objectvarf{v2} are equal and all the elements of \objectvarf{v1} and
\objectvarf{v2} are equal in the sense of \algorithmname{EqualValues?},
\false\ otherwise \\


%% \noindent
%% \textit{Algorithm:}
%% \algorithmappl{EqualVectors?}{\objectvarf{v1}, \objectvarf{v2},
%%   \objectvarf{v}} \\


%% If the lengths of \objectvarf{v1} and \objectvarf{v1} are the same
%% compare the elements of \objectvarf{v1} and \objectvarf{v1} with the
%% algorithm \algorithmname{EqualValues?}. If all the comparisons return
%% \true\ then return \true. Otherwise return \false.


\subsection{\algorithmname{EqualVectorContents?}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{v1}: a general vector \\
\mbox{ } \hspace{1cm} \objectvarf{v2}: a general vector \\
\mbox{ } \hspace{1cm} \objectvarf{v}: the set (list) of object pairs already
visited \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \true\ if the lengths of \objectvarf{v1} and
\objectvarf{v2} are equal and all the elements of \objectvarf{v1} and
\objectvarf{v2} are equal in the sense of \algorithmname{EqualContents?},
\false\ otherwise \\


%% \noindent
%% \textit{Algorithm:}
%% \algorithmappl{EqualVectorContents?}{\objectvarf{v1}, \objectvarf{v2},
%%   \objectvarf{v}} \\


%% If the lengths of \objectvarf{v1} and \objectvarf{v1} are the same
%% compare the elements of \objectvarf{v1} and \objectvarf{v1} with the
%% algorithm \algorithmname{EqualContents?}. If all the comparisons return
%% \true\ then return \true. Otherwise return \false.


\chapter{Procedures}
\label{ch:procedures}


\section{General}


\index{procedure}
\index{simple procedure}
\index{generic procedure}
\index{parametrized procedure}
\index{pure procedure}
\index{nonpure procedure}
\theme\ has three kinds of procedures: \defterm{simple procedures},
\defterm{generic procedures}, and \defterm{parametrized procedures}.
A procedure is applied with syntax
\begin{syntaxblock}
  \slp \objectvar{proc} \indexedprocarg{arg}{1}
  \sldots \indexedprocarg{arg}{n} \srp
\end{syntaxblock}
\noindent
where \syntaxformulacmd{\objectvar{proc}} is the procedure and
\syntaxformulacmd{\indexedprocarg{arg}{1}}, ...,
\syntaxformulacmd{\indexedprocarg{arg}{n}} are the arguments passed
to \syntaxformulacmd{\objectvar{proc}}.
It is possible for a procedure to have no arguments. As regards the
simple procedures \syntaxformulacmd{\objectvar{proc}} can be any
expression that returns
a simple procedure. See section \ref{subsec:expr-lambda} in this manual
and \code{define-simple-method} and 
\code{define-param-proc} in chapter 3 of the standard library reference
for procedure definition syntax.
A procedure is either \textit{pure} or \textit{nonpure}. A pure
procedure can't have any side effects (or
should not have in case of \code{force-pure} and
\code{force-pure-expr}). However, a pure procedure is allowed to raise
exceptions. If a procedure is defined neither \code{pure} nor
\code{nonpure} it is nonpure by default.
If a procedure defines a \defterm{rest argument} an arbitrary number
of instances of the rest argument type can be passed to the procedure
at the end of the argument list.

Procedures should only be applied in procedure bodies. In
particular, a defining expression of a toplevel definition must not be
a procedure application. As an exception to this rule applications of
the procedure \code{list} generated by quasiquotation are legal toplevel,
too.

\index{pure expression}
\index{nonpure expression}
Every expression in \theme\ is either \defterm{pure} or
\defterm{nonpure}. A pure expression cannot have any side effects (or
should not have in case of \code{force-pure} and
\code{force-pure-expr}).
An application of a pure procedure is a pure expression.
Other procedures are nonpure. A
procedure may also be declared \code{force-pure}, in which case
the procedure may contain nonpure expressions but the
\theme\ compiler and linker handle the procedure as pure.
Note that the purity of a procedure is not necessarily the same as the
purity of a procedure application calling the procedure. If the
procedure is pure and some of the subexpressions of the procedure
application is nonpure the procedure application expression is
nonpure.
The implementation of a pure procedure may change the internal
variables of the procedure. More formally, it is legal to change
mutable variables defined inside the nearest lexically enclosing pure
procedure implementation of the expression changing the variable.
A typical use of this feature is a pure procedure having loop
variables.

\index{never returning procedure}
\index{always returning procedure}
A procedure may also be declared to \defterm{never returning} or
\defterm{always returning}. An example of a procedure returning never
is \code{raise},
which raises an exception. A procedure returning always may not raise
any exceptions and it has to handle any exceptions being generated in
it. When a procedure is neither always returning or never returning we
say that it \defterm{may return}.

A lambda expression or a parametrized lambda expression may be
optionally assigned a name. This name is used for debugging purposes
(runtime backtraces) only. If you define a variable having a 
lambda expression (or a parametrized lambda expression)
value with a define or let expression the lambda expression is
assigned the name of the variable automatically and you do not have to
specify it in the lambda expression.


\section{Simple Procedures}


\index{simple procedure}
Every simple procedure is an instance of some
\defterm{simple procedure class}. A simple procedure class is an instance of
the parametrized class \code{:simple-proc}. See subsection
\ref{subsec:proc-class-creation} for the
syntax for defining procedure classes. When you define a procedure the
\theme\ compiler and/or linker deduce the procedure class from the
procedure argument list, result type, and purity specifier
automatically, though.


\section{Generic Procedures}


\index{generic procedure}
A generic procedure is a collection of simple or parametrized
procedures, which are
called methods. When a generic procedure is called and an argument
list is passed to it \theme\ first checks which of the methods of the
generic procedure can be called with the argument list, i.e. the type
af the argument list is a subtype of the method argument list
type. \theme\ then finds out which of the suitable methods is the best
match. If a unique best match is not found an exception is
raised. These checks occur generally run-time.
The dispatch of a generic procedure application must succeed
statically even though the static dispatch is allowed to be ambiguous.
No two methods of the same generic procedure may have identical
argument list types.

Suppose that a generic procedure has two distinct
methods having argument list types \objectvarf{A} and \objectvarf{B} and
result types \objectvarf{R} and \objectvarf{S}, respectively. If \objectvarf{A}
is a subtype of \objectvarf{B} then \objectvarf{R} has to be a subtype of
\objectvarf{S}. This is called the \defterm{covariant typing rule}. The
covariant typing rule allows \theme\ to deduce a supertype of the
result type of a generic procedure application at compile time.

\index{dynamic method}
\index{static method}
Methods can be either \defterm{dynamic} or \defterm{static}.
A dynamic method is dispatched using the runtime types of the
arguments. However, the translator may optimize a dynamic dispatch
with a compile-time dispatch if it can be proved that the dynamic
dispatch yields a certain procedure.
If a method is static it is dispatched
statically, i.e. when the method is selected in compile-time dispatch
that method is used instead of doing runtime dispatch.

When a \theme\ program is linked all the generic procedure with same
name will be merged and the methods defined for each of the merged generic
procedures will be put into the new generic procedure.


\section{Parametrized Procedures}


\index{parametrized procedure}
A parametrized procedure is a procedure having type parameters. When
these type parameters are assigned type values we get a simple
procedure. Note that a parametrized procedure is not a simple
procedure itself. The values of the type parameters are usually not
specified explicitly. \theme\ deduces the values from the argument
types of a parametrized procedure application. This is done in
translation time.
If \theme\ is unable to deduce the
type parameter values a translation error is signalled.


\section{Abstract Procedure Types}


\index{abstract procedure type}
Abstract procedure types are instances of metaclass
\builtin{:procedure}. An object whose static type is a abstract
procedure type may be any kind of procedure, i.e. simple, generic, or
parametrized procedure, with a proper class.


\section{Subtyping of Procedure Types}


A simple or abstract procedure class \objectvarf{A} is a subtype of
a simple or abstract procedure class
\objectvarf{B} if and only if
\begin{itemize}
\item
  One of the following is true:
  \begin{itemize}
    \item Objects \objectvarf{A} and \objectvarf{B} are abstract procedure types.
    \item Objects \objectvarf{A} and \objectvarf{B} are simple procedure classes.
    \item Object \objectvarf{A} is a simple procedure class and \objectvarf{B} an
      abstract procedure type.
  \end{itemize}
\item The argument list type of \objectvarf{B} is a subtype of the
  argument list type of \objectvarf{A} (note the order).
\item The result type of \objectvarf{A} is a subtype of the result type
  of \objectvarf{B}.
\item Either \objectvarf{A} and \objectvarf{B} are both pure, both nonpure,
  or \objectvarf{A} is pure and \objectvarf{B} nonpure.
\item Either \objectvarf{B} may return or the returning attributes of
  \objectvarf{A} and \objectvarf{B} are equal.
\end{itemize}

See subsections
\ref{subsec:IsSubtypeProc},
\ref{subsec:IsSubtypeParamAbstract},
\ref{subsec:IsSubtypeParamProc},
\ref{subsec:IsSubtypeGenAbstract}, and
\ref{subsec:IsSubtypeGenericProc} for further information on procedure
type subtyping.


\section{Argument Type Modifiers and Static Type Expressions}
\label{subsec:static-type-expressions}


\index{argument type modifier}


The type-valued expressions in \theme\ may contain several
\defterm{argument type modifiers}.
These modifiers are
\begin{itemize}
  \item[\keyword{splice}] Adds the arguments of \keyword{splice}
    into the enclosing type list definition.
  \item[\keyword{rest}] Specifies the component type for the variable
    argument list part of the procedure that is being defined.
  \item[\keyword{type-loop}] Assigns the loop variable with the
    expressions from the list given and binds the loop expression
    with each value.
  \item[\keyword{join-tuple-types}] Concatenates the elements of all
    component types.
\end{itemize}


Expression
\begin{codeblock}
  (\ckeyword{\tupletype} a1 a2 ... (\ckeyword{splice}
  (\ckeyword{\tupletype} b1 b2 ...))
  c1 c2 ...)
\end{codeblock}
is equivalent to
\begin{codeblock}
  (\ckeyword{\tupletype} a1 a2 ... b1 b2
  ... c1 c2 ...)
\end{codeblock}
Expression type \keyword{splice} is mainly intended to be used with
\keyword{type-loop} expressions.

Expression
\begin{codeblock}
  (\ckeyword{type-loop} \typeparam{itervar} values expression)
\end{codeblock}
will iterate type variable \code{\typeparam{itervar}} in the type list
\code{values}. Type variable \code{\typeparam{itervar}} is bound to a value from
\code{values} and expression \code{expression} is evaluated with this
binding at each iteration. The result value of the \keyword{type-loop}
expression is a type list containing the evaluated expressions.
A type variable whose value is a type list shall be accepted as the
argument list type for \builtin{:procedure}.


Example:
\begin{codeblock}
  (\ckeyword{define-param-proc} map (\typeparam{arglist} \typeparam{return-type}) \\
  \mbox{ } \hspace{0.5cm} (\ckeyword{prim-proc} map \\
  \mbox{ } \hspace{1cm} ((\cbuiltin{:procedure} %
  ((\ckeyword{splice} \typeparam{arglist})) \typeparam{return-type} pure) \\
  \mbox{ } \hspace{1cm} (\ckeyword{splice} (\ckeyword{type-loop} %
  \typeparam{iter} \typeparam{arglist} (\cbuiltin{:uniform-list} %
  \typeparam{iter})))) \\
  \mbox{ } \hspace{1cm} (\cbuiltin{:uniform-list} \typeparam{return-type}) \\
  \mbox{ } \hspace{1cm} pure))
\end{codeblock}


When \objectvarf{P} is a procedure with declared argument types
\begin{syntaxformula}
  \indexedsyntaxvar{a}{1}, \sldots,
  \indexedsyntaxvar{a}{n}
\end{syntaxformula}
the \defterm{argument type list descriptor} of
\objectvarf{P} is defined to be
\begin{syntaxformula}
  \slp \indexedsyntaxvar{a}{1} \sldots
  \indexedsyntaxvar{a}{n} \srp
\end{syntaxformula}.


\index{static type expression}
A \defterm{static type expression} is defined as follows:
\begin{itemize}
\item Every constant whose value is a static type expression is a
  static type expression.
\item Every constant whose value is a type is a static type
  expression.
\item Every instantiation of a parametrized type is a static type
  expression. The type parameters have to be static type expressions.
\item A \builtin{\tupletype} expression is a static type
  expression. The type parameters have to be static type expressions.
\item Every valid application of an argument type modifier is a static
  type expression.
\item Every type variable is a static type expression.
\end{itemize}
%%The value of a static type expression is always an instance of
%%\builtin{<type>}.


\section{Algorithm to Deduce the Values of Argument Variables}


\subsection{\algorithmname{TranslateArguments}}


This algorithm deduces the values of procedure argument variables from
the arguments in procedure application.
When \(l\) is a list we define \(N(l)\) to be the length of the list
\(l\).


\vspace{0.5cm}

\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{a}{1} %
... \indexedobjectvarf{a}{n} : argument descriptors \\
\mbox{ } \hspace{1cm} \indexedobjectvarf{v}{1} %
... \indexedobjectvarf{v}{m} : argument values in the procedure
application \\

%% \vspace{0.25cm}

\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \indexedobjectvarf{w}{1} %
... \indexedobjectvarf{w}{n} : values assigned to each argument %
variable \\

\noindent
\textit{Algorithm:} \algorithmappl{TranslateArguments}{\indexedobjectvarf{a}{1}, %
..., \indexedobjectvarf{a}{n}, \indexedobjectvarf{v}{1}, %
..., \indexedobjectvarf{v}{m}}\\


\noindent
\begin{enumerate}
  \item If \(n = 0 \land m \neq 0\)
  \item then raise error
  \item else
    \begin{enumerate}
      \item
        \begin{objectformula}
          \setvar{\objectvar{c}}{\alglistlparen \indexedobjectvar{v}{1} %
            \sldots \indexedobjectvar{v}{m} \alglistrparen}
        \end{objectformula}
      \item
        \begin{objectformula}
          \setvar{\objectvar{r}}{\alglistlparen \alglistrparen}
        \end{objectformula}
      \item For \(i = 1, \ldots, n\)
        \begin{enumerate}
          \item
            \begin{objectformula}
              \setvar{\objectvar{t}}{\algorithmappl{TranslateArgument}%
                {\indexedobjectvar{a}{i},\objectvar{c}}}
            \end{objectformula}
          \item
            \begin{objectformula}
              \setvar{\objectvar{r}}%
              {Concatenation of \objectvar{r} and \objectvar{t}[1]}
%%              {\alglistlparen \indexedobjectvar{r}{1}
%%                ... \indexedobjectvar{r}{N(r)} \objectvar{t}[1]
%%                \alglistrparen}
            \end{objectformula}
          \item
            \begin{objectformula}
              \setvar{\objectvar{c}}{\objectvar{t}[2]}
            \end{objectformula}
        \end{enumerate}
      \item Return \objectvarf{r}.
    \end{enumerate}
\end{enumerate}


\subsection{\algorithmname{TranslateArgument}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{a} : The argument descriptor being
handled \\
\mbox{ } \hspace{1cm} \objectvarf{c} : The application arguments left \\


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} A pair whose first element is the value/list of
values to be assigned to the argument \objectvarf{a} and the second element
a list of the application arguments left after handling the current
argument translation \\


\noindent
\textit{Algorithm:} \algorithmappl{TranslateArgument}{\objectvarf{a}, \objectvarf{c}}\\


\noindent
If any of the following is true:
\begin{itemize}
  \item
    \objectvarf{a} is a type
  \item
    \objectvarf{a} is a list of static type expressions
  \item
    \objectvarf{a} is a type join expression
\end{itemize}
return
\begin{objectformula}
  \alglistlparen \alglistlparen \indexedobjectvar{c}{1} \alglistrparen
  \alglistlparen \indexedobjectvar{c}{2} \sldots \indexedobjectvar{c}{N(c)}
  \alglistrparen \alglistrparen
\end{objectformula}.
If \(N(c) = 0\) in the case above raise error.


\noindent
If
\begin{objectformula}
  \objectvar{a} = \algapplylparen \skeyword{rest} \objectvar{r}
  \algapplyrparen
\end{objectformula}
return
\begin{objectformula}
  \alglistlparen \alglistlparen \objectvar{c} \alglistrparen
  \alglistlparen \alglistrparen \alglistrparen
\end{objectformula}.


\noindent
Suppose that
\begin{objectformula}
  \objectvar{a} = \algapplylparen \skeyword{splice} \objectvar{s}
  \algapplyrparen
\end{objectformula}.
Now \objectvarf{s} has to be a list of static type expressions. Define
\(l := N(s)\). If \(N(c) \geq l\) return
\begin{objectformula}
  \alglistlparen \alglistlparen \alglistlparen
  \indexedobjectvar{c}{1} \sldots \indexedobjectvar{c}{l}
  \alglistrparen \alglistrparen
  \alglistlparen
  \indexedobjectvar{c}{l+1} \sldots \indexedobjectvar{c}{N(c)}  
  \alglistrparen \alglistrparen
\end{objectformula}.


\section{Algorithm to Dispatch Generic Procedure Applications}


\subsection{\algorithmname{SelectBestMatch}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm}
\begin{objectformula}
  \objectvar{l} = \alglistlparen \indexedobjectvar{t}{1} \sldots %
  \indexedobjectvar{t}{n} %
  \alglistrparen
\end{objectformula} 
: Call arguments \\
\mbox{ } \hspace{1cm}
\begin{objectformula}
  \indexedobjectvar{s}{j} =
  \alglistlparen
  \indexedobjectvar{s}{j,1} \sldots
  \indexedobjectvar{s}{j,p(j)}
  \indexedobjectvar{r}{j}
  \alglistrparen
\end{objectformula}
: Declared method argument lists, \(j = 1, \ldots, m\) \\


\noindent
\textit{\algresult} \\
\noindent
\objectvarf{result}: Either found, ambiguous, or not found. \\
\objectvarf{methods}: The dispatched methods found. \\
  

\noindent
\textit{Algorithm:} \algorithmappl{SelectBestMatch}{\objectvarf{l},
  \indexedobjectvarf{s}{1}, ... \indexedobjectvarf{s}{m}}\\


\begin{enumerate}
\item
  Deduce the type parameters for all the parametrized methods.
  Reject all the methods for which all type parameters
  could not be deduced.
\item Set \objectvarf{v} to a vector of \(m\) elements, each of which
  equal to \code{\#t}.
\item Set
  \begin{objectformula}
    \setvar{\objectvarf{v}[\objectvar{i}]}{\code{\#f}}
  \end{objectformula}
  iff
  \begin{objectformula}
    \(\lnot\) \inherits{\objectvar{l}}{\indexedobjectvar{s}{i}}
  \end{objectformula}.
\item
  For each \(i = 1, \ldots, n\)
  \begin{enumerate}
  \item
    Define
    \begin{displaymath}
      a(j,i) :=
      \left\{
        \begin{array}{ll}
          s_{j,i} ; & i \leq p(j) \\
          r_j ; & i > p(j)
        \end{array}
      \right.
    \end{displaymath}
    and set \(w\) a vector of \(m\) elements with value \code{\#f}.
  \item
    For each \(j = 1, \ldots, m\):
    if \(v[j] = \#t\) and \(a(j, i) \minhsymb t_i\) set
    \(w[j] := \#t\).
  \item
    If \(w[j_0] = \#t\) for some \(j_0 \in \{ 1, \ldots, m \}\)
    then set \(v := w\) else do
    \algorithmappl{SelectNearestMethod}{\objectvarf{i}, \objectvarf{v}, \objectvarf{l}, %
      \indexedobjectvarf{s}{1}, ..., \indexedobjectvarf{s}{m}}
  \item
    If there is one or zero \(j\) for which \(v[j] = \#t\) break the
    loop.
  \end{enumerate}
\item
  If \(v[j] = \code{\#t}\) for exactly one
  \(j \in \{ 1, \ldots, m \}\) then the result of the algorithm is
  ``found'' and the result method is method number \(j\).
  If \(v[j] = \code{\#t}\) for more than one
  \(j \in \{ 1, \ldots, m \}\) the result of the algorithm is
  ``ambiguous'' and the result methods are all the methods for which
  \(v[j] = \code{\#t}\).
  If \(v[j] = \code{\#f}\) for all
  \(j \in \{ 1, \ldots, m \}\) the result of the algorithm in
  ``not found''.
\end{enumerate}


\subsection{\algorithmname{SelectNearestMethods}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{i} : Index to the argument list \\
\mbox{ } \hspace{1cm} \objectvarf{v} : Boolean values marking the methods
included in computation \\
\mbox{ } \hspace{1cm}
\begin{objectformula}
  \objectvar{l} = \alglistlparen \indexedobjectvar{t}{1} \sldots %
  \indexedobjectvar{t}{n} %
  \alglistrparen
\end{objectformula} 
: Same as in \algorithmname{SelectBestMatch} \\
\mbox{ } \hspace{1cm}
\begin{objectformula}
  \indexedobjectvar{s}{j} =
  \alglistlparen
  \indexedobjectvar{s}{j,1} \sldots
  \indexedobjectvar{s}{j,p(j)}
  \indexedobjectvar{r}{j}
  \alglistrparen
\end{objectformula}
: Same as in \algorithmname{SelectBestMatch} \\


\noindent
\textit{\algresult} \\
\noindent
\objectvarf{v} : Boolean values marking the methods included in
computation \\


\noindent
\textit{Algorithm:} \algorithmappl{SelectNearestMethods}{\objectvarf{i}, %
  \objectvarf{v}, \objectvarf{l}, %
  \indexedobjectvarf{s}{1}, ..., \indexedobjectvarf{s}{m}} \\


Define \(a(j, i)\) as in algorithm \algorithmname{SelectBestMethod}.


\vspace{0.5cm}


\noindent
For each \(j = 1, \ldots, m\) \\
\mbox{ } \hspace{0.5cm} For each \(k = 1, \ldots, m\) \\
\mbox{ } \hspace{1cm} If
\(j \neq k\), \(v[j] \land v[k]\),
\(a(j, i) \minhsymb a(k, i)\), and
\(\lnot a(k, i) \minhsymb a(j, i)\)
set \(v[k] := \#f\).


\section{Algorithm to Dispatch Parametrized Procedure Applications}
\label{sec:param-proc-appl}


This algorithm computes only suggestions for the type parameter values
of a given parametrized procedure. We will bound the type variables in
the type of the parametrized procedure with the suggestions. Then we
will check that the type of the application argument list is a subtype
of the bound parametrized procedure type.

When we compile an implementation of a parametrized procedure we fix
the type parameters of the procedure so that their values are not
deduced and the other type variables may be represented in terms of
them.


\subsection{\algorithmname{DeduceArgumentTypes}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{target}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\


\noindent
\textit{\algresult} \\
\noindent
\objectvarf{all-found?} : \true\ iff values were found for all the
nonfixed type variables in \objectvarf{src} and \objectvarf{target} \\


\noindent
\textit{Algorithm:} \algorithmappl{DeduceArgumentTypes}{\objectvarf{src},
  \objectvarf{target}, \objectvarf{T}, \objectvarf{F}} \\


\begin{enumerate}
\item
  Set
  \begin{objectformula}
    \setvar{\objectvar{state}}{2}
  \end{objectformula},
  \begin{objectformula}
    \setvar{\objectvar{old-count-source}}{0}
  \end{objectformula},
  \begin{objectformula}
    \setvar{\objectvar{old-count-target}}{0}
  \end{objectformula},
  \begin{objectformula}
    \setvar{\objectvar{cur-src}}{\objectvar{src}}
  \end{objectformula},
  \begin{objectformula}
    \setvar{\objectvar{cur-target}}{\objectvar{target}}
  \end{objectformula}, and
  \begin{objectformula}
    \setvar{\objectvar{dir-forward?}}{\true}
  \end{objectformula}.
\item
  Until
  \begin{math}
    \mobjectvar{state} \leq 0
  \end{math}
  do
  \begin{enumerate}
  \item
    If \objectvarf{dir-forward?}
    \begin{enumerate}
    \item
      If
      \begin{math}
        \mobjectvar{state} > 0
      \end{math}
      \begin{enumerate}
        \item
          Apply algorithm
          \algorithmappl{DeduceStepForward}
          {\objectvarf{cur-src},
          \objectvarf{cur-target},
          \objectvarf{T},
          \objectvarf{F},
          \objectvarf{old-count-target},
          \objectvarf{state}}
          and store the result into \objectvarf{res}.
        \item
          Set
          \begin{objectformula}
            \setvar{\objectvar{state}}{\objectvar{res}[1]}
          \end{objectformula}
          and
          \begin{objectformula}
            \setvar{\objectvar{old-count-target}}{\objectvar{res}[2]}
          \end{objectformula}.
        \item
          Bind all the bindings of \objectvarf{T} in expression
          \objectvarf{target} and store the result into \objectvarf{cur-target}.
        \item
          Bind all the bindings of \objectvarf{T} in expression
          \objectvarf{src} and store the result into
          \objectvarf{cur-src}, else
      \end{enumerate}
    \end{enumerate}
    \begin{enumerate}
    \item
      If
      \begin{math}
        \mobjectvar{state} > 0
      \end{math}
      \begin{enumerate}
        \item
          Apply algorithm
          \algorithmappl{DeduceStepForward}
          {\objectvarf{cur-src},
          \objectvarf{cur-target},
          \objectvarf{T},
          \objectvarf{F},
          \objectvarf{old-count-src},
          \objectvarf{state}}
          and store the result into \objectvarf{res}.
        \item
          Set
          \begin{objectformula}
            \setvar{\objectvar{state}}{\objectvar{res}[1]}
          \end{objectformula}
          and
          \begin{objectformula}
            \setvar{\objectvar{old-count-src}}{\objectvar{res}[2]}
          \end{objectformula}.
        \item
          Bind all the bindings of \objectvarf{T} in expression
          \objectvarf{src} and store the result into
          \objectvarf{cur-src}.
        \item
          Bind all the bindings of \objectvarf{T} in expression
          \objectvarf{target} and store the result into \objectvarf{cur-target}.
      \end{enumerate}
    \end{enumerate}
  \item
    Set
    \begin{math}
      \mobjectvar{dir-forward?}
    \end{math}
    to
    \begin{math}
      \lnot \mobjectvar{dir-forward?}
    \end{math}.
  \end{enumerate}
  \item
    Return \true\ iff
    \begin{math}
      \mobjectvar{state} = -1
    \end{math}.
\end{enumerate}


\subsection{\algorithmname{DeduceStepForward}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{target}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{old-count}: The number of type
variables already deduced \\
\mbox{ } \hspace{1cm} \objectvarf{old-state}: The old state of the
algorithm \\


\noindent
\textit{\algresult} \\
\noindent
\objectvarf{new-state}: The new state of the algorithm \\
\noindent
\objectvarf{new-count}: The number of type variables deduced \\


\noindent
\textit{Algorithm:} \algorithmappl{DeduceStepForward}{\objectvarf{src},
  \objectvarf{target}, \objectvarf{T}, \objectvarf{F}, \objectvarf{old-count},
  \objectvarf{old-state}} \\


\begin{enumerate}
\item
  Apply algorithm
  \algorithmappl{DeduceTypeParams}{\objectvarf{src}, \objectvarf{target},
  \objectvarf{T}, \objectvarf{F}, ()}.
\item
  Set
  \begin{objectformula}
    \setvar{\objectvar{new-count}}{the number of bindings in \objectvar{T}}
  \end{objectformula}.
\item
  If all the type variables in \objectvarf{src} and
  \objectvarf{target} were deduced return
  \begin{objectformula}
    \olp -1 \objectvar{new-count} \orp
  \end{objectformula}.
\item
  If
  \begin{objectformula}
    \objectvar{old-count} = \objectvar{new-count}
  \end{objectformula}
  let
  \begin{objectformula}
    \setvar{\objectvar{s}}{\objectvar{old-state} - 1}
  \end{objectformula}
  and return
  \begin{objectformula}
    \olp \objectvar{s} \objectvar{new-count} \orp
  \end{objectformula}.
\item
  Otherwise return
  \begin{objectformula}
    \olp 2 \objectvar{new-count} \orp
  \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{DeduceStepBackward}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{target}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{old-count}: The number of type
variables already deduced \\
\mbox{ } \hspace{1cm} \objectvarf{old-state}: The old state of the
algorithm \\


\noindent
\textit{\algresult} \\
\noindent
\objectvarf{new-state}: The new state of the algorithm \\
\noindent
\objectvarf{new-count}: The number of type variables deduced \\


\noindent
\textit{Algorithm:} \algorithmappl{DeduceStepBackward}{\objectvarf{src},
  \objectvarf{target}, \objectvarf{T}, \objectvarf{F}, \objectvarf{old-count},
  \objectvarf{old-state}} \\


\begin{enumerate}
\item
  Apply algorithm
  \algorithmappl{DeduceTypeParams}{\objectvarf{target}, \objectvarf{src},
  \objectvarf{T}, \objectvarf{F}, ()}.
\item
  Set
  \begin{objectformula}
    \setvar{\objectvar{new-count}}{the number of bindings in \objectvar{T}}
  \end{objectformula}.
\item
  If all the type variables in \objectvarf{src} and
  \objectvarf{target} were deduced return
  \begin{objectformula}
    \olp -1 \objectvar{new-count} \orp
  \end{objectformula}.
\item
  If
  \begin{objectformula}
    \objectvar{old-count} = \objectvar{new-count}
  \end{objectformula}
  let
  \begin{objectformula}
    \setvar{\objectvar{s}}{\objectvar{old-state} - 1}
  \end{objectformula}
  and return
  \begin{objectformula}
    \olp \objectvar{s} \objectvar{new-count} \orp
  \end{objectformula}.
\item
  Otherwise return
  \begin{objectformula}
    \olp 2 \objectvar{new-count} \orp
  \end{objectformula}.
\end{enumerate}


\subsection{\algorithmname{DeduceTypeParams}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceTypeParams}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item
  If \objectvarf{src} is not a (general) pair and \objectvarf{dest} is
  a splice expression compute
  \begin{objectformula}
    \algorithmappl{DeduceTypeParams}{(\objectvar{src}),
      \objectvar{c}, \objectvar{T}, \objectvar{F}, \objectvar{v2}}
  \end{objectformula}
  where \objectvarf{c} is the component type of \objectvarf{dest}.
  If the condition above does not hold and \objectvarf{src} is not a pair
  return \false\ (this may be an error situation).
\item
  Let \objectvarf{x} be the head of list \objectvarf{src}.
  If pair (\objectvarf{x},\objectvarf{dest}) is contained in
  \objectvarf{v} return. Otherwise add (\objectvarf{x},\objectvarf{dest})
  into \objectvarf{v2}.
\item
  Set \objectvarf{src2} to be the value of
  \begin{objectformula}
    \algorithmappl{PrepareSourceType}{\objectvar{src}}.
  \end{objectformula}
\item
  If \objectvarf{src2} is not a pair or there are not any type
  variables in \objectvarf{dest} return.
\item
  If \objectvarf{dest} is a type variable compute
  \begin{objectformula}
    \algorithmappl{DeduceSimpleType}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{src} and \objectvarf{dest} are both signatures compute
  \begin{objectformula}
    \algorithmappl{DeduceSgnSgn}{\objectvar{src},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest} is a signature and \objectvarf{src} is not a
  signature compute
  \begin{objectformula}
    \algorithmappl{DeduceNotSgnSgn}{\objectvar{src},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest} is not a signature and \objectvarf{src} is a
  signature return.
\item
  If \objectvarf{dest} is a pair or a pair class compute
  \begin{objectformula}
    \algorithmappl{DeducePair}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest} is a rest expression compute
  \begin{objectformula}
    \algorithmappl{DeduceRest}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest} is a splice expression compute
  \begin{objectformula}
    \algorithmappl{DeduceSplice}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest} is a type loop expression compute
  \begin{objectformula}
    \algorithmappl{DeduceTypeLoop}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{src2}[1] is a union and \objectvarf{dest} is not a
  union compute
  \begin{objectformula}
    \algorithmappl{DeduceUnionX}{\objectvar{src2}[1],
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{src2}[1] is not a union and \objectvarf{dest} is a
  union compute
  \begin{objectformula}
    \algorithmappl{DeduceXUnion}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{src2}[1] is a union and \objectvarf{dest} is a union
  compute
  \begin{objectformula}
    \algorithmappl{DeduceUnionUnion}{\objectvar{src2}[1],
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{src2}[1] is a generic procedure class and
  \objectvarf{dest} is an abstract procedure type compute
  \begin{objectformula}
    \algorithmappl{DeduceGenAbst}{\objectvar{src2}[1],
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}.
  and return.
\item If \objectvarf{dest} and \objectvarf{src2} are parametrized type
  instances whose type parameters contain type modifiers return
  \true\ if the types and type parameters of these instances are
  equal. If only one of \objectvarf{dest} and \objectvarf{src2} is
  this kind of instance return \false. Note that type modifiers
  may be optimized away by the \theme\ compiler and linker in which
  case these conditions are not fulfilled.
\item
  Compute
  \begin{objectformula}
    \algorithmappl{DeduceSubexprs}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\end{enumerate}


\subsection{\algorithmname{PrepareSourceType}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a list of static type expressions \\


\noindent
\textit{\algresult} \\
\noindent
\objectvarf{u} : a modified list of static type expressions \\


\noindent
\textit{Algorithm:} \algorithmappl{PrepareSourceType}{\objectvarf{t}}


Let \(\mobjectvar{t} = (t_1, \ldots, t_n)\). Define
\begin{displaymath}
  r :=
  \left\{
  \begin{array}{ll}
    c ; & \textrm{if} \; t_1 \; \textrm{is a splice expression} \\
    t_1 ; & \textrm{otherwise}
  \end{array}
  \right.
\end{displaymath}
where \objectvarf{c} is the component type of \(t_1\).
Return \((r, t_2, \ldots, t_n)\).


\subsection{\algorithmname{DeduceSubexprs}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceSubexprs}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item
  Set
  \begin{objectformula}
    \setvar{\objectvar{comp}}{The component list of the head of
      \objectvar{src}}
  \end{objectformula}.
\item
  Set
  \begin{objectformula}
    \setvar{\objectvar{src-new}}{A list whose head is \objectvar{comp}
      and whose tail is the tail of \objectvar{src}}
  \end{objectformula}.
\item
  Compute \algorithmappl{DeduceTypeParams}{\objectvarf{src-new},
    \objectvarf{subexprs2}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}.
\end{enumerate}


\subsection{\algorithmname{DeduceSimpleType}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A variable reference to a type \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceSimpleType}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item 
  Let \objectvarf{var} be the variable which \objectvarf{dest} refers to.
\item
  If \objectvarf{src} is a list, \objectvarf{var} is a type variable, and
  \objectvarf{var} is not already contained in \objectvarf{T} then
  add the binding of \objectvarf{var} with the head of
  \objectvarf{src} into \objectvarf{T}.
\item
  If the formerly deduced type variables contain variable
  \objectvarf{var} substitute the new binding into them.
\item
  If the new deduced value contains formerly deduced type variables
  substitute them into the new value.
\end{enumerate}


\subsection{\algorithmname{DeducePair}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A pair or a pair class  \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeducePair}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item
  Let
  \begin{objectformula}
    \setvar{\objectvar{src2}}{the head of the pair \objectvar{src}}
  \end{objectformula}
\item
  Let
  \begin{objectformula}
    \setvar{\objectvar{u}}{the head of the pair \objectvar{dest}}
  \end{objectformula}
  and compute
  \algorithmappl{DeduceTypeParams}{\objectvarf{src2}, \objectvarf{u},
    \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}.
\item
  Let
  \begin{objectformula}
    \setvar{\objectvar{r}}{\olp \objectvar{r0} \orp}
  \end{objectformula}
  where
  \begin{objectformula}
    \setvar{\objectvar{r0}}{the tail of the pair \objectvar{src2}}
  \end{objectformula}
  and
  \begin{objectformula}
    \setvar{\objectvar{s}}{the tail of the pair \objectvar{dest}}
  \end{objectformula}
  and compute
  \algorithmappl{DeduceTypeParams}{\objectvarf{r}, \objectvarf{s},
    \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}.
\end{enumerate}


\subsection{\algorithmname{DeduceRest}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A rest expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceRest}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


Let \objectvarf{t} be the component type of the rest expression
\objectvarf{dest}. Compute
\begin{objectformula}
  \algorithmappl{DeduceTypeParams}{\objectvar{src}, \objectvar{t}, \objectvarf{T},
  \objectvarf{F}, \objectvarf{v}}
\end{objectformula}.


\subsection{\algorithmname{DeduceSplice}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A splice expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceSplice}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


Let \objectvarf{t} be the component type of the rest expression
\objectvarf{dest}.
Let \objectvarf{l} be the single element list containing \objectvarf{src}.
Compute
\begin{objectformula}
  \algorithmappl{DeduceTypeParams}{\objectvar{l}, \objectvar{t}, \objectvarf{T},
  \objectvarf{F}, \objectvarf{v}}
\end{objectformula}.


\subsection{\algorithmname{DeduceTypeLoop}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A type loop expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceTypeLoop}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


Let \objectvarf{iter-var} be the iteration variable of \objectvarf{dest},
\objectvarf{iter-expr} the iteration expression of \objectvarf{dest},
and \objectvarf{subtype-list} the subtype list of \objectvarf{dest}.


\begin{enumerate}
\item
  Let \objectvarf{source-list} be the first element of \objectvarf{src}.
  If \objectvarf{source-list} is not a list raise error else we have
  \begin{objectformula}
    \objectvar{source-list} =
    \olp
    \indexedobjectvar{t}{1}
    \oldots
    \indexedobjectvar{t}{n}
    \orp
  \end{objectformula}.
\item
  Let \objectvarf{guessed-items} to be the empty list.
\item
  If \objectvarf{source-list} is a type loop and
  \objectvarf{subtype-list} is a type variable then if
  \begin{itemize}
    \item \objectvarf{iter-expr} and the iteration expression of
      \objectvarf{source-list} are equal
    \item \objectvarf{T} does not contain a binding for
      \objectvarf{subtype-list}
    \item The subtype list of \objectvarf{source-list} does not
      contain free type variables other than those contained in
      \objectvarf{F}
  \end{itemize}
  bind type variable \objectvarf{subtype-list} with the subtype list
  of \objectvarf{source-list} in \objectvarf{T}.
\item
  Else if \objectvarf{source-list} is a uniform list type and
  \objectvarf{subtype-list} is a type variable then
  \begin{enumerate}
    \item
      Let \objectvarf{U} be a copy of \objectvarf{T} sharing the same contents.
    \item
      Let \objectvarf{new-src} be a list containing only the component
      type of \objectvarf{source-list}.
    \item
      Call
      \begin{objectformula}
        \algorithmappl{DeduceTypeParams}{\objectvar{new-src}, %
          \objectvar{iter-expr}, \objectvar{U}, \objectvarf{F}, \objectvar{v}}
      \end{objectformula}.
    \item
      If \objectvarf{U} contains a binding of type variable
      \objectvarf{iter-var} then bind \objectvarf{subtype-list}
      in \objectvarf{T}
      with a uniform list type having the binding of
      \objectvarf{iter-var} as the component type.
  \end{enumerate}
\item
  Else if \objectvarf{source-list} is not empty
  then
  \begin{enumerate}
  \item
    For \(i = 1, \ldots, n\)
    \begin{enumerate}
    \item
      Let \objectvarf{U} be a copy of \objectvarf{T} sharing the same contents.
    \item
      Call
      \begin{objectformula}
        \algorithmappl{DeduceTypeParams}{\indexedobjectvar{t}{i}, %
          \objectvar{iter-expr}, \objectvar{U}, \objectvarf{F}, \objectvar{v}}
      \end{objectformula}.
    \item
      If \objectvarf{U} contains a binding for type variable
      \objectvarf{iter-var} add the binding into the list
      \objectvarf{guessed-items}.
    \end{enumerate}
  \item
    If \objectvarf{guessed-items} contains \false\ return.
  \item
    If \objectvarf{subtype-list} is a type
    variable \objectvarf{u} and \objectvarf{u} is not already contained in
    \objectvarf{T} then
    \begin{enumerate}
    \item
      Let \objectvarf{b} be the list consisting of the tails of the
      pairs in \objectvarf{guessed-items} with the same order.
    \item
      Add a binding of \objectvarf{u} with \objectvarf{b} into
      \objectvarf{T}.
    \end{enumerate}
  \item
    Denote the elements of \objectvarf{guessed-items} with
    \indexedobjectvarf{g}{j}, \(j = 1, \ldots, m\).
    For \(j = 1, \ldots, m\)
    \begin{enumerate}
    \item
      Construct list \objectvarf{bindings} by appending the contents of
      \objectvarf{T} and \indexedobjectvarf{g}{j}.
    \item
      Create list \objectvarf{r} by applying \objectvarf{bindings} in
      expression \objectvarf{iter-expr}.
    \item
      Set
      \objectformulacmd{\setvar{\indexedobjectvar{h}{j}}{\objectvar{r}}}.
    \end{enumerate}
  \item
    Compute
    \begin{objectformula}
      \algorithmappl{DeduceTypeParams}{\objectvar{src}, \objectvar{h}, %
        \objectvar{T}, \objectvarf{F}, \objectvar{v}}
    \end{objectformula}.
  \end{enumerate}
\item
  If \objectvarf{subtype-list} is a variable reference to a type
  variable and \objectvarf{T} does not contain a binding of
  \objectvarf{subtype-list} add a binding of \objectvarf{subtype-list}
  with the empty list into \objectvarf{T}.
\end{enumerate}


\subsection{\algorithmname{DeduceUnionX}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A union expression \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceUnionX}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


Let \indexedobjectvarf{u}{1}, ..., \indexedobjectvarf{u}{n} be the member
types of union \objectvarf{src}. For \(i = 1, \ldots, n\) compute
\begin{objectformula}
  \algorithmappl{DeduceTypeParams}{\slp \indexedobjectvar{u}{i} \srp, %
    \objectvar{dest}, \objectvar{T}, \objectvarf{F}, \objectvar{v}}
\end{objectformula}.


\subsection{\algorithmname{DeduceXUnion}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A list of static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A union expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceXUnion}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


Let \indexedobjectvarf{u}{1}, ..., \indexedobjectvarf{u}{n} be the member
types of union \objectvarf{dest}. For \(i = 1, \ldots, n\) call
\begin{objectformula}
  \algorithmappl{DeduceTypeParams}{\objectvar{src}, \indexedobjectvar{u}{i}, %
    \objectvar{T}, \objectvarf{F}, \objectvar{v}}
\end{objectformula}.


\subsection{\algorithmname{DeduceUnionUnion}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A union expression \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A union expression \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceXUnion}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


Let \indexedobjectvarf{t}{1}, ..., \indexedobjectvarf{t}{m} be the member
types of union \objectvarf{src} and
\indexedobjectvarf{u}{1}, ..., \indexedobjectvarf{u}{n} the member
types of union \objectvarf{dest}.
Let \(p := \min \{ m, n \}\).
For \(i = 1, \ldots, p\)
compute
\begin{objectformula}
  \algorithmappl{DeduceTypeParams}{\slp \indexedobjectvar{t}{i} \srp, %
    \indexedobjectvar{u}{i}, %
    \objectvar{T}, \objectvarf{F}, \objectvar{v}}
\end{objectformula}.


\subsection{\algorithmname{DeduceGenAbst}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t1}: A generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{t2}: An abstract procedure type \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceGenAbst}{\objectvarf{t1},
  \objectvarf{t2}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item
  If \objectvarf{t1} contains type variables compute
  \algorithmappl{DeduceGenAbstArgList}{\objectvarf{t1},
    \objectvarf{t2}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}
\item
  If \objectvarf{t2} contains type variables compute
  \algorithmappl{DeduceGenAbstResult}{\objectvarf{t1},
    \objectvarf{t2}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}
\end{enumerate}


\subsection{\algorithmname{DeduceGenAbstResult}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t1}: A generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{a}: The target argument list type \\
\mbox{ } \hspace{1cm} \objectvarf{r}: The target result type \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceGenAbstResult}{\objectvarf{t1},
  \objectvarf{a}, \objectvarf{r}, \objectvarf{T}, \objectvarf{F},
  \objectvarf{v}} \\


\begin{enumerate}
\item
  Let \objectvarf{m} be the method class list of \objectvarf{t1}.
  Compute \objectvarf{result} and \objectvarf{method}
  with algorithm
  \algorithmappl{SelectBestMatch}{\objectvarf{a}, \objectvarf{m}}.
\item
  If an unambiguous match was found let \objectvarf{b} be the result
  type of \objectvarf{m}
  and apply algorithm
  \algorithmappl{DeduceTypeParams}{(\objectvarf{b}), \objectvarf{r},
    \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}.
\end{enumerate}


\subsection{\algorithmname{DeduceGenAbstArgList}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t1}: A generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{a}: The target argument list type \\
\mbox{ } \hspace{1cm} \objectvarf{r}: The target result type \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceGenAbstArgList}{\objectvarf{t1},
  \objectvarf{a}, \objectvarf{r}, \objectvarf{T}, \objectvarf{F},
  \objectvarf{v}} \\


\begin{enumerate}
\item
  Let \objectvarf{m} be the method class list of \objectvarf{t1}.
  Compute \objectvarf{result} and \objectvarf{method}
  with algorithm
  \algorithmappl{SelectBestMatch}{\objectvarf{a}, \objectvarf{m}}.
\item
  If an unambiguous match was found let \objectvarf{c} be the argument
  list type of \objectvarf{m} and apply algorithm
  \algorithmappl{DeduceTypeParams}{(\objectvarf{c}), \objectvarf{a},
    \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}.
\end{enumerate}


\subsection{\algorithmname{DeduceNotSgnSgn}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A static type expressions \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A signature \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceNotSgnSgn}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


For each procedure specifier \objectvarf{s} in \objectvarf{dest}
define \objectvarf{p} be the type of the corresponding procedure and
define \objectvarf{q}
by substituting \code{this} by \objectvarf{src} in \objectvarf{s}.
Apply algorithm
\algorithmappl{DeduceTypeParams}{(\objectvarf{p}), \objectvarf{q},
  \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}.


\subsection{\algorithmname{DeduceSgnSgn}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A signature \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A signature \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceSgnSgn}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{F}, \objectvarf{v}} \\


\noindent
For each procedure specifier \objectvarf{p} in signature \objectvarf{src} \\
\noindent
\mbox{ } \hspace{0.5cm}
for each procedure specifier \objectvarf{q} in signature \objectvarf{dest} \\
\noindent
\mbox{ } \hspace{1cm}
If the names of \objectvarf{p} and \objectvarf{q} are equal apply
algorithm \\
\noindent
\mbox{ } \hspace{1cm}
\algorithmappl{DeduceTypeParams}{(\objectvarf{p}), \objectvarf{q},
  \objectvarf{T}, \objectvarf{F}, \objectvarf{v}}.


\chapter{Expressions}


\section{General}


Note that many of the forms and control structures in \theme\ are
defined by the standard library (module \code{core-forms}). See the
standard library reference for these.  If the type of a syntax
variable (printed in italic) is not defined it is assumed to be an
expression. If we make an union of a set of types and some of these
types is \builtin{<none>} the union is also \builtin{<none>}. Syntax
element ``\syntaxelement{identifier}'' means a legal
\theme\ identifier. Syntax element ``\syntaxelement{null}'' means an
empty list, denoted by either \code{null} or \code{()}.


\section{Macros}


\index{macro}
\index{hygienic macro system}
\index{lexical scoping}


\theme\ has a hygienic and lexically scoped macro system similar to
Scheme macros. The keywords
\builtin{define-syntax}, \builtin{let-syntax}, \builtin{letrec-syntax},
and \builtin{syntax-case} are defined for the
macro system. The macro system is partly implemented by the
\theme\ standard library.
Some of the \theme\ control structures are implemented by macros in
the standard library.
Macros cannot be declared. When you want to export macros you have to
put them into the interface file of a module.
See chapter \macrochapter\ in \theme\ Standard Library Reference and
Scheme standard documentation \cite{shinn2017} for more information.

The macro transformers must expand to a special macro transformer
language resembling Scheme. The value returned by a macro transformer
has to be a \theme\ expression.


\subsection{Forms in the Macro Transformer Language}

\index{\keyword{\$lambda}}
\index{\keyword{\$let}}
\index{\keyword{if-object}}
\index{\keyword{if}}
\index{\keyword{begin}}
\index{\keyword{set{"!}}}
\index{\keyword{quote}}
\index{\keyword{\$let*}}
\index{\keyword{\$letrec}}
\index{\keyword{\$letrec*}}
\index{\keyword{\$and}}
\index{\keyword{\$or}}

The following forms are built-in:
\begin{itemize}
\item \keyword{\$lambda}
\item \keyword{\$let}
\item \keyword{if-object}
\item \keyword{if}
\item \keyword{begin}
\item \keyword{set!}
\item \keyword{quote}
\end{itemize}
The following forms are implemented by the \theme\ standard library:
\begin{itemize}
\item \keyword{\$let*}
\item \keyword{\$letrec}
\item \keyword{\$letrec*}
\item \keyword{\$and}
\item \keyword{\$or}
\end{itemize}


The keywords starting with '\$' behave like the corresponding keywords
in Scheme. The other keywords behave
like the corresponding keywords in \theme.


\subsection{Procedures in the Macro Transformer Language}


\index{\code{\$cons}}
\index{\code{\$car}}
\index{\code{\$cdr}}
\index{\code{\$pair?}}
\index{\code{\$null?}}
\index{\code{\$list?}}
\index{\code{\$list}}
\index{\code{\$for-all}}
\index{\code{\$map}}
\index{\code{\$apply}}
\index{\code{\$equal?}}
\index{\code{\$=}}
\index{\code{\$>=}}
\index{\code{\$>}}
\index{\code{\$length}}
\index{\code{\$append}}
\index{\code{\$+}}
\index{\code{\$-}}
\index{\code{\$vector}}
\index{\code{\$vector->list}}
\index{\code{\$raise}}
\index{\code{\$dotted-length}}
\index{\code{\$dotted-last}}
\index{\code{\$dotted-butlast}}
\index{\code{\$identifier?}}
\index{\code{\$free-identifier=?}}
\index{\code{\$syntax-rename}}
\index{\code{\$invalid-form}}
\index{\code{\$map-while}}
\index{\code{\$syntax-violation}}
\index{\code{\$generate-temporaries}}
\index{\code{\$make-variable-transformer}}
\index{\code{\$undefined}}


These procedures work as the corresponding procedures without the
leading '\$' in Scheme, see \cite{shinn2017}:
\begin{itemize}
\item \code{\$cons}
\item \code{\$car}
\item \code{\$cdr}
\item \code{\$pair?}
\item \code{\$null?}
\item \code{\$list?}
\item \code{\$list}
\item \code{\$for-all}
\item \code{\$map}
\item \code{\$apply}
\item \code{\$equal?}
\item \code{\$=}
\item \code{\$>=}
\item \code{\$>}
\item \code{\$length}
\item \code{\$append}
\item \code{\$+}
\item \code{\$-}
\item \code{\$vector}
\item \code{\$vector->list}
\item \code{\$raise}
\end{itemize}


These procedures are defined by the SRFI-72 implementation (without
the leading '\$'):
\begin{itemize}
\item \code{\$dotted-length}
\item \code{\$dotted-last}
\item \code{\$dotted-butlast}
\item \code{\$identifier?}
\item \code{\$free-identifier=?}
\item \code{\$syntax-rename}
\item \code{\$invalid-form}
\item \code{\$map-while}
\item \code{\$syntax-violation}
\item \code{\$generate-temporaries}
\item \code{\$make-variable-transformer}
\item \code{\$undefined}
\end{itemize}


\section{Procedure Application}


\index{procedure application}


Syntax:
\begin{syntaxblock}
  \slp
  \objectvar{procedure} \indexedprocarg{arg}{1} \sldots
  \indexedprocarg{arg}{n}
  \srp
\end{syntaxblock}


The procedure \objectvarf{procedure} is called with arguments
\indexedprocargf{arg}{1}, ..., \indexedprocargf{arg}{n}. Note that it is legal to
have an expression returning a simple procedure as the procedure to be
called. It is an error if the type of any argument is \builtin{<none>}.
When a procedure is called it is always checked that the types of the
arguments are correct to that procedure. This check occurs either
translation time or run time.


\section{Instantiation of a Parametrized Type}
\label{subsec:param-type-instantiation}


\index{parametrized type instantiation}


Let \objectvarf{A} be a parametrized class or a parametrized logical
type. Let \indexedsyntaxvarf{a}{1}, ..., \indexedsyntaxvarf{a}{n} be
type expressions and \indexedobjectvarf{t}{1}, ...,
\indexedobjectvarf{t}{m} be the translated argument list generated by
them. Then the value of expression
\begin{syntaxformula}
  \slp
  \objectvar{A}
  \indexedsyntaxvar{a}{1} \sldots \indexedsyntaxvar{a}{n}
  \srp
\end{syntaxformula}
is an instance of parametrized type \objectvarf{A} with type parameter
values
\indexedobjectvarf{t}{1}, ..., \indexedobjectvarf{t}{m}.
Two distinct instantiations of a parametrized class with same type
parameter values shall refer to the same class.


\section{Instantiation of Procedure Classes}
\label{subsec:proc-class-creation}


\index{procedure class instantiation}


Abstract and simple procedure classes are instantiated with the
following syntax:
\begin{syntaxblock}
\slp \syntaxvar{proc-metaclass} \syntaxvar{argument-list}
\syntaxvar{result-type} \syntaxvar{attribute-list}
\srp \\
\\
\syntaxvar{proc-metaclass} \syntaxdefequal \builtin{:procedure}
\syntaxor \builtin{:simple-proc} \\
\syntaxvar{argument-list} \syntaxdefequal
\slp \syntaxopt{\indexedsyntaxvar{arg}{1} \sldots
  \indexedsyntaxvar{arg}{n}} \srp \\
\attrlistdefb
\end{syntaxblock}


This syntax creates an abstract or simple procedure class. Expressions
\indexedsyntaxvarf{arg}{1}, ..., \indexedsyntaxvarf{arg}{n}
define the argument types. These expressions
have to be static type expressions.


Parametrized procedure classes are instantiated with the following
syntax:
\begin{syntaxblock}
\slp \builtin{:param-proc}
\syntaxvar{type-param-list}
\syntaxvar{argument-list}
\syntaxvar{result-type} \syntaxvar{attribute-list}
\srp \\
\\
\syntaxvar{type-param-list} \syntaxdefequal \slp
  \syntaxopt{\indexedsyntaxvar{tparam}{1} \sldots
  \indexedsyntaxvar{tparam}{m}} \srp \\
\indexedsyntaxvar{tparam}{k} \syntaxdefequal
\syntaxelement{identifier} \\
\syntaxvar{argument-list} \syntaxdefequal
\slp \syntaxopt{\indexedsyntaxvar{arg}{1} \sldots
  \indexedsyntaxvar{arg}{n}} \srp \\
\attrlistdefb
\end{syntaxblock}


Generic procedure classes cannot be instantiated explicitly for the
moment.


\section{Quotation}


\index{\builtin{quote}}
\index{\builtin{quasiquote}}


Quotation and quasiquotation work as in Scheme. Expression
\begin{codeformula}
  (\cbuiltin{quote} expr)
\end{codeformula}
can be written
\begin{codeformula}
  'expr
\end{codeformula}.
Expression
\begin{codeformula}
  (\cbuiltin{quasiquote} expr)
\end{codeformula}
can be written
\begin{codeformula}
  `expr
\end{codeformula}.


\section{Implicit Declaration of Recursive Definitions}


\index{recursion}


Keywords \keyword{define-simple-proc}, \keyword{define-param-proc},
\keyword{define-simple-method}, \keyword{define-param-method},
\keyword{define-class}, \keyword{define-param-class}, and
\keyword{define-param-logical-type} declare the variables they define
implicitly so that you do not have to declare them explicitly for
recursion. However, mutually recursive definitions require
declarations. Keywords \keyword{define-simple-proc},
\keyword{define-param-proc},
\keyword{define-simple-method}, and \keyword{define-param-method}
are defined in the core library.


\section{Module Forms}


\subsection{\keyword{define-proper-program}}
\index{\keyword{define-proper-program}}
\label{subsec:define-proper-program}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-proper-program} \syntaxvar{program-name} \\
  \syntaxopt{\syntaxvar{module-expression}} \sldots \\
  \syntaxopt{\syntaxelement{expression}} \sldots \srp \\
  \\
  \syntaxvar{program-name} \syntaxdefequal \syntaxelement{module-name} \\
  \syntaxvar{module-expression} \syntaxdefequal
  \slp \syntaxelement{module-keyword} \syntaxelement{module-name}
  \sldots
  \srp \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp \syntaxelement{identifier}
  \sldots \srp \\
  \syntaxelement{module-keyword} \syntaxdefequal
  \skeyword{import} \syntaxor
  \skeyword{use} \syntaxor \skeyword{prelink-body}
\end{syntaxblock}


A proper program with name \syntaxvarf{program-name} is defined.
See chapter \ref{ch:programs-and-modules}.


\subsection{\keyword{define-script}}
\index{\keyword{define-script}}
\label{subsec:define-script}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-script} \syntaxvar{program-name} \\
  \syntaxopt{\syntaxvar{module-expression}} \sldots \\
  \syntaxopt{\syntaxelement{expression}} \sldots \srp \\
  \\
  \syntaxvar{program-name} \syntaxdefequal \syntaxelement{module-name} \\
  \syntaxvar{module-expression} \syntaxdefequal
  \slp \syntaxelement{module-keyword} \syntaxelement{module-name}
  \sldots
  \srp \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp \syntaxelement{identifier}
  \sldots \srp \\
  \syntaxelement{module-keyword} \syntaxdefequal
  \skeyword{import} \syntaxor
  \skeyword{use} \syntaxor \skeyword{prelink-body}
\end{syntaxblock}


A script with name \syntaxvarf{program-name} is defined.
See chapter \ref{ch:programs-and-modules}.


\subsection{\keyword{define-interface}}
\index{\keyword{define-interface}}
\label{subsec:define-interface}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-interface} \syntaxvar{mod-name} \\
  \syntaxopt{\syntaxvar{module-expression}} \sldots\\
  \syntaxopt{\syntaxvar{interface-expression}} \sldots \srp \\
  \\
  \syntaxvar{mod-name} \syntaxdefequal \syntaxelement{module-name} \\
  \syntaxvar{module-expression} \syntaxdefequal
  \slp \syntaxelement{module-keyword} \syntaxelement{module-name}
  \sldots
  \srp \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp \syntaxelement{identifier}
  \sldots \srp \\
  \syntaxelement{module-keyword} \syntaxdefequal
  \skeyword{import} \syntaxor \skeyword{import-and-reexport}
  \syntaxor \skeyword{use} \\
  \syntaxvar{interface-expression} \syntaxdefequal
  \syntaxvar{declaration} \syntaxor
  \syntaxvar{definition}
\end{syntaxblock}


An interface with name \syntaxvarf{mod-name} is defined. The
\syntaxvarf{mod-name} may be either a single identifier or a list of
identifiers.
See chapter \ref{ch:programs-and-modules}.


\subsection{\keyword{define-body}}
\index{\keyword{define-body}}
\label{subsec:define-body}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-body} \syntaxvar{mod-name} \\
  \syntaxopt{\syntaxvar{module-expression}} \\
  \syntaxopt{\syntaxelement{expression}} \sldots \srp \\
  \\
  \syntaxvar{mod-name} \syntaxdefequal \syntaxelement{module-name} \\
  \syntaxvar{module-expression} \syntaxdefequal
  \slp \syntaxelement{module-keyword} \syntaxelement{module-name}
  \sldots
  \srp \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp \syntaxelement{identifier}
  \sldots \srp \\
  \syntaxelement{module-keyword} \syntaxdefequal
  \skeyword{import} \syntaxor
  \skeyword{use} \syntaxor \skeyword{prelink-body}
\end{syntaxblock}


A body with name \syntaxvarf{mod-name} is defined. The
\syntaxvarf{mod-name} may be either a single identifier or a list of
identifiers.
See chapter \ref{ch:programs-and-modules}.


\subsection{\keyword{import}}
\index{\keyword{import}}


Syntax:
\begin{syntaxblock}
  \slp \keyword{import} \syntaxelement{module-name} \sldots \srp \\
  \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp
  \syntaxelement{identifier} \sldots \srp \\
\end{syntaxblock}


An interface is imported.
See chapter \ref{ch:programs-and-modules} and
subsections \ref{subsec:define-proper-program}, \ref{subsec:define-script},
\ref{subsec:define-interface}, and \ref{subsec:define-body}.


\subsection{\keyword{import-and-reexport}}
\index{\keyword{import-and-reexport}}


Syntax:
\begin{syntaxblock}
  \slp \keyword{import-and-reexport} \syntaxelement{module-name} \sldots \srp \\
  \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp
  \syntaxelement{identifier} \sldots \srp \\
\end{syntaxblock}


An interface is imported and reexported.
See chapter \ref{ch:programs-and-modules} and
subsections \ref{subsec:define-interface}, and \ref{subsec:define-body}.


\subsection{\keyword{use}}
\index{\keyword{use}}


Syntax:
\begin{syntaxblock}
  \slp \keyword{use} \syntaxelement{module-name} \sldots \srp \\
  \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp
  \syntaxelement{identifier} \sldots \srp \\
\end{syntaxblock}


An interface can be accessed but its contents are not imported into
the toplevel namespace.
See chapter \ref{ch:programs-and-modules} and
subsections \ref{subsec:define-proper-program}, \ref{subsec:define-script},
\ref{subsec:define-interface}, and \ref{subsec:define-body}.


\subsection{\keyword{@}}


Syntax:
\begin{syntaxblock}
  \slp \keyword{@} \syntaxelement{module-name} \syntaxvar{variable} \srp \\
  \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp
  \syntaxelement{identifier} \sldots \srp \\
  \syntaxvar{variable} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Access a variable in the specified module.
See chapter \ref{ch:programs-and-modules} and
subsections \ref{subsec:define-proper-program}, \ref{subsec:define-script},
\ref{subsec:define-interface}, and \ref{subsec:define-body}.


\subsection{\keyword{reexport}}
\index{\keyword{reexport}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{reexport} \syntaxelement{identifier} \srp
\end{syntaxblock}


A variable is reexported. This expression type can occur only inside
an interface.
See chapter \ref{ch:programs-and-modules} and
subsection \ref{subsec:define-interface}.


\subsection{\keyword{prevent-stripping}}
\index{\keyword{prevent-stripping}}
\label{subsec:prevent-stripping}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{prevent-stripping} \syntaxelement{identifier} \srp
\end{syntaxblock}


This expression prevents stripping off a procedure or a class from the
linker output even though it is not detected in the coverage analysis.
This should be necessary only with the foreign function interface.


\subsection{\keyword{prelink-body}}
\index{\keyword{prelink-body}}
\label{subsec:prelink-body}

Syntax:
\begin{syntaxblock}
  \slp \keyword{prelink-body} \syntaxelement{module-name} \sldots \srp \\
  \\
  \syntaxelement{module-name} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \slp
  \syntaxelement{identifier} \sldots \srp \\
\end{syntaxblock}


The bodies for the specified modules are linked before the unit where
the \keyword{prelink-body} statement is given. Consequently the
procedures defined in the prelinked bodies may be called toplevel in
the unit. Keyword \keyword{prelink-body} may not be used in
interfaces.
See chapter \ref{ch:programs-and-modules} and
subsections \ref{subsec:define-proper-program}, \ref{subsec:define-script},
\ref{subsec:define-interface}, and \ref{subsec:define-body}.


\section{Toplevel Definitions}

\subsection{\keyword{define}}
\index{\keyword{define}}


Syntax:
\begin{syntaxblock}
\slp \skeyword{define} \syntaxvar{variable-name}
\syntaxopt{\syntaxvar{type}}
\objectvar{value} \srp \\
\\
\syntaxvar{variable-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}

A constant with name \syntaxvarf{variable-name} and value
\objectvarf{value} is defined. Expression \syntaxvarf{type} has to be a
static type expression if it is present.
If \syntaxvarf{type} is specified and
\objectvarf{value} is not an instance of \syntaxvarf{type} an error is
signalled.


\subsection{\keyword{define-class}}
\index{\keyword{define-class}}
\label{subsec:define-class}


Syntax:
\begin{syntaxblock}
\slp \skeyword{define-class} \syntaxvar{class-name}
\syntaxvar{superclass}
\syntaxvar{inheritable}
\syntaxvar{immutable}
\syntaxvar{eq-by-value}
\syntaxvar{ctr-access}
\syntaxvar{zero-value}
\syntaxvar{field-list} \srp \\
\\
\syntaxvar{class-name} \syntaxdefequal \syntaxelement{identifier} \\
\syntaxvar{inheritable} \syntaxdefequal \syntaxelement{boolean} \\
\syntaxvar{immutable} \syntaxdefequal \syntaxelement{boolean} \\
\syntaxvar{eq-by-value} \syntaxdefequal \syntaxelement{boolean} \\
\syntaxvar{ctr-access} \syntaxdefequal
\syntaxelement{access-specifier} \\
\syntaxvar{field-list} \syntaxdefequal \slp
\syntaxopt{\syntaxvar{field-specifier}} \sldots \srp \\
\syntaxvar{field-specifier} \syntaxdefequal
\slp \syntaxvar{field-name} \syntaxvar{field-type}
\syntaxvar{read-access} \syntaxvar{write-access}
\syntaxopt{\syntaxvar{field-initial-value}} \srp \\
\syntaxvar{field-name} \syntaxdefequal \syntaxelement{identifier} \\
\syntaxvar{read-access} \syntaxdefequal
\syntaxelement{access-specifier} \\
\syntaxvar{write-access} \syntaxdefequal
\syntaxelement{access-specifier} \\
\syntaxelement{access-specifier} \syntaxdefequal
\code{public} \syntaxor \code{module} \syntaxor \code{hidden}
\end{syntaxblock}


A new class is defined. Parameter \syntaxvarf{superclass} has to be
a static type expression whose value is a class. Parameters
\syntaxvarf{field-type} have to be static type expressions.


\subsection{\keyword{define-generic-proc}}
\index{\keyword{define-generic-proc}}


Syntax:
\begin{syntaxblock}
\slp \skeyword{define-generic-proc} \syntaxvar{generic-name} \srp \\
\\
\syntaxvar{generic-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


This expression defines a generic procedure with the name given. Note
that \keyword{define-simple-method} and \keyword{define-param-method} define
 a generic procedure implicitly if it has not been already defined.


\subsection{\keyword{define-goops-class}}
\index{\keyword{define-goops-class}}
\label{subsec:define-goops-class}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-goops-class} 
  \syntaxvar{name}
  \syntaxvar{target-name}
  \syntaxvar{superclass}
  \syntaxvar{inheritable?}
  \syntaxvar{immutable?}
  \syntaxvar{equal-by-value?}
  \syntaxvar{checked?}
  \syntaxvar{zero-var}
  \syntaxvar{equal-pred}
  \syntaxvar{equal-contents-pred}
  \srp
  \\
  \syntaxvar{name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{target-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{inheritable?} \syntaxdefequal \syntaxelement{boolean} \\
  \syntaxvar{immutable?} \syntaxdefequal \syntaxelement{boolean} \\
  \syntaxvar{equal-by-value?} \syntaxdefequal \syntaxelement{boolean} \\
  \syntaxvar{checked?} \syntaxdefequal \syntaxelement{boolean} \\
  \syntaxvar{zero-var} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null} \\
  \syntaxvar{equal-pred} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \syntaxelement{null} \\
  \syntaxvar{equal-contents-pred} \syntaxdefequal
  \syntaxelement{identifier} \syntaxor \syntaxelement{null}
\end{syntaxblock}


Keyword \keyword{define-goops-class} defines a custom GOOPS class
existing in the target environment.
A custom GOOPS class may only inherit (in \theme) from another custom
GOOPS class or from \builtin{<object>}.
Flags \syntaxvarf{inheritable?}, \syntaxvarf{immutable?} and
\syntaxvarf{equal-by-value?} specify
whether the class is inheritable, immutable or equal by value,
respectively. If
\syntaxvarf{checked?} is \true\ the types of the result values of the
predicates are checked runtime. If \syntaxvarf{zero-var} is not null
it defines a zero value (variable) for the class. Arguments
\syntaxvarf{equal-pred} and
\syntaxvarf{equal-contents-pred} determine the Scheme predicates that
are used to compare values of this class
in predicates \builtin{equal?} and
\builtin{equal-contents?}. When procedure \code{equal-objects?} is
used with GOOPS objects the comparison is performed by the target
Scheme procedure \code{eqv?}. If \syntaxvarf{equal-pred} or
\syntaxvarf{equal-contents-pred} is null the default value is
used. The default value is \code{eqv?} for \syntaxvarf{equal-pred} and
\code{equal?} for \syntaxvarf{equal-contents-pred}.
When the equality predicates are called
both of the arguments belong always to the declared class. If
the Scheme predicate \code{eqv?} returns \true\ for some arguments
predicate \syntaxvarf{equal-pred} must also return \true\ for these
arguments. If \syntaxvarf{equal-pred} returns \true\ for some
arguments predicate \syntaxvarf{equal-contents-pred} must also return
\true\ for these arguments. If two objects belonging to the declared
class are equal by Scheme predicate \code{eq?} both
\syntaxvarf{equal-pred} and \syntaxvarf{equal-contents-pred}
have to return \true\ for these objects.


\subsection{\keyword{define-mutable}}
\index{\keyword{define-mutable}}


Syntax:
\begin{syntaxblock}
 \slp \skeyword{define-mutable} \syntaxvar{variable-name} \syntaxvar{type}
 \objectvar{value}) \\
 \\
 \syntaxvar{variable-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}

A mutable variable with name \syntaxvarf{variable-name},
type \syntaxvarf{type} and initial value
\objectvarf{value} is defined.
Expression \syntaxvarf{type} has to be a
static type expression.
If \objectvarf{value} is not an instance
of \syntaxvarf{type} an error is signalled.


\subsection{\keyword{define-volatile}}
\index{\keyword{define-volatile}}


Syntax:
\begin{syntaxblock}
 \slp \skeyword{define-volatile} \syntaxvar{variable-name} \syntaxvar{type}
 \objectvar{value}) \\
 \\
 \syntaxvar{variable-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}

A volatile variable with name \syntaxvarf{variable-name},
type \syntaxvarf{type} and initial value
\objectvarf{value} is defined.
Expression \syntaxvarf{type} has to be a
static type expression.
If \objectvarf{value} is not an instance
of \syntaxvarf{type} an error is signalled.


\subsection{\keyword{define-param-logical-type}}
\index{\keyword{define-param-logical-type}}
\label{subsec:define-param-logical-type}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-param-logical-type} \syntaxvar{param-ltype-name}
  \syntaxvar{type-parameter-list} \\
  \syntaxvar{type-expression} \srp \\
  \\
  \syntaxvar{param-ltype-name} \syntaxdefequal
  \syntaxelement{identifier}
\end{syntaxblock}


Expression
\syntaxvarf{type-expression} has to be a static type expression.
When the instances of the parametrized logical type are created the
type variables in \syntaxvarf{type-parameter-list} are bound to the
values given for them and these bindings are applied for
\syntaxvarf{type-expression}.


\subsection{\keyword{define-param-class}}
\index{\keyword{define-param-class}}
\label{subsec:define-param-class}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-param-class} 
  \syntaxvar{param-class-name}
  \syntaxvar{type-parameter-list}
  \syntaxvar{superclass}
  \syntaxvar{inheritable}
  \syntaxvar{immutable}
  \syntaxvar{eq-by-value}
  \syntaxvar{ctr-access}
  \syntaxvar{zero-value}
  \syntaxvar{field-list} \srp \\
  \\
  \syntaxvar{type-parameter-list} \syntaxdefequal
  \slp \indexedsyntaxvar{type-param}{1} \sldots
  \indexedsyntaxvar{type-param}{n} \srp \\
  \indexedsyntaxvar{type-param}{k} \syntaxdefequal
  \syntaxelement{identifier}
\end{syntaxblock}


The syntax of \syntaxvarf{type-parameter-list} is the same as in
\keyword{define-param-logical-type}. 
The syntax of the last seven parameters is similar to their syntax in
\keyword{define-class}. Parameter \syntaxvar{superclass} has to be
a static type expression whose value is a class. Parameters
\syntaxvar{field-type} have to be static type expressions. Parameters
\syntaxvar{superclass}, \syntaxvar{inheritable}, \syntaxvar{immutable},
\syntaxvar{eq-by-value}, \syntaxvar{ctr-access}, and the field list
define the properties of the instances of the parametrized class being
defined.
When the instances of the parametrized class are created the
type variables in \syntaxvarf{type-parameter-list} are bound to the
values given for them and these bindings are applied for
\syntaxvarf{field-list} and \syntaxvarf{superclass}.


\subsection{\keyword{define-param-proc-alt}}
\index{\keyword{define-param-proc-alt}}
\label{subsec:expr-define-param-proc-alt}


Syntax:
\begin{syntaxblock}
  \slp
  \skeyword{define-param-proc-alt} \syntaxvar{proc-name}
  \slp \indexedsyntaxvar{type}{1} ... \indexedsyntaxvar{type}{n} \srp
  \ \syntaxvar{proc-expression}
  \srp\\
\\
\syntaxvar{proc-name} \syntaxdefequal \syntaxelement{identifier} \\
\indexedsyntaxvar{type}{k} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


This is an alternate way to define a parametrized procedure.
Expression \syntaxvarf{proc-expression} has to be a \keyword{lambda}
expression.


\subsection{\keyword{define-param-signature}}
\index{\keyword{define-param-signature}}
\label{subsec:expr-define-param-signature}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-param-signature} \syntaxvar{signature-name}
  \syntaxvar{type-param-list}
  \syntaxvar{super}
  \syntaxvar{proc-specifier} \sldots \srp \\
  \\
  \syntaxvar{signature-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{type-param-list} \syntaxdefequal
  \slp
  \syntaxopt{\syntaxelement{identifier} \sldots}
  \srp \\
  \syntaxvar{super} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null} \\
  \syntaxvar{proc-specifier} \syntaxdefequal
  \slp
  \syntaxvar{procedure-name} \syntaxvar{arg-type-list}
  \syntaxvar{result-type} \syntaxvar{attribute-list}
  \srp \\
  \syntaxvar{procedure-name} \syntaxdefequal
  \syntaxelement{identifier} \\
  \syntaxvar{arg-type-list} \syntaxdefequal
  \slp
  \syntaxopt{\syntaxvar{arg-type} \sldots}
  \srp \\
  \attrlistdefb
\end{syntaxblock}


Object \syntaxvarf{super} is the signature from which the parametrized
signature inherits. In case a signature does not inherit anything
\syntaxvarf{super} is set to \syntaxelementf{null}. A complete
specifier list of a parametrized signature is obtained by
concatenating the complete specifier list of the \syntaxvarf{super}
signature with the specifier list of the parametrized signature being
defined.

Expressions \syntaxvarf{proc-specifier} specify the procedures that
all instances of the parametrized signature have to implement. Keyword
\skeyword{this} is used to refer to an instance of the parametrized
signature itself in the procedure specifiers.

When the type variables of a parametrized signature are substituted
with types we get an instance of the parametrized signature. This
instance is a (ordinary) signature.


\subsection{\keyword{define-prim-class}}
\index{\keyword{define-prim-class}}
\label{subsec:define-prim-class}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-prim-class} 
  \syntaxvar{name}
  \syntaxvar{immutable?}
  \syntaxvar{equal-by-value?}
  \syntaxvar{checked?}
  \syntaxvar{zero-var}
  \syntaxvar{member-pred}
  \syntaxvar{equal-pred}
  \syntaxvar{equal-objects-pred}
  \syntaxvar{equal-contents-pred}
  \srp
  \\
  \syntaxvar{name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{immutable?} \syntaxdefequal \syntaxelement{boolean} \\
  \syntaxvar{equal-by-value?} \syntaxdefequal \syntaxelement{boolean} \\
  \syntaxvar{checked?} \syntaxdefequal \syntaxelement{boolean} \\
  \syntaxvar{zero-var} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null} \\
  \syntaxvar{member-pred} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null} \\
  \syntaxvar{equal-pred} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null} \\
  \syntaxvar{equal-objects-pred} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null} \\
  \syntaxvar{equal-contents-pred} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null}
\end{syntaxblock}


Keyword \keyword{define-prim-class} defines a custom primitive class
existing in the target environment. A custom primitive class cannot be
inherited and it is an immediate descendant of \builtin{<object>}.
Procedure \syntaxvarf{member-pred} determines if an object belongs to
the class.  Flags \syntaxvarf{immutable?} and
\syntaxvarf{equal-by-value?} specify whether the class is immutable or
equal by value, respectively. If \syntaxvarf{checked?} is \true\ the
types of the result values of the predicates are checked runtime. If
\syntaxvarf{zero-var} is not null it defines a zero value (variable)
for the class. Arguments \syntaxvarf{equal-pred},
\syntaxvarf{equal-objects-pred}, and \syntaxvarf{equal-contents-pred}
determine the target Scheme procedures that are used to compare the
values of this class in predicates \builtin{equal?},
\builtin{equal-objects?}, and \builtin{equal-contents?}.  If any of
these arguments is \syntaxelement{null} the default value is used. The
default value is \code{eqv?} for \builtin{equal?} and
\builtin{equal-objects?} and \code{equal?} for
\builtin{equal-contents?}. When the equality predicates are called
both of the arguments belong always to the declared class. If
\syntaxvarf{equal-objects-pred} returns \true\ for some arguments
predicate \syntaxvarf{equal-pred} must also return \true\ for these
arguments. If \syntaxvarf{equal-pred} returns \true\ for some
arguments predicate \syntaxvarf{equal-contents-pred} must also return
\true\ for these arguments. If two objects belonging to the declared
class are equal by Scheme predicate \code{eq?} all the three
predicates have to return \true\ for these objects.


\subsection{\keyword{define-signature}}
\index{\keyword{define-signature}}
\label{subsec:expr-define-signature}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-signature} \syntaxvar{signature-name}
  \syntaxvar{super}
  \syntaxvar{proc-specifier} \sldots \srp \\
  \\
  \syntaxvar{signature-name} \syntaxdefequal \syntaxelement{identifier} \
  \syntaxvar{super} \syntaxdefequal \syntaxelement{identifier}
  \syntaxor \syntaxelement{null} \\
  \syntaxvar{proc-specifier} \syntaxdefequal
  \slp
  \syntaxvar{procedure-name} \syntaxvar{arg-type-list}
  \syntaxvar{result-type} \syntaxvar{attribute-list}
  \srp \\
  \syntaxvar{procedure-name} \syntaxdefequal
  \syntaxelement{identifier} \\
  \syntaxvar{arg-type-list} \syntaxdefequal
  \slp
  \syntaxopt{\syntaxvar{arg-type} \sldots}
  \srp \\
  \attrlistdefb
\end{syntaxblock}


Object \syntaxvarf{super} is the signature from which the signature
inherits. In case a signature does not inherit anything
\syntaxvarf{super} is set to \syntaxelementf{null}. A complete
specifier list of a signature is obtained
by concatenating the complete specifier list of the \syntaxvarf{super}
signature with the specifier list of the signature being defined.

Expressions \syntaxvarf{proc-specifier} specify the procedures that
all instances of the signature have to implement. Keyword
\skeyword{this} is used to refer to the signature itself in the
procedure specifiers.


\subsection{\keyword{add-method}}
\index{\keyword{add-method}}
\label{subsec:expr-add-method}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{add-method} \syntaxvar{generic-name}
  \syntaxvar{method} \srp \\
  \\
  \syntaxvar{generic-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{add-method} adds \syntaxvarf{method} into the
generic procedure \syntaxvarf{generic-name}. The method will be
dispatched dynamically.
Procedure \syntaxvarf{method} has to be a simple procedure or a
parametrized procedure.


\subsection{\keyword{add-static-method}}
\index{\keyword{add-static-method}}
\label{subsec:expr-add-static-method}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{add-static-method} \syntaxvar{generic-name}
  \syntaxvar{method} \srp \\
  \\
  \syntaxvar{generic-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{add-static-method} adds \syntaxvarf{method} into the
generic procedure \syntaxvarf{generic-name}. The method will be
dispatched statically.
Procedure \syntaxvarf{method} has to be a simple procedure or a
parametrized procedure.


\section{Declarations}


\subsection{\keyword{declare}}
\index{\keyword{declare}}


Syntax:
\begin{syntaxblock}
\slp \skeyword{declare} \syntaxvar{variable-name} \syntaxvar{class} \srp \\
\\
\syntaxvar{variable-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


A \keyword{declare} expression declares a variable with given class
without defining it. It is possible to use the variable after
declaration although the use may be restricted somehow. E.g. it is not
possible to use a declared class before defining it as a superclass of
another class. Note that \keyword{declare} needs always a class and it
does not accept logical types. Expression \syntaxvarf{class} has to be
a static type expression whose value is a class.
It is possible to redeclare the
variable several times but then the new declared class has to be a
subclass of the old class and the new class must have the same number
of fields as the old class. The same typing rule is applied also when
a declared variable is defined (the defined type is the new class).
A declared variable has to be defined in the same module where the
declaration is.


\subsection{\keyword{declare-method}}
\index{\keyword{declare-method}}
\label{subsec:expr-declare-method}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{declare-method} \syntaxvar{generic-name}
  \syntaxvar{procedure-class} \srp
  \\
  \syntaxvar{generic-name} \syntaxdefequal
  \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{declare-method} declares a dynamic method.
A declaration of the method is added into
the generic procedure \syntaxvarf{generic-name}.
The \syntaxvarf{procedure-class} has to be either a simple or
a parametrized procedure class.
A declared method has to be defined either
\begin{itemize}
\item in the same translation unit where the declaration is or
\item in the body of the interface if the declaration is in an interface.
\end{itemize}


\subsection{\keyword{declare-static-method}}
\index{\keyword{declare-static-method}}
\label{subsec:expr-declare-static-method}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{declare-static-method} \syntaxvar{generic-name}
  \syntaxvar{procedure-class} \srp
  \\
  \syntaxvar{generic-name} \syntaxdefequal
  \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{declare-static-method} declares a static method.
A declaration of the method is added into
the generic procedure \syntaxvarf{generic-name}.
The \syntaxvarf{procedure-class} has to be either a simple or
a parametrized procedure class.
A declared method has to be defined either
\begin{itemize}
\item in the same translation unit where the declaration is or
\item in the body of the interface if the declaration is in an interface.
\end{itemize}


\subsection{\keyword{declare-mutable}}
\index{\keyword{declare-mutable}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{declare-mutable} \syntaxvar{variable-name} \syntaxvar{type}
  \srp \\
  \\
  \syntaxvar{variable-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{declare-mutable} declares a mutable variable. The
\syntaxvarf{type} has to be the type of the variable
\syntaxvarf{variable-name}. Note that a variable declared with
\keyword{declare-mutable} cannot be defined as volatile.


\subsection{\keyword{declare-volatile}}
\index{\keyword{declare-volatile}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{declare-volatile} \syntaxvar{variable-name} \syntaxvar{type}
  \srp \\
  \\
  \syntaxvar{variable-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{declare-volatile} declares a volatile variable. The
\syntaxvarf{type} has to be the type of the variable
\syntaxvarf{variable-name}.


\section{Control Structures}


\subsection{\keyword{if}}
\index{\keyword{if}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{if} \objectvar{condition} \syntaxvar{then-expression}
  \syntaxopt{\syntaxvar{else-expression}}
  \srp
\end{syntaxblock}


The type of \objectvarf{condition} has to be \builtin{<boolean>}.
If \syntaxvarf{else-expression} is defined the type of
the \keyword{if} expression is the union of the types of
\syntaxvarf{then-expression} and \syntaxvarf{else-expression}. Otherwise
the type of the \keyword{if} expression is \builtin{<none>}.

If \objectvarf{condition} is \code{\#t} \syntaxvar{then-expression}
is evaluated. If \objectvarf{condition} is \code{\#f}
and \syntaxvarf{else-expression} is defined \syntaxvarf{else-expression}
is evaluated. If the result type of the \keyword{if} expression is not
\builtin{<none>} the value returned from \syntaxvarf{then-expression}
or \syntaxvarf{else-expression} is returned from the \keyword{if}
expression. Note that \syntaxvarf{then-expression} or
\syntaxvarf{else-expression} are not necessarily evaluated at all.


\subsection{\keyword{if-object}}
\index{\keyword{if-object}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{if-object} \objectvar{condition} \syntaxvar{then-expression}
  \syntaxopt{\syntaxvar{else-expression}}
  \srp
\end{syntaxblock}


The \objectvarf{condition} can be any object.
If \syntaxvarf{else-expression} is defined the type of
the \keyword{if-object} expression is the union of the types of
\syntaxvarf{then-expression} and \syntaxvarf{else-expression}. Otherwise
the type of the \keyword{if-object} expression is \builtin{<none>}.

If \objectvarf{condition} is not \code{\#f} \syntaxvar{then-expression}
is evaluated. If \objectvarf{condition} is \code{\#f}
and \syntaxvarf{else-expression} is defined \syntaxvarf{else-expression}
is evaluated. If the result type of the \keyword{if-object} expression is not
\builtin{<none>} the value returned from \syntaxvarf{then-expression}
or \syntaxvarf{else-expression} is returned from the \keyword{if-object}
expression. Note that \syntaxvarf{then-expression} or
\syntaxvarf{else-expression} are not necessarily evaluated at all.


\subsection{\keyword{until}}
\index{\keyword{until}}

Syntax:
\begin{syntaxblock}
  \slp \keyword{until} \slp \syntaxvar{condition}
  \syntaxopt{\syntaxvar{result-expression}} \srp
  \syntaxspace
  \indexedsyntaxvar{body-expression}{1} \sldots
  \indexedsyntaxvar{body-expression}{n} \srp
\end{syntaxblock}


The type of \syntaxvarf{condition} has to be \builtin{<boolean>}. At the
beginning of each iteration \syntaxvarf{condition} is evaluated. If it
returns \code{\#t} the iteration is stopped and the value of
\syntaxvarf{result-expression} is returned as the result of the
\keyword{until} expression. Otherwise the body expressions are
evaluated in order and the next iteration is started from the
beginning. If \syntaxvar{result-type} is not specified the type of the
\keyword{until} expression is \builtin{<none>}.


\subsection{\keyword{begin}}
\index{\keyword{begin}}


Syntax:
\begin{syntaxblock}
  \slp
  \skeyword{begin} \indexedsyntaxvar{expr}{1} \sldots
  \indexedsyntaxvar{expr}{n}
  \srp
\end{syntaxblock}


The type of the \keyword{begin} expression is the type of the last
component expression \indexedsyntaxvarf{expr}{n}. All the component
expressions \indexedsyntaxvarf{expr}{k} are evaluated in order. If the result
type of the
last component expression is not \builtin{<none>} its value is
returned as the value of the \keyword{begin} expression.


\subsection{\keyword{set!}}
\index{\keyword{set{"!}}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{set!} \syntaxvar{variable-name}
  \objectvar{value} \srp \\
  \\
  \syntaxvar{variable-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


The value of the variable \syntaxvarf{variable-name} is set to
\objectvarf{value}. Variable
\syntaxvarf{variable-name} has to be defined and it has to be
mutable. The type of \objectvarf{value} has to be a subtype
of the type of variable \syntaxvarf{variable-name}. If these rules are
violated a translation error (usually a compilation error) is signalled.


\subsection{\keyword{generic-proc-dispatch}}
\index{\keyword{generic-proc-dispatch}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{generic-proc-dispatch} \syntaxvar{gen-proc-name}
  (\indexedsyntaxvar{arg-type}{1} ... \indexedsyntaxvar{arg-type}{n}) \\
  \syntaxvar{attribute-list}
  \srp \\
  \\
  \syntaxvar{gen-proc-name} \syntaxdefequal \syntaxelement{identifier} \\
  \attrlistdefb
\end{syntaxblock}


Keyword \keyword{generic-proc-dispatch} returns a simple procedure
that dispatches a call to generic procedure \syntaxvarf{gen-proc-name}
with argument type \indexedsyntaxvarf{arg-type}{k}.
Expressions \indexedsyntaxvarf{arg-type}{k} have to be static type expressions.
The dispatched method must be compatible with the given attributes and
its result type must not be \builtin{<none>}.
Although a value of a
\keyword{generic-proc-dispatch} expression is a simple procedure the
dispatch is generally done runtime. Calling a \keyword{generic-proc-dispatch}
expression always finds the correct method based on the methods
contained in the generic procedure run time.


\subsection{\keyword{generic-proc-dispatch-without-result}}
\index{\keyword{generic-proc-dispatch-without-result}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{generic-proc-dispatch-without-result} \syntaxvar{gen-proc-name}
  (\indexedsyntaxvar{arg-type}{1} ... \indexedsyntaxvar{arg-type}{n}) \\
  \syntaxvar{attribute-list}
  \srp \\
  \\
  \syntaxvar{gen-proc-name} \syntaxdefequal \syntaxelement{identifier} \\
  \attrlistdefb
\end{syntaxblock}


Keyword \keyword{generic-proc-dispatch-without-result} returns a
simple procedure that dispatches a call to generic procedure
\syntaxvarf{gen-proc-name} with argument type
\indexedsyntaxvarf{arg-type}{k}.  Expressions
\indexedsyntaxvarf{arg-type}{k} have to be static type expressions.
The result type of the type of the dispatch expression is
\builtin{<none>}.  The dispatched method must be compatible with the
given attributes.  Although a value of a
\keyword{generic-proc-dispatch-without-result} expression is a simple
procedure the dispatch is generally done runtime. Calling a
\keyword{generic-proc-dispatch-without-result} expression always finds
the correct method based on the methods contained in the generic
procedure run time.


\subsection{\keyword{param-proc-dispatch}}
\index{\keyword{param-proc-dispatch}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{param-proc-dispatch} \syntaxvar{param-proc-name}
  \indexedsyntaxvar{arg-type}{1} \sldots
  \indexedsyntaxvar{arg-type}{n} \srp \\
  \\
  \syntaxvar{param-proc-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


A \keyword{param-proc-dispatch} expression returns a simple procedure
obtained by creating an instance of parametrized procedure
\syntaxvarf{param-proc-name}.
The values of the type parameters of parametrized procedure
\syntaxvarf{param-proc-name} are deduced from the types
\indexedsyntaxvarf{arg-type}{k} as if the types
\indexedsyntaxvarf{arg-type}{k}
were argument types in an application of \syntaxvarf{param-proc-name}.
Expressions
\indexedsyntaxvarf{arg-type}{k} have to be static type expressions.


\subsection{\keyword{param-proc-instance}}
\index{\keyword{param-proc-instance}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{param-proc-instance} \syntaxvar{param-proc-name}
  \indexedsyntaxvar{arg-type}{1} \sldots
  \indexedsyntaxvar{arg-type}{n} \srp \\
  \\
  \syntaxvar{param-proc-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


A \keyword{param-proc-instance} expression returns a simple procedure
obtained by creating an instance of parametrized procedure
\syntaxvarf{param-proc-name}.
The type parameters defined in the definition of
\syntaxvarf{param-proc-name} are bound to expressions
\indexedsyntaxvarf{arg-type}{k} in order. Expressions
\indexedsyntaxvarf{arg-type}{k} have to be static type expressions.


\subsection{\keyword{strong-assert}}
\index{\keyword{strong-assert}}
\label{subsec:expr-strong-assert}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{strong-assert} \syntaxvar{condition} \srp
\end{syntaxblock}


An assertion checks if the condition is true. If the condition is not
true an exception will be raised. See also subsection
\ref{subsec:expr-assert}. The difference between \keyword{assert} and
\keyword{strong-assert} is that a strong assertion may never be
neglected because of optimization.


\subsection{\keyword{assert}}
\index{\keyword{assert}}
\label{subsec:expr-assert}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{assert} \syntaxvar{condition} \srp
\end{syntaxblock}


An assertion checks if the condition is true. If the condition is not
true an exception will be raised. See also subsection
\ref{subsec:expr-strong-assert}.


\section{Macro Forms}


\subsection{\keyword{define-syntax}}
\index{\keyword{define-syntax}}

Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-syntax} \syntaxvar{macro-name}
  \syntaxvar{macro-transformer} \srp \\
  \syntaxvar{macro-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


This form defines a macro.


\subsection{\keyword{let-syntax}}
\index{\keyword{let-syntax}}

Syntax:
\begin{syntaxblock}
  \slp \skeyword{let-syntax}
  \syntaxspace \slp \indexedsyntaxvar{var-spec}{1} \sldots
  \indexedsyntaxvar{var-spec}{n}
  \srp
  \syntaxspace \syntaxvar{let-syntax-body-expressions} \srp \\
  \\
  \indexedsyntaxvar{var-spec}{k} \syntaxdefequal \slp
  \indexedsyntaxvar{var-name}{k}
  \indexedobjectvar{value}{k}
  \srp
\end{syntaxblock}


This form defines local macros.


\subsection{\keyword{letrec-syntax}}
\index{\keyword{letrec-syntax}}

Syntax:
\begin{syntaxblock}
  \slp \skeyword{letrec-syntax}
  \syntaxspace \slp \indexedsyntaxvar{var-spec}{1} \sldots
  \indexedsyntaxvar{var-spec}{n}
  \srp
  \syntaxspace \syntaxvar{let-syntax-body-expressions} \srp \\
  \\
  \indexedsyntaxvar{var-spec}{k} \syntaxdefequal \slp
  \indexedsyntaxvar{var-name}{k}
  \indexedobjectvar{value}{k}
  \srp
\end{syntaxblock}


This form defines local macros.


\subsection{\keyword{syntax-case}}
\index{\keyword{syntax-case}}

Syntax:
\begin{syntaxblock}
  \slp \skeyword{syntax-case} \syntaxvar{expression}
  \slp \syntaxopt{\syntaxvar{literal}} \sldots \srp
  \syntaxopt{\syntaxvar{clause}} \sldots \srp \\
  \syntaxvar{literal} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


This form defines a macro transformer.


\section{Binding Forms}


\subsection{\keyword{let}}
\index{\keyword{let}}

Syntax:
\begin{syntaxblock}
  \slp \skeyword{let}
  \syntaxspace \slp \indexedsyntaxvar{var-spec}{1} ... \indexedsyntaxvar{var-spec}{n}
  \srp
  \syntaxspace \syntaxvar{let-body-expressions} \srp \\
  \\
  \indexedsyntaxvar{var-spec}{k} \syntaxdefequal \slp
  \indexedsyntaxvar{var-name}{k}
  \syntaxopt{\indexedsyntaxvar{var-type}{k}} \indexedobjectvar{value}{k}
  \srp \\
  \indexedsyntaxvar{var-name}{k} \syntaxdefequal
  \syntaxelement{identifier} \\
  \syntaxvar{let-body-expressions} \syntaxdefequal
  \syntaxelement{expression} \sldots
\end{syntaxblock}


Expressions \indexedsyntaxvarf{var-type}{k} have to be static type expressions.
The result type of the let expression is the type of the last body
expression. If the result type is not \builtin{<none>} the result
value of the let expression is the value of the last body expression.
The semantics of \keyword{let} expression is
similar to these expressions in Scheme except the variable types are
checked.


\subsection{\keyword{letrec} and \keyword{letrec*}}
\index{\keyword{letrec}}
\index{\keyword{letrec*}}

Syntax:
\begin{syntaxblock}
  \slp \mlp \keyword{letrec} \syntaxor \keyword{letrec*} \mrp
  \syntaxspace  \slp \indexedsyntaxvar{var-spec}{1} \sldots
  \indexedsyntaxvar{var-spec}{n} \srp
  \syntaxspace  \syntaxvar{letrec-body-expressions} \srp \\
  \\
  \indexedsyntaxvar{var-spec}{k} \syntaxdefequal \slp
  \indexedsyntaxvar{var-name}{k}
  \indexedsyntaxvar{var-type}{k} \indexedobjectvar{value}{k} \srp \\
  \indexedsyntaxvar{var-name}{k} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{letrec-body-expressions} \syntaxdefequal
  \syntaxelement{expression} \sldots
\end{syntaxblock}


Expressions \indexedsyntaxvarf{var-type}{k} have to be static type expressions.
The result type of the letrec expression is the type of the last body
expression. If the result type is not \builtin{<none>} the result
value of the letrec expression is the value of the last body expression.
It is possible to refer to the letrec variables
\indexedsyntaxvarf{var-name}{k} recursively in the expressions
\indexedobjectvarf{value}{k} but these recursive uses of the variables must
occur inside a \keyword{lambda} expression.
Keyword \keyword{letrec*} differs from \keyword{letrec} so that
\keyword{letrec*} guarantees to evaluate the expressions
\indexedobjectvarf{value}{k} in order.


\subsection{\keyword{let-mutable},
  \keyword{letrec-mutable}, and \keyword{letrec*-mutable}}
\index{\keyword{let-mutable}}
\index{\keyword{letrec-mutable}}
\index{\keyword{letrec*-mutable}}


Syntax:
\begin{syntaxblock}
  \slp \mlp \skeyword{let-mutable}
  \syntaxor \skeyword{letrec-mutable} \syntaxor
  \skeyword{letrec*-mutable} \mrp
  \syntaxspace  \slp \indexedsyntaxvar{var-spec}{1} \sldots
  \indexedsyntaxvar{var-spec}{n} \srp
  \syntaxspace  \syntaxvar{let-body-expressions} \srp \\
  \\
  \indexedsyntaxvar{var-spec}{k} \syntaxdefequal \slp
  \indexedsyntaxvar{var-name}{k}
  \indexedsyntaxvar{var-type}{k} \indexedobjectvar{value}{k}) \\
  \indexedsyntaxvar{var-name}{k} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{let-body-expressions} \syntaxdefequal
  \syntaxelement{expression} \sldots
\end{syntaxblock}


These expressions differ from the corresponding constant versions
\keyword{let}, \keyword{letrec}, and \keyword{letrec*}
so that the variables \indexedsyntaxvarf{var-name}{k} are mutable in the
\keyword{letxxx-mutable} expressions.
Note that the variable types are compulsory in all of the
\keyword{letxxx-mutable} expressions.


\subsection{\keyword{let-volatile},
  \keyword{letrec-volatile}, and \keyword{letrec*-volatile}}
\index{\keyword{let-volatile}}
\index{\keyword{letrec-volatile}}
\index{\keyword{letrec*-volatile}}


Syntax:
\begin{syntaxblock}
  \slp \mlp \skeyword{let-volatile}
  \syntaxor \skeyword{letrec-volatile} \syntaxor
  \skeyword{letrec*-volatile} \mrp
  \syntaxspace  \slp \indexedsyntaxvar{var-spec}{1} \sldots
  \indexedsyntaxvar{var-spec}{n} \srp
  \syntaxspace  \syntaxvar{let-body-expressions} \srp \\
  \\
  \indexedsyntaxvar{var-spec}{k} \syntaxdefequal \slp
  \indexedsyntaxvar{var-name}{k}
  \indexedsyntaxvar{var-type}{k} \indexedobjectvar{value}{k}) \\
  \indexedsyntaxvar{var-name}{k} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{let-body-expressions} \syntaxdefequal
  \syntaxelement{expression} \sldots
\end{syntaxblock}


These expressions differ from the corresponding mutable versions
\keyword{letxxx-mutable}
so that the variables \indexedsyntaxvarf{var-name}{k} are volatile in the
\keyword{letxxx-volatile} expressions.
Note that the variable types are compulsory in all of the
\keyword{letxxx-volatile} expressions.


\section{Procedure Creation}


\subsection{\keyword{lambda}}
\index{\keyword{lambda}}
\label{subsec:expr-lambda}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{lambda} \syntaxopt{\syntaxvar{name}}
  \slp \syntaxvar{argument-list}
  \syntaxvar{result-type}
  \syntaxvar{attribute-list} \srp
  \syntaxspace \indexedsyntaxvar{body-expr}{1}, \sldots,
  \indexedsyntaxvar{body-expr}{n} \srp \\
  \\
  \syntaxvar{name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{argument-list} \syntaxdefequal \slp
  \syntaxopt{\indexedsyntaxvar{arg}{1} \sldots
    \indexedsyntaxvar{arg}{n}} \srp \\
  \indexedsyntaxvar{arg}{k} \syntaxdefequal (\indexedsyntaxvar{arg-name}{k}
  \indexedsyntaxvar{arg-type}{k}) \\
  \indexedsyntaxvar{arg-name}{k} \syntaxdefequal \syntaxelement{identifier} \\
  \attrlistdef
\end{syntaxblock}


A \keyword{lambda} expression creates a simple procedure. Note that
the argument list may be \code{()}.
Expressions \indexedsyntaxvarf{arg-type}{k} and
\syntaxvarf{result-type} have to be static type expressions.
It is an error if the result type is not \builtin{<none>} and
the type of the last body expression is not a subtype of
\syntaxvarf{result-type}.
If \syntaxvarf{result-type} is not
\builtin{<none>} the result value of the procedure is the value of the
last body expression.
Expression \syntaxvarf{name} is the optional name of the lambda
expression.


\subsection{\keyword{lambda-automatic}}
\index{\keyword{lambda-automatic}}
\label{subsec:expr-lambda-automatic}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{lambda-automatic} \slp \syntaxvar{argument-list}
  \syntaxvar{attribute-list} \srp
  \syntaxspace \indexedsyntaxvar{body-expr}{1}, \sldots,
  \indexedsyntaxvar{body-expr}{n} \srp \\
  \\
  \syntaxvar{argument-list} \syntaxdefequal \slp
  \syntaxopt{\indexedsyntaxvar{arg}{1} \sldots
    \indexedsyntaxvar{arg}{n}} \srp \\
  \indexedsyntaxvar{arg}{k} \syntaxdefequal (\indexedsyntaxvar{arg-name}{k}
  \indexedsyntaxvar{arg-type}{k}) \\
  \indexedsyntaxvar{arg-name}{k} \syntaxdefequal \syntaxelement{identifier} \\
  \attrlistdef
\end{syntaxblock}


This form works as \keyword{lambda} except the result type is set to
the type of the last body expression.


\subsection{\keyword{param-lambda}}
\index{\keyword{param-lambda}}
\label{subsec:expr-param-lambda}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{param-lambda}
  \slp \indexedsyntaxvar{type}{1} ... \indexedsyntaxvar{type}{n} \srp
  \slp \syntaxvar{argument-list}
  \syntaxvar{result-type}
  \syntaxvar{attribute-list} \srp
  \syntaxspace \indexedsyntaxvar{body-expr}{1}, \sldots,
  \indexedsyntaxvar{body-expr}{n} \srp \\
  \\
  \indexedsyntaxvar{type}{k} \syntaxdefequal
  \syntaxelement{identifier} \\
  \syntaxvar{argument-list} \syntaxdefequal \slp
  \syntaxopt{\indexedsyntaxvar{arg}{1} \sldots
    \indexedsyntaxvar{arg}{n}} \srp \\
  \indexedsyntaxvar{arg}{k} \syntaxdefequal (\indexedsyntaxvar{arg-name}{k}
  \indexedsyntaxvar{arg-type}{k}) \\
  \indexedsyntaxvar{arg-name}{k} \syntaxdefequal \syntaxelement{identifier} \\
  \attrlistdef
\end{syntaxblock}


A \keyword{param-lambda} expression creates a parametrized
procedure. Note that the argument list may be \code{()}.
Expressions \indexedsyntaxvarf{arg-type}{k} and
\syntaxvarf{result-type} have to be static type expressions.
It is an error if the result type is not \builtin{<none>} and
the type of the last body expression is not a subtype of
\syntaxvarf{result-type}.
If \syntaxvarf{result-type} is not
\builtin{<none>} the result value of the procedure is the value of the
last body expression.


\subsection{\keyword{param-lambda-automatic}}
\index{\keyword{param-lambda-automatic}}
\label{subsec:expr-param-lambda-automatic}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{param-lambda-automatic}
  \slp \indexedsyntaxvar{type}{1} ... \indexedsyntaxvar{type}{n} \srp
  \slp \syntaxvar{argument-list}
  \syntaxvar{attribute-list} \srp
  \syntaxspace \indexedsyntaxvar{body-expr}{1}, \sldots,
  \indexedsyntaxvar{body-expr}{n} \srp \\
  \\
  \indexedsyntaxvar{type}{k} \syntaxdefequal
  \syntaxelement{identifier} \\
  \syntaxvar{argument-list} \syntaxdefequal \slp
  \syntaxopt{\indexedsyntaxvar{arg}{1} \sldots
    \indexedsyntaxvar{arg}{n}} \srp \\
  \indexedsyntaxvar{arg}{k} \syntaxdefequal (\indexedsyntaxvar{arg-name}{k}
  \indexedsyntaxvar{arg-type}{k}) \\
  \indexedsyntaxvar{arg-name}{k} \syntaxdefequal \syntaxelement{identifier} \\
  \attrlistdef
\end{syntaxblock}


This form works as \keyword{param-lambda} except the result type is
set to the type of the last body expression.


\subsection{\keyword{prim-proc} and
  \keyword{unchecked-prim-proc}}
\label{subsec:expr-prim-proc}
\index{\keyword{prim-proc}}
\index{\keyword{unchecked-prim-proc}}


Syntax:
\begin{syntaxblock}
  \slp \mlp \skeyword{prim-proc} \syntaxor
  \skeyword{unchecked-prim-proc} \mrp
  \syntaxvar{procedure-name}
  \syntaxvar{argument-list}
  \syntaxvar{result-type}
  \syntaxvar{attribute-list}
  \srp \\
  \\
  \syntaxvar{procedure-name} \syntaxdefequal \syntaxelement{identifier}
  \\
  \syntaxvar{argument-list} \syntaxdefequal
  \slp \syntaxopt{\indexedsyntaxvar{arg-type}{1} \sldots
    \indexedsyntaxvar{arg-type}{n}} \srp \\
  \attrlistdef
\end{syntaxblock}


With the current \theme\ implementation
the target platform is Scheme (guile 2.0) and
\keyword{prim-proc}
defines a \theme\ procedure that calls a Scheme procedure
\syntaxvarf{procedure-name}.
Expressions \indexedsyntaxvarf{arg-type}{k} have to be static type
expressions.
If \syntaxvarf{result-type} is not
\builtin{<none>} the \theme\ procedure also checks that the
value returned from the Scheme procedure is an instance of
\syntaxvarf{result-type}.
The semantics of \keyword{unchecked-prim-proc} is similar to
\keyword{prim-proc} except
\keyword{unchecked-prim-proc} generates no run-time type
checks for the result value.


\subsection{\keyword{param-prim-proc} and
  \keyword{unchecked-param-prim-proc}}
\label{subsec:expr-param-prim-proc}
\index{\keyword{param-prim-proc}}
\index{\keyword{unchecked-param-prim-proc}}


Syntax:
\begin{syntaxblock}
  \slp \mlp \skeyword{param-prim-proc} \syntaxor
  \skeyword{unchecked-param-prim-proc} \mrp
  \syntaxvar{procedure-name}
  \syntaxvar{type-parameter-list}
  \syntaxvar{argument-list}
  \syntaxvar{result-type} \syntaxvar{attribute-list} \srp \\
  \\
  \syntaxvar{procedure-name} \syntaxdefequal \syntaxelement{identifier}
  \\
  \syntaxvar{type-parameter-list} \syntaxdefequal
  \slp \syntaxopt{\indexedsyntaxvar{param}{1} \sldots
    \indexedsyntaxvar{param}{m}} \srp \\
  \indexedsyntaxvar{param}{k} \syntaxdefequal
  \syntaxelement{identifier} \\
  \syntaxvar{argument-list} \syntaxdefequal
  \slp \syntaxopt{\indexedsyntaxvar{arg-type}{1} \sldots
    \indexedsyntaxvar{arg-type}{n}} \srp \\
  \attrlistdef
\end{syntaxblock}


With the current \theme\ implementation
the target platform is Scheme (guile 2.0) and
\keyword{param-prim-proc}
defines a \theme\ parametrized procedure that calls a Scheme procedure
\syntaxvarf{procedure-name}.
Expressions \indexedsyntaxvarf{arg-type}{k} have to be static type
expressions.
If \syntaxvarf{result-type} is not
\builtin{<none>} the \theme\ procedure also checks that the
value returned from the Scheme procedure is an instance of
\syntaxvarf{result-type}.
The semantics of \keyword{unchecked-param-prim-proc} is similar to
\keyword{param-prim-proc} except
\keyword{unchecked-prim-proc} generates no run-time type
checks for the result value.


\section{Type Operations}


\subsection{\keyword{cast}}
\index{\keyword{cast}}
\label{subsec:cast}


Syntax:
\begin{syntaxblock}
  \slp
  \skeyword{cast} \syntaxvar{type} \objectvar{casted-value}
  \srp
\end{syntaxblock}


The value of the \keyword{cast} expression is the value of expression
\objectvarf{casted-value}.
The static type of the \keyword{cast} expression is the value of
\syntaxvarf{type}. Expression \syntaxvarf{type} has to be a static type
expression. It is an error (translation time or run-time) if the
result value of \objectvarf{casted-value} is not an instance of
\syntaxvarf{type}. See also subsection \ref{subsec:try-cast}.


\subsection{\keyword{try-cast}}
\index{\keyword{try-cast}}
\label{subsec:try-cast}

Syntax:
\begin{syntaxblock}
  \slp
  \skeyword{try-cast} \syntaxvar{type} \objectvar{casted-value}
  \objectvar{default-value}
  \srp
\end{syntaxblock}


If \objectvarf{casted-value} is an instance of \syntaxvarf{type}
return \objectvarf{casted-value}. Otherwise return
\objectvarf{default-value}.  The static type of the \keyword{try-cast}
expression is the union of \syntaxvarf{type} and the type of
\objectvarf{default-value}. Expression \syntaxvarf{type} has to be a
static type expression. See also subsection \ref{subsec:cast}.


\subsection{\keyword{static-cast}}
\index{\keyword{static-cast}}
\label{subsec:static-cast}


Syntax:
\begin{syntaxblock}
  \slp
  \skeyword{static-cast} \syntaxvar{type} \objectvar{casted-value}
  \srp
\end{syntaxblock}


The value of the \keyword{static-cast} expression is the value of expression
\objectvarf{casted-value}.
The static type of the \keyword{cast} expression is the value of
\syntaxvarf{type}. Expression \syntaxvarf{type} has to be a static type
expression. It is a translation time error if the
static type of \objectvarf{casted-value} is not a subtype of
\syntaxvarf{type}. However, a nonreturning (\code{never-returns})
expression may be casted statically to any type.


\subsection{\keyword{force-pure-expr}}
\index{\keyword{force-pure-expr}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{force-pure-expr} \syntaxvar{expr}
  \srp
\end{syntaxblock}


This form makes the component expression \syntaxvarf{expr} to appear
pure for \theme. See section \ref{sec:creators} for example usage of
the form.


\subsection{\keyword{match-type}}
\index{\keyword{match-type}}
\label{subsec:match-type}


Syntax:
\begin{syntaxblock}
  \slp
  \skeyword{match-type}
  \syntaxvar{value-to-match}
  \syntaxopt{\syntaxvar{clause-list}}
  \syntaxopt{\syntaxvar{else-clause}}
  \srp \\
  \\
  \syntaxvar{clause-list} \syntaxdefequal
  \indexedsyntaxvar{clause}{1},
  \sldots,
  \indexedsyntaxvar{clause}{n} \\
  \indexedsyntaxvar{clause}{k} \syntaxdefequal \slp
  \indexedsyntaxvar{match-spec}{k}
  \indexedsyntaxvar{expr}{k,1}, \sldots,
  \indexedsyntaxvar{expr}{k,m(k)} \srp \\
  \indexedsyntaxvar{match-spec}{k} \syntaxdefequal
  \slp
  \indexedsyntaxvar{var}{k} \indexedsyntaxvar{type}{k}
  \srp
  \syntaxor
  \slp
  \indexedsyntaxvar{type}{k}
  \srp \\
  \syntaxvar{else-clause} \syntaxdefequal \slp \skeyword{else}
  \indexedsyntaxvar{else-expr}{1}, \sldots,
  \indexedsyntaxvar{else-expr}{p} \srp
\end{syntaxblock}


Each clause is processed in order. If \indexedsyntaxvarf{var}{k} is
given and the runtime type of \syntaxvarf{value-to-match} is a subtype
of \indexedsyntaxvarf{type}{k} then bind \indexedsyntaxvar{var}{k} to
\syntaxvarf{value-to-match}, evaluate expressions
\indexedsyntaxvarf{expr}{k,1}, \sldots,
\indexedsyntaxvarf{expr}{k,m(k)} is order and return the value of the
last expression.  The static type of \indexedsyntaxvar{var}{k} is
\indexedsyntaxvarf{type}{k}.  If \indexedsyntaxvarf{var}{k} is not
given and the runtime type of \syntaxvarf{value-to-match} is a subtype
of \indexedsyntaxvarf{type}{k} then evaluate expressions
\indexedsyntaxvarf{expr}{k,1}, \sldots,
\indexedsyntaxvarf{expr}{k,m(k)} is order and return the value of the
last expression.
If none of the types matches and \syntaxvarf{else-clause} is present
evaluate the expressions \indexedsyntaxvarf{else-expr}{1}, \sldots,
\indexedsyntaxvarf{else-expr}{p} and return the value of the last
expression.

Let \(K\) be an integer between \(1\) and \(n\) and \syntaxvarf{u} the
union of types \indexedsyntaxvarf{type}{k}, \(k = 1, \ldots, K\).
If the static type of \syntaxvarf{value-to-match} is a subtype of
\syntaxvarf{u} the following optimizations are done:
\begin{itemize}
\item The clause \indexedsyntaxvarf{clause}{K} needs no runtime type
  check because we already known that the type of
  \syntaxvarf{value-to-match} is a subtype of
  \indexedsyntaxvarf{type}{K}.
\item
  If all the type checks \(k = 1, \ldots, K - 1\) fail the expressions
  of clause \(K\) are automatically evaluated. Consequently the
  clauses \(k > K\) and the else clause need not be compiled.
\end{itemize}


\subsection{\keyword{match-type-strong}}
\index{\keyword{match-type-strong}}
\label{subsec:match-type-strong}


This form is identical to \keyword{match-type} except a translation
time or runtime error is signalled in case \syntaxvarf{value-to-match}
matches none of the clauses. Generally, the error is a translation
time exception except when the \keyword{match-type-strong} expression
is invoked inside a definition of a parametrized method.


\subsection{\keyword{static-type-of}}
\index{\keyword{static-type-of}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{static-type-of} \syntaxvar{expression} \srp
\end{syntaxblock}


This form returns the static type of \syntaxvarf{expression}. This
computation is done compile time and \syntaxvarf{expression} is not
evaluated run time.


\subsection{\keyword{\tupletype}}
\index{\keyword{\tupletype}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{\tupletype}
  \indexedsyntaxvar{a}{1} \sldots \indexedsyntaxvar{a}{n}
  \srp
\end{syntaxblock}


Let
\begin{syntaxformula}
  \syntaxvar{u} \syntaxdefequal \slp \indexedobjectvar{t}{1} \sldots
  \indexedobjectvar{t}{m} \srp
\end{syntaxformula}
be the translated argument list generated from
\begin{syntaxformula}
  \indexedsyntaxvar{a}{1} \sldots \indexedsyntaxvar{a}{n}
\end{syntaxformula}.
Object of type \syntaxvarf{u} is a list with element
types
\begin{syntaxformula}
  \indexedobjectvar{t}{1} \sldots \indexedobjectvar{t}{m}
\end{syntaxformula}.
Expression
\begin{syntaxformula}
  \slp \skeyword{\tupletype}
  \indexedsyntaxvar{a}{1} \sldots \indexedsyntaxvar{a}{n}
  \srp
\end{syntaxformula}
is equivalent to
\begin{syntaxformula}
  \slp \sbuiltin{:pair} \indexedobjectvar{t}{1}
  \slp \sbuiltin{:pair} \indexedobjectvar{t}{2}
  \slp \sldots \slp \sbuiltin{:pair} \indexedobjectvar{t}{n}
  \sbuiltin{<null>} \srp \sldots \srp \srp \srp
\end{syntaxformula}.


\section{Object Creation}


\subsection{\keyword{constructor}}
\index{\keyword{constructor}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{constructor} \objectvar{class} \srp
\end{syntaxblock}


Expression \objectvarf{class} has to be a static type expression and
its value has to be a class.
The value of a \keyword{constructor} expression is the constructor (a
simple procedure) of class \objectvarf{class}.


\subsection{\keyword{quote}}
\index{\keyword{quote}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{quote} \syntaxvar{quoted-expression} \srp
\end{syntaxblock}


Keyword \keyword{quote} is used to create atom and list constants as
in Scheme.


\subsection{\keyword{zero}}
\index{\keyword{zero}}
\label{subsec:expr-zero}


Syntax:
\begin{syntaxblock}
  \slp \keyword{zero} \syntaxvar{class} \srp
\end{syntaxblock}


Keyword \keyword{zero} accesses the zero value of a class.
It is an error to use \keyword{zero} for a class that does not define
a zero value.


\chapter{Special Procedures}


Special procedures are procedures that are treated specially be
\theme\ compiler and linker. They are typically parametrized
procedures whose typing cannot be expressed in current \theme.
Note that the types we give for the arguments of the special
procedures do not generally describe all the requirements the special
procedures have for argument types.
The application procedures \builtin{apply} and \builtin{apply-nonpure},
could be implemented in
\theme\ but they have been included in the core language because of
optimization.


\section{Equality Predicates}


Generic procedure \code{equal?}, which is the main equality predicate,
is defined in the standard library. The user is free to add methods to
it.


\subsection{\builtin{equal-values?}}
\index{\builtin{equal-values?}}


\startprocsyntax
\begin{codeblock}
(equal-values? object1 object2)
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{object1}{\builtin{<object>}}{An object to be compared}
  \argument{object2}{\builtin{<object>}}{An object to be compared}
\end{arglist}


\procresult{\code{\#t} iff \code{object1} is equal to \code{object2}}%
{\code{<boolean>}}


\procpurity{pure}


This is the main equality predicate in \theme. This procedure
implements algorithm \algorithmname{EqualValues?}, see section
\ref{subsubsec:EqualValues?}.
Name \code{=} is an alias for \code{equal-values?}.


\subsection{\builtin{equal-objects?}}
\index{\builtin{equal-objects?}}


\startprocsyntax
\begin{codeblock}
  (equal-objects? object1 object2)
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{object1}{\builtin{<object>}}{An object to be compared}
  \argument{object2}{\builtin{<object>}}{An object to be compared}
\end{arglist}


\procresult{\code{\#t} iff \code{object1} is the same object as \code{object2}}%
{\builtin{<boolean>}}


\procpurity{pure}


This procedure implements algorithm \algorithmname{EqualObjects?}, see
section \ref{subsubsec:EqualObjects?}.


\subsection{\builtin{equal-contents?}}
\index{\builtin{equal-contents?}}


\startprocsyntax
\begin{codeblock}
(equal-contents? object1 object2)
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{object1}{\builtin{<object>}}{An object to be compared}
  \argument{object2}{\builtin{<object>}}{An object to be compared}
\end{arglist}


\procresult{\code{\#t} iff the contents of \code{object1} are equal to
  the contents of \code{object2}}%
{\code{<boolean>}}


\procpurity{pure}


This procedure implements algorithm \algorithmname{EqualContents?},
see section \ref{subsubsec:EqualContents?}.


\section{Control Structures}


\subsection{\builtin{apply}}
\index{\builtin{apply}}


\startprocsyntax
\begin{codeblock}
  (apply \procarg{procedure} \procarg{argument-list})
\end{codeblock}


\typeparams{\typeparam{arglist}, \typeparam{result}}


\startprocarguments
\begin{arglist}
\argument{procedure}{\code{(\builtin{:procedure} ((\keyword{splice} %
    \typeparam{arglist})) \typeparam{result} %
    pure)}}%
{procedure to be called}
\argument{argument-list}{\code{\typeparam{arglist}}}%
{arguments to be passed}
\end{arglist}


\procresult{The value returned from \procarg{procedure}}%
{The result type of \procarg{procedure}}


\procpurity{pure}


The type of \procarg{argument-list} has to be a subtype of the argument
list type of \procarg{procedure}. Procedure \procarg{procedure} has to be pure.
Procedure \builtin{apply} calls \procarg{procedure} with the arguments
from \procarg{argument-list}. This is similar to Scheme \code{apply}.


\subsection{\builtin{apply-nonpure}}
\index{\builtin{apply-nonpure}}


\startprocsyntax
\begin{codeblock}
(\code{apply-nonpure} \procarg{procedure} \procarg{argument-list})
\end{codeblock}


\typeparams{\typeparam{arglist}, \typeparam{result}}


\startprocarguments
\begin{arglist}
  \argument{procedure}{\code{(\builtin{:procedure} ((\keyword{splice} %
      \typeparam{arglist})) \typeparam{result} %
      nonpure)}}%
  {procedure to be called}
  \argument{argument-list}{\code{\typeparam{arglist}}}%
  {arguments to be passed}
\end{arglist}


\procresult{The value returned from \procarg{procedure}}%
{The result type of \procarg{procedure}}


\procpurity{nonpure}


The type of \procarg{argument-list} has to be a subtype of the argument
list type of \procarg{procedure}. Procedure \procarg{procedure} may be pure
or nonpure.
Procedure \builtin{apply-nonpure} calls \procarg{procedure} with the arguments
from \procarg{argument-list}. This is similar to Scheme \code{apply}.


\subsection{\builtin{field-ref}}
\index{\builtin{field-ref}}


\startprocsyntax
\begin{codeblock}
  (\builtin{field-ref} \procarg{object} \procarg{field-name})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{object}{\builtin{<object>}}{object whose field is accessed}
  \argument{field-name}{\builtin{<symbol>}}{name of the field to be accessed}
\end{arglist}


\procresult{Value of the field}{Type of the field}


\procpurity{pure}


Argument \procarg{field-name} has to be a literal symbol.


\subsection{\builtin{field-set!}}
\index{\builtin{field-set{"!}}}


\startprocsyntax
\begin{codeblock}
  (\builtin{field-ref} \procarg{object} \procarg{field-name}
  \procarg{field-value})
\end{codeblock}


\startprocarguments
\begin{arglist}
\argument{object}{\builtin{<object>}}{object whose field is to be set}
\argument{field-name}{\builtin{<symbol>}}{name of the field to be set}
\argument{field-value}{\builtin{<object>}}{new value of the field}
\end{arglist}


\procresult{No result value}{\builtin{<none>}}


\procpurity{nonpure}


Argument \procarg{field-name} has to be a literal symbol. Argument
\procarg{field-value} has to be an instance of the type of the field.


\section{Type Operations}


\subsection{\builtin{class-of}}
\index{\builtin{class-of}}


\startprocsyntax
\begin{codeblock}
(\builtin{class-of} \procarg{object})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{object}{\builtin{<object>}}%
  {the object whose class is accessed}
\end{arglist}


\procresult{Class of the object}{\builtin{<class>}}


\procpurity{pure}


\subsection{\builtin{is-instance?}}
\index{\builtin{is-instance?}}


\startprocsyntax
\begin{codeblock}
  (\builtin{is-instance?} \procarg{object} \proctypearg{type})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{object}{\builtin{<object>}}{An object whose type is checked}
  \typeargument{type}{\builtin{<type>}}{A type}
\end{arglist}


\procresult{\code{\#t} iff \procarg{object} is an instance of \proctypearg{type}}%
{\builtin{<boolean>}}


\procpurity{pure}


Argument \proctypearg{type} has to be a static type expression.
Expression
\begin{codeblock}
  (\builtin{is-instance?} \procarg{object} \proctypearg{type})
\end{codeblock}
is equivalent to
\begin{codeblock}
  (\builtin{is-subtype?} (\builtin{class-of} \procarg{object})
  \proctypearg{type})
\end{codeblock}


\subsection{\builtin{is-subtype?}}
\index{\builtin{is-subtype?}}


\startprocsyntax
\begin{codeblock}
  (\builtin{is-subtype?} \indexedproctypearg{type}{1} \indexedproctypearg{type}{2})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \indexedtypeargument{type}{1}{\builtin{<type>}}{A type}
  \indexedtypeargument{type}{2}{\builtin{<type>}}{A type}
\end{arglist}


\procresult{\code{\#t} iff \indexedproctypearg{type}{1} is a subtype of %
  \indexedproctypearg{type}{2}}%
{\builtin{<boolean>}}


\procpurity{pure}


Arguments \indexedproctypearg{type}{1} and \indexedproctypearg{type}{2} have to be a static
type expressions.


\section{Vector Operations}


\subsection{\builtin{cast-mutable-value-vector}}
\index{\builtin{cast-mutable-value-vector}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-mutable-value-vector} \proctypearg{target-element-type}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{target-element-type}{\builtin{<type>}}%
  {Element type of the new vector}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new element type}%
{\code{(\builtin{:mutable-value-vector} \proctypearg{target-element-type})}}


\procpurity{pure}


Special procedure \builtin{cast-mutable-value-vector} creates a copy of the
source vector and checks that each of its elements is an instance of
\proctypearg{target-element-type}. The check is generally done run time.
The vector metaclass may change in the cast.


\subsection{\builtin{cast-mutable-value-vector-metaclass}}
\index{\builtin{cast-mutable-value-vector-metaclass}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-mutable-value-vector-metaclass}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new metaclass}%
{\code{(\builtin{:mutable-value-vector} \proctypearg{element-type})}}


\procpurity{pure}


Special procedure \builtin{cast-mutable-value-vector-metaclass} creates a copy of the
source vector so that the copy has the class
\code{(:mutable-value-vector \proctypearg{element-type})} where
\proctypearg{element-type} is the element type of the original vector.


\subsection{\builtin{cast-mutable-vector}}
\index{\builtin{cast-mutable-vector}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-mutable-vector} \proctypearg{target-element-type}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{target-element-type}{\builtin{<type>}}%
  {Element type of the new vector}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new element type}%
{\code{(\builtin{:mutable-vector} \proctypearg{target-element-type})}}


\procpurity{pure}


Special procedure \builtin{cast-mutable-vector} creates a copy of the
source vector and checks that each of its elements is an instance of
\proctypearg{target-element-type}. The check is generally done run time.
The vector metaclass may change in the cast.


\subsection{\builtin{cast-mutable-vector-metaclass}}
\index{\builtin{cast-mutable-vector-metaclass}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-mutable-vector-metaclass}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new metaclass}%
{\code{(\builtin{:mutable-vector} \proctypearg{element-type})}}


\procpurity{pure}


Special procedure \builtin{cast-mutable-vector-metaclass} creates a copy of the
source vector so that the copy has the class
\code{(:mutable-vector \proctypearg{element-type})} where
\proctypearg{element-type} is the element type of the original vector.


\subsection{\builtin{cast-value-vector}}
\index{\builtin{cast-value-vector}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-value-vector} \proctypearg{target-element-type}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{target-element-type}{\builtin{<type>}}%
  {Element type of the new vector}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new element type}%
{\code{(\builtin{:value-vector} \proctypearg{target-element-type)}}}


\procpurity{pure}


Special procedure \builtin{cast-value-vector} creates a copy of the
source vector and checks that each of its elements is an instance of
\proctypearg{target-element-type}. The check is generally done run time.
The vector metaclass may change in the cast.


\subsection{\builtin{cast-value-vector-metaclass}}
\index{\builtin{cast-value-vector-metaclass}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-value-vector-metaclass}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new metaclass}%
{\code{(\builtin{:value-vector} \proctypearg{element-type})}}


\procpurity{pure}


Special procedure \builtin{cast-value-vector-metaclass} creates a copy of the
source vector so that the copy has the class
\code{(:value-vector \proctypearg{element-type})} where
\proctypearg{element-type} is the element type of the original vector.


\subsection{\builtin{cast-vector}}
\index{\builtin{cast-vector}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-vector} \proctypearg{target-element-type}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{target-element-type}{\builtin{<type>}}%
  {Element type of the new vector}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new element type}%
{\code{(\builtin{:vector} \proctypearg{target-element-type})}}


\procpurity{pure}


Special procedure \builtin{cast-vector} creates a copy of the
source vector and checks that each of its elements is an instance of
\proctypearg{target-element-type}. The check is generally done run time.
The vector metaclass may change in the cast.


\subsection{\builtin{cast-vector-metaclass}}
\index{\builtin{cast-vector-metaclass}}


\startprocsyntax
\begin{codeblock}
  (\builtin{cast-vector-metaclass}
  \procarg{source-vector})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{source-vector}{any vector class}%
  {The vector to be casted}
\end{arglist}


\procresult{A copy of the source vector with the new metaclass}%
{\code{(\builtin{:vector} \proctypearg{element-type})}}


\procpurity{pure}


Special procedure \builtin{cast-vector-metaclass} creates a copy of the
source vector so that the copy has the class
\code{(:vector \proctypearg{element-type})} where
\proctypearg{element-type} is the element type of the original vector.


\subsection{\builtin{make-mutable-value-vector}}
\index{\builtin{make-mutable-value-vector}}
\label{subsec:sp-make-mutable-value-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{make-mutable-value-vector} \proctypearg{element-type}
   \procarg{nr-of-elements} \procarg{element-value})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{Type of the vector %
    elements}
  \argument{nr-of-elements}{\builtin{<integer>}}{Number of elements in %
    the new vector}
  \argument{element-value}{\builtin{<object>}}{Value with which the new %
    vector is filled}
\end{arglist}


\procresult{A mutable value vector of \procarg{nr-of-elements} elements %
  with value \procarg{element-value}}%
{\code{(\builtin{:mutable-value-vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Argument \procarg{element-value} has to be an
instance of \proctypearg{element-type}.


\subsection{\builtin{make-mutable-vector}}
\index{\builtin{make-mutable-vector}}
\label{subsec:sp-make-mutable-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{make-mutable-vector} \proctypearg{element-type}
  \procarg{nr-of-elements} \procarg{element-value})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{Type of the vector %
    elements}
  \argument{nr-of-elements}{\builtin{<integer>}}{Number of elements in %
    the new vector}
  \argument{element-value}{\builtin{<object>}}{Value with which the new %
    vector is filled}
\end{arglist}


\procresult{A mutable vector of \procarg{nr-of-elements} elements with %
  value \procarg{element-value}}%
{\code{(\builtin{:mutable-vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Argument \procarg{element-value} has to be an
instance of \proctypearg{element-type}.


\subsection{\builtin{make-value-vector}}
\index{\builtin{make-value-vector}}
\label{subsec:sp-make-value-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{make-value-vector} \proctypearg{element-type}
  \procarg{nr-of-elements} \procarg{element-value})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{Type of the vector %
    elements}
  \argument{nr-of-elements}{\builtin{<integer>}}{Number of elements in %
    the new vector}
  \argument{element-value}{\builtin{<object>}}{Value with which the new %
    vector is filled}
\end{arglist}


\procresult{A value vector of \procarg{nr-of-elements} elements with value %
  \procarg{element-value}}%
{\code{(\builtin{:value-vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Argument \procarg{element-value} has to be an
instance of \proctypearg{element-type}.

%% We probably will not do the following:
%%  The \procarg{element-type} argument will
%%  be removed in the future.


\subsection{\builtin{make-vector}}
\index{\builtin{make-vector}}
\label{subsec:sp-make-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{make-vector} \proctypearg{element-type}
  \procarg{nr-of-elements} \procarg{element-value})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{Type of the vector %
    elements}
  \argument{nr-of-elements}{\builtin{<integer>}}{Number of elements in %
    the new vector}
  \argument{element-value}{\builtin{<object>}}{Value with which the new %
    vector is filled}
\end{arglist}


\procresult{A vector of \procarg{nr-of-elements} elements with value %
  \procarg{element-value}}%
{\code{(\builtin{:vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Argument \procarg{element-value} has to be an
instance of \proctypearg{element-type}.
%% We probably will not do the following:
%%  The \procarg{element-type} argument will
%%  be removed in the future.


\subsection{\builtin{mutable-value-vector}}
\index{\builtin{mutable-value-vector}}
\label{subsec:sp-mutable-value-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{mutable-value-vector} \proctypearg{element-type}
  \indexedprocarg{element}{1} ... \indexedprocarg{element}{n})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{The element type of the new vector}
  \indexedargument{element}{k}{\builtin{<object>}}{An element of the new vector}
\end{arglist}


\procresult{A mutable value vector with element type %
  \proctypearg{element-type} and %
  elements \indexedprocarg{element}{1}, ..., \indexedprocarg{element}{n}}%
{\code{(\builtin{:mutable-value-vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Each \indexedprocarg{element}{k} has to be an instance of
\proctypearg{element-type}.


\subsection{\builtin{mutable-vector}}
\index{\builtin{mutable-vector}}
\label{subsec:sp-mutable-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{mutable-vector} \proctypearg{element-type} \indexedprocarg{element}{1}
  ... \indexedprocarg{element}{n})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{The element type of the new vector}
  \indexedargument{element}{k}{\builtin{<object>}}{An element of the new vector}
\end{arglist}


\procresult{A mutable vector with element type \code{element-type} and %
  elements \indexedprocarg{element}{1}, ..., \indexedprocarg{element}{n}}%
{\code{(\builtin{:mutable-vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Each \indexedprocarg{element}{k} has to be an instance of \proctypearg{element-type}.


\subsection{\builtin{value-vector}}
\index{\builtin{value-vector}}
\label{subsec:sp-value-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{value-vector} \proctypearg{element-type}
  \indexedprocarg{element}{1} ... \indexedprocarg{element}{n})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{The element type of the new vector}
  \indexedargument{element}{k}{\builtin{<object>}}{An element of the new vector}
\end{arglist}


\procresult{A value vector with element type \code{element-type} and %
  elements \indexedprocarg{element}{1}, ..., \indexedprocarg{element}{n}}%
{\code{(\builtin{:value-vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Each \indexedprocarg{element}{k} has to be an instance of \proctypearg{element-type}.


\subsection{\builtin{vector}}
\index{\builtin{vector}}
\label{subsec:sp-vector}


\startprocsyntax
\begin{codeblock}
  (\builtin{vector} \proctypearg{element-type}
  \indexedprocarg{element}{1} ... \indexedprocarg{element}{n})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{element-type}{\builtin{<type>}}{The element type of the new vector}
  \indexedargument{element}{k}{\builtin{<object>}}{An element of the new vector}
\end{arglist}



\procresult{A vector with element type \code{element-type} and %
  elements \indexedprocarg{element}{1}, ..., \indexedprocarg{element}{n}}%
{\code{(\builtin{:vector} \proctypearg{element-type})}}


\procpurity{pure}


Argument \proctypearg{element-type} has to be a static type
expression.
Each \indexedprocarg{element}{k} has to be an instance of \proctypearg{element-type}.


\section{Tuple Operations}


\subsection{\builtin{tuple-ref}}
\index{\builtin{tuple-ref}}


\startprocsyntax
\begin{codeblock}
  (\cbuiltin{tuple-ref} \procarg{tuple} \procarg{index})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \argument{tuple}{\code{(\tupletype \indexedsyntaxvarf{t}{1} %
      ... \indexedsyntaxvarf{t}{n})}}{A tuple}
  \argument{index}{\builtin{<integer>}}{Index of the element wanted}
\end{arglist}


\procresult{The element of \code{tuple} at position \code{index}}%
{\indexedsyntaxvarf{t}{\texttt{index}}}


The indices of a tuple start from zero.


\subsection{\builtin{tuple-type-with-tail}}
\index{\builtin{tuple-type-with-tail}}


\startprocsyntax
\begin{codeblock}
  (\cbuiltin{tuple-type-with-tail} \proctypearg{tuple-t} \proctypearg{tail-t})
\end{codeblock}


\startprocarguments
\begin{arglist}
  \typeargument{tuple-t}{\builtin{<type>}}{A tuple type}
  \typeargument{tail-t}{\builtin{<type>}}{A type}
\end{arglist}


\procresult{A list type constructed from \proctypearg{tuple-t} and %
  \proctypearg{tail-t}}{\builtin{<type>}}


Let
\begin{codeformula}
  \proctypearg{tuple-t}
\end{codeformula}
be a tuple type consisting of types
\begin{codeformula}
  \indexedsyntaxvarf{t}{1}, ..., \indexedsyntaxvarf{t}{n}
\end{codeformula}
and
\begin{codeformula}
  \proctypearg{tail-t}
\end{codeformula}
be a type.
Object of type
\begin{codeformula}
  (\cbuiltin{tuple-type-with-tail} \proctypearg{tuple-t} \proctypearg{tail-t})
\end{codeformula}
is a list with first \(n\) element types
\begin{codeformula}
  \indexedsyntaxvarf{t}{1}, ..., \indexedsyntaxvarf{t}{n}
\end{codeformula}
and the tail of the \(n\)th element
%%\begin{codeformula}
  \proctypearg{tail-t}.
%%\end{codeformula}.
Expression
\begin{codeformula}
  (\cbuiltin{tuple-type-with-tail} \proctypearg{tuple-t} \proctypearg{tail-t})
\end{codeformula}
is equivalent to
\begin{codeblock}
  (\cbuiltin{:pair} \indexedsyntaxvarf{t}{1} (\cbuiltin{:pair}
  \indexedsyntaxvarf{t}{2}
  ( ... ( \cbuiltin{:pair} \indexedsyntaxvarf{t}{n}
  \proctypearg{tail-t}) ... )))
\end{codeblock}


\chapter{Examples}


Subdirectory \code{theme-d/theme-d-code/examples} contains some
examples to illustrate the \theme\ programming language. Subdirectory
\code{theme-d/theme-d-code/tests} contains programs and modules used
to test the \theme\ system.


\section{Abstract Data Types}
\label{subsec:adts}


\index{abstract data type}


Abstract data types are data types for which the data type is defined
by specifying the operations (procedures) that the members of the data
type have to implement. In \theme\ ADT's can be implemented either by
using (parametrized) signatures or delegation.

We define the ADT's ``sequence'' and ``association'' as examples.
The following operations are implemented by every sequence class:
\begin{itemize}
  \item \code{sequence-length} that obtains the length of a sequence
  \item \code{sequence-ref} that obtains a sequence element at the
    given index
  \item \code{sequence-map} that maps a given procedure to a sequence
\end{itemize}
Associations resemble associations lists.
The following operations are implemented by every association class:
\begin{itemize}
  \item \code{gen-assoc} that obtains a value belonging to the given key
  \item \code{gen-assoc-set!} that adds a binding with given key and
    value into the association.
\end{itemize}


Files \code{sequence-sgn-test.thp}, \code{sequence-sgn.th?}, and
\code{sequence-list-impl.th?} contain an implementation of ADT
sequence implemented with parametrized signatures.
Files
\code{list-as-sequence.th?}, \code{vector-as-sequence.th?}, and
\code{sequence-test.thp} contain an implementation of ADT sequence
implemented by delegation.
Files \code{assoc-test.thp}, \code{assoc-test2.thp},
\code{assoc-list-impl.th?}, \code{assoc-sgn.th?},
\code{hash-table.th?} and \code{singleton.th?} contain and
implementation of ADT association implemented with parametrized
signatures.


\section{Creators (high-level constructors)}
\label{sec:creators}


\index{creator}


Sometimes the \theme\ constructors are not flexible enough to
construct instances. It is possible to emulate GOOPS-style
constructors in \theme. We give an example here.
Example code can be found from
\begin{codeblock}
  \cl{0.5cm}
  theme-d-code/examples/creators.thp
\end{codeblock}


First define a general purpose generic procedure and macro for creating
objects:
\begin{codeblock}
  \cl{0.5cm}
  (define-generic-proc initialize) \\
  \\
  \cl{0.5cm}
  (define-syntax create \\
  \cl{1.0cm}
    (syntax-rules () \\
  \cl{1.2cm}
      ((create clas arg ...) \\
  \cl{1.4cm}
       (force-pure-expr \\
  \cl{1.6cm}
	(let ((tmp (make clas))) \\
  \cl{1.8cm}
	  (initialize tmp arg ...) \\
  \cl{1.8cm}
	  tmp)))))
\end{codeblock}
\noindent
Next define some classes to be used:
\begin{codeblock}
  \cl{0.5cm}
  (define-class <graphics-context> <object> \#t \#f \#f public () \\
  \cl{1.0cm}
  ((i-x <integer> public public) \\
  \cl{1.2cm}
  (i-y <integer> public public) \\
  \cl{1.2cm}
  (i-width <integer> public public) \\
  \cl{1.2cm}
  (i-height <integer> public public))) \\
  \\
  \cl{0.5cm}
  (define-class <widget> <object> \#t \#f \#f public () \\
  \cl{1.0cm}
    ((context (:maybe <graphics-context>) public module null) \\
  \cl{1.2cm}
     (widget-parent (:maybe <widget>) public module null))) \\
  \\
  \cl{0.5cm}
  (define-class <label> <widget> \#t \#f \#f public () \\
  \cl{1.0cm}
  ((str-text <string> public module "")))
\end{codeblock}
\noindent
Define then the creator for the base class
\begin{codeblock}
  \cl{0.5cm}
  (define-simple-method initialize \\
  \cl{1.5cm}
    (((widget <widget>) (context <graphics-context>) \\
  \cl{1.9cm}
      (widget-parent (:maybe <widget>))) \\
  \cl{1.7cm}
     <none> nonpure) \\
  \cl{1.0cm}
    (logger-print "initialize widget") \\
  \cl{1.0cm}
    (field-set! widget 'context context) \\
  \cl{1.0cm}
    (field-set! widget 'widget-parent widget-parent))
\end{codeblock}
and for the derived class
\begin{codeblock}
  \cl{0.5cm}
  (define-simple-method initialize \\
  \cl{1.5cm}
    (((label <label>) (context <graphics-context>) \\
  \cl{1.7cm}
  (widget-parent <widget>) \\
  \cl{1.7cm}
      (str-text <string>)) \\
  \cl{1.5cm}
     <none> nonpure) \\
  \cl{1.0cm}
    (logger-print "initialize label") \\
  \cl{1.0cm}
    ((generic-proc-dispatch-without-result \\
  \cl{1.4cm}
      initialize \\
  \cl{1.4cm}
      (<widget> <graphics-context> <widget>) \\
  \cl{1.4cm}
      ()) \\
  \cl{1.2cm}
     label context widget-parent) \\
  \cl{1.0cm}
    (field-set! label 'str-text str-text))
\end{codeblock}
Now the instances of the classes can be created as follows:
\begin{codeblock}
  \cl{0.5cm}
    (let* ((widget-parent \\
  \cl{2.0cm}
	    (create <widget> \\
  \cl{2.3cm}
  (make <graphics-context> 100 100 400 200) \\
  \cl{2.3cm}
  null)) \\
  \cl{1.8cm}
	   (label \\
  \cl{2.0cm}
	    (create <label> \\
  \cl{2.3cm}
  (make <graphics-context> 100 100 200 50) \\
  \cl{2.3cm}
		    widget-parent \\
  \cl{2.3cm}
		    "Hello"))) \\
  \cl{0.7cm}
  ... )
\end{codeblock}
Corresponding example for parametrized classes can be found from
\begin{codeblock}
  \cl{0.5cm}
  \code{theme-d-code/examples/param-creators.thp}
\end{codeblock}


In order to define creators follow the following guidelines:
\begin{itemize}
  \item All the fields in the classes for which the creators are
    implemented shall define initial value. Note that you may have to
    declare types of some fields as \code{(:maybe <myclass>)} in order
    to allow \code{null} as the initial value.
  \item
    Define method \code{initialize} with first argument having the class
    for which the instances are created and possibly some other
    arguments.
  \item
    For each method \code{initialize} call the \code{initialize}
    method of the superclass by explicitly dispatching the method.
  \item
    When instances of the class are created its \code{initialize}
    method has to be called.
  \item
    If your creators have side effects other than setting the field
    values create separate macro
    \code{create-nonpure} and generic procedure
    \code{initialize-nonpure} for them:
    \begin{codeblock}
      \cl{0.5cm}
      (define-generic-proc initialize-nonpure) \\
      \\
      \cl{0.5cm}
      (define-syntax create-nonpure \\
      \cl{1.0cm}
      (syntax-rules () \\
      \cl{1.2cm}
      ((create clas arg ...) \\
      \cl{1.4cm}
      (let ((tmp (make clas))) \\
      \cl{1.6cm}
      (initialize-nonpure tmp arg ...) \\
      \cl{1.6cm}
      tmp)))))
    \end{codeblock}
    Methods \code{initialize-nonpure} may call methods \code{initialize}
    in super classes.
\end{itemize}


\section{Invoking the \keyword{match-type} Optimization}
\label{sec:match-type-opt}


\index{\keyword{match-type}}


Consider the following expression:
\begin{codeblock}
  \cl{0.5cm}
  (match-type \objectvar{x}
  \\
  \cl{1.0cm}
  ((\indexedobjectvar{var}{1} \indexedobjectvar{t}{1})
  \indexedobjectvar{clause}{1,1} \(\ldots\) \indexedobjectvar{clause}{1,n_1})
  \\
  \cl{1.5cm}
  \(\vdots\)
  \\
  \cl{1.0cm}
  ((\indexedobjectvar{var}{m} \indexedobjectvar{t}{m})
  \indexedobjectvar{clause}{m,1} \(\ldots\) \indexedobjectvar{clause}{m,n_m})
  \\
  \cl{1.5cm}
  \(\vdots\)
  \\
  \cl{1.0cm}
  ((\indexedobjectvar{var}{N} \indexedobjectvar{t}{N})
  \indexedobjectvar{clause}{N,1} \(\ldots\) \indexedobjectvar{clause}{N,n_N})
  \\
  \cl{1.0cm}
  (else 
  \indexedobjectvar{else-clause}{1} \(\ldots\)
  \indexedobjectvar{else-clause}{n_{\textrm{else}}})
\end{codeblock}
Suppose that the static type of \objectvarf{x} is a subtype of type
\code{(:union \indexedobjectvar{t}{1} \(\ldots\)
  \indexedobjectvar{t}{m})}.
If we arrive to the subexpression \(m\) we know that the type of
\objectvarf{x} is a subtype of \indexedobjectvarf{t}{m} and we need no
runtime typecheck for this.

For example, consider the following code of a mapping functional:
\begin{codeblock}
  \cl{0.5cm}
  (define-param-proc map1
  \\
  \cl{1.5cm}
      (\%argtype \%result-type)
  \\
  \cl{1.5cm}
      (((proc (:procedure (\%argtype) \%result-type pure))
  \\
  \cl{1.7cm}
  	(lst (:uniform-list \%argtype)))
  \\
  \cl{1.5cm}
       (:uniform-list \%result-type)
  \\
  \cl{1.5cm}
       pure)
  \\
  \cl{1.0cm}
    (match-type lst
  \\
  \cl{1.5cm}
      ((<null>) null)
  \\
  \cl{1.5cm}
      ((lst1 (:nonempty-uniform-list \%argtype))
  \\
  \cl{1.7cm}
       (cons (proc (car lst1))
  \\
  \cl{2.8cm}
  (map1 proc (cdr lst1))))))
\end{codeblock}
Now the clause for \code{lst1} needs no runtime type check. We only
need to check if \code{lst} is \code{null}.


\section{Purely Functional Iterators}


See \cite{baker1993} for discussion about purely functional
iterators. In \theme\ purely functional iterators are implemented in
module \code{(standard-library iterator)}. See program
\code{theme-d-code/tests/test470.thp} for a demonstration about
iterators.


\section{Exception handlers and \keyword{static-cast}}


Suppose that we have a class \code{<window>} and procedure
\code{create-window} that creates a window. Suppose also that the
procedure may raise an exception if it fails to create a window. We
may define the following procedure whose return type is
\code{<window>}:
\begin{codeblock}
  \cl{0.5cm}
  (define-simple-proc my-create-window (((str-caption <string>))
  \\
  \cl{2.5cm}
  <window> nonpure)
  \\
  \cl{1.5cm}
  (guard-general-nonpure exc
  \\
  \cl{2.5cm}
  (begin
  \\
  \cl{3.5cm}
  (console-display-line "system error")
  \\
  \cl{3.5cm}
  (static-cast <window> (exit 1)))
  \\
  \cl{2.5cm}
  (create-window str-caption)))
\end{codeblock}


\chapter{Comments}


\begin{itemize}
\item
  Consider the test program \code{(tests test29)} and the application
  of procedure \code{apply} at the end of the procedure
  \code{my-map}. Procedure \code{apply} applies parametrized procedure
  \code{my-map} and the application is dispatched runtime. If the
  components of the argument list \code{cdrs} are \code{null} the type
  parameter \code{\%arglist} in \code{my-map} cannot be deduced and
  the dispatch fails. Consequently we get a runtime error. A solution
  to this problem is to check that \code{cdrs} does not contain
  \code{null} values before the recursive application, see
  \code{(tests test30)}.
\item The type correctness of the implementations of parametrized
  methods cannot always be checked translation time.
\item
  Multiple inheritance is not going to be implemented in
  \theme. Single inheritance arises naturally from the memory layout
  of objects, i.e. a pointer to a derived class is also a pointer to
  the base class. This is not true in case of multiple inheritance.
\item
  The procedures implementing the DeduceXXX algorithms can be found
  in file \code{theme-d-type-system.scm}, procedures \code{deduce-xxx}.
\item
  The \theme\ runtime environment in
  \begin{codeblock}
    \cl{0.5cm}
    theme-d/runtime/runtime-theme-d-environment.scm
  \end{codeblock}
  also contains
  procedures implementing the
  SelectBestMatch algorithm since the procedure dispatch is usually
  done run-time.
\end{itemize}


\backmatter


\bibliographystyle{abbrv}

\bibliography{theme-d-language-manual}


\printindex


\end{document}
