Karthik Divi
·4 min read

Prolog Online Compiler - Run Prolog Code in Your Browser

Prolog does not work like other programming languages. You do not write instructions. You state facts and rules, then ask questions. The runtime figures out the answers through unification and backtracking. It is declarative programming taken to its logical conclusion -- literally.

This makes Prolog strange if you are coming from imperative or functional languages. There are no loops, no assignments in the traditional sense, no step-by-step control flow. You describe what is true, and Prolog searches for solutions that satisfy your constraints.

Why try Prolog online?

Installing SWI-Prolog or GNU Prolog locally works fine, but Prolog is often a language people are exploring rather than committing to. Maybe a university course requires it. Maybe you read about logic programming and want to see it in action. Maybe you are building an expert system and want to prototype rules quickly.

For all of these, an online Prolog compiler makes sense:

  • No installation needed for a language you might be trying for the first time
  • Test individual rules and queries without managing project files
  • Share working Prolog programs when asking for help or explaining a concept
  • Iterate fast on rule definitions

Facts, rules, and queries

Here is a Prolog program that defines a small family tree and derives relationships from it. This is the classic Prolog example because it shows how much you can infer from a few simple facts:

% Facts
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).

female(liz).
female(ann).
female(pat).
male(tom).
male(bob).
male(jim).

% Rules
father(X, Y) :- parent(X, Y), male(X).
mother(X, Y) :- parent(X, Y), female(X).

grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.

ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

% Queries
:- grandparent(tom, X), write(X), write(' is a grandchild of tom'), nl, fail ; true.
:- ancestor(tom, jim), write('tom is an ancestor of jim'), nl.
:- sibling(ann, pat), write('ann and pat are siblings'), nl.

Look at the ancestor rule. It has two clauses: X is an ancestor of Y if X is a parent of Y (base case), or if X is a parent of some Z who is an ancestor of Y (recursive case). Prolog handles the recursion and backtracking automatically. You never wrote a search algorithm, but that is exactly what Prolog executes under the hood.

Unification and backtracking

Two concepts make Prolog tick. Unification is pattern matching on steroids -- Prolog tries to make two terms identical by finding variable bindings that work. Backtracking means when a path fails, Prolog automatically rewinds and tries the next possibility.

Together, they turn Prolog into a search engine for logical solutions. This is why Prolog was historically used in AI and expert systems. You encode domain knowledge as rules, and Prolog answers questions about that domain.

Where Prolog still shows up

Prolog is not a mainstream application language, but it has real niches. Natural language processing, type inference in compilers, database query languages (SQL has roots in relational logic), configuration validation, and scheduling problems all benefit from Prolog's approach. Datalog, a subset of Prolog, powers several modern static analysis tools.

Prolog on OneCompiler

OneCompiler's Prolog environment lets you define facts, write rules, and run queries directly in your browser.

Why it works well for Prolog specifically:

  • Prolog programs are typically short and self-contained, which fits the online compiler format perfectly
  • You can modify rules and re-run queries instantly to see how changes affect the results
  • Shareable URLs make it easy to post Prolog examples in study groups or Q&A sites
  • No SWI-Prolog installation or configuration needed

If you have never tried logic programming, Prolog will change how you think about problem solving. And you can see that for yourself in about thirty seconds.

Try it here: Prolog Online Compiler on OneCompiler