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
- Queries: Questions asked to the Prolog interpreter to retrieve information based on the facts and rules.
- Facts: Statements that are unconditionally true.
- Rules: Conditional statements that define relationships between facts.
- 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:
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:
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:
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:
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:
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:
Write queries to check if:
alice
is a sibling ofbob
.bob
is a sibling ofalice
.charlie
is a sibling ofdave
.
Solution:
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:
- All grandchildren of
john
. - All grandparents of
susan
.
Solution:
Exercise 3: Using Variables
Given the following knowledge base:
Write a query to find all children of mary
.
Solution:
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
andJohn
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.
Prolog Programming Course
Module 1: Introduction to Prolog
- What is Prolog?
- Installing Prolog
- First Steps in Prolog
- Basic Syntax and Structure
- Facts, Rules, and Queries
Module 2: Basic Prolog Programming
Module 3: Data Structures in Prolog
Module 4: Advanced Prolog Programming
- Advanced Unification
- Cut and Negation
- Meta-Programming
- Definite Clause Grammars (DCGs)
- Constraint Logic Programming
Module 5: Prolog in Practice
- File I/O
- Debugging Prolog Programs
- Prolog Libraries
- Interfacing with Other Languages
- Building a Prolog Application