Rabu, 25 November 2009

SUMMARY OF CHAPTER 5

SUMMARY OF INPUT AND OUTPUT

1. Outputting Terms
The main built-in predicate provided for outputting terms is write/1. The write/1 predicate takes a single argument, which must be a valid Prolog term. Evaluating the predicate causes the term to be written to the current output stream, which by default is the user's screen. The built-in predicate nl/0, It takes no arguments. Evaluating a nl goal causes a new line to be output to the current output stream. for examples;

?- write(26),nl.
26
yes

2. Inputting Terms
The built-in predicate read/1 is provided to input terms. It takes a single argument, which must be a variable. Evaluating it causes the next term to be read from the current input stream,
which by default is the user's keyboard. When a read goal is evaluated, the input term is unified with the argument variable. If the variable is unbound (which is usually the case) it is bound to the input value. for example;

?- read(X).
: 26.
X = 26

If the argument variable is already bound (which for most users is far more likely to occur by mistake than by design), the goal succeeds if and only if the input term is identical to the previously bound value.

?- X=fred,read(X
: fred.
X = fred

3. Input and Output Using Characters
Although input and output of terms is straightforward, the use of quotes and full stops can be cumbersome and is not always suitable. A much better approach for problems of this kind is to input a character at a time.
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. The table below gives the numerical ASCII values corresponding to the main printable characters and some others.

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, for example

?- put(97),nl.
a
yes

5. Inputting Characters
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. The variable is then unified with the ASCII value of this character. Assuming the argument variable is unbound (which will usually be the case), it is bound to the ASCII value of the input character.

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

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.

6. Input and Output Using Files
Prolog takes all input from the current input stream and writes all output to the current output stream. By default both of these are the stream named user, denoting the user's terminal.
The user may open and close input and output streams associated with any number of named files but there can only be one current input stream and one current output stream at any time. Note that no file can be open for both input and output at the same time (except user) and that the user input and output streams
cannot be closed.


7. file Output: Changing the Current Output Stream
The current output stream can be changed using the tell/1 predicate. This takes a single argument, which is an atom or variable representing a file name. Evaluating a tell goal causes the named file to become the current outputstream.
If the file is not already open, a file with the specified name is first created.

The default current output stream is user, i.e. the user's terminal. This value can be restored either by using the told predicate or by tell(user).
The built-in predicate told/0 takes no arguments. Evaluating a told goal causes the current output file to be closed and the current output stream to be reset to user, i.e. the user's terminal.
The built-in predicate telling/1 takes one argument, which must be a variable and will normally be unbound. Evaluating a telling goal causes the variable to be bound to the name of the current output stream.


8. File Input: Changing the Current Input Stream

The current input stream can be changed using the see/1 predicate. This takes a single argument, which is an atom or variable representing a file name, e.g. see('myfile.txt').
Evaluating a see goal causes the named file to become the current input stream. If the file is not already open it is first opened (for read access only). If it is not possible to open a file with the given name, an error will be generated.
Note that the file corresponding to the previous current input stream remains open when a new current input stream is selected. Only the current input stream can be closed (using the seen predicate described below).
The default current input stream is user, i.e. the user's terminal. This value can be restored either by using the seen predicate or by see(user).
The built-in predicate seen/0 takes no arguments. Evaluating a see goal causes the current input file to be closed and the current input stream to be reset to user, i.e. the user's terminal.
The built-in predicate seeing/1 takes one argument, which must be a variable and will normally be unbound. Evaluating a seeing goal causes the variable to be bound to the name of the current input stream.

11.Reading from Files: End of File

If the end of file is encountered when evaluating the goal read(X), variable X will be bound to the atom end_of_file.


12.Reading from Files: End of Record

Typically the end of a line of input at the user's terminal will be indicated byt the character with ASCII value 13. The end of a record in a file will generally be indicated by two ASCII values: 13 followed by 10. The following program shows how to read in a series of characters from the keyboard and print them out, one per line.

INPUT AND OUTPUT

The Answer of Practical Exercise 5 Page : 82-83.

1. Number 1 is about predicate makelower

if we want to convert any upper case letters to lower case, we must write some rules or program in notepad use predicate makelower in order that we can consult it in SWI Prolog. for the first we can write some rules in notepad like the picture below:


After you write its program,
you must write makelower . and enter character that you want on your SWI Prolog. So, you can see the result will be shown in SWI Prolog like the picture below:


2. Number 2 is about predicate copyterms

if we want to reads all the term in a text file and outputs them as terms to another text file one by one on separates lines, we must use predicate copyterms. for the first we must make a file in notepad for input file and save as with txt. format. we give name "infile" for our file.

second, we must make a new file again in notepad for output file, we give name "outfile" for our file.
third, we must make some rules in notepad like this picture below in order that we can consult it to SWI Prolog.
After you write its program, you must write copyterms(‘infile.txt’,'outfile.txt’) . or enter the file's name of your file on your SWI Prolog. So, you can see the result will be shown in SWI Prolog like the picture below:

3. Number 3 is about predicate readfile

if we want to convert a characters to the ASCII value, we can use predicate readfile. according to question number 3, we must make a file in notepad which we must write:

Giving name of the file "tesla" and save it with .txt fomat.
in order we can consult on SWI Prolog, so we must make some rules in notepad like this picture below:

After you write its program, So, you can see the result will be shown in SWI Prolog like the picture below:


4. Number 4 is about predicate combine


create 2 text files in1.txt and in2.txt, each comprising a number of terms terminated by end. you can see this picture below:

to define a predicate combine that takes names of two input files as its first two arguments and the name of an output file as its third argument. 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 its program, So, you can see the result will be shown in SWI Prolog like the picture below:



5. Number 5 is about predicate compare

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 make two text files which contain the same number of terms and the final term in each is end. Look the picture below:


then, we must make some rules in notepad using predicate compare like this picture below in order that we can consult it to SWI Prolog.


After you write its program, So, you can see the result will be shown in SWI Prolog like the picture below:





Slide 4

Senin, 23 November 2009

Expert System Design

Expert System Design for knowing

the characteristic of blood types.


Everyone in the world have different blood type. Karl Landstainer had divide blood into 4 types. The blood types are A, B, AB, and O. In usually, we will know what type of our blood when we check our blood in a hospital or when we want to transfuse our blood in an Indonesia Red Cross Committee. But, after we check what type of our blood, we just know our blood type but we don’t know how a blood can said type A, type B, type AB, or type O. Because of the problem, we make expert system design for knowing characteristic and component of blood types in order to everyone can know how a blood can said type A, type B, type AB, or type O. Before, we show our expert system design, we will explain the characteristic and component of each blood types.


1. Blood type A: when the blood is dropped by Anti A substance (agglutinin A), the blood will make some lumps and when the blood is dropped by Anti B substance (agglutinin B), the blood can’t react or can’t make a lump. It caused by blood type A have aglutinogen A and agglutinin B..


2. Blood type B: when the blood is dropped by Anti A substance, the blood will not react or can’t make a lump and when the blood is dropped by Anti B substance, the blood will make some lumps. It caused by blood type B have aglutinogen B and aglutinin A.


3. Blood type AB: when the blood is dropped by Anti A substance, the blood will make some lump and when the blood is dropped by Anti B substance, the blood will make some lumps, too. It caused by blood type AB have aglutinogen A and B but have not aglutinin A or B.


4. Blood type O: when the blood is dropped by Anti A substance, the blood will not react or can’t make a lump and when the blood is dropped by Anti B substance, the blood can’t make a lump, too. It caused by blood type O have not aglutinogen A or B but have aglutinin A and B.


After we explain to you about the characteristic and component of each blood types, we will show you the program of our expert system design in notepad:



In this picture, it can show our expert system design in SWI Prolog:


Knowing our blood type is very useful, maybe one day someone need our blood which our blood have same type with them, So, we can tranfuse our blood to them. beside that, if we know the characteristic and component of each blood type, it will help us to check our blood type by self in laboratory without medical worker.



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 :