-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinators.tex
603 lines (520 loc) · 19.2 KB
/
combinators.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
\section{Query Combinators}
\label{sec:combinators}
In this section, we show how the query model defined in
Section~\ref{sec:cardinality} can support a wide range of operations on data.
\subsection*{Extracting Data}
By traversing the tree of Figure~\ref{fig:unfolded-form}, we can extract data
from the database.
\begin{demo}
\label{ex:department-name}
Show the name of each department.
\begin{equation*}
\Department\To\Name
\end{equation*}
\end{demo}
This example is constructed by descending through nodes $\Department$ and
$\Name$, which represent primitives
\begin{alignat*}{3}
& \Department && : \Void && \to \Seq{\Dept}, \\
& \Name && : \Dept && \to \Text.
\end{alignat*}
The composition of the primitives inherits the input of the first component and
the output of the second component. Since one of the components is plural, the
composition is also plural, which gives it a signature
\begin{equation*}
\Department\To\Name : \Void \to \Seq{\Text}.
\end{equation*}
\begin{demo}
\label{ex:department-employee-name}
For each department, show the name of each employee.
\begin{equation*}
\Department\To\Employee\To\Name
\end{equation*}
\end{demo}
This example takes a path through
\begin{alignat*}{3}
& \Department && : \Void && \to \Seq{\Dept}, \\
& \Employee && : \Dept && \to \Seq{\Emp}, \\
& \Name && : \Emp && \to \Text
\end{alignat*}
to construct a query
\begin{equation*}
\Department\To\Employee\To\Name : \Void \to \Seq{\Text}.
\end{equation*}
This query produces a list of employee names. Since each employee belongs to
exactly one department, the list should contain the name of every employee.
The order in which the names appear in the output depends on the intrinsic
order of the $\Department$ and $\Employee$ primitives, but, in any case,
employees within the same department will be coupled together.
The same collection of names, although not necessarily in the same order, is
produced by the following example.
\begin{demo}
\label{ex:employee-name}
Show the name of each employee.
\begin{equation*}
\Employee\To\Name
\end{equation*}
\end{demo}
On the other hand, the next example is very different from the apparently
similar Example~\ref{ex:department-name}.
\begin{demo}
\label{ex:employee-department-name}
For each employee, show the name of their department.
\begin{equation*}
\Employee\To\Department\To\Name
\end{equation*}
\end{demo}
Here, we should see a list of department names, but each name will appear as
many times as there are employees in the corresponding department.
\begin{demo}
\label{ex:employee-position}
Show the position of each employee.
\begin{equation*}
\Employee\To\Position
\end{equation*}
\end{demo}
Similarly, $\Employee\To\Position$ will output duplicate position titles. We
will see how to produce a list of \emph{unique} positions in
Section~\ref{sec:quotients}.
\begin{demo}
\label{ex:employee}
Show all employees.
\begin{equation*}
\Employee
\end{equation*}
\end{demo}
This example emits a sequence of employee entities, which, in practice, could
be represented as records with employee attributes.
\input{table-common-combinators}
\subsection*{Summarizing Data}
Let us show how the extracted data can be summarized.
\begin{demo}
\label{ex:count-department}
Show the number of departments.
\begin{equation*}
\Count(\Department)
\end{equation*}
\end{demo}
This query produces a single number, so that its signature is
\begin{equation*}
\Count(\Department) : \Void \to \Int.
\end{equation*}
It is constructed by applying the $\Count$ combinator to a query that generates
\emph{a list of all departments}
\begin{equation*}
\Department : \Void \to \Seq{\Dept}.
\end{equation*}
Comparing the signatures of these two queries, we can derive the signature of
the $\Count$ combinator, in this specific case
\begin{equation*}
(\Void \to \Seq{\Dept}) \to (\Void \to \Int),
\end{equation*}
and, in general
\begin{equation*}
\Count: (A \to \Seq{B}) \to (A \to \Int).
\end{equation*}
In other words, the $\Count$ combinator transforms any sequence-valued query
to an integer-valued query. It is implemented by lifting the function that
computes the length of a sequence
\begin{equation*}
|-| : \Seq{A} \to \Int
\end{equation*}
to a query combinator
\begin{equation*}
\Count(q) = a \mapsto |q(a)|.
\end{equation*}
Unary combinators that transform a plural query to a singular (or optional)
query are called \emph{aggregate} combinators.
\begin{demo}
\label{ex:max-employee-salary}
What is the highest employee salary?
\begin{equation*}
\Max(\Employee\To\Salary)
\end{equation*}
\end{demo}
In this example, we extract the relevant data with
\begin{equation*}
\Employee\To\Salary : \Void \to \Seq{\Int}
\end{equation*}
and summarize it using the $\Max$ aggregate
\begin{equation*}
\Max(\Employee\To\Salary) : \Void \to \Opt{\Int}.
\end{equation*}
This query is optional since it produces no output when the database contains
no employees.
\begin{demo}
\label{ex:department-count-employee}
For each department, show the number of employees.
\begin{equation*}
\Department\To\Count(\Employee)
\end{equation*}
\end{demo}
In this example, we transform a plural relationship, \emph{all employees in the
given department}
\begin{equation*}
\Employee : \Dept \to \Seq{\Emp}
\end{equation*}
to a calculated attribute, \emph{the number of employees in the given
department}
\begin{equation*}
\Count(\Employee) : \Dept \to \Int.
\end{equation*}
Then we attach it to
\begin{equation*}
\Department : \Void \to \Seq{\Dept}
\end{equation*}
to get \emph{the number of employees in each department}
\begin{equation*}
\Department\To\Count(\Employee) : \Void \to \Seq{\Int}.
\end{equation*}
Applying the combinator $\Max$ to the query above, we answer the following
question.
\begin{demo}
\label{ex:max-department-count-employee}
How many employees are in the largest department?
\begin{equation*}
\Max(\Department\To\Count(\Employee))
\end{equation*}
\end{demo}
\subsection*{Pipeline Notation}
Queries are often constructed incrementally, by extracting relevant data and
then shaping it into the desired form with a chain of combinators. This
construction is made apparent with the \emph{pipeline notation}.
In pipeline notation, the first argument of a combinator is placed in front of
it, separated by colon (``$\,\Apply\,$''):
\begin{equation*}
p \Apply F \equiv F(p), \qquad
p \Apply F(q_1,\ldots,q_n) \equiv F(p,q_1,\ldots,q_n).
\end{equation*}
For example, $\Count(\Department)$ could also be written
\begin{equation*}
\Department\Apply\Count.
\end{equation*}
A more sophisticated query written in pipeline notation is shown in the
following example.
\begin{demo}
\label{ex:top-ten-highest-paid-policemen}
Show the top 10 highest paid employees in the Police department.
\begin{align*}
& \Employee \\
& \Apply\Filter(\Department\To\Name = \textliteral{POLICE}) \\
& \Apply\Sort(\Salary\Apply\Desc) \\
& \Apply\Select(\Name,\; \Position,\; \Salary) \\
& \Apply\Take(10)
\end{align*}
\end{demo}
Without pipeline notation, this query is much less intelligible:
\begin{multline*}
\Take(\Select(\Sort(\Filter( \\
\Employee,\; \Department\To\Name = \textliteral{POLICE}), \\
\Desc(\Salary)),\; \Name,\; \Position,\; \Salary),\; 10).
\end{multline*}
The combinators $\Filter$, $\Sort$, $\Desc$, $\Select$, and $\Take$ are
described below.
\subsection*{Filtering Data}
We can now demonstrate how to produce entities that satisfy a certain
condition.
\begin{demo}
\label{ex:filter-by-salary}
Which employees have a salary higher than \$150k?
\begin{equation*}
\Employee\Apply\Filter(\Salary>150000)
\end{equation*}
\end{demo}
This query introduces several concepts.
First, the integer literal $150000$ represents a primitive query that
\emph{for any given employee, produces the number $150000$}
\begin{equation*}
150000 : \Emp \to \Int = e \mapsto 150000.
\end{equation*}
Second, the relational symbol ${>}$ denotes a binary combinator that builds a
query \emph{for a given employee, show whether their salary is higher than
\$150k}
\begin{equation*}
\Salary > 150000 : \Emp \to \Bool.
\end{equation*}
The combinator
\begin{equation*}
\placeholder>\placeholder : (A \to \Int,\; A \to \Int) \to (A \to \Bool)
\end{equation*}
is implemented by lifting the relational operator
\begin{equation*}
\placeholder>\placeholder : (\Int,\; \Int) \to \Bool
\end{equation*}
to an operation on queries
\begin{equation*}
(p > q) = a \mapsto (p(a) > q(a)).
\end{equation*}
Third, a binary combinator $\Filter$ emits those $\Employee$ entities that
satisfy the condition $\Salary > 150000$. In general, given
\begin{equation*}
p : A \to \Seq{B}, \qquad q : B \to \Bool,
\end{equation*}
a query
\begin{equation*}
\Filter(p,\; q) : A \to \Seq{B}
\end{equation*}
produces the values of $p$ that satisfy condition $q$
\begin{equation*}
\Filter(p,\; q) = a \mapsto [\,b \mid b \gets p(a),\; q(b)=\True\,].
\end{equation*}
The following example shows how $\Filter$ could be used in tandem with
aggregate combinators.
\begin{demo}
\label{ex:filter-by-size-and-count}
How many departments have more than 1000 employees?
\begin{align*}
& \Department \\
& \Apply\Filter(\Count(\Employee)>1000) \\
& \Apply\Count
\end{align*}
\end{demo}
\subsection*{Sorting and Paginating Data}
The combinator $\Sort$, applied to a plural query, sorts the query output in
ascending order.
\begin{demo}
\label{ex:sort-department-name}
Show the names of all departments in alphabetical order.
\begin{equation*}
\Sort(\Department\To\Name)
\end{equation*}
\end{demo}
The combinator $\Sort$ is implemented by lifting a sequence function
\begin{equation*}
\Sort : \Seq{A} \to \Seq{A}
\end{equation*}
to a query combinator
\begin{align*}
& \Sort : (A \to \Seq{B}) \to (A \to \Seq{B}), \\
& \Sort(p) = a \mapsto \Sort(p(a)).
\end{align*}
\begin{demo}
\label{ex:sort-employee-by-salary}
Show all employees ordered by salary.
\begin{equation*}
\Employee\Apply\Sort(\Salary)
\end{equation*}
\end{demo}
In this example, a list of employees is sorted by the value of the attribute
$\Salary$, which is supplied as the second argument to the $\Sort$ combinator.
In this form, $\Sort$ has a signature
\begin{equation*}
\Sort : (A \to \Seq{B},\; B \to C) \to (A \to \Seq{B}).
\end{equation*}
\begin{demo}
\label{ex:sort-employee-by-salary-desc}
Show all employees ordered by salary, highest paid first.
\begin{equation*}
\Employee\Apply\Sort(\Salary\Apply\Desc)
\end{equation*}
\end{demo}
Here, the sort key is wrapped with the combinator $\Desc$ to indicate the
descending sort order.
It is not immediately obvious how to implement $\Desc$ without violating the
query model. Na\"{\i}vely, $\Desc$ acts like a negation operator, however, not
every type supports negation. Instead, we make the sort order a part of the
type definition, so that
\begin{equation*}
\Int_{\le} \quad\text{and}\quad \Int_{\ge}
\end{equation*}
could indicate the integer type with ascending and descending sort order
respectively. Then, $\Desc$ could be considered a type conversion combinator
with the signature
\begin{equation*}
\Desc : (A \to B) \to (A \to B_{\ge}).
\end{equation*}
\begin{demo}
\label{ex:sort-employee-by-salary-take-top}
Who are the top 1\% of the highest paid employees?
\begin{align*}
& \Employee \\
& \Apply\Sort(\Salary\Apply\Desc) \\
& \Apply\Take(\Count(\Employee)\mathbin \div 100)
\end{align*}
\end{demo}
In this example, only the first 1\% of employees are retained by the combinator
$\Take$, which has two arguments: a query that produces a sequence of employees
\begin{equation*}
\Employee\Apply\Sort(\Salary\Apply\Desc) : \Void \to \Seq{\Emp}
\end{equation*}
and a query that returns how many employees to keep
\begin{equation*}
\Count(\Employee) \div 100 : \Void \to \Int.
\end{equation*}
Notice that both arguments of $\Take$ have the same input ($\Void$ in this
case), which is reflected in the signature
\begin{equation*}
\Take : (A \to \Seq{B},\; A \to \Int) \to (A \to \Seq{B}).
\end{equation*}
\subsection*{Query Output}
The combinator $\Select$ customizes the query output.
Previously, we constructed a query to \emph{show the number of employees for
each department} (see Example~\ref{ex:department-count-employee}):
\begin{equation*}
\Department\To\Count(\Employee).
\end{equation*}
However, this query only produces a list of bare numbers---it does not connect
them to their respective departments. This is corrected in the following example.
\begin{demo}
\label{ex:department-select-name-size}
For each department, show its name and the number of employees.
\begin{equation*}
\Department
\Apply\Select(\Name,\;\Size\As\Count(\Employee))
\end{equation*}
\end{demo}
In this example, the combinator $\Select$ takes three arguments: the base query
\begin{equation*}
\Department : \Void \to \Seq{\Dept}
\end{equation*}
and two field queries
\begin{alignat*}{3}
& \Name && : \Dept && \to \Text, \\
& \Count(\Employee) && : \Dept && \to \Int.
\end{alignat*}
The $\Select$ combinator generates a sequence of records by applying each field
query to every entity produced by the base query, giving this example a
signature
\begin{equation*}
\Void \to \Seq{\Tuple{\Name:\Text,\; \Size:\Int}}.
\end{equation*}
The declaration
\begin{equation*}
\Tuple{\Name:\Text,\; \Size:\Int}
\end{equation*}
defines a \emph{record} type with two fields: a text field $\Name$ and an
integer field $\Size$. The names of the record fields are derived from the
tags of the field queries, which could be set using the \emph{tagging
notation}. For example,
\begin{equation*}
\Size \As \Count(\Employee)
\end{equation*}
binds a tag $\Size$ to the query $\Count(\Employee)$. Since the tag does not
materially affect the query it annotates, we do not expose the tag in the query
model.
A more complex output structure could be defined with nested $\Select$ combinators.
\begin{demo}
\label{ex:department-select-name-etc}
For every department, show the top salary and a list of managers with their
salaries.
\begin{alignat*}{4}
& \Department\hidewidth && && && \\
& \Apply\Select( && \Name, && && \\
& && \TopSalary && \As\; && \Max(\Employee\To\Salary), \\
& && \Manager && \As\; && \Employee \\
& && && && \Apply\Filter(\Exists(\Subordinate)) \\
& && && && \Apply\Select(\Name,\; \Salary))
\end{alignat*}
\end{demo}
In this example, the query output has the type
\begin{align*}
\keyword{Seq}\{\Tuple{& \Name : \Text,\; \TopSalary : \Opt{\Int}, \\
& \Manager : \Seq{\Tuple{\Name : \Text,\; \Salary : \Int}}}\}.
\end{align*}
Recall that we represented the data source in a universal hierarchical form
(see Figure~\ref{fig:unfolded-form}). Furthermore, the query output could also
be represented as a hierarchical database, whose structure is determined by the
query signature (see Figure~\ref{fig:department-select-name-etc}). Thus,
queries could be seen as transformations of hierarchical databases.
\input{figure-department-select-name-etc}
\subsection*{Query Aliases}
A complex query could often be simplified by replacing duplicate expressions
with aliases.
\begin{demo}
\label{ex:department-define-size}
Show the top 3 largest departments and their sizes.
\begin{align*}
& \Department \\
& \Apply\Define(\Size\As\Count(\Employee)) \\
& \Apply\Sort(\Size\Apply\Desc) \\
& \Apply\Select(\Name,\; \Size) \\
& \Apply\Take(3)
\end{align*}
\end{demo}
In this example, the alias $\Size$ is created in two steps: first, the tag
$\Size$ is bound to the query
\begin{equation*}
\Count(\Employee): \Dept \to \Int,
\end{equation*}
and then $\Size$ is added to scope of $\Dept$ by the combinator $\Define$.
Although this query could have been written as
\begin{align*}
& \Department \\
& \Apply\Sort(\Count(\Employee)\Apply\Desc) \\
& \Apply\Select(\Name,\; \Count(\Employee)) \\
& \Apply\Take(3),
\end{align*}
the use of an alias makes this example more legible, not only by reducing
redundancy, but also by assigning a name to a key concept of the query.
\subsection*{Hierarchical Relationships}
Hierarchical relationships are encoded by self-referential primitives.
For example, the relationship between an employee and their manager is
expressed with
\begin{equation*}
\Manager : \Emp \to \Opt{\Emp}.
\end{equation*}
\begin{demo}
\label{ex:employee-filter-salary-manager}
Find all employees whose salary is higher than the salary of their manager.
\begin{equation*}
\Employee\Apply\Filter(\Salary>\Manager\To\Salary)
\end{equation*}
\end{demo}
This example uses familiar combinators $\Filter$ and ${>}$ (see
Example~\ref{ex:filter-by-salary}), but an alert reader will notice the
disagreement between the signature of the combinator
\begin{equation*}
\placeholder>\placeholder : (A \to \Int,\; A \to \Int) \to (A \to \Bool)
\end{equation*}
and the signatures of its arguments
\begin{alignat*}{3}
& \Salary && : \Emp && \to \Int, \\
& \Manager\To\Salary && : \Emp && \to \Opt{\Int}.
\end{alignat*}
Namely, ${>}$ expects its arguments to be singular, but the output of
$\Manager\To\Salary$ is optional.
To legitimize this query, we adopt the following rule. When one argument of a
scalar combinator has a non-trivial cardinality, this cardinality can be
promoted to the output of the combinator. This rule gives ${>}$ a signature
\begin{equation*}
\placeholder>\placeholder : (A \to \Int,\; A \to M\{\Int\}) \to (A \to M\{\Bool\})
\end{equation*}
or, in this specific case,
\begin{equation*}
\Salary > \Manager\To\Salary : \Emp \to \Opt{\Bool}.
\end{equation*}
Finally, we need to let $\Filter$ accept predicate queries with optional
output, by treating $\bot$ as $\False$.
Using expressions
\begin{align*}
& \Manager, \\
& \Manager\To\Manager, \\
& \Manager\To\Manager\To\Manager,\; \ldots
\end{align*}
we can build queries that involve the manager, the manager's manager, etc. We
can also obtain \emph{the complete management chain for the given employee}
with
\begin{equation*}
\Connect(\Manager) : \Emp \to \Seq{\Emp}.
\end{equation*}
\begin{demo}
\label{ex:city-treasurer-subordinates}
Find all direct and indirect subordinates of the City Treasurer.
\begin{multline*}
\Employee \\
\shoveleft{\Apply\Filter(\Any(\Connect(\Manager)\To\Position =} \\
\textliteral{CITY\ TREASURER}))
\end{multline*}
\end{demo}
Here, the query
\begin{equation*}
\Connect(\Manager)\To\Position : \Emp \to \Seq{\Text}
\end{equation*}
produces \emph{the positions of all managers above the given employee}.
In general, the combinator $\Connect$ maps an optional self-referential query to a
plural self-referential query by taking its transitive closure:
\begin{multline*}
\Connect : (A \to \Opt{A}) \to (A \to \Seq{A}), \\
\shoveleft{\Connect(p) = a \mapsto [\;p(a),\; p(p(a)),\; \ldots,\; p^{(n)}(a)\;]} \\
(p^{(n)}(a) \ne \bot,\; p^{(n+1)}(a) = \bot).
\end{multline*}