Prolog online compiler
Write SWI-Prolog 10 code below and run it free, with no sign-in and no ads. Your facts and rules load the way consult/1 loads a file, a :- initialization(main). directive starts the program, and queries or data come from the Input tab. The same compiler is an API and an embeddable editor, for running Prolog in your own app, course or docs.
Também em portuguêsTambién en español
Run Prolog from your own app, bot or course. One API call returns the output, errors, exit code and run time. Free up to 100,000 runs a month, commercial use included, no card.
Checking a solution against several inputs? The Prolog online compiler with test cases runs them all at once and marks each one passed or failed.
Run SWI-Prolog queries online
Without the SWI-Prolog console, a program runs from main. This one loads a family tree, then answers each query in the Input tab the way the console does: every solution on its own line, or false when there is none.
:- initialization(main).
parent(tom, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
% Runs each query in the Input tab, the way the SWI-Prolog console would.
main :-
read_term(user_input, Query, [variable_names(Names)]),
( Query == end_of_file
-> halt
; format("?- ~W.~n", [Query, [variable_names(Names)]]),
( \+ call(Query)
-> writeln(false)
; forall(call(Query), answer(Names))
),
main
).
answer([]) :- writeln(true).
answer([Name=Value|Rest]) :-
findall(Text, ( member(N=V, [Name=Value|Rest]),
format(string(Text), "~w = ~q", [N, V]) ), Texts),
atomic_list_concat(Texts, ", ", Line),
writeln(Line).With these queries in the Input tab, one per line, each ending in a full stop:
grandparent(tom, Who).
ancestor(Who, jim).
parent(ann, Child).
ancestor(tom, jim).it prints:
?- grandparent(tom,Who).
Who = ann
Who = pat
?- ancestor(Who,jim).
Who = pat
Who = tom
Who = bob
?- parent(ann,Child).
false
?- ancestor(tom,jim).
trueConstraint programming works too. library(clpfd) solves SEND + MORE = MONEY, where each letter is a different digit:
:- use_module(library(clpfd)).
:- initialization(main).
% SEND + MORE = MONEY: each letter is a different digit.
puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :-
Vars = [S,E,N,D,M,O,R,Y],
Vars ins 0..9,
all_different(Vars),
S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E #=
M*10000 + O*1000 + N*100 + E*10 + Y,
M #\= 0, S #\= 0,
label(Vars).
main :-
puzzle(Answer),
print(Answer), nl,
halt.It prints [9,5,6,7]+[1,0,8,5]=[1,0,6,5,2].
What runs your code
| Version | Prolog SWI-Prolog 10 |
|---|---|
| Compiled with | No separate step: the run command compiles or interprets it |
| Run with | swipl -f /runcode/init.pl --on-error=status -q -t runcode_halt code.pl |
| Limits | 30 seconds, 512 MB of memory (up to 2 GB through the API on Pro and above), 2 CPUs, 64 KB of output per stream, no network |
| Typical run | 0.62 s, and 0.7 s for the slowest 1 in 20 (how we measured) |
Good to know
- Runs with SWI-Prolog 10. A program runs from an
:- initialization(main).directive, as on GNU Prolog, and the run ends when it finishes or callshalt. read_line_to_string(user_input, Line)reads the next line of the Input tab;read(X)reads a term, which must end with a full stop.- Double-quoted text is a string, as SWI-Prolog has it, not a list of codes as on GNU Prolog. Warnings, such as singleton variables, go to stderr; an error, or a
mainthat fails, makes the exit code 1.
Prolog compiler API
To run Prolog from your own app, send the code and its input to the sync endpoint with an API key, and the output comes back in the response. It's free up to 100,000 runs a month, with no credit card: get a free API key. The API docs cover the async endpoint with callbacks and every response field.
curl -X POST https://api.onlinecompiler.io/api/run-code-sync/ \
-H "Authorization: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"compiler": "swipl-10",
"code": ":- initialization(main).\n\nmain :-\n read_line_to_string(user_input, Name),\n format(\"Hello, ~w!~n\", [Name]),\n halt.",
"input": "Ada"
}'The response has output ("Hello, Ada!\n" here), error, exit_code, time and memory.
Embed a Prolog compiler in your website
The editor above is the embeddable widget. Add the script and a div with your widget key, and your readers can edit and run the code without an account:
<script src="https://onlinecompiler.io/widget.js"></script>
<div class="runcode" data-key="YOUR_WIDGET_KEY" data-lang="swipl-10">
<script type="text/plain">
:- initialization(main).
main :-
read_line_to_string(user_input, Name),
format("Hello, ~w!~n", [Name]),
halt.
</script>
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>On a platform that takes an iframe but not a script, such as many course sites, use the iframe embed. For Docusaurus, MkDocs, Sphinx and Mintlify, see runnable code examples in docs.
Other languages
Python, C, C++, Java, C#, F#, Go, Rust, PHP, Ruby, Haskell, TypeScript, JavaScript, SQL, DuckDB, Bash, Fortran, Visual Basic, Kotlin, R, Swift, COBOL, Perl, Julia, Pascal, Octave, Assembly, Lua, OCaml, Common Lisp, Erlang, Elixir, D, FreeBASIC, Dart, Clojure, Groovy, Scala, Zig, Nim, Crystal, Ada, PowerShell, Racket, Objective-C.
Frequently asked questions
Is this Prolog online compiler free?
Yes. Running Prolog on this page needs no account and shows no ads. To run Prolog from your own app or site, the API and the widget are free up to 100,000 runs a month, commercial use included.
Is there a Prolog compiler API?
Yes. POST the code, the compiler id swipl-10 and any input to https://api.onlinecompiler.io/api/run-code-sync/ with an API key, and the response holds the output, errors, exit code, run time and memory. It's free up to 100,000 runs a month, and the same key works for the other 45 languages.
Can my Prolog program read input?
Yes. Type it in the Input tab before you run, and the program reads it with read_term(user_input, X, []) for a term ending in a full stop, or read_line_to_string(user_input, Line) for a line. The whole input is sent when you press Run, so a program can't ask for more while it runs.
Can I use Prolog libraries?
SWI-Prolog's bundled libraries, such as lists, apply, readutil and clpfd. Packs can't be installed, and programs have no network access.
How do I run a query, like ?- in SWI-Prolog?
There is no interactive console: the whole Input tab is sent when you press Run. Write your queries in the Input tab, one per line ending with a full stop, and use the query runner above, which prints every answer; or call the goal from main, for example with forall(ancestor(tom, X), (print(X), nl)).
Why does my Prolog program print nothing?
Because no goal ran. Your facts and rules are loaded when you press Run, but there is no ?- prompt to type a query into, so a file of facts alone prints nothing and exits with code 0. Add :- initialization(main). and a main that runs your goal and prints the answer, or put your queries in the Input tab and use the query runner above.
Does GNU Prolog code run here?
Usually, yes. The main difference is that double-quoted text is a string in SWI-Prolog, not a list of character codes. Put :- set_prolog_flag(double_quotes, codes). at the top of the program to get GNU Prolog's behaviour.
Does clpfd work?
Yes. Load it with :- use_module(library(clpfd)). and constraints such as ins, #=, all_different and label work, as in the SEND + MORE example above.
Can I share or save my code?
Yes. Share gives you a link that anyone can open and run, lasts 90 days and needs no account. Save keeps the code in your OnlineCompiler.io account until you delete it. When you are signed in, each run you make on this page is also kept in your account's run history.
How long can a program run?
Up to 30 seconds, with 512 MB of memory (up to 2 GB through the API on Pro and above) and 2 CPUs. A program still running at 30 seconds is stopped with exit code 124, and output over 64 KB per stream is cut.
Where does my code run?
On our servers, in a fresh gVisor sandbox with no network access that is deleted when the run ends. Each run is kept in execution history with its output; the security page says what is stored.