Swi Prolog: how to count the number of elements by using rules (not query)

As title stated above, how to make this possible? For example:

parent(child, parent).

parent(child, parent2).

parent(child2, parent).

parent(child2, parent2).

childof(X,Y) :- parent(Y, X).  
number_of_child(X,Y):- X has Y number of child

How should I implement the number_of_child rules? My expected answer is Y will show 2 (since there are child and child2) or something like that. Thank you.

Read more here: https://stackoverflow.com/questions/67930494/swi-prolog-how-to-count-the-number-of-elements-by-using-rules-not-query

Content Attribution

This content was originally published by Zong at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: