Prolog in WebAssembly

Prolog is a logic programming language with several different implementations. Trealla Prolog is the one most frequently associated with WebAssembly.

Available Implementations


The easiest way to use Trealla Prolog is with the Spin plugin. It will manage the Prolog runtime for you.

Install the Spin plugin:

$ spin templates install --git --update

Otherwise, you can follow the installation and build instructions on the Trealla Prolog GitHub page.

Pros and Cons

Things we like:


Create a new app:

$ spin new http-prolog hello-prolog --accept-defaults

Run the app:

$ cd hello-prolog
$ spin up

Logging component stdio to ".spin/logs/"
Storing default key-value data to ".spin/sqlite_key_value.db"

Available Routes:
  hello-prolog: (wildcard)

Doing spin up fetches the prolog interpreter, so there is no spin build step.

The scaffolded code looks like this:

:- use_module(library(spin)).

% See library/ for all the predicates built-in

%% http_handler(+Spec, +Headers, +Body, -Status)

http_handler(get("/", _QueryParams), _RequestHeaders, _RequestBody, 200) :-
		store_open(default, Store),
			(  store_get(Store, counter, N0)
			-> true
			;  N0 = 0
			succ(N0, N),
			store_set(Store, counter, N)
	http_header_set("x-powered-by", "memes"),
	current_prolog_flag(dialect, Dialect),
	% stream alias http_body is the response body
	write(http_body, '<!doctype html><html>'),
	format(http_body, "<h1>Hello, ~a prolog!</h1>", [Dialect]),
	format(http_body, "Welcome, visitor #<b>~d!</b>", [N]),
	write(http_body, '</html>').

http_handler(get("/json", _), _, _, 200) :-
	% json_content({"time": Time}) works too

Learn More

Here are some great resources: