-
Notifications
You must be signed in to change notification settings - Fork 13
/
language.tex
2003 lines (1878 loc) · 88.8 KB
/
language.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\section{Input Languages}\label{sec:language}
This section provides an overview of the input languages of
grounder \gringo, combined grounder and solver \clingo, and solver \clasp.
The joint input language of \gringo\ and \clingo\ is detailed in
Section~\ref{subsec:lang:gringo}.
Finally, Section~\ref{subsec:lang:clasp} is dedicated to the inputs handled by \clasp.
\subsection{Input Language of \gringo\ and \clingo}\label{subsec:lang:gringo}
The tool \gringo~\cite{gescth07a} is a grounder capable of transforming
user-defined logic programs (usually containing variables) into
equivalent ground (that is, variable-free) programs.
The output of \gringo\ can be piped into solver \clasp~\cite{gekanesc07a,gekasc09c},
which then computes answer sets.
System \clingo\ internally couples \gringo\ and \clasp, thus,
it takes care of both grounding and solving.
In contrast to \gringo\ outputting ground programs,
\clingo\ returns answer sets.
Usually, logic programs are specified in one or more (text) files whose names are
provided as arguments in an invocation of either \gringo\ or \clingo.
In what follows,
we describe the constructs belonging to the input language of \gringo\ and \clingo.
\subsubsection{Terms}\label{subsec:gringo:terms}
\index{term}
\index{term!constant}
\index{term!string}
\index{term!variable}
\index{term!anonymous variable}
\index{term!function}
\index{term!integer}
\index{term!\code{\#sup}}
\index{term!\code{\#inf}}
\begin{figure}
\railnontermfont{\rmfamily\itshape}%
\railalias{rusc}{\tt\char95}\railterm{rusc}
\railalias{rlsc}{[A-Za-z0-9\tt\char95']}\railterm{rlsc}
\railalias{any}{[\^{}\symbol{92}"$\dlsh$]}\railterm{any}
\railalias{bs}{\symbol{92}}\railterm{bs}
\railalias{esc}{[\symbol{92}"n]}\railterm{esc}
\begin{rail}
term : simpleterm | function | tuple;
simpleterm : (integer | constant | string | variable | rusc | '\#sup' | '\#inf');
constant : (rusc*) '[a-z]' (rlsc*);
string : '"' ((any | (bs esc))*) '"';
variable : (rusc*) '[A-Z]' (rlsc*);
function : constant '(' term (',' term*) ')';
tuple : '(' (term (',' | ',' term+))? ')';
\end{rail}
\caption{Grammar for Terms.\label{fig:terms}}
\end{figure}
Every (non-propositional) logic program includes \emph{terms},
mainly to specify the arguments of atoms (see below).
The grammar for \gringo's (and \clingo's) terms is shown in Figure~\ref{fig:terms}.
% syntax: simple terms
The basic building blocks are simple terms:
\emph{integers}, \emph{constants}, \emph{strings}, and \emph{variables}
as well as the tokens `\var{\char`\_}', \const{\#sup}, and \const{\#inf}.
An integer is represented by means of an arithmetic expression,
further explained in Section~\ref{subsec:gringo:arith}.
Constants and variables are distinguished by their first letters,
which are \emph{lowercase} and \emph{uppercase}, respectively,
where leading occurrences of `\code{\char`\_}' are allowed
(may be useful to circumvent name clashes).
Furthermore, a string is an arbitrary sequence of characters
enclosed in double quotes (\code{"$\cdot$"}),
where any occurrences of `\code{\textbackslash}', newline, and double quote
must be escaped via `\code{\textbackslash\textbackslash}', `\code{\textbackslash n}',
or `\code{\textbackslash"}', respectively.
% semantics: simple terms
While a constant or string represents itself,
a variable is a placeholder for \emph{all} variable-free terms
in the language of a logic program.%
\footnote{The set of all terms constructible from the available
constants and function symbols is called \emph{Herbrand universe}.}
Unlike a variable name whose recurrences within a rule refer to the same variable,
the token `\var{\char`\_}' (not followed by any letter)
% Note: currently, there is the hidden feature: not p(_) where _ is *not* an anonymous variable
stands for an \emph{anonymous variable} that does not recur anywhere.
(One can view this as if a new variable name is invented on each occurrence of `\var{\char`\_}'.)
In addition, there are the special constants \const{\#sup} and \const{\#inf}
representing the greatest and smallest element among all variable-free terms%
\footnote{Their is a total order defined on variable-free terms; for details see Section~\ref{subsec:gringo:comp}.}
, respectively;
we illustrate their use in Section~\ref{subsec:gringo:aggregate}.
% syntax: tuples and functions
Next, (uninterpreted) \emph{functions} are complex terms composed of a name (like a constant)
and one or more terms as arguments.
For instance,
\code{\const{at}(\const{peter},\const{time}(12),X)}
is a function with three arguments:
constant \const{peter}, another function \code{\const{time}(12)}
with an integer argument, and variable~\var{X}.
Finally, there are \emph{tuples},
which are similar to \emph{functions} but without a name.
Examples for tuples are:
the empty tuple \code{()} and
the tuple \code{(\const{at},\const{peter},\const{time}(12),X)} with four elements.
Tuples may optionally end in a comma `\code{,}'
for distinguishing one-elementary tuples.
That is, \code{($t$,)} is a one-elementary tuple,
while a term of form \code{($t$)} is equivalent to \code{$t$}.
For instance, \code{(42,)} is a one-elementary tuple, whereas \code{(42)} is not,
and the above quadruple is equivalent to \code{(\const{at},\const{peter},\const{time}(12),X,)}.
\subsubsection{Normal Programs and Integrity Constraints}\label{subsec:gringo:normal}
Rules of the following forms are admitted in a
\emph{normal logic program} (with integrity constraints):
\begin{align*}
\textbf{Fact:} & \quad \code{$A_0$.} \\
\textbf{Rule:} & \quad \code{$A_0$ :- $L_1$,$\dots$,$L_n$.} \\
\textbf{Integrity Constraint:} & \quad \code{\phantom{$A_0$} :- $L_1$,$\dots$,$L_n$.}
\end{align*}
\index{logic program}
\index{logic program!normal}%
\index{rule!fact}%
\index{rule!normal}%
\index{rule!integrity constraint}%
The \emph{head}~$A_0$ of a rule or a fact is an \emph{atom} of the same
syntactic form as a constant or function.
\index{atom}%
\index{atom|seealso{literal}}%
In the \emph{body} of a rule or an integrity constraint,
every $L_j$ for $1\leq j\leq n$ is a \emph{literal} of the form $A$ or $\code{not}~A$,
where $A$ is an atom and
the connective \code{not} denotes default negation.
\index{literal}%
\index{literal|seealso{atom}}%
\index{negation|seealso{literal}}%
\index{negation!default}%
We say that a literal~$L$ is \emph{positive} if it is an atom,
and \emph{negative} otherwise.
While the head atom~$A_0$ of a fact must unconditionally be true,
the intuitive reading of a rule corresponds to an implication:
if all positive literals in the rule's body are true and all negative
literals are satisfied, then~$A_0$ must be true.
On the other hand, an integrity constraint is a rule that filters solution candidates,
meaning that the literals in its body must not jointly be satisfied.
A set of (propositional) atoms is called a \emph{model} of a logic program if it satisfies all rules, facts, and integrity constraints.
Atoms are considered true if and only if they are in the model.
In ASP, a model is called an \emph{answer set} if every atom in the model has an (acyclic) derivation from the program.
%
See~\cite{gellif88b,gelfond08a,lifschitz08a} for formal definitions of answer sets of logic programs.
To get the idea, let us consider some small examples.
%
\begin{example}\label{ex:as:one}
Consider the following logic program:
%
\begin{lstlisting}[numbers=none]
a :- b.
b :- a.
\end{lstlisting}
%
When \pred{a} and \pred{b} are false, the bodies of both rules are false as well,
so that the rules are satisfied.
Furthermore, there is no (true) atom to be derived,
which shows that the empty set is an answer set.
On the other hand,
if \pred{a} is true but \pred{b} is not,
then the first rule is unsatisfied because the body holds but the head does not.
Similarly, the second rule is unsatisfied if \pred{b} is true and \pred{a} is not.
Hence, an answer set cannot contain only one of the atoms \pred{a} and~\pred{b}.
It remains to investigate the set including both \pred{a} and~\pred{b}.
Although both rules are satisfied,
\pred{a} and~\pred{b} cannot be derived acyclically:
\pred{a} relies on~\pred{b}, and vice versa.
That is, the set including both \pred{a} and~\pred{b} is not an answer set.
Hence, the empty set is the only answer set of the logic program.
We say that there is a \emph{positive cycle} through \pred{a} and~\pred{b}
subject to minimization.
\end{example}
Consider the following logic program:
%
\begin{lstlisting}[numbers=none]
a :- not b.
b :- not a.
\end{lstlisting}
%
Here, the empty set is not a model because both rules are unsatisfied.
However, the sets containing only~\pred{a} or only~\pred{b} are models.
To see that each of them is an answer set,
note that~\pred{a} is derived by the rule \code{\pred{a}\,:-\:not\:\pred{b}.}
if \pred{b} is false;
similarly,
\pred{b} is derived by \code{\pred{b}\,:-\:not\:\pred{a}.}
if \pred{a} is false.
Note that the set including both~\pred{a} and~\pred{b} is not an answer set
because neither atom can be derived if both are assumed to be true:
the bodies of the rules
\code{\pred{a}\,:-\:not\:\pred{b}.} and
\code{\pred{b}\,:-\:not\:\pred{a}.} are false.
Hence, we have that
either~\pred{a} or~\pred{b} belongs to
an answer set of the logic program.
To illustrate the use of facts and integrity constraints,
let us augment the previous logic program:
\begin{lstlisting}[numbers=none]
a :- not b.
b :- not a.
c.
:- c, not b.
\end{lstlisting}
Since \code{\pred{c}.} is a fact,
atom \pred{c} must unconditionally be true, i.e.,
it belongs to every model.
In view of this,
the integrity constraint
\code{:-\:c,\:not\:\pred{b}.}
tells us that \pred{b} must be true as well
in order to prevent its body from being satisfied.
However, this kind of reasoning does not provide us with
a derivation of \pred{b}.
Rather, we still need to make sure that the body
of the rule \code{\pred{b}\,:-\:not\:\pred{a}.} is satisfied,
so that atom~\pred{a} must be false.
Hence, the set containing \pred{b} and~\pred{c}
is the only answer set of our logic program.
In the above examples,
we used propositional logic programs to exemplify the idea
of an answer set: a model of a logic program such that all its true atoms are
(acyclically) derivable.
In practice, logic programs are typically non-propositional, i.e.,
they include schematic rules with variables.
The next example illustrates this.
\begin{example}\label{ex:flies}
Consider a child from the south pole watching cartoons,
where it sees a yellow bird that is not a penguin.
The child knows that penguins can definitely not fly (due to small wingspread),
but it is unsure about whether the yellow bird flies.
This knowledge is generalized by
the following schematic rules:
%
\lstinputlisting{examples/fly.lp}
%
The first rule expresses that it is generally possible that a bird flies,
unless the contrary, subject to the second rule, is the case.
The definite knowledge that penguins cannot fly
is specified by the third rule.
Later on, the child learns that the yellow bird
is a chicken called ``tweety'',
while its favorite penguin is called ``tux''.
The knowledge about these two individuals is
represented by the following facts:
\lstinputlisting[firstnumber=4]{examples/bird.lp}
When we instantiate the variable~\var{X} in the three schematic rules
with \const{tweety} and \const{tux},
we obtain the following ground rules:
%
\lstinputlisting[numbers=none,xrightmargin=-15pt,nolol]{examples/gfly.lp}
%
Further taking into account that \const{tweety} and \const{tux} are known to
be birds, that \const{tux} is a penguin, while \const{tweety} is not, and that
penguins can definitely not fly,
we can simplify the previous ground rules to obtain the following ones:%
\marginlabel{The reader can reproduce these ground rules
by invoking:\\
\code{\mbox{~}clingo --text \textbackslash\\
\mbox{~}\attach{examples/bird.lp}{bird.lp} \attach{examples/fly.lp}{fly.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash\\
\mbox{~}\attach{examples/bird.lp}{bird.lp} \attach{examples/fly.lp}{fly.lp}}}
%
\lstinputlisting[numbers=none,nolol]{examples/sfly.lp}
%
Now it becomes apparent that \const{tweety}
may fly or not, while \const{tux} surely does not fly.
Thus, there are two answer sets for the three schematic rules above,
instantiated with \const{tweety} and \const{tux}.%
\marginlabel{To compute both answer sets,
invoke:\\
\code{\mbox{~}clingo \attach{examples/bird.lp}{bird.lp} \textbackslash\\
\mbox{~}\attach{examples/fly.lp}{fly.lp} 0}\\
or alternatively:\\
\code{\mbox{~}gringo \attach{examples/bird.lp}{bird.lp} \textbackslash\\
\mbox{~}\attach{examples/fly.lp}{fly.lp} | clasp 0}}
\end{example}
The above example illustrated how variables are used to represent all instances of
rules with respect to the language of a logic program.
In fact, grounder \gringo\ (or the grounding component of \clingo)
takes care of instantiating variables
such that an equivalent propositional logic program is obtained.
To this end,
rules are required to be \emph{safe},
\label{pg:safe}%
\index{safety}%
i.e.,
all variables in a rule must occur in some positive literal
(a literal not preceded by \code{not}) in the body of the rule.
For instance, the first two schematic rules in Example~\ref{ex:flies}
are safe because they include \code{\pred{bird}(\var{X})} in their positive bodies.
This tells \gringo\ (or \clingo)
that the values to be substituted for~\var{X} are limited to birds.
Up to now, we have introduced terms, facts, (normal) rules, and integrity constraints.
Before we proceed to describe handy extensions to this simple core language,
keep in mind that the role of a rule (or fact) is that an atom in the
head can be derived to be true if the body is satisfied.
Unlike this, an integrity constraint implements a test,
but it cannot be used to derive any atom.
This universal meaning still applies when more sophisticated language constructs,
as described in the following, are used.
\subsubsection{Classical Negation}\label{subsec:gringo:negation}
\index{negation!classical}
The connective \code{not} expresses default negation,
i.e., a literal $\code{not}~A$ is assumed to hold unless atom~$A$ is derived to be true.
In contrast, the classical (or strong) negation of an atom~\cite{gellif91a}
holds only if it can be derived.
Classical negation, indicated by symbol `\code{-}', is permitted in front of atoms.
That is, if $A$ is an atom, then $\code{-}A$ is
an atom representing the complement of~$A$.
The semantic relationship between $A$ and~$\code{-}A$
is simply that they
must not jointly hold.
Hence,
classical negation can be understood as a syntactic feature
allowing us to impose an integrity constraint \code{:-\;$A$,\:-$A$.}
without explicitly writing it in a logic program.
Depending on the logic program at hand,
it may be possible that neither~$A$ nor~$\code{-}A$ is contained in an answer set,
thus representing a state where the truth and the falsity of~$A$ are both unknown.
\begin{example}\label{ex:flies:neg}
Using classical negation,
we can rewrite the schematic rules in Example~\ref{ex:flies}
in the following way:
%
\lstinputlisting{examples/flycn.lp}
%
Given the individuals \const{tweety} and \const{tux},
classical negation is reflected by
the following (implicit) integrity constraints:%
\marginlabel{By invoking:\\
\code{\mbox{~}clingo --text \textbackslash\\
\mbox{~}\attach{examples/bird.lp}{bird.lp} \attach{examples/flycn.lp}{flycn.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash\\
\mbox{~}\attach{examples/bird.lp}{bird.lp} \attach{examples/flycn.lp}{flycn.lp}}\\
the reader can observe
that the integrity constraint in Line~4 is indeed part of the grounding.
The second one in Line 5 is not printed;
it becomes obsolete by a static analysis exhibiting that
\const{tux} does surely not fly.}
%
\begin{lstlisting}[firstnumber=4]
:- fly(tweety), -fly(tweety).
:- fly(tux), -fly(tux).
\end{lstlisting}
There are still two answer sets,
containing \code{-\pred{fly}(\const{tux})} and
either \code{\pred{fly}(\const{tweety})} or \code{-\pred{fly}(\const{tweety})}.
Now assume that we add the following fact to the program:
\begin{lstlisting}[numbers=none]
fly(tux).
\end{lstlisting}
Then,
\code{\pred{fly}(\const{tux})} must unconditionally be true,
and \code{-\pred{fly}(\const{tux})} is still derived by
an instance of the third schematic rule.
Since every answer set candidate containing
both \code{\pred{fly}(\const{tux})} and \code{-\pred{fly}(\const{tux})}
triggers
the (implicit) integrity constraint in Line~5,
there is no longer any answer set.
\end{example}
\subsubsection{Disjunction}\label{subsec:gringo:disjunction}
\index{logic program!disjunctive}
\index{rule!disjunctive}
Disjunctive logic programs permit connective~`\code{;}' between atoms in rule heads.%
\footnote{Note that disjunction in rule heads was not supported by \clasp\ and \clingo\ versions before series~3 and~4, respectively.}
\begin{align*}
\textbf{Fact:} & \quad \code{$A_0$;\dots;$A_m$.} \\
\textbf{Rule:} & \quad \code{$A_0$;\dots;$A_m$ :- $L_1$,$\dots$,$L_n$.}
\end{align*}
A disjunctive head holds if at least one of its atoms is true.
Answer sets of a disjunctive logic program satisfy a minimality criterion
that we do not detail here
(see~\cite{eitpol06a,gekasc11b} for an implementation methodology in disjunctive ASP).
We only mention that the simple disjunctive program \code{\pred{a};\pred{b}.} has two answer sets,
one containing~\pred{a} and another one containing~\pred{b},
while both atoms do not jointly belong to an answer set.
After adding the rules of Example~\ref{ex:as:one}, a single answer set containing both~\pred{a} and~\pred{b} is obtained.
This illustrates that disjunction in ASP is neither strictly exclusive or inclusive but subject to minimization.
In general, the use of disjunction may increase
computational complexity~\cite{eitgot95a}.
We thus suggest to use ``choice constructs'' (detailed in Section~\ref{subsec:gringo:aggregate})
instead of disjunction, unless the latter is required for complexity reasons.
\subsubsection{Double Negation and Head Literals}\label{subsec:gringo:double}
\index{negation!double}
\index{literal!head}
The input language of \gringo\ also supports double default negated literals,
written $\code{not}~\code{not}~A$.
They are satisfied whenever their positive counterparts are.
But like negative literals of form $\code{not}~A$,
double negated ones are also preceded by \code{not} and
do thus not require an (acyclic) derivation from the program;
it is sufficient that they are true in the model at hand.
Consider the logic program:
\begin{lstlisting}[numbers=none]
a :- not not b.
b :- not not a.
\end{lstlisting}
%
This program has an empty answer set, like the program in Example~\ref{ex:as:one},
as well as the additional answer set containing both \pred{a} and \pred{b}.
This is because neither `\code{not not a}' nor `\code{not not b}' requires an acyclic derivation from the program.
Note that, in contrast to Example~\ref{ex:as:one},
the above program does not induce
mutual positive dependencies between~\pred{a} and~\pred{b}.
Given this, \pred{a} and~\pred{b} can thus be both true or false, just like in classical logic.
Also, negative literals are admitted in the head of rules.
When disregarding disjunction,
this offers just another way to write integrity constraints,
putting the emphasis on the head literal.
In fact, the rule
\(\code{not}~A_0\)~\code{:-}~\(L_1\text{\code{,}}\dots\text{\code{,}}L_n\)\code{.}
is equivalent to
\code{:-}~\(L_1\text{\code{,}}\dots\text{\code{,}}L_n\text{\code{,}}\code{not}~\code{not}~A_0\)\code{.},
and with double negation in the head, rule
\(\code{not}~\code{not}~A_0\)~\code{:-}~\(L_1\text{\code{,}}\dots\text{\code{,}}L_n\)\code{.}
is equivalent to
\code{:-}~\(L_1\text{\code{,}}\dots\text{\code{,}}L_n\text{\code{,}}\code{not}~A_0\)\code{.}
\begin{example}\label{ex:as:flynn}
Consider the logic program:
\marginlabel{To compute both answer sets, invoke:\\
\code{\mbox{~}clingo \attach{examples/bird.lp}{bird.lp} \textbackslash\\
\mbox{~}\attach{examples/flynn.lp}{flynn.lp} 0}\\
or alternatively:\\
\code{\mbox{~}gringo~\attach{examples/bird.lp}{bird.lp} \textbackslash\\
\mbox{~}\attach{examples/flynn.lp}{flynn.lp} | clasp 0}
}
\lstinputlisting{examples/flynn.lp}
The possibility that a bird flies is expressed with a double negation in the first line.
Solutions with flying penguins are filtered out in the second line.
Like in Example~\ref{ex:flies} there are two answer sets,
but without an explicit atom to indicate that a bird does not fly.
Hence, the answer set where tweety does not fly contains no atoms over predicate \pred{fly}/$1$.
\end{example}
\begin{note}
Note that negative head literals are also supported in disjunctions.
For more information see \cite{litatu99a}.
\end{note}
\subsubsection{Boolean Constants}
\index{atom!Boolean constant}
%
Sometimes it is useful to have literals possessing a constant truth value.
Literals over the two \emph{Boolean constants} \code{\#true} and \code{\#false},
which are always true or false, respectively,
have a constant truth value.
\begin{example}
Consider the following program:
\marginlabel{%
The unique answer set of the program,
can be inspected by invoking:\\
\code{\mbox{~}clingo \attach{examples/bool.lp}{bool.lp} 0}\\
or alternatively:\\
\code{\mbox{~}gringo \attach{examples/bool.lp}{bool.lp} \textbackslash \\ \mbox{~}| clasp 0}\\
Note that this program simply produces an empty grounding:
\code{\mbox{~}clingo --text \textbackslash \\ \mbox{~}\attach{examples/bool.lp}{bool.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash \\ \mbox{~}\attach{examples/bool.lp}{bool.lp}}
}
%
\lstinputlisting{examples/bool.lp}
The first rule uses \code{\#true} in the head.
Because this rule is a fact, it is trivially satisfied.
Similarly, the rules in Line~2 and~3 have satisfied heads.
The bodies of the last three integrity constraints are false.
Hence, the constraints do not cause a conflict.
Note that neither of the rules above derives any atom.
Thus, we obtain the empty answer set for the program.
\end{example}
See Example~\ref{ex:sort} below for an application of interest.
\subsubsection{Built-in Arithmetic Functions}\label{subsec:gringo:arith}
\index{term!integer}
\index{term!arithmetic function@\gobblecomma|see{arithmetic function}}
\index{arithmetic function}
\index{arithmetic function!addition, \code{+}}
\index{arithmetic function!subtraction, \code{-}}
\index{arithmetic function!unary minus, \code{-}}
\index{arithmetic function!multiplication, \code{*}}
\index{arithmetic function!division, \code{/}}
\index{arithmetic function!modulo, \code{\textbackslash}}
\index{arithmetic function!exponentiation, \code{**}}
\index{arithmetic function!absolute value, \mbox{\textbar$\cdot$\textbar}}
\index{arithmetic function!bitwise and, \code{\&}}
\index{arithmetic function!bitwise or, \code{?}}
\index{arithmetic function!bitwise xor, \code{\^}}
\index{arithmetic function!bitwise complement, \code{\textasciitilde}}
Besides integers (constant arithmetic functions),
written as sequences of the digits \code{0}\dots\code{9}
possibly preceded by `\code{-}',
\gringo\ and \clingo\ support a variety of arithmetic functions that
are evaluated during grounding.
The following symbols are used for these functions:
\code{+} (addition),
\code{-} (subtraction, unary minus),
\code{*} (multiplication),
\code{/} (integer division),
\code{\textbackslash} (modulo),
\code{**} (exponentiation),
\code{|$\cdot$|} (absolute value),
\code{\&} (bitwise AND),
\code{?} (bitwise OR),
\code{\^} (bitwise exclusive OR), and
\code{\textasciitilde} (bitwise complement).
\begin{example}\label{ex:arith:fun}
The usage of arithmetic functions is illustrated by the program:%
\marginlabel{%
The unique answer set of the program,
obtained after evaluating all arithmetic functions,
can be inspected by invoking:\\
\code{\mbox{~}clingo --text \textbackslash \\ \mbox{~}\attach{examples/arithf.lp}{arithf.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash \\ \mbox{~}\attach{examples/arithf.lp}{arithf.lp}}}
%
\lstinputlisting{examples/arithf.lp}
%
Note that the variables~\var{L} and~\var{R} are instantiated to~\const{7} and~\const{2},
respectively, before arithmetic evaluations.
Consecutive and non-separative (e.g., before `\code{(}')
spaces can optionally be dropped.
The four bitwise functions apply to signed integers,
using two's complement arithmetic.
\end{example}
\begin{note}\label{note:simple}
An occurrence of a variable in the scope of an arithmetic function
only counts as positive in the sense of safety (cf.\ Page~\pageref{pg:safe}) for simple arithmetic terms.
Such simple arithmetic terms are terms with exactly one variable occurrence
composed of the arithmetic functions `\code{+}', `\code{-}', `\code{*}', and integers.
Moreover,
if multiplication is used, then the constant part must not evaluate to $0$ for the variable occurrence to be considered positive.
E.g., the rule~\code{\pred{q}(\var{X})\,:-\:\pred{p}(\const{2}*(\var{X}+\const{1})).}
is considered safe, but
the rule~\code{\pred{q}(\var{X})\,:-\:\pred{p}(\var{X}+\const{X}).} is not.
\index{safety!arithmetic function}
\end{note}
\subsubsection{Built-in Comparison Predicates}\label{subsec:gringo:comp}
\index{comparison predicate}
\index{comparison predicate!inequality, \code{"!=}}
\index{comparison predicate!less, \code{<}}
\index{comparison predicate!less or equal, \code{<=}}
\index{comparison predicate!greater, \code{>}}
\index{comparison predicate!greater or equal, \code{>=}}
\index{atom!comparison predicate@\gobblecomma|see{comparison predicate}}
Grounder \gringo\ (and \clingo)
feature a total order among variable-free terms (without arithmetic functions).
The built-in predicates to compare terms are
\code{=} (equal),
\code{!=} (not equal),
\code{<} (less than),
\code{<=} (less than or equal),
\code{>} (greater than), and
\code{>=} (greater than or equal).
\emph{Comparison literals} over the above \emph{comparison predicates} are used like
other literals (cf.\ Section~\ref{subsec:gringo:normal})
but are evaluated during grounding.
\begin{example}\label{ex:arith:pred}
The application of comparison literals to integers
is illustrated by the following program:%
\marginlabel{%
The simplified ground program obtained by evaluating built-ins
can be inspected by invoking:\\
\code{\mbox{~}clingo --text \textbackslash}\\
\code{\mbox{~}\attach{examples/arithc.lp}{arithc.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash}\\
\code{\mbox{~}\attach{examples/arithc.lp}{arithc.lp}}}
%
\lstinputlisting{examples/arithc.lp}
%
The last two lines hint at the fact that arithmetic functions are evaluated
before comparison literals, so that the latter actually compare the
results of arithmetic evaluations.
\end{example}
\begin{example}\label{ex:symb:pred}
Comparison literals can also be applied to constants and functions,
as illustrated by the following program:%
\marginlabel{%
As above, by invoking:\\
\code{\mbox{~}clingo --text \textbackslash \\ \mbox{~}\attach{examples/symbc.lp}{symbc.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash \\ \mbox{~}\attach{examples/symbc.lp}{symbc.lp}}\\
one can inspect the simplified ground program
obtained by evaluating built-ins.}
%
\lstinputlisting{examples/symbc.lp}
%
Integers are compared in the usual way, constants are ordered lexicographically,
and functions both structurally and lexicographically.
Furthermore, all integers are smaller than constants,
which in turn are smaller than functions.
\end{example}
The built-in comparison predicate~`\code{=}' has another interesting use case.
Apart from just testing whether a relation between two terms holds,
it can be used to define shorthands (via unification) for terms.
\begin{example}\label{ex:define}
This usage is illustrated by the following program:%
\marginlabel{%
The simplified ground program can be inspected by invoking:\\
\code{\mbox{~}clingo --text \textbackslash \\ \mbox{~}\attach{examples/define.lp}{define.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash \\ \mbox{~}\attach{examples/define.lp}{define.lp}}}
%
\lstinputlisting[lastline=2,belowskip=0pt]{examples/define.lp}%
\lstinputlisting[firstline=3,aboveskip=0pt,numbers=none,nolol]{examples/define.lp}
%
The body of the rule in Line~2 defines four comparison predicates over~`\code{=}',
which directly or indirectly depend on~\var{X} and~\var{Y}.
The values of~\var{X} and~\var{Y} are obtained via instances of the predicate \pred{num}/$1$.
The first comparison predicate depends on~\var{X} to provide shortcut~\var{XX}.
Similarly, the second comparison predicate depends on~\var{Y} to provide shortcut~\var{YY}.
The third comparison predicate provides variable \var{Y'} because it occurs in a simple arithmetic term,
which is solved during unification.
The last comparison predicate provides no variables and,
hence, is just a test,
checking whether its left-hand and right-hand sides are equal.
\end{example}
\begin{example}\label{ex:unify}
This example illustrates how to unify with function terms and tuples:
\marginlabel{%
The simplified ground program can be inspected by invoking:\\
\code{\mbox{~}clingo --text \textbackslash \\ \mbox{~}\attach{examples/unify.lp}{unify.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash \\ \mbox{~}\attach{examples/unify.lp}{unify.lp}}}
%
\lstinputlisting{examples/unify.lp}
%
Here, \code{\const{f}(\const{a},\var{X},\var{X}+\const{1})} or
\code{(\const{a},\var{X},\var{X}+\const{1})}, respectively,
is unified with instances of the predicate \code{sym}/$1$.
To this end,
arguments of \code{sym}/$1$ with matching arity
are used to instantiate the variable~\var{X} occurring
as the second argument in terms on the left-hand sides of~\code{=}.
With a value for~\var{X} at hand,
we can further check whether the arithmetic evaluation of~\code{\var{X}+\const{1}},
occurring as the third argument, coincides with the
corresponding value given on the right-hand side of~`\code{=}'.
%
\end{example}
\begin{note}
Note that comparison literals can be preceded by \code{not} or \code{not}~\code{not}.
In the first case, this is equivalent to using the complementary comparison literal
(e.g.,~`\code{<}' and~`\code{>=}' complement each other).
In the second case, the prefix has no effect on the meaning of the literal.
An occurrence of a variable in the scope of a built-in comparison literal over~`\code{!=}', `\code{<}', `\code{<=}',`\code{>}', or `\code{>=}'
does not count as a positive occurrence in the sense of safety (cf.\ Page~\pageref{pg:safe}),
i.e.,
such comparison literals are not considered to be positive.
Unlike with the built-in comparison literals above,
comparisons predicates over~`\code{=}' are considered as positive (body) literals in the sense of safety (cf.\ Page~\pageref{pg:safe}),
so that variables occurring on one side can be instantiated.
However, this only works when unification can be made directionally,
i.e., it must be possible to instantiate one side without knowing the values of variables on the other side.
For example, the rule~\code{\pred{p}(\var{X})\,:-\:\var{X}\:=\:\var{Y},\:\var{Y}\:=\:\var{X}.}
is not accepted by \gringo\ (or \clingo)
because values for~\var{X} rely on values for~\var{Y}, and vice versa.
Only simple arithmetic terms can be unified with (cf.\ Remark~\ref{note:simple}).
Hence, variable~\var{X} in literal~\code{X*X=8} must be bound by some other positive literal.
%
\index{safety!comparison predicate}%
\end{note}
\subsubsection{Intervals}\label{subsec:gringo:interval}
\index{term!interval}
Line~1 of Example~\ref{ex:define} contains
five facts of the form \code{\pred{num}($k$).}
over consecutive integers~$k$.
For a more compact representation,
\gringo\ and \clingo\ support integer intervals of the form $i$\code{..}$j$.
Such an interval, representing each integer~$k$ such that $i\leq k\leq j$,
is expanded during grounding.
An interval is expanded differently depending on where it occurs.
In the head of a rule, an interval is expanded conjunctively,
while in the body of a rule, it is expanded disjunctively.
So we could have simply written \code{num(1..5).} to represent the five facts.
\begin{example}\label{ex:int}
Consider the following program:
\marginlabel{%
The simplified ground program obtained from intervals
can be inspected by invoking:\\
\code{\mbox{~}clingo --text \attach{examples/int.lp}{int.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \attach{examples/int.lp}{int.lp}}}
\lstinputlisting{examples/int.lp}
Because all intervals in the second rule occur in the rule head,
they expand conjunctively.
Furthermore, the two intervals expand into the cross product \code{(1..3)$\times$(1..3)},
resulting in the following set of facts:
\begin{lstlisting}[escapechar=@,firstnumber=2]
grid(1,1). grid(1,2). grid(1,3).@\\@grid(2,1). grid(2,2). grid(2,3).@\\@grid(3,1). grid(3,2). grid(3,3).
\end{lstlisting}
Similarly, intervals can be used in a rule body.
Typically, this is done using comparison literals over `\code{=}', which expand disjunctively:
\begin{lstlisting}[firstnumber=2]
grid(X,Y) :- X = 1..S, Y = 1..S, size(S).
\end{lstlisting}
This rule expands into the same set of facts as before.
But intervals in comparison literals have the advantage that additional constraints can be added.
For example, one could add the comparison literals \code{X-Y!=0} and \code{X+Y-1!=S}
to the rule body to exclude the diagonals of the grid.
\end{example}
\begin{note}
An occurrence of a variable in the specification of the bounds
of an integer interval, like~\var{S} in Line~2 of Example~\ref{ex:int},
does not count as a positive occurrence
in the sense of safety (cf.\ Page~\pageref{pg:safe}).
Hence, such a variable must also have another positive occurrence elsewhere;
here in \lstinline{size(S)}.
\index{safety!interval}%
\end{note}
\subsubsection{Pooling}\label{subsec:gringo:pool}
\index{pooling, \code{;}}
\index{term!pooling@\gobblecomma|see{pooling}}
\index{atom!pooling@\gobblecomma|see{pooling}}
The token `\code{;}' admits pooling alternative terms
to be used as arguments of an atom, function, or tuple.
Argument lists written in the form \code{($\dots$,X;Y,$\dots$)} abbreviate multiple options:
\code{($\dots$,X),\linebreak[1]\:(Y,$\dots$)}.
Pools are expanded just like intervals, i.e.,
conjunctively in the head and disjunctively in the body of a rule.
In fact, the interval \code{1..3} is equivalent to the pool \code{(1;2;3)}.%
\footnote{%
We make use of the fact that one-elementary tuples
must be made explicit by a trailing `\code{,}' (cf.\ Section~\ref{subsec:gringo:terms}).
E.g., \code{(1;1,)} expands into \code{(1)} and \code{(1,)},
where \code{(1)} is equal to the integer~\code{1}.
On the other hand, note that the rule
\code{\pred{p}(\var{X})\,:-\;\var{X}\;=\,(1,2;3,4).}
is expanded into
\code{\pred{p}((1,2)).} and \code{\pred{p}((3,4)).},
given that \code{(1,2)} and \code{(3,4)} are proper tuples,
and the same facts are also obtained from
\code{\pred{p}((1,2;3,4)).}
Unlike that, \code{\pred{p}(1,2;3,4).} yields
\code{\pred{p}(1,2).} and \code{\pred{p}(3,4).}
because `\code{;}' here splits an argument list,
rather than a tuple.}
\begin{example}\label{ex:pool}
The following program makes use of pooling.
It is similar to Example~\ref{ex:int}
but with the difference that, unlike intervals, pools have a fixed size:%
\marginlabel{%
The simplified ground program obtained from pools
can be inspected by invoking:\\
\code{\mbox{~}clingo --text \textbackslash\newline\mbox{~}\attach{examples/pool.lp}{pool.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash\newline\mbox{~}\attach{examples/pool.lp}{pool.lp}}}
\lstinputlisting[xrightmargin=-5pt]{examples/pool.lp}
%
Because all pools in this rule occur in the head,
they are expanded conjunctively.
Furthermore, the two pools expand into the cross product \code{(1..3)$\times$(1..3)},
resulting again in the following set of facts:
\begin{lstlisting}[numbers=none]
grid(1,1). grid(1,2). grid(1,3).
grid(2,1). grid(2,2). grid(2,3).
grid(3,1). grid(3,2). grid(3,3).
\end{lstlisting}
Like intervals, pools can also be used in the body of a rule,
where they are expanded disjunctively:
\begin{lstlisting}
grid(X,Y) :- X = (1;2;3), Y = (1;2;3).
\end{lstlisting}
This rule expands into the same set of facts as before.
As in Example~\ref{ex:int}, additional constraints involving~\code{X} and~\code{Y} can be added.
\end{example}
For another example on pooling, featuring non-consecutive elements, see Section~\ref{subsec:color:instance}.
\subsubsection{Conditions and Conditional Literals}\label{subsec:gringo:condition}
\index{aggregate!condition}
\index{conditional literal}%
\index{literal!conditional}%
%
A \emph{conditional literal} is of the form
\[\code{$L_0$:$L_1$,$\dots$,$L_n$}\]
%
where every $L_j$ for $0\leq j\leq n$ is a \emph{literal},
\code{$L_1$,$\dots$,$L_n$} is called \textit{condition},
and `\code{:}' resembles mathematical set notation.
Whenever $n=0$, we get a regular literal and denote it as usual by $L_0$.
For example, the rule
\begin{lstlisting}[numbers=none]
a :- b : c.
\end{lstlisting}
yields \code{a} whenever either \code{c} is false (and thus no matter whether \code{b} holds or not) or both \code{b} and \code{c} are true.
\begin{note}
Logically, $L_0$ and \code{$L_1$,$\dots$,$L_n$} act as
head and body, respectively,
which gives \code{$L_0$:$L_1$,$\dots$,$L_n$}
the flavor of a nested implication
(see~\cite{haliya14a} for details).
\end{note}
Together with variables,
conditions allow for specifying collections of expressions within a single rule or aggregate.
This is particularly useful for encoding conjunctions (or disjunctions) over
arbitrarily many ground atoms as well as for the compact representation of aggregates
(detailed in Section~\ref{subsec:gringo:aggregate}).
\begin{example}\label{ex:cond}
The following program uses, in Line~5 and~6, conditions in a rule body and in a rule head, respectively:
%
\lstinputlisting{examples/cond.lp}
%
The rules in Line~5 and~6 are instantiated as follows:%
\marginlabel{%
The reader can reproduce these ground rules by invoking:\\
\code{\mbox{~}clingo --text \textbackslash \\ \mbox{~}\attach{examples/cond.lp}{cond.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash \\ \mbox{~}\attach{examples/cond.lp}{cond.lp}}}
\begin{lstlisting}[numbers=none]
meet :- available(jane), available(john).
on(mon); on(tue); on(wed); on(thu); on(fri) :- meet.
\end{lstlisting}
%
The conjunction in the body of the first ground rule is obtained by replacing~\var{X} in
\code{\pred{available}(\var{X})} with all ground terms~$t$ such that
\code{\pred{person}($t$)} holds, namely, with $t=\const{jane}$ and $t=\const{john}$.
Furthermore, the condition in the head of the rule in Line~6 turns into
a disjunction over all ground instances of
\code{\pred{on}(\var{X})} such that \var{X} is substituted by terms~$t$
for which \code{\pred{day}($t$)} holds.
That is, conditions in the body and in the head of a rule
are expanded to different basic language constructs.\footnote{%
Recall our suggestion from Section~\ref{subsec:gringo:disjunction}
to use ``choice constructs'' (detailed in Section~\ref{subsec:gringo:aggregate})
instead of disjunction, unless the latter is required for complexity reasons.
This also means that conditions must not be used \emph{outside of aggregates} in rule heads
if disjunction is unintended.}
\end{example}
Further following set notation,
a condition can be composed by separating literals with a comma, viz.~`\code{,}'.
Note that commas are used to separate both literals in rule bodies as well as conditions.
To resolve this ambiguity,
a condition is terminated with a semicolon `\code{;}' (rather than `\code{,}')
when further body literals follow.
\begin{example}\label{ex:sort}
The following program uses a literal with a composite condition in the middle of the rule body.
Note the semicolon `\code{;}' after the condition:
%
\lstinputlisting[breaklines,breakatwhitespace]{examples/sort.lp}
%
The conditional literal in the second rule evaluates to false
whenever there is an element~\code{Y} between~\code{X} and~\code{Z}.
Hence, all rule instantiations where~\code{X} and~\code{Z} are not direct successors are discarded
because they have a false body.
On the other hand, whenever~\code{X} and~\code{Z} succeed each other,
the condition is false for all elements~\code{Y}.
This means that the literal with condition stands for an empty conjunction, which is true:%
\marginlabel{%
The reader can reproduce these ground rules by invoking:\\
\code{\mbox{~}clingo --text \textbackslash \\ \mbox{~}\attach{examples/sort.lp}{sort.lp}}\\
or alternatively:\\
\code{\mbox{~}gringo --text \textbackslash \\ \mbox{~}\attach{examples/sort.lp}{sort.lp}}}%
\begin{lstlisting}[numbers=none]
set(1). set(2). set(3). set(4).
next(1,2). next(2,3). next(3,4).
\end{lstlisting}
We obtain an answer set where the elements of \code{set}/$1$ are ordered via \code{next}/$2$.
\end{example}
\begin{note}\label{note:domain}
There are three important issues about the usage of conditions:
\begin{enumerate}
%
\item
Any variable occurring within a condition
does not count as a positive occurrence outside the condition
in the sense of safety (cf.\ Page~\pageref{pg:safe}).
Variables occurring in atoms not subject to any condition are \emph{global}.
Each variable within an atom in front of a condition
must be global or have a positive occurrence on the right-hand side of
the condition.
%
\item
During grounding,
the instantiation of global variables takes precedence over non-global ones,
that is, the former are instantiated before the latter.
As a consequence, variables that occur globally are substituted by terms
before a condition is further evaluated.
Hence, the names of variables in conditions must be chosen with care,
making sure that they do not \emph{accidentally} match the names of global variables.
%
\item
We suggest using \emph{domain predicates}~\cite{lparseManual}
or built-ins (both used in Line~3 of Example~\ref{ex:sort})
in conditions.
\index{domain predicate}%
\label{pg:domain}%
Literals over such predicates are completely evaluated during grounding.
In a logic program, domain predicates can be recognized by observing
that they are neither subject to negative recursion (through \code{not})
nor to disjunction or ``choice constructs'' (detailed in Section~\ref{subsec:gringo:aggregate})
in the head of any rule.
The domain predicates defined in Example~\ref{ex:sort} are
\pred{set}/$1$ and \pred{next}/$1$.
Literals with such conditions expand to arbitrary length disjunctions or conjunctions in the head or body of a rule, respectively.
Otherwise, conditions give rise to nested implications.
For further details see~\cite{haliya14a}.
\end{enumerate}
\end{note}
\subsubsection{Aggregates}\label{subsec:gringo:aggregate}
\index{atom!aggregate@\gobblecomma|see{aggregate}}
\index{aggregate}
Aggregates are expressive modeling constructs that allow for forming values from groups of selected items.
Together with comparisons they allow for expressing conditions over these terms.
For instance, we may state that the sum of a semester's course credits must be at least 20,
or that the sum of prizes of shopping items should not exceed 30 Euros.
More formally,
an aggregate is a function on a set of tuples that are normally subject to conditions.
By comparing an aggregated value with given values, we can extract a truth value from an aggregate's evaluation,
thus
obtaining an aggregate atom.
Aggregate atoms come in two variants depending on whether they occur in a rule head or body.
\paragraph{Body Aggregates}
The form of an \emph{aggregate atom} occurring in a rule body is as follows:
\index{aggregate!body}
%
\[\code{$s_1$~$\prec_1$~$\alpha$~\{~$\boldsymbol{t}_1$:$\boldsymbol{L}_1$;$\dots$;$\boldsymbol{t}_n$:$\boldsymbol{L}_n$ \}~$\prec_2$~$s_2$}\]
%
Here, all $\boldsymbol{t}_i$ and $\boldsymbol{L}_i$, forming \emph{aggregate elements}, are tuples of terms and literals
(as introduced in Section~\ref{subsec:gringo:terms}), respectively.
If a literal tuple is empty and the corresponding term tuple is non-empty, then the colon can be omitted.
$\alpha$ is the name of some function that is to be applied to the term tuples $\boldsymbol{t}_i$
that remain after evaluating the conditions expressed by $\boldsymbol{L}_i$.
%
Finally,
the result of applying $\alpha$ is compared by means of the comparison predicates $\prec_1$ and $\prec_2$
to the terms $s_1$ and~$s_2$, respectively.
Note that one of the \emph{guards} `\code{$s_1$~$\prec_1$}' or `\code{$\prec_2$~$s_2$}'
(or even both) can be omitted;
left out comparison predicates $\prec_1$ or $\prec_2$ default to `\code{<=}',
thus interpreting~$s_1$ and~$s_2$ as lower or upper bound, respectively.
Currently, \gringo\ (and \clingo) support the aggregates
\code{\#count}
\index{aggregate!count, \code{\#count}}%
(the number of elements; used for expressing cardinality constraints),
\code{\#sum}
\index{aggregate!sum, \code{\#sum}}%
(the sum of weights; used for expressing weight constraints),
\code{\#sum+}