3. 1.Introduction to Prolog :
Prolog stands for PROgramming in LOGic. It is a logic language that
is particularly used by programs that use non-numeric objects. For
this reason it is a frequently used language in Artificial
Intelligence where manipulation of symbols is a common task.
Prolog differs from the most common programming languages
because it is declarative language. Traditional programming
languages are said to be procedural. This means that the
programmer specify how to solve a problem. In declarative
languages the programmers only give the problem and the language
find himself how to solve the problem.
4. 1.Introduction to Prolog cont.:
Prolog program is not a sequence of actions , it include an
inference engine because it is a collection of facts together with
rules for drawing conclusion from those facts this is called (
pattern matching)so the programmer arrives at solutions by
logically inferring one thing from something already known .
Prolog is based on Predicate Logic which is a formal system for
reasoning about things and the way they relate to each other .
Prolog takes advantage of this syntax to develop a programming
language based on the logic .
5. 2.Sentences : Facts and Rules :
In predicate logic , you first eliminate all unnecessary words from your
sentences . You then transform the sentence, placing the relationship
first and grouping the objects after the relationship . The objects then
become arguments that the relationship acts upon .
A prolog programmer defines objects and relation , then defines rules abut
when these relations are true . For example, the sentence :
Ahmed likes cats
Shows a relation between the objects Ahmed and Cats , the relation is likes .
Here is a rule that defines when the sentence : Ahmed likes cats is true :
Ahmed likes cats if the cats are nice .
6. 2.1 Facts : What Is Known
In natural language, a relation between objects is symbolized by a sentence . In
prolog , a relation is called predicate which summarized in a simple phrase – a
fact- that consists of the relation name followed by the object or objects
enclosed in parentheses . As with a sentence, the fact ends with a period (.)
For example : the following sentences are transformed into predicate logic syntax :
Predicate Logic
Natural Language
Fun(car).
A car is fun.
Red(rosr).
A rose is red.
Red(car)
A car is red
Likes(Khalid , books).
Khalid likes books.
Likes(Ahmed,car) if fun(car)
Ahmed likes a car if the car is fun .
7. 2.2 Rules : What You Can Infer
from Given Facts :
Rules enable you to infer facts from other facts . Here are some rules
concerning (likes) relation :
Ahmed likes everything that is red .
Ali likes every things that Khalid likes .
Given this rule , you can infer from the previous facts some of the
things that Ahmed likes :
Ahmed likes rose .
Ahmed likes car .
Ali likes books .
8. 2.2 Rules : What You Can Infer
from Given Facts cont.:
To encode this same rules into prolog , you only need to change the
syntax a little, Like this :
Likes (ali , something) :- Likes (khalid , something).
Likes(ahmed , something) :- green(something).
The ( :- ) symbol simply pronounced ( if ) , and serves to separate the
two parts of a rule : the head and the body .
You can also think of rules as meaning " To prove that Ali likes
something , prove that Khalid likes that same thing " and " To prove
that Ahmed likes something , prove that it is green " .
A rule also can ask prolog to perform actions other than proving things
proving things such as : writing something or creating a file.
9. 3.Queries :
Once we give Prolog a set of facts, we can proceed to ask
questions concerning these facts , this is known as
querying the Prolog system .(psattern matching)
In natural language, we can ask you : Does Ahmed like car ?
In Prolog syntax, we can ask Prolog : likes(ahmed, car) . Given
this query, Prolog would answer : yes . because Prolog has a
fact that says so which is : likes(ahmed , car).
10. 3.Queries cont.:
As a little more complicated and general question, we could ask in natural language :
What does Ahmed like?
In Prolog syntax , We ask Prolog likes(ahmed , What). It is important to notice that
the second object ( What) begins with a capital letter , while the first object (ahmed)
does not . This is because (ahmed) is a constant object known as value but What is
variable . Variables always begins with upper-case letter or on underscore .
Prolog always looks for an answer to a query by starting at the top of the facts . It
looks at each fact until it reaches the bottom , where there are no more .
Given the query about what Ahmed likes . Prolog will return :
what = car
what = rose
2 solutions . This is because Prolog knows : likes (ahmed , car). And
likes(ahmed,rose)
11. Putting Facts, Rules and Queries
Together :
A fast car is fun .
A big car is nice .
A little car is practical .
Ahmed likes car if the car is fun
.
When we read this facts , we can deduce that
Ahmed likes a fast car . Prolog will come to the
same conclusion . If no fact were given about fast
cars , then you would not be able to logically
deduce what kind of a car Ahmed likes . We could
guess what kind of a car might be fun but Prolog
only knows what we tell it . Prolog does not guess .
12. 4. Variables : General
Sentences
In Prolog, variables enable us to write general facts and rules
and ask general questions .
For example : In natural language we can write general
sentence as : Ali likes the same thing as Khalid .
To represent that in Prolog , we can write :
likes ( ali , Thing) :- likes (khalid , Thing).
AS we mentioned earlier to represent variables in Prolog the
first character should be an upper-case letter . in our
example ( Thing) is the variable and it must be able to
match any thing that Khalid likes .
The objects : ali and Khalid begins with lower-case letter
because they are symbols having a constant value .
Visual Prolog can also handle text string if the text
surrounded by double quotes . So the name ali could have
been written as "Ali" , if you wanted it to begin with an
upper-case letter .
13. 5. Overview :
1- A Prolog program is made up of tow types of phrases ( also known as clause) : facts and
rules .
Facts : are relations or properties that the programmer know to be true .
Rules : are dependent relations , they allow Prolog to infer one piece of information from
another . A rule become true if a given set of conditions is proven to be true . Each rule
depends upon proving its conditions to be true .
2- In Prolog , all rules have two parts , a head and a body separated by the special :- token .
The head is the fact that would be true if some number of conditions were true . The body
is the set of conditions that must be true so that prolog can prove that the head of the
rule is true .
3- Facts and rules are really the same , except that a fact has no explicit body. The fact
simply behaves as if it had a body that was always true .
4- Once you give Prolog a set of facts and rules, you can proceed to ask questions
concerning these . this is known as querying the prolog system .
Prolog always looks for a solution by starting at the top of the facts and/or rules , and
keeps looking until it reaches the bottom .
5- Prolog's inference engine take the condition of a rule ( the body of the rule ) and looks
through its list of known facts and rules, trying to satisfy the conditions . Once all the
conditions have been met , the dependent relation (the head of the rule ) is found to be
true . If all condition can not be matched with known facts , the rule doesn't conclude
anything .
14. Syntax for Predicate Calculus
Programming:
Prolog
Predicate Calculus
English
,
˄
and
;
˅
or
:-
←
only if
not
¬
not
Example:
George likes kate and George likes Susie.
likes (george,kate),likes(george,susie).
George likes kate or George likes Susie.
likes (george,kate); likes(george,susie).
George likes Susie if George does not likes kate.
likes (george, susie):- not(likes(george, kate)).
15. Example:
A simple description of a blue bird might be a
blue bird is a small blue colored bird and a bird
is a feathered flying vertebrate.
This may be represented as a set of logical
predicates:
hassize(bluebird, small)
hascovering(bird, feather)
hascolor(bluebird, blue)
hasproperty(bird, flies)
is a(bluebird,bird)
is a(bird, vertebrate)
16. Comments:
It's good programming style to include comments in your program
to explain things that might not be obvious to someone else. This
makes the program easy for you and others to understand .
Multiple line comments must begin with the character /*(slash,
asterisk) and end with the characters */ (asterisk, slash )
To set off single line comments you can use these same
characters, or you can begin the comments with a percent sign
(%)
17. the domains section:
The domain section serves two very useful purpose. First, you can give
meaningful names to domains even if, internally, they are the same as
domains that already exist. Second, special domain declarations are used to
declare data structures that are not defined by the standard domains.
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.
Here's an example to illustrate how declaring domains helps to document your
predicates:
Ahmad is a male who is 45 years old.
With the pre-defined domains, you come up with the following predicate
declaration :
Person(symbol, symbol, integer)
The declaration will work fine for most purpose.
18. The following decoration will help you understand what
the argument in the predicate declaration stand for:
DOMAINS
name , sex = symbol
age = integer
PREDICATES
person( name, sex, age)
One of the main advantages of this declarations is that visual prolog can catch the type
errors, like the following obvious mistake :
same_sex(X, Y):-
person(X, sex, _ ),
person(sex, Y,_ ).
Even though name and sex are both defined as symbol, they are not equivalent to each
other. This enables visual prolog to detect an error if you accidentally swap them. This
feature is very useful when your programs get large and complex.
You might be wondering why we don’t use special domains for all argument declarations,
since special domains communicate the meaning of the argument so much better. The
answer is that once an argument is typed to a specific domain, that domain can't be
mixed with another domain you have declared, even if the domains are the same. So,
even though name and sex are of the same domain (symbol), they can't be mixed.
However, all user-defined domains can be matched with the pre-defined domains.
19. The Goal Section:
The goal section is the same as the body of a rule
it's simply a list of subgoals. There are two
differences between the goal section and a rule:
The goal keyword is not followed by :-.
Visual Prolog automatically executes the goal when
the program runs.
It's as if Visual Prolog makes a call to goal and the
program runs, trying to satisfy the body of the
goal rule. If the subgoals in the goal section all
succeed, then the program terminate successfully.
If, while the program is running, a subgoal in the
goal section fails, then the program is said to have
failed.
22. Quires :
Run Run As Interpreted project (pro)
?- father(X,khaled).
X = ahmad ;
no
?-
mother(X,mhmma
d).
X = abeer ;
no
?- bother(X,abeer).
no
?-
?- brother(X,abeer).
X = khaled ;
X = salem ;
no
?- sister(X,salem).
X = abeer ;
no
?- sister(X,samir).
no
predecessor(abeer,X).
X = mhmmad ;
no
?-
predecessor(ahmad,X).
X = abeer ;
X = khaled ;
X = salem ;
X = mhmmad ;
X = adel ;
X = samir ;
X = abdlaziz ;
X = soad ;
no
?- quit.