1

I've got the protocol:

:- protocol(person).
:- public([name/1,
        age/1]).
:- end_protocol.

For example, I've made unknown number of objects by using create_object/4, how can I get a number of them? It is not a problem to get their names by current_object/1, but I need an integer!

1 Answers1

1

Assuming only objects (i.e. no categories) implement the person protocol, you can compute their number using e.g.

count(N) :-
    findall(1, implements_protocol(_,person), L),
    list::length(L, N).

Replace the call to implements_protocol /2 with conforms_to_protocol/2 if you have hierarchies of objects. You can also generalize the count/1 predicate by passing the protocol as an argument.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • Ok, it works. Anyway, on the next step I wanted to count objects with some relationships, e.g. count only these objects, where Person::age(young). Have you any idea how to handle that? – Matek Statek Jan 08 '19 at 05:12
  • When a SO answer is correct, the proper procedure is to mark it as accepted instead of using a comment for that. Also, don't ask followup questions in comments. Instead, create a new question. – Paulo Moura Jan 08 '19 at 09:06