Kamis, 12 November 2009

SUMMARY Operators and Arithmetic

Operators and Arithmetic

1. Operator
Any user-defined predicate with two arguments (a binary predicate) can be converted to an infix operator.
Any user-defined predicate with one argument (a unary predicate) can be converted to a prefix operator. This enables the functor to be written before the argument with no parentheses.
a unary predicate can be converted to a postfix operator. This enables the functor to be written after the argument

Any user-defined predicate with one or two arguments can be converted to an operator by entering a goal using the op predicate at the system prompt. This predicate takes three arguments, for example


?-op(150,xfy,likes).


1. The first argument (150) is the 'operator precedence'. Operator precedence values are
used to determine the order in which operators will be applied when more than one
is used in a term. In most other cases it will suffice to use an arbitrary value such as 150.

2. The second argument should normally be one of the following three atoms:

xfy meaning that the predicate is binary and is to be converted to an infix operator
fy meaning that the predicate is unary and is to be converted to an prefix operator
xf meaning that the predicate is unary and is to be converted to a postfix operator

3. The third argument specifies the name of the predicate that is to be converted to

an operator.


2. Arithmetic
Prolog provides facilities for doing arithmetic using a notation similar to that which will already be familiar to many users from basic algebra.
There are some of the arithmetic operators and arithmetic functions available in Prolog:
1.X+Y the sum of X and Y
2.X-Y the difference of X and Y
3.X*Y the product of X and Y
4.X/Y the quotient of X and Y
5.X//Y the 'integer quotient' of X and Y (the result is truncated to then earest integer betwe it and zero)
6.X^Y X to the power of Y
7.-X the negative of X
8.abs(X) the absolute value of X
9.sin(X) the sine of X (for X measured in degrees)
10.cos(X) the cosine of X (for X measured in degrees)
11.max(X,Y) the larger of X and Y
12.sqrt(X) the square root of X

is predicate is normally used in the way described here, the first argument can also be a number or a bound variable with a numerical value. In this case, the numerical values of the two arguments are calculated. The goal succeeds if these are equal. If not, it fails.

1. first argument is an unbound variable, it is bound to the value of the second argument (as a side effect) .
2. first argument is a number, or a bound variable with a numerical value.
3. first argument is an atom, a compound term, a list, or a variable bound to one of these (none of which should happen), the outcome is implementation-dependent.
It is likely that an error will occur.


Operator Precedence in Arithmetic Expressions
Operators with relatively high precedence such as * and / are applied before those with lower precedence such as + and -. The effect is to give an expression such as A+B*C-D the meaning that a user who is familiar with algebra would expect it to have, i.e. A+(B*C)-D or (A+B)*(C-D).

Relational Operators
The infix operators =:= =\= > >= < =<>

3. Equality Operators
There are three types of relational operator for testing equality and inequality available in Prolog. The first type is used to compare the values of arithmetic expressions. The other two types are used to compare terms.

a. Arithmetic Expression Equality =:=
E1=:=E2 succeeds if the arithmetic expressions E1 and E2 evaluate to the same value.
b. Arithmetic Expression Inequality =\=
E1=\=E2 succeeds if the arithmetic expressions E1 and E2 do not evaluate to the same value.
c. Terms Identical ==
Both arguments of the infix operator == must be terms. The goal Term1==Term2 succeeds if and only if Term1 is identical to Term2. Any variables used in the terms may or may not already be bound, but no variables are bound as a result of evaluating the goal.
d. Terms Not Identical \==

Term1\==Term2 tests whether Term1 is not identical to Term2. The goal succeeds if Term1==Term2 fails. Otherwise it fails.
e. Terms Identical With Unification =

The term equality operator = is similar to == with one vital (and often very useful) difference. The goal Term1=Term2 succeeds if terms Term1 and Term2 unify, i.e.
there is some way of binding variables to values which would make the terms identical. If the goal succeeds, such binding actually takes place.
f. Non-Unification Between Two Terms \=

The goal Term1\=Term2 succeeds if Term1=Term2 fails, i.e. the two terms cannot be unified. Otherwise it fails.

4. Logical Operators
This section gives a brief description of two operators that take arguments that are call terms, i.e. terms that can be regarded as goals.
a. The not Operator
The prefix operator not/1 can be placed before any goal to give its negation. The negated goal succeeds if the original goal fails and fails if the original goal succeeds. The following examples illustrate the use of not/1. It is assumed that the database contains the single clause

b. The Disjunction Operator
The disjunction operator ;/2 (written as a semicolon character) is used to represent 'or'. It is an infix operator that takes two arguments, both of which are goals. Goal1;Goal2 succeeds if either Goal1 or Goal2 succeeds.

Tidak ada komentar:

Posting Komentar