# Logic programming

## Programming paradigm based on formal logic / From Wikipedia, the free encyclopedia

#### Dear Wikiwand AI, let's keep it short by simply answering these key questions:

Can you list the top facts and stats about Logic programming?

Summarize this article for a 10 year old

**Logic programming** is a programming, database and knowledge representation paradigm based on formal logic. A logic program is a set of sentences in logical form, representing knowledge about some problem domain. Computation is performed by applying logical reasoning to that knowledge, to solve problems in the domain. Major logic programming language families include Prolog, Answer Set Programming (ASP) and Datalog. In all of these languages, rules are written in the form of *clauses*:

`A :- B`

_{1}, ..., B_{n}.

and are read as declarative sentences in logical form:

`A if B`

_{1}and ... and B_{n}.

`A`

is called the *head* of the rule, `B`

, ..., _{1}`B`

is called the _{n}*body*, and the `B`

are called _{i}*literals* or conditions. When n = 0, the rule is called a *fact* and is written in the simplified form:

`A.`

Queries (or goals) have the same syntax as the bodies of rules and are commonly written in the form:

`?- B`

_{1}, ..., B_{n}.

In the simplest case of Horn clauses (or "definite" clauses), all of the A, B_{1}, ..., B_{n} are atomic formulae of the form p(t_{1} ,..., t_{m}), where p is a predicate symbol naming a relation, like "motherhood", and the t_{i} are terms naming objects (or individuals). Terms include both constant symbols, like "charles", and variables, such as X, which start with an upper case letter.

Consider, for example, the following Horn clause program:

```
mother_child(elizabeth, charles).
father_child(charles, william).
father_child(charles, harry).
parent_child(X, Y) :-
mother_child(X, Y).
parent_child(X, Y) :-
father_child(X, Y).
grandparent_child(X, Y) :-
parent_child(X, Z),
parent_child(Z, Y).
```

Given a query, the program produces answers.
For instance for a query `?- parent_child(X, william)`

, the single answer is

```
X = charles
```

Various queries can be asked. For instance the program can be queried both to generate grandparents and to generate grandchildren. It can even be used to generate all pairs of grandchildren and grandparents, or simply to check if a given pair is such a pair:

```
grandparent_child(X, william).
X = elizabeth
?- grandparent_child(elizabeth, Y).
Y = william;
Y = harry.
?- grandparent_child(X, Y).
X = elizabeth
Y = william;
X = elizabeth
Y = harry.
?- grandparent_child(william, harry).
no
?- grandparent_child(elizabeth, harry).
yes
```

Although Horn clause logic programs are Turing complete,^{[1]}^{[2]} for most practical applications, Horn clause programs need to be extended to "normal" logic programs with negative conditions. For example, the definition of sibling uses a negative condition, where the predicate = is defined by the clause ` X = X: `

```
sibling(X, Y) :-
parent_child(Z, X),
parent_child(Z, Y),
not(X = Y).
```

Logic programming languages that include negative conditions have the knowledge representation capabilities of a non-monotonic logic.

In ASP and Datalog, logic programs have only a declarative reading, and their execution is performed by means of a proof procedure or model generator whose behaviour is not meant to be controlled by the programmer. However, in the Prolog family of languages, logic programs also have a procedural interpretation as goal-reduction procedures. From this point of view, clause A :- B_{1},...,B_{n} is understood as:

- to solve
`A`

, solve`B`

, and ... and solve_{1}`B`

._{n}

Negative conditions in the bodies of clauses also have a procedural interpretation, known as *negation as failure*: A negative literal ` not B`

is deemed to hold if and only if the positive literal ` B`

fails to hold.

Much of the research in the field of logic programming has been concerned with trying to develop a logical semantics for negation as failure and with developing other semantics and other implementations for negation. These developments have been important, in turn, for supporting the development of formal methods for logic-based program verification and program transformation.