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)  2007-2020, University of Amsterdam
    7                              CWI, 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(prolog_dialect,
   37          [ expects_dialect/1,          % +Dialect
   38            exists_source/1,            % +Source
   39            source_exports/2            % +Source, ?Export
   40          ]).   41:- autoload(library(error),[must_be/2]).   42:- autoload(library(lists),[member/2]).

Support multiple Prolog dialects

The idea for this predicate was raised by Vitor Santos Costa in a discussion to reach as a portability framework between SWI-Prolog and YAP.

This library defines :- expects_dialect/1, telling the system for which Prolog dialect was written, as well as useful tests in conditional compilation:

author
- Jan Wielemaker
- Vitor Santos Costa */
See also
- if/1, require/1, term_expansion/2, goal_expansion/2.
 expects_dialect(+Dialect:atom) is det
Tell Prolog all subsequent code to the end of the file or the next :- expects_dialect/1 directive is written for the indicated Dialect. The current dialect is available through prolog_load_context/2.
To be done
- Should we setup the dialect module only as autoload for the current module?
   73expects_dialect(Dialect) :-
   74    must_be(atom, Dialect),
   75    set_prolog_flag(emulated_dialect, Dialect),
   76    (   Dialect == swi
   77    ->  true
   78    ;   attach_dialect(Dialect)
   79    ).
   80
   81
   82attach_dialect(Dialect) :-
   83    exists_source(library(dialect/Dialect)),
   84    !,
   85    prolog_load_context(module, Module),
   86    use_module(Module:library(dialect/Dialect)),
   87    (   current_predicate(Dialect:setup_dialect/0)
   88    ->  Dialect:setup_dialect
   89    ;   true
   90    ).
   91attach_dialect(_).
 source_exports(+Source, +Export) is semidet
source_exports(+Source, -Export) is nondet
True if Source exports Export. Fails without error if this is not the case. See also exists_source/1.
To be done
- Should we also allow for source_exports(-Source, +Export)?
  101source_exports(Source, Export) :-
  102    open_source(Source, In),
  103    catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
  104    (   ground(Export)
  105    ->  memberchk(Export, Exports)
  106    ;   member(Export, Exports)
  107    ).
 open_source(+Source, -In:stream) is semidet
Open a source location.
  113open_source(File, In) :-
  114    exists_source(File, Path),
  115    open(Path, read, In),
  116    (   peek_char(In, #)
  117    ->  skip(In, 10)
  118    ;   true
  119    ).
  120
  121exports(In, Exports) :-
  122    read(In, Term),
  123    Term = (:- module(_Name, Exports))