Google News
logo
Prolog - Interview Questions
What is the basic syntax of a Prolog clause?
In Prolog, a clause is a fundamental unit of program structure. It consists of a head and a body, separated by an implication symbol (:-).

The basic syntax of a Prolog clause is as follows:
Head :- Body.​

The head represents a predicate, which is a logical statement or goal. It typically consists of a predicate name followed by a comma-separated list of arguments enclosed in parentheses. The arguments can be variables, constants, or other predicates. The head specifies what needs to be proven or satisfied.

The body is a sequence of goals or subgoals that define the conditions or constraints for the head to be true. The body can consist of one or more goals, each separated by a comma. Goals can be predicates or built-in operations that need to be satisfied.

Here's an example of a Prolog clause that defines a simple rule :
mortal(X) :- human(X).​

In this example, `mortal` is the predicate in the head, and `human(X)` is the goal in the body. This clause states that if `X` is a human, then `X` is mortal.

It's important to note that Prolog uses a period (.) at the end of a clause to indicate the end of a logical statement. Multiple clauses can be defined in a Prolog program, and they are typically separated by line breaks.

The syntax of Prolog clauses allows for the composition of complex logical relationships and rules by combining multiple clauses and predicates.
Advertisement