Uploaded by Aliphonza Nderitu

logic programming lesson 1

advertisement
BIT 444E
LOGIC PROGRAMMING
WEEK 1
Lecturer: Mr Samuel maina
Learning topic
An Overview of Prolog
 An example program: defining family relations
 Extending the example program by rules
 A recursive rule definition
 How Prolog answers questions
 Declarative and procedural meaning of programs
An Overview of Prolog Evolution history
What is logic? logic is concerned with the forms of reasoning.







the process of reasoning one makes inferences
The most reliable types of inferences are deductive inferences, in which
the conclusion must be true if the premises are.
Assuming that the postulates/Axiom (must be true without prove) are
true, we prove that other statements, such as the Pythagorean Theorem,
must also be true through maths proof. Geometric proofs, and other
mathematical proofs, typically use many deductive inferences.
The study of logic is essential for students of computer science. It is also
very valuable for mathematics students, and others who make use of
mathematical proofs, for instance, linguistics students.
Logic programming using Prolog language. "Programming in Logic"
Prolog Originally designed for natural language processing and theorem
proving
A prolog allows us to make use of Facts and Rules in order to establish the
relationships between objects in a system.
Whats the point??! Of Learning Prolog.
-
Prolog is predominantly used in AI, however there have also been
incidents of Prolog being used to prototype Logic, and forming the basis
of insurance companies calculating the risks and ultimately premiums
to be paid.
What is logic programing?
 Logic Programming is a method that computer scientists are using to try to
allow machines to reason because it is useful for knowledge representation.
In logic programming, logic is used to represent knowledge(group of information
regarding any discipline i.e knowledge based regarding construction ) and inference(a
conclusion reached on the basis of evidence and reasoning.) is used to manipulate it.
There are primarily two computer languages used in artificial intelligence work,
LISP and PROLOG. LISP, which is short for List Processing, was created by John
McCarthy of Stanford University. It looks klutzy but it is based upon the lamba
calculus and works quite well for computation associated with artificial
intelligence. PROLOG has an elegant formulation but it does not have the range
of application that LISP has. The Japanese when they formulated the Fifth
Generation project chose PROLOG over LISP as the programming language. This
was perhaps one of the factors that contributed to the failure of the Fifth
Generation project. Nevertheless, PROLOG is worth knowing for its power in
solving questions about relationships.
Prolog evolved out of research at the University of Aix-Marseille back in the late 60's and early
70's. Alain Colmerauer and Phillipe Roussel, both of University of Aix-Marseille, colaborated
with Robert Kowalski of the University of Edinburgh to create the underlying design of Prolog
as we know it today. Kowalski contributed the theoretical framework on which Prolog is
founded while Colmerauer's research at that time provided means to formalize the Prolog
language.
1972 is referred to by most sources as the birthdate of Prolog. Since its birth it has branched off
in many different dialects. Two of the main dialects of Prolog stem from the two Universities of
its origin: Edinburgh and Aix-Marseille. At this time the first Prolog interpreter was built by
Roussel. The first Prolog compiler was credited to David Warren, an expert on Artificial
Intelligence at the University of Edinburgh.
To this day Prolog has grown in use throughout North America and Europe. Prolog was used
heavily in the European Esprit programme and in Japan where it was used in building the
ICOT Fifth Generation Computer Systems Initiative. The Japanese Government developed this
project in an attempt to create intelligent computers. Prolog was a main player in these
historical computing endeavours.
Prolog became even more pervasive when Borland's Turbo Prolog was released in the 1980's.
The language has continued to develop and be used by many scientists and industry experts.
Now there is even and ISO Prolog standardisation (1995) where all of its individual parts have
been defined to ensure that the core of the language remains fixed.

declarative programming language. This means that, when you implement the
solution to a problem, instead of specifying how to achieve a certain goal in a
certain situation, you specify what the situation (rules and facts) and the goal
(query) are and let the Prolog interpreter derive the solution for you. Prolog is
particularly useful for certain problem solving tasks in AI, in domains such as
search, planning, and knowledge representation.
Some Applications of Prolog
Prolog is used in various domains. It plays a vital role in automation system. Following
are some other important fields where Prolog is used −

Intelligent Database Retrieval

Natural Language Understanding

Specification Language

Machine Learning

Robot Planning

Automation System

Problem Solving
Prolog - Basics
Knowledge Base − This is one of the fundamental parts of Logic Programming. We will see in
detail about the Knowledge Base, and how it helps in logic programming.
Facts, Rules and Queries − These are the building blocks of logic programming. We will get
some detailed knowledge about facts and rules, and also see some kind of queries that will be
used in logic programming.
Relations in prolog

Rules are expressed in Prolog using relations (a.k.a. predicates) -- evaluates true or false.
Prolog programs specify relationships among objects and properties of objects.




When we say “Tom has a Bike” we are declaring the ownership
relationship between two objects: Tom and Bike
When we ask “Does Tom owns a Bike?” then we are trying to find
out about a relationship.
Relationships can also rule such as:
Two people are brothers IF
They are both male and
They have the same parents.
A rule allows us to find out a relationship even if the relationship
isn’t explicitly stated as a fact.
Prolog – programming Building blocks( Facts, Rules, Queries )
Clauses
Prolog programs are made up of facts and rules. Facts and rules are called clauses
A clause has a head(Fact) and a body (Rule)
–
1. Facts
Facts are properties of objects or relationships between objects:
A fact is a predicate followed by a dot. And predicate is the name given to the word
occurring before the bracket in a fact or rule.
Example: bigger (cat, rat).


It can be read as cat is bigger than rat.
The intuitive meaning of a fact is that, we define a certain instance of
relation
It should be noted that:





Names of the 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 lowercase letters. they also can begin with digits like (1234)
Facts example
In a Prolog program, a presence of a fact indicates a statement that is true. An absence of
a fact indicates a statement that is not true. See the following example:
2. Rules
 A rule can be viewed as an extension of a fact with added conditions that also
have to be satisfied for it to be true.
 NB: Rules It consists of two parts.
-
The first part is similar to a fact (a predicate with arguments).
The second part consists of other clauses (facts or rules which are
separated by commas) which must all be true for the rule itself to be true.
These two parts are separated by ":-". You may interpret this operator as
"if" in English
Some syntax
i. First part similar to a fact.
(i)
Facts
(ii) Rules
Interpreting prolog program.



We start with facts then Rules
- dan likes sally
- sally likes dan
- josh likes Brittney
second part how to write rules from fact
- there exist a friendship between (X,Y) IF X likes Y and Y likes X.
- example
we make a Query of sally and dan will be true why? The friendship will
hold true because sally likes dan .
the if we change the syntax of likes(X,Y) from likes(X,Y) ; to likes( X,Y), “änd”
likes(Y,X).
then the results will be false when we Query josh, Brittney because both rules
must hold equal.
3. Goal or Query
Queries will be based on facts and rules. We can ask questions based on the stored
information.
Prolog installation and writing Prolog codes
Direct downlink for Prolog
http://www.gprolog.org/setup-gprolog-1.4.5-mingw-x64.exe(64 Bit System)
After installing open the swi-prolog console
Follow the video link below to learn how to install and code your first FACT Prolog
basics
http://youtube.com/watch?v=v1K9AnkAeM&list=PLEJXowNB4kPy3_qhGksOO8ch_Di7T8_9E&index=5
title 5-first Prolog code
practice the Exercise provided in the video link
your interface results should look like this.
Family Relationship in Prolog
An example program: defining family relations in programming in PrologA Prolog is a programming language for symbolic, non- numeric computation. It is
specially well suited for solving problems that involve objects and relations between
objects.
Family Relation Rules Using Prolog
a. Write a Prolog program to find the Rules for
- Parents, son, daughter, sibling
b. Given the facts about.
-male, female.
c. predicates (objects relations)
-son (X, Y), male (x), Parent (Y, X).
- daughter (X, Y), female (X), parent (Y, X).
-sibling (X, Y), parent (Z, X), parent (Z, Y), X/=Y.
Family tree fig
-we create knowledge base = facts + rules
Using Prolog, we create facts and rules. = knowledgebase.
Male(rahim).
Male(karim).
Male(kabir).
Male(roy).
Female(tisa).
Female(jesi).
Parent (Rahim, karim).
Parent (Rahim, kabir).
Parent (Karim, tisa).
Parent (Karim, jesi).
Parent (Kabir, roy).
Rule: son (X, Y): - male(X), parent (Y, X).
daughter (X, Y): - female(X), parent (Y, X).
sibling (X, Y): - parent (Z, X), parent (Z, Y), X/Y.
General Questions from our family tree. Using Prolog.
A recursive rule definition
Def 1: Recursion is a technique in which one predicate uses itself/calls its self (may be
with some other predicates) to find the truth value
Def 2: Recursion is the process of defining a problem (or the solution to a problem) in
terms of (a simpler version of) itself.
Let us add one more relation to our family program, the predecessor relation.
This relation will be defined in terms of the parent relation.
The whole definition can be expressed with two rules.
1.
direct(immediate) predecessors
2. indirect predecessors.
We say that some X is an indirect predecessor of some Z if. there is a parentship
chain of people between X and Z, as illustrated in Figure 1 . 5 .
In our example of
Figure 1.1, Tom is a direct predecessor of Liz. and an indirect predecessor of
Pat
Figure 1.5 Examples of the predecessor relation: (a) X is a direct predecessor of Z; (b)
X is an indirect predecessor of. Z.
The first rule is simple and can be formulated as:
For all X and Z,
X is a predecessor of. Z if
X is a parent of Z.
This is straightforwardly translated into Prolog as:
predecessor (X, Z): - parent (X, Z).
The second rule
on the other hand, is more complicated because the chain of parents may present
some problems.
PERSONAL REVISION SUB –TOPIC

Declarative and procedural meaning of programs
Download