Web : Discrete Addict: Summary of Chapter 4 (Operator and Aritmathic) from Logic Programming (with PROLOG) Ebook - URL : http://discreteaddict.blogspot.com/2009/11/summary-of-chapter-4-operator-and_17.html
www.kang-musa.co.cc
.

Summary of Chapter 4 (Operator and Aritmathic) from Logic Programming (with PROLOG) Ebook

Summary of Chapter 4 (OPERATORS)

A binary predicate (any user-defined with two arguments) can be converted to an infix operator. This enables the functor (predicate name) to be written between the two arguments with no parentheses, e.g.

likes (john, mary) into john likes mary

And a unary predicate (any user-defined predicate with one argument) can be converted to a prefix operator. This enables the functor (predicate name) to be written before the argument with no parentheses, e.g.

isa_dog (fred) into isa_dog fred

Alternatively, a unary predicate can be converted to a postfix operator. This enables the functor to be written after the argument, e.g.

isa_dog (fred) into fred isa_dog

Operator notation can also be used with rules to aid readability. Some people may find a rule such as

likes(john,X):-is_female(X),owns(X,Y),isa_cat(Y).


easier to understand if it is written as

john likes X:- X is_female, X owns Y, Y isa_cat.


The standard bracketed 'functor and arguments' notation, e.g. likes(john,X) can still be used with operators if preferred. 'Mixed' notation is also permitted, e.g. if likes/2, is_female/1, owns/2 and isa_cat/1 are all operators

likes(john,X):-is_female(X),X owns Y,isa_cat(Y).


Arithmetic


Prolog provides facilities for doing arithmetic using a notation similar to that which will already be familiar to many users from basic algebra. This is achieved using the built-in predicate is/2, which is predefined as an infix operator and thus is written between its two arguments, e.g.

?- X is 10.5+4.7*2.
X = 19.9

?- Y is 10,Z is Y+1.
Y = 10 ,
Z = 11


Symbols such as + - * / in arithmetic expressions are a special type of infix operator known as arithmetic operators. Unlike operators used elsewhere in Prolog they are not predicates but functions, which return a numerical value. As well as numbers, variables and operators, arithmetic expressions can include arithmetic functions, written with their arguments in parentheses (i.e. not as
operators). Like arithmetic operators these return numerical values, e.g. to find the square root of 36:

?- X is sqrt(36).
X = 6


The arithmetic operator can be used not only as a binary infix operator to denote the difference of two numerical values, e.g. X-6, but also as a unary prefix operator to denote the negative of a numerical value, e.g.

?- X is 10,Y is -X-2.
X = 10 ,
Y = -12


The list below shows 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 the nearest integer between 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


Operator Precedence in Arithmetic Expressions
When there is more than one operator in an arithmetic expression, e.g. A+B*C-D, Prolog needs a means of deciding the order in which the operators will be applied. For the basic operators such as + - * and / it is highly desirable that this is the customary 'mathematical' order, i.e. the expression A+B*C-D should be interpreted as 'calculate the product of B and C, add it to A and then subtract D', not as 'add A and B, then multiply by C and subtract D'. Prolog achieves this by giving each operator a numerical precedence value. Operators with relatively high precedence such as * and / are applied before those with lower precedence such as + and -. Operators with the same precedence (e.g. + and -, * and /) are applied from left to right. 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.
If a different order of evaluation is required this can be achieved by the use of brackets, e.g. X is (A+B)*(C-D). Bracketed expressions are always evaluated first.

Relational Operators
The infix operators =:= =\= > >= < =< are a special type known as relational operators.

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.

Arithmetic Expression Equality =:=
E1=:=E2 succeeds if the arithmetic expressions E1 and E2 evaluate to the same value.

?- 6+4=:=6*3-8.
yes

?- sqrt(36)+4=:=5*11-45.
yes


To check whether an integer is odd or even we can use the checkeven/1 predicate defined below.
checkeven(N):-M is N//2,N=:=2*M.


?- checkeven(12).
yes

?- checkeven(23).
no

?- checkeven(-11).
no

?- checkeven(-30).
yes


The integer quotient operator // divides its first argument by its second and truncates the result to the nearest integer between it and zero. So 12//2 is 6, 23//2 is 11, -11//2 is -5 and -30//2 is -15. Dividing an integer by 2 using // and multiplying it by 2 again will give the original integer if it is even, but not otherwise.

Arithmetic Expression Inequality =\=
E1=\=E2 succeeds if the arithmetic expressions E1 and E2 do not evaluate to the same value

?- 10=\=8+3.
Yes


Terms Identical ==
Both arguments of the infix operator == must be terms. The goal Term1==Term2succeeds 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.

?- likes(X,prolog)==likes(X,prolog).
X = _

?- likes(X,prolog)==likes(Y,prolog).
no
(X and Y are different variables)

?- X is 10,pred1(X)==pred1(10).
X = 10

?- X==0.
no

?- 6+4==3+7.
no

The value of an arithmetic expression is only evaluated when used with the is/2 operator. Here 6+4 is simply a term with functor + and arguments 6 and 4. This is entirely different from the term 3+7.

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

?- pred1(X)\==pred1(Y).
X = _ ,
Y = _


(The output signifies that both X and Y are unbound and are different variables.)

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.

?- pred1(X)=pred1(10).
X = 10

(Variable X is bound to 10, which makes the two terms identical.)

?- likes(X,prolog)=likes(john,Y).
X = john ,
Y = prolog

(Binding X to the atom john and Y to the atom prolog makes the two terms
identical.)

?- X=0,X=:=0.
X = 0

(X=0 causes X to be bound to 0. The goal X=:=0 succeeds, which confirms that
X now has the value zero.)

?- 6+4=3+7.
no

(For the reason explained under ==.)

?- 6+X=6+3.
X = 3

(Binding X to 3 makes the two terms identical. They are both 6+3, not the
number 9.)

?- likes(X,prolog)=likes(Y,prolog).
X = Y = _

(Binding X and Y makes the terms identical.)

?- likes(X,prolog)=likes(Y,ada).
no

(No unification can make the atoms prolog and ada identical.)

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.

?- 6+4\=3+7.
Yes


Logical Operators


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 dog(fido).

?- not dog(fido).
no

?- dog(fred).
no

?- not dog(fred).
yes

?- X=0,X is 0.
X = 0

?- X=0,not X is 0.
no


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.

?- 6<3;7 is 5+2.
Yes

?- 6*6=:=36;10=8+3.
yes


You can download the e-book of logic programming here.

0 komentar:

Posting Komentar

 

Home - About Us - Profile - Tutorial - Literature - Relation