In this section, we will delve into the core components of Prolog programming: facts, rules, and queries. These elements form the foundation of how Prolog operates and allows us to build logical relationships and perform reasoning.

Facts

What are Facts?

Facts are the basic assertions about the world or the domain of interest. They are used to represent known information. In Prolog, facts are written as predicates with a specific structure.

Syntax of Facts

A fact in Prolog is written as:

predicate(argument1, argument2, ..., argumentN).
  • predicate is the name of the fact.
  • argument1, argument2, ..., argumentN are the arguments or parameters of the fact.

Example of Facts

Consider a simple domain of family relationships:

parent(john, mary).
parent(mary, susan).
parent(mary, tom).

Here, parent/2 is a predicate indicating a parent-child relationship. The fact parent(john, mary). means that John is a parent of Mary.

Rules

What are Rules?

Rules define relationships between facts and are used to infer new information. They are conditional statements that describe how facts are related.

Syntax of Rules

A rule in Prolog is written as:

head :- body.
  • head is the conclusion or the result of the rule.
  • body is a conjunction of conditions that must be satisfied for the head to be true.

Example of Rules

Using the family domain, we can define a rule for the grandparent relationship:

grandparent(X, Y) :- parent(X, Z), parent(Z, Y).

This rule states that X is a grandparent of Y if X is a parent of Z and Z is a parent of Y.

Queries

What are Queries?

Queries are questions asked to the Prolog system to retrieve information based on the defined facts and rules. They are used to check if certain conditions hold or to find values that satisfy certain conditions.

Syntax of Queries

A query in Prolog is written as:

?- predicate(argument1, argument2, ..., argumentN).
  • ?- is the query prompt.
  • predicate(argument1, argument2, ..., argumentN) is the condition to be checked.

Example of Queries

Using our family domain, we can ask:

?- parent(john, mary).

This query checks if John is a parent of Mary. The Prolog system will respond with true if the fact is known, or false otherwise.

We can also ask more complex queries:

?- grandparent(john, Y).

This query asks for all Y such that John is a grandparent of Y. The Prolog system will respond with all possible values of Y that satisfy the condition.

Practical Examples

Example 1: Defining Facts

Let's define some facts about family relationships:

% Facts
parent(john, mary).
parent(mary, susan).
parent(mary, tom).
parent(susan, alice).

Example 2: Defining Rules

Now, let's define a rule to find grandparents:

% Rule
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).

Example 3: Querying the Database

We can query the database to find out if John is a grandparent:

?- grandparent(john, Y).

The expected output would be:

Y = susan ;
Y = tom.

Exercises

Exercise 1: Define Facts

Define facts for the following relationships:

  1. Alice is the parent of Bob.
  2. Bob is the parent of Charlie.
  3. Charlie is the parent of David.

Solution:

parent(alice, bob).
parent(bob, charlie).
parent(charlie, david).

Exercise 2: Define a Rule

Define a rule to find siblings. Two individuals are siblings if they share at least one parent.

Solution:

sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.

Exercise 3: Query the Database

Using the facts and rules defined, query the database to find all siblings of Charlie.

Solution:

?- sibling(charlie, Y).

Expected output:

Y = david.

Common Mistakes and Tips

  • Mistake: Forgetting the period (.) at the end of facts, rules, or queries.
    • Tip: Always ensure each statement ends with a period.
  • Mistake: Using the same variable name for different purposes within a rule.
    • Tip: Use distinct variable names to avoid confusion and ensure correct logic.

Conclusion

In this section, we covered the basics of facts, rules, and queries in Prolog. Understanding these core components is essential for building more complex logic and reasoning in Prolog. In the next module, we will explore how to define and use these elements in more detail, including variables and unification.

© Copyright 2024. All rights reserved