# Ann: SWI-Prolog 8.5.18

**URL:** <https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828>\
**Category:** Releases\
**Created:** [October 6, 2022, 2:16pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828 "2022-10-06T14:16:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 6, 2022, 2:16pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/1 "2022-10-06T14:16:04Z")

</div>

Dear SWI-Prolog user,

SWI-Prolog 8.5.18 is ready for download. Quite a few things happened.

- @oskardrums “sweep” Prolog mode is now bundled, which should make  
it easy to obtain the shared object that is required for running  
this nice Emacs mode.

- @anon95304481’s comments and @ridgeworks implementation work brought  
correct rounding and fast big integer and rational support to  
floats.

- @peter.ludemann’s work on a new C++ interface is now included as  
SWI-cpp2.h. It is much more comprehensive and avoids many of the  
pitfalls of the old SWI-cpp.h. Unfortuantely there are still  
some problems with it. Notably we would like to see issues around  
character encodings settled. Still, if you start a new C++ project  
this should be your starting point. Porting old C++ projects is  
a good way to become future proof and evaluate the new interface.

- Quite a few extensions to the WASM version. The basic interfaces  
should be close to stable.

- @swi allowed help/1 to work on your own annotated predicates.

# SWI-Prolog Changelog since version 8.5.17

- WASM: Do not use undocumented interfaces in test/demo.

- WASM: Issue#1052: make output of Prolog.query().once() easier to  
handle.

- FIXED: help/1 for normal built-in predicates (broke after support  
for user predicates). Patch by @swi.

- ADDED: Colour test/1 and test/2 heads and classify them as tests when  
inside a unit test body.

- ADDED: library(prolog\_xref): Make aware of unit tests

- ADDED: Allow help/1 on user predicates. Contributed by @swi

- ADDED: make\_reload\_file/1 to make correct reloading of a file public.

- TEST: Add additional float rounding test cases for various integer  
sub-types.

- ENHANCED: New (faster) function for converting bigints (MPZ) to float  
Also includes minor adjustments for some `mpz_fdiv` rounding modes.

- DOC: Fixed LaTeX errors

- ENHANCED: Replace mpz\_fdiv() with a version that rounds to nearest.

- CLEANUP: Introduce mpz\_to\_double()

- FIXED: Added missing import for pi\_head/2 in library(prolog\_xref).

- PORT: `swipl --abi-version`: VM signature hash depended on big/little  
endian.

- ADDED: PL\_atom\_mbchars() to extract text from an atom in various  
encodings.

- ADDED: PL\_cvt\_i\_[u]int32() for consistency.

- PORT: Avoid false alarm warning from gcc on 32-bit Windows.

- PORT: Windows: fixed compilation of the DDE interface after adding  
`FUNCTOR_error1` to the core system.

- FIXED: Instantiation error in cross-referencer.

- FIXED: xref\_source/1: avoid lazy definition of predicates in `user`  
by calling `predicate_property(user:Goal, ...)`.

- FIXED: prolog\_set\_IO/3: stderr wrapper interpreted return code from  
low-level output wrong. This causes problems if output is connected  
to a socket as used by prolog\_server/2.

- ENHANCED: Make toplevel quit nicely if the I/O streams are in error  
state.

- ADDED: stream\_property(S, error(Boolean)).

- PORT: Msys2, fix LibDir for swipl in build env

- ADDED: sweep package - integration with GNU Emacs

- FIXED: Windows: directory\_files/2 and other directory access for  
long paths. Fixed by @mgondan1. Thanks!

- ENHANCED: portray\_clause/1: add space after `(?- goal)`.

- WASM: Avoid error on HTMLCollection on node

- FIXED: Windows: long file support was broken again.

- FIXED: Handle reexport/1 and reexport/2 properly in multi-file  
.qlf files. Reported by @swi.

- DOC: syntax error handling in conditional compilation.

- ENHANCED: ^ and \*\* arithmetic operator optimization for small integer  
use cases to avoid relying on GMP. This patch also uses integer  
exponentiation when compiled without GMP when possible and handles  
all special cases compatible with the GMP based version.

- ENHANCED: Use \_\_builtin\_mul\_overflow() when available to do fast  
overflow-safe multiplication.

- TEST: Silence syntax error on a rational number when compiled without  
rational support.

- MODIFIED: When inside code fragment under conditional compilation  
where the code is ignored, do not report syntax errors.

- WASM: Added CHAT80 demo.

- FIXED: Prolog.Compound.toJSON()

- WASM: Avoid dependency on `HTMLCollection` to run on node.

- MODIFIED: WASM: Prolog.consult() now calls load\_files/1, allowing it  
to handle a mixture of local files, URLs as well as Prolog sources  
and .qlf files. The sources are now processed _sequentially_ and  
are no longer materialised as files.

- DOC: Improved WASM documentation.

- ADD: License to CBG Chords example

- ADDED: qcompile/2: option `include(user)`. This option allows for  
compiling an application into a single Prolog .qlf file.

- ADDED: load\_files/2: option if(Cond) is extended with `exists` to  
silently not load a file if it does not exist.

- FIXED: det/1 was not declared as a proper meta-predicate.

- WASM: Allow translation of JavaScript `ArrayBuffer` into a Prolog  
byte-string. This allows extracting binary data from `Response.blob()`  
and is used by the WASM compilation hook to allow loading `.qlf`  
files from URLs.

- BUILD: Allow building with AddressSanitizer on Clang. Based on  
patches by Alessandro Bartolucci. Thanks!

## Package clib

- ENHANCED: prolog\_server/2: ensure the server thread terminates,  
also if the client disappears with an error. Improved docs.

## Package cpp

- TEST: Remove .as\_string.c\_str() as this is unsafe.

- DOC: Document both version 1 and version 2.

- DOC: Setup for documenting both SWI-cpp.h and SWI-cpp2.h

- ENHANCED: added PlTerm::record(), PlTerm::write(), PlTerm\_recorded,  
PlRewindOnFail(), PlStringBuffers Removed PlTerm::unify\__check()  
Added example code for backtracking predicate that gets info on int  
types Fixing integer overloading for win32, MacOS - remove many of the  
overloaded methods and instead use names derived from the underlying  
SWI-Prolog PL\_put_() and PL\_unify\*()

- DOC: Added a porting section

- PORT: Make SWI-cpp2.h compile on systems where `long int` is 32 bits  
(Win64). Patch by Peter Ludemann.

- CLEANUP: better reuse of Prolog exceptions.

- ENHANCED: Version 2 of C++ interface - removed some implicit  
conversions and constructors whose use can cause subtle bugs -  
made most conversions and constructors explicit - added subclass  
constructors to make Prolog types explicit and avoid some subtle bugs

## Package http

- FIXED: term//2 from library(http/term\_html) left a choicepoint.

## Package semweb

- DOC: Fixed LaTeX errors.

## Package xpce

- ENHANCED: PceEmacs: properties of predicates in status line (better  
text and ignore more irrelevant).

- ADDED: pce\_xref\_gui:gxref\_called/2 hook into gxref/0 to allow silencing  
predicates that are called by means of hooks or some other mechanism.

- FIXED: gxref/0: edit predicate on click to properly handling included  
files.

- PORT: suppress uninitialized warning in msys2 gcc

- PORT: msys2-compatible mkdir and wildcards

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [October 6, 2022, 8:39pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/2 "2022-10-06T20:39:44Z")

</div>

> [@jan](#):
>
> ENHANCED: Replace mpz\_fdiv() with a version that rounds to nearest.

Minor nit-pick for those who might care:

I think it’s better than that. `mpz_fdiv()` now rounds according to the current IEEE rounding mode; “to nearest” is the default.

---

<div class="post-metadata">

**Author:** ![mgondan1](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mgondan1/32/803_2.png) [@mgondan1](https://swi-prolog.discourse.group/u/mgondan1)\
**Post date:** [October 9, 2022, 12:11pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/4 "2022-10-09T12:11:18Z")

</div>

MSYS2: I get an error in the unit tests for package cpp because the winsock function gethostbyname failed. The error code is 10093, winsock is not initialized. The problem can be fixed by calling WSAStartup beforehand and WSACleanup afterwards.

> **[WSAStartup function (winsock.h) - Win32 apps](https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup)**
>
> The WSAStartup function (winsock.h) initiates use of the Winsock DLL by a process.

I guess this would go into the initialization and cleanup of cpp4pl.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 9, 2022, 1:53pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/5 "2022-10-09T13:53:33Z")

</div>

☹@peter.ludemann , maybe we better backtrack and remove the gethostname() example. I know it comes from the documentation, but having a test that is not easily portable might not be a good idea. You could simply use get\_test\_hostname() to simply return `"example.org"` …

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [October 9, 2022, 5:33pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/6 "2022-10-09T17:33:26Z")

</div>

Or maybe return the string-ified version of one of the predefined macros? It seems that ` __TIMESTAMP__ ` is safe across compilers, for example:

> **[Common Predefined Macros (The C Preprocessor)](https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html)**
>
> Common Predefined Macros (The C Preprocessor)

> **[Predefined macros](https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170)**
>
> Lists and describes the Microsoft C++ compiler predefined preprocessor macros.

For example, this has the same parameters as gethostname():

```cpp
int gettimestamp(char* buf, size_t len)
{ if ( len <= 0 )
    return -1;
  strncpy(buf, __TIMESTAMP__ , len);
  if ( buf[len - 1] )
    return -1; /* buf is too small */
  return 0;
}

```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [October 10, 2022, 12:08pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/7 "2022-10-10T12:08:22Z")

</div>

For now I pushed a14d19a37a193019a6061bd6295020e5d4b8d3a5 to cpp which replaces gethostname() simply by a dummy no\_gethostname() with the same calling conventions. Now we do not depend on anything.

Note that using ` __TIMESTAMP__ ` gives us a binary that cannot be produced reproducible and that also causes some package managers to start complaining 🙂

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [October 10, 2022, 5:27pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/8 "2022-10-10T17:27:35Z")

</div>

> [@anon95304481](#):
>
> Wiki tells me there are at least two “to nearest”:

From [IEEE 754 - Wikipedia](https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules)

> Round to nearest, ties to even is the default for binary floating point and the recommended default for decimal. Round to nearest, ties to away is only required for decimal implementations.

I believe the C floating point API only supports the default.

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [October 13, 2022, 6:32pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/10 "2022-10-13T18:32:22Z")

</div>

> [@anon95304481](#):
>
> Maybe, maybe not.

The C specification API for rounding mode control `<fenv.h>` can be found in section 7.6 of [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) .In particular:

> Each of the macros  
> FE\_DOWNWARD  
> FE\_TONEAREST  
> FE\_TOWARDZERO  
> FE\_UPWARD  
> is defined if and only if the implementation supports getting and setting the represented rounding direction by means of the fegetround and fesetround functions.

> [@anon95304481](#):
>
> Do you knowa test case to figure out what rounding a system, Prolog or not, does …

Evidence to the contrary, I would expect most computer language implementations would use the default round to nearest mode, since that’s what C does.

Among other things, SWIP provides an arithmetic function [`roundtoward/2`](https://www.swi-prolog.org/pldoc/man?function=roundtoward/2) to control rounding mode during arithmetic evaluation. But I think this is uncharted territory for most Prologs. Eclipse supports bounded reals so there must be some control over rounding at some level, but I’m unfamiliar with the details.

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [October 13, 2022, 7:52pm UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/12 "2022-10-13T19:52:29Z")

</div>

> [@anon95304481](#):
>
> Have to dig deeper, could be a parsing problem.

Or output? Seems to manufacture bogus output digits on demand:

```prolog
?- X is float(665723957508124892951320985600), format("~18e",[X]).
6.657239575081249633e+29
X = 6.65723957508125e+29.

```

What can you trust?

---

<div class="post-metadata">

**Author:** ![swi](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@swi](https://swi-prolog.discourse.group/u/swi)\
**Post date:** [October 14, 2022, 12:34am UTC](https://swi-prolog.discourse.group/t/ann-swi-prolog-8-5-18/5828/14 "2022-10-14T00:34:15Z")

</div>

SWI-Prolog gives the same two different binaries with `format('~2r',...)`

```prolog
4 ?- X is integer(6.657239575081249633e+29), format('~2r',[X]).
10000110011100010001111100110111001001001100001111 %added nl here
11000000000000000000000000000000000000000000000000
X = 665723957508124963320065163264.

5 ?- X is integer(6.6572395750812482e+29), format('~2r',[X]).
10000110011100010001111100110111001001001100001111 % added nl here
10100000000000000000000000000000000000000000000000
X = 665723957508124822582576807936.

```
