1\chapter{Foreign Language Interface} \label{sec:foreign} 2 3\newlength{\tableft} 4\settowidth{\tableft}{\const{PL_QUERY_ORGSYMBOLFILE}} 5 6SWI-Prolog offers a powerful interface to C \cite{Kernighan:78}. The 7main design objectives of the foreign language interface are flexibility 8and performance. A foreign predicate is a C function that has the same 9number of arguments as the predicate represented. C functions are 10provided to analyse the passed terms, convert them to basic C types as 11well as to instantiate arguments using unification. Non-deterministic 12foreign predicates are supported, providing the foreign function with a 13handle to control backtracking. 14 15C can call Prolog predicates, providing both a query interface and 16an interface to extract multiple solutions from a non-deterministic 17Prolog predicate. There is no limit to the nesting of Prolog calling 18C, calling Prolog, etc. It is also possible to write the `main' in 19C and use Prolog as an embedded logical engine. 20 21 22\section{Overview of the Interface} \label{sec:foreignoverview} 23 24A special include file called \file{SWI-Prolog.h} should be included 25with each C source file that is to be loaded via the foreign interface. 26The installation process installs this file in the directory 27\file{include} in the SWI-Prolog home directory (\exam{?- 28current_prolog_flag(home, Home).}). This C header file defines various 29data types, macros and functions that can be used to communicate with 30SWI-Prolog. Functions and macros can be divided into the following 31categories: 32 33\begin{shortlist} 34 \item Analysing Prolog terms 35 \item Constructing new terms 36 \item Unifying terms 37 \item Returning control information to Prolog 38 \item Registering foreign predicates with Prolog 39 \item Calling Prolog from C 40 \item Recorded database interactions 41 \item Global actions on Prolog (halt, break, abort, etc.) 42\end{shortlist} 43 44 45\section{Linking Foreign Modules} \label{sec:foreignlink} 46 47Foreign modules may be linked to Prolog in two ways. Using 48\jargon{static linking}, the extensions, a (short) file defining main() 49which attaches the extension calls to Prolog, and the SWI-Prolog kernel 50distributed as a C library, are linked together to form a new executable. 51Using \jargon{dynamic linking}, the extensions are linked to a shared 52library (\fileext{so} file on most Unix systems) or dynamic link library 53(\fileext{DLL} file on Microsoft platforms) and loaded into the 54running Prolog process.% 55 \footnote{The system also contains code to load \fileext{o} files 56 directly for some operating systems, notably Unix systems 57 using the BSD \file{a.out} executable format. As the number of 58 Unix platforms supporting this quickly gets smaller and 59 this interface is difficult to port and slow, it is no 60 longer described in this manual. The best alternative 61 would be to use the \idx{dld} package on machines that do 62 not have shared libraries.} 63 64\subsection{What linking is provided?} 65\label{sec:foreign-linking} 66 67The \jargon{static linking} schema can be used on all versions of 68SWI-Prolog. Whether or not dynamic linking is supported can be deduced 69from the Prolog flag \prologflag{open_shared_object} (see 70current_prolog_flag/2). If this Prolog flag yields \const{true}, 71open_shared_object/2 and related predicates are defined. See 72\secref{shlib} for a suitable high-level interface to these predicates. 73 74\subsection{What kind of loading should I be using?} 75\label{sec:foreign-linking-options} 76 77All described approaches have their advantages and disadvantages. Static 78linking is portable and allows for debugging on all platforms. It is 79relatively cumbersome and the libraries you need to pass to the linker 80may vary from system to system, though the utility program 81\program{swipl-ld} described in \secref{plld} often hides these problems 82from the user. 83 84Loading shared objects (DLL files on Windows) provides sharing and 85protection and is generally the best choice. If a saved state is created 86using qsave_program/[1,2], an initialization/1 directive may be used to 87load the appropriate library at startup. 88 89Note that the definition of the foreign predicates is the same, regardless 90of the linking type used. 91 92\input{lib/shlib} 93 94\subsection{Low-level operations on shared libraries} 95\label{sec:sharedobj} 96 97The interface defined in this section allows the user to load shared 98libraries (\fileext{so} files on most Unix systems, \fileext{dll} files 99on Windows). This interface is portable to Windows as well as to Unix 100machines providing \manref{dlopen}{2} (Solaris, Linux, FreeBSD, Irix and 101many more) or \manref{shl_open}{2} (HP/UX). It is advised to use the 102predicates from \secref{shlib} in your application. 103 104 105\begin{description} 106 \predicate{open_shared_object}{2}{+File, -Handle} 107\arg{File} is the name of a shared object file (DLL in MS-Windows). This file is attached to the current process, and 108\arg{Handle} is unified with a handle to the library. Equivalent to 109\exam{open_shared_object(File, Handle, [])}. See also 110open_shared_object/3 and load_foreign_library/1. 111 112On errors, an exception \term{shared_object}{Action, Message} is 113raised. \arg{Message} is the return value from dlerror(). 114 115 \predicate{open_shared_object}{3}{+File, -Handle, +Options} 116As open_shared_object/2, but allows for additional flags to be passed. 117\arg{Options} is a list of atoms. \const{now} implies the symbols are 118resolved immediately rather than lazy (default). \const{global} implies 119symbols of the loaded object are visible while loading other shared 120objects (by default they are local). Note that these flags may not be 121supported by your operating system. Check the documentation of dlopen() 122or equivalent on your operating system. Unsupported flags are silently 123ignored. 124 125 \predicate{close_shared_object}{1}{+Handle} 126Detach the shared object identified by \arg{Handle}. 127 128 \predicate{call_shared_object_function}{2}{+Handle, +Function} 129Call the named function in the loaded shared library. The function 130is called without arguments and the return value is ignored. Normally 131this function installs foreign language predicates using calls to 132PL_register_foreign(). 133\end{description} 134 135 136\subsection{Static Linking} \label{sec:staticl} 137 138Below is an outline of the file structure required for statically 139linking SWI-Prolog with foreign extensions. \file{.../swipl} refers to 140the SWI-Prolog home directory (see the Prolog flag \prologflag{home}). 141\file{<arch>} refers to the architecture identifier that may be obtained 142using the Prolog flag \prologflag{arch}. 143 144\begin{center} 145\begin{tabular}{ll} 146\file{.../swipl/runtime/<arch>/libswipl.a} & SWI-Library \\ 147\file{.../swipl/include/SWI-Prolog.h} & Include file \\ 148\file{.../swipl/include/SWI-Stream.h} & Stream I/O include file \\ 149\file{.../swipl/include/SWI-Exports} & Export declarations (AIX only) \\ 150\file{.../swipl/include/stub.c} & Extension stub 151\end{tabular} 152\end{center} 153 154The definition of the foreign predicates is the same as for dynamic 155linking. Unlike with dynamic linking, however, there is no 156initialisation function. Instead, the file \file{.../swipl/include/stub.c} 157may be copied to your project and modified to define the foreign 158extensions. Below is \file{stub.c}, modified to link the lowercase example 159described later in this chapter: 160 161\begin{code} 162#include <stdio.h> 163#include <SWI-Prolog.h> 164 165extern foreign_t pl_lowercase(term, term); 166 167PL_extension predicates[] = 168{ 169/*{ "name", arity, function, PL_FA_<flags> },*/ 170 171 { "lowercase", 2 pl_lowercase, 0 }, 172 { NULL, 0, NULL, 0 } /* terminating line */ 173}; 174 175 176int 177main(int argc, char **argv) 178{ PL_register_extensions(predicates); 179 180 if ( !PL_initialise(argc, argv) ) 181 PL_halt(1); 182 183 PL_halt(PL_toplevel() ? 0 : 1); 184} 185\end{code} 186 187Now, a new executable may be created by compiling this file and linking 188it to \file{libpl.a} from the runtime directory and the libraries 189required by both the extensions and the SWI-Prolog kernel. This may be 190done by hand, or by using the \program{swipl-ld} utility described in 191\secref{plld}. If the linking is performed by hand, the command line 192option \cmdlineoption{--dump-runtime-variables} (see \secref{cmdline}) 193can be used to obtain the required paths, libraries and linking options 194to link the new executable. 195 196\section{Interface Data Types} \label{sec:foreigntypes} 197 198\subsection{Type \ctype{term_t}: a reference to a Prolog term} 199\label{sec:type-term-t} 200 201The principal data type is \ctype{term_t}. Type \ctype{term_t} is what 202Quintus calls \ctype{QP_term_ref}. This name indicates better what the 203type represents: it is a \jargon{handle} for a term rather than the term 204itself. Terms can only be represented and manipulated using this type, 205as this is the only safe way to ensure the Prolog kernel is aware of all 206terms referenced by foreign code and thus allows the kernel to perform 207garbage collection and/or stack-shifts while foreign code is active, 208for example during a callback from C. 209 210A term reference is a C unsigned long, representing the offset of a 211variable on the Prolog environment stack. A foreign function is passed 212term references for the predicate arguments, one for each argument. If 213references for intermediate results are needed, such references may be 214created using PL_new_term_ref() or PL_new_term_refs(). These references 215normally live till the foreign function returns control back to Prolog. 216Their scope can be explicitly limited using PL_open_foreign_frame() 217and PL_close_foreign_frame()/PL_discard_foreign_frame(). 218 219A \ctype{term_t} always refers to a valid Prolog term (variable, atom, integer, 220float or compound term). A term lives either until backtracking takes us 221back to a point before the term was created, the garbage collector has 222collected the term, or the term was created after a 223PL_open_foreign_frame() and PL_discard_foreign_frame() has been called. 224 225The foreign interface functions can either {\em read}, {\em unify} or 226{\em write} to term references. In this document we use the 227following notation for arguments of type \ctype{term_t}: 228 229\begin{quote} 230\begin{tabular}{lp{4in}} 231\tt term_t +t & Accessed in read-mode. The `+' indicates the 232 argument is `input'. \\ 233\tt term_t -t & Accessed in write-mode. \\ 234\tt term_t ?t & Accessed in unify-mode. \\ 235\end{tabular} 236\end{quote} 237 238\textbf{WARNING} Term references that are accessed in `write' (-) mode 239will refer to an invalid term if the term is allocated on the global 240stack and backtracking takes us back to a point before the term was 241written.\footnote{This could have been avoided by \jargon{trailing} term 242references when data is written to them. This seriously hurts 243performance in some scenarios though. If this is desired, use 244PL_put_variable() followed by one of the PL_unify_*() functions.} 245Compounds, large integers, floats and strings are all allocated on the 246global stack. Below is a typical scenario where this may happen. The 247first solution writes a term extracted from the solution into \arg{a}. 248After the system backtracks due to PL_next_solution(), \arg{a} becomes a 249reference to a term that no longer exists. 250 251\begin{code} 252term_t a = PL_new_term_ref(); 253... 254query = PL_open_query(...); 255while(PL_next_solution(query)) 256{ PL_get_arg(i, ..., a); 257} 258PL_close_query(query); 259\end{code} 260 261There are two solutions to this problem. One is to scope the term 262reference using PL_open_foreign_frame() and PL_close_foreign_frame() and 263makes sure it goes out of scope before backtracking happens. The other 264is to clear the term reference using PL_put_variable() before 265backtracking. 266 267Term references are obtained in any of the following ways: 268 269\begin{itemlist} 270\item [Passed as argument] 271 The C functions implementing foreign predicates are passed their 272 arguments as term references. These references may be read or 273 unified. Writing to these variables causes undefined behaviour. 274\item [Created by PL_new_term_ref()] 275 A term created by PL_new_term_ref() is normally used to build 276 temporary terms or to be written by one of the interface functions. 277 For example, PL_get_arg() writes a reference to the term argument 278 in its last argument. 279\item [Created by PL_new_term_refs(int n)] 280 This function returns a set of term references with the same characteristics 281 as PL_new_term_ref(). See PL_open_query(). 282\item [Created by PL_copy_term_ref(term_t t)] 283 Creates a new term reference to the same term as the argument. The 284 term may be written to. See \figref{pl-display}. 285\end{itemlist} 286 287Term references can safely be copied to other C variables of type 288\ctype{term_t}, but all copies will always refer to the same term. 289 290\begin{description} 291\cfunction{term_t}{PL_new_term_ref}{} 292Return a fresh reference to a term. The reference is allocated on the 293\jargon{local} stack. Allocating a term reference may trigger a stack-shift 294on machines that cannot use sparse memory management for allocation of the 295Prolog stacks. The returned reference describes a variable. 296\cfunction{term_t}{PL_new_term_refs}{int n} 297Return \arg{n} new term references. The first term reference is returned. 298The others are $\arg{t}+1$, $\arg{t}+2$, etc. There are two reasons 299for using this function. PL_open_query() expects the arguments as a set 300of consecutive term references, and {\em very} time-critical code requiring 301a number of term references can be written as: 302 303\begin{code} 304pl_mypredicate(term_t a0, term_t a1) 305{ term_t t0 = PL_new_term_refs(2); 306 term_t t1 = t0+1; 307 308 ... 309} 310\end{code} 311\cfunction{term_t}{PL_copy_term_ref}{term_t from} 312Create a new term reference and make it point initially to the same 313term as \arg{from}. This function is commonly used to copy a predicate 314argument to a term reference that may be written. 315\cfunction{void}{PL_reset_term_refs}{term_t after} 316Destroy all term references that have been created after \arg{after}, 317including \arg{after} itself. Any reference to the invalidated term 318references after this call results in undefined behaviour. 319 320Note that returning from the foreign context to Prolog will reclaim 321all references used in the foreign context. This call is only necessary 322if references are created inside a loop that never exits back to Prolog. 323See also PL_open_foreign_frame(), PL_close_foreign_frame() and 324PL_discard_foreign_frame(). 325\end{description} 326 327 328\subsubsection{Interaction with the garbage collector and stack-shifter} 329\label{sec:foreign-gc} 330 331Prolog implements two mechanisms for avoiding stack overflow: garbage 332collection and stack expansion. On machines that allow for it, Prolog 333will use virtual memory management to detect stack overflow and expand 334the runtime stacks. On other machines Prolog will reallocate the stacks 335and update all pointers to them. To do so, Prolog needs to know which 336data is referenced by C code. As all Prolog data known by C is 337referenced through term references (\ctype{term_t}), Prolog has all the 338information necessary to perform its memory management without 339special precautions from the C programmer. 340 341 342\subsection{Other foreign interface types} 343\label{sec:foreign-types} 344 345\begin{description} 346 \item[atom_t] 347An atom in Prolog's internal representation. Atoms are pointers to an 348opaque structure. They are a unique representation for represented 349text, which implies that atom $A$ represents the same text as 350atom $B$ if and only if $A$ and $B$ are the same pointer. 351 352Atoms are the central representation for textual constants in Prolog. 353The transformation of a character string $C$ to an atom implies a 354hash-table lookup. If the same atom is needed often, it is advised 355to store its reference in a global variable to avoid repeated lookup. 356 \item[functor_t] 357A functor is the internal representation of a name/arity pair. They are 358used to find the name and arity of a compound term as well as to 359construct new compound terms. Like atoms they live for the whole Prolog 360session and are unique. 361 \item[predicate_t] 362Handle to a Prolog predicate. Predicate handles live forever (although 363they can lose their definition). 364 \item[qid_t] 365Query identifier. Used by 366PL_open_query(), PL_next_solution() and PL_close_query() to handle 367backtracking from C. 368 \item[fid_t] 369Frame identifier. Used by 370PL_open_foreign_frame() and PL_close_foreign_frame(). 371 \item[module_t] 372A module is a unique handle to a Prolog module. Modules are used only 373to call predicates in a specific module. 374 \item[foreign_t] 375Return type for a C function implementing a Prolog predicate. 376 \item[control_t] 377Passed as additional argument to non-deterministic foreign functions. 378See PL_retry*() and PL_foreign_context*(). 379 \item[install_t] 380Type for the install() and uninstall() functions of shared 381or dynamic link libraries. See \secref{shlib}. 382 \item[int64_t] 383Actually part of the C99 standard rather than Prolog. As of version 3845.5.6, Prolog integers are 64-bit on all hardware. The C99 type 385\ctype{int64_t} is defined in the \file{stdint.h} standard header and 386provides platform-independent 64-bit integers. Portable code accessing 387Prolog should use this type to exchange integer values. Please note that 388PL_get_long() can return \const{FALSE} on Prolog integers that cannot be 389represented as a C long. Robust code should not assume any of the 390integer fetching functions to succeed, \emph{even} if the Prolog term is known 391to be an integer. 392\end{description} 393 394\subsubsection{PL_ARITY_AS_SIZE} 395\label{sec:pl-arity-as-size} 396 397As of SWI-Prolog 7.3.12, the arity of terms has changed from \ctype{int} 398to \ctype{size_t}. To deal with this transition, all affecting functions 399have two versions, where the old name exchanges the arity as \ctype{int} 400and a new function with name *_sz() exchanges the arity as 401\ctype{size_t}. Op to 8.1.28, the default was to use the old \ctype{int} 402functions. As of 8.1.29/8.2.x, the default is to use \ctype{size_t} and 403the old behaviour can be restored by defining \const{PL_ARITY_AS_SIZE} 404to \const{0} (zero). This makes old code compatible, but the following 405warning is printed when compiling: 406 407\begin{code} 408#warning "Term arity has changed from int to size_t." 409#warning "Please update your code or use #define PL_ARITY_AS_SIZE 0." 410\end{code} 411 412To make the code compile silently again, change the types you use to 413represent arity from \ctype{int} to \ctype{size_t}. Please be aware that 414\ctype{size_t} is \emph{unsigned}. At some point representing arity 415as \ctype{int} will be dropped completely. 416 417 418\section{The Foreign Include File} \label{sec:foreigninclude} 419 420\subsection{Argument Passing and Control} 421\label{sec:foreign-control} 422 423If Prolog encounters a foreign predicate at run time it will call a 424function specified in the predicate definition of the foreign predicate. 425The arguments $1, \ldots, <arity>$ pass the Prolog arguments to the goal 426as Prolog terms. Foreign functions should be declared of type 427\ctype{foreign_t}. Deterministic foreign functions have two alternatives 428to return control back to Prolog: 429 430\begin{description} 431 \cmacro{(return) foreign_t}{PL_succeed}{} 432Succeed deterministically. PL_succeed is defined as 433\exam{return \const{TRUE}}. 434 \cmacro{(return) foreign_t}{PL_fail}{} 435Fail and start Prolog backtracking. PL_fail is defined as \exam{return 436\const{FALSE}}. 437\end{description} 438 439\subsubsection{Non-deterministic Foreign Predicates} \label{sec:foreignnondet} 440 441By default foreign predicates are deterministic. Using the 442\const{PL_FA_NONDETERMINISTIC} attribute (see PL_register_foreign()) it 443is possible to register a predicate as a non-deterministic predicate. 444Writing non-deterministic foreign predicates is slightly more 445complicated as the foreign function needs context information for 446generating the next solution. Note that the same foreign function should 447be prepared to be simultaneously active in more than one goal. Suppose 448the {natural_number_below_n}/2 is a non-deterministic foreign predicate, 449backtracking over all natural numbers lower than the first argument. Now 450consider the following predicate: 451 452\begin{code} 453quotient_below_n(Q, N) :- 454 natural_number_below_n(N, N1), 455 natural_number_below_n(N, N2), 456 Q =:= N1 / N2, !. 457\end{code} 458 459In this predicate the function {natural_number_below_n}/2 simultaneously 460generates solutions for both its invocations. 461 462Non-deterministic foreign functions should be prepared to handle three 463different calls from Prolog: 464 465\begin{itemlist} 466 \item [Initial call (\const{PL_FIRST_CALL})] 467Prolog has just created a frame for the foreign function and asks it to 468produce the first answer. 469 \item [Redo call (\const{PL_REDO})] 470The previous invocation of the foreign function associated with the 471current goal indicated it was possible to backtrack. The foreign 472function should produce the next solution. 473 \item [Terminate call (\const{PL_PRUNED})] 474The choice point left by the foreign function has been destroyed by 475a cut. The foreign function is given the opportunity to clean the 476environment. 477\end{itemlist} 478 479Both the context information and the type of call is provided by an 480argument of type \ctype{control_t} appended to the argument list for 481deterministic foreign functions. The macro PL_foreign_control() 482extracts the type of call from the control argument. The foreign 483function can pass a context handle using the {\tt PL_retry*()} macros and 484extract the handle from the extra argument using the 485{\tt PL_foreign_context*()} macro. 486 487\begin{description} 488 \cmacro{(return) foreign_t}{PL_retry}{intptr_t value} 489The foreign function succeeds while leaving a choice point. On 490backtracking over this goal the foreign function will be called again, 491but the control argument now indicates it is a `Redo' call and the 492macro PL_foreign_context() returns the handle passed via 493PL_retry(). This handle is a signed value two bits smaller than a pointer, 494i.e., 30 or 62 bits (two bits are used for status indication). 495Defined as \exam{return _PL_retry(n)}. See also PL_succeed(). 496 497 \cmacro{(return) foreign_t}{PL_retry_address}{void *} 498As PL_retry(), but ensures an address as returned by malloc() is 499correctly recovered by PL_foreign_context_address(). 500Defined as \exam{return _PL_retry_address(n)}. See also 501PL_succeed(). 502 503 \cmacro{int}{PL_foreign_control}{control_t} 504Extracts the type of call from the control argument. The return values 505are described above. Note that the function should be prepared to 506handle the \const{PL_PRUNED} case and should be aware that the other 507arguments are not valid in this case. 508 509 \cmacro{intptr_t}{PL_foreign_context}{control_t} 510Extracts the context from the context argument. If the call type is 511\const{PL_FIRST_CALL} the context value is 0L. Otherwise it is the value 512returned by the last PL_retry() associated with this goal (both if the 513call type is \const{PL_REDO} or \const{PL_PRUNED}). 514 515 \cmacro{void *}{PL_foreign_context_address}{control_t} 516Extracts an address as passed in by PL_retry_address(). 517 518 \cmacro{predicate_t}{PL_foreign_context_predicate}{control_t} 519 520Fetch the Prolog predicate that is executing this function. Note that if 521the predicate is imported, the returned predicate refers to the final 522definition rather than the imported predicate, i.e., the module reported 523by PL_predicate_info() is the module in which the predicate is defined 524rather than the module where it was called. See also 525PL_predicate_info(). 526\end{description} 527 528Note: If a non-deterministic foreign function returns using PL_succeed() 529or PL_fail(), Prolog assumes the foreign function has cleaned its 530environment. {\bf No} call with control argument \const{PL_PRUNED} will 531follow. 532 533The code of \figref{nondetermf} shows a skeleton for a 534non-deterministic foreign predicate definition. 535 536\begin{figure} 537\begin{code} 538typedef struct /* define a context structure */ 539{ ... 540} context; 541 542foreign_t 543my_function(term_t a0, term_t a1, control_t handle) 544{ struct context * ctxt; 545 546 switch( PL_foreign_control(handle) ) 547 { case PL_FIRST_CALL: 548 ctxt = malloc(sizeof(struct context)); 549 ... 550 PL_retry_address(ctxt); 551 case PL_REDO: 552 ctxt = PL_foreign_context_address(handle); 553 ... 554 PL_retry_address(ctxt); 555 case PL_PRUNED: 556 ctxt = PL_foreign_context_address(handle); 557 ... 558 free(ctxt); 559 PL_succeed; 560 } 561} 562\end{code} 563 \caption{Skeleton for non-deterministic foreign functions} 564 \label{fig:nondetermf} 565\end{figure} 566 567 568\subsection{Atoms and functors} 569\label{sec:foreign-atoms} 570 571The following functions provide for communication using atoms and 572functors. 573 574\begin{description} 575 \cfunction{atom_t}{PL_new_atom}{const char *} 576Return an atom handle for the given C-string. This function always 577succeeds. The returned handle is valid as long as the atom is 578referenced (see \secref{atomgc}). The following atoms are provided 579as macros, giving access to the empty list symbol and the name of the 580list constructor. Prior to version~7, \const{ATOM_nil} is the same 581as \exam{PL_new_atom("[]")} and \const{ATOM_dot} is the same as 582\exam{PL_new_atom(".")}. This is no long the case in SWI-Prolog 583version~7. 584 585 \begin{description} 586 \cmacro{atom_t}{ATOM_nil} 587Atomic constant that represents the empty list. It is advised to 588use PL_get_nil(), PL_put_nil() or PL_unify_nil() where applicable. 589 590 \cmacro{atom_t}{ATOM_dot} 591Atomic constant that represents the name of the list constructor. 592The list constructor itself is created using 593\exam{PL_new_functor(ATOM_dot,2)}. It is advised to use 594PL_get_list(), PL_put_list() or PL_unify_list() where applicable. 595 \end{description} 596 597 \cfunction{atom_t}{PL_new_atom_mbchars}{int rep, size_t len, 598 const char *s} 599This function generalizes PL_new_atom() and PL_new_atom_nchars() while 600allowing for multiple encodings. The \arg{rep} argument is one of 601\const{REP_ISO_LATIN_1}, \const{REP_UTF8} or \const{REP_MB}. If 602\arg{len} is \exam{(size_t)-1}, it is computed from \arg{s} using 603strlen(). 604 605 \cfunction{const char*}{PL_atom_chars}{atom_t atom} 606Return a C-string for the text represented by the given atom. The 607returned text will not be changed by Prolog. It is not allowed to modify 608the contents, not even `temporary' as the string may reside in read-only 609memory. The returned string becomes invalid if the atom is 610garbage collected (see \secref{atomgc}). Foreign functions that require 611the text from an atom passed in a \ctype{term_t} normally use 612PL_get_atom_chars() or PL_get_atom_nchars(). 613 614 \cfunction{functor_t}{PL_new_functor}{atom_t name, int arity} 615Returns a {\em functor identifier}, a handle for the name/arity 616pair. The returned handle is valid for the entire Prolog session. 617\cfunction{atom_t}{PL_functor_name}{functor_t f} 618Return an atom representing the name of the given functor. 619\cfunction{size_t}{PL_functor_arity}{functor_t f} 620Return the arity of the given functor. 621\end{description} 622 623 624\subsubsection{Atoms and atom garbage collection} \label{sec:atomgc} 625 626With the introduction of atom garbage collection in version 3.3.0, atoms 627no longer live as long as the process. Instead, their lifetime is 628guaranteed only as long as they are referenced. In the single-threaded 629version, atom garbage collections are only invoked at the 630\jargon{call-port}. In the multithreaded version (see \chapref{threads}), 631they appear asynchronously, except for the invoking thread. 632 633For dealing with atom garbage collection, two additional functions are 634provided: 635 636\begin{description} 637 \cfunction{void}{PL_register_atom}{atom_t atom} 638Increment the reference count of the atom by one. PL_new_atom() performs 639this automatically, returning an atom with a reference count of at least 640one.% 641 \footnote{Otherwise asynchronous atom garbage collection might 642 destroy the atom before it is used.} 643 644 \cfunction{void}{PL_unregister_atom}{atom_t atom} 645Decrement the reference count of the atom. If the reference count 646drops below zero, an assertion error is raised. 647\end{description} 648 649Please note that the following two calls are different with respect to 650atom garbage collection: 651 652\begin{code} 653PL_unify_atom_chars(t, "text"); 654PL_unify_atom(t, PL_new_atom("text")); 655\end{code} 656 657The latter increments the reference count of the atom \const{text}, 658which effectively ensures the atom will never be collected. It is 659advised to use the *_chars() or *_nchars() functions whenever 660applicable. 661 662 663\subsection{Analysing Terms via the Foreign Interface} 664\label{sec:foreign-term-analysis} 665 666Each argument of a foreign function (except for the control argument) is 667of type \ctype{term_t}, an opaque handle to a Prolog term. Three groups of 668functions are available for the analysis of terms. The first just 669validates the type, like the Prolog predicates var/1, atom/1, etc., and 670are called {\tt PL_is_*()}. The second group attempts to translate the 671argument into a C primitive type. These predicates take a \ctype{term_t} 672and a pointer to the appropriate C type and return \const{TRUE} or 673\const{FALSE} depending on successful or unsuccessful translation. If the 674translation fails, the pointed-to data is never modified. 675 676\subsubsection{Testing the type of a term} 677\label{sec:foreign-term-type} 678 679\begin{description} 680\cfunction{int}{PL_term_type}{term_t} 681Obtain the type of a term, which should be a term returned by 682one of the other interface predicates or passed as an argument. The 683function returns the type of the Prolog term. The type identifiers are 684listed below. Note that the extraction functions {\tt PL_get_*()} also 685validate the type and thus the two sections below are 686equivalent. 687 688\begin{code} 689 if ( PL_is_atom(t) ) 690 { char *s; 691 692 PL_get_atom_chars(t, &s); 693 ...; 694 } 695 696or 697 698 char *s; 699 if ( PL_get_atom_chars(t, &s) ) 700 { ...; 701 } 702\end{code} 703 704\textbf{Version~7} added \const{PL_NIL}, \const{PL_BLOB}, 705\const{PL_LIST_PAIR} and \const{PL_DICT}. Older versions 706classify \const{PL_NIL} and \const{PL_BLOB} as \const{PL_ATOM}, 707\const{PL_LIST_PAIR} as \const{PL_TERM} and do not have 708dicts. 709 710\begin{tabular}{|p{\tableft}|p{\linewidth-\tableft-2cm}|} 711\hline 712\const{PL_VARIABLE} & A variable or attributed variable \\ 713\const{PL_ATOM} & A Prolog atom \\ 714\const{PL_NIL} & The constant \verb$[]$ \\ 715\const{PL_BLOB} & A blob (see \secref{blobaccess}) \\ 716\const{PL_STRING} & A string (see \secref{strings}) \\ 717\const{PL_INTEGER} & A integer \\ 718\const{PL_RATIONAL} & A rational number \\ 719\const{PL_FLOAT} & A floating point number \\ 720\const{PL_TERM} & A compound term \\ 721\const{PL_LIST_PAIR} & A list cell (\verb$[H|T]$) \\ 722\const{PL_DICT} & A dict (see \secref{bidicts})) \\ 723\hline 724\end{tabular} 725\end{description} 726 727The functions PL_is_<type> are an alternative to PL_term_type(). The 728test \exam{PL_is_variable(term)} is equivalent to 729\exam{PL_term_type(term) == PL_VARIABLE}, but the first is considerably 730faster. On the other hand, using a switch over PL_term_type() is faster 731and more readable then using an if-then-else using the functions below. 732All these functions return either \const{TRUE} or \const{FALSE}. 733 734\begin{description} 735 \cfunction{int}{PL_is_variable}{term_t} 736Returns non-zero if \arg{term} is a variable. 737 738 \cfunction{int}{PL_is_ground}{term_t} 739Returns non-zero if \arg{term} is a ground term. See also ground/1. 740This function is cycle-safe. 741 742 \cfunction{int}{PL_is_atom}{term_t} 743Returns non-zero if \arg{term} is an atom. 744 745 \cfunction{int}{PL_is_string}{term_t} 746Returns non-zero if \arg{term} is a string. 747 748 \cfunction{int}{PL_is_integer}{term_t} 749Returns non-zero if \arg{term} is an integer. 750 751 \cfunction{int}{PL_is_rational}{term_t} 752Returns non-zero if \arg{term} is a rational number ($P/Q$). Note 753that all integers are considered rational and this test thus succeeds 754for any term for which PL_is_integer() succeeds. See also 755PL_get_mpq() and PL_unify_mpq(). 756 757 \cfunction{int}{PL_is_float}{term_t} 758Returns non-zero if \arg{term} is a float. Note that the corresponding 759PL_get_float() converts rationals (and thus integers). 760 761 \cfunction{int}{PL_is_callable}{term_t} 762Returns non-zero if \arg{term} is a callable term. See callable/1 763for details. 764 765 \cfunction{int}{PL_is_compound}{term_t} 766Returns non-zero if \arg{term} is a compound term. 767 768 \cfunction{int}{PL_is_functor}{term_t, functor_t} 769Returns non-zero if \arg{term} is compound and its functor is \arg{functor}. 770This test is equivalent to PL_get_functor(), followed by testing the 771functor, but easier to write and faster. 772 773 \cfunction{int}{PL_is_list}{term_t} 774Returns non-zero if \arg{term} is a compound term using the list 775constructor or the list terminator. See also PL_is_pair() and 776PL_skip_list(). 777 778 \cfunction{int}{PL_is_pair}{term_t} 779Returns non-zero if \arg{term} is a compound term using the list 780constructor. See also PL_is_list() and PL_skip_list(). 781 782 \cfunction{int}{PL_is_dict}{term_t} 783Returns non-zero if \arg{term} is a dict. See also PL_put_dict() 784and PL_get_dict_key(). 785 786 \cfunction{int}{PL_is_atomic}{term_t} 787Returns non-zero if \arg{term} is atomic (not a variable or compound). 788 789 \cfunction{int}{PL_is_number}{term_t} 790Returns non-zero if \arg{term} is an rational (including integers) or 791float. 792 793 \cfunction{int}{PL_is_acyclic}{term_t} 794Returns non-zero if \arg{term} is acyclic (i.e.\ a finite tree). 795\end{description} 796 797 798\subsubsection{Reading data from a term} 799\label{sec:foreign-extract-from-term} 800 801The functions {\tt PL_get_*()} read information from a Prolog term. Most 802of them take two arguments. The first is the input term and the second 803is a pointer to the output value or a term reference. 804 805\begin{description} 806 \cfunction{int}{PL_get_atom}{term_t +t, atom_t *a} 807If \arg{t} is an atom, store the unique atom identifier over \arg{a}. 808See also PL_atom_chars() and PL_new_atom(). If there is no need to 809access the data (characters) of an atom, it is advised to manipulate 810atoms using their handle. As the atom is referenced by \arg{t}, it 811will live at least as long as \arg{t} does. If longer live-time is 812required, the atom should be locked using PL_register_atom(). 813 814 \cfunction{int}{PL_get_atom_chars}{term_t +t, char **s} 815If \arg{t} is an atom, store a pointer to a 0-terminated C-string in 816\arg{s}. It is explicitly \strong{not} allowed to modify the contents 817of this string. Some built-in atoms may have the string allocated in 818read-only memory, so `temporary manipulation' can cause an error. 819 820 \cfunction{int}{PL_get_string_chars}{term_t +t, char **s, size_t *len} 821If \arg{t} is a string object, store a pointer to a 0-terminated 822C-string in \arg{s} and the length of the string in \arg{len}. Note 823that this pointer is invalidated by backtracking, garbage collection 824and stack-shifts, so generally the only save operations are to pass 825it immediately to a C function that doesn't involve Prolog. 826 827 \cfunction{int}{PL_get_chars}{term_t +t, char **s, unsigned flags} 828Convert the argument term \arg{t} to a 0-terminated C-string. 829\arg{flags} is a bitwise disjunction from two groups of constants. The 830first specifies which term types should be converted and the second how 831the argument is stored. Below is a specification of these constants. 832\const{BUF_STACK} implies, if the data is not static (as from an atom), 833that the data is pushed on a stack. If BUF_MALLOC is used, the data must 834be freed using PL_free() when no longer needed. 835 836With the introduction of wide characters (see \secref{encoding}), not 837all atoms can be converted into a \ctype{char*}. This function fails 838if \arg{t} is of the wrong type, but also if the text cannot be 839represented. See the \const{REP_*} flags below for details. 840 841\begin{description} 842 \termitem{CVT_ATOM}{} 843Convert if term is an atom. 844 845 \termitem{CVT_STRING}{} 846Convert if term is a string. 847 848 \termitem{CVT_LIST}{} 849Convert if term is a list of of character codes. 850 851 \termitem{CVT_INTEGER}{} 852Convert if term is an integer. 853 854 \termitem{CVT_FLOAT}{} 855Convert if term is a float. The characters returned are the same as 856write/1 would write for the floating point number. 857 858 \termitem{CVT_NUMBER}{} 859Convert if term is an integer or float. 860 861 \termitem{CVT_ATOMIC}{} 862Convert if term is atomic. 863 864 \termitem{CVT_VARIABLE}{} 865Convert variable to print-name 866 867 \termitem{CVT_WRITE}{} 868Convert any term that is not converted by any of the other flags using 869write/1. If no \const{BUF_*} is provided, \const{BUF_STACK} is implied. 870 871 \termitem{CVT_WRITE_CANONICAL}{} 872As \const{CVT_WRITE}, but using write_canonical/2. 873 874 \termitem{CVT_WRITEQ}{} 875As \const{CVT_WRITE}, but using writeq/2. 876 877 \termitem{CVT_ALL}{} 878Convert if term is any of the above, except for \const{CVT_VARIABLE} and 879\const{CVT_WRITE*}. 880 881 \termitem{CVT_EXCEPTION}{} 882If conversion fails due to a type error, raise a Prolog type error 883exception in addition to failure 884 885 \termitem{BUF_DISCARDABLE}{} 886Data must copied immediately 887 888 \termitem{BUF_STACK}{} 889Data is stored on a stack. The older \const{BUF_RING} is an alias 890for \const{BUF_STACK}. See \secref{foreign-strings}. 891 892 \termitem{BUF_MALLOC}{} 893Data is copied to a new buffer returned by \manref{PL_malloc}{3}. When 894no longer needed the user must call PL_free() on the data. 895 896 \termitem{REP_ISO_LATIN_1}{} 897Text is in ISO Latin-1 encoding and the call fails if text 898cannot be represented. This flag has the value 0 and is thus the 899default. 900 901 \termitem{REP_UTF8}{} 902Convert the text to a UTF-8 string. This works for all text. 903 904 \termitem{REP_MB}{} 905Convert to default locale-defined 8-bit string. Success depends on the 906locale. Conversion is done using the wcrtomb() C library function. 907\end{description} 908 909 910\cfunction{int}{PL_get_list_chars}{+term_t l, char **s, unsigned flags} 911Same as \exam{PL_get_chars(\arg{l}, \arg{s}, CVT_LIST|\arg{flags})}, 912provided \arg{flags} contains none of the {\tt CVT_*} flags. 913\cfunction{int}{PL_get_integer}{+term_t t, int *i} 914If \arg{t} is a Prolog integer, assign its value over \arg{i}. On 91532-bit machines, this is the same as PL_get_long(), but avoids a 916warning from the compiler. See also PL_get_long(). 917 918\cfunction{int}{PL_get_long}{term_t +t, long *i} 919If \arg{t} is a Prolog integer that can be represented as a long, assign 920its value over \arg{i}. If \arg{t} is an integer that cannot be 921represented by a C long, this function returns \const{FALSE}. If \arg{t} 922is a floating point number that can be represented as a long, this 923function succeeds as well. See also PL_get_int64(). 924 925\cfunction{int}{PL_get_int64}{term_t +t, int64_t *i} 926If \arg{t} is a Prolog integer or float that can be represented as a 927\ctype{int64_t}, assign its value over \arg{i}. 928 929\cfunction{int}{PL_get_intptr}{term_t +t, intptr_t *i} 930Get an integer that is at least as wide as a pointer. On most 931platforms this is the same as PL_get_long(), but on Win64 pointers are 8 932bytes and longs only 4. Unlike PL_get_pointer(), the value is not 933modified. 934 935\cfunction{int}{PL_get_bool}{term_t +t, int *val} 936If \arg{t} has the value \const{true} or \const{false}, set \arg{val} 937to the C constant \const{TRUE} or \const{FALSE} and return success, 938otherwise return failure. 939\cfunction{int}{PL_get_pointer}{term_t +t, void **ptr} 940In the current system, pointers are represented by Prolog integers, 941but need some manipulation to make sure they do not get truncated due 942to the limited Prolog integer range. PL_put_pointer() and PL_get_pointer() 943guarantee pointers in the range of malloc() are handled without 944truncating. 945 946\cfunction{int}{PL_get_float}{term_t +t, double *f} 947If \arg{t} is a float, integer or rational number, its value is assigned 948over \arg{f}. Note that if \arg{t} is an integer or rational conversion 949may fail because the number cannot be represented as a float. 950 951\cfunction{int}{PL_get_functor}{term_t +t, functor_t *f} 952If \arg{t} is compound or an atom, the Prolog representation of the 953name-arity pair will be assigned over \arg{f}. See also 954PL_get_name_arity() and PL_is_functor(). 955 956\cfunction{int}{PL_get_name_arity}{term_t +t, atom_t *name, size_t *arity} 957If \arg{t} is compound or an atom, the functor name will be assigned 958over \arg{name} and the arity over \arg{arity}. See also 959PL_get_functor() and PL_is_functor(). See \secref{pl-arity-as-size}. 960 961\cfunction{int}{PL_get_compound_name_arity}{term_t +t, atom_t *name, size_t *arity} 962If \arg{t} is compound term, the functor name will be assigned over 963\arg{name} and the arity over \arg{arity}. This is the same as 964PL_get_name_arity(), but this function fails if \arg{t} is an atom. 965 966\cfunction{int}{PL_get_module}{term_t +t, module_t *module} 967If \arg{t} is an atom, the system will look up or create the 968corresponding module and assign an opaque pointer to it over {\em 969module}. 970 971\cfunction{int}{PL_get_arg}{size_t index, term_t +t, term_t -a} 972If \arg{t} is compound and index is between 1 and arity (inclusive), 973assign \arg{a} with a term reference to the argument. 974 975\cfunction{int}{_PL_get_arg}{size_t index, term_t +t, term_t -a} 976Same as PL_get_arg(), but no checking is performed, neither whether \arg{t} 977is actually a term nor whether \arg{index} is a valid argument index. 978 979\cfunction{int}{PL_get_dict_key}{atom_t key, term_t +dict, term_t -value} 980If \arg{dict} is a dict, get the associated value in \arg{value}. Fails 981silently if \arg{key} does not appear in \arg{dict} and with an 982exception if \arg{dict} is not a dict. 983\end{description} 984 985 986\subsubsection{Exchanging text using length and string} 987\label{sec:foreign-text-with-length} 988 989All internal text representation in SWI-Prolog is represented using 990\type{char *} plus length and allow for \jargon{0-bytes} in them. 991The foreign library supports this by implementing a *_nchars() function 992for each applicable *_chars() function. Below we briefly present the 993signatures of these functions. For full documentation consult the 994*_chars() function. 995 996\begin{description} 997 \cfunction{int}{PL_get_atom_nchars}{term_t t, size_t *len, char **s} 998See PL_get_atom_chars(). 999 \cfunction{int}{PL_get_list_nchars}{term_t t, size_t *len, char **s} 1000See PL_get_list_chars(). 1001 \cfunction{int}{PL_get_nchars}{term_t t, size_t *len, char **s, 1002 unsigned int flags} 1003See PL_get_chars(). 1004 \cfunction{int}{PL_put_atom_nchars}{term_t t, size_t len, const char *s} 1005See PL_put_atom_chars(). 1006 \cfunction{int}{PL_put_string_nchars}{term_t t, size_t len, const char *s} 1007See PL_put_string_chars(). 1008 \cfunction{int}{PL_put_list_ncodes}{term_t t, size_t len, const char *s} 1009See PL_put_list_codes(). 1010 \cfunction{int}{PL_put_list_nchars}{term_t t, size_t len, const char *s} 1011See PL_put_list_chars(). 1012 \cfunction{int}{PL_unify_atom_nchars}{term_t t, size_t len, const char *s} 1013See PL_unify_atom_chars(). 1014 \cfunction{int}{PL_unify_string_nchars}{term_t t, size_t len, const char *s} 1015See PL_unify_string_chars(). 1016 \cfunction{int}{PL_unify_list_ncodes}{term_t t, size_t len, const char *s} 1017See PL_unify_codes(). 1018 \cfunction{int}{PL_unify_list_nchars}{term_t t, size_t len, const char *s} 1019See PL_unify_list_chars(). 1020\end{description} 1021 1022In addition, the following functions are available for creating and 1023inspecting atoms: 1024 1025\begin{description} 1026 \cfunction{atom_t}{PL_new_atom_nchars}{size_t len, const char *s} 1027Create a new atom as PL_new_atom(), but using the given length and characters. 1028If \arg{len} is \exam{(size_t)-1}, it is computed from \arg{s} using 1029strlen(). 1030 1031 \cfunction{const char *}{PL_atom_nchars}{atom_t a, size_t *len} 1032Extract the text and length of an atom. 1033\end{description} 1034 1035 1036\subsubsection{Wide-character versions} 1037\label{sec:foreign-unicode} 1038 1039Support for exchange of wide-character strings is still under 1040consideration. The functions dealing with 8-bit character strings 1041return failure when operating on a wide-character atom or Prolog string 1042object. The functions below can extract and unify both 8-bit and wide 1043atoms and string objects. Wide character strings are represented as C 1044arrays of objects of the type \ctype{pl_wchar_t}, which is guaranteed to 1045be the same as \ctype{wchar_t} on platforms supporting this type. For 1046example, on MS-Windows, this represents 16-bit UCS2 characters, while 1047using the GNU C library (glibc) this represents 32-bit UCS4 characters. 1048 1049\begin{description} 1050 \cfunction{atom_t}{PL_new_atom_wchars}{size_t len, const pl_wchar_t *s} 1051Create atom from wide-character string as PL_new_atom_nchars() does for 1052ISO-Latin-1 strings. If \arg{s} only contains ISO-Latin-1 characters a 1053normal byte-array atom is created. If \arg{len} is \exam{(size_t)-1}, it 1054is computed from \arg{s} using wcslen(). 1055 1056 \cfunction{pl_wchar_t*}{PL_atom_wchars}{atom_t atom, int *len} 1057Extract characters from a wide-character atom. Succeeds on any atom 1058marked as `text'. If the underlying atom is a wide-character atom, 1059the returned pointer is a pointer into the atom structure. If it is 1060an ISO-Latin-1 character, the returned pointer comes from Prolog's 1061`buffer ring' (see PL_get_chars()). 1062 1063 \cfunction{int}{PL_get_wchars}{term_t t, size_t *len, 1064 pl_wchar_t **s, unsigned flags} 1065Wide-character version of PL_get_chars(). The \arg{flags} argument 1066is the same as for PL_get_chars(). 1067 1068 \cfunction{int}{PL_unify_wchars}{term_t t, int type, 1069 size_t len, 1070 const pl_wchar_t *s} 1071Unify \arg{t} with a textual representation of the C wide-character 1072array \arg{s}. The \arg{type} argument defines the Prolog 1073representation and is one of \const{PL_ATOM}, \const{PL_STRING}, 1074\const{PL_CODE_LIST} or \const{PL_CHAR_LIST}. 1075 \cfunction{int}{PL_unify_wchars_diff}{term_t +t, term_t -tail, int type, 1076 size_t len, 1077 const pl_wchar_t *s} 1078Difference list version of PL_unify_wchars(), only supporting the 1079types \const{PL_CODE_LIST} and \const{PL_CHAR_LIST}. It serves two 1080purposes. It allows for returning very long lists from data read from 1081a stream without the need for a resizing buffer in C. Also, the use of 1082difference lists is often practical for further processing in Prolog. 1083Examples can be found in \file{packages/clib/readutil.c} from the 1084source distribution. 1085\end{description} 1086 1087 1088\subsubsection{Reading a list} 1089\label{sec:foreign-read-list} 1090 1091The functions from this section are intended to read a Prolog list from 1092C. Suppose we expect a list of atoms; the following code will print the 1093atoms, each on a line: 1094 1095\begin{code} 1096foreign_t 1097pl_write_atoms(term_t l) 1098{ term_t head = PL_new_term_ref(); /* the elements */ 1099 term_t list = PL_copy_term_ref(l); /* copy (we modify list) */ 1100 1101 while( PL_get_list(list, head, list) ) 1102 { char *s; 1103 1104 if ( PL_get_atom_chars(head, &s) ) 1105 Sprintf("%s\n", s); 1106 else 1107 PL_fail; 1108 } 1109 1110 return PL_get_nil(list); /* test end for [] */ 1111} 1112\end{code} 1113 1114Note that as of version~7, lists have a new representation unless the 1115option \cmdlineoption{--traditional} is used. see \secref{ext-lists}. 1116 1117\begin{description} 1118 \cfunction{int}{PL_get_list}{term_t +l, term_t -h, term_t -t} 1119If \arg{l} is a list and not the empty list, assign a term reference to 1120the head to \arg{h} and to the tail to \arg{t}. 1121 1122 \cfunction{int}{PL_get_head}{term_t +l, term_t -h} 1123If \arg{l} is a list and not the empty list, assign a term reference to 1124the head to \arg{h}. 1125 1126 \cfunction{int}{PL_get_tail}{term_t +l, term_t -t} 1127If \arg{l} is a list and not the empty list, assign a term reference to 1128the tail to \arg{t}. 1129 1130 \cfunction{int}{PL_get_nil}{term_t +l} 1131Succeeds if \arg{l} represents the list termination constant. 1132 1133 \cfunction{int}{PL_skip_list}{term_t +list, term_t -tail, size_t *len} 1134This is a multi-purpose function to deal with lists. It allows for 1135finding the length of a list, checking whether something is a list, 1136etc. The reference \arg{tail} is set to point to the end of the list, 1137\arg{len} is filled with the number of list-cells skipped, and the 1138return value indicates the status of the list: 1139 1140 \begin{description} 1141 \constitem{PL_LIST} 1142 The list is a `proper' list: one that ends in the list terminator 1143 constant and \arg{tail} is filled with the terminator constant. 1144 1145 \constitem{PL_PARTIAL_LIST} 1146 The list is a `partial' list: one that ends in a variable and 1147 \arg{tail} is a reference to this variable. 1148 1149 \constitem{PL_CYCLIC_TERM} 1150 The list is cyclic (e.g. X = [a|X]). \arg{tail} points to 1151 an arbitrary cell of the list and \arg{len} is at most twice 1152 the cycle length of the list. 1153 1154 \constitem{PL_NOT_A_LIST} 1155 The term \arg{list} is not a list at all. \arg{tail} is 1156 bound to the non-list term and \arg{len} is set to the number 1157 of list-cells skipped. 1158 \end{description} 1159 1160It is allowed to pass 0 for \arg{tail} and \const{NULL} for \arg{len}. 1161\end{description} 1162 1163\subsubsection{An example: defining write/1 in C} 1164\label{sec:foreign-write} 1165 1166\Figref{pl-display} shows a simplified definition of write/1 to 1167illustrate the described functions. This simplified version does not 1168deal with operators. It is called display/1, because it mimics closely 1169the behaviour of this Edinburgh predicate. 1170 1171\begin{figure} 1172\begin{code} 1173foreign_t 1174pl_display(term_t t) 1175{ functor_t functor; 1176 int arity, len, n; 1177 char *s; 1178 1179 switch( PL_term_type(t) ) 1180 { case PL_VARIABLE: 1181 case PL_ATOM: 1182 case PL_INTEGER: 1183 case PL_FLOAT: 1184 PL_get_chars(t, &s, CVT_ALL); 1185 Sprintf("%s", s); 1186 break; 1187 case PL_STRING: 1188 PL_get_string_chars(t, &s, &len); 1189 Sprintf("\"%s\"", s); 1190 break; 1191 case PL_TERM: 1192 { term_t a = PL_new_term_ref(); 1193 1194 PL_get_name_arity(t, &name, &arity); 1195 Sprintf("%s(", PL_atom_chars(name)); 1196 for(n=1; n<=arity; n++) 1197 { PL_get_arg(n, t, a); 1198 if ( n > 1 ) 1199 Sprintf(", "); 1200 pl_display(a); 1201 } 1202 Sprintf(")"); 1203 break; 1204 default: 1205 PL_fail; /* should not happen */ 1206 } 1207 } 1208 1209 PL_succeed; 1210} 1211\end{code} 1212 1213 \caption{A Foreign definition of display/1} 1214 \label{fig:pl-display} 1215\end{figure} 1216 1217 1218\subsection{Constructing Terms} 1219\label{sec:foreign-term-construct} 1220 1221Terms can be constructed using functions from the {\tt PL_put_*()} and 1222{\tt PL_cons_*()} families. This approach builds the term `inside-out', 1223starting at the leaves and subsequently creating compound terms. 1224Alternatively, terms may be created `top-down', first creating a 1225compound holding only variables and subsequently unifying the arguments. 1226This section discusses functions for the first approach. This approach 1227is generally used for creating arguments for PL_call() and 1228PL_open_query(). 1229 1230\begin{description} 1231\cfunction{void}{PL_put_variable}{term_t -t} 1232Put a fresh variable in the term, resetting the term reference to its 1233initial state.\footnote{Older versions created a variable on the global 1234stack.} 1235\cfunction{void}{PL_put_atom}{term_t -t, atom_t a} 1236Put an atom in the term reference from a handle. See also 1237PL_new_atom() and PL_atom_chars(). 1238\cfunction{void}{PL_put_bool}{term_t -t, int val} 1239Put one of the atoms \const{true} or \const{false} in the term reference 1240See also PL_put_atom(), PL_unify_bool() and PL_get_bool(). 1241\cfunction{int}{PL_put_chars}{term_t -t, int flags, 1242 size_t len, 1243 const char *chars} 1244New function to deal with setting a term from a \ctype{char*} with 1245various encodings. The \arg{flags} argument is a bitwise \emph{or} 1246specifying the Prolog target type and the encoding of \arg{chars}. A 1247Prolog type is one of \const{PL_ATOM}, \const{PL_STRING}, 1248\const{PL_CODE_LIST} or \const{PL_CHAR_LIST}. A representation is one of 1249\const{REP_ISO_LATIN_1}, \const{REP_UTF8} or \const{REP_MB}. See 1250PL_get_chars() for a definition of the representation types. If 1251\arg{len} is \const{-1} \arg{chars} must be zero-terminated and the 1252length is computed from \arg{chars} using strlen(). 1253\cfunction{int}{PL_put_atom_chars}{term_t -t, const char *chars} 1254Put an atom in the term reference constructed from the zero-terminated 1255string. The string itself will never be referenced by Prolog after this 1256function. 1257\cfunction{int}{PL_put_string_chars}{term_t -t, const char *chars} 1258Put a zero-terminated string in the term reference. The data will be 1259copied. See also PL_put_string_nchars(). 1260\cfunction{int}{PL_put_string_nchars}{term_t -t, 1261 size_t len, 1262 const char *chars} 1263 1264Put a string, represented by a length/start pointer pair in the 1265term reference. The data will be copied. This interface can deal 1266with 0-bytes in the string. See also \secref{foreigndata}. 1267\cfunction{int}{PL_put_list_chars}{term_t -t, const char *chars} 1268Put a list of ASCII values in the term reference. 1269\cfunction{int}{PL_put_integer}{term_t -t, long i} 1270Put a Prolog integer in the term reference. 1271\cfunction{int}{PL_put_int64}{term_t -t, int64_t i} 1272Put a Prolog integer in the term reference. 1273\cfunction{int}{PL_put_uint64}{term_t -t, uint64_t i} 1274Put a Prolog integer in the term reference. Note that unbounded integer 1275support is required for \ctype{uint64_t} values with the highest bit set 1276to 1. Without unbounded integer support, too large values raise a 1277\const{representation_error} exception. 1278\cfunction{int}{PL_put_pointer}{term_t -t, void *ptr} 1279Put a Prolog integer in the term reference. Provided \arg{ptr} is in the 1280`malloc()-area', PL_get_pointer() will get the pointer back. 1281\cfunction{int}{PL_put_float}{term_t -t, double f} 1282Put a floating-point value in the term reference. 1283 1284 \cfunction{int}{PL_put_functor}{term_t -t, functor_t functor} 1285Create a new compound term from \arg{functor} and bind \arg{t} to 1286this term. All arguments of the term will be variables. To create 1287a term with instantiated arguments, either instantiate the arguments 1288using the {\tt PL_unify_*()} functions or use PL_cons_functor(). 1289 1290 \cfunction{int}{PL_put_list}{term_t -l} 1291As PL_put_functor(), using the list-cell functor. Note that on classical 1292Prolog systems or in SWI-Prolog using the option 1293\cmdlineoption{--traditional}, this is \functor{.}{2}, while on 1294SWI-Prolog version~7 this is \functor{[|]}{2}. 1295 1296 \cfunction{int}{PL_put_nil}{term_t -l} 1297Put the list terminator constant in \arg{l}. Always returns 1298\const{TRUE}. Note that in classical Prolog systems or in SWI-Prolog 1299using the option \cmdlineoption{--traditional}, this is the same as 1300\exam{PL_put_atom_chars("[]")}. See \secref{ext-lists}. 1301 1302 \cfunction{void}{PL_put_term}{term_t -t1, term_t +t2} 1303Make \arg{t1} point to the same term as \arg{t2}. 1304 1305 \cfunction{int}{PL_cons_functor}{term_t -h, functor_t f, \ldots} 1306Create a term whose arguments are filled from a variable argument list 1307holding the same number of \ctype{term_t} objects as the arity of the functor. 1308To create the term \exam{animal(gnu, 50)}, use: 1309 1310\begin{code} 1311{ term_t a1 = PL_new_term_ref(); 1312 term_t a2 = PL_new_term_ref(); 1313 term_t t = PL_new_term_ref(); 1314 functor_t animal2; 1315 1316 /* animal2 is a constant that may be bound to a global 1317 variable and re-used 1318 */ 1319 animal2 = PL_new_functor(PL_new_atom("animal"), 2); 1320 1321 PL_put_atom_chars(a1, "gnu"); 1322 PL_put_integer(a2, 50); 1323 PL_cons_functor(t, animal2, a1, a2); 1324} 1325\end{code} 1326 1327 1328After this sequence, the term references \arg{a1} and \arg{a2} may 1329be used for other purposes. 1330\cfunction{int}{PL_cons_functor_v}{term_t -h, functor_t f, term_t a0} 1331Create a compound term like PL_cons_functor(), but \arg{a0} is an 1332array of term references as returned by PL_new_term_refs(). The length 1333of this array should match the number of arguments required by the 1334functor. 1335\cfunction{int}{PL_cons_list}{term_t -l, term_t +h, term_t +t} 1336Create a list (cons-) cell in \arg{l} from the head \arg{h} and tail \arg{t}. The 1337code below creates a list of atoms from a \ctype{char **}. The list 1338is built tail-to-head. The {\tt PL_unify_*()} functions can be used 1339to build a list head-to-tail. 1340 1341\begin{code} 1342void 1343put_list(term_t l, int n, char **words) 1344{ term_t a = PL_new_term_ref(); 1345 1346 PL_put_nil(l); 1347 while( --n >= 0 ) 1348 { PL_put_atom_chars(a, words[n]); 1349 PL_cons_list(l, a, l); 1350 } 1351} 1352\end{code} 1353Note that \arg{l} can be redefined within a {\tt PL_cons_list} call as 1354shown here because operationally its old value is consumed before its 1355new value is set. 1356 1357\cfunction{int}{PL_put_dict}{term_t -h, atom_t tag, size_t len, 1358 const atom_t *keys, term_t values} 1359Create a dict from a \arg{tag} and vector of atom-value pairs and put 1360the result in \arg{h}. The dict's key is set by \arg{tag}, which may 1361be \const{0} to leave the tag unbound. The \arg{keys} vector is a vector 1362of atoms of at least \arg{len} long. The \arg{values} is a term vector 1363allocated using PL_new_term_refs() of at least \arg{len} long. This 1364function returns \const{TRUE} on success, \const{FALSE} on a resource 1365error (leaving a resource error exception in the environment), 1366\const{-1} if some key or the \arg{tag} is invalid and \const{-2} if 1367there are duplicate keys. 1368\end{description} 1369 1370 1371\subsection{Unifying data} 1372\label{sec:foreign-unify} 1373 1374The functions of this section \jargon{unify} terms with other terms or 1375translated C data structures. Except for PL_unify(), these functions 1376are specific to SWI-Prolog. They have been introduced 1377because they shorten the code for returning data to Prolog and at the 1378same time make this more efficient by avoiding the need to allocate 1379temporary term references and reduce the number of calls to the Prolog 1380API. Consider the case where we want a foreign function to return the 1381host name of the machine Prolog is running on. Using the {\tt 1382PL_get_*()} and {\tt PL_put_*()} functions, the code becomes: 1383 1384\begin{code} 1385foreign_t 1386pl_hostname(term_t name) 1387{ char buf[100]; 1388 1389 if ( gethostname(buf, sizeof(buf)) ) 1390 { term_t tmp = PL_new_term_ref(); 1391 1392 PL_put_atom_chars(tmp, buf); 1393 return PL_unify(name, tmp); 1394 } 1395 1396 PL_fail; 1397} 1398\end{code} 1399 1400Using PL_unify_atom_chars(), this becomes: 1401 1402\begin{code} 1403foreign_t 1404pl_hostname(term_t name) 1405{ char buf[100]; 1406 1407 if ( gethostname(buf, sizeof(buf)) ) 1408 return PL_unify_atom_chars(name, buf); 1409 1410 PL_fail; 1411} 1412\end{code} 1413 1414Note that unification functions that perform multiple bindings may leave 1415part of the bindings in case of failure. See PL_unify() for details. 1416 1417 1418\begin{description} 1419 \cfunction{int}{PL_unify}{term_t ?t1, term_t ?t2} 1420Unify two Prolog terms and return \const{TRUE} on success. 1421 1422Care is needed if PL_unify() returns \const{FAIL} and the foreign 1423function does not \emph{immediately} return to Prolog with \const{FAIL}. 1424Unification may perform multiple changes to either \arg{t1} or \arg{t2}. 1425A failing unification may have created bindings before failure is 1426detected. \emph{Already created bindings are not undone}. For 1427example, calling PL_unify() on \term{a}{X, a} and \term{a}{c,b} binds 1428\arg{X} to \const{c} and fails when trying to unify \const{a} to 1429\const{b}. If control remains in C or even if we want to return success 1430to Prolog, we \emph{must} undo such bindings. This is achieved using 1431PL_open_foreign_frame() and PL_rewind_foreign_frame(), as shown in the 1432snippet below. 1433 1434\begin{code} 1435 { fid_t fid = PL_open_foreign_frame(); 1436 1437 ... 1438 if ( !PL_unify(t1, t2) ) 1439 PL_rewind_foreign_frame(fid); 1440 ... 1441 1442 PL_close_foreign_frame(fid); 1443 } 1444\end{code} 1445 1446In addition, PL_unify() may have failed on an \textbf{exception}, 1447typically a resource (stack) overflow. This can be tested using 1448PL_exception(), passing 0 (zero) for the query-id argument. Foreign 1449functions that encounter an exception must return \const{FAIL} to 1450Prolog as soon as possible or call PL_clear_exception() if they wish 1451to ignore the exception. 1452 1453 \cfunction{int}{PL_unify_atom}{term_t ?t, atom_t a} 1454Unify \arg{t} with the atom \arg{a} and return non-zero on success. 1455 1456 \cfunction{int}{PL_unify_bool}{term_t ?t, int a} 1457Unify \arg{t} with either \const{true} or \const{false}. 1458 1459 \cfunction{int}{PL_unify_chars}{term_t ?t, int flags, 1460 size_t len, 1461 const char *chars} 1462New function to deal with unification of \ctype{char*} with various 1463encodings to a Prolog representation. The \arg{flags} argument is a 1464bitwise \emph{or} specifying the Prolog target type and the encoding of 1465\arg{chars}. A Prolog type is one of \const{PL_ATOM}, \const{PL_STRING}, 1466\const{PL_CODE_LIST} or \const{PL_CHAR_LIST}. A representation is one of 1467\const{REP_ISO_LATIN_1}, \const{REP_UTF8} or \const{REP_MB}. See 1468PL_get_chars() for a definition of the representation types. If 1469\arg{len} is \const{-1} \arg{chars} must be zero-terminated and the length 1470is computed from \arg{chars} using strlen(). 1471 1472If \arg{flags} includes \const{PL_DIFF_LIST} and type is one of 1473\const{PL_CODE_LIST} or \const{PL_CHAR_LIST}, the text is converted 1474to a \jargon{difference list}. The tail of the difference list is 1475$t+1$. 1476 1477 \cfunction{int}{PL_unify_atom_chars}{term_t ?t, const char *chars} 1478Unify \arg{t} with an atom created from \arg{chars} and return non-zero 1479on success. 1480 1481 \cfunction{int}{PL_unify_list_chars}{term_t ?t, const char *chars} 1482Unify \arg{t} with a list of ASCII characters constructed from 1483\arg{chars}. 1484 1485 \cfunction{void}{PL_unify_string_chars}{term_t ?t, const char *chars} 1486Unify \arg{t} with a Prolog string object created from the 1487zero-terminated string \arg{chars}. The data will be copied. 1488See also PL_unify_string_nchars(). 1489 \cfunction{int}{PL_unify_integer}{term_t ?t, intptr_t n} 1490Unify \arg{t} with a Prolog integer from \arg{n}. 1491 \cfunction{int}{PL_unify_int64}{term_t ?t, int64_t n} 1492Unify \arg{t} with a Prolog integer from \arg{n}. 1493 \cfunction{int}{PL_unify_uint64}{term_t ?t, uint64_t n} 1494Unify \arg{t} with a Prolog integer from \arg{n}. Note that unbounded 1495integer support is required if \arg{n} does not fit in a \emph{signed} 1496\ctype{int64_t}. If unbounded integers are not supported a 1497\const{representation_error} is raised. 1498 \cfunction{int}{PL_unify_float}{term_t ?t, double f} 1499Unify \arg{t} with a Prolog float from \arg{f}. 1500 \cfunction{int}{PL_unify_pointer}{term_t ?t, void *ptr} 1501Unify \arg{t} with a Prolog integer describing the pointer. See also 1502PL_put_pointer() and PL_get_pointer(). 1503 1504 \cfunction{int}{PL_unify_functor}{term_t ?t, functor_t f} 1505If \arg{t} is a compound term with the given functor, just succeed. 1506If it is unbound, create a term and bind the variable, else fail. 1507Note that this function does not create a term if the argument is 1508already instantiated. If \arg{f} is a functor with arity 0, \arg{t} 1509is unified with an atom. See also PL_unify_compound(). 1510 1511 \cfunction{int}{PL_unify_compound}{term_t ?t, functor_t f} 1512If \arg{t} is a compound term with the given functor, just succeed. 1513If it is unbound, create a term and bind the variable, else fail. 1514Note that this function does not create a term if the argument is 1515already instantiated. If \arg{f} is a functor with arity 0, \arg{t} 1516is unified with compound without arguments. See also 1517PL_unify_functor(). 1518 1519 \cfunction{int}{PL_unify_list}{term_t ?l, term_t -h, term_t -t} 1520Unify \arg{l} with a list-cell ({\tt ./2}). If successful, write a 1521reference to the head of the list into \arg{h} and a reference 1522to the tail of the list into \arg{t}. This reference may be used for 1523subsequent calls to this function. Suppose we want to return a list of 1524atoms from a \ctype{char **}. We could use the example described by 1525PL_put_list(), followed by a call to PL_unify(), or we can use the code 1526below. If the predicate argument is unbound, the difference is minimal 1527(the code based on PL_put_list() is probably slightly faster). If the 1528argument is bound, the code below may fail before reaching the end of 1529the word list, but even if the unification succeeds, this code avoids a 1530duplicate (garbage) list and a deep unification. 1531 1532\begin{code} 1533foreign_t 1534pl_get_environ(term_t env) 1535{ term_t l = PL_copy_term_ref(env); 1536 term_t a = PL_new_term_ref(); 1537 extern char **environ; 1538 char **e; 1539 1540 for(e = environ; *e; e++) 1541 { if ( !PL_unify_list(l, a, l) || 1542 !PL_unify_atom_chars(a, *e) ) 1543 PL_fail; 1544 } 1545 1546 return PL_unify_nil(l); 1547} 1548\end{code} 1549\cfunction{int}{PL_unify_nil}{term_t ?l} 1550Unify \arg{l} with the atom \const{[]}. 1551\cfunction{int}{PL_unify_arg}{int index, term_t ?t, term_t ?a} 1552Unifies the {\em index-th} argument (1-based) of \arg{t} with 1553\arg{a}. 1554\cfunction{int}{PL_unify_term}{term_t ?t, \ldots} 1555Unify \arg{t} with a (normally) compound term. The remaining arguments 1556are a sequence of a type identifier followed by the required 1557arguments. This predicate is an extension to the Quintus and SICStus 1558foreign interface from which the SWI-Prolog foreign interface has been 1559derived, but has proved to be a powerful and comfortable way to create 1560compound terms from C. Due to the vararg packing/unpacking and the 1561required type-switching this interface is slightly slower than using 1562the primitives. Please note that some bad C compilers have fairly 1563low limits on the number of arguments that may be passed to a function. 1564 1565Special attention is required when passing numbers. C `promotes' any 1566integral smaller than \type{int} to \type{int}. That is, the types 1567\type{char}, \type{short} and \type{int} are all passed as \type{int}. 1568In addition, on most 32-bit platforms \type{int} and \type{long} are the 1569same. Up to version 4.0.5, only \const{PL_INTEGER} could be specified, 1570which was taken from the stack as \type{long}. Such code fails when 1571passing small integral types on machines where \type{int} is smaller 1572than \type{long}. It is advised to use \const{PL_SHORT}, \const{PL_INT} 1573or \const{PL_LONG} as appropriate. Similarly, C compilers promote 1574\type{float} to \type{double} and therefore \const{PL_FLOAT} and 1575\const{PL_DOUBLE} are synonyms. 1576 1577The type identifiers are: 1578 1579\begin{description} 1580 \definition{\const{PL_VARIABLE} \arg{none}} 1581No op. Used in arguments of \const{PL_FUNCTOR}. 1582 \definition{\const{PL_BOOL} \arg{int}} 1583Unify the argument with \const{true} or \const{false}. 1584 \definition{\const{PL_ATOM} \arg{atom_t}} 1585Unify the argument with an atom, as in PL_unify_atom(). 1586 \definition{\const{PL_CHARS} \arg{const char *}} 1587Unify the argument with an atom constructed from the C \ctype{char *}, 1588as in PL_unify_atom_chars(). 1589 \definition{\const{PL_NCHARS} \arg{size_t, const char *}} 1590Unify the argument with an atom constructed from length and 1591\ctype{char*} as in PL_unify_atom_nchars(). 1592 \definition{\const{PL_UTF8_CHARS} \arg{const char *}} 1593Create an atom from a UTF-8 string. 1594 \definition{\const{PL_UTF8_STRING} \arg{const char *}} 1595Create a packed string object from a UTF-8 string. 1596 \definition{\const{PL_MBCHARS} \arg{const char *}} 1597Create an atom from a multi-byte string in the current locale. 1598 \definition{\const{PL_MBCODES} \arg{const char *}} 1599Create a list of character codes from a multi-byte string in the current 1600locale. 1601 \definition{\const{PL_MBSTRING} \arg{const char *}} 1602Create a packed string object from a multi-byte string in the 1603current locale. 1604 \definition{\const{PL_NWCHARS} \arg{size_t, const wchar_t *}} 1605Create an atom from a length and a wide character pointer. 1606 \definition{\const{PL_NWCODES} \arg{size_t, const wchar_t *}} 1607Create a list of character codes from a length and a wide character pointer. 1608 \definition{\const{PL_NWSTRING} \arg{size_t, const wchar_t *}} 1609Create a packed string object from a length and a wide character pointer. 1610 \definition{\const{PL_SHORT} \arg{short}} 1611Unify the argument with an integer, as in PL_unify_integer(). As 1612\type{short} is promoted to \type{int}, \const{PL_SHORT} is a 1613synonym for \type{PL_INT}. 1614 \definition{\const{PL_INTEGER} \arg{long}} 1615Unify the argument with an integer, as in PL_unify_integer(). 1616 \definition{\const{PL_INT} \arg{int}} 1617Unify the argument with an integer, as in PL_unify_integer(). 1618 \definition{\const{PL_LONG} \arg{long}} 1619Unify the argument with an integer, as in PL_unify_integer(). 1620 \definition{\const{PL_INT64} \arg{int64_t}} 1621Unify the argument with a 64-bit integer, as in PL_unify_int64(). 1622 \definition{\const{PL_INTPTR} \arg{intptr_t}} 1623Unify the argument with an integer with the same width as a pointer. 1624On most machines this is the same as \const{PL_LONG}. but on 64-bit 1625MS-Windows pointers are 64 bits while longs are only 32 bits. 1626 \definition{\const{PL_DOUBLE} \arg{double}} 1627Unify the argument with a float, as in PL_unify_float(). Note that, 1628as the argument is passed using the C vararg conventions, a float must 1629be casted to a double explicitly. 1630 \definition{\const{PL_FLOAT} \arg{double}} 1631Unify the argument with a float, as in PL_unify_float(). 1632 \definition{\const{PL_POINTER} \arg{void *}} 1633Unify the argument with a pointer, as in PL_unify_pointer(). 1634 \definition{\const{PL_STRING} \arg{const char *}} 1635Unify the argument with a string object, as in PL_unify_string_chars(). 1636 \definition{\const{PL_TERM} \arg{term_t}} 1637Unify a subterm. Note this may be the return value of a PL_new_term_ref() 1638call to get access to a variable. 1639 \definition{\const{PL_FUNCTOR} \arg{functor_t, \ldots}} 1640Unify the argument with a compound term. This specification should be 1641followed by exactly as many specifications as the number of arguments of 1642the compound term. 1643 \definition{\const{PL_FUNCTOR_CHARS} 1644 \arg{const char *name, int arity, \ldots}} 1645Create a functor from the given name and arity and then behave as 1646\const{PL_FUNCTOR}. 1647 \definition{\const{PL_LIST} \arg{int length, \ldots}} 1648Create a list of the indicated length. The remaining arguments contain 1649the elements of the list. 1650\end{description} 1651 1652For example, to unify an argument with the term \exam{language(dutch)}, 1653the following skeleton may be used: 1654 1655 1656\begin{code} 1657static functor_t FUNCTOR_language1; 1658 1659static void 1660init_constants() 1661{ FUNCTOR_language1 = PL_new_functor(PL_new_atom("language"),1); 1662} 1663 1664foreign_t 1665pl_get_lang(term_t r) 1666{ return PL_unify_term(r, 1667 PL_FUNCTOR, FUNCTOR_language1, 1668 PL_CHARS, "dutch"); 1669} 1670 1671install_t 1672install() 1673{ PL_register_foreign("get_lang", 1, pl_get_lang, 0); 1674 init_constants(); 1675} 1676\end{code} 1677 1678\cfunction{int}{PL_chars_to_term}{const char *chars, term_t -t} 1679Parse the string \arg{chars} and put the resulting Prolog term into 1680\arg{t}. \arg{chars} may or may not be closed using a Prolog full-stop 1681(i.e., a dot followed by a blank). Returns \const{FALSE} if a syntax 1682error was encountered and \const{TRUE} after successful completion. 1683In addition to returning \const{FALSE}, the exception-term is 1684returned in \arg{t} on a syntax error. 1685See also term_to_atom/2. 1686 1687The following example builds a goal term from a string and calls it. 1688 1689\begin{code} 1690int 1691call_chars(const char *goal) 1692{ fid_t fid = PL_open_foreign_frame(); 1693 term_t g = PL_new_term_ref(); 1694 BOOL rval; 1695 1696 if ( PL_chars_to_term(goal, g) ) 1697 rval = PL_call(goal, NULL); 1698 else 1699 rval = FALSE; 1700 1701 PL_discard_foreign_frame(fid); 1702 return rval; 1703} 1704 ... 1705 call_chars("consult(load)"); 1706 ... 1707\end{code} 1708 1709PL_chars_to_term() is defined using PL_put_term_from_chars() which can 1710deal with not null-terminated strings as well as strings using different 1711encodings: 1712 1713\begin{code} 1714int 1715PL_chars_to_term(const char *s, term_t t) 1716{ return PL_put_term_from_chars(t, REP_ISO_LATIN_1, (size_t)-1, s); 1717} 1718\end{code} 1719 1720 1721\cfunction{int}{PL_wchars_to_term}{const pl_wchar_t *chars, term_t -t} 1722Wide character version of PL_chars_to_term(). 1723 1724\cfunction{char *}{PL_quote}{int chr, const char *string} 1725 Return a quoted version of \arg{string}. If \arg{chr} is 1726 \verb$'\''$, the result is a quoted atom. If \arg{chr} is 1727 \verb$'"'$, the result is a string. The result string is stored 1728 in the same ring of buffers as described with the \const{BUF_STACK} 1729 argument of PL_get_chars(); 1730 1731 In the current implementation, the string is surrounded by 1732 \arg{chr} and any occurrence of \arg{chr} is doubled. In the 1733 future the behaviour will depend on the 1734 \prologflag{character_escapes} Prolog flag. 1735\end{description} 1736 1737 1738\subsection{Convenient functions to generate Prolog exceptions} 1739\label{sec:cerror} 1740 1741The typical implementation of a foreign predicate first uses the 1742PL_get_*() functions to extract C data types from the Prolog terms. 1743Failure of any of these functions is normally because the Prolog 1744term is of the wrong type. The *_ex() family of functions are 1745wrappers around (mostly) the PL_get_*() functions, such that we 1746can write code in the style below and get proper exceptions if 1747an argument is uninstantiated or of the wrong type. 1748 1749\begin{code} 1750/** set_size(+Name:atom, +Width:int, +Height:int) is det. 1751 1752static foreign_t 1753set_size(term_t name, term_t width, term_t height) 1754{ char *n; 1755 int w, h; 1756 1757 if ( !PL_get_chars(name, &n, CVT_ATOM|CVT_EXCEPTION) || 1758 !PL_get_integer_ex(with, &w) || 1759 !PL_get_integer_ex(height, &h) ) 1760 return FALSE; 1761 1762 ... 1763 1764} 1765\end{code} 1766 1767\begin{description} 1768 \cfunction{int}{PL_get_atom_ex}{term_t t, atom_t *a} 1769As PL_get_atom(), but raises a type or instantiation error if 1770\arg{t} is not an atom. 1771 1772 \cfunction{int}{PL_get_integer_ex}{term_t t, int *i} 1773As PL_get_integer(), but raises a type or instantiation error if 1774\arg{t} is not an integer, or a representation error if the Prolog 1775integer does not fit in a C \ctype{int}. 1776 1777 \cfunction{int}{PL_get_long_ex}{term_t t, long *i} 1778As PL_get_long(), but raises a type or instantiation error if 1779\arg{t} is not an atom, or a representation error if the Prolog 1780integer does not fit in a C \ctype{long}. 1781 1782 \cfunction{int}{PL_get_int64_ex}{term_t t, int64_t *i} 1783As PL_get_int64(), but raises a type or instantiation error if 1784\arg{t} is not an atom, or a representation error if the Prolog 1785integer does not fit in a C \ctype{int64_t}. 1786 1787 \cfunction{int}{PL_get_intptr_ex}{term_t t, intptr_t *i} 1788As PL_get_intptr(), but raises a type or instantiation error if 1789\arg{t} is not an atom, or a representation error if the Prolog 1790integer does not fit in a C \ctype{intptr_t}. 1791 1792 \cfunction{int}{PL_get_size_ex}{term_t t, size_t *i} 1793As PL_get_size(), but raises a type or instantiation error if 1794\arg{t} is not an atom, or a representation error if the Prolog 1795integer does not fit in a C \ctype{size_t}. 1796 1797 \cfunction{int}{PL_get_bool_ex}{term_t t, int *i} 1798As PL_get_bool(), but raises a type or instantiation error if 1799\arg{t} is not an boolean. 1800 1801 \cfunction{int}{PL_get_float_ex}{term_t t, double *f} 1802As PL_get_float(), but raises a type or instantiation error if 1803\arg{t} is not a float. 1804 1805 \cfunction{int}{PL_get_char_ex}{term_t t, int *p, int eof} 1806Get a character code from \arg{t}, where \arg{t} is either an 1807integer or an atom with length one. If \arg{eof} is \const{TRUE} 1808and \arg{t} is -1, \arg{p} is filled with -1. Raises an appropriate 1809error if the conversion is not possible. 1810 1811 \cfunction{int}{PL_get_pointer_ex}{term_t t, void **addrp} 1812As PL_get_pointer(), but raises a type or instantiation error if 1813\arg{t} is not a pointer. 1814 1815 \cfunction{int}{PL_get_list_ex}{term_t l, term_t h, term_t t} 1816As PL_get_list(), but raises a type or instantiation error if 1817\arg{t} is not a list. 1818 1819 \cfunction{int}{PL_get_nil_ex}{term_t l} 1820As PL_get_nil(), but raises a type or instantiation error if 1821\arg{t} is not the empty list. 1822 1823 \cfunction{int}{PL_unify_list_ex}{term_t l, term_t h, term_t t} 1824As PL_unify_list(), but raises a type error if \arg{t} is not a 1825variable, list-cell or the empty list. 1826 1827 \cfunction{int}{PL_unify_nil_ex}{term_t l} 1828As PL_unify_nil(), but raises a type error if \arg{t} is not a 1829variable, list-cell or the empty list. 1830 1831 \cfunction{int}{PL_unify_bool_ex}{term_t t, int val} 1832As PL_unify_bool(), but raises a type error if \arg{t} is not a 1833variable or a boolean. 1834\end{description} 1835 1836The second family of functions in this section simplifies the generation 1837of ISO compatible error terms. Any foreign function that calls this 1838function must return to Prolog with the return code of the error 1839function or the constant \const{FALSE}. If available, these error 1840functions add the name of the calling predicate to the error context. 1841See also PL_raise_exception(). 1842 1843\begin{description} 1844 \cfunction{int}{PL_instantiation_error}{term_t culprit} 1845Raise \const{instantiation_error}. \arg{Culprit} is ignored, but 1846should be bound to the term that is insufficiently instantiated. See 1847instantiation_error/1. 1848 1849 \cfunction{int}{PL_uninstantiation_error}{term_t culprit} 1850Raise \exam{uninstantiation_error(culprit)}. This should be called if an 1851argument that must be unbound at entry is bound to \arg{culprit}. This 1852error is typically raised for a pure output arguments such as a newly 1853created stream handle (e.g., the third argument of open/3). 1854 1855 \cfunction{int}{PL_representation_error}{const char *resource} 1856Raise \exam{representation_error(resource)}. See representation_error/1. 1857 1858 \cfunction{int}{PL_type_error}{const char *expected, term_t culprit} 1859Raise \exam{type_error(expected, culprit)}. See type_error/2. 1860 1861 \cfunction{int}{PL_domain_error}{const char *expected, term_t culprit} 1862Raise \exam{domain_error(expected, culprit)}. See domain_error/2. 1863 1864 \cfunction{int}{PL_existence_error}{const char *type, term_t culprit} 1865Raise \exam{existence_error(type, culprit)}. See type_error/2. 1866 1867 \cfunction{int}{PL_permission_error}{const char *operation, 1868 const char *type, term_t culprit} 1869Raise \exam{permission_error(operation, type, culprit)}. See 1870permission_error/3. 1871 \cfunction{int}{PL_resource_error}{const char *resource} 1872Raise \exam{resource_error(resource)}. See resource_error/1. 1873 \cfunction{int}{PL_syntax_error}{const char *message, IOSTREAM *in} 1874Raise \exam{syntax_error(message)}. If \arg{arg} is not \const{NULL}, 1875add information about the current position of the input stream. 1876\end{description} 1877 1878 1879\subsection{Serializing and deserializing Prolog terms} 1880\label{sec:foreign-serialize} 1881 1882\begin{description} 1883 \cfunction{int}{PL_put_term_from_chars}{term_t t, int flags, 1884 size_t len, const char *s} 1885Parse the text from the C-string \arg{s} holding \arg{len} bytes and 1886put the resulting term in \arg{t}. \arg{len} can be \exam{(size_t)-1}, 1887assuming a 0-terminated string. The \arg{flags} argument controls the 1888encoding and is currently one of \const{REP_UTF8} (string is UTF8 1889encoded), \const{REP_MB} (string is encoded in the current locale) 1890or 0 (string is encoded in ISO latin 1). The string may, but is 1891not required, to be closed by a full stop (.). 1892 1893If parsing produces an exception the behaviour depends on the 1894\const{CVT_EXCEPTION} flag. If present, the exception is propagated into 1895the environment. Otherwise the exceptuion is placed in \arg{t} and 1896the return value is \const{FALSE}.\footnote{The \const{CVT_EXCEPTION} 1897was added in version 8.3.12}. 1898\end{description} 1899 1900 1901\subsection{BLOBS: Using atoms to store arbitrary binary data} 1902\label{sec:blob} 1903 1904\index{Java}\index{COM} 1905SWI-Prolog atoms as well as strings can represent arbitrary binary data 1906of arbitrary length. This facility is attractive for storing foreign 1907data such as images in an atom. An atom is a unique handle to this 1908data and the atom garbage collector is able to destroy atoms that 1909are no longer referenced by the Prolog engine. This property of atoms 1910makes them attractive as a handle to foreign resources, such as 1911Java atoms, Microsoft's COM objects, etc., providing safe combined 1912garbage collection. 1913 1914To exploit these features safely and in an organised manner, the 1915SWI-Prolog foreign interface allows for creating `atoms' with additional 1916type information. The type is represented by a structure holding C 1917function pointers that tell Prolog how to handle releasing the atom, 1918writing it, sorting it, etc. Two atoms created with different types can 1919represent the same sequence of bytes. Atoms are first ordered on the 1920rank number of the type and then on the result of the 1921\cfuncref{compare}{} function. Rank numbers are assigned when the 1922type is registered. 1923 1924 1925\subsubsection{Defining a BLOB type} 1926\label{sec:blobtype} 1927 1928The type \ctype{PL_blob_t} represents a structure with the layout 1929displayed below. The structure contains additional fields at the 1930\ldots for internal bookkeeping as well as future extensions. 1931 1932\begin{code} 1933typedef struct PL_blob_t 1934{ uintptr_t magic; /* PL_BLOB_MAGIC */ 1935 uintptr_t flags; /* Bitwise or of PL_BLOB_* */ 1936 char * name; /* name of the type */ 1937 int (*release)(atom_t a); 1938 int (*compare)(atom_t a, atom_t b); 1939 int (*write)(IOSTREAM *s, atom_t a, int flags); 1940 void (*acquire)(atom_t a); 1941 ... 1942} PL_blob_t; 1943\end{code} 1944 1945For each type, exactly one such structure should be allocated. Its 1946first field must be initialised to \const{PL_BLOB_MAGIC}. The 1947\arg{flags} is a bitwise \emph{or} of the following constants: 1948 1949\begin{description} 1950 \constitem{PL_BLOB_TEXT} 1951If specified the blob is assumed to contain text and is considered 1952a normal Prolog atom. 1953 1954 \constitem{PL_BLOB_UNIQUE} 1955If specified the system ensures that the blob-handle is a unique 1956reference for a blob with the given type, length and content. 1957If this flag is not specified, each lookup creates a new blob. 1958 1959 \constitem{PL_BLOB_NOCOPY} 1960By default the content of the blob is copied. Using this flag the blob 1961references the external data directly. The user must ensure the provided 1962pointer is valid as long as the atom lives. If \const{PL_BLOB_UNIQUE} 1963is also specified, uniqueness is determined by comparing the pointer 1964rather than the data pointed at. 1965\end{description} 1966 1967The \arg{name} field represents the type name as available to Prolog. 1968See also current_blob/2. The other fields are function pointers that must 1969be initialised to proper functions or \const{NULL} to get the default 1970behaviour of built-in atoms. Below are the defined member functions: 1971 1972\begin{description} 1973 \cfunction{void}{acquire}{atom_t a} 1974Called if a new blob of this type is created through PL_put_blob() 1975or PL_unify_blob(). This callback may be used together with the 1976release hook to deal with reference-counted external objects. 1977 1978 \cfunction{int}{release}{atom_t a} 1979The blob (atom) \arg{a} is about to be released. This function 1980can retrieve the data of the blob using PL_blob_data(). If it 1981returns \const{FALSE} the atom garbage collector will \emph{not} 1982reclaim the atom. 1983 1984 \cfunction{int}{compare}{atom_t a, atom_t b} 1985Compare the blobs \arg{a} and \arg{b}, both of which are of the 1986type associated to this blob type. Return values are, as memcmp(), 1987$< 0$ if \arg{a} is less than \arg{b}, $= 0$ if both are equal, and 1988$> 0$ otherwise. 1989 1990 \cfunction{int}{write}{IOSTREAM *s, atom_t a, int flags} 1991Write the content of the blob \arg{a} to the stream \arg{s} 1992respecting the \arg{flags}. The \arg{flags} are a bitwise 1993\emph{or} of zero or more of the \const{PL_WRT_*} flags defined in 1994\file{SWI-Prolog.h}. This prototype is available if the 1995undocumented \file{SWI-Stream.h} is included \emph{before} 1996\file{SWI-Prolog.h}. 1997 1998If this function is not provided, write/1 emits the content 1999of the blob for blobs of type \const{PL_BLOB_TEXT} or a 2000string of the format \verb$<#$\textit{hex data}\verb$>$ 2001for binary blobs. 2002\end{description} 2003 2004If a blob type is registered from a loadable object (shared object 2005or DLL) the blob type must be deregistered before the object may be 2006released. 2007 2008\begin{description} 2009 \cfunction{int}{PL_unregister_blob_type}{PL_blob_t *type} 2010Unlink the blob type from the registered type and transform the type of 2011possible living blobs to \const{unregistered}, avoiding further 2012reference to the type structure, functions referred by it, as well as the 2013data. This function returns \const{TRUE} if no blobs of this type 2014existed and \const{FALSE} otherwise. PL_unregister_blob_type() is 2015intended for the uninstall() hook of foreign modules, avoiding further 2016references to the module. 2017\end{description} 2018 2019 2020\subsubsection{Accessing blobs} 2021\label{sec:blobaccess} 2022 2023The blob access functions are similar to the atom accessing functions. 2024Blobs being atoms, the atom functions operate on blobs and vice versa. 2025For clarity and possible future compatibility issues, however, it is not 2026advised to rely on this. 2027 2028\begin{description} 2029 \cfunction{int}{PL_is_blob}{term_t t, PL_blob_t **type} 2030Succeeds if \arg{t} refers to a blob, in which case \arg{type} is 2031filled with the type of the blob. 2032 2033 \cfunction{int}{PL_unify_blob}{term_t t, void *blob, size_t len, 2034 PL_blob_t *type} 2035Unify \arg{t} to a new blob constructed from the given data and 2036associated to the given type. See also PL_unify_atom_nchars(). 2037 2038 \cfunction{int}{PL_put_blob}{term_t t, void *blob, size_t len, 2039 PL_blob_t *type} 2040Store the described blob in \arg{t}. The return value indicates whether 2041a new blob was allocated (\const{FALSE}) or the blob is a reference to 2042an existing blob (\const{TRUE}). Reporting new/existing can be used to 2043deal with external objects having their own reference counts. If the 2044return is \const{TRUE} this reference count must be incremented, and it 2045must be decremented on blob destruction callback. See also 2046PL_put_atom_nchars(). 2047 2048 \cfunction{int}{PL_get_blob}{term_t t, void **blob, size_t *len, 2049 PL_blob_t **type} 2050If \arg{t} holds a blob or atom, get the data and type and return 2051\const{TRUE}. Otherwise return \const{FALSE}. Each result pointer 2052may be \const{NULL}, in which case the requested information is 2053ignored. 2054 2055 \cfunction{void *}{PL_blob_data}{atom_t a, 2056 size_t *len, 2057 PL_blob_t **type} 2058Get the data and type associated to a blob. This function is mainly 2059used from the callback functions described in \secref{blobtype}. 2060\end{description} 2061 2062 2063\subsection{Exchanging GMP numbers} 2064\label{sec:gmpforeign} 2065 2066If SWI-Prolog is linked with the GNU Multiple Precision Arithmetic 2067Library (GMP, used by default), the foreign interface provides functions 2068for exchanging numeric values to GMP types. To access these functions 2069the header \verb$<gmp.h>$ must be included \emph{before} 2070\verb$<SWI-Prolog.h>$. Foreign code using GMP linked to SWI-Prolog asks 2071for some considerations. 2072 2073\begin{itemize} 2074 \item SWI-Prolog normally rebinds the GMP allocation functions using 2075 mp_set_memory_functions(). This means SWI-Prolog must be initialised 2076 before the foreign code touches any GMP function. You can call 2077 \verb$PL_action(PL_GMP_SET_ALLOC_FUNCTIONS, TRUE)$ to force Prolog's 2078 GMP initialization without doing the rest of the 2079 Prolog initialization. If you do not want Prolog rebinding the 2080 GMP allocation, call \verb$PL_action(PL_GMP_SET_ALLOC_FUNCTIONS, FALSE)$ 2081 \emph{before} initializing Prolog. 2082 2083 \item On Windows, each DLL has its own memory pool. To make exchange 2084 of GMP numbers between Prolog and foreign code possible you must 2085 either let Prolog rebind the allocation functions (default) or you 2086 must recompile SWI-Prolog to link to a DLL version of the GMP 2087 library. 2088\end{itemize} 2089 2090Here is an example exploiting the function mpz_nextprime(): 2091 2092\begin{code} 2093#include <gmp.h> 2094#include <SWI-Prolog.h> 2095 2096static foreign_t 2097next_prime(term_t n, term_t prime) 2098{ mpz_t mpz; 2099 int rc; 2100 2101 mpz_init(mpz); 2102 if ( PL_get_mpz(n, mpz) ) 2103 { mpz_nextprime(mpz, mpz); 2104 2105 rc = PL_unify_mpz(prime, mpz); 2106 } else 2107 rc = FALSE; 2108 2109 mpz_clear(mpz); 2110 return rc; 2111} 2112 2113install_t 2114install() 2115{ PL_register_foreign("next_prime", 2, next_prime, 0); 2116} 2117\end{code} 2118 2119\begin{description} 2120 \cfunction{int}{PL_get_mpz}{term_t t, mpz_t mpz} 2121If \arg{t} represents an integer, \arg{mpz} is filled with the value and 2122the function returns \const{TRUE}. Otherwise \arg{mpz} is untouched and 2123the function returns \const{FALSE}. Note that \arg{mpz} must have been 2124initialised before calling this function and must be cleared using 2125mpz_clear() to reclaim any storage associated with it. 2126 2127 \cfunction{int}{PL_get_mpq}{term_t t, mpq_t mpq} 2128If \arg{t} is an integer or rational number (term \functor{rdiv}{2}), 2129\arg{mpq} is filled with the \emph{normalised} rational number and the 2130function returns \const{TRUE}. Otherwise \arg{mpq} is untouched and 2131the function returns \const{FALSE}. Note that \arg{mpq} must have been 2132initialised before calling this function and must be cleared using 2133mpq_clear() to reclaim any storage associated with it. 2134 2135 \cfunction{int}{PL_unify_mpz}{term_t t, mpz_t mpz} 2136Unify \arg{t} with the integer value represented by \arg{mpz} and return 2137\const{TRUE} on success. The \arg{mpz} argument is not changed. 2138 2139 \cfunction{int}{PL_unify_mpq}{term_t t, mpq_t mpq} 2140Unify \arg{t} with a rational number represented by \arg{mpq} and return 2141\const{TRUE} on success. Note that \arg{t} is unified with an integer if 2142the denominator is 1. The \arg{mpq} argument is not changed. 2143\end{description} 2144 2145 2146\subsection{Calling Prolog from C} 2147\label{sec:calling-prolog-from-c} 2148 2149The Prolog engine can be called from C. There are two interfaces for 2150this. For the first, a term is created that could be used as an argument 2151to call/1, and then PL_call() is used to call Prolog. This system is 2152simple, but does not allow to inspect the different answers to a 2153non-deterministic goal and is relatively slow as the runtime system 2154needs to find the predicate. The other interface is based on 2155PL_open_query(), PL_next_solution() and PL_cut_query() or 2156PL_close_query(). This mechanism is more powerful, but also more 2157complicated to use. 2158 2159 2160\subsubsection{Predicate references} 2161\label{sec:foreign-predicate-handle} 2162 2163This section discusses the functions used to communicate about 2164predicates. Though a Prolog predicate may be defined or not, redefined, 2165etc., a Prolog predicate has a handle that is neither destroyed nor moved. 2166This handle is known by the type \ctype{predicate_t}. 2167 2168\begin{description} 2169 \cfunction{predicate_t}{PL_pred}{functor_t f, module_t m} 2170Return a handle to a predicate for the specified name/arity in the given 2171module. This function always succeeds, creating a handle for an 2172undefined predicate if no handle was available. If the module argument 2173\arg{m} is \const{NULL}, the current context module is used. 2174 2175 \cfunction{predicate_t}{PL_predicate}{const char *name, int arity, 2176 const char* module} 2177Same as PL_pred(), but provides a more convenient interface to 2178the C programmer. 2179 2180 \cfunction{void}{PL_predicate_info}{predicate_t p, atom_t *n, 2181 size_t *a, module_t *m} 2182Return information on the predicate \arg{p}. The name is stored over 2183\arg{n}, the arity over \arg{a}, while \arg{m} receives the definition 2184module. Note that the latter need not be the same as specified with 2185PL_predicate(). If the predicate is imported into the module given to 2186PL_predicate(), this function will return the module where the predicate 2187is defined. Any of the arguments \arg{n}, \arg{a} and \arg{m} can be 2188\const{NULL}. 2189\end{description} 2190 2191 2192\subsubsection{Initiating a query from C} 2193\label{sec:foreign-create-query} 2194 2195This section discusses the functions for creating and manipulating 2196queries from C. Note that a foreign context can have at most one 2197active query. This implies that it is allowed to make strictly nested 2198calls between C and Prolog (Prolog calls C, calls Prolog, calls C, 2199etc.), but it is \strong{not} allowed to open multiple queries and start 2200generating solutions for each of them by calling PL_next_solution(). 2201Be sure to call PL_cut_query() or PL_close_query() on any query you 2202opened before opening the next or returning control back to Prolog. 2203 2204 2205\begin{description} 2206\cfunction{qid_t}{PL_open_query}{module_t ctx, int flags, 2207 predicate_t p, term_t +t0} 2208 2209Opens a query and returns an identifier for it. \arg{ctx} is the {\em 2210context module} of the goal. When \const{NULL}, the context module of 2211the calling context will be used, or \const{user} if there is no calling 2212context (as may happen in embedded systems). Note that the context 2213module only matters for \jargon{meta-predicates}. See meta_predicate/1, 2214context_module/1 and module_transparent/1. The \arg{p} argument 2215specifies the predicate, and should be the result of a call to PL_pred() 2216or PL_predicate(). Note that it is allowed to store this handle as 2217global data and reuse it for future queries. The term reference \arg{t0} 2218is the first of a vector of term references as returned by 2219PL_new_term_refs(n). 2220 2221The \arg{flags} arguments provides some additional options concerning 2222debugging and exception handling. It is a bitwise \emph{or} of the following 2223values: 2224 2225\begin{description} 2226 \definition{\const{PL_Q_NORMAL}} 2227Normal operation. The debugger inherits its settings from the environment. 2228If an exception occurs that is not handled in Prolog, a message is printed 2229and the tracer is started to debug the error.% 2230 \footnote{Do not pass the integer 0 for normal operation, as 2231 this is interpreted as \const{PL_Q_NODEBUG} for 2232 backward compatibility reasons.} 2233 \definition{\const{PL_Q_NODEBUG}} 2234Switch off the debugger while executing the goal. This option is used 2235by many calls to hook-predicates to avoid tracing the hooks. An example 2236is print/1 calling portray/1 from foreign code. 2237 \definition{\const{PL_Q_CATCH_EXCEPTION}} 2238If an exception is raised while executing the goal, do not report it, but 2239make it available for PL_exception(). 2240 \definition{\const{PL_Q_PASS_EXCEPTION}} 2241As \const{PL_Q_CATCH_EXCEPTION}, but do not invalidate the exception-term 2242while calling PL_close_query(). This option is experimental. 2243 \definition{\const{PL_Q_ALLOW_YIELD}} 2244Support the \const{I_YIELD} instruction for engine-based coroutining. 2245See \nopredref{\$engine_yield}{2} in \file{boot/init.pl} for details. 2246 \definition{\const{PL_Q_EXT_STATUS}} 2247Make PL_next_solution() return extended status. Instead of only 2248\const{TRUE} or \const{FALSE} extended status as illustrated in the 2249following table: 2250 2251\begin{center} 2252\begin{tabular}{llp{4in}} 2253\bf Extended & \bf Normal & \\ 2254\hline 2255PL_S_EXCEPTION & FALSE & Exception available through PL_exception() \\ 2256PL_S_FALSE & FALSE & Query failed \\ 2257PL_S_TRUE & TRUE & Query succeeded with choicepoint \\ 2258PL_S_LAST & TRUE & Query succeeded without choicepoint \\ 2259\end{tabular} 2260\end{center} 2261\end{description} 2262 2263 2264PL_open_query() can return the query identifier `0' if there is not enough 2265space on the environment stack. This function succeeds, even if the 2266referenced predicate is not defined. In this case, running the query 2267using PL_next_solution() will return an existence_error. See 2268PL_exception(). 2269 2270The example below opens a query to the predicate \verb$is_a/2$ to find the 2271ancestor of `me'. The reference to the predicate is valid for the 2272duration of the process and may be cached by the client. 2273 2274\begin{code} 2275char * 2276ancestor(const char *me) 2277{ term_t a0 = PL_new_term_refs(2); 2278 static predicate_t p; 2279 2280 if ( !p ) 2281 p = PL_predicate("is_a", 2, "database"); 2282 2283 PL_put_atom_chars(a0, me); 2284 PL_open_query(NULL, PL_Q_NORMAL, p, a0); 2285 ... 2286} 2287\end{code} 2288 2289 \cfunction{int}{PL_next_solution}{qid_t qid} 2290Generate the first (next) solution for the given query. The return 2291value is \const{TRUE} if a solution was found, or \const{FALSE} to indicate 2292the query could not be proven. This function may be called repeatedly 2293until it fails to generate all solutions to the query. 2294 2295 \cfunction{int}{PL_cut_query}{qid_t qid} 2296Discards the query, but does not delete any of the data created by the 2297query. It just invalidates \arg{qid}, allowing for a new call to 2298PL_open_query() in this context. PL_cut_query() may invoke cleanup 2299handlers (see setup_call_cleanup/3) and therefore may experience 2300exceptions. If an exception occurs the return value is \const{FALSE} 2301and the exception is accessible through \exam{PL_exception(0)}. 2302 2303 \cfunction{int}{PL_close_query}{qid_t qid} 2304As PL_cut_query(), but all data and bindings created by the query are 2305destroyed. 2306 2307 \cfunction{qid_t}{PL_current_query}{void} 2308Returns the query id of of the current query or \const{0} if the 2309current thread is not executing any queries. 2310 2311 \cfunction{int}{PL_call_predicate}{module_t m, int flags, 2312 predicate_t pred, term_t +t0} 2313Shorthand for PL_open_query(), PL_next_solution(), PL_cut_query(), 2314generating a single solution. The arguments are the same as for 2315PL_open_query(), the return value is the same as PL_next_solution(). 2316 \cfunction{int}{PL_call}{term_t t, module_t m} 2317Call term \arg{t} just like the Prolog predicate once/1. \arg{t} is called 2318in the module \arg{m}, or in the context module if \arg{m} == NULL. 2319Returns \const{TRUE} if the call succeeds, \const{FALSE} otherwise. 2320\Figref{calling} shows an example to obtain the number of 2321defined atoms. All checks are omitted to improve readability. 2322\end{description} 2323 2324\subsection{Discarding Data} 2325\label{sec:foreign-discard-term-t} 2326 2327The Prolog data created and term references needed to set up the call 2328and/or analyse the result can in most cases be discarded right after the 2329call. PL_close_query() allows for destroying the data, while leaving 2330the term references. The calls below may be used to destroy 2331term references and data. See \figref{calling} for an example. 2332 2333\begin{description} 2334\cfunction{fid_t}{PL_open_foreign_frame}{} 2335Create a foreign frame, holding a mark that allows the system to 2336undo bindings and destroy data created after it, as well as providing 2337the environment for creating term references. This function is called 2338by the kernel before calling a foreign predicate. 2339\cfunction{void}{PL_close_foreign_frame}{fid_t id} 2340Discard all term references created after the frame was opened. All 2341other Prolog data is retained. This function is called by the kernel 2342whenever a foreign function returns control back to Prolog. 2343\cfunction{void}{PL_discard_foreign_frame}{fid_t id} 2344Same as PL_close_foreign_frame(), but also undo all bindings made since 2345the open and destroy all Prolog data. 2346\cfunction{void}{PL_rewind_foreign_frame}{fid_t id} 2347Undo all bindings and discard all term references created since the 2348frame was created, but do not pop the frame. That is, the same frame 2349can be rewound multiple times, and must eventually be closed or 2350discarded. 2351\end{description} 2352 2353It is obligatory to call either of the two closing functions to discard 2354a foreign frame. Foreign frames may be nested. 2355 2356 2357\begin{figure} 2358 2359\begin{code} 2360int 2361count_atoms() 2362{ fid_t fid = PL_open_foreign_frame(); 2363 term_t goal = PL_new_term_ref(); 2364 term_t a1 = PL_new_term_ref(); 2365 term_t a2 = PL_new_term_ref(); 2366 functor_t s2 = PL_new_functor(PL_new_atom("statistics"), 2); 2367 int atoms; 2368 2369 PL_put_atom_chars(a1, "atoms"); 2370 PL_cons_functor(goal, s2, a1, a2); 2371 PL_call(goal, NULL); /* call it in current module */ 2372 2373 PL_get_integer(a2, &atoms); 2374 PL_discard_foreign_frame(fid); 2375 2376 return atoms; 2377} 2378\end{code} 2379 2380 \caption{Calling Prolog} 2381 \label{fig:calling} 2382\end{figure} 2383 2384\subsection{String buffering} 2385\label{sec:foreign-strings} 2386 2387Many of the functions of the foreign language interface involve strings. 2388Some of these strings point into static memory like those associated 2389with atoms. These strings are valid as long as the atom is protected 2390against atom garbage collection, which generally implies the atom must 2391be locked using PL_register_atom() or be part of an accessible term. 2392Other strings are more volatile. Several functions provide a BUF_* flag 2393that can be set to either \const{BUF_STACK} (default) or 2394\const{BUF_MALLOC}. Strings returned by a function accepting 2395\const{BUF_MALLOC} \textbf{must} be freed using PL_free(). Strings 2396returned using \const{BUF_STACK} are pushed on a stack that is cleared 2397when a foreign predicate returns control back to Prolog. More fine 2398grained control may be needed if functions that return strings are 2399called outside the context of a foreign predicate or a foreign predicate 2400creates many strings during its execution. Temporary strings are scoped 2401using these macros: 2402 2403\begin{description} 2404 \cmacro{void}{PL_STRINGS_MARK}{} 2405 \nodescription 2406 \cmacro{void}{PL_STRINGS_RELEASE}{} 2407These macros must be paired and create a C \jargon{block} (\{...\}). Any 2408string created using \const{BUF_STACK} after PL_STRINGS_MARK() is 2409released by the corresponding PL_STRINGS_RELEASE(). These macros 2410should be used like below 2411 2412\begin{code} 2413 ... 2414 PL_STRINGS_MARK(); 2415 <operations involving strings> 2416 PL_STRINGS_RELEASE(); 2417 ... 2418\end{code} 2419\end{description} 2420 2421The Prolog flag \prologflag{string_stack_tripwire} may be used to set a 2422\jargon{tripwire} to help finding places where scoping strings may help 2423reducing resources. 2424 2425\subsection{Foreign Code and Modules} 2426\label{sec:foreign-modules} 2427 2428Modules are identified via a unique handle. The following functions 2429are available to query and manipulate modules. 2430 2431\begin{description} 2432 \cfunction{module_t}{PL_context}{} 2433Return the module identifier of the context module of the currently 2434active foreign predicate. 2435 2436 \cfunction{int}{PL_strip_module}{term_t +raw, module_t *m, term_t -plain} 2437Utility function. If \arg{raw} is a term, possibly holding the module 2438construct \mbox{<module>{\tt :}<rest>}, this function will make 2439\arg{plain} a reference to <rest> and fill \arg{module *} with <module>. 2440For further nested module constructs the innermost module is returned 2441via \arg{module *}. If \arg{raw} is not a module construct, \arg{raw} 2442will simply be put in \arg{plain}. The value pointed to by \arg{m} must 2443be initialized before calling PL_strip_module(), either to the default 2444module or to \const{NULL}. A \const{NULL} value is replaced by the 2445current context module if \arg{raw} carries no module. The following 2446example shows how to obtain the plain term and module if the default 2447module is the user module: 2448 2449\begin{code} 2450{ module m = PL_new_module(PL_new_atom("user")); 2451 term_t plain = PL_new_term_ref(); 2452 2453 PL_strip_module(term, &m, plain); 2454 ... 2455} 2456\end{code} 2457 2458 2459\cfunction{atom_t}{PL_module_name}{module_t module} 2460Return the name of \arg{module} as an atom. 2461\cfunction{module_t}{PL_new_module}{atom_t name} 2462Find an existing module or create a new module with the name \arg{name}. 2463\end{description} 2464 2465 2466\subsection{Prolog exceptions in foreign code} 2467\label{sec:foreign-exceptions} 2468 2469This section discusses PL_exception(), PL_throw() and 2470PL_raise_exception(), the interface functions to detect and generate 2471Prolog exceptions from C code. PL_throw() and PL_raise_exception() from 2472the C interface raise an exception from foreign code. PL_throw() 2473exploits the C function longjmp() to return immediately to the innermost 2474PL_next_solution(). PL_raise_exception() registers the exception term 2475and returns \const{FALSE}. If a foreign predicate returns \const{FALSE}, while 2476an exception term is registered, a Prolog exception will be raised by 2477the virtual machine. 2478 2479Calling these functions outside the context of a function implementing a 2480foreign predicate results in undefined behaviour. 2481 2482PL_exception() may be used after a call to PL_next_solution() fails, 2483and returns a term reference to an exception term if an exception 2484was raised, and 0 otherwise. 2485 2486If a C function implementing a predicate calls Prolog and detects 2487an exception using PL_exception(), it can handle this exception or 2488return with the exception. Some caution is required though. It is 2489\strong{not} allowed to call PL_close_query() or 2490PL_discard_foreign_frame() afterwards, as this will invalidate the 2491exception term. Below is the code that calls a Prolog-defined 2492arithmetic function (see arithmetic_function/1). 2493 2494If PL_next_solution() succeeds, the result is analysed and translated to 2495a number, after which the query is closed and all Prolog data created 2496after PL_open_foreign_frame() is destroyed. On the other hand, if 2497PL_next_solution() fails and if an exception was raised, just pass it. 2498Otherwise generate an exception (PL_error() is an internal call for 2499building the standard error terms and calling PL_raise_exception()). 2500After this, the Prolog environment should be discarded using 2501PL_cut_query() and PL_close_foreign_frame() to avoid invalidating the 2502exception term. 2503 2504\begin{code} 2505static int 2506prologFunction(ArithFunction f, term_t av, Number r) 2507{ int arity = f->proc->definition->functor->arity; 2508 fid_t fid = PL_open_foreign_frame(); 2509 qid_t qid; 2510 int rval; 2511 2512 qid = PL_open_query(NULL, PL_Q_NORMAL, f->proc, av); 2513 2514 if ( PL_next_solution(qid) ) 2515 { rval = valueExpression(av+arity-1, r); 2516 PL_close_query(qid); 2517 PL_discard_foreign_frame(fid); 2518 } else 2519 { term_t except; 2520 2521 if ( (except = PL_exception(qid)) ) 2522 { rval = PL_throw(except); /* pass exception */ 2523 } else 2524 { char *name = stringAtom(f->proc->definition->functor->name); 2525 2526 /* generate exception */ 2527 rval = PL_error(name, arity-1, NULL, ERR_FAILED, f->proc); 2528 } 2529 2530 PL_cut_query(qid); /* do not destroy data */ 2531 PL_close_foreign_frame(fid); /* same */ 2532 } 2533 2534 return rval; 2535} 2536\end{code} 2537 2538 2539\begin{description} 2540 \cfunction{int}{PL_raise_exception}{term_t exception} 2541Generate an exception (as throw/1) and return \const{FALSE}. Below is 2542an example returning an exception from a foreign predicate: 2543 2544\begin{code} 2545foreign_t 2546pl_hello(term_t to) 2547{ char *s; 2548 2549 if ( PL_get_atom_chars(to, &s) ) 2550 { Sprintf("Hello \"%s\"\n", s); 2551 2552 PL_succeed; 2553 } else 2554 { term_t except = PL_new_term_ref(); 2555 2556 PL_unify_term(except, 2557 PL_FUNCTOR_CHARS, "type_error", 2, 2558 PL_CHARS, "atom", 2559 PL_TERM, to); 2560 2561 return PL_raise_exception(except); 2562 } 2563} 2564\end{code} 2565 \cfunction{int}{PL_throw}{term_t exception} 2566Similar to PL_raise_exception(), but returns using the C longjmp() 2567function to the innermost PL_next_solution(). 2568 2569 \cfunction{term_t}{PL_exception}{qid_t qid} 2570If PL_next_solution() fails, this can be due to normal failure of the 2571Prolog call, or because an exception was raised using throw/1. This 2572function returns a handle to the exception term if an exception was 2573raised, or 0 if the Prolog goal simply failed. If there is an exception, 2574PL_exception() allocates a term-handle using PL_new_term_ref() that is 2575used to return the exception term. 2576 2577Additionally, \verb$PL_exception(0)$ returns the pending exception in 2578the current query or 0 if no exception is pending. This can be used to 2579check the error status after a failing call to, e.g., one of the 2580unification functions. 2581 2582 \cfunction{void}{PL_clear_exception}{void} 2583Tells Prolog that the encountered exception must be ignored. This 2584function must be called if control remains in C after a previous API 2585call fails with an exception.\footnote{This feature is non-portable. 2586Other Prolog systems (e.g., YAP) have no facilities to ignore raised 2587exceptions, and the design of YAP's exception handling does not support 2588such a facility.} 2589\end{description} 2590 2591 2592\subsection{Catching Signals (Software Interrupts)} \label{sec:csignal} 2593 2594SWI-Prolog offers both a C and Prolog interface to deal with software 2595interrupts (signals). The Prolog mapping is defined in 2596\secref{signal}. This subsection deals with handling signals from C. 2597 2598If a signal is not used by Prolog and the handler does not call Prolog 2599in any way, the native signal interface routines may be used. 2600 2601Any handler that wishes to call one of the Prolog interface functions 2602should call PL_sigaction() to install the handler. PL_signal() provides 2603a deprecated interface that is notably not capable of properly restoring 2604the old signal status if the signal was previously handled by Prolog. 2605 2606\begin{description} 2607 \cfunction{int}{PL_sigaction}{int sig, pl_sigaction_t *act, 2608 pl_sigaction_t *oldact} 2609Install or query the status for signal \arg{sig}. The signal is 2610an integer between 1 and 64, where the where the signals up to 32 2611are mapped to OS signals and signals above that are handled by 2612Prolog's synchronous signal handling. The \ctype{pl_sigaction_t} 2613is a struct with the following definition: 2614 2615\begin{code} 2616typedef struct pl_sigaction 2617{ void (*sa_cfunction)(int); /* traditional C function */ 2618 predicate_t sa_predicate; /* call a predicate */ 2619 int sa_flags; /* additional flags */ 2620} pl_sigaction_t; 2621\end{code} 2622 2623The \const{sa_flags} is a bitwise or of \const{PLSIG_THROW}, 2624\const{PLSIG_SYNC} and \const{PLSIG_NOFRAME}. Signal handling is enabled 2625if \const{PLSIG_THROW} is provided, \const{sa_cfunction} or 2626\const{sa_predicate} is provided. \const{sa_predicate} is a predicate 2627handle for a predicate with arity~1. If no action is provided the signal 2628handling for this signal is restored to the default before 2629PL_initialise() was called. 2630 2631Finally, 0 (zero) may be passed for \arg{sig}. In that case the system 2632allocates a free signal in the \textit{Prolog range} (32\ldots{}64). 2633Such signal handler are activated using PL_thread_raise(). 2634 2635 \cfunction{void (*)()}{PL_signal}{sig, func} 2636This function is equivalent to the BSD-Unix signal() function, 2637regardless of the platform used. The signal handler is blocked 2638while the signal routine is active, and automatically reactivated 2639after the handler returns. 2640 2641After a signal handler is registered using this function, the native 2642signal interface redirects the signal to a generic signal handler inside 2643SWI-Prolog. This generic handler validates the environment, creates a 2644suitable environment for calling the interface functions described in 2645this chapter and finally calls the registered user-handler. 2646 2647By default, signals are handled asynchronously (i.e., at the time they 2648arrive). It is inherently dangerous to call extensive code fragments, 2649and especially exception related code from asynchronous handlers. The 2650interface allows for \jargon{synchronous} handling of signals. In this 2651case the native OS handler just schedules the signal using PL_raise(), 2652which is checked by PL_handle_signals() at the call- and redo-port. This 2653behaviour is realised by \emph{or}-ing \arg{sig} with the constant 2654\const{PL_SIGSYNC}.% 2655 \footnote{A better default would be to use synchronous handling, 2656 but this interface preserves backward compatibility.} 2657 2658Signal handling routines may raise exceptions using 2659PL_raise_exception(). The use of PL_throw() is not safe. If a synchronous 2660handler raises an exception, the exception is delayed to the next call 2661to PL_handle_signals(); 2662 2663 \cfunction{int}{PL_raise}{int sig} 2664Register \arg{sig} for \emph{synchronous} handling by Prolog. 2665Synchronous signals are handled at the call-port or if foreign code 2666calls PL_handle_signals(). See also thread_signal/2. 2667 2668 \cfunction{int}{PL_handle_signals}{void} 2669Handle any signals pending from PL_raise(). PL_handle_signals() is 2670called at each pass through the call- and redo-port at a safe point. 2671Exceptions raised by the handler using PL_raise_exception() are properly 2672passed to the environment. 2673 2674The user may call this function inside long-running foreign functions 2675to handle scheduled interrupts. This routine returns the number of 2676signals handled. If a handler raises an exception, the return value 2677is -1 and the calling routine should return with \const{FALSE} as 2678soon as possible. 2679 2680 \cfunction{int}{PL_get_signum_ex}{term_t t, int *sig} 2681Extract a signal specification from a Prolog term and store as an integer 2682signal number in \arg{sig}. The specification is an integer, a lowercase 2683signal name without \const{SIG} or the full signal name. These refer 2684to the same: \verb$9$, \verb$kill$ and \verb$SIGKILL$. Leaves a typed, 2685domain or instantiation error if the conversion fails. 2686\end{description} 2687 2688 2689\subsection{Miscellaneous} 2690\label{sec:foreign-misc} 2691 2692\subsubsection{Term Comparison} 2693\label{sec:foreign-compare} 2694 2695\begin{description} 2696\cfunction{int}{PL_compare}{term_t t1, term_t t2} 2697 Compares two terms using the standard order of terms and returns -1, 2698 0 or 1. See also compare/3. 2699\cfunction{int}{PL_same_compound}{term_t t1, term_t t2} 2700 Yields \const{TRUE} if \arg{t1} and \arg{t2} refer to physically 2701 the same compound term and \const{FALSE} otherwise. 2702\end{description} 2703 2704\subsubsection{Recorded database} 2705\label{sec:foreign-recorded} 2706 2707In some applications it is useful to store and retrieve Prolog terms 2708from C code. For example, the XPCE graphical environment does this for 2709storing arbitrary Prolog data as slot-data of XPCE objects. 2710 2711Please note that the returned handles have no meaning at the Prolog 2712level and the recorded terms are not visible from Prolog. The functions 2713PL_recorded() and PL_erase() are the only functions that can operate on 2714the stored term. 2715 2716Two groups of functions are provided. The first group (PL_record() and 2717friends) store Prolog terms on the Prolog heap for retrieval during the 2718same session. These functions are also used by recorda/3 and friends. 2719The recorded database may be used to communicate Prolog terms between 2720threads. 2721 2722\begin{description} 2723\cfunction{record_t}{PL_record}{term_t +t} 2724 Record the term \arg{t} into the Prolog database as recorda/3 and 2725 return an opaque handle to the term. The returned handle remains 2726 valid until PL_erase() is called on it. PL_recorded() is used to 2727 copy recorded terms back to the Prolog stack. 2728 2729\cfunction{record_t}{PL_duplicate_record}{record_t record} 2730 Return a duplicate of \arg{record}. As records are read-only 2731 objects this function merely increments the records reference 2732 count. 2733 2734\cfunction{int}{PL_recorded}{record_t record, term_t -t} 2735 Copy a recorded term back to the Prolog stack. The same record 2736 may be used to copy multiple instances at any time to the Prolog 2737 stack. Returns \const{TRUE} on success, and \const{FALSE} if 2738 there is not enough space on the stack to accommodate the term. 2739 See also PL_record() and PL_erase(). 2740 2741\cfunction{void}{PL_erase}{record_t record} 2742 Remove the recorded term from the Prolog database, reclaiming all 2743 associated memory resources. 2744\end{description} 2745 2746The second group (headed by PL_record_external()) provides the same 2747functionality, but the returned data has properties that enable storing 2748the data on an external device. It has been designed to make it possible 2749to store Prolog terms fast and compact in an external database. Here are 2750the main features: 2751 2752\begin{itemlist} 2753 \item [Independent of session] 2754Records can be communicated to another Prolog session and made visible 2755using PL_recorded_external(). 2756 \item [Binary] 2757The representation is binary for maximum performance. The returned data 2758may contain zero bytes. 2759 \item [Byte-order independent] 2760The representation can be transferred between machines with different 2761byte order. 2762 \item [No alignment restrictions] 2763There are no memory alignment restrictions and copies of the record 2764can thus be moved freely. For example, it is possible to use this 2765representation to exchange terms using shared memory between different 2766Prolog processes. 2767 \item [Compact] 2768It is assumed that a smaller memory footprint will eventually outperform 2769slightly faster representations. 2770 \item [Stable] 2771The format is designed for future enhancements without breaking 2772compatibility with older records. 2773\end{itemlist} 2774 2775\begin{description} 2776\cfunction{char *}{PL_record_external}{term_t +t, size_t *len} 2777 Record the term \arg{t} into the Prolog database as recorda/3 and 2778 return an opaque handle to the term. The returned handle remains 2779 valid until PL_erase_external() is called on it. 2780 2781 It is allowed to copy the data and use PL_recorded_external() on 2782 the copy. The user is responsible for the memory management of 2783 the copy. After copying, the original may be discarded using 2784 PL_erase_external(). 2785 2786 PL_recorded_external() is used to copy such recorded terms back to 2787 the Prolog stack. 2788\cfunction{int}{PL_recorded_external}{const char *record, term_t -t} 2789 Copy a recorded term back to the Prolog stack. The same record 2790 may be used to copy multiple instances at any time to the Prolog 2791 stack. See also PL_record_external() and PL_erase_external(). 2792\cfunction{int}{PL_erase_external}{char *record} 2793 Remove the recorded term from the Prolog database, reclaiming all 2794 associated memory resources. 2795\end{description} 2796 2797 2798\subsubsection{Database} 2799\label{sec:foreign-db} 2800 2801\begin{description} 2802 \cfunction{int}{PL_assert}{term_t t, module_t m, int flags} 2803Provides direct access to asserta/1 and assertz/1 by asserting \arg{t} 2804into the database in the module \arg{m}. Defined flags are: 2805 2806 \begin{description} 2807 \termitem{PL_ASSERTZ}{} 2808Add the new clause as last. Calls assertz/1. This macros is defined as 0 2809and thus the default. 2810 \termitem{PL_ASSERTA}{} 2811Add the new clause as first. Calls asserta/1. 2812 \termitem{PL_CREATE_THREAD_LOCAL}{} 2813If the predicate is not defined, create it as thread-local. See 2814thread_local/1. 2815 \termitem{PL_CREATE_INCREMENTAL}{} 2816If the predicate is not defined, create it as \jargon{incremental} see 2817table/1 and \secref{tabling-incremental}. 2818 \end{description} 2819 2820On success this function returns \const{TRUE}. On failure \const{FALSE} 2821is returned and an exception is left in the environment that describes 2822the reason of failure. See PL_exception(). 2823 2824This predicate bypasses creating a Prolog callback environment and is 2825faster than setting up a call to assertz/1. It may be used together with 2826PL_chars_to_term(), but the typical use case will create a number of 2827clauses for the same predicate. The fastest way to achieve this is by 2828creating a term that represents the invariable structure of the desired 2829clauses using variables for the variable sub terms. Now we can loop over 2830the data, binding the variables, asserting the term and undoing the 2831bindings. Below is an example loading words from a file that contains a 2832word per line. 2833 2834\begin{code} 2835#include <SWI-Prolog.h> 2836#include <stdio.h> 2837#include <string.h> 2838 2839#define MAXWLEN 256 2840 2841static foreign_t 2842load_words(term_t name) 2843{ char *fn; 2844 2845 if ( PL_get_file_name(name, &fn, PL_FILE_READ) ) 2846 { FILE *fd = fopen(fn, "r"); 2847 char word[MAXWLEN]; 2848 module_t m = PL_new_module(PL_new_atom("words")); 2849 term_t cl = PL_new_term_ref(); 2850 term_t w = PL_new_term_ref(); 2851 fid_t fid; 2852 2853 if ( !PL_unify_term(cl, PL_FUNCTOR_CHARS, "word", 1, PL_TERM, w) ) 2854 return FALSE; 2855 2856 if ( (fid = PL_open_foreign_frame()) ) 2857 { while(fgets(word, sizeof(word), fd)) 2858 { size_t len; 2859 2860 if ( (len=strlen(word)) ) 2861 { word[len-1] = '\0'; 2862 if ( !PL_unify_chars(w, PL_ATOM|REP_MB, (size_t)-1, word) || 2863 !PL_assert(cl, m, 0) ) 2864 return FALSE; 2865 PL_rewind_foreign_frame(fid); 2866 } 2867 } 2868 2869 PL_close_foreign_frame(fid); 2870 } 2871 2872 fclose(fd); 2873 return TRUE; 2874 } 2875 2876 return FALSE; 2877} 2878 2879install_t 2880install(void) 2881{ PL_register_foreign("load_words", 1, load_words, 0); 2882} 2883\end{code} 2884\end{description} 2885 2886 2887\subsubsection{Getting file names} \label{sec:cfilenames} 2888 2889The function PL_get_file_name() provides access to Prolog filenames and 2890its file-search mechanism described with absolute_file_name/3. Its 2891existence is motivated to realise a uniform interface to deal with 2892file properties, search, naming conventions, etc., from foreign code. 2893 2894\begin{description} 2895 \cfunction{int}{PL_get_file_name}{term_t spec, char **name, int flags} 2896Translate a Prolog term into a file name. The name is stored in the 2897buffer stack described with the PL_get_chars() option \const{BUF_STACK}. 2898Conversion from the internal UNICODE encoding is done using standard C 2899library functions. \arg{flags} is a bit-mask controlling the conversion 2900process. Options are: 2901 2902\begin{description} 2903 \definition{\const{PL_FILE_ABSOLUTE}} 2904Return an absolute path to the requested file. 2905 \definition{\const{PL_FILE_OSPATH}} 2906Return the name using the hosting OS conventions. On MS-Windows, 2907\chr{\} is used to separate directories rather than the canonical 2908\chr{/}. 2909 \definition{\const{PL_FILE_SEARCH}} 2910Invoke absolute_file_name/3. This implies rules from file_search_path/2 2911are used. 2912 \definition{\const{PL_FILE_EXIST}} 2913Demand the path to refer to an existing entity. 2914 \definition{\const{PL_FILE_READ}} 2915Demand read-access on the result. 2916 \definition{\const{PL_FILE_WRITE}} 2917Demand write-access on the result. 2918 \definition{\const{PL_FILE_EXECUTE}} 2919Demand execute-access on the result. 2920 \definition{\const{PL_FILE_NOERRORS}} 2921Do not raise any exceptions. 2922\end{description} 2923 2924\cfunction{int}{PL_get_file_nameW}{term_t spec, wchar_t **name, int flags} 2925Same as PL_get_file_name(), but returns the filename as a wide-character 2926string. This is intended for Windows to access the Unicode version of 2927the Win32 API. Note that the flag \const{PL_FILE_OSPATH} must be 2928provided to fetch a filename in OS native (e.g., \verb$C:\x\y$) 2929notation. 2930\end{description} 2931 2932 2933\subsubsection{Dealing with Prolog flags from C} 2934\label{sec:cprologflags} 2935 2936Foreign code can set or create Prolog flags using PL_set_prolog_flag(). 2937See set_prolog_flag/2 and create_prolog_flag/3. To retrieve the value 2938of a flag you can use PL_current_prolog_flag(). 2939 2940\begin{description} 2941 \cfunction{int}{PL_set_prolog_flag}{const char *name, int type, ...} 2942Set/create a Prolog flag from C. \arg{name} is the name of the affected 2943flag. \arg{type} is one of the values below, which also 2944dictates the type of the final argument. The function returns 2945\const{TRUE} on success and \const{FALSE} on failure. This function 2946can be called \emph{before} PL_initialise(), making the flag available 2947to the Prolog startup code. 2948 2949\begin{description} 2950 \definition{\const{PL_BOOL}} 2951Create a boolean (\const{true} or \const{false}) flag. The argument must 2952be an \ctype{int}. 2953 \definition{\const{PL_ATOM}} 2954Create a flag with an atom as value. The argument must be of type 2955\ctype{const char *}. 2956 \definition{\const{PL_INTEGER}} 2957Create a flag with an integer as value. The argument must be of type 2958\ctype{intptr_t *}. 2959\end{description} 2960\end{description} 2961 2962 2963\begin{description} 2964 \cfunction{int}{PL_current_prolog_flag}{atom_t name, int type, void *value} 2965Retrieve the value of a Prolog flag from C. \arg{name} is the name of the 2966flag as an \const{atom_t} (see current_prolog_flag/2). 2967\arg{type} specifies the kind of value to be retrieved, it is one 2968of the values below. \arg{value} is a pointer to a location where 2969to store the value. The user is responsible for making sure this memory 2970location is of the appropriate size/type (see the returned types below 2971to determine the size/type). 2972The function returns \const{TRUE} on success 2973and \const{FALSE} on failure. 2974 2975\begin{description} 2976 \definition{\const{PL_ATOM}} 2977Retrieve a flag whose value is an \const{atom}. The returned value is an atom handle of type \ctype{atom_t}. 2978 \definition{\const{PL_INTEGER}} 2979Retrieve a flag whose value is an \const{integer}. The returned value is an integer of type \ctype{int64_t}. 2980 \definition{\const{PL_FLOAT}} 2981Retrieve a flag whose value is a \const{float}. The returned value is a floating point number of type \ctype{double}. 2982 \definition{\const{PL_TERM}} 2983Retrieve a flag whose value is a \const{term}. The returned value is a term handle of type \ctype{term_t}. 2984\end{description} 2985\end{description} 2986 2987\subsection{Errors and warnings} 2988\label{sec:foreign-print-warning} 2989 2990PL_warning() prints a standard Prolog warning message to the standard 2991error (\const{user_error}) stream. Please note that new code should 2992consider using PL_raise_exception() to raise a Prolog exception. See 2993also \secref{exception}. 2994 2995\begin{description} 2996\cfunction{int}{PL_warning}{format, a1, \ldots} 2997Print an error message starting with `{\tt [WARNING: }', followed 2998by the output from \arg{format}, followed by a `\chr{]}' and a newline. 2999Then start the tracer. \arg{format} and the arguments are the same as 3000for \manref{printf}{2}. Always returns \const{FALSE}. 3001\end{description} 3002 3003\subsection{Environment Control from Foreign Code} 3004\label{sec:foreign-control-prolog} 3005 3006\begin{description} 3007\cfunction{int}{PL_action}{int, ...} 3008Perform some action on the Prolog system. \arg{int} describes the 3009action. Remaining arguments depend on the requested action. The actions 3010are listed below: 3011 3012\begin{description} 3013 \termitem{PL_ACTION_TRACE}{} 3014Start Prolog tracer (trace/0). Requires no arguments. 3015 3016 \termitem{PL_ACTION_DEBUG}{} 3017Switch on Prolog debug mode (debug/0). Requires no arguments. 3018 3019 \termitem{PL_ACTION_BACKTRACE}{} 3020Print backtrace on current output stream. The argument (an \ctype{int}) 3021is the number of frames printed. 3022 3023 \termitem{PL_ACTION_HALT}{} 3024Halt Prolog execution. This action should be called rather than Unix 3025exit() to give Prolog the opportunity to clean up. This call does not 3026return. The argument (an \ctype{int}) is the exit code. See halt/1. 3027 3028 \termitem{PL_ACTION_ABORT}{} 3029Generate a Prolog abort (abort/0). This call does not return. Requires 3030no arguments. 3031 3032 \termitem{PL_ACTION_BREAK}{} 3033Create a standard Prolog break environment (break/0). Returns after the 3034user types the end-of-file character. Requires no arguments. 3035 3036 \termitem{PL_ACTION_GUIAPP}{} 3037Windows: Used to indicate to the kernel that the application is a GUI 3038application if the argument is not 0, and a console application if the 3039argument is 0. If a fatal error occurs, the system uses a windows 3040messagebox to report this on a GUI application, and otherwise simply 3041prints the error and exits. 3042 3043 \termitem{PL_ACTION_TRADITIONAL}{} 3044Same effect as using \cmdlineoption{--traditional}. Must be called 3045\emph{before} PL_initialise(). 3046 3047 \termitem{PL_ACTION_WRITE}{} 3048Write the argument, a \ctype{char *} to the current output stream. 3049 3050 \termitem{PL_ACTION_FLUSH}{} 3051Flush the current output stream. Requires no arguments. 3052 3053 \termitem{PL_ACTION_ATTACH_CONSOLE}{} 3054Attach a console to a thread if it does not have one. See 3055attach_console/0. 3056 3057 \termitem{PL_GMP_SET_ALLOC_FUNCTIONS}{} 3058Takes an integer argument. If \const{TRUE}, the GMP allocations are 3059immediately bound to the Prolog functions. If \const{FALSE}, SWI-Prolog 3060will never rebind the GMP allocation functions. See 3061mp_set_memory_functions() in the GMP documentation. The action returns 3062\const{FALSE} if there is no GMP support or GMP is already initialised. 3063\end{description} 3064 3065\cfunction{unsigned int}{PL_version}{int key} 3066Query version information. This function may be called before 3067PL_initialise(). If the key is unknown the function returns 0. 3068See \secref{abi-versions} for a more in-depth discussion on 3069binary compatibility. Defined keys are: 3070 3071 \begin{description} 3072 \termitem{PL_VERSION_SYSTEM}{} 3073SWI-Prolog version as $10,000 \times major + 100 \times minor + patch$. 3074 \termitem{PL_VERSION_FLI}{} 3075Incremented if the foreign interface defined in this chapter changes in 3076a way that breaks backward compatibility. 3077 \termitem{PL_VERSION_REC}{} 3078Incremented if the binary representation of terms as used by 3079PL_record_external() and fast_write/2 changes. 3080 \termitem{PL_VERSION_QLF}{} 3081Incremented if the QLF file format changes. 3082 \termitem{PL_VERSION_QLF_LOAD}{} 3083Represents the oldest loadable QLF file format version. 3084 \termitem{PL_VERSION_VM}{} 3085A hash that represents the VM instructions and their arguments. 3086 \termitem{PL_VERSION_BUILT_IN}{} 3087A hash that represents the names, arities and properties of all 3088built-in predicates defined in C. If this function is called 3089before PL_initialise() it returns 0. 3090 \end{description} 3091\end{description} 3092 3093\subsection{Querying Prolog} 3094\label{sec:foreign-query} 3095 3096\begin{description} 3097\cfunction{long}{PL_query}{int} 3098Obtain status information on the Prolog system. The actual argument 3099type depends on the information required. \arg{int} describes what 3100information is wanted.% 3101 \footnote{Returning pointers and integers as a long is bad 3102 style. The signature of this function should be 3103 changed.} 3104The options are given in \tabref{query}. 3105 3106 3107\begin{table} 3108\begin{quote}\begin{tabular}{|p{\tableft}|p{\linewidth-\tableft-2cm}|} 3109\hline 3110\const{PL_QUERY_ARGC} & Return an integer holding the number of 3111 arguments given to Prolog from Unix. \\ 3112\const{PL_QUERY_ARGV} & Return a \ctype{char **} holding the argument vector 3113 given to Prolog from Unix. \\ 3114\const{PL_QUERY_SYMBOLFILE} & Return a \ctype{char *} holding the current symbol 3115 file of the running process. \\ 3116\const{PL_MAX_INTEGER} & Return a long, representing the maximal integer 3117 value represented by a Prolog integer. \\ 3118\const{PL_MIN_INTEGER} & Return a long, representing the minimal integer 3119 value. \\ 3120\const{PL_QUERY_VERSION} & Return a long, representing the version as 3121 $10,000 \times M + 100 \times m + p$, where 3122 $M$ is the major, $m$ the minor version number 3123 and $p$ the patch level. For example, 3124 \exam{20717} means \exam{2.7.17}. \\ 3125\const{PL_QUERY_ENCODING} & Return the default stream encoding of 3126 Prolog (of type \ctype{IOENC}). \\ 3127\const{PL_QUERY_USER_CPU} & Get amount of user CPU time of the 3128 process in milliseconds. \\ 3129\hline 3130\end{tabular}\end{quote} 3131 3132 \caption{PL_query() options} 3133 \label{tab:query} 3134\end{table} 3135\end{description} 3136 3137 3138\subsection{Registering Foreign Predicates} 3139\label{sec:foreign-register-predicate} 3140 3141\begin{description} 3142\cfunction{int}{PL_register_foreign_in_module}{char *mod, 3143 char *name, int arity, 3144 foreign_t (*f)(), 3145 int flags, ...} 3146Register the C function \arg{f} to implement a Prolog predicate. After 3147this call returns successfully a predicate with name \arg{name} (a 3148\ctype{char *}) and arity \arg{arity} (a C \ctype{int}) is created in 3149module \arg{mod}. If \arg{mod} is \const{NULL}, the predicate is created 3150in the module of the calling context, or if no context is present in the 3151module \const{user}. 3152 3153When called in Prolog, Prolog will call \arg{function}. \arg{flags} 3154form a bitwise \emph{or}'ed list of options for the installation. These are: 3155 3156\begin{tabular}{|p{\tableft}|p{\linewidth-\tableft-2cm}|} 3157\hline 3158\const{PL_FA_META} & Provide meta-predicate info (see below) \\ 3159\const{PL_FA_TRANSPARENT} & Predicate is module transparent (deprecated) \\ 3160\const{PL_FA_NONDETERMINISTIC} & Predicate is non-deterministic. 3161 See also PL_retry(). \\ 3162\const{PL_FA_NOTRACE} & Predicate cannot be seen in the tracer \\ 3163\const{PL_FA_VARARGS} & Use alternative calling convention. \\ 3164\hline 3165\end{tabular} 3166 3167If \const{PL_FA_META} is provided, PL_register_foreign_in_module() takes 3168one extra argument. This argument is of type \ctype{const char*}. This 3169string must be exactly as long as the number of arguments of the 3170predicate and filled with characters from the set \verb$0-9:^-+?$. See 3171meta_predicate/1 for details. \const{PL_FA_TRANSPARENT} is implied if at 3172least one meta-argument is provided (\verb$0-9:^$). Note that 3173meta-arguments are \emph{not always} passed as <module>:<term>. Always 3174use PL_strip_module() to extract the module and plain term from a 3175meta-argument.\footnote{It is encouraged to pass an additional \const{NULL} 3176pointer for non-meta-predicates.} 3177 3178Predicates may be registered either before or after PL_initialise(). 3179When registered before initialisation the registration is recorded and 3180executed after installing the system predicates and before loading the 3181saved state. 3182 3183Default calling (i.e.\ without \const{PL_FA_VARARGS}) \arg{function} is 3184passed the same number of \ctype{term_t} arguments as the arity of the predicate 3185and, if the predicate is non-deterministic, an extra argument of type 3186\ctype{control_t} (see \secref{foreignnondet}). If \const{PL_FA_VARARGS} 3187is provided, \arg{function} is called with three arguments. The first 3188argument is a \ctype{term_t} handle to the first argument. Further 3189arguments can be reached by adding the offset (see also 3190PL_new_term_refs()). The second argument is the arity, which defines the 3191number of valid term references in the argument vector. The last argument 3192is used for non-deterministic calls. It is currently undocumented and should 3193be defined of type \ctype{void*}. Here is an example: 3194 3195\begin{code} 3196static foreign_t 3197atom_checksum(term_t a0, int arity, void* context) 3198{ char *s; 3199 3200 if ( PL_get_atom_chars(a0, &s) ) 3201 { int sum; 3202 3203 for(sum=0; *s; s++) 3204 sum += *s&0xff; 3205 3206 return PL_unify_integer(a0+1, sum&0xff); 3207 } 3208 3209 return FALSE; 3210} 3211 3212install_t 3213install() 3214{ PL_register_foreign("atom_checksum", 2, 3215 atom_checksum, PL_FA_VARARGS); 3216} 3217\end{code} 3218 3219\cfunction{int}{PL_register_foreign}{const char *name, int arity, 3220 foreign_t (*function)(), 3221 int flags, ...} 3222Same as PL_register_foreign_in_module(), passing \const{NULL} for the 3223\arg{module}. 3224 3225 \cfunction{void}{PL_register_extensions_in_module}{const char *module, 3226 PL_extension *e} 3227Register a series of predicates from an array of definitions of the type 3228\ctype{PL_extension} in the given \arg{module}. If \arg{module} is 3229\const{NULL}, the predicate is created in the module of the calling 3230context, or if no context is present in the module \const{user}. 3231The \ctype{PL_extension} type is defined as 3232 3233\begin{code} 3234typedef struct PL_extension 3235{ char *predicate_name; /* Name of the predicate */ 3236 short arity; /* Arity of the predicate */ 3237 pl_function_t function; /* Implementing functions */ 3238 short flags; /* Or of PL_FA_... */ 3239} PL_extension; 3240\end{code} 3241 3242For details, see PL_register_foreign_in_module(). Here is an example of 3243its usage: 3244 3245\begin{code} 3246static PL_extension predicates[] = { 3247{ "foo", 1, pl_foo, 0 }, 3248{ "bar", 2, pl_bar, PL_FA_NONDETERMINISTIC }, 3249{ NULL, 0, NULL, 0 } 3250}; 3251 3252main(int argc, char **argv) 3253{ PL_register_extensions_in_module("user", predicates); 3254 3255 if ( !PL_initialise(argc, argv) ) 3256 PL_halt(1); 3257 3258 ... 3259} 3260\end{code} 3261 3262 \cfunction{void}{PL_register_extensions}{ PL_extension *e} 3263Same as PL_register_extensions_in_module() using \const{NULL} for 3264the \arg{module} argument. 3265\end{description} 3266 3267 3268\subsection{Foreign Code Hooks} 3269\label{sec:foreign-hooks} 3270 3271For various specific applications some hooks are provided. 3272 3273\begin{description} 3274\cfunction{PL_dispatch_hook_t}{PL_dispatch_hook}{PL_dispatch_hook_t} 3275If this hook is not NULL, this function is called when reading from the 3276terminal. It is supposed to dispatch events when SWI-Prolog is connected 3277to a window environment. It can return two values: 3278\const{PL_DISPATCH_INPUT} indicates Prolog input is available on file 3279descriptor 0 or \const{PL_DISPATCH_TIMEOUT} to indicate a timeout. The old 3280hook is returned. The type \ctype{PL_dispatch_hook_t} is defined as: 3281 3282\begin{code} 3283typedef int (*PL_dispatch_hook_t)(void); 3284\end{code} 3285 \cfunction{void}{PL_abort_hook}{PL_abort_hook_t} 3286Install a hook when abort/0 is executed. SWI-Prolog abort/0 is 3287implemented using C setjmp()/longjmp() construct. The hooks are 3288executed in the reverse order of their registration after the longjmp() 3289took place and before the Prolog top level is reinvoked. The type 3290 \ctype{PL_abort_hook_t} is defined as: 3291\begin{code} 3292typedef void (*PL_abort_hook_t)(void); 3293\end{code} 3294 3295 \cfunction{int}{PL_abort_unhook}{PL_abort_hook_t} 3296Remove a hook installed with PL_abort_hook(). Returns \const{FALSE} if no 3297such hook is found, \const{TRUE} otherwise. 3298 3299 \cfunction{void}{PL_on_halt}{int (*f)(int, void *), void *closure} 3300Register the function \arg{f} to be called if SWI-Prolog is halted. The 3301function is called with two arguments: the exit code of the process (0 3302if this cannot be determined) and the \arg{closure} argument passed to 3303the PL_on_halt() call. Handlers \emph{must} return 0. Other return 3304values are reserved for future use. See also at_halt/1.\bug{Although 3305both PL_on_halt() and at_halt/1 are called in FIFO order, \emph{all} 3306at_halt/1 handlers are called before \emph{all} PL_on_halt() handlers.} 3307These handlers are called \emph{before} system cleanup and can therefore 3308access all normal Prolog resources. See also PL_exit_hook(). 3309 3310 \cfunction{void}{PL_exit_hook}{int (*f)(int, void *), void *closure} 3311Similar to PL_on_halt(), but the hooks are executed by PL_halt() instead 3312of PL_cleanup() just before calling exit(). 3313 3314 \cfunction{PL_agc_hook_t}{PL_agc_hook}{PL_agc_hook_t new} 3315Register a hook with the atom-garbage collector (see 3316garbage_collect_atoms/0) that is called on any atom that is reclaimed. 3317The old hook is returned. If no hook is currently defined, \const{NULL} 3318is returned. The argument of the called hook is the atom that is to be 3319garbage collected. The return value is an \ctype{int}. If the return 3320value is zero, the atom is {\bf not} reclaimed. 3321The hook may invoke any Prolog predicate. 3322 3323The example below defines a foreign library for printing the garbage 3324collected atoms for debugging purposes. 3325 3326\begin{code} 3327#include <SWI-Stream.h> 3328#include <SWI-Prolog.h> 3329 3330static int 3331atom_hook(atom_t a) 3332{ Sdprintf("AGC: deleting %s\n", PL_atom_chars(a)); 3333 3334 return TRUE; 3335} 3336 3337static PL_agc_hook_t old; 3338 3339install_t 3340install() 3341{ old = PL_agc_hook(atom_hook); 3342} 3343 3344install_t 3345uninstall() 3346{ PL_agc_hook(old); 3347} 3348\end{code} 3349\end{description} 3350 3351 3352\subsection{Storing foreign data} \label{sec:foreigndata} 3353 3354When combining foreign code with Prolog, it can be necessary to make 3355data represented in the foreign language available to Prolog. For 3356example, to pass it to another foreign function. At the end of this 3357section, there is a partial implementation of using foreign functions to 3358manage bit-vectors. Another example is the SGML/XML library that manages 3359a `parser' object, an object that represents the current state of the 3360parser and that can be directed to perform actions such as parsing a 3361document or make queries about the document content. 3362 3363This section provides some hints for handling foreign data in Prolog. 3364There are four options for storing such data: 3365 3366\begin{itemlist} 3367 \item[Natural Prolog data] 3368Uses the representation one would choose if no foreign interface was 3369required. For example, a bitvector representing a list of small integers can be 3370represented as a Prolog list of integers. 3371 3372 \item[Opaque packed data on the stacks] 3373It is possible to represent the raw binary representation of the foreign 3374object as a Prolog string (see \secref{strings}). Strings may be created 3375from foreign data using PL_put_string_nchars() and retrieved using 3376PL_get_string_chars(). It is good practice to wrap the string in a 3377compound term with arity 1, so Prolog can identify the type. The hook 3378portray/1 rules may be used to streamline printing such terms during 3379development. 3380 3381 \item[Opaque packed data in a blob] 3382Similar to the above solution, binary data can be stored in an atom. The 3383blob interface (\secref{blob}) provides additional facilities to assign 3384a type and hook-functions that act on creation and destruction of the 3385underlying atom. 3386 3387 \item[Natural foreign data, passed as a pointer] 3388An alternative is to pass a pointer to the foreign data. Again, the 3389pointer is often wrapped in a compound term. 3390\end{itemlist} 3391 3392\noindent 3393The choice may be guided using the following distinctions 3394 3395\begin{itemlist} 3396 \item[Is the data opaque to Prolog] 3397With `opaque' data, we refer to data handled in foreign functions, 3398passed around in Prolog, but where Prolog never examines the contents of 3399the data itself. If the data is opaque to Prolog, the selection will be 3400driven solely by simplicity of the interface and performance. 3401 3402 \item[What is the lifetime of the data] 3403With `lifetime' we refer to how it is decided that the object is (or can 3404be) destroyed. We can distinguish three cases: 3405 3406 \begin{enumerate} 3407 \item 3408The object must be destroyed on backtracking and normal Prolog garbage 3409collection (i.e., it acts as a normal Prolog term). In this case, 3410representing the object as a Prolog string (second option above) is the 3411only feasible solution. 3412 3413 \item 3414The data must survive Prolog backtracking. This leaves two options. One 3415is to represent the object using a pointer and use explicit creation and 3416destruction, making the programmer responsible. The alternative is to 3417use the blob-interface, leaving destruction to the (atom) garbage 3418collector. 3419 3420 \item 3421The data lives as during the lifetime of a foreign function that 3422implements a predicate. If the predicate is deterministic, foreign 3423automatic variables are suitable. If the predicate is non-deterministic, 3424the data may be allocated using malloc() and a pointer may be passed. 3425See \secref{foreignnondet}. 3426 \end{enumerate} 3427\end{itemlist} 3428 3429 3430\subsubsection{Examples for storing foreign data} 3431\label{sec:foreign-store-data} 3432 3433In this section, we outline some examples, covering typical cases. In 3434the first example, we will deal with extending Prolog's data 3435representation with integer sets, represented as bit-vectors. Then, we 3436discuss the outline of the DDE interface. 3437 3438\paragraph{Integer sets} with not-too-far-apart upper- and lower-bounds 3439can be represented using bit-vectors. Common set operations, such as 3440union, intersection, etc., are reduced to simple \emph{and}'ing and 3441\emph{or}'ing the bit-vectors. This can be done using Prolog's unbounded 3442integers. 3443 3444For really demanding applications, foreign representation will perform 3445better, especially time-wise. Bit-vectors are naturally expressed using 3446string objects. If the string is wrapped in \functor{bitvector}{1}, 3447the lower-bound of the vector is 0 and the upper-bound is not defined; an 3448implementation for getting and putting the sets as well as the 3449union predicate for it is below. 3450 3451\begin{code} 3452#include <SWI-Prolog.h> 3453 3454#define max(a, b) ((a) > (b) ? (a) : (b)) 3455#define min(a, b) ((a) < (b) ? (a) : (b)) 3456 3457static functor_t FUNCTOR_bitvector1; 3458 3459static int 3460get_bitvector(term_t in, int *len, unsigned char **data) 3461{ if ( PL_is_functor(in, FUNCTOR_bitvector1) ) 3462 { term_t a = PL_new_term_ref(); 3463 3464 PL_get_arg(1, in, a); 3465 return PL_get_string(a, (char **)data, len); 3466 } 3467 3468 PL_fail; 3469} 3470 3471static int 3472unify_bitvector(term_t out, int len, const unsigned char *data) 3473{ if ( PL_unify_functor(out, FUNCTOR_bitvector1) ) 3474 { term_t a = PL_new_term_ref(); 3475 3476 PL_get_arg(1, out, a); 3477 3478 return PL_unify_string_nchars(a, len, (const char *)data); 3479 } 3480 3481 PL_fail; 3482} 3483 3484static foreign_t 3485pl_bitvector_union(term_t t1, term_t t2, term_t u) 3486{ unsigned char *s1, *s2; 3487 int l1, l2; 3488 3489 if ( get_bitvector(t1, &l1, &s1) && 3490 get_bitvector(t2, &l2, &s2) ) 3491 { int l = max(l1, l2); 3492 unsigned char *s3 = alloca(l); 3493 3494 if ( s3 ) 3495 { int n; 3496 int ml = min(l1, l2); 3497 3498 for(n=0; n<ml; n++) 3499 s3[n] = s1[n] | s2[n]; 3500 for( ; n < l1; n++) 3501 s3[n] = s1[n]; 3502 for( ; n < l2; n++) 3503 s3[n] = s2[n]; 3504 3505 return unify_bitvector(u, l, s3); 3506 } 3507 3508 return PL_warning("Not enough memory"); 3509 } 3510 3511 PL_fail; 3512} 3513 3514 3515install_t 3516install() 3517{ PL_register_foreign("bitvector_union", 3, pl_bitvector_union, 0); 3518 3519 FUNCTOR_bitvector1 = PL_new_functor(PL_new_atom("bitvector"), 1); 3520} 3521\end{code} 3522 3523\paragraph{The DDE interface} (see \secref{DDE}) represents another 3524common usage of the foreign interface: providing communication to new 3525operating system features. The DDE interface requires knowledge about 3526active DDE server and client channels. These channels contains various 3527foreign data types. Such an interface is normally achieved using an 3528open/close protocol that creates and destroys a \jargon{handle}. The 3529handle is a reference to a foreign data structure containing the 3530relevant information. 3531 3532There are a couple of possibilities for representing the handle. The 3533choice depends on responsibilities and debugging facilities. The 3534simplest approach is to use PL_unify_pointer() and PL_get_pointer(). 3535This approach is fast and easy, but has the drawbacks of (untyped) 3536pointers: there is no reliable way to detect the validity of the 3537pointer, nor to verify that it is pointing to a structure of the desired 3538type. The pointer may be wrapped into a compound term with arity 35391 (i.e., \exam{dde_channel(<Pointer>)}), making the type-problem 3540less serious. 3541 3542Alternatively (used in the DDE interface), the interface code can 3543maintain a (preferably variable length) array of pointers and return the 3544index in this array. This provides better protection. Especially for 3545debugging purposes, wrapping the handle in a compound is a good 3546suggestion. 3547 3548 3549\subsection{Embedding SWI-Prolog in other applications} \label{sec:embedded} 3550 3551With embedded Prolog we refer to the situation where the `main' program 3552is not the Prolog application. Prolog is sometimes embedded in C, C++, 3553Java or other languages to provide logic based services in a larger 3554application. Embedding loads the Prolog engine as a library to the 3555external language. Prolog itself only provides for embedding in the 3556C language (compatible with C++). Embedding in Java is achieved using 3557JPL using a C-glue between the Java and Prolog C interfaces. 3558 3559The most simple embedded program is below. The interface function 3560PL_initialise() {\bf must} be called before any of the other SWI-Prolog 3561foreign language functions described in this chapter, except for 3562PL_initialise_hook(), PL_new_atom(), PL_new_functor() and 3563PL_register_foreign(). PL_initialise() interprets all the command line 3564arguments, except for the \argoption{-t}{toplevel} flag that is 3565interpreted by PL_toplevel(). 3566 3567\begin{code} 3568int 3569main(int argc, char **argv) 3570{ if ( !PL_initialise(argc, argv) ) 3571 PL_halt(1); 3572 3573 PL_halt(PL_toplevel() ? 0 : 1); 3574} 3575\end{code} 3576 3577\begin{description} 3578\cfunction{int}{PL_initialise}{int argc, char **argv} 3579Initialises the SWI-Prolog heap and stacks, restores the Prolog 3580state, loads the system and personal initialisation files, 3581runs the initialization/1 hooks and finally runs the initialization 3582goals registered using \argoption{-g}{goal}. 3583 3584Special consideration is required for \verb$argv[0]$. On {\bf Unix}, 3585this argument passes the part of the command line that is used 3586to locate the executable. Prolog uses this to find the file holding 3587the running executable. The {\bf Windows} version uses this to find 3588a \jargon{module} of the running executable. If the specified module 3589cannot be found, it tries the module \const{libswipl.dll}, containing 3590the Prolog runtime kernel. In all these cases, the resulting file is 3591used for two purposes: 3592 3593\begin{itemize} 3594 \item See whether a Prolog saved state is appended to the file. 3595 If this is the case, this state will be loaded instead of 3596 the default \file{boot.prc} file from the SWI-Prolog home 3597 directory. See also qsave_program/[1,2] and \secref{plld}. 3598 \item Find the Prolog home directory. This process is described 3599 in detail in \secref{findhome}. 3600\end{itemize} 3601 3602PL_initialise() returns 1 if all initialisation succeeded and 0 3603otherwise.% 3604 \bug{Various fatal errors may cause PL_initialise() to call 3605 PL_halt(1), preventing it from returning at all.} 3606 3607In most cases, \arg{argc} and \arg{argv} will be passed from the main 3608program. It is allowed to create your own argument vector, provided 3609\verb$argv[0]$ is constructed according to the rules above. For 3610example: 3611 3612\begin{code} 3613int 3614main(int argc, char **argv) 3615{ char *av[10]; 3616 int ac = 0; 3617 3618 av[ac++] = argv[0]; 3619 av[ac++] = "-x"; 3620 av[ac++] = "mystate"; 3621 av[ac] = NULL; 3622 3623 if ( !PL_initialise(ac, av) ) 3624 PL_halt(1); 3625 ... 3626} 3627\end{code} 3628 3629Please note that the passed argument vector may be referred from Prolog 3630at any time and should therefore be valid as long as the Prolog engine 3631is used. 3632 3633A good setup in Windows is to add SWI-Prolog's \file{bin} directory 3634to your \env{PATH} and either pass a module holding a saved state, or 3635\verb$"libswipl.dll"$ as \verb$argv[0]$. If the Prolog state is attached 3636to a DLL (see the \cmdlineoption{-dll} option of \program{swipl-ld}), 3637pass the name of this DLL. 3638 3639 \cfunction{int}{PL_winitialise}{int argc, wchar_t **argv} 3640Wide character version of PL_initialise(). Can be used in Windows 3641combined with the wmain() entry point. 3642 3643 \cfunction{int}{PL_is_initialised}{int *argc, char ***argv} 3644Test whether the Prolog engine is already initialised. Returns 3645\const{FALSE} if Prolog is not initialised and \const{TRUE} otherwise. 3646If the engine is initialised and \arg{argc} is not \const{NULL}, the 3647argument count used with PL_initialise() is stored in \arg{argc}. Same 3648for the argument vector \arg{argv}. 3649 3650 \cfunction{int}{PL_set_resource_db_mem}{const unsigned char *data, 3651 size_t size} 3652This function must be called at most once and \emph{before} calling 3653PL_initialise(). The memory area designated by \arg{data} and \arg{size} 3654must contain the resource data and be in the format as produced by 3655qsave_program/2. The memory area is accessed by PL_initialise() as well 3656as calls to open_resource/3.\footnote{This implies that the data must 3657remain accessible during the lifetime of the process if open_resource/3 3658is used. Future versions may provide a function to detach the resource 3659database and cause open_resource/3 to raise an exception.} 3660 3661For example, we can include the bootstrap data into an embedded 3662executable using the steps below. The advantage of this approach is that 3663it is fully supported by any OS and you obtain a single file executable. 3664 3665\begin{enumerate} 3666 \item Create a saved state using qsave_program/2 or 3667\begin{code} 3668% swipl -o state -c file.pl ... 3669\end{code} 3670 \item Create a C source file from the state using e.g., the 3671 Unix utility \program{xxd}(1): 3672\begin{code} 3673% xxd -i state > state.h 3674\end{code} 3675 \item Embed Prolog as in the example below. Instead of calling 3676 the toplevel you probably want to call your application code. 3677\begin{code} 3678#include <SWI-Prolog.h> 3679#include "state.h" 3680 3681int 3682main(int argc, char **argv) 3683{ if ( !PL_set_resource_db_mem(state, state_len) || 3684 !PL_initialise(argc, argv) ) 3685 PL_halt(1); 3686 3687 return PL_toplevel(); 3688} 3689\end{code} 3690\end{enumerate} 3691 3692Alternative to \program{xxd}, it is possible to use inline assembler, 3693e.g. the \program{gcc} \const{incbin} instruction. Code for 3694\program{gcc} was provided by Roberto Bagnara on the SWI-Prolog 3695mailinglist. Given the state in a file \file{state}, create the 3696following assembler program: 3697 3698\begin{code} 3699 .globl _state 3700 .globl _state_end 3701_state: 3702 .incbin "state" 3703_state_end: 3704\end{code} 3705 3706Now include this as follows: 3707 3708\begin{code} 3709#include <SWI-Prolog.h> 3710 3711#if __linux 3712#define STATE _state 3713#define STATE_END _state_end 3714#else 3715#define STATE state 3716#define STATE_END state_end 3717#endif 3718 3719extern unsigned char STATE[]; 3720extern unsigned char STATE_END[]; 3721 3722int 3723main(int argc, char **argv) 3724{ if ( !PL_set_resource_db_mem(STATE, STATE_END - STATE) || 3725 !PL_initialise(argc, argv) ) 3726 PL_halt(1); 3727 return PL_toplevel(); 3728} 3729\end{code} 3730 3731As Jose Morales pointed at 3732\url{https://github.com/graphitemaster/incbin}, which contains a 3733portability layer on top of the above idea. 3734 3735 \cfunction{int}{PL_toplevel}{} 3736Runs the goal of the \argoption{-t}{toplevel} switch (default prolog/0) and 3737returns 1 if successful, 0 otherwise. 3738 3739 \cfunction{int}{PL_cleanup}{int status} 3740This function performs the reverse of PL_initialise(). It runs the 3741PL_on_halt() and at_halt/1 handlers, closes all streams (except for the 3742`standard I/O' streams which are flushed only), deallocates all memory 3743if \arg{status} equals `0' and restores all signal handlers. The 3744\arg{status} argument is passed to the various termination hooks and 3745indicates the \jargon{exit-status}. 3746 3747The function returns \const{TRUE} if successful and \const{FALSE} 3748otherwise. Currently, \const{FALSE} is returned when an attempt is made 3749to call PL_cleanup() recursively or if one of the exit handlers 3750cancels the termination using cancel_halt/1. Exit handlers may only 3751cancel termination if \arg{status} is 0. 3752 3753In theory, this function allows deleting and restarting the Prolog 3754system in the same process. In practice, SWI-Prolog's cleanup process is 3755far from complete, and trying to revive the system using PL_initialise() 3756will leak memory in the best case. It can also crash the application. 3757 3758In this state, there is little practical use for this function. If you 3759want to use Prolog temporarily, consider running it in a separate process. 3760If you want to be able to reset Prolog, your options are (again) a 3761separate process, modules or threads. 3762 3763 \cfunction{void}{PL_cleanup_fork}{} 3764Stop intervaltimer that may be running on behalf of profile/1. The call 3765is intended to be used in combination with fork(): 3766 3767\begin{code} 3768 if ( (pid=fork()) == 0 ) 3769 { PL_cleanup_fork(); 3770 <some exec variation> 3771 } 3772\end{code} 3773 3774The call behaves the same on Windows, though there is probably no 3775meaningful application. 3776 3777 \cfunction{int}{PL_halt}{int status} 3778Clean up the Prolog environment using PL_cleanup() and if successful 3779call exit() with the status argument. Returns \const{FALSE} if exit 3780was cancelled by PL_cleanup(). 3781\end{description} 3782 3783 3784\subsubsection{Threading, Signals and embedded Prolog} 3785\label{sec:sigembedded} 3786 3787This section applies to Unix-based environments that have signals or 3788multithreading. The Windows version is compiled for multithreading, 3789and Windows lacks proper signals. 3790 3791We can distinguish two classes of embedded executables. There are small 3792C/C++ programs that act as an interfacing layer around Prolog. Most of 3793these programs can be replaced using the normal Prolog executable 3794extended with a dynamically loaded foreign extension and in most cases 3795this is the preferred route. In other cases, Prolog is embedded in a 3796complex application that---like Prolog---wants to control the process 3797environment. A good example is Java. Embedding Prolog is generally the 3798only way to get these environments together in one process image. Java 3799VMs, however, are by nature multithreaded and appear to do 3800signal handling (software interrupts). 3801 3802On Unix systems, SWI-Prolog installs handlers for the following 3803signals: 3804 3805\begin{description} 3806 \item[SIGUSR2] has an empty signal handler. This signal is 3807 sent to a thread after sending a thread-signal (see 3808 thread_signal/2). It causes blocking system calls to return 3809 with \const{EINTR}, which gives them the opportunity to react 3810 to thread-signals. 3811 3812 In some cases the embedded system and SWI-Prolog may both use 3813 \const{SIGUSR2} without conflict. If the embedded system redefines 3814 \const{SIGUSR2} with a handler that runs quickly and no harm is 3815 done in the embedded system due to spurious wakeup when initiated 3816 from Prolog, there is no problem. If SWI-Prolog is initialised 3817 \emph{after} the embedded system it will call the handler set by 3818 the embedded system and the same conditions as above apply. 3819 SWI-Prolog's handler is a simple function only chaining a possibly 3820 previously registered handler. SWI-Prolog can handle spurious 3821 \const{SIGUSR2} signals. 3822 3823 \item[SIGINT] is used by the top level to activate the tracer 3824 (typically bound to control-C). The first control-C posts a 3825 request for starting the tracer in a safe, synchronous fashion. 3826 If control-C is hit again before the safe route is executed, 3827 it prompts the user whether or not a forced interrupt is 3828 desired. 3829 3830 \item[SIGTERM, SIGABRT and SIGQUIT] are caught to cleanup before 3831 killing the process again using the same signal. 3832 3833 \item[SIGSEGV, SIGILL, SIGBUS, SIGFPE and SIGSYS] are caught by 3834 to print a backtrace before killing the process again using 3835 the same signal. 3836 3837 \item[SIGHUP] is caught and causes the process to exit with status 2 3838 after cleanup. 3839\end{description} 3840 3841The \cmdlineoption{--no-signals} option can be used to inhibit all signal 3842processing except for \const{SIGUSR2}. The handling of \const{SIGUSR2} 3843is vital for dealing with blocking system call in threads. The used 3844signal may be changed using the \cmdlineoption{--sigalert=NUM} option or 3845disabled using \verb$--sigalert=0$. 3846 3847 3848 3849\section{Linking embedded applications using swipl-ld} \label{sec:plld} 3850 3851The utility program \program{swipl-ld} (Win32: swipl-ld.exe) may be used to link 3852a combination of C files and Prolog files into a stand-alone executable. 3853\program{swipl-ld} automates most of what is described in the previous 3854sections. 3855 3856In normal usage, a copy is made of the default embedding template 3857\file{.../swipl/include/stub.c}. The main() routine is modified to suit 3858your application. PL_initialise() \strong{must} be passed the 3859program name (\arg{argv[0]}) (Win32: the executing program can be 3860obtained using GetModuleFileName()). The other elements of the 3861command line may be modified. Next, \program{swipl-ld} is typically invoked 3862as: 3863 3864\begin{code} 3865swipl-ld -o output stubfile.c [other-c-or-o-files] [plfiles] 3866\end{code} 3867 3868\program{swipl-ld} will first split the options into various groups for both 3869the C compiler and the Prolog compiler. Next, it will add various 3870default options to the C compiler and call it to create an executable 3871holding the user's C code and the Prolog kernel. Then, it will call the 3872SWI-Prolog compiler to create a saved state from the provided Prolog 3873files and finally, it will attach this saved state to the created 3874emulator to create the requested executable. 3875 3876Below, it is described how the options are split and which additional 3877options are passed. 3878 3879\begin{description} 3880 \cmdlineoptionitem{-help}{} 3881Print brief synopsis. 3882 \cmdlineoptionitem{-pl}{prolog} 3883Select the Prolog to use. This Prolog is used for two purposes: get the 3884home directory as well as the compiler/linker options and create a saved 3885state of the Prolog code. 3886 \cmdlineoptionitem{-ld}{linker} 3887Linker used to link the raw executable. Default is to use the C compiler 3888(Win32: \program{link.exe}). 3889 \cmdlineoptionitem{-cc}{C compiler} 3890Compiler for \fileext{c} files found on the command line. Default is the 3891compiler used to build SWI-Prolog accessible through the Prolog flag 3892\prologflag{c_cc} (Win32: \program{cl.exe}). 3893 \cmdlineoptionitem{-c++}{C++-compiler} 3894Compiler for C++ source file (extensions \fileext{cpp}, \fileext{cxx}, 3895\fileext{cc} or \fileext{C}) found on the command line. Default is 3896\program{c++} or \program{g++} if the C compiler is \program{gcc} 3897(Win32: cl.exe). 3898 3899 \cmdlineoptionitem{-nostate}{} 3900Just relink the kernel, do not add any Prolog code to the new kernel. 3901This is used to create a new kernel holding additional foreign predicates 3902on machines that do not support the shared-library (DLL) interface, or if 3903building the state cannot be handled by the default procedure used by 3904\program{swipl-ld}. In the latter case the state is created separately and 3905appended to the kernel using \exam{cat <kernel> <state> > <out>} 3906(Win32: \exam{copy /b <kernel>+<state> <out>}). 3907 3908 \cmdlineoptionitem{-shared}{} 3909Link C, C++ or object files into a shared object (DLL) that can be 3910loaded by the load_foreign_library/1 predicate. If used with 3911\cmdlineoption{-c}{} it sets the proper options to compile a C or C++ 3912file ready for linking into a shared object. 3913 3914 \cmdlineoptionitem{-dll}{} 3915\emph{Windows only}. Embed SWI-Prolog into a DLL rather than an 3916executable. 3917 3918 \cmdlineoptionitem{-c}{} 3919Compile C or C++ source files into object files. This turns 3920\program{swipl-ld} into a replacement for the C or C++ compiler, where proper 3921options such as the location of the include directory are passed 3922automatically to the compiler. 3923 3924 \cmdlineoptionitem{-E}{} 3925Invoke the C preprocessor. Used to make \program{swipl-ld} a replacement for 3926the C or C++ compiler. 3927 3928 \cmdlineoptionitem{-pl-options}{,\ldots} 3929Additional options passed to Prolog when creating the saved state. The 3930first character immediately following \const{pl-options} is used as 3931separator and translated to spaces when the argument is built. 3932Example: \exam{-pl-options,-F,xpce} passes \exam{-F xpce} as additional 3933flags to Prolog. 3934 \cmdlineoptionitem{-ld-options}{,\ldots} 3935Passes options to the linker, similar to \cmdlineoption{-pl-options}. 3936 \cmdlineoptionitem{-cc-options}{,\ldots} 3937Passes options to the C/C++ compiler, similar to \cmdlineoption{-pl-options}. 3938 \cmdlineoptionitem{-v}{} 3939Select verbose operation, showing the various programs and their options. 3940 \cmdlineoptionitem{-o}{outfile} 3941Reserved to specify the final output file. 3942 \cmdlineoptionitem*{-l}{library} 3943Specifies a library for the C compiler. By default, \clib{-lswipl} 3944(Win32: libpl.lib) and the libraries needed by the Prolog kernel are given. 3945 \cmdlineoptionitem*{-L}{library-directory} 3946Specifies a library directory for the C compiler. By default the 3947directory containing the Prolog C library for the current architecture 3948is passed. 3949 \cmdlineoptionitem{\cmdlineoption{-g} \bnfor{} 3950 \cmdlineoption{-I\arg{include-directory}} \bnfor{} 3951 \cmdlineoption{-D\arg{definition}}}{} 3952These options are passed to the C compiler. By default, the include 3953directory containing \file{SWI-Prolog.h} is passed. \program{swipl-ld} adds 3954two additional \argoption*{-D}{def} flags: 3955\begin{description} 3956\cmdlineoptionitem*{-D}{\const{__SWI_PROLOG__}} 3957Indicates the code is to be connected to SWI-Prolog. 3958\cmdlineoptionitem*{-D}{\const{__SWI_EMBEDDED__}} 3959Indicates the creation of an embedded program. 3960\end{description} 3961 \cmdlineoptionitem{}{*.o \bnfor{} 3962 *.c \bnfor{} 3963 *.C \bnfor{} 3964 *.cxx \bnfor{} 3965 *.cpp} 3966Passed as input files to the C compiler. 3967 \cmdlineoptionitem{}{*.pl \bnfor *.qlf} 3968Passed as input files to the Prolog compiler to create the saved state. 3969 \cmdlineoptionitem{}{*} 3970All other options. These are passed as linker options to the 3971C compiler. 3972\end{description} 3973 3974 3975\subsection{A simple example} 3976\label{sec:foreign-example} 3977 3978The following is a very simple example going through all the steps 3979outlined above. It provides an arithmetic expression evaluator. We will 3980call the application \program{calc} and define it in the files \file{calc.c} 3981and \file{calc.pl}. The Prolog file is simple: 3982 3983\begin{code} 3984calc(Atom) :- 3985 term_to_atom(Expr, Atom), 3986 A is Expr, 3987 write(A), 3988 nl. 3989\end{code} 3990 3991The C part of the application parses the command line options, 3992initialises the Prolog engine, locates the \verb$calc/1$ predicate and calls 3993it. The coder is in \figref{calc}. 3994 3995\begin{figure} 3996\begin{code} 3997#include <stdio.h> 3998#include <string.h> 3999#include <SWI-Prolog.h> 4000 4001#define MAXLINE 1024 4002 4003int 4004main(int argc, char **argv) 4005{ char expression[MAXLINE]; 4006 char *e = expression; 4007 char *program = argv[0]; 4008 char *plav[2]; 4009 int n; 4010 4011 /* combine all the arguments in a single string */ 4012 4013 for(n=1; n<argc; n++) 4014 { if ( n != 1 ) 4015 *e++ = ' '; 4016 strcpy(e, argv[n]); 4017 e += strlen(e); 4018 } 4019 4020 /* make the argument vector for Prolog */ 4021 4022 plav[0] = program; 4023 plav[1] = NULL; 4024 4025 /* initialise Prolog */ 4026 4027 if ( !PL_initialise(1, plav) ) 4028 PL_halt(1); 4029 4030 /* Lookup calc/1 and make the arguments and call */ 4031 4032 { predicate_t pred = PL_predicate("calc", 1, "user"); 4033 term_t h0 = PL_new_term_refs(1); 4034 int rval; 4035 4036 PL_put_atom_chars(h0, expression); 4037 rval = PL_call_predicate(NULL, PL_Q_NORMAL, pred, h0); 4038 4039 PL_halt(rval ? 0 : 1); 4040 } 4041 4042 return 0; 4043} 4044\end{code} 4045\caption{C source for the calc application} 4046\label{fig:calc} 4047\end{figure} 4048 4049\noindent 4050The application is now created using the command line below. The option 4051\exam{-goal true} sets the Prolog initialization goal to suppress the 4052banner. Note that the \exam{-o calc} does not specify an extension. If 4053the platform uses a file extension for executables, \program{swipl-ld} 4054will add this (e.g., \fileext{exe} on Windows). 4055 4056\begin{code} 4057% swipl-ld -goal true -o calc calc.c calc.pl 4058\end{code} 4059 4060\noindent 4061The created program \program{calc} is a native executable with the 4062Prolog code attached to it. Note that the program typically depends 4063on the shared object \file{libswipl} and, depending on the platform 4064and configuration, on several external shared objects. 4065 4066\begin{code} 4067% ./calc pi/2 40681.5708 4069\end{code} 4070 4071 4072\section{The Prolog `home' directory} \label{sec:findhome} 4073 4074Executables embedding SWI-Prolog should be able to find the `home' 4075directory of the development environment unless a self-contained 4076saved state has been added to the executable (see qsave_program/[1,2] 4077and \secref{plld}). 4078 4079If Prolog starts up, it will try to locate the development environment. 4080To do so, it will try the following steps until one succeeds: 4081 4082\begin{enumerate} 4083 \item If the \cmdlineoption{--home=DIR} is provided, use this. 4084 \item If the environment variable \env{SWI_HOME_DIR} is defined and 4085 points to an existing directory, use this. 4086 \item If the environment variable \env{SWIPL} is defined and 4087 points to an existing directory, use this. 4088 \item Locate the primary executable or (Windows only) a component 4089 (\jargon{module}) thereof and check whether the parent 4090 directory of the directory holding this file contains the 4091 file \file{swipl}. If so, this file contains the (relative) 4092 path to the home directory. If this directory exists, use 4093 this. This is the normal mechanism used by the binary 4094 distribution. 4095 \item If the precompiled path exists, use it. This is only useful 4096 for a source installation. 4097\end{enumerate} 4098 4099If all fails and there is no state attached to the executable or 4100provided Windows module (see PL_initialise()), SWI-Prolog gives up. If 4101a state is attached, the current working directory is used. 4102 4103The file_search_path/2 alias \const{swi} is set to point to the home 4104directory located. 4105 4106 4107\section{Example of Using the Foreign Interface} \label{sec:foreignxmp} 4108 4109Below is an example showing all stages of the declaration of a foreign 4110predicate that transforms atoms possibly holding uppercase letters into 4111an atom only holding lowercase letters. \Figref{lowercase-c} shows the 4112C source file, \figref{load-foreign} illustrates compiling and loading 4113of foreign code. 4114 4115\begin{figure}[htb] 4116 4117\begin{code} 4118/* Include file depends on local installation */ 4119#include <SWI-Prolog.h> 4120#include <stdlib.h> 4121#include <string.h> 4122#include <ctype.h> 4123 4124foreign_t 4125pl_lowercase(term_t u, term_t l) 4126{ char *copy; 4127 char *s, *q; 4128 int rval; 4129 4130 if ( !PL_get_atom_chars(u, &s) ) 4131 return PL_warning("lowercase/2: instantiation fault"); 4132 copy = malloc(strlen(s)+1); 4133 4134 for( q=copy; *s; q++, s++) 4135 *q = (isupper(*s) ? tolower(*s) : *s); 4136 *q = '\0'; 4137 4138 rval = PL_unify_atom_chars(l, copy); 4139 free(copy); 4140 4141 return rval; 4142} 4143 4144install_t 4145install() 4146{ PL_register_foreign("lowercase", 2, pl_lowercase, 0); 4147} 4148\end{code} 4149 4150 \caption{Lowercase source file} 4151 \label{fig:lowercase-c} 4152\end{figure} 4153 4154 4155\begin{figure}[htb] 4156\begin{code} 4157% gcc -I/usr/local/lib/swipl-\plversion/include -fpic -c lowercase.c 4158% gcc -shared -o lowercase.so lowercase.o 4159% swipl 4160Welcome to SWI-Prolog (...) 4161... 4162 41631 ?- load_foreign_library(lowercase). 4164true. 4165 41662 ?- lowercase('Hello World!', L). 4167L = 'hello world!'. 4168\end{code} 4169 \caption{Compiling the C source and loading the object file} 4170 \label{fig:load-foreign} 4171\end{figure} 4172\clearpage 4173 4174 4175\section{Notes on Using Foreign Code} \label{sec:foreignnotes} 4176 4177\subsection{Foreign debugging functions} 4178\label{sec:foreign-debug} 4179 4180The functions in this section are primarily intended for debugging 4181foreign extensions or embedded Prolog. Violating the constraints of the 4182foreign interface often leads to crashes in a subsequent garbage 4183collection. If this happens, the system needs to be compiled for 4184debugging using \exam{cmake -DCMAKE_BUILD_TYPE=Debug}, after which all 4185functions and predicates listed below are available to use from the 4186debugger (e.g. \program{gdb}) or can be placed at critical location in 4187your code or the system code. 4188 4189\begin{description} 4190 \cfunction{void}{PL_backtrace}{int depth, int flags} 4191Dump a Prolog backtrace to the \const{user_error} stream. \arg{Depth} is 4192the number of frames to dump. \arg{Flags} is a bitwise or of the 4193following constants: 4194 4195 \begin{description} 4196 \termitem{PL_BT_SAFE}{} 4197 (0x1) Do not try to print \jargon{goals}. Instead, just print the 4198 predicate name and arity. This reduces the likelihood to crash if 4199 PL_backtrace() is called in a damaged environment. 4200 \termitem{PL_BT_USER}{} 4201 (0x2) Only show `user' frames. Default is to also show frames 4202 of hidden built-in predicates. 4203 \end{description} 4204 4205 \cfunction{char *}{PL_backtrace_string}{int depth, int flags} 4206As PL_backtrace(), but returns the stack as a string. The string uses 4207UTF-8 encoding. The returned string must be freed using PL_free(). This 4208function is was added to get stack traces from running servers where I/O 4209is redirected or discarded. For example, using \program{gdb}, a stack 4210trace is printed in the gdb console regardless of Prolog I/O redirection 4211using the following command: 4212 4213\begin{code} 4214(gdb) printf "%s", PL_backtrace_string(25,0) 4215\end{code} 4216 4217The source distribution provides the script \file{scripts/swipl-bt} that 4218exploits \program{gdb} and PL_backtrace_string() to print stack traces 4219in various formats for a SWI-Prolog process, given its process id. 4220 4221 \cfunction{int}{PL_check_data}{term_t data} 4222 Check the consistency of the term \arg{data}. Returns \const{TRUE} 4223 this is actually implemented in the current version and 4224 \const{FALSE} otherwise. The actual implementation only exists 4225 if the system is compiled with the cflag \const{-DO_DEBUG} or 4226 \const{-DO_MAINTENANCE}. This is \emph{not} the default. 4227 4228 \cfunction{int}{PL_check_stacks}{} 4229 Check the consistency of the runtime stacks of the calling thread. 4230 Returns \const{TRUE} this is actually implemented in the current 4231 version and \const{FALSE} otherwise. The actual implementation only exists 4232 if the system is compiled with the cflag \const{-DO_DEBUG} or 4233 \const{-DO_MAINTENANCE}. This is \emph{not} the default. 4234\end{description} 4235 4236The Prolog kernel sources use the macro \cfuncref{DEBUG}{Topic, Code}. 4237These macros are disabled in the production version and must be enabled 4238by recompiling the system as described above. Specific topics can be 4239enabled and disabled using the predicates prolog_debug/1 and 4240prolog_nodebug/1. In addition, they can be activated from the 4241commandline using commandline option \const{-d topics}, where 4242\arg{topics} is a comma-separated list of debug topics to enable. For 4243example, the code below adds many consistency checks and prints messages 4244if the Prolog signal handler dispatches signals. 4245 4246\begin{code} 4247$ swipl -d chk_secure,msg_signal 4248\end{code} 4249 4250\begin{description} 4251 \predicate{prolog_debug}{1}{+Topic} 4252 \nodescription 4253 \predicate{prolog_nodebug}{1}{+Topic} 4254Enable/disable a debug topic. \arg{Topic} is an atom that identifies 4255the desired topic. The available topics are defined in 4256\file{src/pl-debug.h}. Please search the sources to find out what 4257is actually printed and when. We highlight one topic here: 4258 4259 \begin{description} 4260 \termitem{chk_secure} 4261 Add many expensive consistency checks to the system. This should 4262 typically be used when the system crashes, notably in the garbage 4263 collector. Garbage collection crashes are in most cases caused 4264 by invalid data on the Prolog stacks. This debug topic may help 4265 locating how the invalid data was created. 4266 \end{description} 4267 4268These predicates require the system to be compiled for debugging using 4269\exam{cmake -DCMAKE_BUILD_TYPE=Debug}. 4270 4271 \cfunction{int}{PL_prolog_debug}{const char *topic} 4272 \nodescription 4273 \cfunction{int}{PL_prolog_nodebug}{const char *topic} 4274(De)activate debug topics. The \arg{topics} argument is a 4275comma-separated string of topics to enable or disable. Matching is 4276case-insensitive. See also prolog_debug/1 and prolog_nodebug/1. 4277 4278These functions require the system to be compiled for debugging using 4279\exam{cmake -DCMAKE_BUILD_TYPE=Debug}. 4280 4281\end{description} 4282 4283 4284\subsection{Memory Allocation} 4285\label{sec:foreign-malloc} 4286 4287SWI-Prolog's heap memory allocation is based on the \manref{malloc}{3} 4288library routines. SWI-Prolog provides the functions below as a wrapper 4289around malloc(). Allocation errors in these functions trap SWI-Prolog's 4290fatal-error handler, in which case PL_malloc() or PL_realloc() do not 4291return. 4292 4293Portable applications must use PL_free() to release strings returned 4294by PL_get_chars() using the \const{BUF_MALLOC} argument. Portable 4295applications may use both PL_malloc() and friends or malloc() and 4296friends but should not mix these two sets of functions on the same 4297memory. 4298 4299\begin{description} 4300 \cfunction{void *}{PL_malloc}{size_t bytes} 4301Allocate \arg{bytes} of memory. On failure SWI-Prolog's fatal-error 4302handler is called and PL_malloc() does not return. Memory allocated 4303using these functions must use PL_realloc() and PL_free() rather than 4304realloc() and free(). 4305 4306 \cfunction{void *}{PL_realloc}{void *mem, size_t size} 4307Change the size of the allocated chunk, possibly moving it. The 4308\arg{mem} argument must be obtained from a previous PL_malloc() or 4309PL_realloc() call. 4310 4311 \cfunction{void}{PL_free}{void *mem} 4312Release memory. The \arg{mem} argument must be obtained from a 4313previous PL_malloc() or PL_realloc() call. 4314\end{description} 4315 4316\subsection{Compatibility between Prolog versions} 4317\label{sec:foreign-compat} 4318 4319Great care is taken to ensure binary compatibility of foreign extensions 4320between different Prolog versions. Only the much less frequently used stream 4321interface has been responsible for binary incompatibilities. 4322 4323\index{PLVERSION}% 4324Source code that relies on new features of the foreign interface can 4325use the macro \const{PLVERSION} to find the version of 4326\file{SWI-Prolog.h} and PL_query() using the option 4327\const{PL_QUERY_VERSION} to find the version of the attached Prolog 4328system. Both follow the same numbering schema explained with PL_query(). 4329 4330 4331\subsection{Foreign hash tables} 4332\label{sec:foreign-hash-tables} 4333 4334As of SWI-Prolog 8.3.2 the foreign API provides access to the internal 4335thread-safe and lock-free hash tables that associate pointers or objects 4336that fit in a pointer such as atoms (\ctype{atom_t}). An argument 4337against providing these functions is that they have little to do with 4338Prolog. The argument is favor is that it is hard to implement efficient 4339lock-free tables without low-level access to the underlying Prolog 4340threads and exporting this interface has a low cost. 4341 4342The functions below \textbf{can only be called if the calling thread is 4343associated with a Prolog thread}. Failure to do so causes the call to be 4344ignored or return the failure code where applicable. 4345 4346\begin{description} 4347 \cfunction{hash_table_t}{PL_new_hash_table}{int size, 4348 void (*free_symbol)(void *n, void *v)} 4349Create a new table for \arg{size} key-value pairs. The table is resized 4350when needed. If you know the table will hold 10,000 key-value pairs, 4351providing a suitable initial size avoids resizing. The \arg{free_symbol} 4352function is called whethever a key-value pair is removed from the table. 4353This can be \const{NULL}. 4354 4355 \cfunction{int}{PL_free_hash_table}{hash_table_t table} 4356Destroy the hash table. First calls PL_clear_hash_table(). 4357 4358 \cfunction{void*}{PL_lookup_hash_table}{hash_table_t table, void *key} 4359Return the value matching \arg{key} or \const{NULL} if \arg{key} does not 4360appear in the table. 4361 4362 \cfunction{void*}{PL_add_hash_table}{hash_table_t table, 4363 void *key, void *value, int flags} 4364Add \arg{key}-\arg{value} to the table. The behaviour if \arg{key} is 4365already in the table depends on \arg{flags}. If \const{0}, this function 4366returns the existing value without updating the table. If 4367\const{PL_HT_UPDATE} the old \arg{value} is \emph{replaced} and the 4368function returns the old value. If \const{PL_HT_NEW}, a message and 4369backtrace are printed and the function returns \arg{NULL} if \arg{key} 4370is already in the table. 4371 4372 \cfunction{void*}{PL_del_hash_table}{hash_table_t table, void *key} 4373Delete \arg{key} from the table, returning the old associated value or 4374\const{NULL} 4375 4376 \cfunction{int}{PL_clear_hash_table}{hash_table_t table} 4377Delete all key-value pairs from the table. Call \arg{free_symbol} for 4378each deleted pair. 4379 4380 \cfunction{hash_table_enum_t}{PL_new_hash_table_enum}{hash_table_t table} 4381Return a table \jargon{enumerator} (cursor) that can be used to 4382enumerate all key-value pairs using PL_advance_hash_table_enum(). The 4383enumerator must be discarded using PL_free_hash_table_enum(). It is safe 4384for another thread to add symbols while the table is being enumerated, 4385but undefined whether or not these new symbols are visible. If another 4386thread deletes a key that is not yet enumerated it will not be 4387enumerated. 4388 4389 \cfunction{void}{PL_free_hash_table_enum}{hash_table_enum_t e} 4390Discard an enumerator object created using PL_new_hash_table_enum(). 4391Failure to do so causes the table to use more and more memory on 4392subsequent modifications. 4393 4394 \cfunction{int}{PL_advance_hash_table_enum}{hash_table_enum_t e, 4395 void **key, void **value} 4396Get the next key-value pair from a cursor. 4397\end{description} 4398 4399\subsection{Debugging and profiling foreign code (valgrind)} 4400\label{sec:foreign-debug-and-profile} 4401 4402\index{valgrind}\index{profiling,foreign code}% 4403This section is only relevant for Unix users on platforms supported by 4404\href{http://valgrind.org/}{valgrind}. Valgrind is an excellent binary 4405instrumentation platform. Unlike many other instrumentation platforms, 4406valgrind can deal with code loaded through dlopen(). 4407 4408The \program{callgrind} tool can be used to profile foreign code loaded 4409under SWI-Prolog. Compile the foreign library adding \cmdlineoption{-g} 4410option to \program{gcc} or \program{swipl-ld}. By setting the environment 4411variable \env{VALGRIND} to \const{yes}, SWI-Prolog will \emph{not} 4412release loaded shared objects using dlclose(). This trick is required to 4413get source information on the loaded library. Without, valgrind claims 4414that the shared object has no debugging information.\footnote{Tested 4415using valgrind version 3.2.3 on x64.} Here is the complete sequence 4416using \program{bash} as login shell: 4417 4418\begin{code} 4419% VALGRIND=yes valgrind --tool=callgrind pl <args> 4420<prolog interaction> 4421% kcachegrind callgrind.out.<pid> 4422\end{code} 4423 4424\subsection{Name Conflicts in C modules} 4425\label{sec:foreign-name-conflicts} 4426 4427In the current version of the system all public C functions of 4428SWI-Prolog are in the symbol table. This can lead to name clashes with 4429foreign code. Someday I should write a program to strip all these 4430symbols from the symbol table (why does Unix not have that?). For now 4431I can only suggest you give your function another name. You can do this 4432using the C preprocessor. If---for example---your foreign package uses 4433a function warning(), which happens to exist in SWI-Prolog as well, the 4434following macro should fix the problem: 4435 4436\begin{code} 4437#define warning warning_ 4438\end{code} 4439 4440Note that shared libraries do not have this problem as the shared 4441library loader will only look for symbols in the main executable for 4442symbols that are not defined in the library itself. 4443 4444 4445\subsection{Compatibility of the Foreign Interface} 4446\label{sec:foreign-quintus-sicstus} 4447 4448The term reference mechanism was first used by Quintus Prolog version~3. 4449SICStus Prolog version 3 is strongly based on the Quintus interface. The 4450described SWI-Prolog interface is similar to using the Quintus or 4451SICStus interfaces, defining all foreign-predicate arguments of type 4452\const{+term}. SWI-Prolog explicitly uses type \ctype{functor_t}, while 4453Quintus and SICStus use <name> and <arity>. As the names of the functions 4454differ from Prolog to Prolog, a simple macro layer dealing with the 4455names can also deal with this detail. For example: 4456 4457\begin{code} 4458#define QP_put_functor(t, n, a) \ 4459 PL_put_functor(t, PL_new_functor(n, a)) 4460\end{code} 4461 4462The {\tt PL_unify_*()} functions are lacking from the Quintus and 4463SICStus interface. They can easily be emulated, or the put/unify 4464approach should be used to write compatible code. 4465 4466The PL_open_foreign_frame()/PL_close_foreign_frame() combination is 4467lacking from both other Prologs. SICStus has PL_new_term_refs(0), 4468followed by PL_reset_term_refs(), that allows for discarding 4469term references. 4470 4471The Prolog interface for the graphical user interface package XPCE 4472shares about 90\% of the code using a simple macro layer to deal 4473with different naming and calling conventions of the interfaces. 4474