5-FOL - Knowledge Base Using Prolog
5-FOL - Knowledge Base Using Prolog
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)
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.