Prolog Fundamentals
Prolog Fundamentals
1972 at the university of Marseilles in France. 0 PROLOG is the one of the most widely used programming languages in artificial intelligence research. 0 As opposed to imperative languages such as C or JAVA, it is an object-oriented language.
INTRODUCTION
0 That means, when implementing the solution to problem, instead of specifying how to achieve
certain goal in a certain situation, we specify what the situation (rules and facts) and the goal (query) are and let the PROLOG interpreter derive the solution for us. 0 PROLOG is very useful in some problem areas, like artificial intelligence, natural language processing, data-bases, robotics, expert system . but pretty useless in others, like graphics and numerical algorithms
3
FEATURES OF PROLOG
1.
2. 3. 4. 5.
6.
Can compile stand alone programs. A full complement of standard predicates is available. A functional interface to other languages is provided. Declared variables are used to provide more secure development control. Both integer and floating point arithmetic supported. Program development, compilation and debugging is very easy because of integrated editor. 4
COMPARISON
PROCEDURAL LANG. OBJECT-ORIENTED LANG.
problems
0 Use heuristics to solve problems 0 Most efficient at formal reasoning 0 Systems developed and maintained by knowledge engineers
and by
cyclic
5
Contd
Contd
Dialog: Output
Contd
Contd
10
Contd
Menu Bar
11
12
language, it means Programming in PROLOG means describing the world. 0 Using such programs means, asking PROLOG questions about previously describe world. 0 The simplest way of describing the world is by stating facts. 0 This states that elephant is bigger than horse.
facts
bigger (elephant, horse). bigger (horse, donkey). bigger (donkey, dog). bigger (donkey, monkey).
13
Contd
14
Contd
0 This is syntactically correct program, and after compile it we can ask question to PROLOG about it.
Goal: bigger ( donkey, dog ) Yes 0 It means Is a donkey bigger than dog?. It says YES because it is already communicated in PROLOG system (fact..). 0 Now, Goal: bigger ( monkey, elephant ) No 0 Here we get exactly what we expected.
15
Contd
16
Contd
17
Contd
18
Contd
19
Contd
20
Contd
Goal: bigger ( elephant, monkey ) No 0 According to this, elephant is not bigger than monkey. This is clearly wrong as far as our real world concerned. 0 In our program, it say nothing about the relationship between elephant and monkey. 0 But, we know that elephant > horse > donkey > monkey.. 0 In mathematical term : the bigger-relation is transitive. But in our program , this has not been 21 defined.
Contd
0 If we would like to get a positive reply for a query like bigger (elephant, monkey), we have to provide a
more accurate description of world. 0 One way of doing this would be to add the remaining facts, like e.g. bigger (elephant, monkey). In our little program this would mean adding another 5 facts. 0 So it means clearly, it would be too much work and probably not too intelligible anyway. 0 The far better solution would be to define a new relation, which we will call is_bigger, as the transitive closure of bigger. 22
Contd
0 Animal X is bigger than Y either if this has been stated as a fact or if there is an animal Z and it can be shown that animal Z is bigger than animal Y. 0 In PROLOG such statements are called rules and are implemented like this :
is_bigger ( X, Y) :- bigger (X, Y) is_bigger ( X, Y) :- bigger (X, Z), is_bigger (Z, Y) 0 In these rule :- means something like if and the coma (,) between the two terms bigger(X, Z) and is_bigger(Z, Y) stands for and. 0 Here, X,Y,Z are variables.
23
Contd
Goal: is_bigger( elephant, monkey) Yes 0 Here, PROLOG still cannot find the fact that bigger (elephant, monkey) in its database, so it tries to 2nd rule instead. 0 This is done by matching the query with the head of the rule [is_bigger (X, Y)]. 0 Here, X= elephant, Y= Monkey 0 The rule says that in order to prove the goal is_bigger (X, Y) PROLOG has to prove the two subgoals bigger (X, Z) and is_bigger(Z, Y) again with 24 the same variables.
Contd
0 This process is repeated recursively until the facts that make up the chain between elephant and
monkey are found and the query finally succeeds. 0 Now, suppose , we want to know what animals are bigger than a donkey? is_bigger( X , donkey). 0 The PROLOG interpreter replies as follows: X= horse 0 In case, we want to find out if there are more animals that are bigger than donkey, we can press the semicolon key(;), which will cause PROLOG to 25 search more solutions to our query
Contd
Goal: is_bigger (X, donkey). X= horse ; X= elephant ; No 0 As a final example, we ask whether there is an animal X that is both smaller than a donkey and bigger than a monkey. Goal: is_bigger (donkey, X), is_bigger (X, monkey). No
26
0 This collection is called a database, and this database is expressed in facts and rules.
EXPRESSING FACTS 0 Prolog permits to describe facts in terms of symbolic relationships. 0 E.g.. Right speaker is not emitting sound: 0 English : The right speaker is dead. 0 PROLOG : is(right_speaker,dead).
27
Contd
is ( right_speaker, dead ) .
Relation : Defines the way in which a collection of Objects belong together.
28
Contd
is ( right_speaker, dead ) .
Objects : Name of an element of certain type. Represents an entity or property of an entity in the real world. There six different object type. This is symbol object type.
29
Contd
is ( right_speaker, dead ) .
PREDICATES : Function with the value of true or false. It express a property or a relationship. The word before the parentheses is the name of relation and elements within the parentheses are the arguments of the predicate , which may be object or variables.
30
Contd
is ( right_speaker, dead ) .
PREDICATE + PERIOD = CLAUSE
31
Contd
0 The relations used in the clauses of the clauses section are
defined in the predicates section. 0 Each relation in each clause must have a corresponding predicate definition in the predicate section. 0 For example :In the clause section is(right_speaker,defective). In the predicate section is(component,status)
32
Contd
0 The domain section also there , it defines the type of each object
Domains Type 1. char single character 2. integer Integer form (32,786 to 32,785) 3. real Floating-point no. 4. string character sequence 5. symbol character sequence of letter 6. file symbolic file name
33
Rules
Relations
Arguments
Objects
Variables
34
35
Contd
36
Contd
37
Symptom(Diesease,runny_nose)
Clause 1
Symptom(flu , runny_nose)
Clause 2
Clause 3
Clause 4
Clause 5
Symptom(cold,runny_nose)
38
COMPOUND GOALS
0 Suppose patient has several symptoms and we want to express all of these symptoms in a compound goal
to find the single dieses that all of them relate to. 0 E.g. :- the patient has a runny nose and a mild body ache. Goal:symptom(Diseas, mild_body_ache) and symptom(Diseas, runny_nose) 0 Lets implement it on PROLOG..
39
Contd
40
BACKTRACKING
0 The solution of the compound goal proceeds :--
Execution
Top to bottom Left to right
41
BACKTRACKING
0 If any condition in the chain fails, Prolog backtracks to previous condition,
through the conditions, trying every available binding in an attempt to get the goal to succeed as many ways as possible. It find its own way.
42
Contd
43
Contd
44
USING RULES
0 A rule is an expression that indicates the truth of a particular fact depending upon one or more facts.
X is sister of Y If X is female And parents of X are M and F And parents of Y are M and F 0 The process of using rules to solve problems is called formal reasoning 0 It gives PROLOG the ability of decision making.
45
Contd
hypothesis(vitc_deficiency) if symptom(arthritis) and symptom(infection_sensitivity). 0 Here, we can see that conclusion is stated first and is followed by the word if. 0 The condition upon which the conclusion depends are stated next, each connected by the word and. 0 The rule, like a fact, is terminated with a period. 0 Conclusion is viewed as a PROLOG goal.
46
Contd
0 This goal is true if all the conditions for the goal are true. 0 Every rule has a conclusion (or Head) and
antecedent ( or body). 0 The Body may be consists one or more premises. 0 If all the premises are true, the conclusion is true, if any premises fails, the conclusion fails. hypothesis(vitc_deficiency) :symptom(arthritis), symptom(infection_sensitivity). 0 The :- operator is called a break, a comma express an and relationship, and semicolon expresses an or relationship. 47
UNIFICATION
0 The process by which prolog tries to match a term against the facts or the heads of other rules in an
effort to prove goal is called unification. 0 Predicates unify each other if : They have the same relation name. They have the same number of arguments. All argument pairs unify with each other.
48
BUILT-IN PREDICATES
0 It classified into nine groups..
Control predicates ii. Reading predicates iii. Writing predicates iv. File system predicates v. Screen-handling predicates vi. String-handling predicates vii. Type-conversion predicates viii. System-level predicates ix. Not- predicate
i.
49
OUTPUT PREDICATES
0 Three output predicates: 1. write
2. writedevice 3. writef
50
go :hypothesis(Patient,Disease), write(Patient, probably has ,Disease,.) ,nl. 0 If we have program like. 0 Now when we start turbo PROLOG and it prompts you for a goal, we only need enter the word go. 0 Go will unify the head of you new rule and PROLOG will begin trying to prove hypothesis(Patient, Diseas).
51
Contd
0 If this goal will succeed , the write predicate displays the value of these variables in sentence:
Charlie probably has german_measles. 0 If there is no nl (new line)then It could be test:write (This is an example), write(of multiple write statements.). Displays: This is an example of multiple write statements.
52
Contd
write (This is an example),nl. write(of multiple write statements.). 0 Then the result would be. 0 This is an example of multiple write statements.
53
54
INPUT PREDICATES
readln : string or symbol readchar : character readint : integer readreal : real
55
Contd
readchar Predicate 0 Use to read only single character. write(Does the ,patient, have a fever(y/n)?), readchar(Reply),nl Reply = y .
Readln Predicate 0 Use to read any string or symbol into a variable. Symptom(patient,fever):write(Does the ,patient, have a fever(yes/no)?), readln(Reply)
Reply=yes.
56
Contd
readint Predicate 0 Can be used to read an integer value to a variable. Write(what is,patient, s age?). readint(Age), Age>=12, Write(Patient,cannot be evaluated with),nl Write(this system.). readreal Predicate 0 Can be used to read floating-point numbers into a variable.
0 Write(what is the real price of ,Item,?),
readreal(Price).
57
ARITHMETIC OPERATOR
+ * / // ** Addition Subtraction Multiplication Real division Integer division Power
0 Goal : X is 3*4.
X = 12 Yes
58
LOGICAL OPERATOR
a :- b. /* a if b */ a :- b,c. /* a if b and c.*/ a :- b;c. /* a if b or c.*/ a :- \++ b. /* a if b is not provable*/ a :- not b. /* a if b fails*/ a :- b -> c;d. /* a if (if b then c else d)*/
59
0 Basic Applications:
Expert System Natural Language Processing
Robotics
Gaming and Simulation
60
Contd
EXPERT SYSTEM 0 Expert system are programs that use inference techniques that involves formal reasoning normally performed by a human expert to solve problems in specific area of knowledge. 0 Each expert system can advise, diagnose, analyze and categorize using a previously defined knowledge base. 0 The knowledge base is a collection of rule and facts which can be written in PROLOG.
61
Contd
NATURAL LANGUAGE PROCESSING 0 Using Prolog, knowledge about human language is expressed in formal rules and facts. 0 The computer can then begin a dialog with the user, asking questions about the problem and interpreting the users answer.
62
Contd
ROBOTICS 0 Robotics is a branch of artificial intelligence concerned with enabling computers to see and manipulate objects in their environment. 0 Prolog facilitates the development of robotic control programs that can use input data from sensors to control manipulators. 0 A robot can then apply formal reasoning to decisions, much as a human being does.
63
Contd
GAMING AND SIMULATION 0 Prolog is ideal for games and simulations (cognitive modeling) involving formal reasoning. 0 Most games and simulation employ a set of logical rules that control the play or action which is very adaptable to Prolog programming. 0 Classical game such as N Queen Problem is written in Prolog.
64
65
is a type of control and is essential to good programming. 0 In prolog, the fail forces backtracking in an attempt to unify with another clause. 0 This predicate when invoked, the goal being proved fails and Backtracking is initiated. 0 The predicate has no arguments so failing at the fail predicate is not dependent on variable binding.
19 April 2012 66
19 April 2012
67
the first rule, and the testing begins for that rule. 0 The location(city,state) predicate forces city to bind with jackson and state to bind with MS. 0 Bcoz of formatting of writef predicate, the City is leftjustified in a field of 10 characters, newline character also executed. 0 The testing of the rule fails, forcing backtracking. 0 Prolog backtracks, due to writef predicate which is always true.
19 April 2012 68
19 April 2012
69
marks the place of each binding, so it can return & unify with the next clause in sequence.
0 Prolog proceeds from left to right, going as far as it can on one path before backtracking & trying another. In eg., it exhausts all possibilities of proving the 1st go clause, before moving on to the 2nd one.
19 April 2012
70
RESULT
0 The compilation and execution will give following
19 April 2012
71
EXAMPLE
0 In prolog, two or more rules may end with fail
19 April 2012
72
criteria. 0 The fail predicate is used to skip particular items from database . Exa, we can list all city/state combinations in the database except DC. 0 When location(city,state) has state bound to DC, chkstate(state) will fail, forcing backtracking. 0 Prolog backtracks when chkstate(DC) fails, which tests the second clause, which succeeds and Prolog writes Washington, DC but that is required to be skipped. 0 So to make this sructure work, we need to use CUT predicate.
19 April 2012 73
74 74
Exclusion Example
19 April 2012
75
19 April 2012
76
in terms of itself.
19 April 2012
77
78 78
Explanation
0 When the goal count(N) is specified, it unifies with the second
clause.
0 The number is displayed, and NN is bound to 1+1, or 2. 0 The rule now tries to match count(2), which unifies with a copy
19 April 2012
79
always succeeds.
the backtracking, causing Prolog to move forward again on the same path.
19 April 2012 31 January 2007 81
82 82
83 83
Explanation
0 The logon routine compares name and password entered by each user to a pair stored in a table, if matching, logon predicates succeed otherwise fails 0 First logon predicate will clear window 0 Asking for input as name and password 0 If it matches desired data, first logon rule will make logon successful, but if not matching, then second logon rule will be executed and will display
85 85
86 86
87 87
explanation
0 When the wrong input is used prolog falls through the second
logon rule and begins to try to prove it. 0 The repeat predicate will succeed first time bcoz of the first repeat clause(a fact), which is always true. 0 Prolog reaches the getinput(Name, Password) premise, asking for other user inputs. 0 If the input is correct this time, the second logon rule succeeds 0 If fails, backtracking is invoked, returning prolog to the first repeat predicate. 0 Prolog invokes second repeat predicate, which is a rule:
19 April 2012
88
Cont
0 So the repeat clause will repeatedly ask for a new inputs
another pair.
0 So actually creating a loop. 0 It moves forward and backward repeatedly and ntill
19 April 2012
89
RULES OF RECURSION
0 A program must include some method of terminating
the recursion.
current layer. when the copy of rule is invoked, the variable in the copy are bound through the process of unification, just as in any other invocation.
19 April 2012
90
THE CUT
0 The primary purpose of the cut is to prevent or block
0 The cut predicate always succeeds, & once it succeeds, it act as fence, effectively blocking any backtracking beyond the cut
19 April 2012 91
THE CUT
0 If any premise beyond the cut fails, Prolog can only
0 If the rule itself fails & the cut is the last premise, no
19 April 2012
92
93 93
explanation
0 In the original example without cut, the chkstate(DC)
predicate unified with the first chkstate(DC) clause, which then failed. 0 The prolog yhen backtracked to the chkstate(_) clause. 0 This always succeeded, all items were displayed whether they were DC addresses or not. 0 In the modified program. The cut prevents prolog from backtracking to the chkstate(_) premise and try to prove it. 0 When the chkstate(DC) conclusion fails, Prolog gives up on proving chkstate with the DC binding, so it backtracks to location(City,State) and next variable binding is tried.
19 April 2012
94
THE CUT
0 The basic purpose of the cut is to eliminate certain
0 The cut prevents backtracking. 0 In some cases, it serves a procedural function: The program will not work without it.
19 April 2012
95
THE CUT
0 In some cases, it serves a heuristics function: The
program will work without it but cut eliminates the need to test irrelevant paths & speed up execution
0 Assume rule of the form go:-premise1 and premise2 and ! And premise 3 and premise4.
19 April 2012
96
THE CUT
0 The cut can be used for any or all three purposes: To terminate the search for any further solutions to a goal once a
particular rule has been forced to fail with the fail predicate.
To eliminate paths in the database to speed up execution.
19 April 2012
97
TYPES OF CUT
0 Two types of cut:- red cut and green cut. 0 The green cut is use for force binding to be retained, once the fail clause is reached. 0 Used to express determinism. 0 The red cut is used to omit explicit conditions.
19 April 2012
98
ARITHMETIC OPERATIONS
0 EXAMPLE-1 0 EXAMPLE-2
7 False Goal:
99
ARITHMETIC OPERATIONS
Five basic rules govern the equal sign in Turbo Prolog 0 If one operand is a free variable & other is object, the variable will become bound to the object value, and the goal will succeed. Eg. If X=4+3, X will be bound to 7
0 If one operand is bound variable & other is an object, the goal will succeed only if the variable is
already bound to the value of the object. 0 If X is bound to 7 & X=5+2 is assessed, the pgm display True
19 April 2012 100
ARITHMETIC OPERATIONS
0 If one variable is free variable & other is bound variable, the free variable will be bound to same
value as the bound variable, and the goal will succeed. 0 If Y has been bound to 7, X=Y will bind to 7 0 If both variable are bound variables, the goal will succeed only if both are bound to same value. 0 If X is bound to 7 & Y is also, then X=Y will be true; otherwise it will be false.
19 April 2012
101
ARITHMETIC OPERATIONS
0 If both operands are objects, the goal will succeed only if they are the same.
19 April 2012
102
COMPARISON OPERATORS
0 Permit a program to compare bound variables or
19 April 2012
103
ARITHMETIC OPERATIONS
0 Example: 0 Goal: go
2 True Goal:
104
ARITHMETIC OPERATIONS
0 TYPE CONVERSIONS Predicates:
COMPOUND OBJECTS
0 In Prolog, it is possible to create object that contains other objects. The resulting structure is called a
compound object. eg.: address(name,street,city,state,zip) Here, Entire address treated as single object in predicate. First part of compound object is the objects name is called functor. Eg. Address
19 April 2012 107
COMPOUND OBJECTS
0 Each object that is a part of the arguments list is called
19 April 2012
108
19 April 2012
109
EXPERT SYSTEM
19 April 2012 110
EXPERT SYSTEM
0 An expert system is a computer program that uses
knowledge and inference techniques to solve problems that are usually solved with human expertise.
0 Stores large body of facts with rules, used to solve the problem.
19 April 2012
111
EXPERT SYSTEM
0 Knowledge is stored for only single subject area called
domain.
human expert to determine how to express the reasoning process of human expert in objective form.
19 April 2012
112
PRODUCTION SYSTEM
USER
KNOWLEDGE BASE
19 April 2012
113
FRAME-BASED SYSTEM
0 Object is represented by frame.
relationships,
expressing
19 April 2012
114
Reference
0 Prolog software 0 Introduction to turbo prolog, By: carl townsend
19 April 2012
115
THANK YOU
19 April 2012 116