In this section, we will explore how to write and execute simple queries in Prolog. Queries are the primary way to interact with a Prolog program, allowing you to ask questions about the facts and rules defined in your knowledge base.

Key Concepts

  1. Queries: Questions asked to the Prolog interpreter to retrieve information based on the facts and rules.
  2. Facts: Statements that are unconditionally true.
  3. Rules: Conditional statements that define relationships between facts.
  4. Unification: The process of making two terms equal by finding a suitable substitution for variables.

Writing Simple Queries

Basic Query Structure

A query in Prolog is written as a goal followed by a period (.). For example:

?- parent(john, mary).

This query asks if john is a parent of mary.

Example Knowledge Base

Let's start with a simple knowledge base:

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

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

Querying Facts

To check if a fact is true, you can directly query it:

?- parent(john, mary).

Explanation: This query checks if john is a parent of mary. If the fact exists in the knowledge base, Prolog will return true.

Querying Rules

You can also query rules to infer new information:

?- grandparent(john, susan).

Explanation: This query checks if john is a grandparent of susan. Prolog will use the grandparent rule to determine this by checking if there is an intermediate Z such that john is a parent of Z and Z is a parent of susan.

Using Variables in Queries

Variables in Prolog start with an uppercase letter or an underscore (_). You can use variables to make more general queries:

?- parent(john, X).

Explanation: This query asks for all X such that john is a parent of X. Prolog will return all possible values of X that satisfy the query.

Multiple Solutions

Prolog can return multiple solutions for a query. You can request more solutions by typing ; after each solution:

?- parent(mary, X).
X = susan ;
false.

Explanation: This query asks for all X such that mary is a parent of X. Prolog finds susan as a solution. Typing ; asks Prolog to find more solutions, but since there are no more, it returns false.

Practical Exercises

Exercise 1: Querying Facts

Given the following knowledge base:

% Facts
sibling(alice, bob).
sibling(bob, charlie).
sibling(charlie, dave).

Write queries to check if:

  1. alice is a sibling of bob.
  2. bob is a sibling of alice.
  3. charlie is a sibling of dave.

Solution:

?- sibling(alice, bob).
true.

?- sibling(bob, alice).
false.

?- sibling(charlie, dave).
true.

Exercise 2: Querying Rules

Given the following knowledge base:

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

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

Write queries to find:

  1. All grandchildren of john.
  2. All grandparents of susan.

Solution:

?- grandparent(john, X).
X = susan.

?- grandparent(X, susan).
X = john.

Exercise 3: Using Variables

Given the following knowledge base:

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

Write a query to find all children of mary.

Solution:

?- parent(mary, X).
X = susan.

Common Mistakes and Tips

  • Forgetting the period: Always end your queries with a period (.). Without it, Prolog will not execute the query.
  • Case sensitivity: Remember that Prolog is case-sensitive. john and John are considered different atoms.
  • Using variables correctly: Variables must start with an uppercase letter or an underscore (_). Using a lowercase letter will be interpreted as an atom.

Conclusion

In this section, we learned how to write and execute simple queries in Prolog. We covered querying facts, rules, and using variables to retrieve information from the knowledge base. Understanding how to formulate queries is fundamental to working with Prolog, as it allows you to interact with and extract meaningful information from your programs. In the next section, we will delve deeper into variables and unification, which are crucial for more complex queries and logic programming.

© Copyright 2024. All rights reserved