xIIPANIKIIx
xIIPANIKIIx

Reputation: 83

What does the / slash mean in this Prolog Predicate

Would anyone be able to explain to me what the forward slash '/' means in the context of this Prolog predicate. I've tried Googling it, reviewing other questions but I can't find a definitive answer, or at least one that makes sense to me. I'm aware of what arity is but I'm not sure this is related.

move_astar([Square | Path] / G / _, [NextSquare, Square | Path] / SumG / NewH) :-
     square(Square, NextSquare, Distance),
     not(member(NextSquare, Path)),
     SumG is G + Distance,
     heuristic(NextSquare, NewH).

Upvotes: 3

Views: 157

Answers (3)

false
false

Reputation: 10102

The / has no meaning here, except for being a structure and a left associative operator. So it is unrelated to division. Sometimes people like to add such decoration to their code. It is hard to tell if this serves anything from that single clause, but think of someone who just wants to refer to the list and the two values like in:

?- start(A), move_astar(A,A).

So here it would be much more compact to ask that question than to hand over each parameter manually.

Another use would be:

?- start(A), closure(move_astar, A,B).

Using closure/2. That is, existing predicates may expect a single argument.

Upvotes: 2

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

It has no implicit meaning, and it is not the same as arity. Prolog terms are name(Arg1, Arg2) and / can be a name. /(Arg1, Arg2).

There is a syntax sugar which allows some names to be written inline, such as *(X,Y) as X * Y and /(X,Y) as X / Y which is useful so they look like arithmetic (but NB. this does not do arithmetic). Your code is using this syntax to keep three things together:

?- write_canonical([Square | Path] / G / _).

/(/([_|_],_),_)

That is, it has no more semantic meaning than xIIPANIKIIx(X,Y) would have, it's Ls/G/H kept together.

Upvotes: 2

brebs
brebs

Reputation: 4438

It's an outdated style. It's bad because:

  • It conveys no useful information, apart from being a weird-looking delimiter
  • There's a slight performance hit for Prolog having to assemble and re-assemble those slash delimiters for parsing

It's better to either:

  • Keep parameters individual (and therefore fast to use)
  • Group parameters in a reasonably-named term, or e.g. v if brevity is more appropriate than classification of the term

Upvotes: 1

Related Questions