Hello again,
this time we're gonna learn about Operators and Arithmetic in Prolog.
But first we have to know what the meaning of Operators and Arithmetic.
Operators
The standard one of a functor followed by a number of arguments in parentheses, e.g. likes(john,mary).
As an alternative, any user-defined predicate with two arguments (a binary predicate) can be converted to an infix operator. This enables the functor (predicate name) to be written beetwen the two arguments with no parentheses e.g. john likes mary.
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 e.g.
isa_dog fred
instead of
isa_dog(fred)
a unary predicates can be converted to a postfix operator. This is enables the functor to be written after the argument e.g.
fred isa_dog
Operators notation can be used with rules to aid readability.
Such as :
likes(john,X):-is_female(X),owns(X,Y),isa_cat.(Y)
is a valid form from previous rule but 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 can still be used with operators if preferred. likes, is_female, owns, ang isa_cat are operators.
Bracketed notation may also built-in predicates that are defined as operators.
Arithmetic
Prolog also provides facilities for doing arithmetic using a notation simiiar to that which will already be familiar to many users from basic algebra.
Any variables appearing in an arithmatic expressions must already be bound and their values must be numerical.
Symbols such as +-*/ in arithmetic expressions are a special type of infix operators known as arithmetic operators. Unlike operators used elsewhere in Prolog they are not predicates but functions, which return a numerical value.
Operator Precedence in Arithmetic Expressions
Whwn there is more than one operator in an arithmetic expressions, example A*B+C-D, prolog needs a means of deciding the order in which the operators will be applied.
Prolog achives this by giving each operators a numerical precedence value.
Operators with relatively high precedence such as * and / are applied before those with lower precedence such as + and -. Operators with same precedence are applied from left to right.
If a different order of evaluation is required this can be archieved by use of brackets. Bracketed expressions are always evaluated first.
Relational Operators
The infix operators =:= =\= > >= < <= are a special type known as relational operators. They are used to compare the value of two arithmetic expressions. Both arguments must be numbers, bound variables or arithmetic expressions.
Equality Operators
There are 3 types of relational operators for testing equality and inequality available in Prolog. The first type is to compare the values of arithmetic expressions, the othr two are to compare terms.
Arithmetic Expression Equality =:=
E1=:=E2 succeeds if the arithmetic expression E1 and E2 to the same value.
Arithmetic Expression Inequality =\=
E1=\=E2 succeeds if the arithmetic expression E1 and E2 do not evaluate to the same value.
Term Identical
Boh arguments of the infix operator == must be terms.
E1==E2 succeeds if and only if E1 is identical to E2.
Terms Not Identical \==
E1\==E2 tests whether E1 is not identical to E2.
E1\==E2 succeeds if E1==E2 fails.
Terms Identical With Unifications =
The term equality operator = is similar to == with one vital (and often very usful) difference.
E1=E2 succeeds if terms E1 and E2 unify, i.e. there is some way of binding variables to values which would make the terms identical.
Non-Unification Between Two Terms \=
E1\=E2 succeeds if E1=E2 fails.
Logical Operators
The Not Operator
The prefix operator not can be placed before any goal to give its negation.
The negated goal succeeds if the oriinal goal fails and fails if the original goal succeeds.
The Disjunction Operator
The disjunction operator ; is used to represent 'or'.
It is an infix operator that takes two arguments, both of which are goals.
E1;E2 succeeds if either E1 or E2 succeeds.
Okay,
that's all the summary Chapter 4 of Logic programming With Prolog.
Next we'll try to figure out the Pratical Exercise 4 Page 68 with the images of captured prolog work.
Be there or behind (~_^)
Regards,
Mathlovers.
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar