Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts

Wednesday, December 16, 2009

Tuesday, December 8, 2009

Chapter 6 from E-Book : Looping with PROLOG

6.1 LOOPING A FIXED NUMBER OF TIMES

Loops enable a set of instructions to be executed a fixed number of times. In prolog, looping can be obtained using recursion. In this case, looping function on PROLOG is similiar to another programming language. We can see in the example given below:

loop(0).
loop(N):-N>0,write('The value is: '),write(N),nl,
M is N-1,loop(M).

Those clauses means that ‘the terminating condition of the recurtion is 0. To loop from N, first determine the value of N, then substract it to having M, then loop from M, until the value is 0. If the value is 0, the looping process is stopped there’

6.2 LOOPING UNTIL A CONDITION IS SATISFIED
No facility on PROLOG that directly enable a set of instructions to be executed repeatedly until a given condition is met. But there are two way to obtained the similiar effect :

- Recursion
Recursion read the input from keyboard, and output it to the screen, until the ‘end’ instruction is endountered.
Example of the form :

go:-loop(start). /* start is a dummy value used to get
the looping process started.*/
loop(end).
loop(X):-X\=end,write('Type end to end'),read(Word),
write('Input was '),write(Word),nl,loop(Word).

The last clauses mean: The looping of X will be stopped if the ‘end’ is encountered. Saat looping berlangsung (before ‘end’ had encountered), PROLOG will always ask the user to enter the input by the sentence ‘Type end to end’ .

- Using the ‘repeat’ Predicate
The goal repeat doesn’t repeat anything, it will be succeeds whenever it’s being called. If user enter another term but yes or no, PROLOG will always repeat the instruction until the user enter the term ‘yes’ or ‘no’. This is the example of the form :

get_answer(Ans):-
write('Enter answer to question'),nl,
repeat,write('answer yes or no'),read(Ans),
valid(Ans),write('Answer is '),write(Ans),nl.
valid(yes). valid(no).

In the case of looping, the two goals write(‘answer yes or no’) and read(Ans) will always repeat until the condition valid(Ans) had satisfied.

Repeat predicate can also processing the sequence of terms from a specified file an outputs them until the term ‘end’ is encountered.
The example of the form :

readterms(Infile):-
seeing(S),see(Infile),
repeat,read(X),write(X),nl,X=end,
seen,see(user).

Then, the file that will being outputted containing (for example, the file ‘myfile.txt’ :

'first term'. 'second term'.
'third term'. 'fourth term'.
'fifth term'. 'sixth term'.
'seventh term'.
'eighth term'.
end.

Then, call the goal readterms will produced a result :

?- readterms('myfile.txt').
first term
second term
third term
fourth term
fifth term
sixth term
seventh term
eighth term
end
yes

If ‘end’ hasn’t been encountered, there will be a looping process between the goals repeat and X=end.

6.3 Backtracking with Failure

ex:
Supposing the database contains clauses such as
dog(fido).
dog(fred).
dog(jonathan).

Each dog clause can be processed in turn using the alldogs predicate defined
below.
alldogs:-dog(X),write(X),write(' is a dog'),nl,fail.
alldogs.

The effect is to loop through the database finding all possible values of X that
satisfy the goal dog(X).
?- alldogs.
fido is a dog
fred is a dog
jonathan is a dog
yes


Note the importance of the second clause of the alldogs predicate. It is there to
ensure that, after the database has been searched, the goal succeeds. With only the
first line, any call to alldogs will eventually fail.

The next program is designed to search a database containing clauses
representing the name, age, place of residence and occupation of a number of
people.It should be noted that it is not always necessary to use 'backtracking with
failure' to search the database.


Finding Multiple Solutions

Suppose that a predicate findroute(Town1,Town2,Route) finds a route Route
between two towns Town1 and Town2.

as follows:
find_all_routes(Town1,Town2):-
findroute(Town1,Town2,Route),
write('Possible route: '),write(Route),nl,fail.
find_all_routes(_,_).


So, Backtracking with failure can also be used to find all the ways of satisfying a goal.
And also 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'.



Thursday, November 26, 2009

excercise prolog chapter 5 (cont'd)

4. Create the text that you want to combine on notepad, for example: the file ‘ntar.txt’ and ‘ntar2.txt’ that we can see below:



in order that the output file contain the terms in the first input file followed by the terms in the second, you must write some rules using predicate combine in notepad like the picture below:




After you write the program, you can consult it on SWI PROLOG then you can see how’s the output, like we can see below:




5. in order that we can reads that two text files term by term and each pair of corresponding terms output a messsage saying that they are the same or different, we must open two files that contain the same starting term and finished by end. Like we can see below this :


after that, we need to make a formula on notepad using predicare ‘compare’. Then, consult it on SWI PROLOG.


this is it, the output:

exercise prolog chapter 5




1.Changing from Upper to Lower

- Make a predicate make lower in notepad and writing formulas to be written uppercase to lowercase. Example:

=X<=90,huruf besar ada dalam ambang 65-90(menurut ASCII).">Numbers 65> = X <= 90, upper threshold of 65-90 in (according to ASCII). Figures 32 to print a space.
- Saved in. Pl
- Opening prologue and consult

- =X<=90,huruf besar ada dalam ambang 65-90(menurut ASCII).">Numbers 65> = X <= 90, upper threshold of 65-90 in (according to ASCII). Figures 32 to print a space.
- Saved in. Pl
- Opening prologue and ending consultketik makelower dot (.)
- Write the words you want capitalized, for example: Denia, RIFAnda, QORY, and Deby and ..
- The results:

2. Question


The formula used...

Display prologue

Output



3. Creating readfile predicate in the notepad, as follows:

- Saved in. Pl
- Opening prologue and consult
- Type readfile ending point (.)
- Write ABCDE fghij
- The results:


Friday, November 13, 2009

Summary

Operators

• Sampai sekarang notasi digunakan sebagai predikat dan standart nama predikat diikuti oleh beberapa argumen dalam tanda kurung. Contoh : likes(john,mary).
• Sebagai alternatif, banyak pengguna yang mendefiisikan predikat dengan dua argumen (binary predicate) yang dapat dikonversikan ke dalam infix operator. Nama predikat ditulis diantara dua argumen tanpa tanda kurung. Contoh : john likes mary
• Beberapa pengguna mendsefinisikan predikat dengan sebuah argumen yang dapat dikonversikan kedalam prefix operator. Nama predikat ditulis sebelum argumen tanpa tanda kurung.
Contoh : isa_dog fred
Sebagai pengganti
isa_dog (fred)
• Sebagai alternatif , unary predicate dapat dikonversikan kedalam postfix operator. Nama predikat ditulis setelah argumen. Contoh : fred isa_dog
• Notasi operator dapat digunakan dengan peraturan untuk membantu memudahkan membaca. Beberapa peraturannya adalah :
o likes(john,X):-is_female(X),owns(X,Y),isa_cat(Y).
Lebih mudah dibaca jika ditulis :
john likes X:- X is_female, X owns Y, Y isa_cat.
• Standart notasi nama predikat dan argumen dalam kurung, seprti likes(john,X) dapat digunakan dengan operator jika lebih disukai.
• Perpaduan notasi juga diizinkan
Contoh : jika likes/2, is_female/1, owns/2 and isa_cat/1 semuanya adalah operator
Maka likes(john,X):-is_female(X),X owns Y,isa_cat(Y)adalah bentuk yang valid dari peraturan yang sebelumnya.
• Beberapa pengguna mendefinisiakan predikat dengan satu atau dua argumen yang dapat dikonversikan kedalam operator dengan menggunakan predikat op pada sistem prompt. Predikat ini memerlukan tiga argumen.
Contoh : ?-op(150,xfy,likes).
o Argumen pertama adalah ‘operator precedence’, yaitu integer mulai 0 keatas.
 Jarak angka yang digunakan tergantung implementasi khusus.Lebih rendah dari angka, lebih tinggi dari precedece.
 Operator precedence value digunakan untuk menentukan operator yang akan digunakan ketika lebih dari satu digunakan dalam sebuah istilah.
 Yang paling penting digunakan adalah operator untuk aritmatika.
o Argumen kedua merupakan argumen yang biasa digunakan dari ketiga argumen, yaitu:
 xfy => predikat binary dan dikonversikan ke infix operator
 fy => predikat unary dan dikonversikan ke prefix operator
 xf => predikat unary dan dikonversiakan ke postfix operator
o Argumen ketiga merupakan spesifikasi nama dari predikat yang dikonversikan kedalam operator.

• Predikat dapat juga dikonversikan kedalam operator dengan mengubah seperti:
o ?-op(150,xfy,likes).
• Dalam program prolog file akan diproses menggunakan consult atau reconsult. Prompt (karakter ?-) harus ada. Directive harus ditempatkan di file sebelum clausa pertama yaitu operator likes.
• Beberapa pembangun predikat telah didefinisikan sebagai operator, seperti : relational operator untuk membandingkan data numeric, yaitu <‘kurang dari’ dan > ‘lebih dari’.
Contoh : X>4
Y(X,4)sebagai pengganti X>4

Arithmetic

• Penggunaan predikat is/2 yang didefinisikan sebagai infix operator dan ditulis diantara dua argumen. Penggunaan yang umum adalah argumen pertama merupakan bound variabel.
Contoh : X is -6.5
 X adalah bound angka -6.5
• Argumen kedua dapat berupa angka atau tanda aritmatik.
Contoh : X is 6*Y+Z-3.2+P-Q/4
 Eksekusi akan berhasil dan variabel yang membentuk argumen pertama akan menjadi bound dari hasil operasi aritmatika. Jika tidak akan terjadi error.
• ?- X is 10.5+4.7*2.
X = 19.9
?- Y is 10,Z is Y+1.
Y = 10 ,
Z = 11
o Simbol-simbol seperti + - * / dalam operasi aritmatika termasuk infix operator yang diketahui sebagai operator aritmatika. Namun mereka bukan operator tetapi fungsi yang menghasikan data numerik.
• Angka, variabel dan operator, operasi aritmatika dapat tergolong fungsi aritmatik, yang ditulis dengan argumen di dalam tanda kurung (tidak sebagai operator).
Contoh : untuk mencari akar dari 36 =>
?-X is sqrt(36).
X = 6
• Operator aritmatika tidak hanya dapat digunakan sebagai binary infix operator untuk mendeskripsikan selisih dari dua data numerik (X-6), tetapi juga sebagai unary prefix operator untuk mendeskirpsikan bentuk negatif dari data numerik.
Contoh : -X is 10, Y is –X-2.
X = 10, Y = -12
• Operator aritmatik dan fungsi aritmatik
X+Y the sum of X and Y
X-Y the difference of X and Y
X*Y the product of X and Y
X/Y the quotient of X and Y
X//Y the 'integer quotient' of X and Y (the result is truncated to the
nearest integer between it and zero)
X^Y X to the power of Y
-X the negative of X
abs(X) the absolute value of X
sin(X) the sine of X (for X measured in degrees)
cos(X) the cosine of X (for X measured in degrees)
max(X,Y) the larger of X and Y
sqrt(X) the square root of X
Contoh :

?- X is 30,Y is 5,Z is X+Y+X*Y+sin(X).
X = 30 ,
Y = 5 ,
Z = 185.5
o Meskipun predikat is biasa digunakan dalam pendeskripsian, argumen pertama juga dapat berupa angka atau variabel bound dengan nilai numerik. Sehingga nilai numerik dari dua argumen terhitung. Program akan berhasil jika sama dan gagal jika tidak sama.
Contoh : ?- X is 7,X is 6+1.
X = 7
?- 10 is 7+13-11+9.
no
?- 18 is 7+13-11+9.
yes

Operators and Arithmatic Part 2



Define and test a predicate which takes two arguments, both numbers, and calculates and outputs the following values :

a. their average

b. the square root of their product

c. the larger of a and b






Operators and Arithmatic

Hi Guys!!!
Sekarang kita akan mencoba untuk mengerti bagaimana mengoperasikan Operators & Arithmatic dalam prolog.

Oke, sekarang kita akan mengambil contoh kasus.
Berikut ini akan di tampilkan contoh penulisan data yang salah pada notepad. dari data yang salah tersebut, maka akan muncul hasil prolog yang salah pula atau error :
untuk itu kita perlu mengidentifikasikan kesalahan-kesalahan yang dibuat oleh persoalan tersebut. Dari data di atas, kita bisa mengidentifikasikan kesalahan-kesalahan itu dari :

1. Data yang ada masih acak, sedangkan prolog hanya membaca data yang urut dan dikelompokkan masing2.


2. Variable ada yang ambigu, sehingga yang keluar bukan yang diharapkan. Misalnya :

Salah :

-small_animal(A):-dog(A), small(A).

-small_animal(B):-cat(B), small(B).

Jika variabel seperti itu, maka yang akan di print adalah seluruh animal yang berukuran small.


Untuk itu, perlu ada pembeda diantara keduanya. Misalnya :

-small_dog(X):-dog(X),small(X).

-small_cat(Y):-cat(Y),small(Y).


3. Karena variabelnya di ganti, maka keterangan untuk kalimatpun harus di ganti.

Dari keterangan di atas, maka setelah kita perbaiki, maka hasilnya pada notepad akan menjadi seperti :










Itu adalah contoh Operator yang simpel.