Facts, Rules, and Queries in Prolog Programming Language
Facts, Rules, and Queries in Prolog Programming Language
Prolog is a logic programming language. It is based on the idea of using facts and rules to represent knowledge. Facts are simple statements that are assumed to be true, while rules are expressions that describe how to infer new facts from existing ones.
Facts
Facts in Prolog are represented using predicate expressions. A predicate expression is a list of terms, enclosed in parentheses, with a predicate name at the beginning. For example, the following facts represent the knowledge that John is a father and that Jane is a mother:
father(john).
mother(jane).
Rules
Rules in Prolog are represented as Horn clauses. A Horn clause is an expression of the form:
Head :- Body.
where the head is a predicate expression and the body is a conjunction of predicate expressions. The head of the rule is the conclusion of the rule, and the body of the rule is the condition that must be satisfied in order for the conclusion to hold true.
Example, the following rule represents the knowledge that if someone is a father and a mother, then they are a parent:
parent(X) :- father(X), mother(X).
Queries
Queries in Prolog are also represented as predicate expressions. When a query is posed to the Prolog interpreter, the interpreter tries to find a sequence of rules that can be used to prove that the query is true. If the interpreter is successful, it succeeds in answering the query and may bind variables in the query to specific values. If the interpreter is unsuccessful, it fails to answer the query.
Example, the following query asks the Prolog interpreter whether John is a parent:
parent(john).
If the interpreter has the facts and rules listed above in its knowledge base, it will succeed in answering the query and bind the variable X in the query to the value john.
Example
The following example shows how to use facts and rules in Prolog to represent the knowledge that John is a father, Jane is a mother, and their child is Mary:
father(john).
mother(jane).
parent(X) :- father(X), mother(X).
child(X, Y) :- parent(Y), X = Y.
We can now pose queries to the Prolog interpreter to find out information about this family. For example, the following query asks the interpreter whether John is a parent of Mary:
parent(john, mary).
The interpreter will succeed in answering this query and bind the variable X in the query to the value mary.
We can also use queries to ask the interpreter to infer new knowledge from the facts and rules in its knowledge base. For example, the following query asks the interpreter whether Mary has a sibling:
sibling(X, mary).
The interpreter will succeed in answering this query and bind the variable X in the query to the value john. This is because the interpreter can infer that John is a sibling of Mary from the following facts and rules:
- John is a parent of Mary.
- A parent's child is their sibling.
Prolog is a powerful language for representing and reasoning about knowledge. It is used in a variety of applications, including artificial intelligence, natural language processing, and database systems.