Prolog
Prolog
It is a logical and
declarative programming language.
Logic Programming is one of the Computer Programming Paradigm, in which the program statements
express the facts and rules about different problems within a system of formal logic. Here, the rules are
written in the form of logical clauses, where head and body are present.
For example, H is head and B1, B2, B3 are the elements of the body. Now if we state that “H is true, when
B1, B2, B3 all are true”, this is a rule. On the other hand, facts are like the rules, but without any body.
So, an example of fact is “H is true”.
Some logic programming languages like Datalog or ASP (Answer Set Programming) are known as purely
declarative languages. These languages allow statements about what the program should accomplish.
There is no such step-by-step instruction on how to perform the task. However, other languages like
Prolog, have declarative and also imperative properties. This may also include procedural statements like
“To solve the problem H, perform B1, B2 and B3”.
From this illustration, we can see that in Functional Programming, we have to define the procedures,
and the rule how the procedures work. These procedures work step by step to solve one specific problem
based on the algorithm. On the other hand, for the Logic Programming, we will provide knowledge base.
Using this knowledge base, the machine can find answers to the given questions, which is totally different
from functional programming.
In functional programming, we have to mention how one problem can be solved, but in logic
programming we have to specify for which problem we actually want the solution. Then the logic
programming automatically finds a suitable solution that will help us solve that specific problem.
Functional Programming Logic Programming
Functional Programming follows the Von-Neumann Logic Programming uses abstract model, or
Architecture, or uses the sequential steps. deals with objects and their relationships.
The syntax is actually the sequence of statements The syntax is basically the logic formulae (Horn
like (a, s, I). Clauses).
The computation takes part by executing the It computes by deducting the clauses.
statements sequentially.
Logic and controls are mixed together. Logics and controls can be separated.
What is Prolog?
Prolog or PROgramming in LOGics is a logical and declarative programming language. It is one major
example of the fourth generation language that supports the declarative programming paradigm. This is
particularly suitable for programs that involve symbolic or non-numeric computation. This is the main
reason to use Prolog as the programming language in Artificial Intelligence, where symbol
manipulation and inference manipulation are the fundamental tasks.
In Prolog, we need not mention the way how one problem can be solved, we just need to mention what
the problem is, so that Prolog automatically solves it. However, in Prolog we are supposed to give clues
as the solution method.
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.
Facts:
We can define fact as an explicit relationship between objects, and properties these objects might have.
So facts are unconditionally true in nature.
Suppose we have some facts as given below −
• Tom is a cat
• Kunal loves to eat Pasta
• Hair is black
• Nawaz loves to play games
• Pratyusha is lazy.
So these are some facts, that are unconditionally true. These are actually statements, that we have to
consider as true.
Following are some guidelines to write facts −
1. Names of properties/relationships begin with lower case letters.
2. The relationship name appears as the first term.
3. Objects appear as comma-separated arguments within parentheses.
Syntax
The syntax for facts is as follows −
relation(object1,object2...).
Example
Following is an example of the above concept −
1. cat(tom).
2. loves_to_eat(kunal,pasta).
3. of_color(hair,black).
4. loves_to_play_games(nawaz).
5. lazy(pratyusha).
Rules
We can define rule as an implicit relationship between objects. So facts are conditionally true. So when
one associated condition is true, then the predicate is also true. Suppose we have some rules as given
below −
• Lili is happy if she dances.
• Tom is hungry if he is searching for food.
• Jack and Bili are friends if both of them love to play cricket.
• will go to play if school is closed, and he is free.
So these are some rules that are conditionally true, so when the right hand side is true, then the left hand
side is also true.
Here the symbol ( :- ) will be pronounced as “If”, or “is implied by”. This is also known as neck symbol,
the LHS of this symbol is called the Head, and right hand side is called Body. Here we can use comma
(,) which is known as conjunction, and we can also use semicolon, that is known as disjunction.
Syntax
rule_name(object1, object2, ...) :- fact/rule(object1, object2, ...)
Suppose a clause is like :
P :- Q;R.
This can also be written as
P :- Q.
P :- R.
Is understood as
P :- (Q,R);(S,T,U).
Or can also be written as:
P :- Q,R.
P :- S,T,U.
Example
1. happy(lili) :- dances(lili).
2. hungry(tom) :- search_for_food(tom).
3. friends(jack, bili) :- lovesCricket(jack), lovesCricket(bili).
4. goToPlay(ryan) :- isClosed(school), free(ryan).
Queries
Queries are some questions on the relationships between objects and object properties. So question can
be anything, as given below −
• Is tom a cat?
• Does Kunal love to eat pasta?
• Is Lili happy?
• Will Ryan go to play?
So according to these queries, Logic programming language can find the answer and return them.
We know there are three main components in logic programming − Facts, Rules and Queries. Among
these three if we collect the facts and rules as a whole then that forms a Knowledge Base. So we can say
that the knowledge base is a collection of facts and rules.
Now, we will see how to write some knowledge bases. Suppose we have our very first knowledge base
called KB1. Here in the KB1, we have some facts. The facts are used to state things, that are
unconditionally true of the domain of interest.
Knowledge Base 1
Suppose we have some knowledge, that Priya, Tiyasha, and Jaya are three girls, among them, Priya can
cook. Let’s try to write these facts in a more generic way as shown below −
girl(priya).
girl(tiyasha).
girl(jaya).
can_cook(priya).
Note − Here we have written the name in lowercase letters, because in Prolog, a string starting with
uppercase letter indicates a variable.
Now we can use this knowledge base by posing some queries. “Is priya a girl?”, it will reply “yes”, “is
jamini a girl?” then it will answer “No”, because it does not know who jamini is. Our next question is
“Can Priya cook?”, it will say “yes”, but if we ask the same question for Jaya, it will say “No”.
Knowledge Base 2
Rules contain some information that are conditionally true about the domain of interest. Suppose our
knowledge base is as follows −
sing_a_song(ananya).
listens_to_music(rohit).
listens_to_music(ananya) :- sing_a_song(ananya).
happy(ananya) :- sing_a_song(ananya).
happy(rohit) :- listens_to_music(rohit).
playes_guitar(rohit) :- listens_to_music(rohit).
So there are some facts and rules given above. The first two are facts, but the rest are rules. As we know
that Ananya sings a song, this implies she also listens to music. So if we ask “Does Ananya listen to
music?”, the answer will be true. Similarly, “is Rohit happy?”, this will also be true because he listens to
music. But if our question is “does Ananya play guitar?”, then according to the knowledge base, it will
say “No”. So these are some examples of queries based on this Knowledge base.