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    (   Dialect == swi
   76    ->  true
   77    ;   attach_dialect(Dialect)
   78    ),
   79    set_prolog_flag(emulated_dialect, Dialect).
   80
   81attach_dialect(Dialect) :-
   82    exists_source(library(dialect/Dialect)),
   83    !,
   84    prolog_load_context(module, Module),
   85    use_module(Module:library(dialect/Dialect)),
   86    (   current_predicate(Dialect:setup_dialect/0)
   87    ->  Dialect:setup_dialect
   88    ;   true
   89    ).
   90attach_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)?
  100source_exports(Source, Export) :-
  101    open_source(Source, In),
  102    catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
  103    (   ground(Export)
  104    ->  memberchk(Export, Exports)
  105    ;   member(Export, Exports)
  106    ).
 open_source(+Source, -In:stream) is semidet
Open a source location.
  112open_source(File, In) :-
  113    exists_source(File, Path),
  114    open(Path, read, In),
  115    (   peek_char(In, #)
  116    ->  skip(In, 10)
  117    ;   true
  118    ).
  119
  120exports(In, Exports) :-
  121    read(In, Term),
  122    Term = (:- module(_Name, Exports))