35
36:- module('$syspreds',
37 [ leash/1,
38 visible/1,
39 style_check/1,
40 (spy)/1,
41 (nospy)/1,
42 trace/1,
43 trace/2,
44 nospyall/0,
45 debugging/0,
46 rational/3,
47 flag/3,
48 atom_prefix/2,
49 dwim_match/2,
50 source_file_property/2,
51 source_file/1,
52 source_file/2,
53 unload_file/1,
54 prolog_load_context/2,
55 stream_position_data/3,
56 current_predicate/2,
57 '$defined_predicate'/1,
58 predicate_property/2,
59 '$predicate_property'/2,
60 clause_property/2,
61 current_module/1, 62 module_property/2, 63 module/1, 64 current_trie/1, 65 trie_property/2, 66 working_directory/2, 67 shell/1, 68 on_signal/3,
69 current_signal/3,
70 open_shared_object/2,
71 open_shared_object/3,
72 format/1,
73 garbage_collect/0,
74 set_prolog_stack/2,
75 prolog_stack_property/2,
76 absolute_file_name/2,
77 require/1,
78 call_with_depth_limit/3, 79 call_with_inference_limit/3, 80 numbervars/3, 81 term_string/3, 82 nb_setval/2 83 ]).
84
85 88
90
91:- meta_predicate
92 map_bits(2, +, +, -).
93
94map_bits(_, Var, _, _) :-
95 var(Var),
96 !,
97 '$instantiation_error'(Var).
98map_bits(_, [], Bits, Bits) :- !.
99map_bits(Pred, [H|T], Old, New) :-
100 map_bits(Pred, H, Old, New0),
101 map_bits(Pred, T, New0, New).
102map_bits(Pred, +Name, Old, New) :- 103 !,
104 bit(Pred, Name, Bits),
105 !,
106 New is Old \/ Bits.
107map_bits(Pred, -Name, Old, New) :- 108 !,
109 bit(Pred, Name, Bits),
110 !,
111 New is Old /\ (\Bits).
112map_bits(Pred, ?(Name), Old, Old) :- 113 !,
114 bit(Pred, Name, Bits),
115 Old /\ Bits > 0.
116map_bits(_, Term, _, _) :-
117 '$type_error'('+|-|?(Flag)', Term).
118
119bit(Pred, Name, Bits) :-
120 call(Pred, Name, Bits),
121 !.
122bit(_:Pred, Name, _) :-
123 '$domain_error'(Pred, Name).
124
125:- public port_name/2. 126
127port_name( call, 2'000000001).
128port_name( exit, 2'000000010).
129port_name( fail, 2'000000100).
130port_name( redo, 2'000001000).
131port_name( unify, 2'000010000).
132port_name( break, 2'000100000).
133port_name( cut_call, 2'001000000).
134port_name( cut_exit, 2'010000000).
135port_name( exception, 2'100000000).
136port_name( cut, 2'011000000).
137port_name( all, 2'000111111).
138port_name( full, 2'000101111).
139port_name( half, 2'000101101). 140
141leash(Ports) :-
142 '$leash'(Old, Old),
143 map_bits(port_name, Ports, Old, New),
144 '$leash'(_, New).
145
146visible(Ports) :-
147 '$visible'(Old, Old),
148 map_bits(port_name, Ports, Old, New),
149 '$visible'(_, New).
150
151style_name(atom, 0x0001) :-
152 print_message(warning, decl_no_effect(style_check(atom))).
153style_name(singleton, 0x0042). 154style_name(discontiguous, 0x0008).
155style_name(charset, 0x0020).
156style_name(no_effect, 0x0080).
157style_name(var_branches, 0x0100).
158
160
161style_check(Var) :-
162 var(Var),
163 !,
164 '$instantiation_error'(Var).
165style_check(?(Style)) :-
166 !,
167 ( var(Style)
168 -> enum_style_check(Style)
169 ; enum_style_check(Style)
170 -> true
171 ).
172style_check(Spec) :-
173 '$style_check'(Old, Old),
174 map_bits(style_name, Spec, Old, New),
175 '$style_check'(_, New).
176
177enum_style_check(Style) :-
178 '$style_check'(Bits, Bits),
179 style_name(Style, Bit),
180 Bit /\ Bits =\= 0.
181
182
190
191:- multifile
192 prolog:debug_control_hook/1. 193
199
200:- meta_predicate
201 trace(:),
202 trace(:, +).
203
204trace(Preds) :-
205 trace(Preds, +all).
206
207trace(_:X, _) :-
208 var(X),
209 !,
210 throw(error(instantiation_error, _)).
211trace(_:[], _) :- !.
212trace(M:[H|T], Ps) :-
213 !,
214 trace(M:H, Ps),
215 trace(M:T, Ps).
216trace(Pred, Ports) :-
217 '$find_predicate'(Pred, Preds),
218 Preds \== [],
219 set_prolog_flag(debug, true),
220 ( '$member'(PI, Preds),
221 pi_to_head(PI, Head),
222 ( Head = _:_
223 -> QHead0 = Head
224 ; QHead0 = user:Head
225 ),
226 '$define_predicate'(QHead0),
227 ( predicate_property(QHead0, imported_from(M))
228 -> QHead0 = _:Plain,
229 QHead = M:Plain
230 ; QHead = QHead0
231 ),
232 '$trace'(Ports, QHead),
233 trace_ports(QHead, Tracing),
234 print_message(informational, trace(QHead, Tracing)),
235 fail
236 ; true
237 ).
238
239trace_alias(all, [trace_call, trace_redo, trace_exit, trace_fail]).
240trace_alias(call, [trace_call]).
241trace_alias(redo, [trace_redo]).
242trace_alias(exit, [trace_exit]).
243trace_alias(fail, [trace_fail]).
244
245'$trace'([], _) :- !.
246'$trace'([H|T], Head) :-
247 !,
248 '$trace'(H, Head),
249 '$trace'(T, Head).
250'$trace'(+H, Head) :-
251 trace_alias(H, A0),
252 !,
253 tag_list(A0, +, A1),
254 '$trace'(A1, Head).
255'$trace'(+H, Head) :-
256 !,
257 trace_alias(_, [H]),
258 '$set_predicate_attribute'(Head, H, true).
259'$trace'(-H, Head) :-
260 trace_alias(H, A0),
261 !,
262 tag_list(A0, -, A1),
263 '$trace'(A1, Head).
264'$trace'(-H, Head) :-
265 !,
266 trace_alias(_, [H]),
267 '$set_predicate_attribute'(Head, H, false).
268'$trace'(H, Head) :-
269 atom(H),
270 '$trace'(+H, Head).
271
272tag_list([], _, []).
273tag_list([H0|T0], F, [H1|T1]) :-
274 H1 =.. [F, H0],
275 tag_list(T0, F, T1).
276
277:- meta_predicate
278 spy(:),
279 nospy(:).
280
295
296spy(_:X) :-
297 var(X),
298 throw(error(instantiation_error, _)).
299spy(_:[]) :- !.
300spy(M:[H|T]) :-
301 !,
302 spy(M:H),
303 spy(M:T).
304spy(Spec) :-
305 notrace(prolog:debug_control_hook(spy(Spec))),
306 !.
307spy(Spec) :-
308 '$find_predicate'(Spec, Preds),
309 '$member'(PI, Preds),
310 pi_to_head(PI, Head),
311 '$define_predicate'(Head),
312 '$spy'(Head),
313 fail.
314spy(_).
315
316nospy(_:X) :-
317 var(X),
318 throw(error(instantiation_error, _)).
319nospy(_:[]) :- !.
320nospy(M:[H|T]) :-
321 !,
322 nospy(M:H),
323 nospy(M:T).
324nospy(Spec) :-
325 notrace(prolog:debug_control_hook(nospy(Spec))),
326 !.
327nospy(Spec) :-
328 '$find_predicate'(Spec, Preds),
329 '$member'(PI, Preds),
330 pi_to_head(PI, Head),
331 '$nospy'(Head),
332 fail.
333nospy(_).
334
335nospyall :-
336 notrace(prolog:debug_control_hook(nospyall)),
337 fail.
338nospyall :-
339 spy_point(Head),
340 '$nospy'(Head),
341 fail.
342nospyall.
343
344pi_to_head(M:PI, M:Head) :-
345 !,
346 pi_to_head(PI, Head).
347pi_to_head(Name/Arity, Head) :-
348 functor(Head, Name, Arity).
349
353
354debugging :-
355 notrace(prolog:debug_control_hook(debugging)),
356 !.
357debugging :-
358 current_prolog_flag(debug, true),
359 !,
360 print_message(informational, debugging(on)),
361 findall(H, spy_point(H), SpyPoints),
362 print_message(informational, spying(SpyPoints)),
363 findall(trace(H,P), trace_point(H,P), TracePoints),
364 print_message(informational, tracing(TracePoints)).
365debugging :-
366 print_message(informational, debugging(off)).
367
368spy_point(Module:Head) :-
369 current_predicate(_, Module:Head),
370 '$get_predicate_attribute'(Module:Head, spy, 1),
371 \+ predicate_property(Module:Head, imported_from(_)).
372
373trace_point(Module:Head, Ports) :-
374 current_predicate(_, Module:Head),
375 '$get_predicate_attribute'(Module:Head, trace_any, 1),
376 \+ predicate_property(Module:Head, imported_from(_)),
377 trace_ports(Module:Head, Ports).
378
379trace_ports(Head, Ports) :-
380 findall(Port,
381 (trace_alias(Port, [AttName]),
382 '$get_predicate_attribute'(Head, AttName, 1)),
383 Ports).
384
385
390
391flag(Name, Old, New) :-
392 Old == New,
393 !,
394 get_flag(Name, Old).
395flag(Name, Old, New) :-
396 with_mutex('$flag', update_flag(Name, Old, New)).
397
398update_flag(Name, Old, New) :-
399 get_flag(Name, Old),
400 ( atom(New)
401 -> set_flag(Name, New)
402 ; Value is New,
403 set_flag(Name, Value)
404 ).
405
406
407 410
415
416rational(Rat, M, N) :-
417 rational(Rat),
418 ( Rat = rdiv(M, N)
419 -> true
420 ; integer(Rat)
421 -> M = Rat,
422 N = 1
423 ).
424
425
426 429
430dwim_match(A1, A2) :-
431 dwim_match(A1, A2, _).
432
433atom_prefix(Atom, Prefix) :-
434 sub_atom(Atom, 0, _, _, Prefix).
435
436
437 440
451
452source_file(File) :-
453 ( current_prolog_flag(access_level, user)
454 -> Level = user
455 ; true
456 ),
457 ( ground(File)
458 -> ( '$time_source_file'(File, Time, Level)
459 ; absolute_file_name(File, Abs),
460 '$time_source_file'(Abs, Time, Level)
461 ), !
462 ; '$time_source_file'(File, Time, Level)
463 ),
464 Time > 0.0.
465
470
471:- meta_predicate source_file(:, ?).
472
473source_file(M:Head, File) :-
474 nonvar(M), nonvar(Head),
475 !,
476 ( predicate_property(M:Head, multifile)
477 -> multi_source_files(M:Head, Files),
478 '$member'(File, Files)
479 ; '$source_file'(M:Head, File)
480 ).
481source_file(M:Head, File) :-
482 ( nonvar(File)
483 -> true
484 ; source_file(File)
485 ),
486 '$source_file_predicates'(File, Predicates),
487 '$member'(M:Head, Predicates).
488
489:- thread_local found_src_file/1.
490
491multi_source_files(Head, Files) :-
492 call_cleanup(
493 findall(File, multi_source_file(Head, File), Files),
494 retractall(found_src_file(_))).
495
496multi_source_file(Head, File) :-
497 nth_clause(Head, _, Clause),
498 clause_property(Clause, source(File)),
499 \+ found_src_file(File),
500 asserta(found_src_file(File)).
501
502
506
507source_file_property(File, P) :-
508 nonvar(File),
509 !,
510 canonical_source_file(File, Path),
511 property_source_file(P, Path).
512source_file_property(File, P) :-
513 property_source_file(P, File).
514
515property_source_file(modified(Time), File) :-
516 '$time_source_file'(File, Time, user).
517property_source_file(module(M), File) :-
518 ( nonvar(M)
519 -> '$current_module'(M, File)
520 ; nonvar(File)
521 -> '$current_module'(ML, File),
522 ( atom(ML)
523 -> M = ML
524 ; '$member'(M, ML)
525 )
526 ; '$current_module'(M, File)
527 ).
528property_source_file(load_context(Module, Location, Options), File) :-
529 '$time_source_file'(File, _, user),
530 clause(system:'$load_context_module'(File, Module, Options), true, Ref),
531 ( clause_property(Ref, file(FromFile)),
532 clause_property(Ref, line_count(FromLine))
533 -> Location = FromFile:FromLine
534 ; Location = user
535 ).
536property_source_file(includes(Master, Stamp), File) :-
537 system:'$included'(File, _Line, Master, Stamp).
538property_source_file(included_in(Master, Line), File) :-
539 system:'$included'(Master, Line, File, _).
540property_source_file(derived_from(DerivedFrom, Stamp), File) :-
541 system:'$derived_source'(File, DerivedFrom, Stamp).
542property_source_file(reloading, File) :-
543 source_file(File),
544 '$source_file_property'(File, reloading, true).
545property_source_file(load_count(Count), File) :-
546 source_file(File),
547 '$source_file_property'(File, load_count, Count).
548property_source_file(number_of_clauses(Count), File) :-
549 source_file(File),
550 '$source_file_property'(File, number_of_clauses, Count).
551
552
556
557canonical_source_file(Spec, File) :-
558 atom(Spec),
559 '$time_source_file'(Spec, _, _),
560 !,
561 File = Spec.
562canonical_source_file(Spec, File) :-
563 system:'$included'(_Master, _Line, Spec, _),
564 !,
565 File = Spec.
566canonical_source_file(Spec, File) :-
567 absolute_file_name(Spec,
568 [ file_type(prolog),
569 access(read),
570 file_errors(fail)
571 ],
572 File),
573 source_file(File).
574
575
581
582prolog_load_context(module, Module) :-
583 '$current_source_module'(Module).
584prolog_load_context(file, F) :-
585 source_location(F, _).
586prolog_load_context(source, F) :- 587 source_location(F0, _),
588 '$input_context'(Context),
589 '$top_file'(Context, F0, F).
590prolog_load_context(stream, S) :-
591 source_location(F, _),
592 ( system:'$load_input'(F, S0)
593 -> S = S0
594 ).
595prolog_load_context(directory, D) :-
596 source_location(F, _),
597 file_directory_name(F, D).
598prolog_load_context(dialect, D) :-
599 current_prolog_flag(emulated_dialect, D).
600prolog_load_context(term_position, TermPos) :-
601 source_location(_, L),
602 ( nb_current('$term_position', Pos),
603 compound(Pos), 604 stream_position_data(line_count, Pos, L)
605 -> TermPos = Pos
606 ; TermPos = '$stream_position'(0,L,0,0)
607 ).
608prolog_load_context(script, Bool) :-
609 ( '$toplevel':loaded_init_file(script, Path),
610 source_location(Path, _)
611 -> Bool = true
612 ; Bool = false
613 ).
614prolog_load_context(variable_names, Bindings) :-
615 nb_current('$variable_names', Bindings).
616prolog_load_context(term, Term) :-
617 nb_current('$term', Term).
618prolog_load_context(reloading, true) :-
619 prolog_load_context(source, F),
620 '$source_file_property'(F, reloading, true).
621
625
626unload_file(File) :-
627 ( canonical_source_file(File, Path)
628 -> '$unload_file'(Path)
629 ; true
630 ).
631
632
633 636
641
642stream_position_data(Prop, Term, Value) :-
643 nonvar(Prop),
644 !,
645 ( stream_position_field(Prop, Pos)
646 -> arg(Pos, Term, Value)
647 ; throw(error(domain_error(stream_position_data, Prop)))
648 ).
649stream_position_data(Prop, Term, Value) :-
650 stream_position_field(Prop, Pos),
651 arg(Pos, Term, Value).
652
653stream_position_field(char_count, 1).
654stream_position_field(line_count, 2).
655stream_position_field(line_position, 3).
656stream_position_field(byte_count, 4).
657
658
659 662
668
669:- meta_predicate
670 call_with_depth_limit(0, +, -).
671
672call_with_depth_limit(G, Limit, Result) :-
673 '$depth_limit'(Limit, OLimit, OReached),
674 ( catch(G, E, '$depth_limit_except'(OLimit, OReached, E)),
675 '$depth_limit_true'(Limit, OLimit, OReached, Result, Det),
676 ( Det == ! -> ! ; true )
677 ; '$depth_limit_false'(OLimit, OReached, Result)
678 ).
679
691
692:- meta_predicate
693 call_with_inference_limit(0, +, -).
694
695call_with_inference_limit(G, Limit, Result) :-
696 '$inference_limit'(Limit, OLimit),
697 ( catch(G, Except,
698 system:'$inference_limit_except'(OLimit, Except, Result0)),
699 system:'$inference_limit_true'(Limit, OLimit, Result0),
700 ( Result0 == ! -> ! ; true ),
701 Result = Result0
702 ; system:'$inference_limit_false'(OLimit)
703 ).
704
705
706 709
722
723
724:- meta_predicate
725 current_predicate(?, :),
726 '$defined_predicate'(:).
727
728current_predicate(Name, Module:Head) :-
729 (var(Module) ; var(Head)),
730 !,
731 generate_current_predicate(Name, Module, Head).
732current_predicate(Name, Term) :-
733 '$c_current_predicate'(Name, Term),
734 '$defined_predicate'(Term),
735 !.
736current_predicate(Name, Module:Head) :-
737 default_module(Module, DefModule),
738 '$c_current_predicate'(Name, DefModule:Head),
739 '$defined_predicate'(DefModule:Head),
740 !.
741current_predicate(Name, Module:Head) :-
742 current_prolog_flag(autoload, true),
743 \+ current_prolog_flag(Module:unknown, fail),
744 ( compound(Head)
745 -> compound_name_arity(Head, Name, Arity)
746 ; Name = Head, Arity = 0
747 ),
748 '$find_library'(Module, Name, Arity, _LoadModule, _Library),
749 !.
750
751generate_current_predicate(Name, Module, Head) :-
752 current_module(Module),
753 QHead = Module:Head,
754 '$c_current_predicate'(Name, QHead),
755 '$get_predicate_attribute'(QHead, defined, 1).
756
757'$defined_predicate'(Head) :-
758 '$get_predicate_attribute'(Head, defined, 1),
759 !.
760
764
765:- meta_predicate
766 predicate_property(:, ?).
767
768:- '$iso'(predicate_property/2).
769
770predicate_property(Pred, Property) :- 771 nonvar(Property),
772 !,
773 property_predicate(Property, Pred).
774predicate_property(Pred, Property) :- 775 define_or_generate(Pred),
776 '$predicate_property'(Property, Pred).
777
783
784property_predicate(undefined, Pred) :-
785 !,
786 Pred = Module:Head,
787 current_module(Module),
788 '$c_current_predicate'(_, Pred),
789 \+ '$defined_predicate'(Pred), 790 \+ current_predicate(_, Pred),
791 goal_name_arity(Head, Name, Arity),
792 \+ system_undefined(Module:Name/Arity).
793property_predicate(visible, Pred) :-
794 !,
795 visible_predicate(Pred).
796property_predicate(autoload(File), _:Head) :-
797 !,
798 current_prolog_flag(autoload, true),
799 ( callable(Head)
800 -> goal_name_arity(Head, Name, Arity),
801 ( '$find_library'(_, Name, Arity, _, File)
802 -> true
803 )
804 ; '$find_library'(_, Name, Arity, _, File),
805 functor(Head, Name, Arity)
806 ).
807property_predicate(implementation_module(IM), M:Head) :-
808 !,
809 atom(M),
810 ( default_module(M, DM),
811 '$get_predicate_attribute'(DM:Head, defined, 1)
812 -> ( '$get_predicate_attribute'(DM:Head, imported, ImportM)
813 -> IM = ImportM
814 ; IM = M
815 )
816 ; \+ current_prolog_flag(M:unknown, fail),
817 goal_name_arity(Head, Name, Arity),
818 '$find_library'(_, Name, Arity, LoadModule, _File)
819 -> IM = LoadModule
820 ; M = IM
821 ).
822property_predicate(Property, Pred) :-
823 define_or_generate(Pred),
824 '$predicate_property'(Property, Pred).
825
826goal_name_arity(Head, Name, Arity) :-
827 compound(Head),
828 !,
829 compound_name_arity(Head, Name, Arity).
830goal_name_arity(Head, Head, 0).
831
832
838
839define_or_generate(M:Head) :-
840 callable(Head),
841 atom(M),
842 '$get_predicate_attribute'(M:Head, defined, 1),
843 !.
844define_or_generate(M:Head) :-
845 callable(Head),
846 nonvar(M), M \== system,
847 !,
848 '$define_predicate'(M:Head).
849define_or_generate(Pred) :-
850 current_predicate(_, Pred),
851 '$define_predicate'(Pred).
852
853
854'$predicate_property'(interpreted, Pred) :-
855 '$get_predicate_attribute'(Pred, foreign, 0).
856'$predicate_property'(visible, Pred) :-
857 '$get_predicate_attribute'(Pred, defined, 1).
858'$predicate_property'(built_in, Pred) :-
859 '$get_predicate_attribute'(Pred, system, 1).
860'$predicate_property'(exported, Pred) :-
861 '$get_predicate_attribute'(Pred, exported, 1).
862'$predicate_property'(public, Pred) :-
863 '$get_predicate_attribute'(Pred, public, 1).
864'$predicate_property'(foreign, Pred) :-
865 '$get_predicate_attribute'(Pred, foreign, 1).
866'$predicate_property'((dynamic), Pred) :-
867 '$get_predicate_attribute'(Pred, (dynamic), 1).
868'$predicate_property'((static), Pred) :-
869 '$get_predicate_attribute'(Pred, (dynamic), 0).
870'$predicate_property'((volatile), Pred) :-
871 '$get_predicate_attribute'(Pred, (volatile), 1).
872'$predicate_property'((thread_local), Pred) :-
873 '$get_predicate_attribute'(Pred, (thread_local), 1).
874'$predicate_property'((multifile), Pred) :-
875 '$get_predicate_attribute'(Pred, (multifile), 1).
876'$predicate_property'(imported_from(Module), Pred) :-
877 '$get_predicate_attribute'(Pred, imported, Module).
878'$predicate_property'(transparent, Pred) :-
879 '$get_predicate_attribute'(Pred, transparent, 1).
880'$predicate_property'(meta_predicate(Pattern), Pred) :-
881 '$get_predicate_attribute'(Pred, meta_predicate, Pattern).
882'$predicate_property'(file(File), Pred) :-
883 '$get_predicate_attribute'(Pred, file, File).
884'$predicate_property'(line_count(LineNumber), Pred) :-
885 '$get_predicate_attribute'(Pred, line_count, LineNumber).
886'$predicate_property'(notrace, Pred) :-
887 '$get_predicate_attribute'(Pred, trace, 0).
888'$predicate_property'(nodebug, Pred) :-
889 '$get_predicate_attribute'(Pred, hide_childs, 1).
890'$predicate_property'(spying, Pred) :-
891 '$get_predicate_attribute'(Pred, spy, 1).
892'$predicate_property'(number_of_clauses(N), Pred) :-
893 '$get_predicate_attribute'(Pred, number_of_clauses, N).
894'$predicate_property'(number_of_rules(N), Pred) :-
895 '$get_predicate_attribute'(Pred, number_of_rules, N).
896'$predicate_property'(indexed(Indices), Pred) :-
897 '$get_predicate_attribute'(Pred, indexed, Indices).
898'$predicate_property'(noprofile, Pred) :-
899 '$get_predicate_attribute'(Pred, noprofile, 1).
900'$predicate_property'(iso, Pred) :-
901 '$get_predicate_attribute'(Pred, iso, 1).
902'$predicate_property'(quasi_quotation_syntax, Pred) :-
903 '$get_predicate_attribute'(Pred, quasi_quotation_syntax, 1).
904'$predicate_property'(defined, Pred) :-
905 '$get_predicate_attribute'(Pred, defined, 1).
906
907system_undefined(user:prolog_trace_interception/4).
908system_undefined(user:prolog_exception_hook/4).
909system_undefined(system:'$c_call_prolog'/0).
910system_undefined(system:window_title/2).
911
917
918visible_predicate(Pred) :-
919 Pred = M:Head,
920 current_module(M),
921 ( callable(Head)
922 -> ( '$get_predicate_attribute'(Pred, defined, 1)
923 -> true
924 ; \+ current_prolog_flag(M:unknown, fail),
925 functor(Head, Name, Arity),
926 '$find_library'(M, Name, Arity, _LoadModule, _Library)
927 )
928 ; setof(PI, visible_in_module(M, PI), PIs),
929 '$member'(Name/Arity, PIs),
930 functor(Head, Name, Arity)
931 ).
932
933visible_in_module(M, Name/Arity) :-
934 default_module(M, DefM),
935 DefHead = DefM:Head,
936 '$c_current_predicate'(_, DefHead),
937 '$get_predicate_attribute'(DefHead, defined, 1),
938 \+ hidden_system_predicate(Head),
939 functor(Head, Name, Arity).
940visible_in_module(_, Name/Arity) :-
941 '$in_library'(Name, Arity, _).
942
943hidden_system_predicate(Head) :-
944 functor(Head, Name, _),
945 atom(Name), 946 sub_atom(Name, 0, _, _, $),
947 \+ current_prolog_flag(access_level, system).
948
949
971
972clause_property(Clause, Property) :-
973 '$clause_property'(Property, Clause).
974
975'$clause_property'(line_count(LineNumber), Clause) :-
976 '$get_clause_attribute'(Clause, line_count, LineNumber).
977'$clause_property'(file(File), Clause) :-
978 '$get_clause_attribute'(Clause, file, File).
979'$clause_property'(source(File), Clause) :-
980 '$get_clause_attribute'(Clause, owner, File).
981'$clause_property'(size(Bytes), Clause) :-
982 '$get_clause_attribute'(Clause, size, Bytes).
983'$clause_property'(fact, Clause) :-
984 '$get_clause_attribute'(Clause, fact, true).
985'$clause_property'(erased, Clause) :-
986 '$get_clause_attribute'(Clause, erased, true).
987'$clause_property'(predicate(PI), Clause) :-
988 '$get_clause_attribute'(Clause, predicate_indicator, PI).
989'$clause_property'(module(M), Clause) :-
990 '$get_clause_attribute'(Clause, module, M).
991
992
993 996
997:- meta_predicate
998 require(:).
999
1006
1007require(M:List) :-
1008 ( is_list(List)
1009 -> require(List, M)
1010 ; throw(error(type_error(list, List), _))
1011 ).
1012
1013require([], _).
1014require([N/A|T], M) :-
1015 !,
1016 functor(Head, N, A),
1017 '$require'(M:Head),
1018 require(T, M).
1019require([H|_T], _) :-
1020 throw(error(type_error(predicate_indicator, H), _)).
1021
1022
1023 1026
1030
1031current_module(Module) :-
1032 '$current_module'(Module, _).
1033
1047
1048module_property(Module, Property) :-
1049 nonvar(Module), nonvar(Property),
1050 !,
1051 property_module(Property, Module).
1052module_property(Module, Property) :- 1053 nonvar(Property), Property = file(File),
1054 !,
1055 ( nonvar(File)
1056 -> '$current_module'(Modules, File),
1057 ( atom(Modules)
1058 -> Module = Modules
1059 ; '$member'(Module, Modules)
1060 )
1061 ; '$current_module'(Module, File),
1062 File \== []
1063 ).
1064module_property(Module, Property) :-
1065 current_module(Module),
1066 property_module(Property, Module).
1067
1068property_module(Property, Module) :-
1069 module_property(Property),
1070 ( Property = exported_operators(List)
1071 -> '$exported_ops'(Module, List, []),
1072 List \== []
1073 ; '$module_property'(Module, Property)
1074 ).
1075
1076module_property(class(_)).
1077module_property(file(_)).
1078module_property(line_count(_)).
1079module_property(exports(_)).
1080module_property(exported_operators(_)).
1081module_property(program_size(_)).
1082module_property(program_space(_)).
1083
1087
1088module(Module) :-
1089 atom(Module),
1090 current_module(Module),
1091 !,
1092 '$set_typein_module'(Module).
1093module(Module) :-
1094 '$set_typein_module'(Module),
1095 print_message(warning, no_current_module(Module)).
1096
1101
1102working_directory(Old, New) :-
1103 '$cwd'(Old),
1104 ( Old == New
1105 -> true
1106 ; '$chdir'(New)
1107 ).
1108
1109
1110 1113
1117
1118current_trie(Trie) :-
1119 current_blob(Trie, trie).
1120
1134
1135trie_property(Trie, Property) :-
1136 current_trie(Trie),
1137 trie_property(Property),
1138 '$trie_property'(Trie, Property).
1139
1140trie_property(node_count(_)).
1141trie_property(value_count(_)).
1142trie_property(size(_)).
1143trie_property(hashed(_)).
1144
1145
1146
1147 1150
1151shell(Command) :-
1152 shell(Command, 0).
1153
1158
1159:- if(current_prolog_flag(windows, true)).
1160:- export(win_add_dll_directory/1).
1161win_add_dll_directory(Dir) :-
1162 win_add_dll_directory(Dir, _),
1163 !.
1164win_add_dll_directory(Dir) :-
1165 prolog_to_os_filename(Dir, OSDir),
1166 getenv('PATH', Path0),
1167 atomic_list_concat([Path0, OSDir], ';', Path),
1168 setenv('PATH', Path).
1169:- endif.
1170
1171 1174
1175:- meta_predicate
1176 on_signal(+, :, :),
1177 current_signal(?, ?, :).
1178
1180
1181on_signal(Signal, Old, New) :-
1182 atom(Signal),
1183 !,
1184 '$on_signal'(_Num, Signal, Old, New).
1185on_signal(Signal, Old, New) :-
1186 integer(Signal),
1187 !,
1188 '$on_signal'(Signal, _Name, Old, New).
1189on_signal(Signal, _Old, _New) :-
1190 '$type_error'(signal_name, Signal).
1191
1193
1194current_signal(Name, Id, Handler) :-
1195 between(1, 32, Id),
1196 '$on_signal'(Id, Name, Handler, Handler).
1197
1198:- multifile
1199 prolog:called_by/2.
1200
1201prolog:called_by(on_signal(_,_,New), [New+1]) :-
1202 ( new == throw
1203 ; new == default
1204 ), !, fail.
1205
1206
1207 1210
1222
1223open_shared_object(File, Handle) :-
1224 open_shared_object(File, Handle, []). 1225
1226open_shared_object(File, Handle, Flags) :-
1227 ( is_list(Flags)
1228 -> true
1229 ; throw(error(type_error(list, Flags), _))
1230 ),
1231 map_dlflags(Flags, Mask),
1232 '$open_shared_object'(File, Handle, Mask).
1233
1234dlopen_flag(now, 2'01). 1235dlopen_flag(global, 2'10). 1236
1237map_dlflags([], 0).
1238map_dlflags([F|T], M) :-
1239 map_dlflags(T, M0),
1240 ( dlopen_flag(F, I)
1241 -> true
1242 ; throw(error(domain_error(dlopen_flag, F), _))
1243 ),
1244 M is M0 \/ I.
1245
1246
1247 1250
1251format(Fmt) :-
1252 format(Fmt, []).
1253
1254 1257
1259
1260absolute_file_name(Name, Abs) :-
1261 atomic(Name),
1262 !,
1263 '$absolute_file_name'(Name, Abs).
1264absolute_file_name(Term, Abs) :-
1265 '$chk_file'(Term, [''], [access(read)], true, File),
1266 !,
1267 '$absolute_file_name'(File, Abs).
1268absolute_file_name(Term, Abs) :-
1269 '$chk_file'(Term, [''], [], true, File),
1270 !,
1271 '$absolute_file_name'(File, Abs).
1272
1273
1274 1277
1284
1285garbage_collect :-
1286 '$garbage_collect'(0).
1287
1291
1292set_prolog_stack(Stack, Option) :-
1293 Option =.. [Name,Value0],
1294 Value is Value0,
1295 '$set_prolog_stack'(Stack, Name, _Old, Value).
1296
1300
1301prolog_stack_property(Stack, Property) :-
1302 stack_property(P),
1303 stack_name(Stack),
1304 Property =.. [P,Value],
1305 '$set_prolog_stack'(Stack, P, Value, Value).
1306
1307stack_name(local).
1308stack_name(global).
1309stack_name(trail).
1310
1311stack_property(limit).
1312stack_property(spare).
1313stack_property(min_free).
1314
1315
1316 1319
1320:- '$iso'((numbervars/3)).
1321
1327
1328numbervars(Term, From, To) :-
1329 numbervars(Term, From, To, []).
1330
1331
1332 1335
1339
1340term_string(Term, String, Options) :-
1341 nonvar(String),
1342 !,
1343 read_term_from_atom(String, Term, Options).
1344term_string(Term, String, Options) :-
1345 ( '$option'(quoted(_), Options)
1346 -> Options1 = Options
1347 ; '$merge_options'(_{quoted:true}, Options, Options1)
1348 ),
1349 format(string(String), '~W', [Term, Options1]).
1350
1351
1352 1355
1359
1360nb_setval(Name, Value) :-
1361 duplicate_term(Value, Copy),
1362 nb_linkval(Name, Copy).