Unit content
Logic programming with facts, rules and queries
In logic programming, a program describes relations that hold rather than prescribing one fixed sequence of state-changing commands.
A knowledge base can contain facts such as
parent(alice, bob).
parent(bob, carol).
and rules such as
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
The rule says that grandparent(X,Z) holds when some Y makes both premises true.
A query asks whether a relation can be satisfied and, when variables are present, which substitutions make it true:
grandparent(alice, Who)?
can produce Who = carol.
Variables in rules are logical variables: they stand for unknown terms constrained by relations, not mutable storage locations that are assigned repeatedly. Matching a query against facts and rules uses unification to find compatible substitutions.
Logic programming therefore changes the programmer's role. The program states relations and implications; an execution strategy searches for a proof of the query. This separation between declarative meaning and search procedure makes logic languages useful for symbolic reasoning, rule systems and relational specifications.