Logic Programming is a programming paradigm that is largely based on formal logic. Any program written in a logic programming language is a set of sentences in logical form, expressing facts and rules about some problem domain. The most well-known logic programming language is Prolog (short for “Programming in Logic”).

Key Concepts of Logic Programming

Facts are basic assertions about some world entities. They are used to represent known information. Example in Prolog:

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

Rules are logical statements that define relationships between facts. They are used to infer new information from known facts.

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

Queries are questions asked about the information stored in the form of facts and rules. The logic programming system attempts to find answers to these queries based on the given facts and rules.

?- grandparent(john, susan).

Here’s a simple example to illustrate logic programming in Prolog:

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

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

% Queries
% ?- grandparent(john, susan).
% Expected output: true
% ?- grandparent(mary, alice).
% Expected output: false

The aim of logic programming is to provide the programmer with techniques for decomposing a computational problem into two separate problems: what is to be computed, and how this should be computed. This is accomplished by selecting a subset of the statements of mathematical logic that is powerful enough to be able to describe anything one might want to compute, yet weak enough to have a con- trollable procedural interpretation. The intention here is that, on the one hand, a program specified in a logic programming language should be an effective program that can be carried out by a computer. Control (“how” to compute) is effected by using the order of evalua- tion of the language. We should be able to arrange the order of clauses and the order of subgoals within each clause so that the computation is done in an order deemed to be effective and efficient. At the same time, we should be able to view the result of the computation (“what” to compute) as a simple consequence of the laws of logic.

Summary

Logic Programming is a paradigm that uses formal logic to express facts, rules, and queries about a problem domain. It is declarative, meaning that the programmer specifies what the program should accomplish rather than explicitly defining how it should be done. Prolog is the most well-known logic programming language, and it is used in various fields such as artificial intelligence, natural language processing, and knowledge representation.