Logic programming is a model of computation that applies mathematical logic to problem solving.
IntroductionLogic programming is declarative, not procedural. In the procedural programming paradigm, the programmer specifies data and the algorithms that should be executed on those data to reach a solution. In the logic programming paradigm, the programmer specifies relationships that hold between the data in the form of facts and rules. The programmer then specifies a goal, and the computer works out the implementation details of achieving that goal.
Specifying relationshipsA relation specifies a relationship that holds between some objects. We denote relations with the form pred(obj1, obj2, ...)
, where the name of the relation is called the predicate.
To store relations in our system for use in proving goals, we use clauses. A clause consists of a head relation and some body relations. For example, in the clause
compatible(John, June) :- common_interests(John, June), lazy(June)
we are specifying that John and June are compatible if they have common interests and June is lazy. The head of this clause is compatible(John, June)
and the body consists of the two relations common_interests(John, June)
and lazy(June)
. We call clauses of this form rules, since they specify when a relation is true. To specify a relation that is unconditionally true, we use a clause with no body, called a fact: girl(June)
.
We can use logic variables to describe more abstract relations. Consider the following clauses:
female(June)
likes(June, running)
likes(John, running)
similar_hobbies(?x, ?y) :- likes(?x, ?z), likes(?y, ?z)
compatible(John, ?x) :- female(?x), similar_hobbies(John, ?x)
The last two rules use logic variables. The second-to-last rule specifies when two people have similar hobbies (that is, there is something they both like), and the last rule specifies the people with whom John is compatible.
GoalsOnce we have some clauses, we can specify a goal, and the system will attempt to satisfy that goal. In logic programming parlance, we call this proving a goal. The goal is stated in the form of a relation.
Sometimes our goal requires a yes or no answer. The system will use the existing clauses to determine whether the given goal can be satisfied. For example, using the five clauses defined above, we might specify the goal compatible(John, June)
. If we try to prove this goal, the result will simply be "Yes." If we try to prove the goal male(June)
, the result will be "No.", as the system is unable to prove that June is male from the specified clauses.
We can specify much more interesting goals. For instance, we might specify the goal likes(?x, running)
. Here, we are interested in determining who is interested in running. If we attempt to prove this goal, the system will determine if it can be satisfied, and if so, what values of ?x
satisfy the goal. Here, the results will be John and June, since we declared that both of them like running. These results that, when substituted for the variables, satisfy the goal are called the bindings of those variables.
For more examples, see the following databases of clauses:
These databases can be loaded into the provided Prolog interpreter for experimentation.
ImplementationProgramming in this model requires some adjustment coming from a procedural programming background; logic programming appears very mysterious at first. The implementation, however, is simple, and relies on three basic concepts:
We will see how these three concepts are implemented below.
UseThis module provides a library that enables the use of logic programming in arbitrary Python programs. For some examples of this, see the following:
Written by Daniel Connelly. This implementation is inspired by chapter 11 of "Paradigms of Artificial Intelligence Programming" by Peter Norvig.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4