35
36:- module(xsb,
37 [ add_lib_dir/1, 38 add_lib_dir/2, 39
40 compile/2, 41 load_dyn/1, 42 load_dyn/2, 43 load_dync/1, 44 load_dync/2, 45
46 set_global_compiler_options/1, 47 compiler_options/1, 48
49 xsb_import/2, 50 xsb_set_prolog_flag/2, 51
52 fail_if/1, 53
54 sk_not/1, 55 gc_tables/1, 56
57 cputime/1, 58 walltime/1, 59
60 (thread_shared)/1, 61
62 debug_ctl/2, 63
64 fmt_write/2, 65 fmt_write/3, 66
67 path_sysop/2, 68 path_sysop/3, 69
70 abort/1, 71
72 op(1050, fy, import),
73 op(1050, fx, export),
74 op(1040, xfx, from),
75 op(1100, fy, index), 76 op(1100, fy, ti), 77 op(1100, fx, mode),
78 op(1045, xfx, as),
79 op(900, fy, tnot),
80 op(900, fy, not), 81 op(1100, fx, thread_shared)
82 ]). 83:- use_module(library(error)). 84:- use_module(library(debug)). 85:- use_module(library(dialect/xsb/source)). 86:- use_module(library(dialect/xsb/consult)). 87:- use_module(library(tables)). 88:- use_module(library(aggregate)). 89:- use_module(library(option)). 90:- use_module(library(apply)). 91:- use_module(library(prolog_code), [comma_list/2]). 92:- if(exists_source(library(dialect/xsb/timed_call))). 93:- use_module(library(dialect/xsb/timed_call)). 94:- export(timed_call/2). 95:- endif. 96
102
103:- meta_predicate
104 xsb_import(:, +), 105
106 compile(:, +), 107 load_dyn(:),
108 load_dyn(:, +),
109 load_dync(:),
110 load_dync(:, +),
111
112 thread_shared(:),
113
114 fail_if(0), 115 sk_not(0). 116
117
118
119 122
127
128push_xsb_library :-
129 ( absolute_file_name(library(dialect/xsb), Dir,
130 [ file_type(directory),
131 access(read),
132 solutions(all),
133 file_errors(fail)
134 ]),
135 asserta((user:file_search_path(library, Dir) :-
136 prolog_load_context(dialect, xsb))),
137 fail
138 ; true
139 ).
140
141:- push_xsb_library. 142
146
147:- public setup_dialect/0. 148
149setup_dialect :-
150 style_check(-discontiguous).
151
152:- multifile
153 user:term_expansion/2,
154 user:goal_expansion/2. 155
156:- dynamic
157 moved_directive/2. 158
160
161user:term_expansion(In, Out) :-
162 prolog_load_context(dialect, xsb),
163 xsb_term_expansion(In, Out).
164
165xsb_term_expansion((:- Directive), []) :-
166 prolog_load_context(file, File),
167 retract(moved_directive(File, Directive)),
168 debug(xsb(header), 'Moved to head: ~p', [Directive]),
169 !.
170xsb_term_expansion((:- import Preds from From),
171 (:- xsb_import(Preds, From))).
172xsb_term_expansion((:- index(_PI, _, _)), []). 173xsb_term_expansion((:- index(_PI, _How)), []).
174xsb_term_expansion((:- index(_PI)), []).
175xsb_term_expansion((:- ti(_PI)), []).
176xsb_term_expansion((:- mode(Modes)), Clauses) :-
177 phrase(mode_specs(Modes), Specs),
178 ( Specs == []
179 -> Clauses = []
180 ; comma_list(Spec, Specs),
181 Clauses = [(:- mode(Spec))]
182 ).
183
189
190mode_specs(Var) -->
191 { var(Var) },
192 !.
193mode_specs((A,B)) -->
194 !,
195 mode_specs(A),
196 mode_specs(B).
197mode_specs(Spec) -->
198 { compound(Spec),
199 compound_name_arguments(Spec, _, Args),
200 maplist(swi_mode, Args)
201 },
202 !,
203 [Spec].
204mode_specs(_) -->
205 [].
206
207swi_mode(Arg) :-
208 atom(Arg),
209 memberchk(Arg, [+,-,?]).
210
211user:goal_expansion(In, Out) :-
212 prolog_load_context(dialect, xsb),
213 ( xsb_mapped_predicate(In, Out)
214 -> true
215 ; xsb_inlined_goal(In, Out)
216 ).
217
218xsb_mapped_predicate(expand_file_name(File, Expanded),
219 xsb_expand_file_name(File, Expanded)).
220xsb_mapped_predicate(set_prolog_flag(Flag, Value),
221 xsb_set_prolog_flag(Flag, Value)).
222xsb_mapped_predicate(abolish_module_tables(UserMod),
223 abolish_module_tables(user)) :-
224 UserMod == usermod.
225
226xsb_inlined_goal(fail_if(P), \+(P)).
227
232
233:- dynamic
234 mapped__module/2. 235
236xsb_import(Into:Preds, From) :-
237 mapped__module(From, Mapped),
238 !,
239 xsb_import(Preds, Into, Mapped).
240xsb_import(Into:Preds, From) :-
241 xsb_import(Preds, Into, From).
242
243xsb_import(Var, _Into, _From) :-
244 var(Var),
245 !,
246 instantiation_error(Var).
247xsb_import((A,B), Into, From) :-
248 !,
249 xsb_import(A, Into, From),
250 xsb_import(B, Into, From).
251xsb_import(Name/Arity, Into, From) :-
252 functor(Head, Name, Arity),
253 xsb_mapped_predicate(Head, NewHead),
254 functor(NewHead, NewName, Arity),
255 !,
256 xsb_import(NewName/Arity, Into, From).
257xsb_import(PI, Into, usermod) :-
258 !,
259 export(user:PI),
260 @(import(user:PI), Into).
261xsb_import(Name/Arity, Into, _From) :-
262 functor(Head, Name, Arity),
263 predicate_property(Into:Head, iso),
264 !,
265 debug(xsb(import), '~p: already visible (ISO)', [Into:Name/Arity]).
266xsb_import(PI, Into, From) :-
267 import_from_module(clean, PI, Into, From),
268 !.
269xsb_import(PI, Into, From) :-
270 prolog_load_context(file, Here),
271 absolute_file_name(From, Path,
272 [ extensions(['P', pl, prolog]),
273 access(read),
274 relative_to(Here),
275 file_errors(fail)
276 ]),
277 !,
278 debug(xsb(import), '~p: importing from ~p', [Into:PI, Path]),
279 load_module(Into:Path, PI).
280xsb_import(PI, Into, From) :-
281 absolute_file_name(library(From), Path,
282 [ extensions(['P', pl, prolog]),
283 access(read),
284 file_errors(fail)
285 ]),
286 !,
287 debug(xsb(import), '~p: importing from ~p', [Into:PI, Path]),
288 load_module(Into:Path, PI).
289xsb_import(Name/Arity, Into, _From) :-
290 functor(Head, Name, Arity),
291 predicate_property(Into:Head, visible),
292 !,
293 debug(xsb(import), '~p: already visible', [Into:Name/Arity]).
294xsb_import(PI, Into, From) :-
295 import_from_module(dirty, PI, Into, From),
296 !.
297xsb_import(_Name/_Arity, _Into, From) :-
298 existence_error(xsb_module, From).
299
305
306import_from_module(clean, PI, Into, From) :-
307 module_property(From, exports(List)),
308 memberchk(PI, List),
309 !,
310 debug(xsb(import), '~p: importing from module ~p', [Into:PI, From]),
311 @(import(From:PI), Into).
312import_from_module(dirty, PI, Into, From) :-
313 current_predicate(From:PI),
314 !,
315 debug(xsb(import), '~p: importing from module ~p', [Into:PI, From]),
316 ( check_exported(From, PI)
317 -> @(import(From:PI), Into)
318 ; true
319 ).
320import_from_module(dirty, PI, _Into, From) :-
321 module_property(From, file(File)),
322 !,
323 print_message(error, xsb(not_in_module(File, From, PI))).
324
325check_exported(Module, PI) :-
326 module_property(Module, exports(List)),
327 memberchk(PI, List),
328 !.
329check_exported(Module, PI) :-
330 module_property(Module, file(File)),
331 print_message(error, xsb(not_in_module(File, Module, PI))).
332
333load_module(Into:Path, PI) :-
334 use_module(Into:Path, []),
335 ( module_property(Module, file(Path))
336 -> file_base_name(Path, File),
337 file_name_extension(Base, _, File),
338 ( Base == Module
339 -> true
340 ; atom_concat(xsb_, Base, Module)
341 -> map_module(Base, Module)
342 ; print_message(warning,
343 xsb(file_loaded_into_mismatched_module(Path, Module))),
344 map_module(Base, Module)
345 )
346 ; print_message(warning, xsb(loaded_unknown_module(Path)))
347 ),
348 import_from_module(_, PI, Into, Module).
349
350map_module(XSB, Module) :-
351 mapped__module(XSB, Module),
352 !.
353map_module(XSB, Module) :-
354 assertz(mapped__module(XSB, Module)).
355
356
360
361xsb_set_prolog_flag(unify_with_occurs_check, XSBVal) :-
362 !,
363 map_bool(XSBVal, Val),
364 set_prolog_flag(occurs_check, Val).
365xsb_set_prolog_flag(Flag, Value) :-
366 set_prolog_flag(Flag, Value).
367
368map_bool(on, true).
369map_bool(off, false).
370
371
372 375
380
381compile(File, _Options) :-
382 qcompile(File).
383
395
396load_dyn(File) :-
397 '$style_check'(Style, Style),
398 setup_call_cleanup(
399 style_check(-singleton),
400 load_files(File),
401 '$style_check'(_, Style)).
402
403load_dyn(File, Dir) :- must_be(oneof([z]), Dir), load_dyn(File).
404load_dync(File) :- load_dyn(File).
405load_dync(File, Dir) :- load_dyn(File, Dir).
406
410
411:- multifile xsb_compiler_option/1. 412:- dynamic xsb_compiler_option/1. 413
414set_global_compiler_options(List) :-
415 must_be(list, List),
416 maplist(set_global_compiler_option, List).
417
418set_global_compiler_option(+Option) :-
419 !,
420 valid_compiler_option(Option),
421 ( xsb_compiler_option(Option)
422 -> true
423 ; assertz(xsb_compiler_option(Option))
424 ).
425set_global_compiler_option(-Option) :-
426 !,
427 valid_compiler_option(Option),
428 retractall(xsb_compiler_option(Option)).
429set_global_compiler_option(-Option) :-
430 valid_compiler_option(Option),
431 ( xsb_compiler_option(Option)
432 -> true
433 ; assertz(xsb_compiler_option(Option))
434 ).
435
436valid_compiler_option(Option) :-
437 must_be(oneof([ singleton_warnings_off,
438 optimize,
439 allow_redefinition,
440 xpp_on,
441 spec_off
442 ]), Option).
443
447
448compiler_options(Options) :-
449 must_be(list, Options),
450 maplist(compiler_option, Options).
451
452compiler_option(+Option) :-
453 !,
454 valid_compiler_option(Option),
455 set_compiler_option(Option).
456compiler_option(-Option) :-
457 !,
458 valid_compiler_option(Option),
459 clear_compiler_option(Option).
460compiler_option(Option) :-
461 valid_compiler_option(Option),
462 set_compiler_option(Option).
463
464set_compiler_option(singleton_warnings_off) :-
465 style_check(-singleton).
466set_compiler_option(optimize) :-
467 set_prolog_flag(optimise, true).
468set_compiler_option(allow_redefinition).
469set_compiler_option(xpp_on).
470set_compiler_option(spec_off).
471
472clear_compiler_option(singleton_warnings_off) :-
473 style_check(+singleton).
474clear_compiler_option(optimize) :-
475 set_prolog_flag(optimise, false).
476clear_compiler_option(allow_redefinition).
477clear_compiler_option(xpp_on).
478
479 482
487
488fail_if(P) :-
489 \+ P.
490
491 494
500
501sk_not(P) :-
502 not_exists(P).
503
513
514gc_tables(Remaining) :-
515 garbage_collect_atoms,
516 aggregate_all(count, remaining_table(_), Remaining).
517
518remaining_table(Trie) :-
519 current_blob(Trie, trie),
520 '$is_answer_trie'(Trie, _Type),
521 '$atom_references'(Trie, 0).
522
526
527cputime(Seconds) :-
528 statistics(cputime, Seconds).
529
533
534walltime(Seconds) :-
535 get_time(Now),
536 statistics(epoch, Epoch),
537 Seconds is Now - Epoch.
538
543
544debug_ctl(prompt, off) :-
545 !,
546 leash(-all).
547debug_ctl(prompt, on) :-
548 !,
549 leash(+full).
550debug_ctl(hide, Preds) :-
551 !,
552 '$hide'(Preds).
553debug_ctl(Option, Value) :-
554 debug(xsb(compat), 'XSB: not implemented: ~p',
555 [ debug_ctl(Option, Value) ]).
556
561
562thread_shared(Spec) :-
563 dynamic(Spec).
564
565
574
575fmt_write(Fmt, Term) :-
576 fmt_write(current_output, Fmt, Term).
577
578fmt_write(Stream, Fmt, Term) :-
579 ( compound(Term)
580 -> Term =.. [_|Args]
581 ; Args = [Term]
582 ),
583 fmt_write_format(Fmt, Format),
584 format(Stream, Format, Args).
585
586:- dynamic
587 fmt_write_cache/2. 588
589fmt_write_format(Fmt, Format) :-
590 fmt_write_cache(Fmt, Format),
591 !.
592fmt_write_format(Fmt, Format) :-
593 string_codes(Fmt, FmtCodes),
594 phrase(format_fmt(Codes, []), FmtCodes),
595 atom_codes(Format, Codes),
596 asserta(fmt_write_cache(Fmt, Format)).
597
598format_fmt(Format, Tail) -->
599 "%",
600 ( format_esc(Format, Tail0)
601 -> !
602 ; here(Rest),
603 { print_message(warning, xsb(fmt_write(ignored(Rest)))),
604 fail
605 }
606 ),
607 format_fmt(Tail0, Tail).
608format_fmt([0'~,0'~|T0], T) -->
609 "~",
610 !,
611 format_fmt(T0, T).
612format_fmt([H|T0], T) -->
613 [H],
614 !,
615 format_fmt(T0, T).
616format_fmt(T, T) --> [].
617
618format_esc(Fmt, Tail) -->
619 format_esc(Fmt0),
620 !,
621 { append(Fmt0, Tail, Fmt)
622 }.
623
624format_esc(`~16r`) --> "x".
625format_esc(`~d`) --> "d".
626format_esc(`~f`) --> "f".
627format_esc(`~s`) --> "s".
628format_esc(`%`) --> "%".
629
630here(Rest, Rest, Rest).
631
641
642path_sysop(isplain, File) :-
643 exists_file(File).
644path_sysop(isdir, Dir) :-
645 exists_directory(Dir).
646path_sysop(rm, File) :-
647 delete_file(File).
648path_sysop(rmdir, Dir) :-
649 delete_directory(Dir).
650path_sysop(rmdir_rec, Dir) :-
651 delete_directory_and_contents(Dir).
652path_sysop(cwd, CWD) :-
653 working_directory(CWD, CWD).
654path_sysop(chdir, CWD) :-
655 working_directory(_, CWD).
656path_sysop(mkdir, Dir) :-
657 make_directory(Dir).
658path_sysop(exists, Entry) :-
659 access_file(Entry, exist).
660path_sysop(readable, Entry) :-
661 access_file(Entry, read).
662path_sysop(writable, Entry) :-
663 access_file(Entry, write).
664path_sysop(executable, Entry) :-
665 access_file(Entry, execute).
666path_sysop(tmpfilename, Name) :-
667 tmp_file(swi, Name).
668path_sysop(isabsolute, Name) :-
669 is_absolute_file_name(Name).
670
671
672path_sysop(rename, Old, New) :-
673 rename_file(Old, New).
674path_sysop(copy, From, To) :-
675 copy_file(From, To).
676path_sysop(link, From, To) :-
677 link_file(From, To, symbolic).
678path_sysop(modtime, Path, Time) :-
679 time_file(Path, Time).
680path_sysop(newerthan, Path1, Path2) :-
681 time_file(Path1, Time1),
682 ( catch(time_file(Path2, Time2), error(existence_error(_,_),_), fail)
683 -> Time1 > Time2
684 ; true
685 ).
686path_sysop(size, Path, Size) :-
687 size_file(Path, Size).
688path_sysop(extension, Path, Ext) :-
689 file_name_extension(_, Ext, Path).
690path_sysop(basename, Path, Base) :-
691 file_base_name(Path, File),
692 file_name_extension(Base, _, File).
693path_sysop(dirname, Path, Dir) :-
694 file_directory_name(Path, Dir0),
695 ( sub_atom(Dir0, _, _, 0, /)
696 -> Dir = Dir0
697 ; atom_concat(Dir0, /, Dir)
698 ).
699path_sysop(expand, Name, Path) :-
700 absolute_file_name(Name, Path).
701
705
706abort(Message) :-
707 print_message(error, aborted(Message)),
708 abort.
709
710 713
714:- multifile
715 prolog:message//1. 716
717prolog:message(xsb(not_in_module(File, Module, PI))) -->
718 [ 'XSB: ~p, implementing ~p does not export ~p'-[File, Module, PI] ].
719prolog:message(xsb(file_loaded_into_mismatched_module(File, Module))) -->
720 [ 'XSB: File ~p defines module ~p'-[File, Module] ].
721prolog:message(xsb(ignored(debug_ctl(Option, Value)))) -->
722 [ 'XSB: debug_ctl(~p,~p) is not implemented'-[Option,Value] ].
723prolog:message(xsb(table_option_ignored(Option))) -->
724 [ 'XSB: table option ~p is ignored'-[Option] ]