You may want to watch this video, where we explain sum aggregates in answer set programming.
The video does not explain count aggregates, but they are very similar to sum aggregates. They also define a set, but they simply count the number of tuples in that set (instead of adding the first element of every tuple of that set).
%%clingo -V0 0
a(1,1). a(1,2). a(2,1). a(2,2). a(2,3).
b(1,1). b(2,2). b(3,3).
% c(S) if S is the cardinality of the set
% of all tuples X,Y such that a(X,Y) belongs to the answer set.
% In this case the set is { 1,1; 1,2; 2,1; 2,2; 2,3 } and S is 5.
c(S) :- S = #count{ X,Y : a(X,Y) }.
% d(S) if S is the cardinality of the set
% of all tuples X such that a(X,Y) belongs to the answer set.
% In this case the set is { 1; 2 } and S is 2.
d(S) :- S = #count{ X : a(X,Y) }.
% e(S) if S is the cardinality of the set
% of all tuples Y such that a(X,Y) belongs to the answer set.
% In this case the set is { 1; 2; 3 } and S is 3.
e(S) :- S = #count{ Y : a(X,Y) }.
#show c/1. #show d/1. #show e/1.
c(5) d(2) e(3) SATISFIABLE
%%clingo -V0 0
a(1,1). a(1,2). a(2,2). a(2,3).
b(1,1). b(3,3).
c(S) :- S = #count{ X,Y : a(X,Y) }.
d(S) :- S = #count{ X : a(X,Y) }.
e(S) :- S = #count{ Y : a(X,Y) }.
#show c/1. #show d/1. #show e/1.
%%clingo -V0 0
a(1,1). a(1,2). a(2,1). a(2,2). a(2,3).
b(1,1). b(2,2). b(3,3).
% c(S) if S is the cardinality of the set
% of all tuples X,Y such that a(X,Y) belongs to the answer set, and
% all tuples X,Y such that b(X,Y) belongs to the answer set.
% In this case the set is { 1,1; 1,2; 2,1; 2,2; 2,3; 3,3 } and S is 6.
c(S) :- S = #count{ X,Y : a(X,Y); X,Y : b(X,Y) }.
% d(S) if S is the cardinality of the set
% of all tuples X,Y such that a(X,Y) belongs to the answer set, and
% all tuples Z,W such that b(Z,W) belongs to the answer set.
% In this case the set is { 1,1; 1,2; 2,1; 2,2; 2,3; 3,3 } and S is 6.
d(S) :- S = #count{ X,Y : a(X,Y); Z,W : b(Z,W) }.
% e(S) if S is the cardinality of the set
% of all tuples X,Y,1 such that a(X,Y) belongs to the answer set, and
% all tuples X,Y,2 such that b(X,Y) belongs to the answer set.
% In this case the set is { 1,1,1; 1,2,1; 2,1,1; 2,2,1; 2,3,1; 1,1,2; 2,2,2; 3,3,2 } and S is 8.
e(S) :- S = #count{ X,Y,1 : a(X,Y); X,Y,2 : b(X,Y) }.
% f(S) if S is the cardinality of the set
% of all tuples X,Y such that a(X,Y) belongs to the answer set, and
% all tuples X,Y,2 such that b(Z,W) belongs to the answer set.
% In this case the set is { 1,1; 1,2; 2,1; 2,2; 2,3; 1,1,2; 2,2,2; 3,3,2 } and S is 8.
f(S) :- S = #count{ X,Y : a(X,Y); X,Y,2 : b(X,Y) }.
#show c/1. #show d/1. #show e/1. #show f/1.
c(6) d(6) e(8) f(8) SATISFIABLE
%%clingo -V0 0
a(1,1). a(1,2). a(2,2). a(2,3).
b(1,1). b(3,3).
c(S) :- S = #count{ X,Y : a(X,Y); X,Y : b(X,Y) }.
d(S) :- S = #count{ X,Y : a(X,Y); Z,W : b(Z,W) }.
e(S) :- S = #count{ X,Y,1 : a(X,Y); X,Y,2 : b(X,Y) }.
f(S) :- S = #count{ X,Y : a(X,Y); X,Y,2 : b(X,Y) }.
#show c/1. #show d/1. #show e/1. #show f/1.
%%clingo -V0 0
a(1,1). a(1,2). a(2,1). a(2,2). a(2,3).
b(1,1). b(2,2). b(3,3).
c(1). c(2).
% d(X,S) if c(X) and S is the cardinality of the set
% of all tuples Y such that a(X,Y) belongs to the answer set, and
% all tuples Z such that b(X,Z) belongs to the answer set.
% In this case when X is 1 the set is { 1; 2 } and S is 2, and
% when X is 2 the set is { 1; 2; 3 } and S is 3.
d(X,S) :- c(X), S = #count{ Y : a(X,Y); Z : b(X,Z) }.
% e(X,S) if c(X) and C is the cardinality of the set
% of all tuples Y,a such that a(X,Y) belongs to the answer set, and
% all tuples Z,b such that b(X,Z) belongs to the answer set.
% In this case when X is 1 the set is { 1,a; 2,a; 1,b } and S is 3, and
% when X is 2 the set is { 1,a; 2,a; 2,a; 2,b } and S is 4.
e(X,S) :- c(X), S = #count{ Y,a : a(X,Y); Z,b : b(X,Z) }.
#show d/2. #show e/2.
d(1,2) d(2,3) e(1,3) e(2,4) SATISFIABLE
%%clingo -V0 0
a(1,1). a(1,2). a(2,2). a(2,3).
b(1,1). b(3,3).
c(1). c(2).
d(X,S) :- c(X), S = #count{ Y : a(X,Y); Z : b(X,Z) }.
e(X,S) :- c(X), S = #count{ Y,a : a(X,Y); Z,b : b(X,Z) }.
#show d/2. #show e/2.
%%clingo -V0 0
n(1..2).
{ a(X) : n(X) }.
:- 2 #count{ X : a(X) }.
#show a/1.
a(2) a(1) SATISFIABLE
%%clingo -V0 0
n(1..3).
{ a(X) : n(X) }.
:- 2 #count{ X : a(X) }.
#show a/1.
%%clingo -V0 0
n(1..2).
{ a(X,Y) : n(X), n(Y) }.
:- 2 #count{ X,Y : a(X,Y) }.
#show a/2.
a(1,2) a(2,1) a(2,2) a(1,1) SATISFIABLE
%%clingo -V0 0
n(1..2).
{ a(X,Y) : n(X), n(Y) }.
:- 2 #count{ X : a(X,Y) }.
#show a/2.
This time we have another exercise for the same example.
%%clingo -V0 0
n(1..2).
{ a(X,Y) : n(X), n(Y) }.
:- 2 #count{ Y : a(X,Y) }.
#show a/2.
%%clingo -V0 0
n(1..2).
1 { a(X,Y) : n(X), n(Y) } 1.
1 { b(X,Y) : n(X), n(Y) } 1.
:- 2 #count{ X,Y : a(X,Y); Z,W : b(Z,W) }.
#show a/2. #show b/2.
a(2,2) b(2,2) a(1,2) b(1,2) a(2,1) b(2,1) a(1,1) b(1,1) SATISFIABLE
%%clingo -V0 0
n(1..2).
1 { a(X,Y) : n(X), n(Y) } 1.
1 { b(X,Y) : n(X), n(Y) } 1.
:- 2 #count{ X,Y,a : a(X,Y); Z,W,b : b(Z,W) }.
#show a/2. #show b/2.
%%clingo -V0 0
n(1..2).
1 { a(X,Y) : n(X), n(Y) } 1.
1 { b(X,Y) : n(X), n(Y) } 1.
ok(X) :- 2 #count{ Y : a(X,Y); Z : b(X,Z) }, n(X).
:- not ok(1), not ok(2).
#show a/2. #show b/2. #show ok/1.
b(1,1) a(1,2) ok(1) a(1,1) b(1,2) ok(1) b(2,1) a(2,2) ok(2) a(2,1) b(2,2) ok(2) SATISFIABLE
%%clingo -V0 0
n(1..2).
1 { a(X,Y) : n(X), n(Y) } 1.
1 { b(X,Y) : n(X), n(Y) } 1.
ok(X) :- 2 #count{ Y,first : a(X,Y); Z,second : b(X,Z) }, n(X).
:- not ok(1), not ok(2).
#show a/2. #show b/2. #show ok/1.