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
- Reads the query
- Searches for matching facts
- Applies rules if needed
- Uses recursion to explore
- 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 withfather(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
Post a Comment