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 is the best-known logic programming language, created by Alain Colmerauer and his colleagues in Marseille in 1972 on theoretical foundations laid by Robert Kowalski. Instead of describing steps to execute, you state facts and rules, then pose queries that the engine answers through unification and backtracking. XCODX runs the widely used SWI-Prolog implementation, maintained by Jan Wielemaker, which is known for its robustness and rich standard library. Programs are declarative: you define relationships once and let the solver search for values that satisfy them. In this cloud sandbox you can load and query a Prolog program from the browser without installing SWI-Prolog locally.

Hello World in Prolog

:- initialization(main).

parent(tom, bob).
parent(bob, ann).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).

main :-
    ( grandparent(tom, Who)
    -> format("tom is the grandparent of ~w~n", [Who])
    ;  writeln("no grandparent found")
    ),
    halt.

When to use Prolog

Prolog is at its best for problems that are naturally expressed as facts, rules, and search: symbolic reasoning, parsing, expert systems, and constraint puzzles. Its built-in unification and backtracking let you write concise solutions to combinatorial problems, and SWI-Prolog's clpfd library adds constraint solving over integers. Choose it when a declarative, relational description of the problem is clearer than an imperative algorithm. It is a poor fit for number-crunching, tight performance loops, or conventional stateful application code, where imperative languages are more direct.

Common questions

How do I run a Prolog program, and where does execution start?

Your whole program lives in one file that the sandbox consults (loads) automatically. Add a directive like :- initialization(main). and define a main/0 predicate as the entry point; that goal runs once the file has loaded. There is no separate consult of external files, since the program is a single self-contained source.

How do I read input in SWI-Prolog?

Use read_line_to_string(user_input, Line) to grab a whole line as a string, or read(Term) to read one Prolog term ending in a period. On XCODX you can type into the live terminal, which pauses until you press Enter, or paste into the Stdin Box beforehand; Ctrl+D closes the stream and yields end_of_file.

Can I install packs with pack_install?

No. pack_install needs network access, which the sandbox does not have, so external packs are unavailable. Libraries that ship with SWI-Prolog itself — such as library(lists), library(apply), and library(clpfd) — still load normally with use_module.

How do I print output in Prolog?

Use write/1 or writeln/1 for terms, and format/2 for templated output, for example format("~w~n", [X]). nl/0 prints a newline. Remember that a query which simply succeeds prints nothing on its own — you have to write the result explicitly.

How do I stop the program or set an exit code?

Call halt/0 to end immediately, or halt/1 with an integer to set the process exit code. Ending main with halt is a common way to avoid dropping into an interactive top level after your goal finishes.

Is clpfd constraint solving available?

Yes. library(clpfd) is bundled with SWI-Prolog, so :- use_module(library(clpfd)). works for constraint logic programming over integers. Only third-party packs from the web are out of reach; the libraries distributed with SWI-Prolog are present.

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.