]> matita.cs.unibo.it Git - helm.git/blob - helm/mathql/doc/mathql_introduction_core.tex
ocaml 3.09 transition
[helm.git] / helm / mathql / doc / mathql_introduction_core.tex
1 \subsection{The core language} 
2
3 {\MathQL}.4 consists of a core language and of a basic library. Other
4 user-defined libraries can be added at will. The core language includes the
5 \TT{property} operator mentioned in \subsecref{HighAccess} that queries the
6 underlying {\RDF} database, and the infrastructure to post-process the
7 query results. The components of this infrastructure are listed below:
8
9 \begin{itemize}
10
11 \item 
12 \textbf{Explicit sets of attributed values.}
13 An explicit {\av} set can be placed in a query in two forms: 
14 as a quoted string, like \verb+"this is a query result"+, that evaluates in a
15 single {\av} with that value and no attributes, or as a full {\av} set in the
16 syntax shown in the previous sections but surrounded by square brackets, like 
17 \verb+["head" attr {/"attribute" = "contents"}]+.
18 \newline
19 In the second form, the contents of an attribute can be the result of a query
20 and in this case the contents of the attribute are the head strings of the
21 query result, whose attributes (if any) are discarded. 
22 \end{itemize}
23
24 \begin{center}
25 \verb+["head" attr {/"attribute" = property /"metadata" of "resource"}]+
26 \end{center}
27
28 \begin{itemize}
29
30 \item 
31 \textbf{Variable assignment.}
32 Variables for {\av} sets (preceded by a \TT{\$} sign and called
33 \emph{set variables}) can be assigned using a standard \emph{let-in}
34 construction and may appear wherever an {\av} set ({\ie} a query result) is
35 allowed.  
36 The assignment has the form: 
37 \TT{let \$}\EM{variable} \TT{=} \EM{av-set} \TT{in} \EM{av-set}
38 so we can write:
39 \newline
40 \verb+let $var = "contents" in ["head" attr {/"attribute" = $var}]+.
41
42 The scope of {\MathQL} variables is typical for an imperative programming
43 language and any case of assignment propagation will be indicated.
44
45 \item 
46 \textbf{Sequential composition.}
47 This construction has the form: \EM{av-set} \TT{;;} \EM{av-set} and works as
48 follows: the two {\av} sets are evaluated one after the other and the first
49 one is discarded but the variables assigned in the first {\av} set are
50 available to the second one.  
51
52 \item 
53 \textbf{Unbounded iteration.}
54 This construction comes in two forms:
55
56 \TT{while} \EM{av-set-1} \TT{sup} \EM{av-set-2}:
57 iterates the evaluation of \EM{av-set-2} until \EM{av-set-1} is empty and
58 returns the {\MathQL} set-theoretic union of all the evaluations of
59 \EM{av-set-2}.
60 \TT{while} \EM{av-set-1} \TT{inf} \EM{av-set-2}:
61 like the former but the set-theoretic intersection is used instead of the
62 set-theoretic union.
63
64 In order for \TT{while} to work as expected, both {\av} sets are evaluated in
65 a common context during the iteration ({\ie} the variables defined in both
66 are available to both) and this context is also propagated outside the
67 \TT{while}. 
68
69 \item 
70 \textbf{Bounded iteration.}
71 Also this construction comes in two forms:
72
73 \TT{for @}\EM{variable} \TT{in} \EM{av-set} \TT{sup} \EM{av-set}:
74 iterates the evaluation of the second \EM{av-set} assigning the \EM{variable} 
75 to each element in the first \EM{av-set} and builds the {\MathQL}
76 set-theoretic union of the obtained results.
77
78 \TT{for @}\EM{variable} \TT{in} \EM{av-set} \TT{inf} \EM{av-set}:
79 like the former but the set-theoretic intersection is used instead of the
80 set-theoretic union.
81
82 The variables for attributed values (preceded by a \TT{@} sign and called 
83 \emph{element variables}) may appear wherever an {\av} set is allowed and
84 and in some additional places. 
85 The element variables are kept distinct from the set variables (therefore
86 \TT{\$variable} and \TT{@variable} may appear in the same query without
87 ambiguity).
88 Concerning the scope rules used in these constructions, the variables
89 assigned by the first {\av} set are available to the second {\av} set during
90 the iteration and the variables assigned by both {\av} sets are available 
91 outside the \TT{for} as in the previous case. 
92
93 \item
94 \textbf{Addition of groups.}
95 \TT{add} \EM{optional-flag} \EM{attribute-groups} \TT{in} \EM{av-set}
96 builds an {\av} set adding the specified \EM{attribute-groups} to each element
97 of the given {\av} set.
98 If no \EM{flag} is specified the addition is set-theoretic, whereas with the
99 \TT{distr} flag the addition is distributive.
100 The \EM{attribute-groups} can be given explicitly (in the same syntax used for
101 explicit {\av} sets) or they can be replaced by an element variable. In the
102 latter case the attribute groups of the {\av} stored in the variable are
103 considered.
104
105 \figref{Add} shows how to build a one-element {\av} set using \TT{add}.
106
107 \begin{figure}
108 \begin{footnotesize} \begin{verbatim}
109 The set of attributed values given explicitly:
110  ["head" attr {/"attribute" = property /"metadata" of "resource"}]
111
112 The same set built with the add operator:
113  add {/"attribute" = property /"metadata" of "resource"} in "head"
114 \end{verbatim} \end{footnotesize}
115 \vspace{-1pc}
116 \caption{A simple use of the add operator}
117 \label{Add}
118 \end{figure}
119
120 \item 
121 \textbf{Existential test.}
122 The existential test has the form \TT{ex} \EM{av-set} where the
123 specification of the {\av} set contains some instances of the construction
124 \TT{@}\EM{variable}\TT{.}\EM{attribute-name}, and runs as follows:
125 the given {\av} set is evaluated replacing each 
126 \TT{@}\EM{variable}\TT{.}\EM{attribute-name}
127 with the contents of \EM{attribute-name} in an attribute group of the {\av}
128 stored in \TT{@}\EM{variable} and the evaluation is repeated for every
129 possible choice of these groups (recall that different groups are allowed to
130 contain attributes with the same name). If one evaluation gives a non empty
131 result, the default representation of \emph{true} is returned, in the other
132 case the empty {\av} set, {\ie} \emph{false}, is returned.
133
134 \item
135 \textbf{Function invocation.}
136 The core language allows to invoke two kinds of external functions (with
137 which a language extension may be provided): the functions of the first kind 
138 return an {\av} set, the functions of the second kind return a piece of 
139 {\MathQL} code representing an {\av} set ({\ie} they interface a {\MathQL}
140 code generator). In particular:
141
142 \EM{function-name} \verb+{+ 
143 \EM{name} \TT{,} $\cdots$ \TT{,} \EM{name} \verb+} {+
144 \EM{av-set} \TT{,} $\cdots$ \TT{,} \EM{av-set} \verb+}+
145 invokes a function of the first kind on the given arguments and returns its
146 result. The \EM{name} arguments are {\MathQL} paths and usually
147 represent attribute names.
148
149 \TT{gen} \EM{function-name} \verb+{+
150 \EM{av-set} \TT{,} $\cdots$ \TT{,} \EM{av-set} \verb+}+
151 invokes a function of the second kind on the given arguments and replaces
152 itself with the function result.
153
154 The function names are {\MathQL} paths exactly as the attribute names and the 
155 graph paths used by the \TT{property} operator. The names of the two kinds of
156 functions are kept in distinct environments so they do not clash.
157
158 {\MathQL}.4 comes with a basic library of functions of the first kind 
159 (see \subsecref{Basic}) that integrate the core language providing several
160 facilities.
161
162 \end{itemize}