View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  1985-2015, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(quintus,
   37        [ unix/1,
   38%         file_exists/1,
   39
   40          abs/2,
   41          sin/2,
   42          cos/2,
   43          tan/2,
   44          log/2,
   45          log10/2,
   46          pow/3,
   47          ceiling/2,
   48          floor/2,
   49          round/2,
   50          acos/2,
   51          asin/2,
   52          atan/2,
   53          atan2/3,
   54          sign/2,
   55          sqrt/2,
   56
   57          genarg/3,
   58
   59          (mode)/1,
   60          no_style_check/1,
   61          otherwise/0,
   62          simple/1,
   63%         statistics/2,                 % Please access as quintus:statistics/2
   64          prolog_flag/2,
   65
   66          date/1,                       % -date(Year, Month, Day)
   67
   68          current_stream/3,             % ?File, ?Mode, ?Stream
   69          stream_position/3,            % +Stream, -Old, +New
   70          skip_line/0,
   71          skip_line/1,                  % +Stream
   72
   73          compile/1,                    % +File(s)
   74
   75          atom_char/2,
   76          midstring/3,                  % ABC, B, AC
   77          midstring/4,                  % ABC, B, AC, LenA
   78          midstring/5,                  % ABC, B, AC, LenA, LenB
   79          midstring/6,                  % ABC, B, AC, LenA, LenB, LenC
   80
   81          raise_exception/1,            % +Exception
   82          on_exception/3                % +Ball, :Goal, :Recover
   83        ]).   84:- autoload(library(apply),[maplist/3]).   85:- autoload(library(date),[date_time_value/3]).   86:- autoload(library(shell),[shell/0]).

Quintus compatibility

This module defines several predicates from the Quintus Prolog libraries. Note that our library structure is totally different. If this library were complete, Prolog code could be ported by removing the use_module/1 declarations, relying on the SWI-Prolog autoloader.

Bluffers guide to porting:

Of course, this library is incomplete ... */

  105                /********************************
  106                *      SYSTEM INTERACTION       *
  107                *********************************/
 unix(+Action)
interface to Unix.
  112unix(system(Command)) :-
  113    shell(Command).
  114unix(shell(Command)) :-
  115    shell(Command).
  116unix(shell) :-
  117    shell.
  118unix(access(File, 0)) :-
  119    access_file(File, read).
  120unix(cd) :-
  121    expand_file_name(~, [Home]),
  122    working_directory(_, Home).
  123unix(cd(Dir)) :-
  124    working_directory(_, Dir).
  125unix(args(L)) :-
  126    current_prolog_flag(os_argv, L).
  127unix(argv(L)) :-
  128    current_prolog_flag(os_argv, S),
  129    maplist(to_prolog, S, L).
  130
  131to_prolog(S, A) :-
  132    name(S, L),
  133    name(A, L).
  134
  135
  136                /********************************
  137                *        META PREDICATES        *
  138                *********************************/
 otherwise
For (A -> B ; otherwise -> C)
  144otherwise.
  145
  146
  147                /********************************
  148                *          ARITHMETIC           *
  149                *********************************/
 abs(+Number, -Absolute)
Unify `Absolute' with the absolute value of `Number'.
  154abs(Number, Absolute) :-
  155    Absolute is abs(Number).
 sin(+Angle, -Sine) is det
 cos(+Angle, -Cosine) is det
 tan(+Angle, -Tangent) is det
 log(+X, -NatLog) is det
 log10(+X, -Log) is det
 pow(+X, +Y, -Pow) is det
 ceiling(+X, -Value) is det
 floor(+X, -Value) is det
 round(+X, -Value) is det
 sqrt(+X, -Value) is det
 acos(+X, -Value) is det
 asin(+X, -Value) is det
 atan(+X, -Value) is det
 atan2(+Y, +X, -Value) is det
 sign(+X, -Value) is det
Math library predicates. SWI-Prolog (and ISO) support these as functions under is/2, etc.
deprecated
- Do not use these predicates except for compatibility reasons.
Compatibility
- Quintus Prolog.
  180sin(A, V) :-      V is sin(A).
  181cos(A, V) :-      V is cos(A).
  182tan(A, V) :-      V is tan(A).
  183log(A, V) :-      V is log(A).
  184log10(X, V) :-    V is log10(X).
  185pow(X,Y,V) :-     V is X**Y.
  186ceiling(X, V) :-  V is ceil(X).
  187floor(X, V) :-    V is floor(X).
  188round(X, V) :-    V is round(X).
  189sqrt(X, V) :-     V is sqrt(X).
  190acos(X, V) :-     V is acos(X).
  191asin(X, V) :-     V is asin(X).
  192atan(X, V) :-     V is atan(X).
  193atan2(Y, X, V) :- V is atan(Y, X).
  194sign(X, V) :-     V is sign(X).
  195
  196
  197                 /*******************************
  198                 *      TERM MANIPULATION       *
  199                 *******************************/
 genarg(?Index, +Term, ?Arg) is nondet
Generalised version of ISO arg/3. SWI-Prolog's arg/3 is already genarg/3.
  206genarg(N, T, A) :-
  207    arg(N, T, A).
  208
  209
  210                 /*******************************
  211                 *            FLAGS             *
  212                 *******************************/
 prolog_flag(?Flag, ?Value) is nondet
Same as ISO current_prolog_flag/2. Maps version.
bug
- Should map relevant Quintus flag identifiers.
  220prolog_flag(version, Version) :-
  221    !,
  222    current_prolog_flag(version_data, swi(Major, Minor, Patch, _)),
  223    current_prolog_flag(arch, Arch),
  224    current_prolog_flag(compiled_at, Compiled),
  225    atomic_list_concat(['SWI-Prolog ',
  226                 Major, '.', Minor, '.', Patch,
  227                 ' (', Arch, '): ', Compiled], Version).
  228prolog_flag(Flag, Value) :-
  229    current_prolog_flag(Flag, Value).
  230
  231
  232                 /*******************************
  233                 *          STATISTICS          *
  234                 *******************************/
  235
  236%       Here used to be a definition of Quintus statistics/2 in traditional
  237%       SWI-Prolog statistics/2.  The current built-in emulates Quintus
  238%       almost completely.
  239
  240
  241                 /*******************************
  242                 *           DATE/TIME          *
  243                 *******************************/
 date(-Date) is det
Get current date as date(Y,M,D)
  249date(Date) :-
  250    get_time(T),
  251    stamp_date_time(T, DaTime, local),
  252    date_time_value(date, DaTime, Date).
  253
  254
  255                /********************************
  256                *          STYLE CHECK          *
  257                *********************************/
 no_style_check(Style) is det
Same as SWI-Prolog style_check(-Style). The Quintus option single_var is mapped to singleton.
See also
- style_check/1.
  266q_style_option(single_var, singleton) :- !.
  267q_style_option(Option, Option).
  268
  269no_style_check(QOption) :-
  270    q_style_option(QOption, SWIOption),
  271    style_check(-SWIOption).
  272
  273
  274                /********************************
  275                *         DIRECTIVES            *
  276                *********************************/
 mode(+ModeDecl) is det
Ignore a DEC10/Quintus :- mode(Head) declaration. Typically these declarations are written in operator form. The operator declaration is not part of the Quintus emulation library. The following declaration is compatible with Quintus:
:- op(1150, fx, [(mode)]).
  289mode(_).
  290
  291
  292                 /*******************************
  293                 *            TYPES             *
  294                 *******************************/
 simple(@Term) is semidet
Term is atomic or a variable.
  300simple(X) :-
  301    (   atomic(X)
  302    ->  true
  303    ;   var(X)
  304    ).
  305
  306
  307                 /*******************************
  308                 *            STREAMS           *
  309                 *******************************/
 current_stream(?Object, ?Mode, ?Stream)
SICStus/Quintus and backward compatible predicate. New code should be using the ISO compatible stream_property/2.
  316current_stream(Object, Mode, Stream) :-
  317    stream_property(Stream, mode(FullMode)),
  318    stream_mode(FullMode, Mode),
  319    (   stream_property(Stream, file_name(Object0))
  320    ->  true
  321    ;   stream_property(Stream, file_no(Object0))
  322    ->  true
  323    ;   Object0 = []
  324    ),
  325    Object = Object0.
  326
  327stream_mode(read,   read).
  328stream_mode(write,  write).
  329stream_mode(append, write).
  330stream_mode(update, write).
 stream_position(+Stream, -Old, +New)
True when Old is the current position in Stream and the stream has been repositioned to New.
deprecated
- New code should use the ISO predicates stream_property/2 and set_stream_position/2.
  340stream_position(Stream, Old, New) :-
  341    stream_property(Stream, position(Old)),
  342    set_stream_position(Stream, New).
 skip_line is det
 skip_line(Stream) is det
Skip the rest of the current line (on Stream). Same as skip(0'\n).
  351skip_line :-
  352    skip(10).
  353skip_line(Stream) :-
  354    skip(Stream, 10).
  355
  356
  357                 /*******************************
  358                 *         COMPILATION          *
  359                 *******************************/
 compile(+Files) is det
Compile files. SWI-Prolog doesn't distinguish between compilation and consult.
See also
- load_files/2.
  368:- meta_predicate
  369    compile(:).  370
  371compile(Files) :-
  372    consult(Files).
  373
  374                 /*******************************
  375                 *         ATOM-HANDLING        *
  376                 *******************************/
 atom_char(+Char, -Code) is det
atom_char(-Char, +Code) is det
Same as ISO char_code/2.
  383atom_char(Char, Code) :-
  384    char_code(Char, Code).
 midstring(?ABC, ?B, ?AC) is nondet
 midstring(?ABC, ?B, ?AC, LenA) is nondet
 midstring(?ABC, ?B, ?AC, LenA, LenB) is nondet
 midstring(?ABC, ?B, ?AC, LenA, LenB, LenC) is nondet
Too difficult to explain. See the Quintus docs. As far as I understand them the code below emulates this function just fine.
  394midstring(ABC, B, AC) :-
  395    midstring(ABC, B, AC, _, _, _).
  396midstring(ABC, B, AC, LenA) :-
  397    midstring(ABC, B, AC, LenA, _, _).
  398midstring(ABC, B, AC, LenA, LenB) :-
  399    midstring(ABC, B, AC, LenA, LenB, _).
  400midstring(ABC, B, AC, LenA, LenB, LenC) :-      % -ABC, +B, +AC
  401    var(ABC),
  402    !,
  403    atom_length(AC, LenAC),
  404    (   nonvar(LenA) ; nonvar(LenC)
  405    ->  plus(LenA, LenC, LenAC)
  406    ;   true
  407    ),
  408    sub_atom(AC, 0, LenA, _, A),
  409    LenC is LenAC - LenA,
  410    sub_atom(AC, _, LenC, 0, C),
  411    atom_length(B, LenB),
  412    atomic_list_concat([A,B,C], ABC).
  413midstring(ABC, B, AC, LenA, LenB, LenC) :-
  414    sub_atom(ABC, LenA, LenB, LenC, B),
  415    sub_atom(ABC, 0, LenA, _, A),
  416    sub_atom(ABC, _, LenC, 0, C),
  417    atom_concat(A, C, AC).
  418
  419
  420                 /*******************************
  421                 *           EXCEPTIONS         *
  422                 *******************************/
 raise_exception(+Term)
Quintus compatible exception handling
  428raise_exception(Term) :-
  429    throw(Term).
 on_exception(+Template, :Goal, :Recover)
  433:- meta_predicate
  434    on_exception(+, 0, 0).  435
  436on_exception(Except, Goal, Recover) :-
  437    catch(Goal, Except, Recover)