Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog, using expressions

Tags:

prolog

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))
like image 298
Lyndon White Avatar asked May 30 '11 03:05

Lyndon White


People also ask

What technique does Prolog use to match expressions?

Prolog uses the unification technique, and it is a very general form of matching technique. In unification, one or more variables being given value to make the two call terms identical.

What does :- mean in Prolog?

body the last part of a Prolog rule. It is separated from the head by the neck symbol, written as :- . It has the form of a comma-separated list of goals, each of which is a the name part of a functor, possibly followed by a comma-separated list of arguments, in parentheses.

How do you represent or in Prolog?

or, logical disjunction, ; The logical conjunction (logical-and, ∧) of goals is achieved in Prolog by separating the goals by commas: happy(X) :- rich(X), famous(X). says that X is happy if X is rich and X is famous.


1 Answers

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).
like image 53
false Avatar answered Nov 09 '22 05:11

false