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

\makeindex

\input commands.tex


\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 \copyright\ 2012-2025 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 the
functional programming language ocaml is located in
http://caml.inria.fr/.  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-proc (() <none> nonpure)\\
  \cl{2.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 or argument list consisting of one argument
  with type \code{(:uniform-list <string>)}.
\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}.

A module may declare other modules as \textit{friends}. See sections
\ref{sec:classes-and-logical-types} and \ref{subsec:friend}. If module
\objectvarf{A} declares module \objectvarf{B} as its friend the code
in module \objectvarf{B} may access the fields of the classes defined
in module \objectvarf{A} having \code{module} access.


\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}
\label{sec: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 and not a pair class
%%\code{:pair} or \code{:procedure}
is called a \defterm{simple class}. 
\index{simple class}
Every class that is not a pair class is called a \defterm{normal class}.
\index{normal 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{immutable}
\item \defterm{equality by value}
\end{itemize}
\index{class attributes} 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 and its
friend modules. 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.

The instances of a class are created with a special procedure called a
\defterm{constructor}.  See section \ref{subsec:define-class} for
information how to specify the constructor. See section
\ref{subsec:constructor-example} for examples about constructors.
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 follows the same rules as the access of
fields.  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}.

The inheritance access of a class may be either:
\begin{itemize}
  \item{\code{public}:} The class may be inherited anywhere
  \item{\code{module}:} The class may be inherited only in the module
    where it is defined and its friend modules
  \item{\code{hidden}:} The class may be not be inherited at all
\end{itemize}


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-vector \%number)) \\
  \cl{2.0cm}
               (v2 (:mutable-vector \%number))) \\
  \cl{2.0cm}
              (:mutable-vector \%number) \\
  \cl{2.0cm}
              (force-pure)) \\
  \cl{1.0cm}
    (let ((len1 (mutable-vector-length v1)) \\
  \cl{2.1cm}
          (len2 (mutable-vector-length v2))) \\
  \cl{1.5cm}
      (assert (= len1 len2)) \\
  \cl{1.5cm}
      (let ((result (make-mutable-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-vector-set! \\
  \cl{3.0cm}
           result i \\
  \cl{3.0cm}
           (+ (mutable-vector-ref v1 i) \\
  \cl{3.5cm}
              (mutable-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}.


\begin{figure}[h]
  \centering \includegraphics[scale=0.5]{simple-classes.pdf}
  \caption{Example inheritance hierarchy for simple classes.
     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.}	
  \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}.
\index{parametrized type}


\begin{figure}[h]
  \centering \includegraphics[scale=0.5]{param-classes.pdf}
  \caption{Example inheritance hierarchy for parametrized classes.
  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.}
  \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{Fundamental Classes}


\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.


\section{Primitive 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{<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.


\subsection{\code{<keyword>}}


\index{\code{<keyword>}} Keywords are similar to Guile keywords.
They are variables starting with \(\#:\) and they evaluate to themselves.
Class
\code{<keyword>} is immutable, equal by value, and not inheritable.


\section{Unit Types}
\index{unit type}

A unit type is a type that consists of a simple value of a primitive class. See
article \cite{wp-unit-type}. Unit types are created with syntax
\code{(:unit-type \objectvar{component})} where \objectvarf{component} is a
constant primitive object.

Unit types can be used to create enumerated types, e.g.
\begin{codeblock}
    \cl{0.5cm}
    (define <orientation> \\
    \cl{1.0cm}
    (:union (:unit-type 'horizontal) (:unit-type 'vertical)))
\end{codeblock}
The components of unit types are not restricted to symbols. For example, we can
define
\begin{codeblock}
    \cl{0.5cm}
    (define <bit> (:union (:unit-type 0) (:unit-type 1)))
\end{codeblock}
See example programs \code{orientation1.thp}, \code{orientation2.thp}, and
\code{bits.thp}.


\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{: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}}


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} and \builtin{:mutable-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.
Normal vector classes are equal by value.

\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.
Mutable vector classes are equal by value.

\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-foreign-prim-class}}
\index{\keyword{define-foreign-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-foreign-prim-class}, \keyword{define-foreign-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-foreign-prim-class}. See section
\ref{subsec:define-foreign-prim-class} and tests \code{test223},
\code{test224}, and \code{test226}. GOOPS classes may be imported into
Theme-D with keyword \keyword{define-foreign-goops-class}. See section
\ref{subsec:define-foreign-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.
The Theme-D compiler checks that the arguments to a GOOPS class \code{make}
expression have correct types. The compiler also checks that all slots having
no default value get initialized.


\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
  Let \(t'_1 := \algorithmappl{CheckNoneUnion}{t_1, \emptyset}\)
  and  \(t'_2 := \algorithmappl{CheckNoneUnion}{t_2, \emptyset}\).
\item
  Set \(M' := M \cup \{ (t'_1, t'_2) \}\).
\item
  If \(t'_1 = t'_2\) return \true.
\item
  If \stap\ and \stbp\ are type variables return \true\ iff \stap\ and
  \stbp\ are equal. If only one of them is a type variable return
  \false.
\item
  If \stap\ and \stbp\ are primitive classes then return \true\ iff they
  are the same object.
\item
  If \stap\ is not a signature and \stbp\ is a signature return
  \algorithmappl{IsSubtypeXSignature}{\stap, \stbp, \(M'\)}.
\item
  If \stap\ and \stbp\ is are signatures return
  \algorithmappl{IsSignatureSubtype}{\stap, \stbp, \(M'\)}.
\item
  If \stap\ is a signature and \stbp\ is a union return
  \algorithmappl{IsSubtypeXUnion}{\stap, \stbp, \(M'\)}.
\item
  If \stap\ is a signature and \(t'_2 = \mbuiltin{<object>}\) return \true.
\item
  If \stap\ is a signature and \stbp\ is not a signature return \false.
\item
  If \stap\ is a union return \algorithmappl{IsSubtypeUnionX}{\stap,
    \stbp, \(M'\)}.
\item
  If \stbp\ is a union return \algorithmappl{IsSubtypeXUnion}{\stap,
    \stbp, \(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 \(t'_2 = \mbuiltin{<object>}\) return \true.
\item
  If \(t'_1 = \mbuiltin{<object>}\) return \false.
\item
  If \stap\ and \stbp\ are unit types return \true\ iff their component
  objects are equal.
\item
  If \stap\ is a unit type but \stbp\ is not return \true\ iff the class of
  the component of \stap is equal to \stbp.
\item
  If \stbp\ is a unit type but \stap\ is not return \false.
\item If both \stap\ and \stbp\ are pair classes return
  \algorithmappl{IsSubtypePair}{\stap, \stbp, \(M'\)}.  If only one of
  \stap\ and \stbp\ is a pair class return \false.
\item
  If both \stap\ and \stbp\ are procedure types return
  \algorithmappl{IsSubtypeGeneralProc}{\stap, \stbp, \(M'\)}.
\item If \stap\ and \stbp\ are both vector classes, \stap\ =
  \code{(:vector <a>)} and \stbp\ = \code{(:vector <b>)}, return
  \algorithmappl{IsSubtype}{\code{<a>}, \code{<b>}, \(M'\)}.
\item
  If both \stap\ and \stbp\ are instances of a parametrized logical type
  whose type arguments contain type modifiers return \true\ iff the
  contents of \stap\ and \stbp\ are equal and \false\ otherwise.
\item
  If \stap\ and \stbp\ are splice expressions return \true\ iff the
  component type of \stap\ is a subtype of the component type of \stbp.
  If only \stbp\ is a splice expression return \false.
\item
  If \stap\ and \stbp\ are rest expressions return \true\ iff the
  component type of \stap\ is a subtype of the component type of \stbp.
  If only \stbp\ is a rest expression return \false.
\item
  If \stap\ and \stbp\ are type list expressions return
  \algorithmappl{IsGeneralListSubtype}{\objectvarf{a}, \objectvarf{b}}
  where \objectvarf{a} and \objectvarf{b} are the contents of
  \stap\ and \stbp\, respectively.  If only \stbp\ is a type list
  expression return \false.
\item
  If \stap\ and \stbp\ are type loop expressions return
  \algorithmappl{IsSubtypeLoop}{\stap, \stbp, \(M'\)}.  If only \stbp\ is
  a type loop expression return \false.
\item
  If \stap\ and \stbp\ are type join expressions return
  \algorithmappl{IsGeneralListSubtype}{\objectvarf{a}, \objectvarf{b}}
  where \objectvarf{a} and \objectvarf{b} are the contents of
  \stap\ and \stbp\, respectively.  If only \stbp\ is a type join
  expression return \false.
\item
  If \stap\ is a normal class and \stbp\ is an instance of a parametrized class return
  \algorithmappl{IsSubtypeParamClassInst}{\stap, \stbp, \(M'\)}.
\item If \stap\ and \stbp\ are normal classes return
  \algorithmappl{IsSubtypeSimple}{\stap, \stbp}.
\item If \stap\ and \stbp\ are \code{this} return \true. If only one of
  \stap\ and \stbp\ is \code{this} return \false.
\item else return \false.
\end{enumerate}


\subsection{\algorithmname{CheckNoneUnion}}


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


\noindent
\textit{\algresult} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{type?} :
\code{<none>} if \objectvarf{t} is a union containing \code{<none>} types,
\objectvarf{t} otherwise \\


\noindent
\textit{Algorithm:}
\algorithmappl{CheckNoneUnion}{\objectvarf{t}, \objectvarf{M}} \\


\begin{enumerate}
\item
  If \(t \in M\) return \true.
\item
  Set \(M' := M \cup \{ t \}\).
\item
  If \objectvarf{t} is a union type let \indexedobjectvarf{u}{k} be the component types
  of \objectvarf{t} and
  \begin{math}
    w_k := \algorithmappl{CheckNoneUnion}{u_k, M'}
  \end{math}.
  If any of the types \indexedobjectvarf{w}{k} is \code{<none>} return
  \code{<none>} else return \objectvarf{t}.
\item
  Otherwise return \objectvarf{t}.
\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}: an instance of a parametrized
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{IsSubtypeParamClassInst}{\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}. Algorithm \algorithmname{EqualTypes} is
used currently only in the equivalence predicates. Usually the equality of types
is compute by checking if both types are subtypes of each other.


\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 either of the objects is a primitive object or a pair use Scheme predicate
  \code{equal?} to compute the equivalence.
\item
  If \objectvarf{obj1} and \objectvarf{obj2} are the same nonprimitive
  object return \true.
\item
  If \objectvarf{obj1} and \objectvarf{obj2} are both \code{<class>} return
  \true. If only one of them is \code{<class>} return \false.  
\item
  If
  \begin{math}
    ( \mobjectvar{obj1} \; \mobjectvar{obj2} ) \in \mobjectvar{v}
  \end{math}
  return \true.
\item
  If both of the objects are union types the objects are equal iff they are both
  subtypes of each other. If only one of the objects is a union type return
  \false.
\item
  If both objects are parametrized class instances they are equal iff their
  metaclasses and type parameters are equal. If only one of the objects is a
  parametrized class instance return \false.
\item
  If both objects are normal classes (not pair classes) they are equal iff they
  are the same object. If only one of the objects is a normal class return
  \false.
\item
  If both objects are vectors they are equal iff
  \begin{itemize}
    \item their metaclasses are equal
    \item their component type are equal
    \item they have the same number of elements
    \item their elements are pairwise equal in the sense of
    \algorithmname{EqualValues?}
  \end{itemize}
  If only one of the objects is a vector return \false.
\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 the class of the objects is equal by value the objects are equal iff their
  field values are pairwise equal to each other in the sense of
  \algorithmname{EqualValues?}.
  Otherwise they are equal iff they are the same object.
\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 either of the objects is an primitive object or a pair use Scheme predicate
    \code{equal?} to compute the equivalence.
    \item
    If \objectvarf{obj1} and \objectvarf{obj2} are the same nonprimitive
    object return \true.
    \item
    If \objectvarf{obj1} and \objectvarf{obj2} are both \code{<class>} return
    \true. If only one of them is \code{<class>} return \false.  
    \item
    If
    \begin{math}
        ( \mobjectvar{obj1} \; \mobjectvar{obj2} ) \in \mobjectvar{v}
    \end{math}
    return \true.
    \item
    If both of the objects are union types the objects are equal iff they are both
    subtypes of each other. If only one of the objects is a union type return
    \false.
    \item
    If both objects are parametrized class instances they are equal iff their
    metaclasses and type parameters are equal. If only one of the objects is a
    parametrized class instance return \false.
    \item
    If both objects are normal classes (not pair classes) they are equal iff they
    are the same object. If only one of the objects is a normal class return
    \false.
    \item
    If both objects are vectors they are equal iff
    \begin{itemize}
      \item their metaclasses are equal
      \item their component type are equal
      \item they have the same number of elements
      \item their elements are pairwise equal in the sense of
      \algorithmname{EqualContents?}
    \end{itemize}
    If only one of the objects is a vector return \false.
    \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
    The objects are equal iff their field values are pairwise equal to each
    other in the sense of \algorithmname{EqualContents?}.
\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}} \\


If either of the objects is a null value, boolean, symbol, or keyword use Scheme
predicate \code{eq?} to compute the equivalence of the objects. Otherwise use
the Scheme predicate \code{eqv?}.


\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
  \begin{math}
    ( \mobjectvar{t1} \; . \; \mobjectvar{t2} ) \in \mobjectvar{v}
  \end{math}
  return \true.
\item
  If either of the arguments is a union type return \true\ iff
  \(t1 \minhsymb t2 \land t2 \minhsymb t1\).
\item
  If both of the objects are \code{<class>} return \true.
  If only one of the objects is \code{<class>} return \false.
\item
  If either of the objects is a primitive object return \true iff they are equal
  in the sense of Scheme predicate \code{eqv?}.
\item
  If either of the arguments is a primitive class or a custom primitive class
  return \true iff they are the same object.
\item
  Let
  \vprime := {\ttfamily{(cons (cons \objectvar{t1} \objectvar{t2}) \objectvarf{v})}}.
\item
  If \objectvarf{t1} and \objectvarf{t2} are both 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 one of \objectvarf{t1} or \objectvarf{t2} is a pair class return \false.
\item
  If the arguments are both vector classes return \true iff their metaclasses
  are the same object and their component types are equal in the sense of
  \algorithmappl{EqualTypes}. If only one of the arguments is a vector class
  return \false.
\item
  If \objectvarf{t1} and \objectvarf{t2} are normal 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 only one of \objectvarf{t1} or \objectvarf{t2} is an instance of a
    parametrized class return \false.
  \item
    If both of the argument are simple classes
    return \true iff they are the same object.
  \end{enumerate}
  \item
    If only one of the arguments is a simple class return \false.
  \item
    Otherwise return
    \begin{objectformula}
      \inherits{\objectvar{t1}}{\objectvar{t2}} \(\land\)
      \inherits{\objectvar{t2}}{\objectvar{t1}}
    \end{objectformula}.
\end{enumerate}


\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 four kinds of procedures: \defterm{simple
  procedures}, \defterm{normal generic procedures}, \defterm{virtual
  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. A generic
procedure is either normal or virtual.  Normal generic procedures are
lexically scoped and virtual generic procedures dynamically scoped.
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
compile-time for the normal generic procedures and run-time for
virtual generic procedures.  The dispatch of a virtual 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. In some
cases virtual generic procedure dispatch may be optimized with a
static dispatch.

Suppose that a virtual 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 virtual generic
procedure application at compile time.

\index{dynamic method} \index{static method} Methods can be either
\defterm{dynamic} or \defterm{static}.  All methods of a normal
generic procedure are 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 virtual generic procedures
with same name will be merged and the methods defined for each of the
merged virtual generic procedures will be put into the new virtual
generic procedure.

The class hierarchy for generic procedures is presented in figure
\ref{fig:gen-proc-class-hierarchy}.
Example class hierarchy for methods is presented in figure
\ref{fig:method-class-hierarchy}.


Atomic objects are dispatched by their class and not by their unit type. This is
so for performance reasons. To see this, consider the following definitions:
\begin{codeblock}
    \cl{0.5cm}
    (add-method r-sin \\
    \cl{1.0cm}
    (unchecked-prim-proc r-sin (<real>) <real> (pure)))
    \\
    \cl{0.5cm}
    (include-virtual-methods sin r-sin)
\end{codeblock}
If the generic procedure \code{sin} were dispatched by the argument unit types
the its applications with real arguments should be dispatched runtime. When the
application of the procedure to a real argument is dispatched by argument
classes the application can be dispatched compile time.


\begin{figure}[h]
	\centering \includegraphics[scale=0.5]{gen-proc-classes.pdf}
	\caption{Inheritance hierarchy for generic procedures.
	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.}  
	\label{fig:gen-proc-class-hierarchy}
\end{figure}


\begin{figure}[h]
	\centering \includegraphics[scale=0.5]{method-classes.pdf}
	\caption{Example inheritance hierarchy for methods.
	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.}  
	\label{fig:method-class-hierarchy}
\end{figure}
  

\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}
where s is a static type expression.
Compute the following:
\begin{enumerate}
\item
  If \objectvarf{s} is a type loop expression and the subtype list of
  \objectvarf{s} is a type list expression or a tuple type set
  \objectvarf{n} to be the length of the subtype list.
  If \objectvarf{s} is a type loop expression and the subtype list of
  \objectvarf{s} is a type variable set \objectvarf{n} to be \(-1\).
  If \objectvarf{s} is a type list expression or a tuple type set
  \objectvarf{n} to be the length of \objectvarf{s}.
\item
  If \(n = -1\) return
  \begin{objectformula}
    \alglistlparen
    \alglistlparen
    \indexedobjectvar{c}{1} \sldots \indexedobjectvar{c}{N(c)}  
    \alglistrparen
    \alglistlparen
    \alglistrparen
    \alglistrparen
  \end{objectformula}
\item
  If \(N(c) \geq n\) return
  \begin{objectformula}
    \alglistlparen \alglistlparen \alglistlparen
    \indexedobjectvar{c}{1} \sldots \indexedobjectvar{c}{n}
    \alglistrparen \alglistrparen
    \alglistlparen
    \indexedobjectvar{c}{n+1} \sldots \indexedobjectvar{c}{N(c)}  
    \alglistrparen \alglistrparen
  \end{objectformula}
\item
  else signal an error
\end{enumerate}


\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 the deduced argument list type
  contains free type parameters. For each parametrized method \(j\)
  let \(w[j]\) be type of method \(j\) with all the deduced type
  parameters substituted with their values. For simple methods let
  \(w[j]\) be the type of the method.
\item
  If \(m=1\) and the argument method is a parametrized method return the
  method.
  If \(m=1\) and the call argument list type is a subtype of the
  deduced argument list type return the method.
  If \(m=1\) and neither of the conditions does not hold return the
  empty list.
\item
  If \(m=0\) return the empty list.
\item
  If \objectvarf{l} is not a list or a tuple type return all the methods
  for which the call argument list type is a subtype of the deduced
  argument list type.
\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}
  if
  \begin{objectformula}
    \objectvarf{w}[\objectvar{i}] = \code{\#f}
  \end{objectformula}.
\item
  Set
  \begin{objectformula}
    \setvar{\objectvarf{v}[\objectvar{i}]}{\code{\#f}}
  \end{objectformula}
  if
  \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 \(z\) 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
    \(z[j] := \#t\).
  \item
    If \(z[j_0] = \#t\) for some \(j_0 \in \{ 1, \ldots, m \}\)
    then set \(v := z\) else do
    \algorithmappl{SelectNearestMethods}{\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'', the result method is method number \(j\), and the result
  processed type is \(w[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{SelectBestMatch}.


\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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A},
          \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{DeduceStepBackward}
          {\objectvarf{cur-src},
          (\objectvarf{cur-target}),
          \objectvarf{T},
          \objectvarf{A},
          \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{A}: A list of type variables to be
deduced \\
\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{A}, \objectvarf{F}, \objectvarf{old-count},
  \objectvarf{old-state}} \\


\begin{enumerate}
\item
  Apply algorithm
  \algorithmappl{DeduceTypeParams}{\objectvarf{src}, \objectvarf{target},
  \objectvarf{T}, \objectvarf{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \objectvarf{F}, \objectvarf{old-count},
  \objectvarf{old-state}} \\


\begin{enumerate}
\item
  Apply algorithm
  \algorithmappl{DeduceTypeParams}{\objectvarf{target}, \objectvarf{src},
  \objectvarf{T}, \objectvarf{A}, \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{DeduceUniformLists}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{hd}: A 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{A}: A list of all type variables to
be deduced \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\
\mbox{ } \hspace{1cm} \objectvarf{v2}: A set (list) of
expression pairs visited \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceUniformLists}{\objectvarf{hd}, %
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, %
  \objectvarf{v2}} \\


\noindent
If any of the following branches match \objectvarf{hd} and \objectvarf{dest}
stop the deduction.

\begin{enumerate}
\item
  Let \objectvarf{uniform?} be \true\ iff all of the following
  conditions hold:
  \begin{enumerate}
  \item
    \objectvarf{dest2} is a uniform list type
  \item
    The component type of \objectvarf{dest2} is a type variable
  \item
    \objectvarf{src2} is a general pair
  \item
    \objectvarf{hd} does not contain any free type variables not
    belonging to \objectvarf{F}
  \item
    The component type variable of \objectvarf{dest2} belongs to
    \objectvarf{A} and is not already bound in \objectvarf{T}.
  \end{enumerate}
\item
  If \objectvarf{uniform?} is \true\ and \objectvarf{hd} is a nonempty tuple
  type then
  \begin{enumerate}
  \item 
    let \objectvarf{var} be the component type variable of
    \objectvarf{dest2}
  \item
    let \objectvarf{u} be the union of the component
    types of \objectvarf{hd}
  \item
    compute
    \algorithmappl{AddDeduction}{\objectvarf{u}, \objectvarf{var}, %
      \objectvarf{T}},
  and return.
 \end{enumerate}
\item
  If \objectvarf{uniform?} is \true\ and \objectvarf{hd} is a nonempty
  uniform list type
  \begin{enumerate}
  \item 
    let \objectvarf{var} be the component type variable of
    \objectvarf{dest2}
  \item
    let \objectvarf{u} be the component type of \objectvarf{hd}
  \item
    compute
    \algorithmappl{AddDeduction}{\objectvarf{u}, \objectvarf{var}, %
      \objectvarf{T}},
  and return.
 \end{enumerate}
\item
 If \objectvarf{uniform?} is \true\ and \objectvarf{hd} is a uniform list type
 \begin{enumerate}
 \item 
   let \objectvarf{var} be the component type variable of
   \objectvarf{dest2}
 \item
   let \objectvarf{u} be the component type of \objectvarf{hd}
 \item
   compute
   \algorithmappl{DeduceTypeParams}{(\objectvarf{u}), \objectvarf{var},
     \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v2}},
 and return.
 \end{enumerate}
\item
  If \objectvarf{uniform?} is \true\ and \objectvarf{hd} is a tuple
  type with a tail (an uniform list)
  \begin{enumerate}
  \item 
    let \objectvarf{var} be the component type variable of
    \objectvarf{dest2}
  \item
    let \objectvarf{u1} be the set of component types of the tuple
    part of \objectvarf{hd}
  \item
    let \objectvarf{u2} be the component type of the tail part of
    \objectvarf{hd}
  \item
    let \objectvarf{u} be the union of the types of \objectvarf{u1}
    and \objectvarf{u2}
  \item
    compute
    \algorithmappl{AddDeduction}{\objectvarf{u}, \objectvarf{var}, %
      \objectvarf{T}},
  and return.
  \end{enumerate}
\end{enumerate}


\subsection{\algorithmname{DeduceTypeParams}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A 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{A}: A list of all type variables to
be deduced \\
\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{A}, \objectvarf{F}, %
  \objectvarf{v}} \\


\begin{enumerate}
\item
  If \objectvarf{src} is not a (general) pair and \objectvarf{dest} is
  a splice expression
  set \objectvarf{src1} to be the value of
  \begin{objectformula}
    \algorithmappl{HandleStaticRepr}{\objectvar{src}, \false}.
  \end{objectformula}
  If \objectvarf{src1} is a type list object let \objectvarf{src2} be the
  content list of \objectvarf{src1}. Otherwise let \objectvarf{src2} be
  \objectvarf{src1}.
  \begin{objectformula}
    \algorithmappl{DeduceTypeParams}{(\objectvar{src2}),
      \objectvar{c}, \objectvar{T}, \objectvar{F}, \objectvar{v}}
  \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 let \objectvarf{v2} be the union of
  (\objectvarf{x}, \objectvarf{dest}) and \objectvarf{v}.
\item
  Set \objectvarf{src1} to be the value of
  \begin{objectformula}
    \algorithmappl{PrepareSourceType}{\objectvar{src}}.
  \end{objectformula}
  If \objectvarf{src1} is a type list object let \objectvarf{src2} be the
  content list of \objectvarf{src1}. Otherwise let \objectvarf{src2} be
  \objectvarf{src1}.
\item
  Set \objectvarf{dest1} to be the value of
  \begin{objectformula}
    \algorithmappl{HandleStaticRepr}{\objectvar{dest}, \false}.
  \end{objectformula}
  If \objectvarf{dest1} is a type list object let \objectvarf{dest2} be the
  content list of \objectvarf{dest1}. Otherwise let \objectvarf{dest2} be
  \objectvarf{dest1}.
\item
  If \objectvarf{src2} is not a pair or there are not any type
  variables in \objectvarf{dest} return.
\item
  If \objectvarf{dest2} is a type variable compute
  \begin{objectformula}
    \algorithmappl{DeduceSimpleType}{\objectvar{src2},
      \objectvar{dest}, \objectvar{T}, \objectvar{A}, \objectvar{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  Let \objectvarf{hd} to be the head of \objectvarf{src2}.
\item
  If \objectvarf{hd} is a type variable compute
  \begin{objectformula}
    \algorithmappl{DeduceSimpleType}{(\objectvar{dest2}),
      \objectvar{hd}, \objectvar{T}, \objectvar{A}, \objectvar{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{hd} and \objectvarf{dest2} are both signatures compute
  \begin{objectformula}
    \algorithmappl{DeduceSgnSgn}{\objectvar{hd},
      \objectvar{dest}, \objectvar{T}, \objectvar{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest2} is a signature and \objectvarf{hd} is not a
  signature compute
  \begin{objectformula}
    \algorithmappl{DeduceNotSgnSgn}{\objectvar{hd},
      \objectvar{dest2}, \objectvar{T}, \objectvar{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest2} is not a signature and \objectvarf{hd} is a
  signature compute
  \begin{objectformula}
    \algorithmappl{DeduceSgnNotSgn}{\objectvar{hd},
      \objectvar{dest2}, \objectvar{T}, \objectvar{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  Compute
  \begin{objectformula}
    \algorithmappl{DeduceUniformLists}{\objectvarf{hd}, %
    \objectvarf{dest}, \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, %
    \objectvarf{v2}}
  \end{objectformula}
  and return if it matches.
\item
  Compute
  \begin{objectformula}
    \algorithmappl{DeduceUniformLists}{\objectvarf{dest}), %
    \objectvarf{hd}, \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, %
    \objectvarf{v2}}
  \end{objectformula}
  and return if it matches.
\item
  If \objectvarf{dest2} is a pair or a pair class compute
  \begin{objectformula}
    \algorithmappl{DeducePair}{\objectvar{src2},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest2} is a rest expression compute
  \begin{objectformula}
    \algorithmappl{DeduceRest}{\objectvar{src2},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{dest2} is a splice expression compute
  \begin{objectformula}
    \algorithmappl{DeduceSplice}{\objectvar{src2},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{src2} is not a pair or a pair class return \false.
\item
  If \objectvarf{dest2} is a type loop expression compute
  \begin{objectformula}
    \algorithmappl{DeduceTypeLoop}{\objectvar{src2},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{hd} is a union and \objectvarf{dest2} is not a
  union compute
  \begin{objectformula}
    \algorithmappl{DeduceUnionX}{\objectvar{hd},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{hd} is not a union and \objectvarf{dest2} is a
  union compute
  \begin{objectformula}
    \algorithmappl{DeduceXUnion}{\objectvar{src2},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{hd} is a union and \objectvarf{dest2} is a union
  compute
  \begin{objectformula}
    \algorithmappl{DeduceUnionUnion}{\objectvar{hd},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\item
  If \objectvarf{hd} is a generic procedure class and
  \objectvarf{dest2} is an abstract procedure containing free type variables
  compute
  \begin{objectformula}
    \algorithmappl{DeduceGenAbst2}{\objectvar{hd},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}.
  and return.
\item
  If \objectvarf{hd} is a generic procedure class and
  \objectvarf{dest2} is an abstract procedure type compute
  \begin{objectformula}
    \algorithmappl{DeduceGenAbst}{\objectvar{hd},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}.
  and return.
\item
  If \objectvarf{hd} is an abstract procedure type and
  \objectvarf{dest2} is a generic procedure class compute
  \begin{objectformula}
    \algorithmappl{DeduceAbstGen}{\objectvar{hd},
      \objectvar{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}.
  and return.
\item If \objectvarf{dest2} 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{dest2} 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{dest2}, \objectvar{T}, \objectvarf{A}, \objectvarf{F}, \objectvar{v2}}
  \end{objectformula}
  and return.
\end{enumerate}


\subsection{\algorithmname{HandleStaticRepr}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t}: a static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{tuples?}: \true\ to convert lists to
tuples \\


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


\noindent
\textit{Algorithm:}
\algorithmappl{HandleStaticRepr}{\objectvarf{t}, \objectvarf{tuples?}}
              

\begin{enumerate}
\item
  If \objectvarf{t} is a parametrized procedure class return the
  content type (simple procedure type) of \objectvarf{t}.
\item
  If \objectvarf{t} is a type list object and \objectvarf{tuples?} is
  \true\ return a tuple type consisting of the components of
  \objectvarf{t}.
\item
  If \objectvarf{t} is a type list object and \objectvarf{tuples?} is
  \false\ the components of \objectvarf{t} as a list.
\item
  Otherwise return \objectvarf{t}.
\end{enumerate}


\subsection{\algorithmname{PrepareSourceType}}


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

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


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


\begin{enumerate}
\item
  If \objectvarf{t} is a pair then
  \begin{enumerate}
  \item
    let \objectvarf{u} be the head of \objectvarf{t} and
    \objectvarf{w} be the tail of \objectvarf{t}
  \item
    let \objectvarf{q} be the result of
    \algorithmappl{HandleStaticRepr}{\objectvarf{u}, \false}
  \item
    return the list with head \objectvarf{q} and tail \objectvarf{w}.
  \end{enumerate}
\item
  If \objectvarf{t} is a pair class then
  \begin{enumerate}
  \item
    let \objectvarf{u} be the head of \objectvarf{t} and
    \objectvarf{w} be the tail of \objectvarf{t}
  \item
    let \objectvarf{q} be the result of
    \algorithmappl{HandleStaticRepr}{\objectvarf{u}, \true}
  \item
    return the pair class with head \objectvarf{q} and tail \objectvarf{w}.
  \end{enumerate}
\item
  If \objectvarf{t} is an abstract pair class then
  \begin{enumerate}
  \item
    let \objectvarf{u} be the head of \objectvarf{t} and
    \objectvarf{w} be the tail of \objectvarf{t}
  \item
    let \objectvarf{q} be the result of
    \algorithmappl{HandleStaticRepr}{\objectvarf{u}, \true}
  \item
    return the abstract pair class with head \objectvarf{q} and tail
    \objectvarf{w}
  \end{enumerate}
\item
  If none of the conditions above hold signal an error.
\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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \objectvarf{F}, \objectvarf{v}}.
\end{enumerate}


\subsection{\algorithmname{AddDeduction}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{src}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{var}: A type variable \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{AddDeduction}{\objectvarf{src},
  \objectvarf{var}, \objectvarf{T}} \\


\begin{enumerate}
\item
  Add the binding of \objectvarf{var} with \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{DeduceSimpleType}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{srclist}: A static type expression \\
\mbox{ } \hspace{1cm} \objectvarf{dest}: A type variable \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{A}: A list of all type variables to
be deduced \\
\mbox{ } \hspace{1cm} \objectvarf{F}: A list of fixed type variables \\


\noalgresult


\noindent
\textit{Algorithm:} \algorithmappl{DeduceSimpleType}{\objectvarf{srclist},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{A}, \objectvarf{F}} \\


If
\begin{itemize}
\item
  \objectvarf{srclist} is a list,
\item
  \objectvarf{dest} is contained in \objectvarf{A},
\item
  \objectvarf{dest} is not already contained in \objectvarf{T}, and
\item
  the head of \objectvarf{srclist} does not contain any free type
  variables not in \objectvarf{F}
\end{itemize}
then
let \objectvarf{src} be the head of \objectvarf{srclist} and compute
\algorithmappl{AddDeduction}{\objectvarf{src}, \objectvarf{dest}, %
  \objectvarf{T}}.


\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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \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{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \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{A}, \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{A}, \objectvarf{F}, \objectvar{v}}
    \end{objectformula}.
  \end{enumerate}
\item
  Else 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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item
  Let \objectvarf{m} be the method class list of \objectvarf{t1}.
  Compute
  \algorithmappl{SelectBestMatch}{\objectvarf{a}, \objectvarf{m}}.
\item
  If an unambiguous match was not found terminate the algorithm.
\item
  Let \objectvarf{n} be the substituted type of the match.
\item
  Let \objectvarf{a} be the target argument list type.
  If the argument list of the match contains type variables
  let \objectvarf{c} be the argument
  list type of \objectvarf{n} and apply algorithm
  \algorithmappl{DeduceTypeParams}{(\objectvarf{c}), \objectvarf{a},
    \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v}}.
\item
  Let \objectvarf{r} be the target result type.
  If the result type of the match contains type variables let
  \objectvarf{b} be the result type of \objectvarf{m}
  and apply algorithm
  \algorithmappl{DeduceTypeParams}{(\objectvarf{b}), \objectvarf{r},
    \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v}}.
\end{enumerate}


\subsection{\algorithmname{DeduceGenAbst2}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t1}: A generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{t2}: An abstract procedure type
containing free type variables \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{A}: A list of type variables to be
deduced \\
\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{DeduceGenAbst2}{\objectvarf{t1},
  \objectvarf{t2}, \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item
  Let \objectvarf{m} be the method class list of \objectvarf{t1}.
\item
  If \objectvarf{m} contains only one method \objectvarf{m1} apply algorithm
  \algorithmappl{DeduceTypeParams}{(\objectvarf{m1}), \objectvarf{t2},
    \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v}}
  and exit.
\item
  %% Maybe wrong algorithm here.
  Apply \algorithmappl{EqualTypes?}{\objectvarf{mn},
    \objectvarf{t2}, \objectvarf{v}}
  for each method \objectvarf{mn}
  and add the bindings found to \objectvarf{T}.
\end{enumerate}


\subsection{\algorithmname{DeduceAbstGen}}


\noindent
\textit{\algarguments} \\
\noindent
\mbox{ } \hspace{1cm} \objectvarf{t1}: An abstract procedure type \\
\mbox{ } \hspace{1cm} \objectvarf{t2}: A generic procedure class \\
\mbox{ } \hspace{1cm} \objectvarf{T}: An object containing type variable
bindings \\
\mbox{ } \hspace{1cm} \objectvarf{A}: A list of type variables to be
deduced \\
\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{DeduceAbstGen}{\objectvarf{t1},
  \objectvarf{t2}, \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v}} \\


\begin{enumerate}
\item
  Let \objectvarf{m} be the method class list of \objectvarf{t2}.
  Compute \objectvarf{result}, \objectvarf{method}, and
  \objectvarf{processed-type} with algorithm
  \algorithmappl{SelectBestMatch}{\objectvarf{a}, \objectvarf{m}}.
\item
  If an unambiguous match was not found terminate the algorithm.
\item
  Let \objectvarf{a} be the target argument list type.
  If the argument list of the match contains type variables
  let \objectvarf{c} be the argument
  list type of \objectvarf{processed-type} and apply algorithm
  \algorithmappl{DeduceTypeParams}{(\objectvarf{c}), \objectvarf{a},
    \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v}}.
\item
  Let \objectvarf{r} be the target result type.
  If the result type of the match contains type variables let
  \objectvarf{b} be the result type of \objectvarf{processed-type}
  and apply algorithm
  \algorithmappl{DeduceTypeParams}{(\objectvarf{b}), \objectvarf{r},
    \objectvarf{T}, \objectvarf{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \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{A}, \objectvarf{F}, \objectvarf{v}}.


\subsection{\algorithmname{DeduceSgnNotSgn}}


\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{A}: A list of type variables to be
deduced \\
\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{DeduceSgnNotSgn}{\objectvarf{src},
  \objectvarf{dest}, \objectvarf{T}, \objectvarf{A}, \objectvarf{F}, \objectvarf{v}} \\


For each procedure specifier \objectvarf{s} in \objectvarf{src}
define \objectvarf{p} be the type of the corresponding procedure and
define \objectvarf{q}
by substituting \code{this} by \objectvarf{dest} in \objectvarf{s}.
Apply algorithm
\algorithmappl{DeduceTypeParams}{(\objectvarf{q}), \objectvarf{p},
  \objectvarf{T}, \objectvarf{A}, \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{A}: A list of type variables to be
deduced \\
\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{A}, \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 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{\$not}}
\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{\$not}
\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} \syntaxor \skeyword{friend} \\
  \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}.


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

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


Keyword \keyword{friend} may be used only in interfaces.
If module
\objectvarf{A} declares module \objectvarf{B} as its friend the code
in module \objectvarf{B} may access the fields of the classes defined
in module \objectvarf{A} having \code{module} access.


\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}
  \syntaxopt{\syntaxvar{key-specifier}} \sldots
  \srp \\
  \syntaxvar{class-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{key-specifier} \syntaxdefequal
  \syntaxvar{superclass-specifier} \syntaxor
  \syntaxvar{fields-specifier} \syntaxor
  \syntaxvar{construct-specifier} \syntaxor
  \syntaxvar{attribute-specifier} \syntaxor
  \syntaxvar{ctr-access-specifier} \syntaxor
  \syntaxvar{make-access-specifier} \syntaxor
  \syntaxvar{inh-access-specifier} \syntaxor
  \syntaxvar{zero-specifier} \\
  \syntaxvar{superclass-specifier} \syntaxdefequal
  \slp \skeyword{superclass} \syntaxvar{superclass} \srp \\
  \syntaxvar{fields-specifier} \syntaxdefequal
  \slp \skeyword{fields} \syntaxopt{field-spec} \sldots \srp \\
  \syntaxvar{construct-specifier} \syntaxdefequal \\
  \cl{0.5cm}
  \code{(\skeyword{construct} default)} \syntaxor \\
  \cl{0.5cm}
  \slp
  \skeyword{construct} \slp \indexedsyntaxvar{ctr-arg}{1} \sldots
  \indexedsyntaxvar{ctr-arg}{m} \srp \\
  \cl{2.4cm}
  \slp \indexedsyntaxvar{super-arg}{1} \sldots
  \indexedsyntaxvar{super-arg}{n} \srp \\
  \cl{2.4cm}
  \syntaxelement{constructor-attribute} \syntaxor
  \slp
  \syntaxelement{constructor-attribute} \sldots
  \srp
  \srp \\
  \syntaxvar{attribute-specifier} \syntaxdefequal
  \slp
  \skeyword{attributes} \syntaxopt{\syntaxelement{attribute}} \sldots
  \srp \\
  \syntaxvar{field-spec} \syntaxdefequal \\
  \cl{0.5cm}
  \slp
  \syntaxvar{field-name} \syntaxvar{field-type}
  \syntaxvar{read-access} \syntaxvar{write-access}
  \syntaxopt{\syntaxvar{field-initializer}}
  \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} \\
  \syntaxelement{attribute} \syntaxdefequal
  \code{immutable} \syntaxor \code{equal-by-value}
  \syntaxor \code{abstract} \\
  \syntaxelement{constructor-attribute} \syntaxdefequal
  \code{pure} \syntaxor \code{nonpure} \syntaxor \code{always-returns}
  \syntaxor \code{never-returns} \\
  \syntaxvar{ctr-access-specifier} \syntaxdefequal
  \slp \skeyword{constructor-access} \syntaxelement{access-specifier} \srp \\
  \syntaxvar{make-access-specifier} \syntaxdefequal
  \slp \skeyword{make-access} \syntaxelement{access-specifier} \srp \\
  \syntaxvar{inh-access-specifier} \syntaxdefequal
  \slp \skeyword{inheritance-access} \syntaxelement{access-specifier} \srp \\
  \syntaxvar{zero-specifier} \syntaxdefequal
  \slp
  \skeyword{zero} \syntaxvar{zero-expression}
  \srp \\
  \indexedsyntaxvar{ctr-arg}{k} \syntaxdefequal
  \slp \indexedsyntaxvar{arg-name}{k} \indexedsyntaxvar{arg-type}{k} \srp \\
  \indexedsyntaxvar{arg-name}{k} \syntaxdefequal \syntaxelement{identifier}
\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} and \indexedsyntaxvarf{arg-type}{k}
have to be static type expressions. Each key may occur at most once in
the class definition.

Parameter \syntaxvarf{superclass} is the superclass of the new
class. If it is not specified the superclass is \code{<object>}.
Parameter \skeyword{fields} defined the fields of the class. 
Parameter \skeyword{construct} specifies how the instances of the
class are constructed. If it is not present or it is given a single
argument \code{default} a default constructor is generated.
The default constructor takes the following arguments:
\begin{itemize}
\item Arguments passed to the constructor of the superclass
\item Arguments to set those fields for which no default value is
  given (not the fields of the superclasses)
\end{itemize}

If an explicit \skeyword{construct} statement is present all the
new fields defined by the class must have an initializer.
The initializers may use the constructor argument variables.
The values \indexedsyntaxvarf{super-arg}{k} are passed to the
constructor of the superclass. The field initializers are used to set
the initial values of the fields.

A class an be declared abstract by attribute \code{abstract}. Such a class can't
have any direct instances. Declaring class to be abstract is equivalent to
declaring \skeyword{constructor-access} and \skeyword{make-access} to
\code{hidden}.


\subsection{\keyword{define-virtual-gen-proc}}
\index{\keyword{define-virtual-gen-proc}}


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


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


\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{class-name}
  \syntaxopt{\syntaxvar{key-specifier}} \sldots
  \srp \\
  \syntaxvar{class-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{key-specifier} \syntaxdefequal
  \syntaxvar{parameter-specifier} \syntaxor
  \syntaxvar{superclass-specifier} \syntaxor
  \syntaxvar{fields-specifier} \syntaxor
  \syntaxvar{construct-specifier} \syntaxor
  \syntaxvar{attribute-specifier} \syntaxor
  \syntaxvar{ctr-access-specifier} \syntaxor
  \syntaxvar{inh-access-specifier} \syntaxor
  \syntaxvar{zero-specifier} \\
  \syntaxvar{parameter-specifier} \syntaxdefequal
  \slp
  \skeyword{parameters} \indexedsyntaxvar{param}{1} \sldots
  \indexedsyntaxvar{param}{p}
  \srp \\
  \syntaxvar{superclass-specifier} \syntaxdefequal
  \slp \skeyword{superclass} \syntaxvar{superclass} \srp \\
  \syntaxvar{fields-specifier} \syntaxdefequal
  \slp \skeyword{fields} \syntaxopt{field-spec} \sldots \srp \\
  \syntaxvar{construct-specifier} \syntaxdefequal \\
  \cl{0.5cm}
  \code{(\skeyword{construct} default)} \syntaxor \\
  \cl{0.5cm}
  \slp
  \skeyword{construct} \slp \indexedsyntaxvar{ctr-arg}{1} \sldots
  \indexedsyntaxvar{ctr-arg}{m} \srp \\
  \cl{2.4cm}
  \slp \indexedsyntaxvar{super-arg}{1} \sldots
  \indexedsyntaxvar{super-arg}{n} \srp \\
  \cl{2.4cm}
  \syntaxelement{constructor-attribute} \syntaxor
  \slp
  \syntaxelement{constructor-attribute} \sldots
  \srp
  \srp \\
  \syntaxvar{attribute-specifier} \syntaxdefequal
  \slp
  \skeyword{attributes} \syntaxopt{\syntaxelement{attribute}} \sldots
  \srp \\
  \syntaxvar{field-spec} \syntaxdefequal \\
  \cl{0.5cm}
  \slp
  \syntaxvar{field-name} \syntaxvar{field-type}
  \syntaxvar{read-access} \syntaxvar{write-access}
  \syntaxopt{\syntaxvar{field-initializer}}
  \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} \\
  \syntaxelement{attribute} \syntaxdefequal
  \code{immutable} \syntaxor \code{equal-by-value} \\
  \syntaxelement{constructor-attribute} \syntaxdefequal
  \code{pure} \syntaxor \code{nonpure} \syntaxor \code{always-returns}
  \syntaxor \code{never-returns} \\
  \syntaxvar{ctr-access-specifier} \syntaxdefequal
  \slp \skeyword{constructor-access} \syntaxelement{access-specifier} \srp \\
  \syntaxvar{make-access-specifier} \syntaxdefequal
  \slp \skeyword{make-access} \syntaxelement{access-specifier} \srp \\
  \syntaxvar{inh-access-specifier} \syntaxdefequal
  \slp \skeyword{inheritance-access} \syntaxelement{access-specifier} \srp \\
  \syntaxvar{zero-specifier} \syntaxdefequal
  \slp
  \skeyword{zero} \syntaxvar{zero-expression}
  \srp \\
  \indexedsyntaxvar{ctr-arg}{k} \syntaxdefequal
  \slp \indexedsyntaxvar{arg-name}{k} \indexedsyntaxvar{arg-type}{k} \srp \\
  \indexedsyntaxvar{arg-name}{k} \syntaxdefequal
  \syntaxelement{identifier} \\
  \indexedsyntaxvar{param}{k} \syntaxdefequal
  \syntaxelement{identifier}
\end{syntaxblock}

Key \skeyword{parameters} specifies the type parameters of the
parametrized class.
The syntax of the other keys is similar to \keyword{define-class},
see section \ref{subsec:define-class}.
When the instances of the parametrized class are created the
type variables in \syntaxvarf{parameter-specifier} are bound to the
values given for them and these bindings are applied for
\syntaxvarf{fields-specifier} and \syntaxvarf{superclass-specifier}.


\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-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
normal generic procedure \syntaxvarf{generic-name}. The method will be
dispatched statically.
Procedure \syntaxvarf{method} has to be a simple procedure or a
parametrized procedure. If the generic procedure does not exist it is
created.


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


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


Keyword \keyword{add-virtual-method} adds \syntaxvarf{method} into the
virtual 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-virtual-method}}
\index{\keyword{add-static-virtual-method}}
\label{subsec:expr-add-static-virtual-method}


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


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


\subsection{\keyword{include-methods}}
\index{\keyword{include-methods}}
\label{subsec:expr-include-methods}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{include-methods} \syntaxvar{target-generic-name}
  \syntaxvar{source-generic-name} \srp \\
  \\
  \syntaxvar{target-generic-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{source-generic-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{include-methods} adds the methods of the source
generic procedure into the target generic procedure.
Both generic procedures have to be normal.


\subsection{\keyword{include-virtual-methods}}
\index{\keyword{include-virtual-methods}}
\label{subsec:expr-include-virtual-methods}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{include-virtual-methods} \syntaxvar{target-generic-name}
  \syntaxvar{source-generic-name} \srp \\
  \\
  \syntaxvar{target-generic-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{source-generic-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{include-virtual-methods} adds the methods of the source
generic procedure into the target generic procedure as dynamically
dispatched methods. The target generic procedure has to be virtual and
the source generic procedure normal. Only the virtual methods declared
in the same module are included.


\subsection{\keyword{include-static-virtual-methods}}
\index{\keyword{include-static-virtual-methods}}
\label{subsec:expr-include-static-virtual-methods}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{include-static-virtual-methods} \syntaxvar{target-generic-name}
  \syntaxvar{source-generic-name} \srp \\
  \\
  \syntaxvar{target-generic-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{source-generic-name} \syntaxdefequal \syntaxelement{identifier}
\end{syntaxblock}


Keyword \keyword{include-static-virtual-methods} adds the methods of the source
generic procedure into the target generic procedure as statically
dispatched methods. The target generic procedure has to be virtual and
the source generic procedure normal. Only the virtual methods declared
in the same module are included.


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


Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-foreign-prim-class} 
  \syntaxvar{class-name}
  \syntaxopt{\syntaxvar{key-specifier}} \sldots
  \srp \\
  \syntaxvar{key-specifier} \syntaxdefequal
  \syntaxvar{member-specifier} \syntaxor
  \syntaxvar{attribute-specifier} \syntaxor
  \syntaxvar{zero-specifier} \\
  \syntaxvar{member-specifier} \syntaxdefequal
  \slp
  \skeyword{member-pred} \syntaxvar{member-pred}
  \srp \\
  \syntaxvar{attribute-specifier} \syntaxdefequal
  \slp
  \skeyword{attributes} \syntaxopt{\syntaxelement{attribute}} \sldots
  \srp \\
  \syntaxvar{zero-specifier} \syntaxdefequal
  \slp
  \skeyword{zero} \syntaxvar{zero-var}
  \srp \\
  \syntaxvar{zero-var} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{member-pred} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxelement{attribute} \syntaxdefequal
  \code{immutable} \syntaxor \code{equal-by-value} 
  \syntaxor \code{use-eq?} \\
\end{syntaxblock}


Keyword \keyword{define-foreign-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.  Attributes \syntaxvarf{immutable} and
\syntaxvarf{equal-by-value} specify whether the class is immutable or
equal by value, respectively. If attribute \code{use-eq?} is declared the
class uses Scheme predicate \code{eq?} instead of \code{eqv?} for
implementing predicate \code{equal-objects?}.
If
\syntaxvarf{zero-var} is not null it defines a zero value (variable)
for the class.


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


Syntax:
Syntax:
\begin{syntaxblock}
  \slp \skeyword{define-foreign-goops-class} 
  \syntaxvar{class-name}
  \syntaxopt{\syntaxvar{key-specifier}} \sldots
  \srp \\
  \syntaxvar{key-specifier} \syntaxdefequal
  \syntaxvar{target-name-specifier} \syntaxor
  \syntaxvar{superclass-specifier} \syntaxor
  \syntaxvar{slots-specifier} \syntaxor
  \syntaxvar{inh-access-specifier} \syntaxor
  \syntaxvar{attribute-specifier} \syntaxor
  \syntaxvar{zero-specifier} \\
  \syntaxvar{target-name-specifier} \syntaxdefequal
  \slp
  \skeyword{target-name} \syntaxvar{target-name}
  \srp \\
  \syntaxvar{superclass-specifier} \syntaxdefequal
  \slp
  \skeyword{superclass} \syntaxvar{superclass}
  \srp \\
  \syntaxvar{slots-specifier} \syntaxdefequal
  \slp \skeyword{slots} \syntaxopt{slot-spec} \sldots \srp \\
  \syntaxvar{inh-access-specifier} \syntaxdefequal
  \slp
  \skeyword{inheritance-access} \syntaxvar{superclass}
  \srp \\
  \syntaxvar{attribute-specifier} \syntaxdefequal
  \slp
  \skeyword{attributes} \syntaxopt{\syntaxelement{attribute}} \sldots
  \srp \\
  \syntaxvar{slot-spec} \syntaxdefequal \\
  \cl{0.5cm}
  \slp
  \syntaxvar{slot-name} \syntaxvar{slot-type}
  \syntaxvar{read-access} \syntaxvar{write-access}
  \syntaxvar{init-value-spec} \syntaxvar{ref-purity}
  \syntaxopt{\syntaxvar{keyword}}
  \srp \\
  \syntaxvar{slot-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} \\
  \syntaxelement{init-value-spec} \syntaxdefequal
  \code{has-init-value} \syntaxor \code{no-init-value} \\
  \syntaxelement{ref-purity} \syntaxdefequal
  \code{pure-ref} \syntaxor \code{nonpure-ref} \\
  \syntaxvar{keyword} \syntaxdefequal \syntaxelement{keyword} \\
  \syntaxvar{zero-specifier} \syntaxdefequal
  \slp
  \skeyword{zero} \syntaxvar{zero-var}
  \srp \\
  \syntaxvar{target-name} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxvar{inh-access-specifier} \syntaxdefequal
  \syntaxelement{access-specifier} \\
  \syntaxvar{zero-var} \syntaxdefequal \syntaxelement{identifier} \\
  \syntaxelement{attribute} \syntaxdefequal
  \code{immutable} \syntaxor \code{equal-by-value}
  \syntaxor \code{use-eq?} \syntaxor{pure-make?}
  \\
  \syntaxelement{access-specifier} \syntaxdefequal
  \code{public} \syntaxor \code{module} \syntaxor \code{hidden}
\end{syntaxblock}


Keyword \keyword{define-foreign-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>}. The components of a slot specifier are:
\begin{description}
  \item[\syntaxvar{slot-name}] The name of the slot (a symbol).
  \item[\syntaxvar{slot-type}] The type of the slot.
  \item[\syntaxvar{read-access}] The read access of the slot.
  \item[\syntaxvar{write-access}] The write access of the slot.
  \item[\syntaxvar{init-value-spec}] Indicates if the slot has an initial value.
  \item[\syntaxvar{ref-purity}] The purity of \code{slot-ref} for the slot.
  \item[\syntaxvar{keyword}] The keyword used to define the slot value in a
  \code{make} expression.
\end{description}

If attribute \code{use-eq?} is declared the
class uses Scheme predicate \code{eq?} instead of \code{eqv?} for
implementing predicate \code{equal-objects?}.
If \syntaxvarf{zero-var} is defined
it defines a zero value (variable) for the class. If a slot keyword is
specified the keyword has to be defined by \code{\#:init-keyword} option in the
target GOOPS class. Attribute \code{pure-make?} indicates that making an
instance of the class is pure (if the arguments to \code{make} are pure).


\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 (static) method for
a normal generic procedure.
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}
If the generic procedure does not exist it is created.


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


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


Keyword \keyword{declare-virtual-method} declares a dynamic method for
a virtual generic procedure.
A declaration of the method is added into
the virtual 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-virtual-method}}
\index{\keyword{declare-static-virtual-method}}
\label{subsec:expr-declare-static-virtual-method}


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


Keyword \keyword{declare-static-virtual-method} declares a static
method for a virtual generic procedure-
A declaration of the method is added into
the virtual 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}
  \slp \indexedsyntaxvar{arg-type}{1} ... \indexedsyntaxvar{arg-type}{n} \srp \\
  \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}
  \slp \indexedsyntaxvar{arg-type}{1} ... \indexedsyntaxvar{arg-type}{n} \srp
  \; \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{static-gen-proc-dispatch}}
\index{\keyword{static-gen-proc-dispatch}}


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


Keyword \keyword{static-gen-proc-dispatch} dispatches a normal generic
procedure compile time. It returns either a simple or parametrized
procedure. The argument type list may contain type variables. Unlike
\keyword{generic-proc-dispatch} the argument list type has to match
the method argument list type exactly.


\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.


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


Syntax:
\begin{syntaxblock}
  \slp
  \skeyword{match-type-weak}
  \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}}
\index{\keyword{match-type}}
\label{subsec:match-type}


This form is identical to \keyword{match-type-weak} 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} 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. For a constant of a primitive class a unit type is
returned.


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


Syntax:
\begin{syntaxblock}
  \slp \skeyword{static-type-of0} \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. For a constant of a primitive class the class is
returned.


\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{make}}
\index{\keyword{make}}


Syntax:
\begin{syntaxblock}
  \slp \skeyword{make} \objectvar{class}
  \indexedsyntaxvar{keyword}{1} \indexedsyntaxvar{initial-value}{1}
  \sldots \;
  \indexedsyntaxvar{keyword}{n} \indexedsyntaxvar{initial-value}{n}
  \srp
\end{syntaxblock}


Expression \objectvarf{class} has to be a static type expression and
its value has to be a \theme\ class or a foreign GOOPS class.

For a \theme\ class, each field of the constructed object is initialized by the
argument
\indexedsyntaxvarf{initial-value}{k} following they keyword corresponding to the
field. If the keyword of a field is not contained in the argument list the
initial value of the field specified in the class construction is used. It is an
error if an initial value of a field can't be computed by either way. The types
of the initial values are checked by the \theme\ compiler, too.

For a GOOPS class the argument list is passed to a Guile \code{make} expression.
The \theme\ compiler checks that each field having no initial field in the foreign
class specification has an initializer in the \code{make} expression. The
compiler also checks that the initializers have correct types.


\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{apply0} and \builtin{apply-nonpure0},
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 procedure
implements algorithm \algorithmname{EqualValues?}, see section
\ref{subsubsec:EqualValues?}.


\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{apply0}}
\index{\builtin{apply0}}


\startprocsyntax
\begin{codeblock}
  (apply0 \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{apply0} calls \procarg{procedure} with the arguments
from \procarg{argument-list}. This is similar to Scheme \code{apply}.


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


\startprocsyntax
\begin{codeblock}
(\code{apply-nonpure0} \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-nonpure0} 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.


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


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


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


\procresult{Type of the object}{\builtin{<type>}}


\procpurity{pure}


Procedure \builtin{type-of} is the same as procedure \builtin{class-of} except a
unit type is returned for an object of a primitive atomic class.


\section{Vector Operations}


\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-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-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-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-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{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{Constructors}
\label{subsec:constructor-example}


\index{constructor}


Constructs are special procedures used to create instances (objects)
of classes. They are not defined like other procedures but
\theme\ creates them using the \skeyword{construct} statement in a
class and field initializers in a class. The translator-generated
default constructor is sufficient in many cases. For example, consider
the class \code{<complex>} defined in the standard-library:
\begin{codeblock}
  \cl{0.5cm}
  (define-class <complex>
  \\
  \cl{1.5cm}
  (attributes immutable equal-by-value)
  \\
  \cl{1.5cm}
  (inheritance-access hidden)
  \\
  \cl{1.5cm}
  (fields
  \\
  \cl{2.0cm}
  (re <real> public hidden)
  \\
  \cl{2.0cm}
  (im <real> public hidden))
  \\
  \cl{1.5cm}
  (zero (make <complex> 0.0 0.0)))
\end{codeblock}
The translator-generated default constructor takes two real arguments
and sets the first to the field \code{re} and the second to the field
\code{im}.

The programs \code{objects1.thp} and \code{objects2.thp} in
subdirectory \code{examples} demonstrate user-defined constructors.
Here is the first example:
\begin{codeblock}
  \cl{0.5cm}
  (define-class <widget>
  \\
  \cl{1.5cm}
    (fields
  \\
  \cl{2.0cm}
     (str-id <string> public module)))
  \\

  \cl{0.5cm}
  (define-class <window>
  \\
  \cl{1.5cm}
    (superclass <widget>)
  \\
  \cl{1.5cm}
    (construct ((str-id1 <string>) (i-x11 <integer>) (i-y11 <integer>)
  \\
  \cl{3.7cm}
        (i-x21 <integer>) (i-y21 <integer>))
  \\
  \cl{3.5cm}
           (str-id1))
  \\
  \cl{1.5cm}
    (fields
  \\
  \cl{2.0cm}
     (i-x1 <integer> public module i-x11)
  \\
  \cl{2.0cm}
     (i-y1 <integer> public module i-y11)
  \\
  \cl{2.0cm}
     (i-x2 <integer> public module i-x21)
  \\
  \cl{2.0cm}
     (i-y2 <integer> public module i-y21)
  \\
  \cl{2.0cm}
     (i-width <integer> public module (+ (- i-x21 i-x11) 1))
  \\
  \cl{2.0cm}
     (i-height <integer> public module (+ (- i-y21 i-y11) 1))))
\end{codeblock}
The constructor of class \code{<window>} passes the first argument
\code{str-id1} to the constructor of its superclass
\code{<widget>}. The constructors also initialize the fields using their
arguments. Note that the field initializers may contain more complex
expressions than just copying an argument variable.
Here is the second example:
\begin{codeblock}
  \cl{0.5cm}
  (define-class <widget>
  \\
  \cl{1.5cm}
    (construct ((str-id1 <string>)) () (nonpure))
  \\
  \cl{1.5cm}
    (fields
  \\
  \cl{2.0cm}
     (str-id <string> public module
  \\
  \cl{2.5cm}
         (begin
  \\
  \cl{2.5cm}
           (console-display "new widget: ")
  \\
  \cl{2.5cm}
           (console-display-line str-id1)
  \\
  \cl{2.5cm}
           str-id1))))
               \\
               
  \cl{0.5cm}
  (define-class <window>
  \\
  \cl{1.5cm}
    (superclass <widget>)
  \\
  \cl{1.5cm}
    (construct ((str-id1 <string>) (i-x11 <integer>) (i-y11 <integer>)
  \\
  \cl{3.7cm}
        (i-x21 <integer>) (i-y21 <integer>))
  \\
  \cl{3.5cm}
           (str-id1) (nonpure))
  \\
  \cl{1.5cm}
    (fields
  \\
  \cl{2.0cm}
     (i-x1 <integer> public module i-x11)
  \\
  \cl{2.0cm}
     (i-y1 <integer> public module i-y11)
  \\
  \cl{2.0cm}
     (i-x2 <integer> public module i-x21)
  \\
  \cl{2.0cm}
     (i-y2 <integer> public module i-y21)
  \\
  \cl{2.0cm}
     (i-width <integer> public module (+ (- i-x21 i-x11) 1))
  \\
  \cl{2.0cm}
     (i-height <integer> public module (+ (- i-y21 i-y11) 1))))
\end{codeblock}
Here we log the calls to the constructor of \code{<widget>} to the
console. Note that we have to declare the constructors as
\code{nonpure} if they have side effects.


\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{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}
