0% found this document useful (0 votes)
5 views4 pages

Stud Exp1

Prolog is a logic programming language that focuses on describing relationships and facts rather than specifying algorithms. A Prolog program consists of clauses that represent facts and rules about objects and their relationships, allowing users to ask questions that the system answers by searching through a database of facts. The language utilizes predicates, variables, and conjunctions to express complex relationships and dependencies among facts.

Uploaded by

n56755392
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Stud Exp1

Prolog is a logic programming language that focuses on describing relationships and facts rather than specifying algorithms. A Prolog program consists of clauses that represent facts and rules about objects and their relationships, allowing users to ask questions that the system answers by searching through a database of facts. The language utilizes predicates, variables, and conjunctions to express complex relationships and dependencies among facts.

Uploaded by

n56755392
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

AIM: Program to represent simple fact for a statement.

PROLOG-PROGRAMMING IN LOGIC
PROLOG stands for Programming, In Logic — an idea that emerged in the early 1970’s to use logic as
programming language.
Prolog programming is not like specifying an algorithm.
Prolog programmer asks more about which formal relationships and objects occur in the problem, and
which relationships are “true” about the desired solution.
Prolog approach is more about describing known facts and relationships about a problem, and less about
prescribing the sequence of steps taken by a computer to solve a problem.
A Prolog program consists of a set of clauses, where each clause is either a fact about the given
information or a rule about how the solution may relate to or to be inferred from the given facts.
Computer programming in prolog consists of:
 Specifying some “facts” about objects and their relationships.
 Defining some “rules” about objects and their relationships, and
 Asking “questions” about objects and their relationships.
Prolog is a storehouse of facts and rules, and it uses the facts and rules to answer questions.
Facts: “John likes Mary”.
This fact consists of two objects, called “Mary” and “John”. A relationship called “likes”.
In prolog, facts can be written as
likes(john, mary).
 The names of all relationships and objects must begin with a lower-case letter.
 The relationship is written first, and the objects are separated by commas, and the objects are
enclosed in a pair of round braces.
 Every fact should be terminated by a period.
 The order of objects is important while defining relationships .
Ex:
likes( john, mary). // john likes mary
likes(mary, john,). // mary likes john
valuable(gold). // gold is valuable
female(jane). // jane is female
owns(jane, gold) // jane owns gold.
father( john, mary) // john is the father of mary.
gives(john, book, mary) // john gives the book to mary
The name of the relationship is called as “predicate”.
In prolog, a collection of facts is called a “database”.
Questions:
In prolog, a question looks like a fact.
?- owns( mary, book).
//Does mary owns the book? or Is it a fact that mary owns the book?
When a question is asked, Prolog system will search through the database, it looks for facts that “unify”
the fact in the question. Two facts “unify”, if their predicates are same, and if their corresponding
arguments each are the same.
If a fact unifies with the question, prolog responds “yes”, else “no”.
Ex: consider the following database
likes( joe, fish).
likes( joe, mary).
likes( mary, books).
likes( john, books).
likes( john, France).
Questions:
?- likes (joe, money).
No // means nothing unifies with the question
?- likes (mary, joe).
No
?- likes (mary, book).
Yes
Variables:
In prolog, we can not only name particular objects, but we can also use terms like X to stand for objects
that we are unwilling or unable to name.
A variable can be “instantiated” or uninstantiated”.
A variable is instantiated when there is an object that the variable stands for.
A variable is not-instantiated when what the variable stands for is not yet known.
When a question containing a variable is found, prolog searches through all its facts to find an object that
the variable could stand for.

Ex: likes (john, flowers).


likes (john, mary).
likes (paul, mary).
?- likes(john, X). // is there anything that john likes?
X= flowers;
X= mary;
no
Conjunctions: , is used to separate any number of different goals that have to be satisfied in order to
answer a question.
Ex:
Do john and mary like each other?
?-likes(john, mary ), likes(mary,john).
Facts:
likes(mary, chocolate).
likes(mary, icecream).
likes( jhon, icecream).
likes( john, mary).
Is there anything that John and Mary both like?
?-likes(mary, X), likes(john, X).
Rules:
Rules are used when we want to say that a fact “depends” on a group of other facts.
Ex: I use an umbrella if there is rain.
John buys a pizza if it is less expensive than the ice cream.
Rules are used to express definitions.
X is a bird if:
X is an animal, and
X has feathers.
or
X is a sister of Y if:
X is female, and
X and Y have the same parents.
A rule is a general statement about objects and their relationships.
Ex:
John likes anyone who likes ice cream.
Or john likes anything if it likes ice cream
Or john likes X if X likes ice cream.
likes( john, icecream):- likes(X, icecream).
Ex: john likes anyone who likes ice cream and food.
likes( john, X):- likes(X, icecream), likes(X, food).
Ex: X is siste of Y if:
- X is female
- X has mother M and father F and
- Y has same mother and father as X does.
Prolog rule:
sister_of(X, Y): female(X),
parents(X,M,F),
parents(Y, M, F).
Facts:
male(albert).
male(Edward).
female( alice).
female( victoria).
parents(edward, victoria,albert).
parents(alice, victoria, albert)
queries:
?- sister_of(alice, Edward).
true.

You might also like