\subsection{The core language} 

{\MathQL}.4 consists of a core language and of a basic library. Other
user-defined libraries can be added at will. The core language includes the
\TT{property} operator mentioned in \subsecref{HighAccess} that queries the
underlying {\RDF} database, and the infrastructure to post-process the
query results. The components of this infrastructure are listed below:

\begin{itemize}

\item 
\textbf{Explicit sets of attributed values.}
An explicit {\av} set can be placed in a query in two forms: 
as a quoted string, like \verb+"this is a query result"+, that evaluates in a
single {\av} with that value and no attributes, or as a full {\av} set in the
syntax shown in the previous sections but surrounded by square brackets, like 
\verb+["head" attr {/"attribute" = "contents"}]+.
\newline
In the second form, the contents of an attribute can be the result of a query
and in this case the contents of the attribute are the head strings of the
query result, whose attributes (if any) are discarded. 
\end{itemize}

\begin{center}
\verb+["head" attr {/"attribute" = property /"metadata" of "resource"}]+
\end{center}

\begin{itemize}

\item 
\textbf{Variable assignment.}
Variables for {\av} sets (preceded by a \TT{\$} sign and called
\emph{set variables}) can be assigned using a standard \emph{let-in}
construction and may appear wherever an {\av} set ({\ie} a query result) is
allowed.  
The assignment has the form: 
\TT{let \$}\EM{variable} \TT{=} \EM{av-set} \TT{in} \EM{av-set}
so we can write:
\newline
\verb+let $var = "contents" in ["head" attr {/"attribute" = $var}]+.

The scope of {\MathQL} variables is typical for an imperative programming
language and any case of assignment propagation will be indicated.

\item 
\textbf{Sequential composition.}
This construction has the form: \EM{av-set} \TT{;;} \EM{av-set} and works as
follows: the two {\av} sets are evaluated one after the other and the first
one is discarded but the variables assigned in the first {\av} set are
available to the second one.  

\item 
\textbf{Unbounded iteration.}
This construction comes in two forms:

\TT{while} \EM{av-set-1} \TT{sup} \EM{av-set-2}:
iterates the evaluation of \EM{av-set-2} until \EM{av-set-1} is empty and
returns the {\MathQL} set-theoretic union of all the evaluations of
\EM{av-set-2}.
\TT{while} \EM{av-set-1} \TT{inf} \EM{av-set-2}:
like the former but the set-theoretic intersection is used instead of the
set-theoretic union.

In order for \TT{while} to work as expected, both {\av} sets are evaluated in
a common context during the iteration ({\ie} the variables defined in both
are available to both) and this context is also propagated outside the
\TT{while}. 

\item 
\textbf{Bounded iteration.}
Also this construction comes in two forms:

\TT{for @}\EM{variable} \TT{in} \EM{av-set} \TT{sup} \EM{av-set}:
iterates the evaluation of the second \EM{av-set} assigning the \EM{variable} 
to each element in the first \EM{av-set} and builds the {\MathQL}
set-theoretic union of the obtained results.

\TT{for @}\EM{variable} \TT{in} \EM{av-set} \TT{inf} \EM{av-set}:
like the former but the set-theoretic intersection is used instead of the
set-theoretic union.

The variables for attributed values (preceded by a \TT{@} sign and called 
\emph{element variables}) may appear wherever an {\av} set is allowed and
and in some additional places. 
The element variables are kept distinct from the set variables (therefore
\TT{\$variable} and \TT{@variable} may appear in the same query without
ambiguity).
Concerning the scope rules used in these constructions, the variables
assigned by the first {\av} set are available to the second {\av} set during
the iteration and the variables assigned by both {\av} sets are available 
outside the \TT{for} as in the previous case. 

\item
\textbf{Addition of groups.}
\TT{add} \EM{optional-flag} \EM{attribute-groups} \TT{in} \EM{av-set}
builds an {\av} set adding the specified \EM{attribute-groups} to each element
of the given {\av} set.
If no \EM{flag} is specified the addition is set-theoretic, whereas with the
\TT{distr} flag the addition is distributive.
The \EM{attribute-groups} can be given explicitly (in the same syntax used for
explicit {\av} sets) or they can be replaced by an element variable. In the
latter case the attribute groups of the {\av} stored in the variable are
considered.

\figref{Add} shows how to build a one-element {\av} set using \TT{add}.

\begin{figure}
\begin{footnotesize} \begin{verbatim}
The set of attributed values given explicitly:
 ["head" attr {/"attribute" = property /"metadata" of "resource"}]

The same set built with the add operator:
 add {/"attribute" = property /"metadata" of "resource"} in "head"
\end{verbatim} \end{footnotesize}
\vspace{-1pc}
\caption{A simple use of the add operator}
\label{Add}
\end{figure}

\item 
\textbf{Existential test.}
The existential test has the form \TT{ex} \EM{av-set} where the
specification of the {\av} set contains some instances of the construction
\TT{@}\EM{variable}\TT{.}\EM{attribute-name}, and runs as follows:
the given {\av} set is evaluated replacing each 
\TT{@}\EM{variable}\TT{.}\EM{attribute-name}
with the contents of \EM{attribute-name} in an attribute group of the {\av}
stored in \TT{@}\EM{variable} and the evaluation is repeated for every
possible choice of these groups (recall that different groups are allowed to
contain attributes with the same name). If one evaluation gives a non empty
result, the default representation of \emph{true} is returned, in the other
case the empty {\av} set, {\ie} \emph{false}, is returned.

\item
\textbf{Function invocation.}
The core language allows to invoke two kinds of external functions (with
which a language extension may be provided): the functions of the first kind 
return an {\av} set, the functions of the second kind return a piece of 
{\MathQL} code representing an {\av} set ({\ie} they interface a {\MathQL}
code generator). In particular:

\EM{function-name} \verb+{+ 
\EM{name} \TT{,} $\cdots$ \TT{,} \EM{name} \verb+} {+
\EM{av-set} \TT{,} $\cdots$ \TT{,} \EM{av-set} \verb+}+
invokes a function of the first kind on the given arguments and returns its
result. The \EM{name} arguments are {\MathQL} paths and usually
represent attribute names.

\TT{gen} \EM{function-name} \verb+{+
\EM{av-set} \TT{,} $\cdots$ \TT{,} \EM{av-set} \verb+}+
invokes a function of the second kind on the given arguments and replaces
itself with the function result.

The function names are {\MathQL} paths exactly as the attribute names and the 
graph paths used by the \TT{property} operator. The names of the two kinds of
functions are kept in distinct environments so they do not clash.

{\MathQL}.4 comes with a basic library of functions of the first kind 
(see \subsecref{Basic}) that integrate the core language providing several
facilities.

\end{itemize}
