Search code examples
prolog

Prolog, using expressions


I'm trying to learn SWI prolog, but my simple program fails when I believe it should succeed.

%My code:
orthogonal((X1,Y1,Z1),(X2,Y2,Z2)) :- (X1*X2)+(Y1*Y2)+(Z1*Z2)==0.
integerVector((X,Y,Z)) :- integer(X),integer(Y),integer(Z).

?-orthogonal((1,0,0),(0,0,1)).

I press compile Buffer in the pseudoemacs window and the output is:

% [PATH].pl compiled 0.00 sec, 136 bytes
ERROR: emacs_prolog_mode ->error_at_location: Argument 1 (int): `int' expected, found `@11470948?start'
ERROR: emacs_prolog_mode ->error_at_location: Argument 1 (int): `int' expected, found `@11470948?start'
Warning: [PATH]s.pl:5:
        Goal (directive) failed: user:orthogonal((1,0,0), (0,0,1))

Solution

  • You have used (==)/2 in place of (=:=)/2 which evaluates its arguments as arithmetic expressions.

    You can use (X,Y,Z), but it isn't a triple as in e.g. Haskell. To see this:

    ?- write_canonical((1,2,3)).
    ','(1,','(2,3))
    
    ?- (1,2,3) = (X,Y).
       X = 1, Y = (2,3).