4.7 Control Predicates
All Application Manual Name SummaryHelp

  • 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[ISO]:Condition -> :Action
If-then and If-Then-Else. The ->/2 construct commits to the choices made at its left-hand side, destroying choice points created inside the clause (by ;/2), or by goals called by this clause. Unlike !/0, the choice point of the predicate as a whole (due to multiple clauses) is not destroyed. Disregarding the interaction with !/0, the combination ;/2 and ->/2 acts as if defined as:
If -> Then; _Else :- If, !, Then.
If -> _Then; Else :- !, Else.
If -> Then :- If, !, Then.

Please note that (If -> Then) acts as (If -> Then ; fail), making the construct fail if the condition fails. This unusual semantics is part of the ISO and all de-facto Prolog standards.

Please note that (if->then;else) is read as ((if->then);else) and that the combined semantics of this syntactic construct as defined above is different from the simple nesting of the two individual constructs, i.e., the semantics of ->/2 changes when embedded in ;/2. See also once/1.

As with ;/2, this construct is always nested in parenthesis. Here is an example of the preferred layout for SWI-Prolog.

p :-
    a,
    (   b,
        c
    ->  d,
        e
    ;   f
    ->  g
    ;   h
    ).