4.7 Control Predicates
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • Control Predicates
          • fail/0
          • false/0
          • true/0
          • repeat/0
          • !/0
          • ,/2
          • ;/2
          • |/2
          • ->/2
          • *->/2
          • \+/1
    • Packages
Availability:built-in
Source:Condition *-> :Action ; :Else
This construct implements the so-called‘soft-cut'. The control is defined as follows: If Condition succeeds at least once, the semantics is the same as (call(Condition), Action).72Note that the Condition is wrapped in call/1, limiting the scope of the cut (!/0 If Condition does not succeed, the semantics is that of (\+ Condition, Else). In other words, if Condition succeeds at least once, simply behave as the conjunction of call(Condition) and Action, otherwise execute Else. The construct is known under the name if/3 in some other Prolog implementations.

The construct A *-> B, i.e., without an Else branch, the semantics is the same as (call(A), B).

This construct is rarely used. An example use case is the implementation of optional in sparql. The optional construct should preserve all solutions if the argument succeeds as least once but still succeed otherwise. This is implemented as below.

optional(Goal) :-
    (   Goal
    *-> true
    ;   true
    ).

Now calling e.g., optional(member(X, [a,b])) has the solutions X=a and X=b, while optional(member(X,[])) succeeds without binding X.