2. Different programming languages to
implement AI problem
AIML (Artificial Intelligence Markup Language)
IPL (Information Processing Language)
LISP
PROLOG (Programming in Logic)
Planner
Haskell
Wolfram Language
3. History of prolog
developed in 1972
by Alain Colmerauer and P. Roussel
at the University of Marseilles in France.
6. Who is father of Roy?
Bob is male.
May is Female.
Bo is Jill’s brother.
Kath is Bo’s sister.
Bo is father of Ali.
Ron is brother of Ali.
Ali is sister of Ron.
Bo is husband of Joy.
Joy is Bo’s wife.
Ben id Jill’s husband.
Jill is Ben’s wife.
Bo is father of Roy.
Jeff is kath’s husband.
Kath is Jeff’s wife.
Tim is brother of Roy.
Beth is daughter of Jeff and Kath.
Tim is son of Jeff and Kath .
7. Declarative v/s Imperative
Declarative Language Imperative Language
Programmer specifies what is to be
computed
Programmer specifies how this to be
computed
A set of statements A sequence of commands
Can be divided in to Functional
Language and Logic Programming
Divided in to procedural Language and
Object Oriented Language
Logic Programming (PROLOG)
Functional Language (Haskell)
Procedural Language (C , PASCAL)
Object Oriented Language (C++, Java)
8. Why Prolog in Intelligent?
Because…..
It approaches the
problems in the
same way that the
human does.
9. Applications for prolog
• Expert Systems
• Natural Language Processing
• Robotics
• Gaming and Simulation
11. Collection of knowledge - database
The main part of prolog program consist of collection
of knowledge about specific subject.
This collection is known as database.
Database can be expressed in facts and rules.
12. Knowledge Base
Bob is male.
Bo is father of Ron.
Statements like this is known as facts.
Natural Language Translation in Prolog
Bob is male. Male(bob).
May is Female. Female(May).
Bo is Jill’s brother. Brother(Bo,Jill).
Bo is husband of Joy. Husband(Bo,Joy).
Jill is Ben’s wife. Wife(Jill,Ben).
Jeff is kath’s husband Husband(Jeff,Kath).
Beth is daughter of Jeff and Kath. Daughter(Beth,Jeff,Kath).
13. Expressing Facts
English- The right speaker is dead.
Prolog- is (right_speaker, dead)
English- Bill is an employee.
Prolog- employee(Bill)
English- Bob is married to Marry.
Prolog- married_to(bob,mary)
English- The speaker is defective.
Prolog- defective(speaker)
English- Tom is student.
Prolog- student(Tom)
English- Ganesh is son of Shiva and Parvati.
Prolog- son(Ganesh, Shiva, Parvati)
14. Some Facts about “Fact”
Full stop “.” at the end of every fact.
Functors must begin with a small letter.
There must never be a space between the functor and the
opening bracket!
The number of argument in a fact is called “arity”.
Eg. Female (mary) is an instance of female/1.
Where female is functor and 1 is arity.
Husband(Jeff,Kath) is an instance of Husband/2.
Daughter(Beth,Jeff,Kath) is an instance of daughter/3.
Arity is useful to say female/1 is different than female/2.
16. clauses
In Prolog, rules (and facts) are called clauses.
Clause in Prolog is a unit of information in a Prolog
program ending with a full stop ("."). A clause may
be a fact, like:
likes(mary, pizza).
food(pizza).
Notice the period at the end of clause.
The above clauses are also known as base clause.
18. Predicate
Has_a(bill,computer).
The entire expression before the period (.) is known
as predicate.
A predicate is a function with a value of true or false.
Predicate express a property or a relationship.
Here “has_a” is name of predicate.
20. Relations
is (reight_speaker,dead)
The Word “is” is the relation in the example.
A relation is a name that defines the way in which a
collection of objects (or objects and variables
referring to objects) belong together.
Fact Relation
has_a(bill,computer). has_a
is_a(collie,dog). is_a
likes(sue,chocolate). likes
23. Object and variable
Is(right_speaker,dead)
An object is the name of an element of a certain type.
It represents an entity or a property of an entity in
the real world.
Here right_speaker and dead are object
Likes(X,Y)
Here X and Y are variable with specific data type.
24. Several Things about clauses
A given relation can have any number of objects. A predicate
can have any number of arguments, including zero. The
number of argument is called the arity of the predicate.
An object name can represent a physical entity (Bob,
automobiles) or an abstract concept (defective). It can be a
noun, adverb or adjective. Indeed, it can any sequence of
characters.
Objects are always singular. Ex, automobiles is considered as
singular object.
Certain names are reserved in Prolog and should not be used
for object names. Ex, abs, not, fail ,if , display
The prolog expression does not have to contain all the words
of the English expression.
25. Think and give Answer…
Likes( tom, janet)
Likes( janet , tom)
Both predicates are same????
27. Parts of Prolog Program
A standard prolog program consist of 4 parts.
1. Domains
2. Predicates
3. Clauses
4. goal
28. Structure of Prolog Program
/* comments */
% A full line comment
domains
// ….. //
predicates
//………….//
clauses
//………….//
Goal
//……………..//
29. Turbo Prolog domain Type
Char : Single character enclosed between single
quotation mars. Ex: ‘A’ , ’X’ , ’W’
Integer: Integer from -32768 to +32768
Ex : 1, 735,1001
Real : floating point number
Ex: 2.54, 6,78
Symbol : character sequence of letters, numbers and
underscores with the first character as lower case
letter. Ex: status, disease
File: symbolic file name. Ex, file.txt
30. Domains
Frank is a male who is 45 years old.
person(Frank,male,45)
person(symbol , symbol , integer)
domains
Name , gender=symbol
Age=integer
31. domains
The domains section is where you declare any
domains you're using that aren't Prolog's standard
domains.
Domains enable you to give distinctive names to
different kinds of data that would otherwise look
alike.
It is sometimes useful to declare a domain when you
want to clarify portions of the predicates section.
Declaring your own domains helps document the
predicates that you define by giving a useful name to
the argument type.
32. Predicates
Frank is a male who is 45 years old.
person(Frank,male,45)
person(symbol , symbol , integer)
predicates
person(symbol , symbol , integer)
Frank is a male who is 45 years old.
person(Frank,male,45)
person(symbol , symbol , integer)
33. Predicates
If you define your own predicate in
the clauses section of a Prolog program,
you must declare it in a predicates section,
or Prolog won't know what you're talking about.
When you declare a predicate, you
tell Prolog which domains the arguments of that
predicate belong to.
You don't need to declare any of Prolog's built-
in predicates that you use in your program
34. clauses : An instance of predicate
Frank is a male who is 45 years old.
person(Frank,male,45)
clauses
person(frank,male,45)
person(sue,female,26)
person(bob,male,39)
35. clauses
The clauses section is where you put all the facts and
rules that make up your program.
Clauses for a given predicate must be placed
together in the clauses section; a sequence of clauses
defining a predicate is called a procedure.
39. Term
Data structures in PROLOG = Terms
Term is a basic data structure in Prolog.
i.e., everything including program and data is
expressed in form of term.
The generic name for all forms of Prolog data is
"term".
40. Prolog Data structure/Data Type
Term/data object
simple object structure
constants variables
Atoms numbers
41. Atom
Atom is non-numeric literal constants.
Atom can be constructed in three ways.
Atoms can be strings of following characters.
1. upper-case letters (A,B,…..Z)
2. Lowe-case lettres (a,b,…..z)
3. Digits (0,1,2….9)
4. Special characters (+ , _ , * , / , < , > , = , : , . , & , _ , ~ )
42. Atom
1. String of letters , digits and underscore character, ‘_’
starting with a lowercase letter.
Ex, anna
nil
x25
x_25
x_25AB
x_
x_ _y
alpha_beta_procedure
miss_Jones
sarah_jones
43. Atom
2. Strings of special characters
Ex, <--->
======>
…
::=
When using atom of this form, some care is necessary
because some string of special characters already
have a predefined meaning; an example is ‘:-’
44. Atom
3. String of characters enclosed in single quotes.
This is useful if we want, for ex, to have an atom that
starts with a capital letter.
Ex, ‘Tom’
‘South_America’
‘Sarah Jones’
45. Confusing!!!!! How????
Atoms and arguments are different things! Atoms
may be used as arguments but not all arguments are
automatically atoms!
46. Number
Number include integer numbers and real numbers.
Ex, 1
1313
0
-97
3.14
-0.0035
100.2
47. Variables
Variables are string of letters, digits and underscore
characters. They are start with an upper-case letter
or and underscore character.
Ex, X
Result
Object2
Participant_list
ShoppingList
_x23
_23
48. Ground Term
The variable free term is known as ground term.
Ex,
person(peter, mueller, date(27, 11, 2007))
50. Anonymous Variable / Don’t care variable
Goal: symptom(_,chills)
True
Goal: likes(jane,_). (Jane likes everything)
Because there is no variable, there is no binding; if prolog
program can match the relation name and the last
argument, the goal succeeds.
Prolog doesn’t tell you which disease had the symptom
chills, as it would have if you had used ordinary variable.
Note that in a clause, the anonymous variable stands for
all values, while in a goal it is satistied if at least one
value corresponds to it.
51. Think Think….
Can we use underscore in a clause ??
If yes, what is impact of it…
52. Structure
Structure objects are objects that have several components.
The components themselves can, in turn be structure.
For ex, the date can be viewed as structure with three
components: day,month,year.
Although composed of several components , structures are
treated in the program as single objects.
In order to combine the component into single we choose a
functor.
For Ex, date(1,may,2001)
All the components in this example are constants (2 integer
and one atom).
Date is functor.
53. Which of the following are syntactically correct
prolog object? What kind of object are they?
1. Diana
2. diana
3. ‘Diana’
4. _diana
5. ‘Diana goes south’
6. goes(diana,south)
7. 45
8. 5(X,Y)
9. +(north,west)
10. three(Black(Cats))
1. Variable
2. atom
3. atom
4. variable
5. atom
6. structure
7. number
8. incorrect
9. structure
10. structure
54. Compound Goal
domains
disease , indication=symbol
predicates
sympton(disease,indication)
clauses
sympton(chicken_pox,high_fever).
sympton(chicken_pox, chills).
sympton(flu,chills).
sympton(cold,mild_body_ache).
sympton(cold, runny_nose).
sympton(flu,runny_nose).
sympton(flu,moderate_couh).
Goal: symptom(Disease,runny_nose) and
symptom(Disease,mild_body_ache)
Disease=cold
1 solution
True
Prolog
work from
left to
right for
proving
compound
goal.
56. Backtracking
The solution of compound goal proceeds from left to
right
If any condition in the chain fails, prolog backtracks
to the previous condition, tries to prove it again with
another variable binding, then moves forward again
to see if the failed condition will succeed with the
new binding,
Prolog moves relentlessly forward and backward
through the conditions, trying every variable binding
in an attempt to get the goal to succeed in many ways
as possible.
58. Rules
A rule is an expression that indicates that the truth of
a particular fact depends upon one or more other
facts.
Ex,
A and B are sisters if
A and B are both female and
they have the same father and
they have the same mother and
A is not the same as B
Sister(A,B):-
female(A),female(B),father(X,A),father(X,B),mother
(Y,A),mother(Y,B),not_same(A,B).
59. Rules
The symbol ‘ :- ‘ is known as turnstile or break
operator.
‘ :- ’ is pronounced as ‘ If ’.
Syntax:
head_of_the_rule :- body_of_the_rule
(a.k.a) (a.k.a)
conclusion antecedent
(consist of)
premises 1,premises 2, …premises n
60. Rules
Each premises are connected by the word ‘and’ or
‘conjunction’ (^).
The ‘goal’ is true if all the conditions specified for the
goal are true.
If all the premises are true, the conclusion is true; if
any premises fails the conclusion fails.
A rule expresses a relationship between facts. Any
given prolog program is simply a database
(collection of clauses) of facts and rules.
61. Conjunction and disjunction in rule
A comma expresses an ‘and’ relationship and
semicolon expresses an ‘or’ relationship.
Holiday in college if
there is Sunday or
there is public holiday
Holiday(college):-day(Sunday);day(public_holiday)
OR
Holiday(college):-day(Sunday).
Holiday(college):-day(public_holiday)
62. Formal Reasoning
The process of using rules and facts to solve a
problem is called formal reasoning.
63. Some important points about prolog execution
Rules
In medical diagnosis system you have seen how the
variable are used.
There are no “global” variable; that is, variables that
maintain a value throughout the entire program’s
execution.
A variable is local to the clause of which they are part.
Even if the same variable name is used in another clause ,
it is not the same variable.
Ex, daughter(X):-father(Roy),mother(Sue).
daughter(X):-father(Bob),mother(Lice).
64. A term is said to be unify if…..
Unification is a pattern matching process.
1. Both terms appear in predicates that have the same
number of arguments (i.e. same arity), and both
terms appear in the same position in their
predicate.
2. Both terms appear as argument of the same type - a
symbol type can only unify with a symbol and so
on.
3. All subterms unify with each other .
65. Rules for Unification (1)
1. A variable that is free will unify with any term that
satisfies the preceding conditions. After unification, the
variable is bound to the value of the term.
likes(ron,br0om).
likes(harry,X):-likes(ron,X).
Goal: likes(harry,broom)
Here Variable X is bounded with the value Broom.
66. Rules for Unification (2)
2. A constant can unify with itself or any free variable.
If the constant is unified with variable, the variable
will be bound to the value of the constant.
Ex, likes(harry,school).
Goal : likes(harry,school)
Ex, likes(harry,school).
Goal: likes(harry,X).
67. Rules for Unification (3)
3. A free variable will unify with any other free
variable. After unifying, the two variable will act as
one. If one of the variable becomes bound, the other
will be bound to the same value.
Ex,
likes(ron,broom).
likes(harry,X):-likes(ron,X).
Goal: likes(harry,broom).
68. Instantiation & Unification Exercise
Unify With Result
Likes(jem,piano) Likes(jem,X) X=piano
yes
Likes(jem,X) Likes(Y,piano) Y=jem
yes
69. Unifiability : Binding, substitution and unifiers
Testing equality of terms with variables:
Terms T1 and T2 are unifiable if there is a substitution that makes them
equal!
A binding is an association of a variable to a term .
Two sample bindings: Name mueller and MM 11
A substitution is a set of bindings
A sample substitution: {Name mueller, MM 11 }
A unifier is a substitution that makes two terms equal
The above substitution is a unifier for the two person/3 terms above
70. The ‘not’ predicate
To explicitly express that in the database particular
fact is not true.
To try this use built-in ‘not’ predicate.
The ‘not’ predicate cannot be used to express a fact
or appear in the head of a rule. It can only be used in
a premises.
Ex, replace(right_speaker) :-
not(is(right_speaker,functional).
In the case if is(right_speaker,functional) is in
database the rule will fail.
71. ‘not’ predicate
Be careful in your program design to distinguish
between the use of the not predicate and the
omission of facts from the databse.
If a fact is not in the database, prolog considers it
false.
However this falsity based on absence from the
database is not sufficient to prove a not premise.
The absence of the is fact above would not prove the
not premise in the rule.
72. Which are the connectors available that connects
the clauses
implication (:-)
conjunction (,)
disjunction (;)
73. Scope of variable in prolog
The scope of a variable is the clause in which it
appears