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:


Wednesday, November 25, 2009

Chapter 5 form E-book : Logic Programming Using PROLOG

5.1 Outputting terms


The write/1 predicate takes a single argument, which must be a valid Prolog

term. The built-in predicate nl/0 has also been used many times previously. It takes no arguments. Evaluating a nl goal causes a new line to be output to

the current output stream.

Example:

?- write(college),nl.

college

Yes

5.2 Inputting term

The built-in predicate read/1 is provided to input terms. It takes a single argument,

which must be a variable.

In the input stream, the term must be followed by a dot ('.') and at least one white space character, such as space or newline. The dot and white space characters are read in but are not considered part of the term.

If the argument variable is already bound the goal succeeds if and only if the

input term is identical to the previously bound value.

?- X=rooney,read(X).

: rooney.

X = rooney


5.3 Input and Output using computer

Although input and output of terms is easy, the use of quotes and full

stops can be not always suitable.

A much better approach for

problems of this kind is to input a character at a time. To do this it is first necessary

to know about the ASCII value of a character..

All printing characters and many non-printing characters (such as space and

tab) have a corresponding ASCII (American Standard Code for Information

Interchange) value, which is an integer from 0 to 255.





5.4 Outputting Characters


Characters are output using the built-in predicate put/1. The predicate takes a
single argument, which must be a number from 0 to 255 or an expression that
evaluates to an integer in that range.
Evaluating a put goal causes a single character to be output to the current
output stream. This is the character corresponding to the numerical value (ASCII
value) of its argument.

EXAMPLE :
?- put(97),nl.
a
yes


5.5 Inputting Characters

Two built-in predicates are provided to input a single character: get0/1 and get/1.
The get0 predicate takes a single argument, which must be a variable. Evaluating a
get0 goal causes a character to be read from the current input stream.


Assuming the argument variable is unbound :
?- get0(N).
: Z
N = 90

If the argument variable is already bound,:

?- get0(X).
: a
X = 97

?- M is 41,get0(M).
: )
M = 41


The get predicate takes a single argument, which must be a variable.
The
variable is then unified with the ASCII value of this character in the same way as
for get0.

?- get(X).
: Z
X = 90


5.6 Using Characters


  1. 1. The predicate readin is defined recursively. It causes a single character to be
input and variable X to be bound to its (numerical) ASCII value. The action taken
(the process(X) goal) depends on whether or not X has the value 42 signifying a *
Input and Output 75
character. If it has, the evaluation of the goal stops. If not, the value of X is output,
followed by a new line, followed by a further call to readin.

2.
ASCII
values of the input characters are not output, but the number of characters
(excluding the *) is output. The count predicate is defined with two arguments
which can be read as 'the number of characters counted so far' and 'the total number
of characters before the *

3.
count predicate can be interpreted as 'the
number of vowels so far' and 'the total number of vowels'.
The three arguments of
the process predicate can be read as 'the ASCII value of an input character', 'the
number of vowels up to but not including that character' and 'the total number of
vowels', respectively.



5.7 Input and Output Using Files

Prologue to take input from the current input stream and take the output from the current output stream. For example, the keyboard as input devices and monitors as a means of output. Input and output can also be raised through files and files. Users can open and close the input and output with a variety of files, but can only process one type of file it at the same time.

5.8 File Output : Changing the Current Output Stream

Predicate tell / 1
Current output stream can be changed using this Predicate. With this Predicate, the file was not opened will be deleted and replaced with the current output stream.

Predicate told / 0
This Predicate causes the file name changed to the user's terminal.

Predicate telling / 1
This Predicate causes the variable bound by the name of the current output stream.

5.9 File Input: Changing the Current Input Stream

Current input stream can be changed using the Predicate see, followed by the atom, as an example, see ( 'myfile.txt'). This Predicate causes the file name changed to current input stream. see the Predicate consists of several parts. First, the order seen / 0 which causes current input file into the user's terminal.
Reading from Files: End of File
If the end of the file found when evaluating Predicate get (X) or get0 (X), variable X will be bound to specific numerical values. If the range is 0-255 ASCII code, the code that appears before the code is 1.
Reading from Files: End of Record
End of the line indicated by karakterASCII input value 13. End of the record indicated by 2 values of 13 ASCII characters followed by 10.




Tuesday, November 24, 2009

Simple Expert System Idea

Background
As we know, the expert system is a system that tried to adopt human knowledge to computer designed to model the ability to solve problems like an expert. In this case, we apply the expert system to identify the race of people in the world.
In the world of so many human beings from different types of countries. So that we do not tell people where it came from, then we have to identify their characteristics. By identifying them we will also gain some advantages.

* Mongoloid, with the characteristics:
yellow to dark brown,
straight hair,
little body hair,
narrow eyes especially Asian Mongoloids
smaller and shorter than Kaukasoid race.
North Asia, East Asia, Southeast Asia, North Europe, North America, South America

* Kaukasoid, has the physical characteristics
nose,
whites,
Wavy hair
blonde hair brown to blackish,
straight eyelid
Europe, North Africa, Middle East, Pakistan and North India

* Negroid, with physical characteristics
curly hair,
blacks,
high body
thick lips
straight eyelid
Africa, Guinea, Melanesia


* Austroloid, including indigenous Australians (Aboriginal)
Black
curly black hair
southern India, Sri Lanka, the Original in Malaysia

Benefit:
1. Helping the customs that no illegal immigrants
2. For general knowledge about human diversity in the world
3.
No racism

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.