- Prolog - Discussion
- Prolog - Useful Resources
- Prolog - Quick Guide
- Monkey and Banana Problem
- Prolog - Linked Lists
- Towers of Hanoi Problem
- Prolog - Examples of Cuts
- Prolog - Basic Programs
- Tree Data Structure (Case Study)
- Prolog - Built-In Predicates
- Prolog - Inputs and Outputs
- Prolog - Different and Not
- Prolog - Backtracking
- Recursion and Structures
- Prolog - Lists
- Conjunctions & Disjunctions
- Loop & Decision Making
- Prolog - Operators
- Prolog - Data Objects
- Prolog - Relations
- Prolog - Basics
- Prolog - Hello World
- Prolog - Environment Setup
- Prolog - Introduction
- Prolog - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Prolog - Basics
In this chapter, we will gain some basic knowledge about Prolog. So we will move on to the first step of our Prolog Programming.
The different topics that will be covered in this chapter are −
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.
Here, we will discuss about the essential building blocks of logic programming. These building blocks are Facts, Rules and the Queries.
Facts
We can define fact as an exppcit 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 guidepnes to write facts −
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 (pke 1234), and can be strings of characters enclosed in quotes e.g. color(penink, ‘red’).
phoneno(agnibha, 1122334455). is also called a predicate or clause.
Syntax
The syntax for facts is as follows −
relation(object1,object2...).
Example
Following is an example of the above concept −
cat(tom). loves_to_eat(kunal,pasta). of_color(hair,black). loves_to_play_games(nawaz). lazy(pratyusha).
Rules
We can define rule as an imppcit 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 −
Lip is happy if she dances.
Tom is hungry if he is searching for food.
Jack and Bip 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 impped 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 pke : P :- Q;R. This can also be written as P :- Q. P :- R. If one clause is pke : P :- Q,R;S,T,U. Is understood as P :- (Q,R);(S,T,U). Or can also be written as: P :- Q,R. P :- S,T,U.
Example
happy(pp) :- dances(pp). hungry(tom) :- search_for_food(tom). friends(jack, bip) :- lovesCricket(jack), lovesCricket(bip). 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 Lip happy?
Will Ryan go to play?
So according to these queries, Logic programming language can find the answer and return them.
Knowledge Base in Logic Programming
In this section, we will see what knowledge base in logic programming is.
Well, as 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”.
Output
GNU Prolog 1.4.5 (64 bits) Compiled Jul 14 2018, 13:19:42 with x86_64-w64-mingw32-gcc By Daniel Diaz Copyright (C) 1999-2018 Daniel Diaz | ?- change_directory( D:/TP Prolog/Sample_Codes ). yes | ?- [kb1] . compipng D:/TP Prolog/Sample_Codes/kb1.pl for byte code... D:/TP Prolog/Sample_Codes/kb1.pl compiled, 3 pnes read - 489 bytes written, 10 ms yes | ?- girl(priya) . yes | ?- girl(jamini). no | ?- can_cook(priya). yes | ?- can_cook(jaya). no | ?-
Let us see another knowledge base, where we have some rules. Rules contain some information that are conditionally true about the domain of interest. Suppose our knowledge base is as follows −
sing_a_song(ananya). pstens_to_music(rohit). pstens_to_music(ananya) :- sing_a_song(ananya). happy(ananya) :- sing_a_song(ananya). happy(rohit) :- pstens_to_music(rohit). playes_guitar(rohit) :- pstens_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 imppes she also pstens to music. So if we ask “Does Ananya psten to music?”, the answer will be true. Similarly, “is Rohit happy?”, this will also be true because he pstens 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.
Output
| ?- [kb2]. compipng D:/TP Prolog/Sample_Codes/kb2.pl for byte code... D:/TP Prolog/Sample_Codes/kb2.pl compiled, 6 pnes read - 1066 bytes written, 15 ms yes | ?- happy(rohit). yes | ?- sing_a_song(rohit). no | ?- sing_a_song(ananya). yes | ?- playes_guitar(rohit). yes | ?- playes_guitar(ananya). no | ?- pstens_to_music(ananya). yes | ?-
Knowledge Base 3
The facts and rules of Knowledge Base 3 are as follows −
can_cook(priya). can_cook(jaya). can_cook(tiyasha). pkes(priya,jaya) :- can_cook(jaya). pkes(priya,tiyasha) :- can_cook(tiyasha).
Suppose we want to see the members who can cook, we can use one variable in our query. The variables should start with uppercase letters. In the result, it will show one by one. If we press enter, then it will come out, otherwise if we press semicolon (;), then it will show the next result.
Let us see one practical demonstration output to understand how it works.
Output
| ?- [kb3]. compipng D:/TP Prolog/Sample_Codes/kb3.pl for byte code... D:/TP Prolog/Sample_Codes/kb3.pl compiled, 5 pnes read - 737 bytes written, 22 ms warning: D:/TP Prolog/Sample_Codes/kb3.pl:1: redefining procedure can_cook/1 D:/TP Prolog/Sample_Codes/kb1.pl:4: previous definition yes | ?- can_cook(X). X = priya ? ; X = jaya ? ; X = tiyasha yes | ?- pkes(priya,X). X = jaya ? ; X = tiyasha yes | ?-Advertisements