Logic programming using Prolog

 

🧠 Logic Programming & PROLOG – The Language That Thinks for You

🌍 1. Understanding Logic Programming

Most programming languages tell the computer how to do something — step by step.
Logic programming is different. You tell the computer what is true, and it figures out how to reach the answer.

Instead of writing procedures, you describe facts and rules that define relationships.
This makes it especially powerful for:

  • Artificial Intelligence (AI)
  • Expert systems
  • Problem solving and reasoning

πŸ’‘ 2. What Is PROLOG?

PROLOG stands for Programming in Logic.
It’s built for reasoning and deduction rather than traditional control flow.

In Prolog, you define:

  • Facts → Things that are true
  • Rules → Logic that connects facts
  • Queries → Questions you ask the system

⚙️ Key Features

  • Declarative syntax: state what should hold true
  • Efficient knowledge representation
  • Inference engine with backtracking
  • Recursion instead of loops
  • Pattern matching (unification)

🧱 3. Building Blocks of PROLOG

🟒 Facts

parent(jack, tom).

Jack is the parent of Tom.

🟑 Rules

grandfather(X, Y) :- father(X, Z), parent(Z, Y).

X is the grandfather of Y if X is the father of Z and Z is the parent of Y.

πŸ”΅ Queries

?- grandfather(jack, tom).

Is Jack the grandfather of Tom?

πŸ“Š Diagram 2 – Structure of a Prolog Program

Knowledge Base → Query Phase → Inference Engine
(Facts + Rules → Query → Logical reasoning)

⚖️ 4. Declarative vs. Imperative Thinking

In Python:

for x in range(5): print(x)

In Prolog, you describe relationships and let the system figure out the flow.
This shift from doing to describing is key to AI reasoning.

πŸ” 5. How PROLOG Works

  1. Reads the query
  2. Searches for matching facts
  3. Applies rules if needed
  4. Uses recursion to explore
  5. Backtracks if a path fails


Start → Input Query → Check Facts → Apply Rules → Return YES/NO

🧩 6. Simple PROLOG Program

% Facts
man(john).
woman(mary).
capital_of(france, paris).

% Rule
not_same(X, Y) :- man(X), woman(Y), X \= Y.

% Queries
?- not_same(john, mary).
?- capital_of(france, X).

Explanation:
John is a man, Mary is a woman.
The rule checks if they’re different genders and not the same person.
The capital query returns X = paris.

🧠 7. Logical Reasoning Example

bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).

% Transitive rule
bigger_than(X, Y) :- bigger(X, Y).
bigger_than(X, Y) :- bigger(X, Z), bigger_than(Z, Y).

Query:

?- bigger_than(elephant, monkey).

Prolog’s reasoning:
Elephant → Horse → Donkey → Monkey → ✅ True


πŸ” 8. Backtracking & Unification

  • Unification: matching terms
    father(X, john) unifies with father(mike, john) → X = mike
  • Backtracking: if one path fails, Prolog tries another

🧩 Diagram 4 – Backtracking Tree

Query: ?- bigger_than(elephant, Y)
elephant → horse → donkey → dog ✔
↘ monkey ✔

πŸš€ 9. Real-World Applications

Domain Example Use
πŸ€– AI Expert systems, chatbots
🧠 NLP Language understanding
🧩 Theorem Proving Mathematical logic
πŸ—‚️ Databases Relationship queries
πŸ“… Scheduling Constraint solving

πŸ’» 10. Installing & Running PROLOG

Linux setup:

sudo apt-get install swi-prolog

Run your program:

$ swipl
?- [yourprogram].

🧰 11. Common Predicates

Predicate Use
write() Display output
nl New line
halt Stop program
is/2 Arithmetic operations
= Unification

πŸ”„ 12. Recursion Example

sum_list([], 0).
sum_list([H|T], S) :-
    sum_list(T, S1),
    S is H + S1.

Sum of a list = head + sum of the rest

🧩 Diagram 5 – Recursive Flow

sum_list([2,3,5], S)
→ 2 + sum_list([3,5], S1)
→ 3 + sum_list([5], S2)
→ 5 + sum_list([], 0)
Result: S = 10

🧭 13. Thinking Like a Logic Programmer

Instead of commanding the computer, describe relationships.
Prolog handles the reasoning using facts, rules, unification, and backtracking.

✏️ 14. Quick Practice Zone

✅ Fact: mother(riya, aryan).
✅ Query: ?- mother(X, aryan).X = riya
✅ Explanation:
Facts = direct truths
Rules = logical conditions built from facts

🌈 Final Thoughts

Logic programming teaches you to think like a reasoner, not just a coder.
Prolog proves that programming can be about knowledge, not just commands.
It’s one of the earliest AI languages — and still one of the smartest.



Comments

Popular posts from this blog

Machine Learning Rules :Perceptron learning rule , delta learning rule (LMS–Widrow Hoff)

Types of environments in AI