How to do Logic Programming With PROLOG

In this Blog, I am going to provide a short introduction to the language and its essential features and After reading the blog, you should be able to write simple programs in Prolog and understand the language’s underlying primary principles.

What is Prolog?

Prolog is a high-level programming language based on the formal logic used in artificial intelligence work and it is a declarative programming language.The general idea behind declarative languages is that you describe a situation. First of all, we know the full form of Prolog.

Prolog = Programmation en Logique (Programming in Logic).

It is the most useful in the area related to Artificial Intelligence, research and computational linguistics and based on Horn clauses (a subset of first-order logic) and probably the most famous language in the logic programming family.

Applications of Prolog

Some applications of Prolog are:

The intelligent database retrieval

Natural language understanding

Expert systems

A specification language

Machine learning

Robot planning

Automated reasoning

Problem-solving

Theorem proving

A PROLOG program consists of:

Declaration of the facts of the relations involved.

Declaration of rules concerning relations

Formulation of questions to be answered.

Specifying Relationships

Relations can be defined in several different ways.

Relations

Prolog programs specify relationships among objects and also specify the properties of objects.
When we say, “Marry owns the pen”, we are declaring the ownership relationship between two objects: Marry and the pen.
And when we ask, “Does Marry own the pencil?” we are trying to find out about a relationship.
Relationships can also rule such as:

Two people are the brother if they are both male and they have the same parents.

In programming, you need to be careful how you phrase things:
The following would be better:

U and V are brothers if both are male and both have same father and same mother and U is not the same as V

So in the above example, we define the relationship between brothers and the properties of brother object.

Programming Facts, Rules and Queries

Programming in Prolog

In Prolog program we declare facts describing explicit relationship between objects and properties objects might have(e.g. Ram like ice-cream, Hair is black, Honda is a company, Mini is a cat, John teaches Jolly)

We define rules defining implicit relationship between objects (e.g. brother relationship) and/or rules defining implicit objects properties(e.g. A is a child of B if B is parent of A)

One then uses the system to generate queries by asking questions about relationships between objects, and/or about object properties (e.g. does John like ice-cream? and e.g. Rathin is the parent of whom?)

Facts

Facts are properties of objects or relationships between objects;

“John has phone number 11119997543” and is written in Prolog as:

Phoneno(John,11119997543)

“Paris is the capital of France” and is written in Prolog as:

capital(Paris, France)

and it is also called a predicate or clause

It should be noted that:

* Names of properties/relationships begin with lower case letters.

* The relationship name appears as the first term.

* Objects more ap as comma-separated arguments within parentheses.

* A period “.” must end a fact.

* Objects also begin with lower case letters and digits (like 123456) and can be the string of charters enclosed in quotes e.g.

Together, these facts will form Prolog’s database.So we can easily create a database.

Rules

Consider the following case which produces a general rule-e.g.* One teacher will guide a student if that student studied that very course id on which the teacher teaches* In prolog this will be written as:

guide(Teacher,Courseid)studies(Student,Courseid).

* Facts are unit clauses.

* Rules are non-unit clauses.* Variable names will start with a capital letter.

Goal or Query

Queries will be based on facts and rules.We can ask questions based on the stored information.

Suppose we want to know if Sudhir lectures in course001 or not then we can ask:

?-teaches(Sudhir,course001).

Output:- Yes

In GNU Prolog, queries are terminated by a full stop.

To answer this query, Prolog consults its database to see if this is a known fact or not.

We can also ask-

?-teaches(Sudhir, X).

Output:- X=course001

Hence the output comes course001.

if an answer is true or yes, then the query succeeded.

if an answer is false or no, then the query failed.

Syntax of a Clause

“:-” means “if” or “is implies by”. Also called the neck symbol.

The left-hand side of the neck is called the head.

The right-hand side of the neck is called the body.

The comma, “,” stands for and/ conjection.

The semicolon “;” stands for or /disjunction.

A program consists of a clause. these are three types: facts, rules, and questions. A procedure is a set of clause about the same relation.

Example of Clause writing-

e.g.

P:-Q; R.

it can also be written as

P:-Q.P:-R.

e.g.

P:-Q, R; S, T, U.

it can also be written as

P:-(Q,R);(S,T,U).

and Can also be written as

P:-Q, R.P: -S, T, U.

How does a Prolog program execute?

Input/Output view of the procedure that executes a list of goals

Declarative Semantics and Procedural Semantics-

The Declarative Semantics of Prolog defines whether a goal is true or not with respect to a given program and if it is true, for what instantiation of variables is true.

The procedural Semantics is satisfying a list of goals in the context of a given program. It outputs the truth or falsity of the goal list and the corresponding instantiations of variables and automatically backtracks to examine Substitute.

Simple Program to find Factorial of a number in Prolog:

fact(0,1).fact(N,R):-N>0,S is N-1,fact(S,T),R is N*T.

Input:- ?-fact(3,R).

Output:- R=6

Hence the output comes 6.So you can easily write any program.

I have described the history of the development of Prolog. According to my opinion, the major factors in its success are partly technical (especially stability, portability and a wide coverage of features), partly organizational and partly timing.