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-2011, University of Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(prolog_dialect,
   36          [ expects_dialect/1,          % +Dialect
   37            exists_source/1,            % +Source
   38            source_exports/2            % +Source, ?Export
   39          ]).   40:- use_module(library(error)).   41:- use_module(library(lists)).

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?
   71expects_dialect(Dialect) :-
   72    must_be(atom, Dialect),
   73    set_prolog_flag(emulated_dialect, Dialect),
   74    (   Dialect == swi
   75    ->  true
   76    ;   attach_dialect(Dialect)
   77    ).
   78
   79
   80attach_dialect(Dialect) :-
   81    exists_source(library(dialect/Dialect)),
   82    !,
   83    prolog_load_context(module, Module),
   84    use_module(Module:library(dialect/Dialect)),
   85    (   current_predicate(Dialect:setup_dialect/0)
   86    ->  Dialect:setup_dialect
   87    ;   true
   88    ).
   89attach_dialect(_).
 exists_source(+Source) is semidet
True if Source (a term valid for load_files/2) exists. Fails without error if this is not the case. The predicate is intended to be used with :- if, as in the example below. See also source_exports/2.
:- if(exists_source(library(error))).
:- use_module_library(error).
:- endif.
  105exists_source(Source) :-
  106    exists_source(Source, _Path).
  107
  108exists_source(Source, Path) :-
  109    absolute_file_name(Source, Path,
  110                       [ file_type(prolog),
  111                         access(read),
  112                         file_errors(fail)
  113                       ]).
 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)?
  123source_exports(Source, Export) :-
  124    open_source(Source, In),
  125    catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
  126    (   ground(Export)
  127    ->  memberchk(Export, Exports)
  128    ;   member(Export, Exports)
  129    ).
 open_source(+Source, -In:stream) is semidet
Open a source location.
  135open_source(File, In) :-
  136    exists_source(File, Path),
  137    open(Path, read, In),
  138    (   peek_char(In, #)
  139    ->  skip(In, 10)
  140    ;   true
  141    ).
  142
  143exports(In, Exports) :-
  144    read(In, Term),
  145    Term = (:- module(_Name, Exports))