Logic Programming
Logic programming expresses computation as formal logic, using rules and facts rather than explicit algorithms.
Core Concept
Programs consist of:
- Facts: Things that are true
- Rules: Logical implications
- Queries: Questions to answer
Example: Prolog
% Facts
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
% Rules
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
% Queries
?- grandparent(tom, Who).
% Who = ann
% Who = pat
?- sibling(bob, liz).
% Yes
Key Features
- Declarative nature
- Pattern matching
- Backtracking
- Unification
Use Cases
- AI and expert systems
- Natural language processing
- Constraint solving
- Knowledge bases
🧠