Programming Languages
3. Key characteristics of Procedural Programming
3.3. Logic Programming
Logic programming uses formal logic, expressing programs as facts and rules. The system infers solutions via unification and backtracking.
Core Principles
Facts and Rules: Define relationships.
Queries: Ask the system to deduce answers.
Backtracking: Tries alternative solutions.
Declarative: Logic-focused, not procedural.
Example (Java with tuProlog)
Java integrates logic programming via libraries like tuProlog
import alice.tuprolog.*;
public class LogicExample {
public static void main(String[] args) throws Exception {
Prolog engine = new Prolog();
engine.addTheory(new Theory(
"parent('John', 'Mary').\n" +
"parent('Mary', 'Alice').\n" +
"grandparent(X, Z) :- parent(X, Y), parent(Y, Z)."
));
SolveInfo info = engine.solve("grandparent('John', 'Alice').");
System.out.println(info.isSuccess()); // Output: true
}
}