Jervis Chen
Jervis Chen

Reputation: 3

I am try to write a small Prolog predicate

I am trying to write a Prolog predicate. Assume we have data type A and type B. We want to get all the possible combination for both type such that:

x=no y=(1,3)
x=no y=(3,4)
x=yes y=(1,3)
....

That is the code we have:

A(1,3).
A(3,4).
B(no).
B(yes).

C(X,Y):-
   ***************.

Do we need to create a type C which contain two type

like
C(B,A)?

or we can just do the match in the C..

C(X,Y):-
C(B(_),A(_,_)).

Anyone can give a hit?

Upvotes: 0

Views: 88

Answers (1)

SimoV8
SimoV8

Reputation: 1402

As luker said, predicates in Prolog must start with a lower case letter and variables with an upper case one. Said that, I think you want something like that:

a(1,3).
a(3,4).
b(no).
b(yes).

c(a(X,Y), b(Z)):- a(X, Y), b(Z).

The last line means that c's arguments are of type a and b and that there must exist some values X, Y, Z such that they satisfy the a and b predicates. Prolog will automatically unify variables in the header and body of the predicate.

In such a way you can query all combinations of your values:

?- c(X, Y).
X = a(1, 3),
Y = b(no) ;
X = a(1, 3),
Y = b(yes) ;
X = a(3, 4),
Y = b(no) ;
X = a(3, 4),
Y = b(yes).

Or you can query for specific values:

?- c(a(1,3), b(yes)).
true.
?- c(a(1,5), b(yes)).
false.

Upvotes: 1

Related Questions