Tampilkan postingan dengan label Tutorial. Tampilkan semua postingan
Tampilkan postingan dengan label Tutorial. Tampilkan semua postingan

Senin, 07 Desember 2009

LOOPS

Most conventional programming languages have a looping facility that enables a set of instructions to be executed repeatedly either a fixed number of times or until a given condition is met. In Prolog, this can be done in a variety of ways, using backtracking, recursion, built-in predicates, or a combination of these.

1. Looping a Fixed Number of Times
No such facility is available in Prolog (directly), but a similar effect can be obtained using recursion,

2. Looping Until a Condition Is Satisfied
Again, no such facility is available directly in Prolog, but a similar effect can be obtained in several ways.
1. Recursion
The function of recursion is to read terms entered by the user from the keyboard and output them to the screen, until end is encountered. This recursive program repeatedly prompts the user to enter a term until either yes or no is entered.
2. Using the 'repeat' Predicate
Recursion is not always the easiest way to provide the types of looping required in Prolog programs. Another method that is often used is based on the built-in predicate repeat.
The goal repeat does not repeat anything; it merely succeeds whenever it is called. The great value of repeat is that it also succeeds (as many times as necessary) on backtracking. The effect of this, as for any other goal succeeding, is to change the order of evaluating goals from 'right to left' (i.e. backtracking) back to 'left-to-right'. This can be used to create a looping effect.
This program repeatedly prompts the user to enter a term until either yes or no is entered. It is an alternative to the recursive program shown at the end of the previous section. In this case it is debatable whether using repeat is an improvement on using recursion.
Goals to the left of repeat in the body of a clause will never be reached on backtracking. The next program reads a sequence of terms from a specified file and outputs them to the current output stream until the term end is encountered. This program shows how to implement a menu structure which loops back repeatedly to request more input. Entering go at the prompt causes Prolog to output a menu from which the user can choose activities one at a time until option d is chosen. Note that all inputs are terms and so must be followed by a full stop character.
3. Backtracking with Failure
As the name implies, the predicate fail always fails, whether on 'standard' evaluation left-to-right or on backtracking. Advantage can be taken of this, combined with Prolog's automatic backtracking, to search through the database to find all the clauses with a specified property.
1. Searching the Prolog Database
2. Finding Multiple Solutions


This chapter describes how a set of goals can be evaluated repeatedly in Prolog, either a fixed number of times or until a specified condition is met, and how multiple solutions can be arrived at using the technique of 'backtracking with failure'.

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.

The Answer of Exercise 4 about OPERATOR and ARITHMETIC

OPERATORS AND ARITHMATIC
1. Program yang harus dibuat dengan menggunakan bentuk operator untuk menghasilkan hasil yang sama seperti Animal Program pada chapter 2 dapat dibuat dengan beberapa rumus pada notepad, cara yaitu sebagai berikut:

a. - Karena argumen yang digunakan lebih dari satu argumen maka agar dapat dirubah atau dikonvert menjadi sebuah operator maka dengan memasukkan perdikat op pada system prompt. Predikat ini digunakan untuk tiga argumen. ?-op(150, fy, isa_dog).
- argument pertama menjelaskan Operator Precedence dimana merupakan sebuah integer dengan nilai dari 0 sampai ke atas.
- argumen kedua menggunakan fy dimana untuk mengkonversikan predikat unary menjadi operator prefix.
-argumen ketiga yang digunakan disesuaikan dengan argumen kedua karena argumen kedua mengkonversikan predikat unary menjadi operator prefix , maka bentuk argumen ketika adalah sebagai berikut: isa_dog.
Sehingga yang program bisa dibuat pada notepad adalah sebagai berikut:


b. Selanjutnya mendeklarasikan data sesuai pada animal program pada chapter 2 dengan cara merubah bentuk penulisan menjadi seperti berikut:

NB: digunakan bentuk isa_dog(fido) agar bisa mengkonversikan predikat unary menjadi operator prefix sesuai dengan argumen yang telah dideklarasikan sebelumnya.

c. Selanjutnya operator notation yang digunakan adalah sebagai berikut:

Jadi program yang harus dibuat pada notepad agar "animal program" bisa dieksekusi pada SWI prolog adalah sebagai berikut:


Tampilan program pada SWI Prolog adalah seperti gambar di bawah ini:


2. Program yang digunakan untuk menghitung rata-rata, akar dan mengetahui nilai maksimum dari 2 angka dapat langsung di buat pada SWI Prolog tanpa harus membuat program terlebih dahulu pada notepad.

a. Tampilan Program pada SWI Prolog untuk menghitung rata-rata dari 2 angka:


b. Tampilan Program pada SWI Prolog untuk menghitung akar dari perkalian 2 angka :

NB: Ada 2 cara berbeda yang dapat digunakan untuk menemukan akar dari hasil perkalian 2 angka yaitu pada no.1 dan no.2

c. Tampilan Program pada SWI Prolog untuk mengetahui nilai maksimum dari 2 angka :