13.1.1 Loading Prolog files
The WASM build ships with the Prolog library and thus Prolog
libraries can be loaded as normal using use_module/1,
etc., for example, we can include the lists
library using
this directive. Note that the normal autoloading of library
code works in the WASM version.
:- use_module(library(lists)).
When Prolog is in asynchronous mode, i.e., called through
Prolog.forEach(),
we can also load code from a URL. For example, we can load the CHAT80
demo program directly from
GitHub
using240The
c
continues the quoted atom from the next line after removing leading
white space.
\
?- consult('https://raw.githubusercontent.com/JanWielemaker/\c chat80/master/prolog/chat80.pl').
Larger files can be loaded as .qlf
files. See section
4.3.3 and
qcompile/2.
Notably we can create a single qlf file from an application using the include(user)
option. Below we create a .qlf
file from CHAT80.
The resulting chat80.qlf
can be loaded from a URL using consult/1
as above.
?- qcompile('chat80.pl', [include(user)]).
There are three ways to load Prolog code from JavaScript: (1) loading
from a string, (2) loading from <script>
elements and
(3) loading from URL. Note that all the loading methods return a Promise
that is resolved when loading the data is completed.
- Promise Prolog.load_string(String, Id)
- Load Prolog code from String, pretending it was loaded from
the file Id. The Id is optional. When omitted it
generates
/string/1
,/string/2
, ... . - Promise Prolog.load_scripts()
- Load all scripts from the current docement that have their
type
set totext/prolog
. The file reference for the loaded script is/script/Id
, where Id is derived from (1) theid
of the script, (2) thename
of the script or (3) being the nth Prolog script in the document. When resolved, the promise returns an array with the names of the loaded scripts. - Promise Prolog.consult(...Sources)
- Load the given Sources. Each source is either a file from the
local file system, e.g.,
library(lists)
or a URL. The sources are downloaded and processed sequentially. This uses Prolog.forEach() calling load_files/1. The returnedPromise
returns 1 on success.