Summary Input and Output

built-in predicates that read from and write well for users
terminal (keyboard and screen) or a file, the second term by term and characterby --
characters in your own programs. ASCII value for manipulating character strings. Prolog has a facility to enable both input and output of the term or character. Using simple terms and will be explained first. Initially, it will be assumed that
all the output to the screen the user and all user input is the keyboard. Inputs and outputs use external files.

1.Outputting Terms

predicate takes one argument, which must be a valid Prolog term. Evaluate the causes predicate terms to be written to the output current river, which by default is the user's screen. (The meaning of output current Logic Programming With Prolog streaming)
Examples
?- write(26),nl.
26
yes
?- write('a string of characters'),nl.
a string of characters
yes
?- write([a,b,c,d,[x,y,z]]),nl.
[a,b,c,d,[x,y,z]]
yes
?- write(mypred(a,b,c)),nl.
mypred(a,b,c)
yes
?- write('Example of use of nl'),nl,nl,write('end of example'),nl.
Example of use of nl
end of example
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. (The meaning of current input stream willbe explained in Sections 5.7 and 5.9. At present it can simply be taken to mean theuser's keyboard.)
In the input stream, the term must be followed by a dot ('.') and at least onewhite space character, such as space or newline. The dot and white space
characters are read in but are not considered part of the term.
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.

4.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. For example, it would be tedious to define a predicate (using read) which would read a series of characters from the keyboard and count the number of vowels. 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.
The table below gives the numerical ASCII values corresponding to the main printable characters and some others.

5. 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 (ASCIIvalue) of its argument.

6. 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. The variable is then unified with the ASCII value of this character.

7. Using Characters: Examples

The first example shows how to read in a series of characters from the keyboard finishing with * and to output their corresponding ASCII values one per line (for all characters excluding *).
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 * 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. This process goes on
indefinitely until a * character is read. (In the example below, the ASCII values of characters P, r, o etc. are correctly shown to be 80, 114, 111 etc.)
Assuming the argument variable is unbound (which will usually be the case), it is bound to the ASCII value of the input character.

8. 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, i.e. keyboard for input and screen for output.
The same facilities available for input and output from and to the user's terminal either term by term or character by character are also available for input and output from and to files (e.g. files on a hard disk or a CD-ROM).
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.

9. 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, e.g. tell('outfile.txt').
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 (any existing file with the same name is deleted).
Note that the file corresponding to the previous current output stream remains open when a new current output stream is selected. Only the current output stream can be closed (using the told predicate described below).
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.

10. 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.
If the end of file is encountered while evaluating the goal get(X) or get0(X), variable X will be bound to a 'special' numerical value. As ASCII values must be in the range 0 to 255 inclusive, this will typically be -1, but may vary from one implementation of Prolog to another.

12.Reading from Files: End of Record

Depending on the version of Prolog used, there may be an incompatibility for
character input between reading the end of a record (i.e. the character(s) that
signify the end of a line) from the user's terminal and from a file.
Typically the end of a line of input at the user's terminal will be indicated by
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.
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Exercise 5 INPUT and OUTPUT (compare)

nice to meet you again my friends..
this is last tutorial in exercise 5..
now we will learn about compare..
check it out...
1. Firstly make a new file in notepad and write like this as below.
2. Secondly same with last step, make a new file in notepad and write like as below.
3. Then, write a new rule in notepad like as below, nad save it with name exercise5no5.pl.
4. After that, consult that in SWIprolog.
5. Write command like this " ?- compare('test1.txt','test2.txt'). "
6. Finally the result is
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Exercise 5 INPUT and OUTPUT (combine)

hei hei hei..
nice to meet you again...
same like last tutorial...
we will continuing last exercise, now we will learn about combine..
let's it...
1. Firstly, make a new rules in notepad like as below.
2. Then, save it with name exercise5no4.pl, don't forget as the format is .pl.
3. After that make a new file in notepad like as below.
4. same like before step, now we make a new file like as below.
5. Don't forget save it in .txt format.
6. After that consul exercise5no4.pl in your SWIprolog.
7. last write the command like this " ?- combine('in1.txt','in2.txt','out.txt'). "
8. Finally the result is
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Exercise 5 INPUT and OUTPUT (readfile)

hua hua hua..
Why I always meet you..., just kidding my friends..
ok, now we will learn about readfile...
do you know about that???
if you don't know, so check it out...
1. Firstly, write abcdefghjij in notepad like as below in notepad.
2. And then save it with name text3.txt.
3. Then, now we must make a new rule in notepad like as below, and don't forget save it with
name exercise5no3.pl.
4. Last consult this file in SWIprolog and give the command.
5. Finally the result is like as below.




  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Exercise 5 INPUT and OUTPUT (copyterm)

hello my friends...
this is continuing from last tutorial..
now we will learn about copyterm..
so let's it...
1. firstly, you must write like below in notepad and save it with names text1.txt.

2. After that write the new rules like as below.

3. last consult the rule in your SWIprolog and give your command.
4. Finally, the result is

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Exercise 5 INPUT and OUTPUT (makelower)

hello all of my friends..., nice to meet you again...
it's to learn input and output...
so let's learn it...
1. First you must make a new rule in notepad.
2. write the rules as below.
3. After that save it with name's exercise5n01.
4 .Then open your SWIprolog and consult exerccise5no1.
5. Test with command is makelower.
6. Finally, the result will be shown as follows:

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Rice-Crop Doctor Expert System

National Institute of Agricultural Extension Management (MANAGE) has developed an expert system to diagnose pests and diseases for rice crop and suggest preventive/curative measures. The rice crop doctor illustrates the use of expert-systems broadly in the area of agriculture and more specifically in the area of rice production through development of a prototype, taking into consideration a few major pests and diseases and some deficiency problems limiting rice yield.

The following diseases and pests have been included in the system for identification and suggesting preventive and curative measures. The diseases included are rice blast, brown spots, sheath blight, rice tungro virus, false smut fungi, bacterial leaf blight, sheath rot and zinc deficiency disease. The pests included are stem borers, rice gall midge, brown plant hopper, rice leaf folder, green leaf hopper and Gundhi bug.

example X,Y,Z. X is diseases, Y is symptoms, and Z is solution.

for example1 : the diseases is hama putih, then we will be given the symptoms and solution.

sysmptomps : daun padi menjadi putih

solution : penyemprotan insektisida Klitiop 50EC atau Tomator 3G

this is the table of diseases and solution :


  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Summary 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.
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Operator and Arithmatic part2

Now, we continue our studying with Arithmetic Operator using Prolog. As we know that there are some operators we can use in arithmetic, for example +, -, /, *, etc. For more information about this operator, you can see it in TUTORIAL category and title OPERATOR AND ARITHMETIC (Summary).

For application of this operator, see the question below :

With a predicates that consists of two arguments, both numbers, please calculate

a. Their average

b. The square root of their product

c. The larger of those number

1. To answer it, open your prolog and then create new file namely part.pl

2. Write a rule as follow :

Notes :

average(X,Y,Average) :- Average is (X/Y)/2.

Means that to calculate the average, we use formula as : Average = (X+Y)/2

square_root(X,Y,Square_root):- Square_root is sqrt(X*Y).

means that to calculate the square root of two product numbers, we use formula :

Square_root = sqrt(X*Y)

result :

3. Consult part2.pl, write number(10,20).

you can change 4 and 5 with other numbers.

4. Press Enter


just try it my friends..
good luck..
do the best...
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Operators and Arithmatic part1

hahai friends

why I always meet you???

ok2 no wories..

Now we will learn about Operator and Arithmetic in Prolog.

For example:

It is file convert.pl

and then the result is.............

Many kinds of operator that used in Prolog, now we will explain about another steps.

e.g .

likes(john,X).

Using infix operator, become : john likes X

dog(fred).

Using prefix operator, become : isa_dog(fred).

Using postfix operator, become : fred is_a_dog

So, if you find rules like :

likes(john,X):-is_female(X),owns(X,Y),isa_cat(Y).

It will be easier to understand if written as below :

john likes X:- X is_female, X owns Y, Y isa_cat.

NO WORRIES, the result will be the same as.

Still not sure????

Proof it together with us!!!

1. Open your prolog and create new file namely convert.pl agian

2. Write rules and fact like :

and then the result is......
no wories to leran it...
just try and study hard...
bye.....
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS