Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of predicates in Prolog

Tags:

list

prolog

Is it possible to define a list, that consists of predicates and how do I call the predicates.

Also, is it possible to pass one predicate to another predicate (like passing atoms)?

Example:

pre1:- something.
pre2(Predicate1, List):-
    call(Predicate1),
    append([Predicate1], List, R),
    .....
like image 256
Rob Fox Avatar asked Oct 29 '25 21:10

Rob Fox


1 Answers

You can't store predicates in a list, but you can store terms (or functors) and call terms as goals.

Here's a predicate that tests whether a term has the properties described by a list of functors:

has_properties([], _).
has_properties([P|Ps], X) :-
    Goal =.. [P, X],            % construct goal P(X)
    call(Goal),
    has_properties(Ps, X).

Usage:

% is 4 a number, an integer and a foo?
?- has_properties([number, integer, foo], 4).

The answer to this query will depend on your definition of foo/1, of course. See my explanation of =.. if needed.

Edit: as @false reports in the comments, it's not necessary to use =.., since Goal =.. [P, X], call(Goal) can be replaced by call(P, X) will have the same effect. It might still be worthwhile learning about =.., though, as you may encounter it in other people's code.

like image 63
Fred Foo Avatar answered Nov 03 '25 04:11

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!