Web Prolog manual

This document reproduces the predicate reference material from Appendix A in HTML form. Each predicate entry carries an HTML anchor using its predicate indicator so that entries can be linked directly.

Contents

Predicates for programming with core actors

Predicate: self/1
actor
self(-Pid) is det.

Binds Pid to the process identifier of the calling process.

Note: The runtime uses a global pid model. Conceptually, pids are of the form Id@Node, although some compatibility-oriented surfaces, especially certain JSON responses, may still expose local pids as plain integers.

Predicate: spawn/1-3
actor
spawn(+Goal) is det.
spawn(+Goal, -Pid) is det.
spawn(+Goal, -Pid, +Options) is det.

Creates a new Web Prolog actor process running Goal. Valid options are:

Note: In spawn(+Goal,-Pid,+Options), the option monitor(true) installs monitoring as part of process creation. When the child terminates, the parent receives a message down(Ref,Pid,Reason) where Pid is the terminated actor, Reason is its exit reason, and Ref identifies the monitor instance. For monitoring created via monitor(true), the current implementation uses Ref = Pid.

Note: The semantics of link(true) are directional: if a parent spawns a child with linking enabled, termination of the parent causes linked children to be terminated. The link does not imply symmetric bidirectional exit propagation.

Note: It is possible to pass an arbitrary number of the load_* options to spawn/3, and possibly more than one instance of each variant. To ensure that clauses end up in a well-defined order, they are converted into Prolog source text before being loaded into the database. The order of clauses and directives in the resulting source text is determined by the order of the load_* options in the option list.

Predicate: actors/1
actor
actors(-Pids) is det.

Binds Pids to the list of actor pids visible from the current execution context. In ordinary public client execution, this means the current actor or toplevel together with any actors that have been spawned on behalf of the same client interaction. Actors belonging to other clients are not included.

Note: Outside public client execution, for example in node-owned runtime code or administrative code, actors/1 returns the pids of all active local actors on the node.

Predicate: monitor/2
actor
monitor(+PidOrName, -Ref) is det.

Installs a monitor and returns a fresh reference in Ref. The first argument may be a pid or a registered local name. When the monitored process terminates, the monitoring process receives a message of the form down(Ref,Pid,Reason).

Predicate: demonitor/1-2
actor
demonitor(+Ref) is det.
demonitor(+Ref, +Options) is det.

Stops monitoring identified by Ref. This is idempotent. There is only one valid option:

Using monitor(true) in spawn/3 is the safest variant for short-lived children, since monitoring is established during spawn. Installing monitoring later with monitor/2 is a separate step and may miss a child that exits immediately.

Predicate: register/2
actor
register(+Name, +Pid) is det.

Register a local process under a name, where Name is an atom and Pid identifies the actor process. The association between the name and the pid is removed when the process terminates.

Predicate: whereis/2
actor
whereis(+Name, ?Pid) is det.

Determine the identity of the actor process associated with the name. Binds Pid to undefined if the process does not exist.

Predicate: unregister/1
actor
unregister(+Name) is det.

Remove the association between the name and the process.

Predicate: exit/1
actor
exit(+Reason) is det.

Exit the calling process with Reason.

Predicate: exit/2
actor
exit(+Pid, +Reason) is det.

Exit the process identified by Pid with Reason. For remote actors, the runtime routes this request through the remote node.

Predicate: !/2, send/2-3
actor
+PidOrName ! +Message is det.
send(+PidOrName, +Message) is det.
send(+PidOrName, +Message, +Options) is det.

Sends Message to the mailbox of the process identified as PidOrName. PidOrName may be a local pid, a local registered name, or a global pid of the form Id@Node. Valid options for send/3 are:

Predicate: cancel/1
actor
cancel(+ID) is det.

Tries to cancel the sending of all delayed messages with the specified ID. This is best-effort only, since a message may already have been sent by the time the call is made.

Predicate: output/1-2
actor
output(+Data) is det.
output(+Data, +Options) is det.

Sends a message output(Pid,Data) to the target process. Pid is the pid of the current process. Valid option:

Note that this is just a convenience predicate. A toplevel, like any other actor, may use !/2 to send any term to any process to which it has a pid.

Predicate: input/2-3
actor
input(+Prompt, -Data) is det.
input(+Prompt, -Data, +Options) is det.

Sends a message prompt(Pid,Prompt) to the target process and waits for its input. Prompt may be any non-variable term. Pid is the pid of the current process. Data will be bound to the term that the target process sends using respond/2. Valid option:

Predicate: respond/2
actor
respond(+Pid, +Input) is det.

Sends a response in the form of the term Input to a process that has prompted its parent process for input.

Predicate: receive/1-2
actor
receive(+Clauses) is semidet.
receive(+Clauses, +Options) is semidet.

Clauses is a sequence of receive clauses delimited by a semicolon:

{  Pattern1 [if Guard1] ->
         Body1 ;
   ...
   PatternN [if GuardN] ->
         BodyN
}

Each pattern in turn is matched against the first message in the mailbox. If a pattern matches and the corresponding guard succeeds, the matching message is removed from the mailbox and the body of the receive clause is called. If no message is accepted, the process waits for new messages, checking them one at a time in arrival order. Messages that are not accepted are deferred, i.e. left in the mailbox without any change in their contents or order. Valid options:

Predicate: flush/0
actor
flush is det.

Removes all messages currently pending in the mailbox of the calling process. Each removed message is rendered through the actor terminal-output path as a line of the form Shell got Message, where Message is the full Prolog term.

Note: flush/0 is primarily a shell-level inspection utility. It does not wait for future messages: once the mailbox is empty at the time of the call, it succeeds immediately.

Predicate: listing/0-1
isotope, actor
listing is det.
listing(+Pid) is det.

listing/0 lists the content of the private database of the current actor process through the terminal-output path. Imported predicates and the injected I/O wrapper predicates are omitted.

listing/1 lists the content of the private database of the local actor identified by Pid. In public execution contexts this is namespace-scoped, so a client may only inspect actors that belong to its own session namespace.

Predicates for programming with toplevel actors

Predicate: toplevel_spawn/1-2
actor
toplevel_spawn(-Pid) is det.
toplevel_spawn(-Pid, +Options) is det.

Spawns a toplevel actor and binds Pid to its pid. All options accepted by spawn/3 are also accepted by toplevel_spawn/2. In addition, toplevel_spawn/2 accepts the following options:

Predicate: toplevel_call/2-3
actor
toplevel_call(+Pid, +Goal) is det.
toplevel_call(+Pid, +Goal, +Options) is det.

Asks the toplevel Pid for solutions to Goal. Valid options are:

Variables in Goal are not bound directly in the caller. Instead, solutions and other kinds of output are returned in the form of answer messages delivered to the mailbox of the target process.

Predicate: toplevel_next/1-2
actor
toplevel_next(+Pid) is det.
toplevel_next(+Pid, +Options) is det.

Asks toplevel Pid for more solutions. Valid options:

The messages delivered to the target mailbox are the same as for toplevel_call/2-3.

Predicate: toplevel_stop/1
actor
toplevel_stop(+Pid) is det.

Asks toplevel Pid to stop searching for more solutions.

Predicate: toplevel_abort/1
actor
toplevel_abort(+Pid) is det.

Tells toplevel Pid to abort the execution of the currently running goal.

Predicate: toplevel_halt/2
actor
toplevel_halt(+Pid, -Reply) is det.

Sends '$halt' to an idle toplevel actor Pid, causing it to terminate from its idle state and acknowledge with Reply.

Predicates for programming with server actors

Predicate: server_spawn/3-4
actor
server_spawn(+Pred, +State, -Pid) is det.
server_spawn(+Pred, +State, -Pid, +Options) is det.

Spawns a generic server actor using callback predicate Pred/4 and initial state State. The callback receives (Request, OldState, Response, NewState).

All options are forwarded to spawn/3 except:

Predicate: server_request/3-4
actor
server_request(+To, +Request, -Response) is det.
server_request(+To, +Request, -Response, +Options) is det.

Makes a synchronous request-response call to the server To. server_request/4 accepts the options of receive/2, for example timeout(+Seconds).

Monitoring is installed automatically so the call fails fast if the server terminates before replying.

Predicate: server_promise/3-4
actor
server_promise(+To, +Request, -Ref) is det.
server_promise(+To, +Request, -Ref, -MonRef) is det.

Sends Request to the server To and returns a correlation reference Ref. The reply must later be collected with server_yield/2-4.

The four-argument variant additionally installs a monitor and returns its reference in MonRef.

Predicate: server_yield/2-4
actor
server_yield(+Ref, -Response) is det.
server_yield(+Ref, -Response, +Options) is det.
server_yield(+Ref, +MonRef, -Response, +Options) is det.

Waits for the response matching Ref. The three-argument variant accepts the options of receive/2.

The four-argument variant uses MonRef to detect server termination and throws server_down(Reason) if the server dies before replying.

Predicate: server_upgrade/2
actor
server_upgrade(+To, +Pred) is det.

Replaces the callback predicate of the running server To without disturbing its current state. Pred must be arity 4.

Predicate: server_halt/2
actor
server_halt(+To, -Reply) is det.

Asks the server To to stop gracefully and binds Reply to its acknowledgement.

Predicates for programming with statechart actors

Predicate: statechart_spawn/1-2
actor
statechart_spawn(-Pid) is det.
statechart_spawn(-Pid, +Options) is det.

Spawns a statechart actor and binds Pid to its pid.

statechart_spawn/2 requires exactly one source option:

In addition, statechart_spawn/2 accepts:

All remaining options are passed through to spawn/3.

load_list/1 and load_predicates/1 are rejected for statechart_spawn/2.

Predicate: statechart_halt/2-3
actor
statechart_halt(+Pid, -Reply) is det.
statechart_halt(+Pid, -Reply, +Timeout) is det.

Asks the statechart actor Pid to halt gracefully and waits for an acknowledgement in Reply.

The three-argument form waits at most Timeout seconds. If the actor does not reply in time, it is killed and Reply becomes killed.

Predicate: raise/1
statechart builtin
raise(+Event) is det.

Enqueues Event on the internal event queue of the current statechart interpreter.

Important: raise/1 is only meaningful inside executable statechart content such as <datamodel>, <onentry>, <onexit>, and <go>.

Predicates for programming with supervisor actors

Predicate: supervisor_spawn/2-3
actor
supervisor_spawn(+ChildSpecs, -Pid) is det.
supervisor_spawn(+ChildSpecs, -Pid, +Options) is det.

Spawns a supervisor actor and starts its children. Supported options are:

Other options are passed through to spawn/3.

Child specifications take the form child(Id, ChildOptions). Child options are:

Predicate: supervisor_spawn_child/3
actor
supervisor_spawn_child(+Pid, +ChildSpec, -Reply) is det.

Dynamically adds and starts one child. Reply is one of ok, error(already_present), or error(start_failed).

Predicate: supervisor_terminate_child/3
actor
supervisor_terminate_child(+Pid, +Id, -Reply) is det.

Stops child Id but keeps its specification. Reply is ok or error(not_found).

Predicate: supervisor_delete_child/3
actor
supervisor_delete_child(+Pid, +Id, -Reply) is det.

Removes a non-running child specification. Reply is ok, error(running), or error(not_found).

Predicate: supervisor_respawn_child/3
actor
supervisor_respawn_child(+Pid, +Id, -Reply) is det.

Restarts a previously terminated child from its stored specification. Reply is either ok(NewPid), error(running), error(not_found), or error(start_failed).

Predicate: supervisor_which_children/2
actor
supervisor_which_children(+Pid, -Children) is det.

Returns a list of:

info(Id, Pid, Type, Restart)

where Pid may be undefined for stopped children.

Predicate: supervisor_count_children/2
actor
supervisor_count_children(+Pid, -Counts) is det.

Returns:

[specs-N, active-N, supervisors-N, workers-N]
Predicate: supervisor_halt/1
actor
supervisor_halt(+Pid) is det.

Stops the supervisor and shuts down children in reverse start order.

Note on synchronous supervisor calls

The dynamic/query API above uses monitored synchronous calls and may throw:

Built-in predicates for RPC

Predicate: rpc/2-3
isobase
rpc(+URI, +Goal) is nondet.
rpc(+URI, +Goal, +Options) is nondet.

Semantically, rpc/2-3 executes a copy of Goal on the remote node identified by URI and then unifies the local goal with the remote copy on backtracking. The current implementation uses the stateless /call endpoint. This matters: even if the remote node happens to be deployed as an ACTOR node, a call made through rpc/2-3 still runs against the node's stateless /call surface rather than its actor-facing APIs. Actor-only predicates such as self/1, spawn/1-3, and receive/1-2 are therefore not available through rpc/2-3. The following options are valid:

http_timeout/1 controls only the client-side HTTP transport timeout. It does not change the remote execution timeout.

Predicate: promise/3-4
isobase
promise(+URI, +Goal, -Ref) is det.
promise(+URI, +Goal, -Ref, +Options) is det.

Makes an asynchronous RPC call to node URI with Goal. The current implementation supports the following options:

The reference returned in Ref can later be used by yield/2-3 to collect the answer.

Predicate: yield/2-3
isobase
yield(+Ref, ?Answer) is det.
yield(+Ref, ?Answer, +Options) is det.

Returns the promised answer from a previous call to promise/3-4. This predicate must be called by the same process from which the previous call to promise/3-4 was made. Valid options:

ISO predicates and related constructs

The following ISO Prolog predicates and related constructs are available to clients of a Web Prolog node. Predicates that require direct stream or file access, module loading, runtime reflection, or parser state mutation are excluded by the sandbox.

Control Constructs

true/0, fail/0, call/1-8, !/0, (,)/2, (;)/2, (->)/2, (\+)/1, catch/3, throw/1

Directives

dynamic/1, multifile/1, discontiguous/1

Term Unification

(=)/2, (\=)/2, unify_with_occurs_check/2

Arithmetic Evaluation and Comparison

is/2, (=:=)/2, (=\=)/2, (<)/2, (>)/2, (>=)/2, (=<)/2

Arithmetic Functions

(+)/1-2, (-)/1-2, (*)/2, (//)/2, (/)/2, rem/2, mod/2, abs/1, sign/1, float_integer_part/1, float_fractional_part/1, float/1, truncate/1, round/1, ceiling/1, floor/1, max/2, min/2, (**)/2, sin/1, cos/1, atan/1, atan2/2, exp/1, log/1, sqrt/1, (>>)/2, (<<)/2, (/\)/2, (\/)/2, (\)/1, xor/2, pi/0

Term Comparison

(@<)/2, (@>)/2, (@>=)/2, (@=<)/2, compare/3

Type Testing

var/1, nonvar/1, atom/1, number/1, integer/1, float/1, compound/1, atomic/1, callable/1, ground/1

Term Creation and Decomposition

functor/3, arg/3, (=..)/2, copy_term/2

Clause Retrieval

clause/2

Clause Creation and Destruction

asserta/1, assertz/1, retract/1, abolish/1

All-Solutions

findall/3, bagof/3, setof/3

Actor I/O (Overrides)

The ambient stream I/O predicates of ISO Prolog are blacklisted. In their place, the actor runtime prelude injects local overrides that route output through the actor messaging layer:

nl/0, write/1, writeq/1, write_term/2, writeln/1, write_canonical/1, print/1, display/1, format/1-2

Atomic Term Processing

atom_length/2, atom_concat/3, atom_chars/2, atom_codes/2, sub_atom/5, char_code/2, number_chars/2, number_codes/2