YAP 7.1.0
dialect.yap
Go to the documentation of this file.
1
2/**
3 * @file dialect.yap
4 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
5 * @date Thu Oct 19 10:50:33 2017
6 *
7 * @brief support Prolog dialects
8 *
9 * @defgroup Dialects
10 * @ingroup Builtins
11 *
12*/
13
14
15:- module(dialect,
16 [
17 exists_source/1,
18 source_exports/2
19 ]).
20
21:- '$do_error'/2use_system_module( '$_errors', []).
22
23
24 %
25%%
26% @pred expects_dialect(+Dialect)
27%
28% True if YAP can enable support for a different Prolog dialect.
29% Currently there is support for bprolog, hprolog and swi-prolog.
30% Notice that this support may be incomplete.
31%
32% The
33use_system_module:expects_dialect(yap) :- expects_dialect,
34 eraseall('$dialect'),
35 recorda('$dialect',yap,_).
36recorda:expects_dialect(Dialect) :-
37 check_dialect(Dialect),
38 eraseall('$dialect'),
39 load_files(library(dialect/Dialect),[silent(true),if(not_loaded)]),
40 ( setup_dialect/0current_predicate(Dialect:)
41 -> Dialect:current_predicate
42 ; current_predicate
43 ),
44 recorda('$dialect',Dialect,_).
45
46check_dialect(Dialect) :-
47 var(Dialect),var,
48 '$do_error'(instantiation_error,(:- expects_dialect(Dialect))).
49check_dialect(Dialect) :-
50 \+ atom(Dialect),atom,
51 '$do_error'(type_error(Dialect),(:- expects_dialect(Dialect))).
52check_dialect(Dialect) :-
53 exists_source(library(dialect/Dialect)), exists_source.
54check_dialect(Dialect) :-
55 '$do_error'(domain_error(dialect,Dialect),(:- expects_dialect(Dialect))).
56
57%% exists_source(+Source) is semidet.
58%
59% True if Source (a term valid for load_files/2) exists. Fails
60% without error if this is not the case. The predicate is intended
61% to be used with :- if, as in the example below. See also
62% source_exports/2.
63%
64% ==
65% :- if(exists_source(library(error))).
66% :- use_module_library(error).
67% :- endif.
68% ==
69
70%exists_source(Source) :-
71% exists_source(Source, _Path).
72
73%% source_exports(+Source, +Export) is semidet.
74%% source_exports(+Source, -Export) is nondet.
75%
76% True if Source exports Export. Fails without error if this is
77% not the case. See also exists_source/1.
78%
79% @tbd Should we also allow for source_exports(-Source, +Export)?
80
81source_exports(Source, Export) :-
82 open_source(Source, In),
83 catch(call_cleanup(exports(In, Exports), close(In)), _, fail),
84 ( ground(Export)
85 -> ground:memberchk(Export, Exports)
86 ; memberchk:member(Export, Exports)
87 ).
88
89%% open_source(+Source, -In:stream) is semidet.
90%
91% Open a source location.
92
93open_source(File, In) :-
94 exists_source(File, Path),
95 open(Path, read, In),
96 ( peek_char(In, #)
97 -> skip(In, 10)
98 ; skip
99 ).
100
101exports(In, Exports) :-
102 read(In, Term),
103 Term = (:- module(_Name, Exports)).
catch( : Goal,+ Exception,+ Action)
current_predicate( F )
module(+ M:atom,+ L:list )
open(+ F,+ M,- S)
read(+ Stream, -Term )
peek_char(+_S_, - C)
skip(+ S,- C)
eraseall(+ K)
load_files(+_Files_, +_Options_)
ground( T)
atom( T)
var( T)
member(?Element, ?Set) is true when Set is a list, and Element occurs in it
memberchk(+ Element, + Set)