XCODX |

Prolog Online Compiler & Interpreter

Select Language
Online Code Compiler
Full HTML IDE
Py main.py
Program Output Ready
  Welcome to XCODX Online Compiler

  Quick Start:
  Ctrl+Enter  Run code
  Ctrl+S      Save / Download
  Ctrl+L      Clear output

  Select a language and start coding.
Success
Operation completed

About Prolog

Prolog turns programming on its head: instead of describing steps, you declare facts and rules, then ask questions and let the engine search for answers through unification and backtracking. Born in Marseille in 1972, it remains the definitive logic programming language, taught in virtually every AI and programming-languages curriculum and famously used in the rule-based components of IBM's Watson. The dominant modern implementation is SWI-Prolog, which adds strings, modules, and a rich library ecosystem to the ISO standard. This page runs your program in an interactive terminal, so predicates that read from user_input genuinely wait for your typed response, and you can watch query results stream without installing anything.

Hello World in Prolog

:- initialization(main).

parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).

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

main :-
    write("What is your name? "),
    read_line_to_string(user_input, Name),
    format("Hello, ~w!~n", [Name]),
    forall(grandparent(G, C),
           format("~w is a grandparent of ~w~n", [G, C])),
    findall(K, parent(bob, K), Kids),
    format("bob's children: ~w~n", [Kids]).

When to use Prolog

Prolog dominates coursework in artificial intelligence, computational linguistics, and programming language theory, where assignments cover family-tree queries, graph search, parsing with definite clause grammars, and constraint puzzles like N-queens or Sudoku. Professionally it appears in rule engines, static analysis tools, and scheduling systems where declarative constraints beat imperative logic. Students preparing for PL exams use this page to test unification behavior and cut semantics quickly, and researchers sketch knowledge bases before committing to larger SWI-Prolog projects.

Common questions

Can my Prolog program prompt me for input interactively?

Yes. Predicates like read_line_to_string(user_input, X) and read/1 suspend execution until you type into the live terminal. That makes it possible to build interactive expert systems and quizzes, not just batch queries. Remember read/1 expects a valid Prolog term ending with a period, while read_line_to_string takes raw text.

Which Prolog implementation runs my code?

The environment executes code with SWI-Prolog, the most widely used open source implementation. That means SWI extensions such as strings in double quotes, format/2, read_line_to_string, and the clpfd constraint library behave as documented on swi-prolog.org. Strictly ISO-only textbooks map over cleanly, since SWI is a superset for typical coursework.

Why do I need :- initialization(main) at the top?

Locally you often load a file into the SWI-Prolog REPL and type queries by hand. Here the file runs as a script, so the initialization directive tells the engine which goal to execute after consulting your clauses. Define a main predicate, put your queries inside it, and output with write or format.

How Prolog runs on XCODX

Sandbox filename
main.pl
Entry point
single main source file
Editor grammar
plain text — no highlighter ships for this language
Reading stdin
read/1
Input delivery
live WebSocket stream
Prompt flushing
flush manually before reading input
Compile limit
10 s
Run limit
3 s batch · up to 3 min live
Memory
256 MB per stage
Max source
50,000 characters

Default program on this page

:- initialization(main).

main :-
    write('Hello from Prolog!'), nl,
    write('Welcome to XCODX!'), nl,
    X is 5 + 3,
    format('5 + 3 = ~w~n', [X]),
    factorial(6, F),
    format('6! = ~w~n', [F]),
    halt.

factorial(0, 1).
factorial(N, F) :-
    N > 0,
    N1 is N - 1,
    factorial(N1, F1),
    F is N * F1.