Comparisons

Visual Prolog can compare arithmetic expressions as well as characters, strings, and symbols. The following statement is the Visual Prolog equivalent of "The total of X and 4 is less than 9 minus Y."

    X + 4 < 9 - Y

The less than (<) relational operator indicates the relation between the two expressions, X + 4 and 9 - Y.

Visual Prolog uses infix notation, which means that operators are placed between the operands (like this: X+4) instead of preceding them (like this: +(X,4)).

The complete range of relational operators allowed in Visual Prolog is shown in Table 9.¿À·ù! Ã¥°¥ÇÇ°¡ Á¤ÀǵǾî ÀÖÁö ¾Ê½À´Ï´Ù..

Table 9.4: Relational Operators

Symbol

Relation

<

less than

<=

less than or equal to

=

equal

>

greater than

>=

greater than or equal to

<> or ><

not equal

1. Equality and the equal (=) Predicate

In Visual Prolog, statements like N = N1 - 2 indicate a relation between three objects (N, N1, and 2), or a relation between two objects (N and the value of N1 - 2). If N is still free, the statement can be satisfied by binding N to the value of the expression N1 - 2. This corresponds roughly to what other programming languages call an assignment statement. Note that N1 must always be bound to a value, since it is part of an expression to be evaluated.

When using the equal predicate (=) to compare real values, you must take care to ensure that the necessarily approximate representation of real numbers does not lead to unexpected results. For example, the goal

    7/3 * 3 = 7

will frequently fail (the exact outcome depends on the accuracy of the floating point calculations in use on your particular platform). Program 2 illustrates another example:

/* Program ch9e02.pro */

Except when running Prolog on the UNIX platform, where it behaves as one might expect, this prints:

    47<>47
    X-Y = 7.1054273576E-15

Therefore, when comparing two real values for equality you should always check that the two are within a certain range of one another.

(1) Example

Program 3 shows how to handle approximate equality; this is an iterative procedure for finding the square root in order to calculate the solutions to the quadratic equation:

    A*X*X + B*X + C = 0

The existence of solutions depends on the value of the discriminant defined as follows:

    D = B*B - 4*A*C.

D > 0 implies that there are two unique solutions.

D = 0 implies there is only one solution.

D < 0 implies that there are no solutions if X is to take a real value (there can be one or two complex solutions).

/* Program ch9e03.pro */

To solve the quadratic equation, this program calculates the square root of the discriminant, D. The program calculates square roots with an iterative formula where a better guess (NewGuess) for the square root of X can be obtained from the previous guess (Guess):

    NewGuess = Guess-(Guess*Guess-X)/2/Guess

Each iteration gets a little closer to the square root of X. Once the condition equal(X, Y) is satisfied, no further progress can be made, and the calculation stops. Once this calculation stops, the program can solve the quadratic using the values X1 and X2, where

    X1 = (-B + sqrtD)/(2*A)
    X2 = (-B - sqrtD)/(2*A)

(2) Exercises

Load Program 3 and try the following goals:

    solve(1, 2, 1).
    solve(1, 1, 4).
    solve(1, -3, 2).

The solutions should be

    x = -1
    No solution
    x1 = 2 and x2 = 1

respectively.

The object of this exercise is to experiment with the mysqrt predicate in Program 3. To ensure that temporary calculations are monitored, add the following as the first subgoal in the first mysqrt clause:

    write(Guess).

To see the effect of this amendment, try this goal:

    mysqrt(8, 1, Result).

Next, replace the equal clause with this fact:

    equal(X, X).

and retry the goal. Experiment a little more with the properties of equal. For instance, try

    equal(X, Y) :-
       X/Y < 1.1 , X/Y > 0.9.

Visual Prolog has a built-in square root function, sqrt. For example,

    X = sqrt(D)

will bind X to the square root of the value to which D is bound. Rewrite Program 3 using sqrt and compare the answers with those from the original version.

2. Comparing Characters, Strings, and Symbols

Besides numeric expressions, you can also compare single characters, strings and symbols. Consider the following comparisons:

    'a' < 'b'                                       /* Characters */
    "antony" > "antonia"
                       /* Strings */
    P1 = peter, P2 = sally, P1 > P2
         /* Symbols */

(1) Characters

Visual Prolog converts the 'a' < 'b' to the equivalent arithmetic expression 97 < 98, using the corresponding ASCII code value for each character. You should be aware that only 7 bit ASCII comparisons should be relied upon (i.e. upper and lower case letters a-z, digits, etc.). 8 bit characters, used for a number of national characters, are not necessarily portable between the different platforms.

(2) Strings

When two strings or symbols are compared, the outcome depends on a character-by-character comparison of the corresponding positions. The result is the same as you'd get from comparing the initial characters, unless those two characters are the same. If they are, Visual Prolog compares the next corresponding pair of characters and returns that result, unless those characters are also equal, in which case it examines a third pair, and so on. Comparison stops when two differing characters are found or the end of one of the strings is reached. If the end is reached without finding a differing pair of characters, the shorter string is considered smaller.

The comparison "antony" > "antonia" evaluates to true, since the two symbols first differ at the position where one contains the letter y (ASCII value 79) and the other the letter i (ASCII value 69). In the same vein, the character comparison "aa" > "a" is true.

Similarly, the expression "peter" > "sally" would be false--as determined by comparing the ASCII values for the characters that make up peter and sally, respectively. The character p comes before s in the alphabet, so p has the lower ASCII value. Because of this, the expression evaluates to false.

(3) Symbols

Symbols can't be compared directly because of syntax. In the preceding P1 = peter, P2 ... example, the symbol peter can't be compared directly to the symbol sally; they must be bound to variables to be compared, or written as strings