0% found this document useful (0 votes)
10 views16 pages

Prolog Lab I

Uploaded by

meselalinfo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Prolog Lab I

Uploaded by

meselalinfo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Prolog

Computer Engineering Dep’t


2008/ 2015
HIGHLIGHT ON PROLOG
• Invented early seventies by Alain Colmerauer in
France and Robert Kowalski in Britain
• Prolog = Programmation en Logique
(Programming in Logic).
• Prolog is a declarative programming language
unlike most common programming languages. In
a declarative language the programmer specifies
a goal to be achieved and the Prolog system
works out how to achieve it
• Relational databases owe something to Prolog
• Procedural programmer must specify in detail
how to solve a problem:
mix ingredients;
beat until smooth;
bake for 20 minutes in a moderate oven;
remove tin from oven;
put on bench;
close oven;
turn off oven;
• In purely declarative languages, the
programmer only states what the problem is
and leaves the rest to the language system.
APPLICATIONS OF PROLOG
Some applications of Prolog are:
• intelligent data base retrieval
• natural language understanding
• expert systems
• specification language
• machine learning
• robot planning
• automated reasoning
• problem solving
RELATIONS
• Prolog programs specify relationships among objects and
properties of objects.
• When we say, "John owns the book", we are declaring the
ownership relationship between two objects: John and the
book.
• When we ask, "Does John own the book?" we are trying to
find out about a relationship.
• Relationships can also rules such as:
Two people are sisters, if they are both female and
they have the same parents.
• A rule allows us to find out about a relationship even if the
relationship isn't explicitly stated as a fact.
Continued…
• As usual in programming, you need to be a bit
careful how you phrase things:
• The following would be better:
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
Programming in Prolog
Declare facts describing explicit relationships between
objects and properties objects might have
• Example: Mary likes pizza, grass has_colour green, Mr.
chu taught Paul Japanese
Define rules defining implicit relationships between objects
• Example: The sister rule above and/or rules defining
implicit object properties such as X is a parent if there is a
Y such that Y is a child of X.
Then uses the system by:
Asking questions above relationships between objects,
and/or about object properties
• Example: Does Mary like pizza? is Joe a parent?
FACTS
• What is already known.
• Axiom about some object and their relationship.
Example: bonny is fat. // what is fat? = fat(bonny).
• Properties of objects, or relationships between objects
• "Dr Turing lectures in course 9020", is written in Prolog as: lectures(turing,
9020).
• Notice that:
– names of properties/relationships begin with lower case letters.
– the relationship name appears as the first term
– objects appear as comma-separated arguments within parentheses.
– A period "." must end a fact.
– objects also begin with lower case letters. They also can begin with digits (like
9020), and can be strings of characters enclosed in quotes (as in reads(fred, "War
and Peace")).
• lectures(turing, 9020). is also called a predicate
Facts about a hypothetical computer
science department:
• % lectures(X, Y): person X lectures in course Y
lectures(turing, 9020).
lectures(codd, 9311).
lectures(backus, 9021).
lectures(ritchie, 9201).
lectures(minsky, 9414).
lectures(codd, 9314).
Together, these facts form Prolog's database.
QUERIES
• Once we have a database of facts and rules, we can ask questions about
the stored information.
• Suppose we want to know if Turing lectures in course 9020. We can ask:

Notice that:
• In SWI Prolog, queries are terminated by a full stop.
• To answer this query, Prolog consults its database to see if this is a
known fact.
• In example dialogues with Prolog, the text in green italics is what the
user types.
Another example query
?- lectures(codd, 9020).
false.
• if answer is true., the query succeeded
• if answer is false., the query failed.
Note: Many early versions of Prolog, including early versions of SWI-
Prolog, say No instead of false. In the latest version of SWI Prolog, it
no longer says "No." but says "false." instead.
• The use of lower case for cod is critical.
• Prolog is not being intelligent about this - it would not see a
difference between this query and
lectures(fred, 9020). or lectures(xyzzy, 9020).
though a person inspecting the database can see that fred is a
student, not a lecturer, and that xyzzy is neither student nor
lecturer.
Variables
• Suppose we want to ask, "What course does Turing teach"?
• This could be written as: Is there a course, X, that Turing teaches?
• The variable X stands for an object that the questioner does not
know about yet.
• To answer the question, Prolog has to find out the value of X, if it
exists.
• As long as we do not know the value of a variable it is said to be
unbound.
• When a value is found, the variable is said to bound to that value.
• The name of a variable must begin with a capital letter or an
underscore character, "_".
Variables 2
• To ask Prolog to find the course that Turing teaches, enter this:
?- lectures(turing, Course).
Course = 9020 ← output from Prolog
• To ask which course(s) Prof. Cod teaches, we may ask,
?- lectures(codd , Course).
Course = 9311 ; ← type ";" to get next solution
Course = 9314
?-
• If Prolog can tell that there are no more solutions, it just gives you
the ?- prompt for a new query, as here. If Prolog can't tell, it will let
you type ; again, and then if there is no further solution, report false.
• Prolog can find all possible ways to answer a query, unless you
explicitly tell it not to.
Practice
• On text editor, write the following fact.
female(hanan).
female(aster).
female(ruth).
male(tom).
male(abdu).
male(zola).
mother(hanan,abdu).
mother(hanan,zola).
mother(hanan,aster).
mother(tsehay,ruth).
father(john,ruth).
father(tom,abdu).
father(tom,zola).
father(tom,aster).
parent(zola,tom,hanan).
Save it as anything.pl
Continued…
• Open SWI-Prolog -> File -> Edit [choose the .pl file] ->
Compile[Editor] -> Make [make done] -> On SWI-Prolog, File ->
Edit -> Consult.
• Query
1 ?- parents (zola,F,M).
F=tom,
M=hanan.
2 ?- father (tom,zola).
true
3 ?- father (abc,zola).
F=tom.
Rules
• Extend the fact about some object and their
relationship.
Example:
likes(john,suzie). :- If
likes(suzie,john).
likes(bob,ket). , And
dating(X,Y):-
likes(X,Y),
; Or
likes(X,Y).
Not not
friendship(X,Y).
likes(X,Y);
likes(Y,X).

You might also like