0% found this document useful (0 votes)
7 views18 pages

Introduction to Logic Programming in Prolog

Logic Programming is a programming paradigm that expresses problems as facts and rules within a formal logic system, with Prolog being a widely used language for this approach. Prolog allows users to query a database of facts and rules to derive conclusions, making it suitable for applications in artificial intelligence, natural language processing, and expert systems. The syntax involves defining facts and rules, which serve as the building blocks for solving problems, and queries can be made to retrieve information based on the established knowledge base.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Introduction to Logic Programming in Prolog

Logic Programming is a programming paradigm that expresses problems as facts and rules within a formal logic system, with Prolog being a widely used language for this approach. Prolog allows users to query a database of facts and rules to derive conclusions, making it suitable for applications in artificial intelligence, natural language processing, and expert systems. The syntax involves defining facts and rules, which serve as the building blocks for solving problems, and queries can be made to retrieve information based on the established knowledge base.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to logic

Programming
PROLOG
Logic Programming
• Logic Programming is a programming paradigm in which the problems
are expressed as facts and rules by program statements but within a
system of formal logic.
• Just like other programming paradigms like object oriented,
functional, declarative, and procedural, etc.,
• it is also a particular way to approach programming
How to Solve Problems with Logic Programming

• Logic Programming uses facts and rules for solving the problem.
• That is why they are called the building blocks of Logic Programming.
• A goal needs to be specified for every program in logic programming.
• To understand how a problem can be solved in logic programming,
we need to know about the building blocks − Facts and Rules
Facts and Rules
• Facts
Actually, every logic program needs facts to work with so that it can achieve the
given goal. Facts basically are true statements about the program and data. For
example, Delhi is the capital of India.
• Rules
Actually, rules are the constraints which allow us to make conclusions about
the problem domain. Rules basically written as logical clauses to express various facts.
For example, if we are building any game then all the rules must be defined.
Rules are very important to solve any problem in Logic Programming. Rules
are basically logical conclusion which can express the facts. Following is the syntax of
rule −
• A∶− B1,B2,...,Bn.
• Here, A is the head and B1, B2, ... Bn is the body.
Rule
• likes(bill, Activity):- likes(tom, Activity).

This rule corresponds to the natural language statement

• Bill likes an activity if Tom likes that activity.

• In this rule, the head is likes(bill, Activity), and the body is likes(tom,
Activity). Notice that there is no fact in this example about Bill liking
baseball. For Prolog to discover if Bill likes baseball, you can give the
query
What is Prolog
• Prolog stands for programming in logic.
• In the logic programming paradigm, prolog language is most widely available.
• Prolog is a declarative language, which means that a program consists of data
based on the facts and rules (Logical relationship) rather than computing how
to find a solution.
• A logical relationship describes the relationships which hold for the given
application.
• To obtain the solution, the user asks a question rather than running a program.
• When a user asks a question, then to determine the answer, the run time
system searches through the database of facts and rules.
• The language was developed and implemented in Marseille, France, in 1972 by
Alain Colmerauer.
What is Prolog

• Prolog is used in some areas like database, natural language processing,


artificial intelligence, but it is pretty useless in some areas like a numerical
algorithm or instance graphics.
• In artificial intelligence applications, prolog is used. The artificial intelligence
applications can be automated reasoning systems, natural language
interfaces, and expert systems.
• The expert system consists of an interface engine and a database of facts.
The prolog's run time system provides the service of an interface engine.
Applications of Prolog
• Specification Language
• Robot Planning
• Natural language understanding
• Machine Learning
• Problem Solving
• Intelligent Database retrieval
• Expert System
• Automated Reasoning
Programming with PROLOG:

• PROLOG build a database of facts and develop knowledge base of


protocols/rules
• PROLOG is especially well suited for problems that involve objects and
relations between them
• PROLOG is declarative language it is generally used for theorem proving,
developing relational databases, natural language processing
• PROLOG files can have following file extensions
“ .pl ”
“.pro ”
“ . P”
• By default PROLOG uses “ .pl “ extension
The syntax of Prolog is as follows:
Symbols:
• Using the following truth-functional symbols, the Prolog expressions are
comprised. These symbols have the same interpretation as in the
predicate calculus.
Program Window:
Query Window
Facts and Rule with Example
• FACTS:
• Some facts about family relationships could be written as :
• Sister (neeta,rakesh) QUERIES :
• Parent (ramesh,om)
Given a database of facts
• Male (sam)
and rules such as that
• Female (riya)
above, we may make
• RULES : queries by typing after a
• To represent the general rule for grandfather, we write: symbol ‘?_’ such as:
• grandfather(X, Z) ?_ parent(X,om).
• Parent(X, Y) X=ramesh
• Parent(Y, Z) ?_sister(neeta,Y).
• Male(X) Y=rakesh
Queries
• In Prolog, the query is the action of asking the program about the
information which is available within its database. When a Prolog program is
loaded, we will get the query prompt,
“?-”
• After this, we can ask about the information to the run time system. Using
the above simple database, we can ask a question to the program like
?- 'It is sunny'.
• and it will give the answer
yes
?-
Queries
• The system responds to the query with yes if the database information is
consistent to answer the query. Using the available database information, we
can also check that the program is capable of proving the query true. No
indicates that the fact is not deducible based on the available information.
• The system answers no to the query if the database does not have sufficient
information.
?- 'It is cold’.
no
?-
Rules and Facts Example:
• Rules
Rules extend the logic program capabilities. Rules are used to provide the decision-
making process in Prolog. Rules are specified in the form:
head:- t1, t2, t3,….., tk. Where k>=1
The head is known as the clause of the head.
:- is known as the clause neck. It is read as 'if'. The body of the clause is specified by t1,
t2, t3,…, tk.
It contains one or more components, and it can be separated using the commas. A rule
will read as 'head is true if t1, t2, t3,…., tk are all true’.

• In the following program, first two lines indicate the facts and last two lines indicate the rules:
[Link](rottweiler). large(rottweiler).
[Link](siamese). large(siamese).
3.large_animal(A) :- dog(A),large(A).
4.large_animal(C) :- cat(C),large(C).
• The above rules mean that 'large_animal(A) is true if dog(A) is true, and large(A) is true, etc.'
• The last line means that 'large_animal(C) is true if cat(C) is true, and large(C) is true.

You might also like