0% found this document useful (0 votes)
10 views31 pages

5-FOL - Knowledge Base Using Prolog

The document provides an overview of Prolog, a declarative programming language used in artificial intelligence, highlighting its key features such as facts, rules, queries, backtracking, unification, and recursion. It includes examples of knowledge bases that demonstrate how to represent relationships and make queries about entities, particularly in family and academic contexts. The document also explains how to infer information using logical reasoning and rules within Prolog.

Uploaded by

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

5-FOL - Knowledge Base Using Prolog

The document provides an overview of Prolog, a declarative programming language used in artificial intelligence, highlighting its key features such as facts, rules, queries, backtracking, unification, and recursion. It includes examples of knowledge bases that demonstrate how to represent relationships and make queries about entities, particularly in family and academic contexts. The document also explains how to infer information using logical reasoning and rules within Prolog.

Uploaded by

i220600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Knowledge Representation and Reasoning

BS- Artificial Intelligence


FALL 24
MR . SHAHBAZ HASSAN
FOL –Knowledge Base Using Prolog
Introduction- Prolog
Prolog (short for Programming in Logic) is a high-level programming language primarily
used for artificial intelligence (AI) and computational linguistics.

It is a declarative programming language, meaning that the programmer specifies what the
problem is rather than how to solve it

This is in contrast to procedural languages like C or Python, where the programmer writes a
sequence of steps to solve a problem.
Key Features- Prolog
Declarative Paradigm:
Prolog is based on first-order logic or predicate logic, where the program consists of a series of facts,
rules, and queries.
Facts, Rules, and Queries:
Facts represent statements that are unconditionally true, such as parent(john, mary). (John is a parent of
Mary).
Rules are logical implications that express relationships between facts, such as grandparent(X, Y) :-
parent(X, Z), parent(Z, Y). (X is a grandparent of Y if X is a parent of Z, and Z is a parent of Y).
Queries are questions asked to the Prolog system, like ?- grandparent(john, Who). (Who is John a
grandparent of?).
Key Features- Prolog
Backtracking:
Prolog uses a mechanism called backtracking to search for all possible solutions to a query.
When a query is posed, Prolog starts searching for answers by evaluating facts and rules. If it encounters
a dead end, it backtracks to try a different path.

Unification:
Unification is the process by which Prolog matches two terms (such as constants or variables) by finding
substitutions that make them identical. It’s a fundamental operation in Prolog for matching facts and
queries.
Key Features- Prolog
Built-in Recursion:
Prolog supports recursive definitions, which is useful for solving complex problems like graph traversal,
list processing, and more.

Logical Variables:
Variables in Prolog are logical and can represent unknowns that are "filled in" during computation. For
example, in parent(X, Y)., X and Y are variables that Prolog tries to resolve when the query is executed.
Atoms and Variables in Prolog
Atoms : Prolog
Atoms:
Atoms in Prolog are constants that represent fixed values. They are symbolic names that refer to
specific objects, facts, or entities, and they are always literal—they do not change during the
program's execution.
Atoms are case-sensitive.
They can consist of lowercase letters, numbers, and underscores, or they can be enclosed in
single quotes (').
Examples of atoms: apple, john, car, red.
Atoms can be any lowercase string of characters or symbols (e.g., dog, john_doe).
Variable : Prolog
Variable
Variables in Prolog are placeholders for values that can be unbound (i.e., not assigned a value
yet) or bound (i.e., assigned to specific values) during query evaluation. They are used to refer to
unknown values or to retrieve data from the knowledge base.
Variables are case-insensitive.
They always start with an uppercase letter or an underscore (_).
Variables do not have fixed values until they are bound during a query.
Comparison
Feature Variables Atoms
Placeholders for values (can be
Definition Fixed, symbolic constants
bound or unbound)

Starts with an uppercase letter or Starts with a lowercase letter or


Syntax
underscore enclosed in quotes
Case Sensitivity Case-insensitive Case-sensitive
Used to store or retrieve
Used for fixed values, constants,
unknown values
and relations
Usage Typically found in facts and
Variables are often used in rules
predicates that define fixed
to create generalized statements.
relationships.

Examples X, Person, Fruit, _ (anonymous) apple, john, 'New York', blue_car


Syntax: Prolog
Syntax: Prolog
Facts: Prolog
Facts are statements that describe object properties or relations between objects.
Knowledge Base I
Knowledge Base 1 (KB1) is simply a collection of facts. Facts are used to state
things that are unconditionally true of some situation of interest. For example, we
can state that Mia, Jody, and Yolanda are women, that Jody plays air guitar, and
that a party is taking place, using the following five facts:
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
Knowledge Base I
consult('C:/GNU-Prolog/bin/mykb1.pl').
Query:
?- woman(mia): Yes
?- playsAirGuitar(jody):Yes
?- playsAirGuitar(mia): No
Knowledge Base 2
happy(yolanda).
listens2Music(mia).
listens2Music(yolanda):-
happy(yolanda).
playsAirGuitar(mia):-
listens2Music(mia).
playsAirGuitar(yolanda):-
listens2Music(yolanda).
Knowledge Base 2: Query
?- playsAirGuitar(mia).
Prolog will respond yes. Why? Well, although it can’t find playsAirGuitar(mia) as a fact
explicitly recorded in KB2, it can find the rule playsAirGuitar(mia):- listens2Music(mia).
Moreover, KB2 also contains the fact listens2Music(mia) . Hence Prolog can use the rule of
modus ponens to deduce thatplaysAirGuitar(mia) .
Knowledge Base 2: Query
?- playsAirGuitar(yolanda).

Prolog would respond yes. Why? Well, first of all, by using the fact happy(yolanda) and the rule
listens2Music(yolanda):- happy(yolanda). Prolog can deduce the new fact
listens2Music(yolanda) . This new fact is not explicitly recorded in the knowledge base — it is
only implicitlypresent (it is inferred knowledge). Nonetheless, Prolog can then use it just like an
explicitly recorded fact. In particular, from this inferred fact and the rule
Knowledge Base 3
woman(mia).
 woman(jody).
 woman(yolanda).
 loves(vincent,mia).
 loves(marsellus,mia).
 loves(pumpkin,honey_bunny).
 loves(honey_bunny,pumpkin).
Knowledge Base 3: Query
?- woman(X).
?- loves(marsellus,X), woman(X)
Knowledge Base 4
Scenario: Family Relationships
We have a small family:
Tom is the father of Alice.
Mary is the mother of Alice.
Alice has a pet cat named Whiskers.
We want to represent these relationships and make simple queries like:
Who is Alice's father?
Who are the parents of Alice?
What pet does Alice have?
Knowledge Base 4
% Facts
father(tom, alice).
mother(mary, alice).
has_pet(alice, whiskers).
% Rule for general parent-child relationship
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
Knowledge Base 4
Queries:
Who is Alice's father?
?- father(Father, alice).
What pet does Alice have?
?- has_pet(alice, Pet).
Is Tom a parent of Alice?
?- parent(tom, alice).
Knowledge Base 5
In a university, students can enroll in courses. However, some courses have prerequisites that students must complete before enrolling.
Additionally, students have academic advisors assigned to guide them in selecting courses.

Scenario Description:
Alice is a student.

Bob is a student.

Dr. Smith is Alice's academic advisor.

Dr. Johnson is Bob's academic advisor.

The course "Data Structures" is offered by the university.

The course "Algorithms" is offered by the university.

"Data Structures" is a prerequisite for "Algorithms".

Alice has completed "Data Structures".

Bob has not completed "Data Structures".


Knowledge Base 5
Rules
A student can enroll in a course if they have completed its prerequisite.
Any student can enroll in a course that has no prerequisite.

We Want to Infer from them:


Who can enroll in the "Algorithms" course.
Who advises Alice and Bob.
What courses have prerequisites.
Knowledge Base 5 : Facts
student(alice).
student(bob).
advisor(dr_smith, alice).
advisor(dr_johnson, bob).
course(data_structures).
course(algorithms).
prerequisite(data_structures, algorithms).
completed(alice, data_structures).
Knowledge Base 5 : Facts
% Rule:
can_enroll(Student, Course) :- prerequisite(Prereq, Course),
completed(Student, Prereq).

can_enroll(Student, Course) :- course(Course),


\+ prerequisite(_, Course).
Knowledge Base 5 : Facts
Queries
Who can enroll in the "Algorithms" course?
?- can_enroll(Student, algorithms).
Who is Alice’s academic advisor?
?- advisor(Advisor, alice).
Who is Bob’s academic advisor?
?- advisor(Advisor, bob).
Knowledge Base 5 : Facts
Queries
What are the prerequisites for the "Algorithms" course?
?- prerequisite(Prereq, algorithms).
Has Alice completed "Data Structures"?
?- completed(alice, data_structures).
Can Bob enroll in "Algorithms"?
?- can_enroll(bob, algorithms).
Simplification using ROI
Can Alice Enroll in Algorithms?
Student(Alice) (Given fact)
Completed(Alice, DataStructures) (Given fact)
Prerequisite(DataStructures, Algorithms) (Given fact)
Apply Universal Instantiation to Rule 1 for Alice:
(Student(Alice)∧Prerequisite(DataStructures,Algorithms)∧Completed(Alice,DataStructures))→CanEnroll(Alice,A
lgorithms)
Modus Ponens:
From facts 1, 2, and 3, the premises of the implication hold.
Therefore, CanEnroll(Alice, Algorithms) is true.
Conclusion: Alice can enroll in "Algorithms."
Simplification using ROI
Can Bob Enroll in Algorithms?
Student(Bob) (Given fact)
¬Completed(Bob, DataStructures) (Given fact)
Prerequisite(DataStructures, Algorithms) (Given fact)
Apply Universal Instantiation to Rule 1 for Bob:
(Student(Bob)∧Prerequisite(DataStructures,Algorithms)∧Completed(Bob,DataStructures))→CanEnroll(Bob,Alg
orithms)
Modus Tollens:
From fact 2, we know that ¬Completed(Bob, DataStructures).
Therefore, using Modus Tollens, CanEnroll(Bob, Algorithms) is false.
Conclusion: Bob cannot enroll in "Algorithms."

You might also like