You are on page 1of 11

TRIBHUVAN UNIVERSITY

INSTITUTE OF ENGINEERING
PULCHOWK CAMPUS

Artificial Intelligence
Lab Report
Introduction to Prolog

Submitted By: Submitted To:

Pranab Ratna Gubhaju Department of Electronics and


076BCT048 Computer Engineering
Theory

Logic
Logic is the systematic study of reasoning and argumentation. It is concerned with the
principles of valid reasoning and inference, and is used to evaluate the correctness of
arguments and draw conclusions based on evidence and facts.

In logic, we use a set of rules and principles to determine whether an argument is valid or
not. This involves examining the structure of the argument and the relationships between
its premises and its conclusion.

Logic is used in a variety of fields, including mathematics, philosophy, computer science,


and linguistics, to name a few. It is an essential tool for critical thinking, problem solving,
and decision making.

Propositional Logic
Propositional logic, also known as sentential logic, is a branch of mathematical logic that
studies propositions, which are statements that are either true or false. In propositional
logic, these statements are represented by propositional variables or atomic propositions,
which can be combined using logical connectives to form compound propositions.

The five basic logical connectives in propositional logic are:


● Negation (¬): The negation of a proposition p is a new proposition ¬p, which is
true if p is false and false if p is true.

● Conjunction (∧): The conjunction of two propositions p and q, denoted by p ∧


q, is true if both p and q are true, and false otherwise.

● Disjunction (∨): The disjunction of two propositions p and q, denoted by p ∨ q,


is true if at least one of p and q is true, and false if both p and q are false.

● Implication (→): The implication of two propositions p and q, denoted by p → q,


is true if either p is false or q is true (or both), and false otherwise.

● Bi-implication (↔): The bi-implication of two propositions p and q, denoted by p


↔ q, is true if and only if p and q have the same truth value.
Propositional logic is a formal language that is used in a variety of fields, including
mathematics, computer science, and philosophy. Its applications include the design of
digital circuits, the construction of knowledge representation systems, and the
formalization of arguments in natural language.

First Order Predicate Logic


First-order predicate logic (also known as first-order logic or FOL) is a more expressive
logical system than propositional logic, which allows for the representation of complex
relationships and quantification over objects. FOL is a formal system that extends
propositional logic by introducing quantifiers, variables, and predicates.

In FOL, a domain of discourse is defined, which is the collection of all objects that can be
referred to in the logical statements. This domain can be finite or infinite and can include
objects of any type, including numbers, people, or abstract concepts.

In addition to propositional variables, FOL introduces two new types of symbols:


● Predicates: Predicates are statements that can be either true or false depending on
the objects they refer to. They are usually written with a capital letter followed by
a set of variables in parentheses. For example, P(x) could be a predicate that is true
for all values of x greater than 3.

● Quantifiers: Quantifiers are symbols that allow us to refer to a range of objects in


the domain. The two most common quantifiers are the universal quantifier (∀)
and the existential quantifier (∃). The universal quantifier (∀) asserts that a
statement holds for all objects in the domain, while the existential quantifier (∃)
asserts that a statement holds for at least one object in the domain.

Using these symbols, we can write complex statements in FOL, such as "For all x, there
exists a y such that x is greater than y." This statement is represented in FOL as ∀x ∃y
(x > y), which means "For all x, there exists a y such that x is greater than y."
Conversion of Prolog into FOPL
Prolog (PROgramming in LOGic) is a declarative programming language that is based on
first-order predicate logic (FOPL). As such, it is possible to convert Prolog programs into
FOPL statements.

To convert a Prolog program into FOPL, we need to translate each clause in the program
into an equivalent FOPL statement. Here are the steps to follow:

● Identify the predicates in the Prolog program: Predicates are represented by their
names followed by their arguments in parentheses. For example, in the clause
likes(john, mary)., likes is the predicate and john and mary are its arguments.

● Represent each predicate as a relation in FOPL: In FOPL, predicates are


represented as relations between the arguments. For example, the predicate
likes(john, mary) can be represented as Likes(john, mary).

● Represent each Prolog clause as a FOPL statement: A Prolog clause consists of a


head and a body. The head is a single predicate, and the body is a conjunction of
one or more predicates. We can represent a Prolog clause as an implication in
FOPL, where the head is the antecedent and the body is the consequent. For
example, the Prolog clause likes(john, mary) :- happy(mary). can be represented in
FOPL as Happy(mary) → Likes(john, mary).

● Translate Prolog operators into FOPL notation: Prolog uses some operators such
as = and is that do not have direct equivalents in FOPL. These operators can be
translated into FOPL notation using standard logical symbols. For example, the
Prolog clause X is Y + 1 can be translated into FOPL as S(Y) = X, where S
represents the successor function.
Assignments

1. Premises 1
1. Horses, cows, and pigs are mammals.
2. An offspring of a horse is a horse.
3. Bluebeard is a horse.
4. Bluebeard is Charlie’s parent.
5. Offspring and parent are inverse relations.
6. Every mammal has a parent.

Query
1. Is Charlie a horse?

Code
PREDICATES
horse(STRING)
mammal(STRING)
cow(STRING)
pig(STRING)
isOffspring(STRING,STRING)
isParent(STRING, STRING)

CLAUSES
horse("Bluebeard").
horse(X):- mammal(X).
horse(X):- isOffspring(Y,X),horses(Y).

isOffspring(X,Y):-parent(Y,X).
isParent("Bluebeard","Charlie").
isParent(X,Y):- isOffspring(Y,X).
cow(X):- mammal(X).
pig(X):- mammal(X).
Mammal(X):- parent(Y,X).

GOAL
horse("Charlie").
Output

2. Premises 2
1. All people who are not poor and are smart are happy.
2. Those people who read are not stupid.
3. John can read and is wealthy.
4. Happy people have exciting lives.

Question:
1. Can anyone be found with an exciting life?”?

Code
PREDICATES
notPoor(STRING)
smart(STRING)
happy(STRING)
notStupid(STRING)
read(STRING)
wealthy(STRING)
excitingLife(STRING)

CLAUSES
excitingLife(X):-happy(X).
happy(X):- notPoor(X),smart(X).
smart(X):- notStupid(X).
notStupid(X):- read(X).
notPoor(X):- wealthy(X).
read("John").
wealthy("John").

GOAL
excitingLife(X).
Output

3. Premises 3
1. All Pompeians are Romans.
2. All Romans were either loyal to Caesar or hated him.
3. Everyone is loyal to someone.
4. People only try to assassinate rulers they are not loyal to.
5. Marcus tried to assassinate Caesar.
6. Marcus was Pompeian.

Question
1. Did Marcus hate Caesar?

Code
PREDICATES
pompeian(STRING)
roman(STRING)
loyal(STRING,STRING)
hate(STRING,STRING)
assassinate(STRING,STRING)

CLAUSES
pompeian(X):- roman(X).
pompeian(X):- loyal(X,Y).
roman(X):- loyal(X,Y).
roman(X):- loyal(X,"Caesar");hate(X,"Caesar").
assassinate(X,Y):- not(loyal(X,Y)).
assassinate("Marcus","Caesar").
hate(X,Y).
loyal(X,Y).

GOAL
hate("Marcus","Caesar").
Output

4. Premises 4
Bhogendra likes all kinds of food. Oranges are food. Chicken is food. Anything anyone eats and
isn’t killed by its food. If a person likes a food means that person has eaten it. Jogendra eats
peanuts and is still alive. Shailendra eats everything Bhogendra eats.

Question
Does Shailendra like chicken?

Code
PREDICATES
food(STRING)
eat(STRING,STRING)
notKilledby(STRING,STRING)
like(STRING,STRING)

CLAUSES
food("Oranges").
food("Chicken").
food(X):- like("Bhogendra",X).
food(X):- eat(Y,X),notKilledby(Y,X).

like(X,Y):- eat(X,Y).
eat("Jogendra","Peanuts").
eat("Bhogendra",X):- eat("Shailendra",X).
notKilledby("Jogendra","Peanuts").

GOAL
like("Shailendra","Chicken").
Output

5. Premises 5
Dave and Fred are members of a dancing club in which no member can both waltz and jive.
Fred’s dad can’t waltz and Dave can do whatever Fred can’t do. If a child can do something, then
their parents can do it also.

Question
Prove that there is a member of the dancing club who can’t jive.

Code
PREDICATES
member(STRING,STRING)
parent(STRING,STRING)
can(STRING,STRING)
cant(STRING,STRING)

CLAUSES
member("Dave","Dancing Club").
member("Fred","Dancing Club").
cant("Freds Dad","Waltz").
cant(X,Y):- parent(Z,X), cant(Z ,Y).
cant(X,"Waltz"):- member(X,"Dancing Club"), can(X,"Jive").
cant(X,"Jive"):- member(X,"Dancing Club"), can(X,"Waltz").

can("Dave",X):- cant("Fred",X).
can(X,Y):- parent(Z,X), can(Z,Y).
parent("Freds Dad","Fred").

GOAL
cant(X,"Jive"),member(X,"Dancing Club").
Output

Conclusion
During the lab session, we were taught First Order Predicate Logic and applied it to
problem solving in Visual Prolog. The session began with a focus on Propositional Logic
before moving on to Predicate Logic, where we learned about quantifiers and observed
how universal and existential quantifiers work. We solved five predicate logic questions
in Visual Prolog and constructed first order predicate logic to represent general natural
language statements.

You might also like