[ ECLiPSe Control built-in.|Group Index| Full Index]

+Goal1 -> +Goal2 ; +Goal3

If..Then..Else - succeeds if either Goal1 succeeds, and then Goal2 succeeds; or else if Goal1 fails, and then Goal3 succeeds.

+Goal1
Atom or compound term.
+Goal2
Atom or compound term.
+Goal3
Atom or compound term.

Description

Used to implement the If...Then...Else construct. First Goal1 is called and if this succeeds any further solutions of Goal1 are cut out and Goal2 is called. Goal3 is never executed in this case regardless of the outcome of Goal2.

If Goal1 fails, Goal3 is called. In this case, Goal2 is never executed.

Note that this predicate is really a call to ;/2, but since the first argument contains an implicit cut in the first goal, ;/2 behaves like If...Then...Else and only executes one of Goal2 and Goal3.

Also note that:

Goal1 must not contain a !/0. If a !/0 appears in Goal2 or Goal3, it cuts through ->/3.

Since ->/3 has a lower precedence than ,/2, a call to ->/3 should always be enclosed in parentheses.

Fail Conditions

Fails if Goal1 succeeds and Goal2 fails, or if Goal1 and Goal3 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, ; / 2, ! / 0