View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    WWW:           http://www.swi-prolog.org
    4    Copyright (c)  2020-2021, SWI-Prolog Solutions b.v.
    5    All rights reserved.
    6
    7    Redistribution and use in source and binary forms, with or without
    8    modification, are permitted provided that the following conditions
    9    are met:
   10
   11    1. Redistributions of source code must retain the above copyright
   12       notice, this list of conditions and the following disclaimer.
   13
   14    2. Redistributions in binary form must reproduce the above copyright
   15       notice, this list of conditions and the following disclaimer in
   16       the documentation and/or other materials provided with the
   17       distribution.
   18
   19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   20    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   22    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   23    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   24    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   25    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   26    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   27    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   29    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   30    POSSIBILITY OF SUCH DAMAGE.
   31*/
   32
   33:- module(sicstus4,
   34	  [ nonmember/2,		% ?Element, ?List
   35	    statistics/2,		% ?Key, ?Value
   36	    op(1100, xfy, (do))
   37	  ]).   38:- reexport(sicstus,
   39	    [ (block)/1,
   40	      if/3,
   41	      use_module/3,
   42	      bb_put/2,
   43	      bb_get/2,
   44	      bb_delete/2,
   45	      bb_update/3,
   46	      is_mutable/1 as mutable,
   47	      create_mutable/2,
   48	      get_mutable/2,
   49	      update_mutable/2,
   50	      sicstus_is_readable_stream/1,
   51	      read_line/1,
   52	      read_line/2,
   53	      trimcore/0,
   54	      prolog_flag/3,
   55	      op(1150, fx, (block)),
   56	      op(1150, fx, (mode)),
   57	      op(900, fy, (spy)),
   58	      op(900, fy, (nospy))
   59	    ]).   60
   61/** <module> SICStus 4 compatibility library
   62
   63This library is intended to be activated using the directive below in
   64files that are designed for use with SICStus Prolog 4. The changes are in
   65effect until the end of the file and in each file loaded from this file.
   66
   67    ==
   68    :- expects_dialect(sicstus4).
   69    ==
   70
   71This library only provides compatibility with version 4 of SICStus Prolog.
   72For SICStus Prolog 3 compatibility, use library(dialect/sicstus) instead.
   73
   74@tbd	The dialect-compatibility packages are developed in a
   75	`demand-driven' fashion. Please contribute to this package.
   76*/
   77
   78% Note: Although the do operator is declared here, do loops currently
   79% aren't emulated by library(dialect/sicstus4).
   80:- op(1100, xfy, user:(do)).   81
   82
   83		 /*******************************
   84		 *	    LIBRARY SETUP	*
   85		 *******************************/
   86
   87%%	push_sicstus4_library
   88%
   89%	Pushes searching for dialect/sicstus4 in front of every library
   90%	directory that contains such as sub-directory.
   91
   92push_sicstus4_library :-
   93	(   absolute_file_name(library(dialect/sicstus4), Dir,
   94			       [ file_type(directory),
   95				 access(read),
   96				 solutions(all),
   97				 file_errors(fail)
   98			       ]),
   99	    asserta((user:file_search_path(library, Dir) :-
  100		    prolog_load_context(dialect, sicstus4))),
  101	    fail
  102	;   true
  103	).
  104
  105
  106:- push_sicstus4_library.  107
  108
  109%%	setup_dialect
  110%
  111%	Further dialect initialization.
  112%
  113%	sicstus4 currently performs the same initialization
  114%	as the sicstus (SICStus 3) dialect.
  115
  116setup_dialect :- sicstus:setup_dialect.
  117
  118
  119		 /*******************************
  120		 *	  LIBRARY MODULES	*
  121		 *******************************/
  122
  123%%	rename_module(?SICStus4Module, ?RenamedSICStus4Module) is nondet.
  124%
  125%	True if RenamedSICStus4Module is the  name  that  we use for the
  126%	SICStus 4 native module SICStus4Module. We do this in places where
  127%	the module-name conflicts. All explicitly qualified goals are
  128%	mapped to the SICStus 4 equivalent of the module.
  129
  130:- multifile
  131	rename_module/2.  132
  133system:goal_expansion(M:Goal, SicstusM:Goal) :-
  134	atom(M),
  135	rename_module(M, SicstusM),
  136	prolog_load_context(dialect, sicstus4).
  137
  138
  139% SICStus 4 copy_term/2 behaves like SWI copy_term_nat/2.
  140user:goal_expansion(copy_term(Term, Copy), copy_term_nat(Term, Copy)) :-
  141	prolog_load_context(dialect, sicstus4).
  142
  143
  144%%	nonmember(?Element, ?List) is semidet.
  145%
  146%	True if Element doesn't unify with any element in List.
  147%	No variables are bound.
  148
  149nonmember(Element, List) :- \+ memberchk(Element, List).
  150
  151% As of SICStus 4.6.0, the following statistics/2 keys are still missing:
  152% Introduced in SICStus 4.0:
  153% * defragmentation
  154% Introduced in SICStus 4.1:
  155% * choice_used, choice_free
  156% * defrag_count, defrag_time
  157% * dpgc_count, dpgc_time
  158% * memory_culprit
  159% * memory_buckets
  160% Introduced in SICStus 4.3:
  161% * jit_count, jit_time
  162
  163statistics(total_runtime, Stats) :- !, system:statistics(runtime, Stats).
  164% The following keys were introduced with SICStus Prolog 4.1.
  165statistics(memory_used, BytesUsed) :- !, system:statistics(memory, [BytesUsed, _]).
  166statistics(memory_free, BytesFree) :- !, system:statistics(memory, [_, BytesFree]).
  167statistics(global_stack_used, BytesUsed) :- !, system:statistics(global_stack, [BytesUsed, _]).
  168statistics(global_stack_free, BytesFree) :- !, system:statistics(global_stack, [_, BytesFree]).
  169statistics(local_stack_used, BytesUsed) :- !, system:statistics(local_stack, [BytesUsed, _]).
  170statistics(local_stack_free, BytesFree) :- !, system:statistics(local_stack, [_, BytesFree]).
  171statistics(trail_used, BytesUsed) :- !, system:statistics(trail, [BytesUsed, _]).
  172statistics(trail_free, BytesFree) :- !, system:statistics(trail, [_, BytesFree]).
  173statistics(atoms_used, BytesUsed) :- !, system:statistics(atom_space, BytesUsed).
  174statistics(atoms_nbused, CountUsed) :- !, system:statistics(atoms, CountUsed).
  175statistics(atoms_nbfree, CountFree) :- !, CountFree = 0.
  176statistics(ss_global, Count) :- !, system:statistics(stack_shifts, [Count, _, _]).
  177statistics(ss_local, Count) :- !, system:statistics(stack_shifts, [_, Count, _]).
  178statistics(ss_time, Time) :- !, system:statistics(stack_shifts, [_, _, Time]).
  179statistics(gc_count, Count) :- !, system:statistics(garbage_collection, [Count, _, _|_]).
  180statistics(gc_freed, BytesFreed) :- !, system:statistics(garbage_collection, [_, BytesFreed, _|_]).
  181statistics(gc_time, Time) :- !, system:statistics(garbage_collection, [_, _, Time|_]).
  182statistics(agc_count, Count) :- !, system:statistics(atom_garbage_collection, [Count, _, _]).
  183statistics(agc_freed, BytesFreed) :- !, system:statistics(atom_garbage_collection, [_, BytesFreed, _]).
  184statistics(agc_time, Time) :- !, system:statistics(atom_garbage_collection, [_, _, Time]).
  185statistics(dcgc_count, Count) :- !, system:statistics(clause_garbage_collection, [Count, _, _]).
  186statistics(dcgc_time, Time) :- !, system:statistics(clause_garbage_collection, [_, _, Time]).
  187
  188:- use_module(sicstus, [statistics/2 as sicstus3_statistics]).  189statistics(Keyword, Value) :- sicstus3_statistics(Keyword, Value).
  190
  191% Provide (\)/2 as arithmetic function.  Ideally, we should be able to
  192% bind multiple names to built-in functions.  This is rather slow.  We
  193% could also consider adding \ internally, but not turning it into an
  194% operator.
  195
  196:- arithmetic_function(user:(\)/2).  197
  198user:(\(X,Y,R)) :-
  199	R is xor(X,Y)