Logic programming Created 2026-10-31 Updated 2026-11-05
Logic programming represents knowledge and queries through logical clauses, letting an inference procedure search for substitutions making a query true. A fact such as
parent(ada, ben) and a rule saying that a parent of a parent is a grandparent can answer a grandparent query without listing every answer explicitly. Prolog commonly combines unification with ordered goal search and backtracking. Declarative meaning and execution strategy must be distinguished: two logically equivalent rule orders can have different termination behaviour. The method connects mathematical logic to executable symbolic inference. Negation as failure Created 2026-10-31 Updated 2026-11-05
Negation as failure interprets failure of a specified proof search as a negative result. In the Prolog operation
\+ Goal, the negative goal succeeds when trying Goal fails in finite time; it fails when Goal succeeds. A search that never finishes does not establish the negative result.[ref]For example, suppose a finite knowledge base records
penguin(opus) and has no rule capable of deriving penguin(robin). A search for penguin(robin) finitely fails, so its negation succeeds under this operational rule. That result relies on the chosen knowledge base and search semantics. Adding the missing fact changes the result; an unrecorded fact in the outside world has not been independently disproved.Stable model semantics gives a related model-based interpretation for negative rule premises through the stable model reduct. The rules and have two stable models, and : in either candidate, the surviving rule forces exactly its one member. Neither atom follows by skeptical entailment. Operational finite failure and a chosen model semantics must therefore be distinguished when interpreting a negative rule.
Prolog 2026-11-05
Prolog expresses programs through facts and rules and answers queries by solving goals. A rule such as
ancestor(X,Y) :- parent(X,Y). provides one way to establish a relationship. A recursive rule can search through intermediate parents. Unification matches a goal with a rule head and propagates bindings; backtracking explores alternative matches when a branch fails or another answer is requested.[ref]Getting started quickly in the SWI-Prolog manual. §2.1, rules and queries
The logical core is associated with Horn clauses and SLD resolution, but clause order and goal order also determine a conventional Prolog program's search behavior. A logically valid recursive specification can loop before reaching an available answer. Predicates with input/output effects and control operations add further operational behavior. Arithmetic evaluation is also distinct from term matching:
X = 1+2 builds a term, whereas X is 1+2 evaluates arithmetic in the usual Prolog interface. SLD resolution 2026-11-05
SLD resolution is a goal-directed inference rule for definite first-order Horn clauses. It selects an atom from the current goal, matches it with a suitably renamed clause head using a most general unifier, and replaces it by that clause's body, applying the same first-order substitution throughout the goal. Reaching the empty goal supplies a successful derivation.[ref]
For example, the rule
ancestor(X,Y) :- parent(X,Y) reduces the query ancestor(ada,Y) to parent(ada,Y). Matching the fact parent(ada,ben) then gives Y=ben. Alternative matching clauses create a search tree. Prolog normally selects the leftmost goal and searches clauses in order with backtracking; a recursive branch can therefore run indefinitely before another branch's valid answer is reached. The logical inference rule and the strategy traversing its alternatives are separate choices.