Sunday 3 February 2019

Basic constructs in Prolog


There are three basic constructs in prolog programming.
a.   Facts
b.   Rules
c.    Queries

Facts
We need to specify the facts about the objects and their relationships. Facts always starts with lowercase letter and ends with a dot. Fact name can consist of any letters, numbers and underscore.

For example, below all are facts
Sun rises in the east
Elephant is bigger than ant
A female can give birth to a baby

Syntax to define facts
factName(<argument1>,<argument2>,....,<argumentN> ).

Example
bigger(elephant, ant).

a.   In the above snippet, elephant, ant are objects and bigger is the relationgship between the objects. Relationship is written first followed by objects separated by comma.
b.   As per the convention, all the relationships and objects must start with lowercase letter.
c.    The ‘.’ Operator is used to the end the fact, you can assume it as statement terminator.

A fact can take zero or more arguments.
valuable(diamond): Diamond is valuable
animals(lion, elephant, ant, dog): lion, elephant, ant and dog are animals.
borrow(krishna, 1000, chamu): krishna borrows 1000 from chamu.
mother(rama, krishna): rama is mother of Krishna

Whenever you load the facts, these are loaded into Prolog database, you can query these facts, prolog return true, if the fact exists in database, else error is thrown.

facts.pl
sun_rises_in_the_east.
it_is_raining_today.

valuable(diamond).
animals(lion, elephant, ant, dog).
borrow(krishna, 1000, chamu).

1 ?- consult(facts).
true.

2 ?- sun_rises_in_the_east.
true.

3 ?- valuable(diamond).
true.

4 ?- it_is_not_raining_today.
ERROR: Undefined procedure: it_is_not_raining_today/0 (DWIM could not correct goal)
5 ?- valuable(1, 2).
ERROR: Undefined procedure: valuable/2
ERROR:     However, there are definitions for:
ERROR:         valuable/1
false.

‘consult’ statement is used to load the prolog facts and rules to the database.

What are arguments
Objects that are placed in the brackets of each fact are called arguments.

For example, diamond, lion, elephant, dog, Krishna, 1000, chamu all are the arguments.

What is predicate?
Name that comes before the brackets of the fact is called predicate.

For example, valuable, animals, borrow and mother are predicates.

What is a database?
Collection of facts is called a database.

Rules
It defines the relationship between objects.

For example, a rule can be something like below.
a.   Person ‘x’ is taller than person ‘y’, if person x’s height is more than person y’s height.
b.   A person is eligible to vote, if he is more than 18 years old
c.    Two persons are brothers, if they are male and their parents are same
d.   I will go for a trek, if there is no rain

Queries
Once we define the facts and rules, we can ask prolog system about something.

For example, we can ask prolog system ‘Is Donkey bigger than Elephant?’, Prolog return either true or false depending on what facts and rules we fed to prolog system.

Technically, we should put special symbol ? to query the prolog system

Syntax
?-askSomething.

Example
?-bigger(elephant, ant).


Above statement asks prolog, is elephant bigger than ant? Prolog gives you the answer yes or no, depending on the facts it had.



Previous                                                 Next                                                 Home

No comments:

Post a Comment