Flux
Flux

Reputation: 10920

How to check that all predicates of a specified name have the specified number of arguments?

Suppose I have many predicates used to hold data. I have manually entered all that data by hand. Now I need to ensure that all of them have the same number of arguments.

Take the example below, where I need to ensure that data always has 6 arguments:

% data(Id, Name, Age, Street, City, Phone).
data(1, 'John', 12, "1 New Street", "Central City", "873289712").
data(2, 'Ali', 11, "122 Sesame Street", "Central City", "823283821").
% ...

How can I write a predicate that checks whether or not all data predicates have the correct number of arguments? If the number of arguments is incorrect for a particular entry, I would like to know which entry is incorrect.

For example:

CheckArguments(data, 6).

Should give false if there exists a predicate data(2, 'Farah')., because this entry only has 2 arguments out of the required 6. In addition, the predicate should have some way of telling me that the offending predicate is data(2, 'Farah')..

Upvotes: 1

Views: 113

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

Use the ISO Prolog standard predicate current_predicate/1. For example, after loading your data clauses:

| ?- current_predicate(data/Arity).

Backtrack into all solutions to get all the arities for user-defined predicates with name data.

Upvotes: 1

Related Questions