I am trying to develop a program that will be able to solve an analogy IQ test. As you can see below I've written the figures and relations as facts and I've also made an analogy Predicate that should return one figure number when given three other figure numbers. The first two figures are connected through a relation and the 3rd and 4th are also connected through the same relation.
For example, analogy should work like this: analogy(1,5,3,X). X=7.
figure 1 -> middle(triangle, square)
figure 5 -> middle(square, triangle)
relation between 1 & 5 -> inverse
figure 3 -> middle(square, circle)
figure 7 -> middle(circle, square)
relation between 3 & 7 -> inverse
when I compile the code I get the following exception: file test lab 1.ecl, line 105: syntax error: unexpected token | figure(S1, _(Sh1,Sh2)), | ^ here
What do I do wrong?
figure(1, middle(triangle, square)).
figure(2, middle(circle, triangle)).
figure(3, middle(square, circle)).
figure(4, middle(square, square)).
figure(5, middle(square, triangle)).
figure(6, middle(triangle, circle)).
figure(7, middle(circle, square)).
figure(8, middle(triangle, triangle)).
figure(9, samepattern(lowerleft, circle)).
figure(10, samepattern(upperleft, circle)).
figure(11, samepattern(lowerright, circle)).
figure(12, samepattern(upperright, circle)).
figure(13, samepattern(upperleft, square)).
figure(14, samepattern(lowerleft, square)).
figure(15, samepattern(upperright, square)).
figure(16, samepattern(lowerright, square)).
relation(middle(S1,S2), middle(S2,S1), inverse).
relation(samepattern(S1,S2), samepattern(S1,S3), spinverse).
analogy(S1,S2,S3,S4):-
figure(S1, _(Sh1,Sh2)),
figure(S2, _(Sh3,Sh4)),
relation(_(Sh1,Sh2),_(Sh3,Sh4),R),
figure(S3, _(Sh5,Sh6)),
figure(S4, _(Sh7,Sh8)),
relation(_(Sh5,Sh6),_(Sh7,Sh8),R).
The name of a term must be an atom. Thus, a term such as _(X,Y)
is not accepted (because _
is a variable) and causes a syntax error. Thus, you need to modify the definition of predicate analogy/4
as following:
analogy(S1,S2,S3,S4):-
figure(S1, F1),
figure(S2, F2),
relation(F1, F2, R),
figure(S3, F3),
figure(S4, F4),
relation(F3, F4, R).
Observe that, in the predicate analogy/4
, the specific arguments in figures denoted by terms F1
, F2
, F3
and F4
are not accessed individually and, consequently, it is not necessary to name each one of them explicitly.
Running example:
?- analogy(1,5,3,X).
X = 7 ;
false.