Course contentsShow
← Computer Science
Lesson 2157 of 2,87248. Programming ParadigmsFree lesson

What is Logic Programming?

Introduction to declarative programming with logical rules, facts, and queries instead of step-by-step instructions.

What is Logic Programming?

You'll learn what logic programming is and how it differs from telling a computer how to solve a problem by instead describing what the problem is.

The Core Idea

Logic programming is a declarative paradigm where you express programs as collections of facts, rules, and queries based on formal logic. Instead of writing step-by-step instructions (imperative style), you state what is true about the problem domain and let the language's inference engine figure out how to derive answers.

Think of it like being a detective: you don't tell the detective how to solve the case step-by-step. Instead, you give them facts ("Alice was at the park"), rules ("if someone was at the park and it was raining, they got wet"), and then ask questions ("who got wet?"). The detective uses logical reasoning to find the answer.

How It Works

You define:

  • Facts: Basic truths (parent(alice, bob) means Alice is Bob's parent)
  • Rules: Logical implications (grandparent(X, Z) :- parent(X, Y), parent(Y, Z) means X is Z's grandparent if X is Y's parent and Y is Z's parent)
  • Queries: Questions you want answered (?- grandparent(alice, Who) asks "who is Alice's grandchild?")

The system uses logical inference (backward chaining, unification, pattern matching) to automatically search for solutions that satisfy your query based on the facts and rules you've provided.

Connection to Declarative Programming

Logic programming is a specialized form of declarative programming (which you just studied). While SQL declares what data to retrieve and CSS declares what appearance to achieve, logic programming declares what logical relationships hold and lets inference derive conclusions.

Key Takeaway: Logic programming expresses computation as logical deduction from facts and rules rather than explicit control flow, letting the system automatically search for solutions.