File: source.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (308 lines) | stat: -rw-r--r-- 9,796 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2019-2020, VU University Amsterdam
                              CWI, Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(xsb_source, []).
:- autoload(library(apply),  [convlist/3,partition/4]).
:- autoload(library(debug),  [debug/3]).
:- autoload(library(error),  [instantiation_error/1]).
:- autoload(library(occurs), [sub_term/2]).

/** <module> Support XSB source .P files

This module is a lightweight module that  allows loading .P files as XSB
source  files.  This   module   is   intended    to   be   loaded   from
``<config>/init.pl``, providing transparent usage  of   XSB  files  with
neglectable impact impact if no XSB sources are used.
*/

% xsb_max_file_size is used for buffering  the   source  in  memory when
% reading a source file through the   gpp preprocessor. Eventually, this
% should probably create an intermediate file.

:- create_prolog_flag(xsb_max_file_size, 100 000 000,
                      [ keep(true)
                      ]).

:- multifile
    user:prolog_file_type/2,
    user:term_expansion/2.

user:prolog_file_type('P', prolog).

user:term_expansion(begin_of_file, Out) :-
    prolog_load_context(file, File),
    file_name_extension(Path, 'P', File),
    include_options(File, Include),
    compiler_options(COptions),
    '$append'(Include, COptions, Extra),
    xsb_directives(File, Directives),
    directive_exports(Directives, Public, Directives1),
    (   Public == []
    ->  Out = Out1
    ;   file_base_name(Path, Module),
        Out = [ (:- module(Module, Public))
              | Out1
              ]
    ),
    Out1 = [ (:- expects_dialect(xsb)),
             (:- use_module(library(tables)))
           | Out2
           ],
    '$append'(Extra, More, Out2),
    (   nonvar(Module)
    ->  setup_call_cleanup(
            '$set_source_module'(OldM, Module),
            phrase(head_directives(Directives1, File), More),
            '$set_source_module'(OldM))
    ;   phrase(head_directives(Directives1, File), More)
    ),
    debug(xsb(header), '~p: directives: ~p', [File, More]).

include_options(File, Option) :-
    (   xsb_header_file(File, FileH)
    ->  Option = [(:- include(FileH))]
    ;   Option = []
    ).

:- multifile xsb:xsb_compiler_option/1.
:- dynamic   xsb:xsb_compiler_option/1.

compiler_options(Directives) :-
    findall(D, mapped_xsb_option(D), Directives).

mapped_xsb_option((:- D)) :-
    xsb:xsb_compiler_option(O),
    map_compiler_option(O, D).

map_compiler_option(singleton_warnings_off, style_check(-singleton)).
map_compiler_option(optimize,               set_prolog_flag(optimise, true)).

xsb_header_file(File, FileH) :-
    file_name_extension(Base, _, File),
    file_name_extension(Base, 'H', FileH),
    exists_file(FileH).

%!  directive_exports(+AllDirectives, -Public, -OtherDirectives)

directive_exports(AllDirectives, Exports, RestDirectives) :-
    partition(is_export, AllDirectives, ExportDirectives, RestDirectives),
    phrase(exports(ExportDirectives), Exports).

is_export(export(_)).

exports([]) -->
    [].
exports([export(H)|T]) -->
    export_decl(H),
    exports(T).

export_decl(Var) -->
    { var(Var),
      !,
      instantiation_error(Var)
    }.
export_decl((A,B)) -->
    !,
    export_decl(A),
    export_decl(B).
export_decl(PI) -->
    [PI].

%!  head_directives(+Directives, +File)// is det.
%!  head_directives_s(+Directives, +State)// is det.

head_directives(Directives, File) -->
    { current_prolog_flag(max_table_subgoal_size_action, Action),
      (   current_prolog_flag(max_table_subgoal_size, Size)
      ->  true
      ;   Size = -1
      )
    },
    head_directives_s(Directives,
                      #{file: File,
                        max_table_subgoal_size_action: Action,
                        max_table_subgoal_size:Size
                       }).


head_directives_s([], _) --> [].
head_directives_s([H|T], State0) -->
    { update_state(H, State0, State) },
    !,
    head_directives_s(T, State).
head_directives_s([H|T], State) -->
    head_directive(H, State),
    head_directives_s(T, State).

update_state(set_prolog_flag(max_table_subgoal_size_action, Action),
             State0, State) :-
    State = State0.put(max_table_subgoal_size_action, Action).
update_state(set_prolog_flag(max_table_subgoal_size, Size),
             State0, State) :-
    State = State0.put(max_table_subgoal_size, Size).

%!  head_directive(+Directive, +State)// is det.

head_directive(import(from(Preds, From)), State) -->
    !,
    { assertz(xsb:moved_directive(State.file, import(from(Preds, From))))
    },
    [ (:- xsb_import(Preds, From)) ].
head_directive(table(Preds as XSBOptions), State) -->
    !,
    { ignored_table_options(XSBOptions, Options),
      table_clauses(Preds, Options, Clauses, State),
      assertz(xsb:moved_directive(State.file, table(Preds as XSBOptions)))
    },
    seq(Clauses).
head_directive(table(Preds), State) -->
    !,
    { table_clauses(Preds, true, Clauses, State),
      assertz(xsb:moved_directive(State.file, table(Preds)))
    },
    seq(Clauses).
head_directive(_, _) -->
    [].

seq([]) --> [].
seq([H|T]) --> [H], seq(T).

ignored_table_options((A0,B0), Conj) :-
    !,
    ignored_table_options(A0, A),
    ignored_table_options(B0, B),
    mkconj(A, B, Conj).
ignored_table_options(Option, Option) :-
    supported_table_option(Option),
    !.
ignored_table_options(opaque, true) :-
    !.
ignored_table_options(Option, true) :-
    print_message(warning, xsb(table_option_ignored(Option))).

supported_table_option(variant).
supported_table_option(subsumptive).
supported_table_option(incremental).
supported_table_option(shared).
supported_table_option(private).
supported_table_option(max_answers(_)).
supported_table_option(subgoal_abstract(_)).
supported_table_option(answer_abstract(_)).

mkconj(true, X, X) :- !.
mkconj(X, true, X) :- !.
mkconj(X, Y, (X,Y)) :- !.

table_clauses(Preds, Options0, Clauses, State) :-
    add_defaults(Options0, Options, State),
    (   Options == true
    ->  expand_term((:- table(Preds)), Clauses)
    ;   expand_term((:- table(Preds as Options)), Clauses)
    ).

add_defaults(Opts, Opts, _) :-
    sub_term(subgoal_abstract(_), Opts),
    !.
add_defaults(Opts0, Opts, State) :-
    #{max_table_subgoal_size_action:abstract,
      max_table_subgoal_size:Size} :< State,
    Size >= 0,
    !,
    mkconj(Opts0, subgoal_abstract(Size), Opts).
add_defaults(Opts, Opts, _).

%!  xsb_directives(+File, -Directives) is semidet.
%
%   Directives is a list of all directives in File and its header.
%
%   @bug: track :- op/3 declarations to update the syntax.

xsb_directives(File, Directives) :-
    setup_call_cleanup(
        '$push_input_context'(xsb_directives),
        xsb_directives_aux(File, Directives),
        '$pop_input_context').

xsb_directives_aux(File, Directives) :-
    xsb_header_file(File, FileH),
    !,
    setup_call_cleanup(
        open(FileH, read, In),
        findall(D, stream_directive(In, D), Directives, PDirectives),
        close(In)),
    xsb_P_directives(PDirectives).
xsb_directives_aux(_File, Directives) :-
    xsb_P_directives(Directives).

xsb_P_directives(Directives) :-
    prolog_load_context(stream, In),
    stream_property(In, reposition(true)),
    !,
    setup_call_cleanup(
        stream_property(In, position(Pos)),
        findall(PI, stream_directive(In, PI), Directives),
        set_stream_position(In, Pos)).
xsb_P_directives(Directives) :-
    prolog_load_context(stream, In),
    current_prolog_flag(xsb_max_file_size, MaxSize),
    peek_string(In, MaxSize, String),
    setup_call_cleanup(
        open_string(String, In2),
        findall(PI, stream_directive(In2, PI), Directives),
        close(In2)).

stream_directive(In, Directive) :-
    repeat,
        read_term(In, Term,
                  [ syntax_errors(quiet),
                    module(xsb_source)
                  ]),
        (   Term == end_of_file
        ->  !, fail
        ;   Term = (:- Directive),
            nonvar(Directive)
        ;   fail
        ).

% define the typical XSB operators to limit syntax errors while
% scanning for :- export(_).
:- op(1050,  fy, import).
:- op(1100,  fx, export).
:- op(1100,  fx, mode).
:- op(1040, xfx, from).
:- op(1100,  fy, index).
:- op(1100,  fy, ti).
:- op(1045, xfx, as).
:- op(900,   fy, tnot).