English 中文(简体)
What s the -> operator in Prolog and how can I use it?
原标题:

I ve read about it in a book but it wasn t explained at all. I also never saw it in a program. Is part of Prolog syntax? What s it for? Do you use it?

最佳回答

It represents implication. The righthand side is only executed if the lefthand side is true. Thus, if you have this code,

implication(X) :-
  (X = a ->
    write( Argument a received. ), nl
  ; X = b ->
    write( Argument b received. ), nl
  ;
    write( Received unknown argument. ), nl
  ).

Then it will write different things depending on it argument:

?- implication(a).
Argument a received.
true.

?- implication(b).
Argument b received.
true.

?- implication(c).
Received unknown argument.
true.

(link to documentation.)

问题回答

It s a local version of the cut, see for example the section on control predicated in the SWI manual.

It is mostly used to implement if-then-else by (condition -> true-branch ; false-branch). Once the condition succeeds there is no backtracking from the true branch back into the condition or into the false branch, but backtracking out of the if-then-else is still possible:

?- member(X,[1,2,3]), (X=1 -> Y=a ; X=2 -> Y=b ; Y=c).
X = 1,
Y = a ;
X = 2,
Y = b ;
X = 3,
Y = c.

?- member(X,[1,2,3]), (X=1, !, Y=a ; X=2 -> Y=b ; Y=c).
X = 1,
Y = a.

Therefore it is called a local cut.

It is possible to avoid using it by writing something more wordy. If I rewrite Stephan s predicate:

implication(X) :-
  (
    X = a,
    write( Argument a received. ), nl
  ; 
    X = b,
    write( Argument b received. ), nl
  ;
    X = a,
    X = b,
    write( Received unknown argument. ), nl
  ).

(Yeah I don t think there is any problem with using it, but my boss was paranoid about it for some reason, so we always used the above approach.)

With either version, you need to be careful that you are covering all cases you intend to cover, especially if you have many branches.

ETA: I am not sure if this is completely equivalent to Stephan s, because of backtracking if you have implication(X). But I don t have a Prolog interpreter right now to check.





相关问题
Declaring function objects for comparison?

I have seen other people questions but found none that applied to what I m trying to achieve here. I m trying to sort Entities via my EntityManager class using std::sort and a std::vector<Entity *&...

Equality Test for Derived Classes in C++ [duplicate]

Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In C++, how can derived classes override the base class equality test in a meaningful way? For ...

Is there an Non-Short circuited logical "and" in C++?

tl;dr: Is there a non-short circuit logical AND in C++ (similar to &&)? I ve got 2 functions that I want to call, and use the return values to figure out the return value of a 3rd composite ...

Javascript String Assignment Operators

How come I can use += on a string, but I cannot use -= on it? For example... var test = "Test"; var arr = "&#8660;" test += arr; alert(test); // Shows "Test&#8660;" test -= arr; alert(...

PHP Comparison Operators and Data Types

I m currently working through O Reilly s "Programming PHP" and have come across this table titled "Type of comparison performed by the comparison operators": First Operand | Second ...

热门标签