Which operator & # 8594; in Prolog and how to use it?

I read about it in the book, but it was not explained at all. I also never saw him in the program. Is part of the Prolog syntax? What is this for? Do you use it?

thanks

+7
operators prolog
source share
3 answers

It is an implication. The right side is satisfied only if the left side is true. So, 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 he will write different things depending on the argument:

 ?- implication(a). Argument a received. true. ?- implication(b). Argument b received. true. ?- implication(c). Received unknown argument. true. 

( link to documentation .)

+7
source share

This is a local section view; see, for example, the management section in the SWI manual.

It is mainly used to implement if-then-else through (condition β†’ true-branch; false-branch). Once the condition is successful, there is no return from the true branch back to the condition or to the false branch, but return from if-then-else is still possible. Therefore, it is called a local incision or soft incision.

+2
source share

You can avoid using this by writing something more verbose. If I rewrite Stefan'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 ). 

(Yes, I do not think there are any problems with its use, but for some reason my boss was paranoid, so we always used the above approach.)

In any version, you must be careful to cover all the cases that you intend to cover, especially if you have many branches.

ETA: I'm not sure if this is completely equivalent to Stephan's, due to indentation if you have implication(X) . But I do not have a Prolog interpreter to check.

0
source share

All Articles