Friday, November 13, 2009

Operators and Arithmetics in Prolog

Operators and Arithmetics
Operators
Up to now, Prolog user usually use the notation for predicates by a number of arguments in parentheses.
Ex : likes(john,mary)
There is another alternative :
- Two arguments (a binary predicates) be converted to an infix operator
the functor be written between two arguments with no parentheses
Ex : john likes mary
- One argument (a unary predicate) be converted to :
1. Prefix operator  the functor be written before the argument with no parentheses
Ex : isa_dog fred
2. Postfix operator  the functor be written after the argument
Ex : fred isa_dog
Both of predicate (one or two arguments) can be converted to an operator by entering a goal using the op predicate at the system prompt. Ex :
?-op(150,xfy,likes).
This predicate takes three arguments :
1. 150 (operator precedence) : an integer from 0 upwards
So we can change it with another integer.
2. Xfy : the predicate is binary and is to be converted to an infix operator.
This argument should normally be one of the following three atoms:
1. Xfy
2. Fy : the predicate is unary and is to be converted to an prefix operator
3. Xf : the predicate is unary and is to be converted to a postfix operator
3. Likes : the name of the predicate that is to be converted to an operator.
Arithmetics
Prolog user can doing arithmetic calculate with prolog, such as :
1. Arithmetic operator
X+Y : sum of X and Y
X-Y : dfference of X and Y
X*Y : product of X and Y
X/Y : quotient of X and Y
X//Y : the ‘integer quotient’ of X and Y (the result is truncated to the nearest integerbetween it and zero)
X^Y : X to the power of Y
-X : negative of X
abs(X) : absolute value of X
sin(X) : sine of X
cos(X) : cosine of X
max(X,Y) : yhe larger of X and Y
sqrt(X) : square root of X
2. Operator presedence in arithmetic expression
Prolog use ordinary algebra algorithm in arithmetic operation.
Ex : A+B*C-D
In the algebra, C*B are calculate first, then the result+A, then the result of sum-D. it is same with those in prolog. But, if we want to calculate A+B, C-D, then multiply both of the result, we must add the parentheses.
Ex : (A+B)*(C-D)
3. Relasion operator
The operator like =,!=, >, >=, <, <= can be used in prolog

Degree operator
Under is the rist of equality operators that used in prolog with the function of each operator:
• Arithmetic Expression Equality ( =:= )
• Arithmetic Expression Inequality ( =\= )
• Terms Identical ( == )
• Terms Not Identical ( \== )
• Terms Identical With Unification ( = )
• Non-Unification Between Two Terms( \= )

Logic operator
a. Operator NOT
Operator not can be placed before predicate to give the negation. Predicate that be negation has the truth value if the origin predicate is false and has the false value if the origin predicate is truth.
The example of using operator not :
dog(fido).
?- not dog(fido).
no
?- dog(fred).
no
?- not dog(fred).
Yes
b. Disjunction operator
Disjunction operator is used as operator ‘atau’.
Ex :
?- 6<3;7 is 5+2.
yes
?- 6*6=:=36;10=8+3.
yes

No comments:

Post a Comment