I see slashes used like this in code:
solution([X/Y|Others]) :-
noattack(X/Y, Others).
But then sometimes I see "/1" "/2" etc. in Prolog.
What do these statements (characters) mean?
I see slashes used like this in code:
solution([X/Y|Others]) :-
noattack(X/Y, Others).
But then sometimes I see "/1" "/2" etc. in Prolog.
What do these statements (characters) mean?
X/Y
is infix syntax for the term /(X, Y)
, that is, a term whose functor is "/", with two arguments: X and Y. It is possible to use infix syntax in this case because / is defined as one of the default operators in ISO Prolog. Just like a+b
is infix syntax for the term +(a, b)
and X is Y + Z
is infix syntax for is(X, +(Y, Z))
.
Well, in your code it might just be used as a separator. It serves for pattern matching purposes through unification:
A-B = 1-2.
will return
A = 1,
B = 2.
because we used the delimiter -
to help match content. Here the delimiter would be /
.
The other place where you would see /1
and /2
a lot is when you describe predicates.
somepredicate/arity
indicates that the predicate somepredicate
takes arity
arguments.
Example:
% member/2 : member(?Element, ?List)
member(Element, [Element|_Tail]).
member(Element, [_Head|Tail]) :-
member(Element, Tail).
Here our first line says that member takes 2 argument.
This form can also be used, in swi-prolog for example, to specify which predicate you target in predicates such as listing/1
: there you can pass as an argument maplist/2
or maplist/3
and the result will be different.
In both your examples it's used as infix operator, but with different, unrelated, meaning.
In the first rule you cite it's just a separator, as Mog already pointed out. The other use, mostly for documentation purposes, it's as predicate indicator. Useful because we can have different predicates with the same functor.
Expressions are just syntax sugar for binary or unary relations, where the functor is an operator. The meaning of such expressions is defined by context: for instance, is/2 takes care of arithmetic evaluation of expressions: here the operator performs the expected arithmetic operation
?- X is 10 / 3.
X = 3.3333333333333335.
The builtin current_op allows inspecting operators' definitions. Try
?- current_op(Precedence,Associativity,/).
Precedence = 400,
Associativity = yfx .
We can have a predicate named /. A silly example:
/(A, B) :- format(A, B).
or better
A / B :- format(A, B).
could be used as a shorthand where we have many format(s). This usage is discouraged, leading to hard to read programs, but given such definition, this is a valid rule:
?- 'hello ~s' / [world].
hello world