0
votes

I have to make a predicate/2 which takes a list of "correct" numbers as validation, and a function which needs to be validated. The numbers in the function (represented as number(X)) has to be the same as the numbers in the validation list, to become "true".

I have no problem making two seperate predicates/1 which give the correct answer when the list is "hard-coded" in the library, but I can for the god of mine not combine these.

I have this so far:

number(X) :- member(X, [1,2,3,4,5]).

This gives the correct result as when i ask for for example and(number(2),number(4)), it says yes, and and(number(2),number(6)) gives no.

But now I have to do predicate/2, which takes the list as the first argument. Can anyone help/give any hint?

predicate(ValidationList, Function) :- ???? 
2
not sure to unserstand... and(number(2),number(4)) and and(number(2),number(6)) are examples of functions to validate? Or what do you mean (can you show some examples?) with "function which needs to be validated"? Anyway... can you show your predicate/1? - max66
yes, these are exaples of functions to be validated. It's about propositional formulas. If "and(number(1),number(2))" is to be correct, then the validation list has to contain the numbers 1 and 2. That's why "number(X) :- member(X, [1,2,3,4,5])" works. My problem is to define a predicate, so that I can afterwards type for example "predicate([1,3,5,7] , or(number(1),number(2)))." And it checks if 1 and 2 are a part of the list. - DaoDib

2 Answers

0
votes

You can use call predicate:

and(X,Y) :- call(X), call(Y).
or(X,Y) :- call(X); call(Y).

predicate(List, Function) :-
    Function =.. [P,X,Y],
    call(P, member(X, List), member(Y, List)).

For example:

?- predicate([1,2,3], and(1,3)).
true.

?- predicate([1,2,3], and(1,4)).
false.

?- predicate([1,2,3], or(1,4)).
true.

?- predicate([1,2,3], or(4,5)).
false.
0
votes

Another solution could be

predicate(ValList, and(X, Y)) :-
    predicate(ValList, X),
    predicate(ValList, Y).

predicate(ValList, or(X, Y)) :-
      predicate(ValList, X)
    ; predicate(ValList, Y).

predicate(ValList, number(X)) :-
    member(X, ValList).