expert system-DETECTING DAMAGE OF THE CAR


step 1:
declare the (problem,syptoms,solutions)



step 2:
check the compability of prolog.
with identifying problem with the solutions

Summary

Many programming languages provide 'for loops' which enable a set of instructions to be executed a fixed number of times. No such facility is available in Prolog (directly), but a similar effect can be obtained using recursion, as shown in the example programs below.

Example 1

The following program outputs integers from a specified value down to 1.

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

The loop predicate is defined in terms of itself. The second clause can be thought of as: 'to loop from N, first write the value of N, then subtract one to give M, then loop from M'. This process clearly needs to be terminated and this is achieved by the first clause: 'when the argument is zero, do nothing (and hence stop)'. The first clause can be regarded as a terminating condition for the recursion.

?- loop(6).
The value is: 6
The value is: 5
The value is: 4
The value is: 3
The value is: 2
The value is: 1
yes

Example 2

The next program outputs integers from First to Last inclusive.
/* output integers from First to Last inclusive */
output_values(Last,Last):- write(Last),nl,
write('end of example'),nl.
output_values(First,Last):-First=\=Last,write(First),
nl,N is First+1,output_values(N,Last).

Here output_values has two arguments, which can be read as 'output the integers from First to Last inclusive'. The loop terminates when both arguments are the same.

?- output_values(5,12).
5
6
7
8
9
10
11
12
end of example
yes

Looping Until a Condition Is Satisfied

Many languages have an 'until loop' which enables a set of instructions to be
executed repeatedly until a given condition is met. Again, no such facility is
available directly in Prolog, but a similar effect can be obtained in several ways.

Recursion

The first example below shows the use of recursion to read terms entered by the
user from the keyboard and output them to the screen, until end is encountered.

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).

?- go.
Type end to end: university.
Input was university
Type end to end: of.
Input was of
Type end to end: portsmouth.
Input was portsmouth
Type end to end: end.
Input was end
yes

Using the 'repeat' Predicate

Although it can often be used to great effect, 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 name of this predicate is really a misnomer. 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, as shown in the examples below. 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, but the example is included for purposes of illustration.

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).

The first five goals in the body of get_answer will always succeed. Evaluating the fifth goal: read(Ans) will prompt the user to enter a term. If the term input is anything but yes or no, say unsure, the following goal valid(Ans) will fail. Prolog will then backtrack over read(Ans) and write('answer yes or no'), both of which are unresatisfiable, i.e. will always fail on backtracking. Backtracking will then reach the predicate repeat and succeed, causing evaluation to proceed forward (left-to-right) again, with write('answer yes or no') and read(Ans) both succeeding, followed by a further evaluation of valid(Ans). Depending on the value of Ans, i.e. the user's input, the valid(Ans) goal will either fail, in which case Prolog will backtrack as far as repeat, as before, or it will succeed in which case the final three goals write('Answer is'), write(Ans) and nl will all succeed. The overall effect is that the two goals write('answer yes or no') and read(Ans) are called repeatedly until the terminating condition valid(Ans) is satisfied, effectively creating a loop between repeat and valid(Ans).

?- get_answer(X).
Enter answer to question
answer yes or no: unsure.
answer yes or no: possibly.
answer yes or no: no.
answer is no
X = no

Goals to the left of repeat in the body of a clause will never be reached on backtracking.

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.

Searching the Prolog Database

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.

Calling alldogs will cause dog(X) to be matched with the dog clauses in the database. Initially X will be bound to fido and 'fido is a dog' will be output. The final goal in the first clause of the alldogs predicate will then cause evaluation to fail. Prolog will then backtrack over nl and the two write goals (all of which are unresatisfiable) until it reaches dog(X). This goal will succeed for a second time causing X to be bound to fred.

This process will continue until fido, fred and jonathan have all been output, when evaluation will again fail. This time the call to dog(X) will also fail as there are no further dog clauses in the database. This will cause the first clause for alldogs to fail and Prolog to examine the second clause of alldogs. This will succeed and evaluation will stop.

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.

alldogs:-dog(X),write(X),write(' is a dog'),nl,fail.

?- alldogs.
fido is a dog
fred is a dog
jonathan is a dog
no

Finding Multiple Solutions

Backtracking with failure can also be used to find all the ways of satisfying a goal. Suppose that a predicate findroute(Town1,Town2,Route) finds a route Route between two towns Town1 and Town2. The details of this predicate are irrelevant here. It may be assumed that Town1 and Town2 are atoms and that Route is a list. Backtracking with failure can then be used to find all possible routes between Town1 and Town2 and write out each one on a separate line, as follows:

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

INPUT AND OUTPUT in Prolog

Problem 1


Define a predicate makelower/0 which reads in a line of characters from the
keyboard and outputs it again as a single line with any upper case letters converted
to lower case. (The ASCII values of the characters a, z, A and Z are 97, 122, 65 and
90, respectively.)

1.Make a notepad/txt file like the picture below, then save it in .pl format


Problem 1

2.Then consult it in prolog, then type "makelower. Input the letter to be lowercased , and the result will be like this.




Problem 2

Define a predicate copyterms which reads all the terms in a text file and
outputs them as terms to another text file one by one on separate lines.
The output file should be in a format suitable for use as the input file in a
subsequent call of copyterms. Thus for example if the input file contained

'first term'. 'second term'.
'third term'.
fourth. 'fifth term'.
sixth.
The output file would contain
'first term'.
'second term'.
'third term'.
fourth.
'fifth term'.
sixth.

1. Make infile (save in .txt) and outfile (save in .pl) like in the picture.








2.Then consult .pl file then type copyterms('Infile.txt','Outfile.txt').




3. The result will be like this




Problem 3

Create a text file testa.txt containing two lines, each of five characters followed
by a new line, e.g.
abcde
fghij
Define a predicate readfile that will read fifteen characters from this file one by
one and output the ASCII value of each character. Use this to establish whether the Input and Output 83 representations of 'end of file' and 'end of record' for your version of Prolog are as suggested in Sections 5.9.1 and 5.9.2, respectively.

1.make a new prolog file (.pl) like in the picture



2.Then make one .txt file that contain character like in the picture



3.Then consult your prolog file then type readfile('testa.txt'). Then the result will be shown like this :




Problem 4

Using a text editor, create two text files in1.txt and in2.txt, each comprising a
number of terms terminated by end.Define and test a predicate combine that takes the names of two input files as its first two arguments and the name of an output file as its third argument. The output file should contain the terms in the first input file followed by the terms in the second, one per line and terminated by end.

1.make a prolog code like in the picture




2.then make 2 .txt that contain something we want to combine





3. Then consult the prolog file then type combine('In1.txt','In2.txt','Hasil.txt').



4.And the result will be made into hasil.txt

Summary For Chapter 5

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.


Output 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


Inputting Terms

Built-in predicate read is provided to include the term. It takes one argument, which must be variable. Evaluating the causes for the next term read from the input stream, which by default is the user's keyboard. (The meaning of the input current). In the input stream, the term must be followed by a dot ('.') and at least one space, such as spaces or new lines. Point and space characters are read in but not considered part of the term.

example :

?- read(X).
: jim.
X = jim
?- read(X).
: 26.
X = 26
?- read(X).
: mypred(a,b,c).
X = mypred(a,b,c)
?- read(Z).
: [a,b,mypred(p,q,r),[z,y,x]].
Z = [a,b,mypred(p,q,r),[z,y,x]]
?- read(Y).
: 'a string of characters'.
Y = 'a string of characters'


Input and Output Using a Character

Although the input and output of the conditions is very easy, use quotation marks and full stops can be complicated and not always appropriate. For example, would be tedious to determine the predicate (using read) that will read a series of characters from the keyboard and count the number of vowels. A better approach for such problems is to input a character at a time. To do this, first of all necessary
to know about the value of ASCII characters.

Outputting Character

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.

example:
?- put(97),nl.
a
yes
?- put(122),nl.
z
yes
?- put(64),nl.
@
yes

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.

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.)

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 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.

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 output
stream. 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.


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.
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.


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.

Harry's Idea

MATTRESSES BODY CONDITIONING
The reason I wanted to make a mattress body conditioning because people often feel hot and sweaty, especially in the back during sleep.
This is the same as regular mattresses , but have additional cooling in the bed and didn't remove the spring. This makes people feel comfortable going to sleep because the whole body exposed to the wind and sweating. This mattress can be adjusted to the temperature level of each person. So that the temperature will be stable and not be sick.

OPERATOR AND ARITHMATIC in prolog

pada bab ini diperkenalkan notasi operator predikat dan menggambarkan operator
disediakan untuk mengevaluasi dan membandingkan nilai-nilai ekspresi aritmatika, untuk
pengujian untuk kesetaraan aritmatika baik ungkapan atau istilah-istilah dan untuk pengujian untuk
negasi dari tujuan atau pemisahan dari dua gol.

1. Operator

Binary predicate atau predikat dengan dua argumen dapat diubah menjadi bentuk infix operator. Contoh :
Standar : makan (kucing,tikus)
infix operator : kucing makan tikus

Unary predikat (predikat dengan satu argumen) dapat diubah menjadi bentuk prefix operator atau postfix operator. Contoh :
Standar : lucu (anjing)
prefix operator : lucu anjing
postfix operator: anjing lucu

2. Aritmatika
Dengan Prolog kita dapat melakukan penghitungan aritmatika.

a. Operator Aritmatika
X+Y (penjumlahan)
X-Y (pengurangan)
X*Y (perkalian)
X/Y (pembagian)
X//Y the ‘integer quotient’ of X and Y (the result is truncated to the nearest integer between it and zero)
X^Y (pangkat)
-X (negatif)
abs(X) (nilai absolut)
sin(X) (sinus)
cos(X) (cosinus)
max(X,Y)(nilai terbesar)
sqrt(X) (akar)

b. Pengutamaan Operator Dalam Ekspresi Aritmatika
Prolog menggunakan algoritma aljabar biasa dalam pengopersian aritmatika. Contohnya A+B*C-D. Di dalam ajabar C dan D dikalikan lebih dahulu lalu ditambah dengan A lalu dikurangi dengan D. DI prolog juga demikian. Untuk pengecualian, kita tinggal menggunakan kurung. Contoh : (A+B)*(C+D).

c. Operator Relasi
Operator seperti =, !=, >,>=, <, =<, dapat digunakan di Prolog.

3. Operator Pembanding

Berikut merupakan daftar dari equality operators yang digunakan dalam prolog beserta fungsi dari masing-masing operator.
Arithmetic Expression Equality =:=
Arithmetic Expression Inequality =\=
Terms Identical ==
Terms Not Identical \==
Terms Identical With Unification =
Non-Unification Between Two Terms \=

4. Operator Logika


a. Operator Not
Operator not dapat ditempatkan sebelum predikat untuk memberikan negasi. Predikat yang dinegasikan bernilai benar jika predikat yang asli salah dan bernilai salah jika predikat yang asli benar. Contoh penggunaan operator not :
dog(fido).
?- not dog(fido).
no
?- dog(fred).
no
?- not dog(fred).
yes
b. Operator Disjungsi
Operator disjungsi (;) digunakan sebagai operator ‘atau’. Contoh :
?- 6<3;7 is 5+2.
yes
?- 6*6=:=36;10=8+3.
yes



Hasil Practical Exercise 4



Operator and aritmatic



langkah 1 :
buat konfigurasi angka dengan memasukkan rumus rata-rata (X+Y/2), nilai kali, dan nilai terbesar.



langkah 2 :
ketik "number(angka,angka)."
untuk menjalankan prolog.