[ Control | The ECLiPSe Built-In Predicates | Reference Manual | Alphabetic Index ]

+Condition -> +Then ; +Else

Conditional construct - succeeds if either Condition succeeds, and then Goal2 succeeds; or else if Condition fails, and then Else succeeds.
+Condition
Atom or compound term.
+Then
Atom or compound term.
+Else
Atom or compound term.

Description

The conditional (if-then-else) construct. First Condition is called and if this succeeds any further solutions of Condition are cut and Then is called. Else is never executed in this case regardless of the outcome of Then.

If Condition fails, Else is called. In this case, Then is never executed.

It is allowed, but not recommended to use ->/2 without ;/2 as

   ( Condition -> Then )
   
If Condition succeeds, any further solutions of Condition are cut and Then is called (as above). If Condition fails, the whole construct fails, which is often considered unintuitive. If this behaviour is really wanted, it can be expressed more clearly by
   once(Condition), Then
   

The more common idiom, where nothing is to be done in the else-case, must be written like this, using true/0:

   ( Condition -> Then ; true )
   

Also note that Condition must not contain a !/0. If a !/0 appears in Then or Else, it cuts through the whole construct.

Since ->/2 and ;/2 have a lower precedence than ,/2, call should always be enclosed in parentheses:

    ( Condition ->
        Then
    ;
        Else
    )
    

Fail Conditions

Fails if Condition succeeds and Then fails, or if Condition and Else fail.

Resatisfiable

No.

Exceptions

(4) instantiation fault
One of the arguments is not instantiated.
(5) type error
One of the arguments is neither an atom nor a compound term.

Examples

Success:
      [eclipse]: X = 1, (X == 1 -> write(a); write(b)).
      a
      X = 1
      yes.

      [eclipse]: fail->write(not_me); write(me).
      me
      yes.
      [eclipse]: [user].
       p(1). p(2).
       q(1). q(3).
       r(2). r(3).
       user        compiled 408 bytes in 0.00 seconds
      yes.
      [eclipse]: p(X)->q(Y);r(Y).
      X = 1
      Y = 1     More? (;)   % p/1 is cut; q/1 isn't.

      X = 1
      Y = 3
      yes.
      [eclipse]: p(3)->q(2);r(2).
      yes.


Fail:
      [eclipse]: X = 1, (X == 2 -> write(da); write(nyet)).
      nyet
      X = _g76      % X is not bound.
      no.

Error:
      Goal -> write(a); fail.        (Error 4).
      "write(a)" -> true; fail.      (Error 5).



See Also

; / 2, ! / 0