DEV Community

Arhan Khan
Arhan Khan

Posted on

Prolog for Beginners: Programming by Asking Questions Instead of Giving Orders

Prolog is a logic programming language, and it works backwards compared to everything you know. Instead of telling the computer what to do step by step, you describe facts and rules about the world — and then you ask questions. The computer figures out the answers. If that sounds strange, it is. It is also one of the most mind-expanding ideas in programming.

Prolog is built on facts and rules. You state that Socrates is a man, and that all men are mortal, and Prolog can conclude that Socrates is mortal. This style is perfect for problems about relationships, puzzles, family trees, and anything where the answer is "find all the things that fit these conditions."

% Facts
parent(tom, bob).
parent(bob, ann).

% Rule: X is a grandparent of Z if X is a parent of Y and Y is a parent of Z
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

% Query: ?- grandparent(tom, ann).
% Answer: true
Enter fullscreen mode Exit fullscreen mode

Getting started is simple. Install SWI-Prolog, which is free and comes with an editor and a query console. Type a few facts, write one rule, and ask your first question. The moment Prolog answers "true" to something you did not explicitly tell it, the idea clicks.

Prolog is for beginners who like puzzles, students studying AI, and developers who want a genuinely different way of thinking. It is the language behind classic AI, expert systems, and natural language processing research, and its ideas live on in modern tools like Datalog and logic-based query engines.

The limits are honest. Prolog is slow for number-crunching, has a small ecosystem, and is rarely used to build everyday apps or websites. Debugging a program that "reasons" instead of "executes" takes practice, and the job market is tiny.

Start today with one thing: install SWI-Prolog and write facts about your own family, then a rule for "grandparent" and query it. In a week you will have written a small expert system — and you will never think about programming the same way again.

Top comments (0)