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.
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.
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.
:- 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.
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.
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.
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.
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.
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.
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.
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.
main.plplain text — no highlighter ships for this languageread/1:- 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.