Understanding Predicates and Clauses in Prolog Language with electro4u

21 Oct 2023 Balmiki Mandal 0 Prolog Programming language

Predicates and Clauses in Prolog Language

Prolog is a logic programming language that uses predicates and clauses to represent knowledge and reason about it. A predicate is a relation between one or more terms. A clause is a logical statement that consists of a head and a body. The head is a predicate, and the body is a sequence of goals.

Predicates in Prolog can be either built-in or user-defined. Built-in predicates are provided by the Prolog interpreter and can be used to perform various tasks, such as arithmetic operations, string manipulation, and input/output. User-defined predicates are defined by the programmer and can be used to represent any kind of knowledge that is relevant to the problem at hand.

Clauses in Prolog can be either facts or rules. A fact is a clause with an empty body. It states that the predicate in the head is true. A rule is a clause with a non-empty body. It states that the predicate in the head is true if all of the goals in the body are true.

Prolog programs are executed by trying to prove queries. A query is a predicate that is given to the Prolog interpreter without a body. The interpreter tries to find a clause in the program that matches the query and whose body is true. If it finds such a clause, then the query is proven true. Otherwise, the query is proven false.

Here are some examples of predicates and clauses in Prolog:

Prolog
% Predicates
parent(X, Y). % X is a parent of Y.
male(X). % X is male.
female(X). % X is female.

% Facts
parent(john, mary).
parent(anne, mary).
male(john).
female(mary).

% Rules
father(X, Y) :- parent(X, Y), male(X).
mother(X, Y) :- parent(X, Y), female(X).

The first four lines are predicate definitions. The last four lines are clauses. The first two clauses are facts, and the last two clauses are rules.

Here is an example of a query:

Prolog
father(X, mary).

This query asks whether there is someone who is the father of Mary. The Prolog interpreter will find the rule father(X, Y) :- parent(X, Y), male(X), and try to prove the body of the rule. The body of the rule consists of two goals: parent(X, Y) and male(X).

The interpreter will first try to prove the goal parent(X, Y). It will find the fact parent(john, mary), and therefore prove that the goal parent(X, Y) is true.

The interpreter will then try to prove the goal male(X). It will find the fact male(john), and therefore prove that the goal male(X) is true.

Since both goals in the body of the rule have been proven true, the interpreter will prove the query father(X, mary) true.

 

Predicates and clauses are the fundamental building blocks of Prolog programs. By understanding how to use them, you can write Prolog programs to solve a wide variety of problems.

 

 

Enroll Now: 

Prolog Programming language Scratch to advance 2023 - 2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

 

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.